File: std_LT04_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 (38 lines) | stat: -rw-r--r-- 1,394 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
"""Tests the python routines within LT04."""

import sqlfluff


def test__rules__std_LT04_unparseable():
    """Verify that LT04 doesn't try to fix queries with parse errors.

    This has been observed to frequently cause syntax errors, especially in
    combination with Jinja templating, e.g. undefined template variables.
    """
    # This example comes almost directly from a real-world example. The user
    # accidentally ran "sqlfluff fix" without defining
    # "readability_features_numeric" and "readability_features_count_list", and
    # doing so corrupted their query.
    sql = """
        SELECT
          user_id,
          campaign_id,
          business_type,
          SPLIT(intents, ",") AS intent_list,
          {% for feature in readability_features_numeric %}
            CAST(JSON_EXTRACT(readability_scores,
            '$.data.{{feature}}') AS float64) AS {{feature}} {% if not loop.last %} ,
            {% endif %}
          {% endfor %},
          {% for feature in readability_features_count_list %}
            CAST(JSON_EXTRACT(asset_structure,
            '$.{{feature}}') AS float64) AS {{feature}}_count {% if not loop.last %} ,
            {% endif %}
          {% endfor %},
            track_clicks_text,
            track_clicks_html
        FROM
          t
    """
    result = sqlfluff.lint(sql)
    assert "LT04" not in [r["code"] for r in result]