Coverage for tests / unit / tools / sqlfluff / test_options.py: 100%

103 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-04-03 18:53 +0000

1"""Unit tests for sqlfluff plugin default options and set_options validation.""" 

2 

3from __future__ import annotations 

4 

5import pytest 

6from assertpy import assert_that 

7 

8from lintro.tools.definitions.sqlfluff import ( 

9 SQLFLUFF_DEFAULT_TIMEOUT, 

10 SqlfluffPlugin, 

11) 

12 

13# Tests for default option values 

14 

15 

16@pytest.mark.parametrize( 

17 ("option_name", "expected_value"), 

18 [ 

19 ("timeout", SQLFLUFF_DEFAULT_TIMEOUT), 

20 ("dialect", None), 

21 ("exclude_rules", None), 

22 ("rules", None), 

23 ("templater", None), 

24 ], 

25 ids=[ 

26 "timeout_equals_default", 

27 "dialect_is_none", 

28 "exclude_rules_is_none", 

29 "rules_is_none", 

30 "templater_is_none", 

31 ], 

32) 

33def test_default_options_values( 

34 sqlfluff_plugin: SqlfluffPlugin, 

35 option_name: str, 

36 expected_value: object, 

37) -> None: 

38 """Default options have correct values. 

39 

40 Args: 

41 sqlfluff_plugin: The SqlfluffPlugin instance to test. 

42 option_name: The name of the option to check. 

43 expected_value: The expected value for the option. 

44 """ 

45 assert_that( 

46 sqlfluff_plugin.definition.default_options[option_name], 

47 ).is_equal_to(expected_value) 

48 

49 

50# Tests for SqlfluffPlugin.set_options method - valid options 

51 

52 

53@pytest.mark.parametrize( 

54 ("option_name", "option_value"), 

55 [ 

56 ("dialect", "postgres"), 

57 ("dialect", "mysql"), 

58 ("dialect", "bigquery"), 

59 ("dialect", "snowflake"), 

60 ("exclude_rules", ["L001"]), 

61 ("exclude_rules", ["L001", "L002"]), 

62 ("rules", ["L002"]), 

63 ("rules", ["L002", "L003"]), 

64 ("templater", "jinja"), 

65 ("templater", "raw"), 

66 ("templater", "python"), 

67 ], 

68 ids=[ 

69 "dialect_postgres", 

70 "dialect_mysql", 

71 "dialect_bigquery", 

72 "dialect_snowflake", 

73 "exclude_rules_single", 

74 "exclude_rules_multiple", 

75 "rules_single", 

76 "rules_multiple", 

77 "templater_jinja", 

78 "templater_raw", 

79 "templater_python", 

80 ], 

81) 

82def test_set_options_valid( 

83 sqlfluff_plugin: SqlfluffPlugin, 

84 option_name: str, 

85 option_value: object, 

86) -> None: 

87 """Set valid options correctly. 

88 

89 Args: 

90 sqlfluff_plugin: The SqlfluffPlugin instance to test. 

91 option_name: The name of the option to set. 

92 option_value: The value to set for the option. 

93 """ 

94 sqlfluff_plugin.set_options(**{option_name: option_value}) # type: ignore[arg-type] 

95 assert_that(sqlfluff_plugin.options.get(option_name)).is_equal_to(option_value) 

96 

97 

98# Tests for SqlfluffPlugin.set_options method - invalid types 

99 

100 

101@pytest.mark.parametrize( 

102 ("option_name", "invalid_value", "error_match"), 

103 [ 

104 ("dialect", 123, "dialect must be a string"), 

105 ("dialect", ["postgres"], "dialect must be a string"), 

106 ("exclude_rules", "L001", "exclude_rules must be a list"), 

107 ("exclude_rules", 123, "exclude_rules must be a list"), 

108 ("rules", "L002", "rules must be a list"), 

109 ("rules", 123, "rules must be a list"), 

110 ("templater", 123, "templater must be a string"), 

111 ("templater", ["jinja"], "templater must be a string"), 

112 ], 

113 ids=[ 

114 "invalid_dialect_int", 

115 "invalid_dialect_list", 

116 "invalid_exclude_rules_str", 

117 "invalid_exclude_rules_int", 

118 "invalid_rules_str", 

119 "invalid_rules_int", 

120 "invalid_templater_int", 

121 "invalid_templater_list", 

122 ], 

123) 

