Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

test: SQL parsing with f-strings (#2532)

Akshay Agrawalc8755e16a6c3
diff --git a/tests/_ast/test_visitor.py b/tests/_ast/test_visitor.py
index 1835c65c38..08296dc900 100644
--- a/tests/_ast/test_visitor.py
+++ b/tests/_ast/test_visitor.py
@@ -1109,3 +1109,43 @@ def test_unparsable_sql_doesnt_fail() -> None:
v.visit(mod)
assert v.defs == set(["df"])
assert v.refs == set(["mo"])
+
+
[email protected](not HAS_DEPS, reason="Requires duckdb")
+def test_sql_attach() -> None:
+ code = "\n".join(
+ [
+ "mo.sql(f\"ATTACH 'dbname=postgres user=postgres host=127.0.0.1 password=password' as db\")" # noqa:E501
+ ]
+ )
+ v = visitor.ScopedVisitor()
+ mod = ast.parse(code)
+ v.visit(mod)
+ assert v.defs == set(["db"])
+ assert v.refs == set(["mo"])
+
+
[email protected](reason="f-string interpolation breaks SQL parsing")
[email protected](not HAS_DEPS, reason="Requires duckdb")
+def test_sql_attach_f_string() -> None:
+ code = "\n".join(
+ [
+ "mo.sql(f\"ATTACH 'dbname=postgres user=postgres host=127.0.0.1 password={PASSWORD}' as db\")" # noqa:E501
+ ]
+ )
+ v = visitor.ScopedVisitor()
+ mod = ast.parse(code)
+ v.visit(mod)
+ assert v.defs == set(["db"])
+ assert v.refs == set(["mo"])
+
+
[email protected](reason="f-string interpolation breaks SQL parsing")
[email protected](not HAS_DEPS, reason="Requires duckdb")
+def test_sql_ref_f_string() -> None:
+ code = "\n".join(["mo.sql(f'SELECT * FROM df LIMIT {lim}')"])
+ v = visitor.ScopedVisitor()
+ mod = ast.parse(code)
+ v.visit(mod)
+ assert not v.defs
+ assert v.refs == set(["mo", "df", "lim"])