Exemplo n.º 1
0
def test__rules__std_L020_complex():
    """Verify that L020 returns the correct error message for complex example."""
    sql = """
        SELECT
            a.pk,
            b.pk
        FROM table_1 AS a
        JOIN table_2 AS a ON a.pk = a.pk
        JOIN table_3 AS b ON a.pk = b.pk
        JOIN table_4 AS b ON b.pk = b.pk
        JOIN table_5 AS a ON b.pk = a.pk
    """
    result = sqlfluff.lint(sql)
    result_filter = [r for r in result if r["code"] == "L020"]
    # Error message only show two times, not three
    assert len(result_filter) == 3
    assert (
        len(
            [
                r
                for r in result_filter
                if "Duplicate table alias 'a'" in r["description"]
            ]
        )
        == 2
    )
    assert (
        len(
            [
                r
                for r in result_filter
                if "Duplicate table alias 'b'" in r["description"]
            ]
        )
        == 1
    )
    # Test specific line number
    assert result_filter[0]["line_no"] == 6
    assert result_filter[1]["line_no"] == 8
    assert result_filter[2]["line_no"] == 9
Exemplo n.º 2
0
"""This is an example of how to use the simple sqlfluff api."""

from typing import Any, Dict, Iterator, List, Union
import sqlfluff

#  -------- LINTING ----------

my_bad_query = "SeLEct  *, 1, blah as  fOO  from mySchema.myTable"

# Lint the given string and return an array of violations in JSON representation.
lint_result = sqlfluff.lint(my_bad_query, dialect="bigquery")
# lint_result =
# [
#     {
#         "code": "L010",
#         "line_no": 1,
#         "line_pos": 1,
#         "description": "Keywords must be consistently upper case.",
#     }
#     ...
# ]

#  -------- FIXING ----------

# Fix the given string and get a string back which has been fixed.
fix_result_1 = sqlfluff.fix(my_bad_query, dialect="bigquery")
# fix_result_1 = 'SELECT  *, 1, blah AS  foo  FROM myschema.mytable\n'

# We can also fix just specific rules.
fix_result_2 = sqlfluff.fix(my_bad_query, rules=["L010"])
# fix_result_2 = 'SELECT  *, 1, blah AS  fOO  FROM mySchema.myTable'
Exemplo n.º 3
0
def test__api__lint_string_specific():
    """Basic checking of lint functionality."""
    rules = ["L014", "L009"]
    result = sqlfluff.lint(my_bad_query, rules=rules)
    # Check which rules are found
    assert all(elem["code"] in rules for elem in result)
Exemplo n.º 4
0
def test__api__lint_file():
    """Basic checking of lint functionality from a file object."""
    string_buffer = io.StringIO(my_bad_query)
    result = sqlfluff.lint(string_buffer)
    # Check actual result
    assert result == lint_result
Exemplo n.º 5
0
def test__api__lint_string_without_violations():
    """Check lint functionality when there is no violation."""
    result = sqlfluff.lint("select column from table\n")
    assert result == []
Exemplo n.º 6
0
def test__api__lint_string_specific_exclude_all_failed_rules():
    """Basic checking of lint functionality."""
    exclude_rules = ["L009", "L010", "L013", "L014", "L036", "L039", "L044"]
    result = sqlfluff.lint(my_bad_query, exclude_rules=exclude_rules)
    # Check it passes
    assert result == []
Exemplo n.º 7
0
def test__rules__std_L016_L036_long_line_lint():
    """Verify that a long line that causes a clash between L016 and L036 is not changed."""
    sql = "SELECT\n1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n"
    result = sqlfluff.lint(sql)
    assert "L016" in [r["code"] for r in result]
    assert "L036" in [r["code"] for r in result]