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
« 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."""
3from __future__ import annotations
5import pytest
6from assertpy import assert_that
8from lintro.tools.definitions.sqlfluff import (
9 SQLFLUFF_DEFAULT_TIMEOUT,
10 SqlfluffPlugin,
11)
13# Tests for default option values
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.
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)
50# Tests for SqlfluffPlugin.set_options method - valid options
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.
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)
98# Tests for SqlfluffPlugin.set_options method - invalid types
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.
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]
142# Tests for SqlfluffPlugin._build_lint_command method
145def test_build_lint_command_basic(sqlfluff_plugin: SqlfluffPlugin) -> None:
146 """Build basic lint command without extra options.
148 Args:
149 sqlfluff_plugin: The SqlfluffPlugin instance to test.
150 """
151 from lintro.tools.definitions.sqlfluff import SQLFLUFF_DEFAULT_FORMAT
153 cmd = sqlfluff_plugin._build_lint_command(files=["test.sql"])
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")
162def test_build_lint_command_with_dialect(sqlfluff_plugin: SqlfluffPlugin) -> None:
163 """Build lint command with dialect option.
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"])
171 assert_that(cmd).contains("--dialect")
172 dialect_idx = cmd.index("--dialect")
173 assert_that(cmd[dialect_idx + 1]).is_equal_to("postgres")
176def test_build_lint_command_with_exclude_rules(
177 sqlfluff_plugin: SqlfluffPlugin,
178) -> None:
179 """Build lint command with exclude_rules option.
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"])
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")
193def test_build_lint_command_with_rules(sqlfluff_plugin: SqlfluffPlugin) -> None:
194 """Build lint command with rules option.
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"])
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")
208def test_build_lint_command_with_templater(sqlfluff_plugin: SqlfluffPlugin) -> None:
209 """Build lint command with templater option.
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"])
217 assert_that(cmd).contains("--templater")
218 templater_idx = cmd.index("--templater")
219 assert_that(cmd[templater_idx + 1]).is_equal_to("jinja")
222def test_build_lint_command_with_all_options(sqlfluff_plugin: SqlfluffPlugin) -> None:
223 """Build lint command with all options set.
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"])
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")
243def test_build_lint_command_multiple_files(sqlfluff_plugin: SqlfluffPlugin) -> None:
244 """Build lint command with multiple files.
246 Args:
247 sqlfluff_plugin: The SqlfluffPlugin instance to test.
248 """
249 cmd = sqlfluff_plugin._build_lint_command(files=["test1.sql", "test2.sql"])
251 assert_that(cmd).contains("test1.sql")
252 assert_that(cmd).contains("test2.sql")
255# Tests for SqlfluffPlugin._build_fix_command method
258def test_build_fix_command_basic(sqlfluff_plugin: SqlfluffPlugin) -> None:
259 """Build basic fix command without extra options.
261 Args:
262 sqlfluff_plugin: The SqlfluffPlugin instance to test.
263 """
264 cmd = sqlfluff_plugin._build_fix_command(files=["test.sql"])
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")
272def test_build_fix_command_with_dialect(sqlfluff_plugin: SqlfluffPlugin) -> None:
273 """Build fix command with dialect option.
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"])
281 assert_that(cmd).contains("--dialect")
282 dialect_idx = cmd.index("--dialect")
283 assert_that(cmd[dialect_idx + 1]).is_equal_to("mysql")
286def test_build_fix_command_with_exclude_rules(
287 sqlfluff_plugin: SqlfluffPlugin,
288) -> None:
289 """Build fix command with exclude_rules option.
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"])
297 assert_that(cmd).contains("--exclude-rules")
300def test_build_fix_command_with_rules(sqlfluff_plugin: SqlfluffPlugin) -> None:
301 """Build fix command with rules option.
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"])
309 assert_that(cmd).contains("--rules")
312def test_build_fix_command_with_templater(sqlfluff_plugin: SqlfluffPlugin) -> None:
313 """Build fix command with templater option.
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"])
321 assert_that(cmd).contains("--templater")
322 templater_idx = cmd.index("--templater")
323 assert_that(cmd[templater_idx + 1]).is_equal_to("raw")
326def test_build_fix_command_with_all_options(sqlfluff_plugin: SqlfluffPlugin) -> None:
327 """Build fix command with all options set.
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"])
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")
348# Tests for plugin definition
351def test_plugin_definition_name(sqlfluff_plugin: SqlfluffPlugin) -> None:
352 """Plugin definition has correct name.
354 Args:
355 sqlfluff_plugin: The SqlfluffPlugin instance to test.
356 """
357 assert_that(sqlfluff_plugin.definition.name).is_equal_to("sqlfluff")
360def test_plugin_definition_can_fix(sqlfluff_plugin: SqlfluffPlugin) -> None:
361 """Plugin definition indicates it can fix issues.
363 Args:
364 sqlfluff_plugin: The SqlfluffPlugin instance to test.
365 """
366 assert_that(sqlfluff_plugin.definition.can_fix).is_true()
369def test_plugin_definition_file_patterns(sqlfluff_plugin: SqlfluffPlugin) -> None:
370 """Plugin definition has correct file patterns.
372 Args:
373 sqlfluff_plugin: The SqlfluffPlugin instance to test.
374 """
375 assert_that(sqlfluff_plugin.definition.file_patterns).contains("*.sql")
378def test_plugin_definition_native_configs(sqlfluff_plugin: SqlfluffPlugin) -> None:
379 """Plugin definition has correct native config files.
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")