Skip to content

Commit

Permalink
plot tests
Browse files Browse the repository at this point in the history
plot_typo

Renamed test file
  • Loading branch information
neelasha23 committed May 31, 2023
1 parent 57b4e1e commit 1477903
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 141 deletions.
15 changes: 15 additions & 0 deletions src/sql/error_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@


def parse_sqlglot_error(e, q):
"""
Function to parse the error message from sqlglot
Parameters
----------
e: sqlglot.errors.ParseError, exception
while parsing through sqlglot
q : str, user query
Returns
-------
str
Formatted error message containing description
and positions
"""
err = e.errors
position = ""
for item in err:
Expand Down
4 changes: 4 additions & 0 deletions src/sql/query_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ def extract_tables_from_query(query):
tables = [table.name for table in parse_one(query).find_all(exp.Table)]
return tables
except ParseError:
# TODO : Instead of returning [] replace with call to
# error_messages.py::parse_sqlglot_error. Currently this
# is not possible because of an exception raised in test
# fixtures.
return []
141 changes: 0 additions & 141 deletions src/tests/test_compose.py

This file was deleted.

1 change: 1 addition & 0 deletions src/tests/test_extract_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
["sf_crime_incidents_2014_01", "sf_crime_incidents_2014_01"],
),
],
ids=["join", "union", "case", "groupby", "subquery", "subquery_join"],
)
def test_extract(query, expected):
tables = extract_tables_from_query(query)
Expand Down
143 changes: 143 additions & 0 deletions src/tests/test_magic_cte.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import pytest
from IPython.core.error import UsageError


def test_trailing_semicolons_removed_from_cte(ip):
ip.run_cell(
"""%%sql --save positive_x
Expand Down Expand Up @@ -32,3 +36,142 @@ def test_trailing_semicolons_removed_from_cte(ip):
"FROM number_table WHERE y > 0)\nSELECT * FROM positive_x\n"
"UNION\nSELECT * FROM positive_y;"
)


def test_infer(ip, capsys):
ip.run_cell_magic(
"sql",
"--save author_sub",
"SELECT last_name FROM author WHERE year_of_death > 1900",
)

ip.run_cell_magic(
"sql",
"--save final",
"SELECT last_name FROM author_sub;",
)
out, _ = capsys.readouterr()
result = ip.run_cell("%sqlrender final").result
expected = (
"WITH `author_sub` AS (\nSELECT last_name FROM author "
"WHERE year_of_death > 1900)\nSELECT last_name FROM author_sub;"
)

assert result == expected
assert "Generating CTE with stored snippets : author_sub" in out


def test_deprecation_warning(ip):
ip.run_cell_magic(
"sql",
"--save author_sub",
"SELECT last_name FROM author WHERE year_of_death > 1900",
)

with pytest.warns(FutureWarning) as record:
ip.run_cell_magic(
"sql",
"--with author_sub --save final",
"SELECT last_name FROM author_sub;",
)
assert len(record) == 1
assert (
"CTE dependencies are now automatically inferred,"
" you can omit the --with arguments. Using --with will "
"raise an exception in the next major release so please "
"remove it." in record[0].message.args[0]
)


TABLE_NAME_TYPO_ERR_MSG = """
There is no table with name 'author_subb'.
Did you mean : 'author_one', or 'author_sub'
If you need help solving this issue, send us a message: https://ploomber.io/community
"""


def test_table_name_typo(ip):
ip.run_cell_magic(
"sql",
"--save author_sub",
"SELECT last_name FROM author WHERE year_of_death > 1900",
)

with pytest.raises(UsageError) as excinfo:
ip.run_cell_magic(
"sql",
"--save final",
"SELECT last_name FROM author_subb;",
)

assert excinfo.value.error_type == "TableNotFoundError"
assert str(excinfo.value) == TABLE_NAME_TYPO_ERR_MSG.strip()


def test_snippets_delete(ip, capsys):
ip.run_cell(
"""
%%sql sqlite://
CREATE TABLE orders (order_id int, customer_id int, order_value float);
INSERT INTO orders VALUES (123, 15, 150.67);
INSERT INTO orders VALUES (124, 25, 200.66);
INSERT INTO orders VALUES (211, 15, 251.43);
INSERT INTO orders VALUES (312, 5, 333.41);
CREATE TABLE another_orders (order_id int, customer_id int, order_value float);
INSERT INTO another_orders VALUES (511,15, 150.67);
INSERT INTO another_orders VALUES (512, 30, 200.66);
CREATE TABLE customers (customer_id int, name varchar(25));
INSERT INTO customers VALUES (15, 'John');
INSERT INTO customers VALUES (25, 'Sheryl');
INSERT INTO customers VALUES (5, 'Mike');
INSERT INTO customers VALUES (30, 'Daisy');
"""
)
ip.run_cell_magic(
"sql",
"--save orders_less",
"SELECT * FROM orders WHERE order_value < 250.0",
)

ip.run_cell_magic(
"sql",
"--save another_orders",
"SELECT * FROM orders WHERE order_value > 250.0",
)

ip.run_cell_magic(
"sql",
"--save final",
"""
SELECT o.order_id, customers.name, o.order_value
FROM another_orders o
INNER JOIN customers ON o.customer_id=customers.customer_id;
""",
)

out, _ = capsys.readouterr()
assert "Generating CTE with stored snippets : another_orders" in out
result_del = ip.run_cell(
"%sqlcmd snippets --delete-force-all another_orders"
).result
assert (
"final, another_orders has been deleted.\nStored snippets : "
"author_one, author_sub, orders_less" == result_del
)
ip.run_cell_magic(
"sql",
"--save final",
"""
SELECT o.order_id, customers.name, o.order_value
FROM another_orders o
INNER JOIN customers ON o.customer_id=customers.customer_id;
""",
)
result = ip.run_cell("%sqlrender final").result
expected = (
"WITH\n\n SELECT o.order_id, customers.name, "
"o.order_value\n "
"FROM another_orders o\n INNER JOIN customers "
"ON o.customer_id=customers.customer_id"
)
assert expected in result
Loading

0 comments on commit 1477903

Please sign in to comment.