mirror of
https://github.com/apache/superset.git
synced 2026-04-19 16:14:52 +00:00
fix: sqlparse fallback for formatting queries (#30578)
This commit is contained in:
@@ -281,7 +281,7 @@ class TestSqlLabApi(SupersetTestCase):
|
||||
"/api/v1/sqllab/format_sql/",
|
||||
json=data,
|
||||
)
|
||||
success_resp = {"result": "SELECT\n 1\nFROM my_table"}
|
||||
success_resp = {"result": "SELECT 1\nFROM my_table"}
|
||||
resp_data = json.loads(rv.data.decode("utf-8"))
|
||||
self.assertDictEqual(resp_data, success_resp) # noqa: PT009
|
||||
assert rv.status_code == 200
|
||||
|
||||
@@ -241,14 +241,7 @@ def test_select_star(mocker: MockerFixture) -> None:
|
||||
latest_partition=False,
|
||||
cols=cols,
|
||||
)
|
||||
assert (
|
||||
sql
|
||||
== """SELECT
|
||||
a
|
||||
FROM my_table
|
||||
LIMIT ?
|
||||
OFFSET ?"""
|
||||
)
|
||||
assert sql == "SELECT a\nFROM my_table\nLIMIT ?\nOFFSET ?"
|
||||
|
||||
sql = NoLimitDBEngineSpec.select_star(
|
||||
database=database,
|
||||
@@ -260,12 +253,7 @@ OFFSET ?"""
|
||||
latest_partition=False,
|
||||
cols=cols,
|
||||
)
|
||||
assert (
|
||||
sql
|
||||
== """SELECT
|
||||
a
|
||||
FROM my_table"""
|
||||
)
|
||||
assert sql == "SELECT a\nFROM my_table"
|
||||
|
||||
|
||||
def test_extra_table_metadata(mocker: MockerFixture) -> None:
|
||||
|
||||
@@ -284,6 +284,40 @@ def test_extract_tables_show_tables_from() -> None:
|
||||
)
|
||||
|
||||
|
||||
def test_format_show_tables() -> None:
|
||||
"""
|
||||
Test format when `ast.sql()` raises an exception.
|
||||
|
||||
In that case sqlparse should be used instead.
|
||||
"""
|
||||
assert (
|
||||
SQLScript("SHOW TABLES FROM s1 like '%order%'", "mysql").format()
|
||||
== "SHOW TABLES FROM s1 LIKE '%order%'"
|
||||
)
|
||||
|
||||
|
||||
def test_format_no_dialect() -> None:
|
||||
"""
|
||||
Test format with an engine that has no corresponding dialect.
|
||||
"""
|
||||
assert (
|
||||
SQLScript("SELECT col FROM t WHERE col NOT IN (1, 2)", "firebolt").format()
|
||||
== "SELECT col\nFROM t\nWHERE col NOT IN (1,\n 2)"
|
||||
)
|
||||
|
||||
|
||||
def test_split_no_dialect() -> None:
|
||||
"""
|
||||
Test the statement split when the engine has no corresponding dialect.
|
||||
"""
|
||||
sql = "SELECT col FROM t WHERE col NOT IN (1, 2); SELECT * FROM t; SELECT foo"
|
||||
statements = SQLScript(sql, "firebolt").statements
|
||||
assert len(statements) == 3
|
||||
assert statements[0]._sql == "SELECT col FROM t WHERE col NOT IN (1, 2)"
|
||||
assert statements[1]._sql == "SELECT * FROM t"
|
||||
assert statements[2]._sql == "SELECT foo"
|
||||
|
||||
|
||||
def test_extract_tables_show_columns_from() -> None:
|
||||
"""
|
||||
Test `SHOW COLUMNS FROM`.
|
||||
|
||||
Reference in New Issue
Block a user