Skip to content

Commit

Permalink
Allow global config for rule testcases (sqlfluff#1580)
Browse files Browse the repository at this point in the history
* FIx broken block comments in exasol

* Allow adding a global test config per testcase

* Respect config per testcase

* Allow adding a global test config per testcase

* Respect config per testcase

* add testcase

* make unittest description more meaningful

Co-authored-by: Alan Cruickshank <alanmcruickshank@gmail.com>
Co-authored-by: Barry Pollard <barry@tunetheweb.com>
  • Loading branch information
3 people authored and ttomasz committed Oct 12, 2021
1 parent 75f8628 commit ee5b019
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/sqlfluff/testing/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ def load_test_cases(
y = yaml.safe_load(raw)

rule = y.pop("rule")
global_config = y.pop("configs", None)
if global_config:
for i in y:
if not ("configs" in y[i].keys()):
y[i].update({"configs": global_config})
ids.extend([rule + "_" + t for t in y])
test_cases.extend([RuleTestCase(rule=rule, **v) for k, v in y.items()])

Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/rules/R001_global_config_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
rule: R001
configs:
core:
dialect: exasol


tc1:
pass_str: |
create table if not exists tab.xxx (col1 varchar(10))
configs:
core:
dialect: ansi

tc2:
pass_str: |
create table tab.xxx (col1 varchar(10))
12 changes: 12 additions & 0 deletions test/rules/yaml_test_cases_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@ def test__rule_test_case(test_case, caplog):
assert is_fix_compatible(
rule
), f'Rule {test_case.rule} returned fixes but does not specify "@document_fix_compatible".'


def test__rule_test_global_config():
"""Test global config in rule test cases."""
ids, test_cases = load_test_cases(
os.path.join("test/fixtures/rules/R001_global_config_test.yml")
)
assert len(test_cases) == 2
# tc1: overwrites global config
assert test_cases[0].configs["core"]["dialect"] == "ansi"
# tc2: global config is used
assert test_cases[1].configs["core"]["dialect"] == "exasol"

0 comments on commit ee5b019

Please sign in to comment.