Skip to content

Commit

Permalink
delete snippet test
Browse files Browse the repository at this point in the history
  • Loading branch information
neelasha23 committed May 23, 2023
1 parent cf903bd commit b103039
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/sql/magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ def interactive_execute_wrapper(**kwargs):
if with_:
command.set_sql_with(with_)
print(f"Forming CTE from saved snippets : {', '.join(with_)}")
else:
with_ = None

# Create the interactive slider
if args.interact and not is_interactive_mode:
Expand Down
69 changes: 69 additions & 0 deletions src/tests/test_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,72 @@ def test_table_name_typo(ip):

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 "Forming CTE from saved snippets : another_orders" in out
result_del = ip.run_cell(
"%sqlcmd snippets --delete-force-all another_orders"
).result
assert (
"Deleted snippets : final, another_orders. "
"Current saved snippets : 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

0 comments on commit b103039

Please sign in to comment.