124def test_set_options_invalid_type( 

125 sqlfluff_plugin: SqlfluffPlugin, 

126 option_name: str, 

127 invalid_value: object, 

128 error_match: str, 

129) -> None: 

130 """Raise ValueError for invalid option types. 

131 

132 Args: 

133 sqlfluff_plugin: The SqlfluffPlugin instance to test. 

134 option_name: The name of the option being tested. 

135 invalid_value: An invalid value for the option. 

136 error_match: Pattern expected in the error message. 

137 """ 

138 with pytest.raises(ValueError, match=error_match): 

139 sqlfluff_plugin.set_options(**{option_name: invalid_value}) # type: ignore[arg-type] 

140 

141 

142# Tests for SqlfluffPlugin._build_lint_command method 

143 

144 

145def test_build_lint_command_basic(sqlfluff_plugin: SqlfluffPlugin) -> None: 

146 """Build basic lint command without extra options. 

147 

148 Args: 

149 sqlfluff_plugin: The SqlfluffPlugin instance to test. 

150 """ 

151 from lintro.tools.definitions.sqlfluff import SQLFLUFF_DEFAULT_FORMAT 

152 

153 cmd = sqlfluff_plugin._build_lint_command(files=["test.sql"]) 

154 

155 assert_that(cmd).contains("sqlfluff") 

156 assert_that(cmd).contains("lint") 

157 assert_that(cmd).contains("--format") 

158 assert_that(cmd).contains(SQLFLUFF_DEFAULT_FORMAT) 

159 assert_that(cmd).contains("test.sql") 

160 

161 

162def test_build_lint_command_with_dialect(sqlfluff_plugin: SqlfluffPlugin) -> None: 

163 """Build lint command with dialect option. 

164 

165 Args: 

166 sqlfluff_plugin: The SqlfluffPlugin instance to test. 

167 """ 

168 sqlfluff_plugin.set_options(dialect="postgres") 

169 cmd = sqlfluff_plugin._build_lint_command(files=["test.sql"]) 

170 

171 assert_that(cmd).contains("--dialect") 

172 dialect_idx = cmd.index("--dialect") 

173 assert_that(cmd[dialect_idx + 1]).is_equal_to("postgres") 

174 

175 

176def test_build_lint_command_with_exclude_rules( 

177 sqlfluff_plugin: SqlfluffPlugin, 

178) -> None: 

179 """Build lint command with exclude_rules option. 

180 

181 Args: 

182 sqlfluff_plugin: The SqlfluffPlugin instance to test. 

183 """ 

184 sqlfluff_plugin.set_options(exclude_rules=["L001", "L002"]) 

185 cmd = sqlfluff_plugin._build_lint_command(files=["test.sql"]) 

186 

187 assert_that(cmd).contains("--exclude-rules") 

188 # Rules should be comma-separated per SQLFluff CLI docs 

189 exclude_idx = cmd.index("--exclude-rules") 

190 assert_that(cmd[exclude_idx + 1]).is_equal_to("L001,L002") 

191 

192 

193def test_build_lint_command_with_rules(sqlfluff_plugin: SqlfluffPlugin) -> None: 

194 """Build lint command with rules option. 

195 

196 Args: 

197 sqlfluff_plugin: The SqlfluffPlugin instance to test. 

198 """ 

199 sqlfluff_plugin.set_options(rules=["L002", "L003"]) 

200 cmd = sqlfluff_plugin._build_lint_command(files=["test.sql"]) 

