def test__rules__std_L003_L036_L039(): """Verify that double indents don't flag L039.""" sql = """ WITH example AS ( SELECT my_id, other_thing, one_more FROM my_table ) SELECT * FROM example\n""" fixed_sql = """ WITH example AS ( SELECT my_id, other_thing, one_more FROM my_table ) SELECT * FROM example\n""" result = sqlfluff.fix(sql, exclude_rules=["L050"]) assert result == fixed_sql
def test__api__fix_string(): """Basic checking of lint functionality.""" result = sqlfluff.fix(my_bad_query) # Check return types. assert isinstance(result, str) # Check actual result assert result == "SELECT *, 1, blah AS foo FROM mytable\n"
def test__api__fix_string_unparsable(): """Test behavior with parse errors.""" bad_query = """SELECT my_col FROM my_schema.my_table where processdate ! 3""" result = sqlfluff.fix(bad_query, rules=["L010"]) # Check fix result: should be unchanged because of the parse error. assert result == bad_query
def test__rules__std_L016_L036_long_line_fix2(): """Verify that a long line that causes a clash between L016 and L036 does not add multiple newlines (see #1424).""" sql = "SELECT\n 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n" result = sqlfluff.fix(sql) assert ( result == "SELECT 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n" )
def test__rules__std_L016_L036_long_line_fix2(): """Verify clash between L016 & L036 does not add multiple newlines (see #1424).""" sql = ( "SELECT\n 100000000000000000000000000000000000000000000000000000000000000000" "0000000000000000000000000000000000\n") result = sqlfluff.fix(sql) assert result == ( "SELECT 10000000000000000000000000000000000000000000000000000000000000000000000" "00000000000000000000000000000\n")
def test__api__fix_string_unparsable_fix_even_unparsable(): """Test behavior with parse errors.""" bad_query = """SELECT my_col FROM my_schema.my_table where processdate ! 3""" result = sqlfluff.fix(bad_query, rules=["L010"], fix_even_unparsable=True) # Check fix result: should be fixed because we overrode fix_even_unparsable. assert (result == """SELECT my_col FROM my_schema.my_table WHERE processdate ! 3""")
def test__rules__std_L003_L065_union_all_in_subquery_fix(): """Verify combination of rules L003 and L065 produces a correct indentation.""" sql = ( "SELECT c FROM (\n" " SELECT 'g' UNION ALL\n" " SELECT 'h'\n" " UNION ALL SELECT 'j'\n" ")\n" ) fixed_sql = ( "SELECT c FROM (\n" " SELECT 'g'\n" " UNION ALL\n" " SELECT 'h'\n" " UNION ALL\n" " SELECT 'j'\n" ")\n" ) result = sqlfluff.fix(sql) assert result == fixed_sql
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' # Or a subset of rules... fix_result_3 = sqlfluff.fix(my_bad_query, rules=["L010", "L014"]) # fix_result_3 = 'SELECT *, 1, blah AS fOO FROM myschema.mytable' # -------- PARSING ---------- # Parse the given string and return a JSON representation of the parsed tree. parse_result = sqlfluff.parse(my_bad_query) # parse_result = {'file': {'statement': {...}, 'newline': '\n'}}
def test__api__fix_string_specific(): """Basic checking of lint functionality with a specific rule.""" result = sqlfluff.fix(my_bad_query, rules="L010") # Check actual result assert result == "SELECT *, 1, blah AS fOO FROM myTable"
def test__api__fix_string_specific_exclude(): """Basic checking of lint functionality with a specific rule exclusion.""" result = sqlfluff.fix(my_bad_query, exclude_rules=["L036"]) # Check actual result assert result == "SELECT *, 1, blah AS foo FROM mytable\n"
my_bad_query = "SeLEct *, 1, blah as fOO from myTable" # Lint the given string and get a list of violations found. result = sqlfluff.lint(my_bad_query, dialect="bigquery") # 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. result = sqlfluff.fix(my_bad_query, dialect="bigquery") # result = 'SELECT *, 1, blah AS foo FROM mytable\n' # We can also fix just specific rules. result = sqlfluff.fix(my_bad_query, rules="L010") # result = 'SELECT *, 1, blah AS fOO FROM myTable' # Or a subset of rules... result = sqlfluff.fix(my_bad_query, rules=["L010", "L014"]) # result = 'SELECT *, 1, blah AS fOO FROM mytable' # -------- PARSING ---------- # NOTE: sqlfluff is still in a relatively early phase of its # development and so until version 1.0.0 will offer no guarantee # that the names and structure of the objects returned by these # parse commands won't change between releases. Use with care