File: std_LT02_LT04_test.py

package info (click to toggle)
sqlfluff 3.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 34,000 kB
  • sloc: python: 106,131; sql: 34,188; makefile: 52; sh: 8
file content (74 lines) | stat: -rw-r--r-- 1,528 bytes parent folder | download | duplicates (3)
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
"""Tests the python routines within LT02 and LT04."""

import pytest

from sqlfluff.core import FluffConfig, Linter


@pytest.mark.parametrize(
    ["in_sql", "out_sql"],
    [
        (
            """SELECT
    acct_id,
    date_x,
    't' AS test,

    CASE
        WHEN condition_1 = '1' THEN ''
        ELSE condition_1
    END AS case_1,

    CASE
        WHEN condition_2 = '2' THEN ''
        ELSE condition_2
    END AS case_2,
    dollar_amt,
FROM
    table_x""",
            """SELECT
    acct_id
    , date_x
    , 't' AS test

    , CASE
        WHEN condition_1 = '1' THEN ''
        ELSE condition_1
    END AS case_1

    , CASE
        WHEN condition_2 = '2' THEN ''
        ELSE condition_2
    END AS case_2
    , dollar_amt,
FROM
    table_x""",
        ),
    ],
)
def test_rules_std_LT02_LT04_interaction_indentation_leading(in_sql, out_sql) -> None:
    """Test interaction between LT02 and LT04.

    Test sql with two newlines with trailing commas expecting leading.
    """
    # Lint expected rules.
    cfg = FluffConfig.from_string(
        """[sqlfluff]
dialect = snowflake
rules = LT02, LT04

[sqlfluff:layout:type:comma]
spacing_before = touch
line_position = leading
"""
    )
    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"}

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