Skip to content

Commit

Permalink
fix test lint
Browse files Browse the repository at this point in the history
  • Loading branch information
eschutho committed Dec 21, 2021
1 parent 398a01f commit dc40fe1
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 72 deletions.
2 changes: 0 additions & 2 deletions superset/common/query_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
from superset import app, db
from superset.connectors.base.models import BaseDatasource
from superset.connectors.connector_registry import ConnectorRegistry
from superset.exceptions import QueryObjectValidationError
from superset.typing import Metric, OrderBy
from superset.exceptions import (
QueryClauseValidationException,
QueryObjectValidationError,
Expand Down
1 change: 0 additions & 1 deletion superset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
from flask import Blueprint
from flask_appbuilder.security.manager import AUTH_DB
from pandas.io.parsers import STR_NA_VALUES
from typing_extensions import Literal
from werkzeug.local import LocalProxy

from superset.jinja_context import BaseTemplateProcessor
Expand Down
2 changes: 0 additions & 2 deletions tests/integration_tests/charts/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2118,5 +2118,3 @@ def test_chart_data_virtual_table_with_colons(self):
assert "':asdf'" in result["query"]
assert "':xyz:qwerty'" in result["query"]
assert "':qwerty:'" in result["query"]


77 changes: 10 additions & 67 deletions tests/unit_tests/sql_parse_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
# specific language governing permissions and limitations
# under the License.

from typing import Set
from superset.sql_parse import ParsedQuery


import pytest
import sqlparse
import unittest

from superset.exceptions import QueryClauseValidationException
from superset.sql_parse import (
Expand Down Expand Up @@ -535,29 +537,12 @@ def test_extract_tables_multistatement() -> None:
}


def test_extract_tables_keyword() -> None:
"""
Test that table names that are keywords work as expected.
If the table name is a ``sqlparse`` reserved keyword (eg, "table_name") the parser
needs extra logic to identify it.
"""
assert extract_tables("SELECT * FROM table_name") == {Table("table_name")}
assert extract_tables("SELECT * FROM table_name AS foo") == {Table("table_name")}

# these 3 are considered keywords
assert extract_tables("SELECT * FROM catalog_name.schema_name.table_name") == {
Table("table_name", "schema_name", "catalog_name")
}


def test_extract_tables_complex() -> None:
"""
Test a few complex queries.
"""
assert (
extract_tables(
"""
assert extract_tables(
"""
SELECT sum(m_examples) AS "sum__m_example"
FROM (
SELECT
Expand All @@ -578,14 +563,12 @@ def test_extract_tables_complex() -> None:
ORDER BY "sum__m_example" DESC
LIMIT 10;
"""
)
== {
Table("my_l_table"),
Table("my_b_table"),
Table("my_t_table"),
Table("inner_table"),
}
)
) == {
Table("my_l_table"),
Table("my_b_table"),
Table("my_t_table"),
Table("inner_table"),
}

assert (
extract_tables(
Expand Down Expand Up @@ -995,46 +978,6 @@ def test_is_select_cte_with_comments() -> None:
assert sql.is_select()


def test_cte_is_select() -> None:
"""
Some CTEs are not correctly identified as SELECTS.
"""
# `AS(` gets parsed as a function
sql = ParsedQuery(
"""WITH foo AS(
SELECT
FLOOR(__time TO WEEK) AS "week",
name,
COUNT(DISTINCT user_id) AS "unique_users"
FROM "druid"."my_table"
GROUP BY 1,2
)
SELECT
f.week,
f.name,
f.unique_users
FROM foo f"""
)
assert sql.is_select()


def test_unknown_select() -> None:
"""
Test that `is_select` works when sqlparse fails to identify the type.
"""
sql = "WITH foo AS(SELECT 1) SELECT 1"
assert sqlparse.parse(sql)[0].get_type() == "UNKNOWN"
assert ParsedQuery(sql).is_select()

sql = "WITH foo AS(SELECT 1) INSERT INTO my_table (a) VALUES (1)"
assert sqlparse.parse(sql)[0].get_type() == "UNKNOWN"
assert not ParsedQuery(sql).is_select()

sql = "WITH foo AS(SELECT 1) DELETE FROM my_table"
assert sqlparse.parse(sql)[0].get_type() == "UNKNOWN"
assert not ParsedQuery(sql).is_select()


def test_get_query_with_new_limit_comment() -> None:
"""
Test that limit is applied correctly.
Expand Down

0 comments on commit dc40fe1

Please sign in to comment.