201 

202 assert_that(cmd).contains("--rules") 

203 # Rules should be comma-separated per SQLFluff CLI docs 

204 rules_idx = cmd.index("--rules") 

205 assert_that(cmd[rules_idx + 1]).is_equal_to("L002,L003") 

206 

207 

208def test_build_lint_command_with_templater(sqlfluff_plugin: SqlfluffPlugin) -> None: 

209 """Build lint command with templater option. 

210 

211 Args: 

212 sqlfluff_plugin: The SqlfluffPlugin instance to test. 

213 """ 

214 sqlfluff_plugin.set_options(templater="jinja") 

215 cmd = sqlfluff_plugin._build_lint_command(files=["test.sql"]) 

216 

217 assert_that(cmd).contains("--templater") 

218 templater_idx = cmd.index("--templater") 

219 assert_that(cmd[templater_idx + 1]).is_equal_to("jinja") 

220 

221 

222def test_build_lint_command_with_all_options(sqlfluff_plugin: SqlfluffPlugin) -> None: 

223 """Build lint command with all options set. 

224 

225 Args: 

226 sqlfluff_plugin: The SqlfluffPlugin instance to test. 

227 """ 

228 sqlfluff_plugin.set_options( 

229 dialect="postgres", 

230 exclude_rules=["L001"], 

231 rules=["L002"], 

232 templater="jinja", 

233 ) 

234 cmd = sqlfluff_plugin._build_lint_command(files=["test.sql"]) 

235 

236 assert_that(cmd).contains("--dialect") 

237 assert_that(cmd).contains("--exclude-rules") 

238 assert_that(cmd).contains("--rules") 

239 assert_that(cmd).contains("--templater") 

240 assert_that(cmd).contains("test.sql") 

241 

242 

243def test_build_lint_command_multiple_files(sqlfluff_plugin: SqlfluffPlugin) -> None: 

244 """Build lint command with multiple files. 

245 

246 Args: 

247 sqlfluff_plugin: The SqlfluffPlugin instance to test. 

248 """ 

249 cmd = sqlfluff_plugin._build_lint_command(files=["test1.sql", "test2.sql"]) 

250 

251 assert_that(cmd).contains("test1.sql") 

252 assert_that(cmd).contains("test2.sql") 

253 

254 

255# Tests for SqlfluffPlugin._build_fix_command method 

256 

257 

258def test_build_fix_command_basic(sqlfluff_plugin: SqlfluffPlugin) -> None: 

259 """Build basic fix command without extra options. 

260 

261 Args: 

262 sqlfluff_plugin: The SqlfluffPlugin instance to test. 

263 """ 

264 cmd = sqlfluff_plugin._build_fix_command(files=["test.sql"]) 

265 

266 assert_that(cmd).contains("sqlfluff") 

267 assert_that(cmd).contains("fix") 

268 assert_that(cmd).contains("--force") 

269 assert_that(cmd).contains("test.sql") 

270 

271 

272def test_build_fix_command_with_dialect(sqlfluff_plugin: SqlfluffPlugin) -> None: 

273 """Build fix command with dialect option. 

274 

275 Args: 

276 sqlfluff_plugin: The SqlfluffPlugin instance to test. 

277 """ 

278 sqlfluff_plugin.set_options(dialect="mysql") 

279 cmd = sqlfluff_plugin._build_fix_command(files=["test.sql"]) 

280 

281 assert_that(cmd).contains("--dialect") 

282 dialect_idx = cmd.index("--dialect") 

283 assert_that(cmd[dialect_idx + 1]).is_equal_to("mysql") 

284 

285 

286def test_build_fix_command_with_exclude_rules( 

287 sqlfluff_plugin: SqlfluffPlugin, 

288) -> None: 

289 """Build fix command with exclude_rules option. 

290 

291 Args: 

292 sqlfluff_plugin: The SqlfluffPlugin instance to test. 

293 """ 

