summaryrefslogtreecommitdiffstats
path: root/tests/test_expressions.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_expressions.py')
-rw-r--r--tests/test_expressions.py25
1 files changed, 22 insertions, 3 deletions
diff --git a/tests/test_expressions.py b/tests/test_expressions.py
index 9ad2bf5..79b4ee5 100644
--- a/tests/test_expressions.py
+++ b/tests/test_expressions.py
@@ -123,13 +123,16 @@ class TestExpressions(unittest.TestCase):
self.assertEqual(exp.table_name(parse_one("a.b.c", into=exp.Table)), "a.b.c")
self.assertEqual(exp.table_name("a.b.c"), "a.b.c")
+ def test_table(self):
+ self.assertEqual(exp.table_("a", alias="b"), parse_one("select * from a b").find(exp.Table))
+
def test_replace_tables(self):
self.assertEqual(
exp.replace_tables(
- parse_one("select * from a join b join c.a join d.a join e.a"),
+ parse_one("select * from a AS a join b join c.a join d.a join e.a"),
{"a": "a1", "b": "b.a", "c.a": "c.a2", "d.a": "d2"},
).sql(),
- 'SELECT * FROM "a1" JOIN "b"."a" JOIN "c"."a2" JOIN "d2" JOIN e.a',
+ "SELECT * FROM a1 AS a JOIN b.a JOIN c.a2 JOIN d2 JOIN e.a",
)
def test_named_selects(self):
@@ -495,11 +498,15 @@ class TestExpressions(unittest.TestCase):
self.assertEqual(exp.convert(value).sql(), expected)
def test_annotation_alias(self):
- expression = parse_one("SELECT a, b AS B, c #comment, d AS D #another_comment FROM foo")
+ sql = "SELECT a, b AS B, c # comment, d AS D # another_comment FROM foo"
+ expression = parse_one(sql)
self.assertEqual(
[e.alias_or_name for e in expression.expressions],
["a", "B", "c", "D"],
)
+ self.assertEqual(expression.sql(), sql)
+ self.assertEqual(expression.expressions[2].name, "comment")
+ self.assertEqual(expression.sql(annotations=False), "SELECT a, b AS B, c, d AS D")
def test_to_table(self):
table_only = exp.to_table("table_name")
@@ -514,6 +521,18 @@ class TestExpressions(unittest.TestCase):
self.assertEqual(catalog_db_and_table.name, "table_name")
self.assertEqual(catalog_db_and_table.args.get("db"), exp.to_identifier("db"))
self.assertEqual(catalog_db_and_table.args.get("catalog"), exp.to_identifier("catalog"))
+ with self.assertRaises(ValueError):
+ exp.to_table(1)
+
+ def test_to_column(self):
+ column_only = exp.to_column("column_name")
+ self.assertEqual(column_only.name, "column_name")
+ self.assertIsNone(column_only.args.get("table"))
+ table_and_column = exp.to_column("table_name.column_name")
+ self.assertEqual(table_and_column.name, "column_name")
+ self.assertEqual(table_and_column.args.get("table"), exp.to_identifier("table_name"))
+ with self.assertRaises(ValueError):
+ exp.to_column(1)
def test_union(self):
expression = parse_one("SELECT cola, colb UNION SELECT colx, coly")