File: std_LT04_ST06_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 (112 lines) | stat: -rw-r--r-- 1,980 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
"""Tests the python routines within LT04 and ST06."""

import pytest

from sqlfluff.core import FluffConfig, Linter


@pytest.mark.parametrize(
    ["in_sql", "out_sql"],
    [
        (
            """SELECT COALESCE(a, 0) AS b

    , COALESCE(c, 0) AS d
    , e
FROM t""",
            """SELECT e,

    COALESCE(a, 0) AS b,
    COALESCE(c, 0) AS d
FROM t""",
        ),
        (
            """SELECT COALESCE(a, 0) AS b--comment

    , COALESCE(c, 0) AS d
    , e
FROM t""",
            """SELECT e,--comment

    COALESCE(a, 0) AS b,
    COALESCE(c, 0) AS d
FROM t""",
        ),
        (
            """with cte1 as (
select "a"
      ,"b"
      ,coalesce("g1"
               ,"g2"
               ,"g3"
       ) as "g_combined"

      ,"i"
      ,"j"
  from test
),

cte2 as (
select "col1"

      ,'start: ' + "col2" as "new_col2"

      ,'start2: ' + "col3" as "new_col3"

      ,"col4"
      ,"col5"
from cte1
),

select * from cte2""",
            """with cte1 as (
select "a",
      "b",
      "i",

      "j",
      coalesce("g1",
               "g2",
               "g3"
       ) as "g_combined"
  from test
),

cte2 as (
select "col1",

      "col4",

      "col5",

      'start: ' + "col2" as "new_col2",
      'start2: ' + "col3" as "new_col3"
from cte1
),

select * from cte2""",
        ),
    ],
)
def test_rules_std_LT04_and_ST06_interaction_trailing(in_sql, out_sql) -> None:
    """Test interaction between LT04 and ST06.

    Test sql with two newlines with leading commas expecting trailing.
    """
    # Lint expected rules.
    cfg = FluffConfig.from_string(
        """[sqlfluff]
dialect = ansi
rules = LT04, ST06
"""
    )
    linter = Linter(config=cfg)

    # Return linted/fixed file.
    linted_file = linter.lint_string(in_sql, fix=True)

    # Check expected lint errors are raised.
    assert set([v.rule.code for v in linted_file.violations]) == {"LT04", "ST06"}

    # Check file is fixed.
    assert linted_file.fix_string()[0] == out_sql