294 sqlfluff_plugin.set_options(exclude_rules=["L001"]) 

295 cmd = sqlfluff_plugin._build_fix_command(files=["test.sql"]) 

296 

297 assert_that(cmd).contains("--exclude-rules") 

298 

299 

300def test_build_fix_command_with_rules(sqlfluff_plugin: SqlfluffPlugin) -> None: 

301 """Build fix command with rules option. 

302 

303 Args: 

304 sqlfluff_plugin: The SqlfluffPlugin instance to test. 

305 """ 

306 sqlfluff_plugin.set_options(rules=["L002"]) 

307 cmd = sqlfluff_plugin._build_fix_command(files=["test.sql"]) 

308 

309 assert_that(cmd).contains("--rules") 

310 

311 

312def test_build_fix_command_with_templater(sqlfluff_plugin: SqlfluffPlugin) -> None: 

313 """Build fix command with templater option. 

314 

315 Args: 

316 sqlfluff_plugin: The SqlfluffPlugin instance to test. 

317 """ 

318 sqlfluff_plugin.set_options(templater="raw") 

319 cmd = sqlfluff_plugin._build_fix_command(files=["test.sql"]) 

320 

321 assert_that(cmd).contains("--templater") 

322 templater_idx = cmd.index("--templater") 

323 assert_that(cmd[templater_idx + 1]).is_equal_to("raw") 

324 

325 

326def test_build_fix_command_with_all_options(sqlfluff_plugin: SqlfluffPlugin) -> None: 

327 """Build fix command with all options set. 

328 

329 Args: 

330 sqlfluff_plugin: The SqlfluffPlugin instance to test. 

331 """ 

332 sqlfluff_plugin.set_options( 

333 dialect="bigquery", 

334 exclude_rules=["L001"], 

335 rules=["L002"], 

336 templater="python", 

337 ) 

338 cmd = sqlfluff_plugin._build_fix_command(files=["test.sql"]) 

339 

340 assert_that(cmd).contains("--dialect") 

341 assert_that(cmd).contains("--exclude-rules") 

342 assert_that(cmd).contains("--rules") 

343 assert_that(cmd).contains("--templater") 

344 assert_that(cmd).contains("--force") 

345 assert_that(cmd).contains("test.sql") 

346 

347 

348# Tests for plugin definition 

349 

350 

351def test_plugin_definition_name(sqlfluff_plugin: SqlfluffPlugin) -> None: 

352 """Plugin definition has correct name. 

353 

354 Args: 

355 sqlfluff_plugin: The SqlfluffPlugin instance to test. 

356 """ 

357 assert_that(sqlfluff_plugin.definition.name).is_equal_to("sqlfluff") 

358 

359 

360def test_plugin_definition_can_fix(sqlfluff_plugin: SqlfluffPlugin) -> None: 

361 """Plugin definition indicates it can fix issues. 

362 

363 Args: 

364 sqlfluff_plugin: The SqlfluffPlugin instance to test. 

365 """ 

366 assert_that(sqlfluff_plugin.definition.can_fix).is_true() 

367 

368 

369def test_plugin_definition_file_patterns(sqlfluff_plugin: SqlfluffPlugin) -> None: 

370 """Plugin definition has correct file patterns. 

371 

372 Args: 

373 sqlfluff_plugin: The SqlfluffPlugin instance to test. 

374 """ 

375 assert_that(sqlfluff_plugin.definition.file_patterns).contains("*.sql") 

376 

377 

378def test_plugin_definition_native_configs(sqlfluff_plugin: SqlfluffPlugin) -> None: 

379 """Plugin definition has correct native config files. 

380 

381 Args: 

382 sqlfluff_plugin: The SqlfluffPlugin instance to test. 

383 """ 

384 assert_that(sqlfluff_plugin.definition.native_configs).contains(".sqlfluff") 

385 assert_that(sqlfluff_plugin.definition.native_configs).contains("pyproject.toml")