summaryrefslogtreecommitdiffstats
path: root/tests/test_expressions.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2022-10-25 16:01:47 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2022-10-25 16:01:47 +0000
commitbe33e8ff4eed68ad217f9ef6ad535206df4129f8 (patch)
tree752695efdccc8387e37e233c900ce7c3c8f1aaff /tests/test_expressions.py
parentReleasing debian version 9.0.1-1. (diff)
downloadsqlglot-be33e8ff4eed68ad217f9ef6ad535206df4129f8.tar.xz
sqlglot-be33e8ff4eed68ad217f9ef6ad535206df4129f8.zip
Merging upstream version 9.0.3.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/test_expressions.py')
-rw-r--r--tests/test_expressions.py66
1 files changed, 64 insertions, 2 deletions
diff --git a/tests/test_expressions.py b/tests/test_expressions.py
index 79b4ee5..9af59d9 100644
--- a/tests/test_expressions.py
+++ b/tests/test_expressions.py
@@ -135,6 +135,53 @@ class TestExpressions(unittest.TestCase):
"SELECT * FROM a1 AS a JOIN b.a JOIN c.a2 JOIN d2 JOIN e.a",
)
+ def test_replace_placeholders(self):
+ self.assertEqual(
+ exp.replace_placeholders(
+ parse_one("select * from :tbl1 JOIN :tbl2 ON :col1 = :col2 WHERE :col3 > 100"),
+ tbl1="foo",
+ tbl2="bar",
+ col1="a",
+ col2="b",
+ col3="c",
+ ).sql(),
+ "SELECT * FROM foo JOIN bar ON a = b WHERE c > 100",
+ )
+ self.assertEqual(
+ exp.replace_placeholders(
+ parse_one("select * from ? JOIN ? ON ? = ? WHERE ? > 100"),
+ "foo",
+ "bar",
+ "a",
+ "b",
+ "c",
+ ).sql(),
+ "SELECT * FROM foo JOIN bar ON a = b WHERE c > 100",
+ )
+ self.assertEqual(
+ exp.replace_placeholders(
+ parse_one("select * from ? WHERE ? > 100"),
+ "foo",
+ ).sql(),
+ "SELECT * FROM foo WHERE ? > 100",
+ )
+ self.assertEqual(
+ exp.replace_placeholders(parse_one("select * from :name WHERE ? > 100"), another_name="bla").sql(),
+ "SELECT * FROM :name WHERE ? > 100",
+ )
+ self.assertEqual(
+ exp.replace_placeholders(
+ parse_one("select * from (SELECT :col1 FROM ?) WHERE :col2 > 100"),
+ "tbl1",
+ "tbl2",
+ "tbl3",
+ col1="a",
+ col2="b",
+ col3="c",
+ ).sql(),
+ "SELECT * FROM (SELECT a FROM tbl1) WHERE b > 100",
+ )
+
def test_named_selects(self):
expression = parse_one("SELECT a, b AS B, c + d AS e, *, 'zz', 'zz' AS z FROM foo as bar, baz")
self.assertEqual(expression.named_selects, ["a", "B", "e", "*", "zz", "z"])
@@ -504,9 +551,24 @@ class TestExpressions(unittest.TestCase):
[e.alias_or_name for e in expression.expressions],
["a", "B", "c", "D"],
)
- self.assertEqual(expression.sql(), sql)
+ self.assertEqual(expression.sql(), "SELECT a, b AS B, c, d AS D")
self.assertEqual(expression.expressions[2].name, "comment")
- self.assertEqual(expression.sql(annotations=False), "SELECT a, b AS B, c, d AS D")
+ self.assertEqual(
+ expression.sql(pretty=True, annotations=False),
+ """SELECT
+ a,
+ b AS B,
+ c,
+ d AS D""",
+ )
+ self.assertEqual(
+ expression.sql(pretty=True),
+ """SELECT
+ a,
+ b AS B,
+ c # comment,
+ d AS D # another_comment FROM foo""",
+ )
def test_to_table(self):
table_only = exp.to_table("table_name")