File: std_LT03_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 (76 lines) | stat: -rw-r--r-- 2,308 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
"""Tests the python routines within LT03."""

import sqlfluff
from sqlfluff.core import Linter
from sqlfluff.core.config import FluffConfig

EXPECTED_LEADING_MESSAGE = (
    "Found trailing binary operator. Expected only leading near line breaks."
)
EXPECTED_TRAILING_MESSAGE = (
    "Found leading binary operator. Expected only trailing near line breaks."
)


def test__rules__std_LT03_default():
    """Verify that LT03 returns the correct error message for default (trailing)."""
    sql = """
        SELECT
            a,
            b
        FROM foo
        WHERE
            a = 1 AND
            b = 2
    """
    result = sqlfluff.lint(sql)
    assert "LT03" in [r["code"] for r in result]
    assert EXPECTED_LEADING_MESSAGE in [r["description"] for r in result]


def test__rules__std_LT03_leading():
    """Verify correct error message when leading is used."""
    sql = """
        SELECT
            a,
            b
        FROM foo
        WHERE
            a = 1 AND
            b = 2
    """
    config = FluffConfig(
        configs={"layout": {"type": {"binary_operator": {"line_position": "leading"}}}},
        overrides={"dialect": "ansi"},
    )
    # The sqlfluff.lint API doesn't allow us to pass config so need to do what it does
    linter = Linter(config=config)
    result_records = linter.lint_string_wrapped(sql).as_records()
    result = result_records[0]["violations"]
    assert "LT03" in [r["code"] for r in result]
    assert EXPECTED_LEADING_MESSAGE in [r["description"] for r in result]


def test__rules__std_LT03_trailing():
    """Verify correct error message when trailing is used."""
    sql = """
        SELECT
            a,
            b
        FROM foo
        WHERE
            a = 1
            AND b = 2
    """
    config = FluffConfig(
        configs={
            "layout": {"type": {"binary_operator": {"line_position": "trailing"}}}
        },
        overrides={"dialect": "ansi"},
    )
    # The sqlfluff.lint API doesn't allow us to pass config so need to do what it does
    linter = Linter(config=config)
    result_records = linter.lint_string_wrapped(sql).as_records()
    result = result_records[0]["violations"]
    assert "LT03" in [r["code"] for r in result]
    assert EXPECTED_TRAILING_MESSAGE in [r["description"] for r in result]