File: std_AL09_CP02_RF06_combo_test.py

package info (click to toggle)
sqlfluff 3.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,000 kB
  • sloc: python: 106,131; sql: 34,188; makefile: 52; sh: 8
file content (251 lines) | stat: -rw-r--r-- 4,784 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
"""Test the interactions of AL09, CP02 & RF06.

AL09: Self aliasing
CP02: Identifier Capitalisation
RF06: Identifier Quoting
"""

import pytest

from sqlfluff.core import Linter

input_query = """
select
    a as A,
    B as b,
    "C" as C,
    "d" as d,
    "E" as e,
    "f" as F,
    g as "G",
    h as h,
    I as I
from foo
"""


@pytest.mark.parametrize(
    "rules,dialect,fixed_sql,post_fix_errors",
    [
        # NOTE: The first few examples here are with ANSI which is
        # configured as a natively UPPERCASE dialect.
        (
            ["AL09"],
            "ansi",
            """
select
    a as A,
    B as b,
    "C" as C,
    "d" as d,
    "E" as e,
    "f" as F,
    g as "G",
    h,
    I
from foo
""",
            [
                # These two (A & B) are detected as self aliases, but not
                # auto-fixed, because the intent is ambiguous.
                # Should the alias/reference be quoted or removed?
                ("AL09", 3, 5),
                ("AL09", 4, 5),
            ],
        ),
        (
            ["CP02"],
            "ansi",
            """
select
    a as a,
    b as b,
    "C" as c,
    "d" as d,
    "E" as e,
    "f" as f,
    g as "G",
    h as h,
    i as i
from foo
""",
            [],
        ),
        (
            ["RF06"],
            "ansi",
            """
select
    a as A,
    B as b,
    C as C,
    "d" as d,
    E as e,
    "f" as F,
    g as G,
    h as h,
    I as I
from foo
""",
            [],
        ),
        (
            ["AL09", "CP02"],
            "ansi",
            """
select
    a,
    b,
    "C" as c,
    "d" as d,
    "E" as e,
    "f" as f,
    g as "G",
    h,
    i
from foo
""",
            # NOTE: When CP02 is active, AL09 errors are no longer
            # present, because CP02 allowed them to be resolved.
            [],
        ),
        (
            ["AL09", "RF06"],
            "ansi",
            """
select
    a as A,
    B as b,
    C,
    "d" as d,
    E as e,
    "f" as F,
    g as G,
    h,
    I
from foo
""",
            [
                # Without CPO2, the errors on line 3 & 5 are present. They're
                # detected as self-aliases, but with ambiguous fixes (A & B).
                ("AL09", 3, 5),
                ("AL09", 4, 5),
                # Additionally, with RF06 removing quotes, it creates two
                # new issues, where the previously quoted aliases are now
                # unquoted, but still different cases (E & G).
                ("AL09", 7, 5),
                ("AL09", 9, 5),
            ],
        ),
        (
            ["CP02", "RF06"],
            "ansi",
            """
select
    a as a,
    b as b,
    c as c,
    "d" as d,
    e as e,
    "f" as f,
    g as g,
    h as h,
    i as i
from foo
""",
            [],
        ),
        (
            ["AL09", "CP02", "RF06"],
            "ansi",
            """
select
    a,
    b,
    c,
    "d" as d,
    e,
    "f" as f,
    g,
    h,
    i
from foo
""",
            [],
        ),
        # Postgres is natively lowercase, and so the results are
        # different.
        (
            ["AL09", "CP02", "RF06"],
            "postgres",
            """
select
    a,
    b,
    "C" as c,
    d,
    "E" as e,
    f,
    g as "G",
    h,
    i
from foo
""",
            [],
        ),
        # DuckDB is always case insensitive so likewise has a different result.
        (
            ["AL09", "CP02", "RF06"],
            "duckdb",
            """
select
    a,
    b,
    c,
    d,
    e,
    f,
    g,
    h,
    i
from foo
""",
            [],
        ),
        # Clickhouse is always case sensitive has a more conservative result.
        # All the quotes are gone, but all the aliases with a case change remain.
        (
            # NOTE: Testing without CP02 as that rule is much less appropriate
            # for clickhouse.
            ["AL09", "RF06"],
            "clickhouse",
            """
select
    a as A,
    B as b,
    C,
    d,
    E as e,
    f as F,
    g as G,
    h,
    I
from foo
""",
            # None of those aliases should be flagged as an issue.
            [],
        ),
    ],
)
def test__rules__std_AL09_CP02_RF06(rules, dialect, fixed_sql, post_fix_errors):
    """Test interactions between AL09, CP02 & RF06."""
    print(f"Running with rules: {rules}")
    linter = Linter(dialect=dialect, rules=rules)
    result = linter.lint_string(input_query, fix=True)
    fixed, _ = result.fix_string()
    assert fixed == fixed_sql
    # Check violations after fix.
    # NOTE: We should really use the rules testing utilities here
    # but they don't yet support multiple rules.
    post_fix_result = linter.lint_string(fixed, fix=False)
    assert post_fix_result.check_tuples() == post_fix_errors