summaryrefslogtreecommitdiffstats
path: root/tests/test_diff.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tests/test_diff.py139
1 files changed, 92 insertions, 47 deletions
diff --git a/tests/test_diff.py b/tests/test_diff.py
index f0e0747..440502e 100644
--- a/tests/test_diff.py
+++ b/tests/test_diff.py
@@ -2,7 +2,6 @@ import unittest
from sqlglot import exp, parse_one
from sqlglot.diff import Insert, Move, Remove, Update, diff
-from sqlglot.expressions import Join, to_table
def diff_delta_only(source, target, matchings=None, **kwargs):
@@ -14,22 +13,24 @@ class TestDiff(unittest.TestCase):
self._validate_delta_only(
diff_delta_only(parse_one("SELECT a + b"), parse_one("SELECT a - b")),
[
- Remove(parse_one("a + b")), # the Add node
- Insert(parse_one("a - b")), # the Sub node
+ Remove(expression=parse_one("a + b")), # the Add node
+ Insert(expression=parse_one("a - b")), # the Sub node
+ Move(source=parse_one("a"), target=parse_one("a")), # the `a` Column node
+ Move(source=parse_one("b"), target=parse_one("b")), # the `b` Column node
],
)
self._validate_delta_only(
diff_delta_only(parse_one("SELECT a, b, c"), parse_one("SELECT a, c")),
[
- Remove(parse_one("b")), # the Column node
+ Remove(expression=parse_one("b")), # the Column node
],
)
self._validate_delta_only(
diff_delta_only(parse_one("SELECT a, b"), parse_one("SELECT a, b, c")),
[
- Insert(parse_one("c")), # the Column node
+ Insert(expression=parse_one("c")), # the Column node
],
)
@@ -40,8 +41,8 @@ class TestDiff(unittest.TestCase):
),
[
Update(
- to_table("table_one", quoted=False),
- to_table("table_two", quoted=False),
+ source=exp.to_table("table_one", quoted=False),
+ target=exp.to_table("table_two", quoted=False),
), # the Table node
],
)
@@ -53,8 +54,12 @@ class TestDiff(unittest.TestCase):
),
[
Update(
- exp.Lambda(this=exp.to_identifier("a"), expressions=[exp.to_identifier("a")]),
- exp.Lambda(this=exp.to_identifier("b"), expressions=[exp.to_identifier("b")]),
+ source=exp.Lambda(
+ this=exp.to_identifier("a"), expressions=[exp.to_identifier("a")]
+ ),
+ target=exp.Lambda(
+ this=exp.to_identifier("b"), expressions=[exp.to_identifier("b")]
+ ),
),
],
)
@@ -65,8 +70,8 @@ class TestDiff(unittest.TestCase):
parse_one('SELECT a, b, "my.udf1"()'), parse_one('SELECT a, b, "my.udf2"()')
),
[
- Insert(parse_one('"my.udf2"()')),
- Remove(parse_one('"my.udf1"()')),
+ Insert(expression=parse_one('"my.udf2"()')),
+ Remove(expression=parse_one('"my.udf1"()')),
],
)
self._validate_delta_only(
@@ -75,41 +80,73 @@ class TestDiff(unittest.TestCase):
parse_one('SELECT a, b, "my.udf"(x, y, w)'),
),
[
- Insert(exp.column("w")),
- Remove(exp.column("z")),
+ Insert(expression=exp.column("w")),
+ Remove(expression=exp.column("z")),
],
)
def test_node_position_changed(self):
+ expr_src = parse_one("SELECT a, b, c")
+ expr_tgt = parse_one("SELECT c, a, b")
+
self._validate_delta_only(
- diff_delta_only(parse_one("SELECT a, b, c"), parse_one("SELECT c, a, b")),
+ diff_delta_only(expr_src, expr_tgt),
[
- Move(parse_one("c")), # the Column node
+ Move(source=expr_src.selects[2], target=expr_tgt.selects[0]),
],
)
+ expr_src = parse_one("SELECT a + b")
+ expr_tgt = parse_one("SELECT b + a")
+
self._validate_delta_only(
- diff_delta_only(parse_one("SELECT a + b"), parse_one("SELECT b + a")),
+ diff_delta_only(expr_src, expr_tgt),
[
- Move(parse_one("a")), # the Column node
+ Move(source=expr_src.selects[0].left, target=expr_tgt.selects[0].right),
],
)
+ expr_src = parse_one("SELECT aaaa AND bbbb")
+ expr_tgt = parse_one("SELECT bbbb AND aaaa")
+
self._validate_delta_only(
- diff_delta_only(parse_one("SELECT aaaa AND bbbb"), parse_one("SELECT bbbb AND aaaa")),
+ diff_delta_only(expr_src, expr_tgt),
[
- Move(parse_one("aaaa")), # the Column node
+ Move(source=expr_src.selects[0].left, target=expr_tgt.selects[0].right),
],
)
+ expr_src = parse_one("SELECT aaaa OR bbbb OR cccc")
+ expr_tgt = parse_one("SELECT cccc OR bbbb OR aaaa")
+
self._validate_delta_only(
- diff_delta_only(
- parse_one("SELECT aaaa OR bbbb OR cccc"),
- parse_one("SELECT cccc OR bbbb OR aaaa"),
- ),
+ diff_delta_only(expr_src, expr_tgt),
+ [
+ Move(source=expr_src.selects[0].left.left, target=expr_tgt.selects[0].right),
+ Move(source=expr_src.selects[0].right, target=expr_tgt.selects[0].left.left),
+ ],
+ )
+
+ expr_src = parse_one("SELECT a, b FROM t WHERE CONCAT('a', 'b') = 'ab'")
+ expr_tgt = parse_one("SELECT a FROM t WHERE CONCAT('a', 'b', b) = 'ab'")
+
+ self._validate_delta_only(
+ diff_delta_only(expr_src, expr_tgt),
+ [
+ Move(source=expr_src.selects[1], target=expr_tgt.find(exp.Concat).expressions[-1]),
+ ],
+ )
+
+ expr_src = parse_one("SELECT a as a, b as b FROM t WHERE CONCAT('a', 'b') = 'ab'")
+ expr_tgt = parse_one("SELECT a as a FROM t WHERE CONCAT('a', 'b', b) = 'ab'")
+
+ b_alias = expr_src.selects[1]
+
+ self._validate_delta_only(
+ diff_delta_only(expr_src, expr_tgt),
[
- Move(parse_one("aaaa")), # the Column node
- Move(parse_one("cccc")), # the Column node
+ Remove(expression=b_alias),
+ Move(source=b_alias.this, target=expr_tgt.find(exp.Concat).expressions[-1]),
],
)
@@ -130,23 +167,30 @@ class TestDiff(unittest.TestCase):
self._validate_delta_only(
diff_delta_only(parse_one(expr_src), parse_one(expr_tgt)),
[
- Remove(parse_one("LOWER(c) AS c")), # the Alias node
- Remove(parse_one("LOWER(c)")), # the Lower node
- Remove(parse_one("'filter'")), # the Literal node
- Insert(parse_one("'different_filter'")), # the Literal node
+ Remove(expression=parse_one("LOWER(c) AS c")), # the Alias node
+ Remove(expression=parse_one("LOWER(c)")), # the Lower node
+ Remove(expression=parse_one("'filter'")), # the Literal node
+ Insert(expression=parse_one("'different_filter'")), # the Literal node
+ Move(source=parse_one("c"), target=parse_one("c")), # the new Column c
],
)
def test_join(self):
- expr_src = "SELECT a, b FROM t1 LEFT JOIN t2 ON t1.key = t2.key"
- expr_tgt = "SELECT a, b FROM t1 RIGHT JOIN t2 ON t1.key = t2.key"
+ expr_src = parse_one("SELECT a, b FROM t1 LEFT JOIN t2 ON t1.key = t2.key")
+ expr_tgt = parse_one("SELECT a, b FROM t1 RIGHT JOIN t2 ON t1.key = t2.key")
- changes = diff_delta_only(parse_one(expr_src), parse_one(expr_tgt))
+ src_join = expr_src.find(exp.Join)
+ tgt_join = expr_tgt.find(exp.Join)
- self.assertEqual(len(changes), 2)
- self.assertTrue(isinstance(changes[0], Remove))
- self.assertTrue(isinstance(changes[1], Insert))
- self.assertTrue(all(isinstance(c.expression, Join) for c in changes))
+ self._validate_delta_only(
+ diff_delta_only(expr_src, expr_tgt),
+ [
+ Remove(expression=src_join),
+ Insert(expression=tgt_join),
+ Move(source=exp.to_table("t2"), target=exp.to_table("t2")),
+ Move(source=src_join.args["on"], target=tgt_join.args["on"]),
+ ],
+ )
def test_window_functions(self):
expr_src = parse_one("SELECT ROW_NUMBER() OVER (PARTITION BY a ORDER BY b)")
@@ -157,8 +201,8 @@ class TestDiff(unittest.TestCase):
self._validate_delta_only(
diff_delta_only(expr_src, expr_tgt),
[
- Remove(parse_one("ROW_NUMBER()")),
- Insert(parse_one("RANK()")),
+ Remove(expression=parse_one("ROW_NUMBER()")),
+ Insert(expression=parse_one("RANK()")),
Update(source=expr_src.selects[0], target=expr_tgt.selects[0]),
],
)
@@ -178,20 +222,21 @@ class TestDiff(unittest.TestCase):
self._validate_delta_only(
diff_delta_only(expr_src, expr_tgt),
[
- Remove(expr_src),
- Insert(expr_tgt),
- Insert(exp.Literal.number(2)),
- Insert(exp.Literal.number(3)),
- Insert(exp.Literal.number(4)),
+ Remove(expression=expr_src),
+ Insert(expression=expr_tgt),
+ Insert(expression=exp.Literal.number(2)),
+ Insert(expression=exp.Literal.number(3)),
+ Insert(expression=exp.Literal.number(4)),
+ Move(source=exp.Literal.number(1), target=exp.Literal.number(1)),
],
)
self._validate_delta_only(
diff_delta_only(expr_src, expr_tgt, matchings=[(expr_src, expr_tgt)]),
[
- Insert(exp.Literal.number(2)),
- Insert(exp.Literal.number(3)),
- Insert(exp.Literal.number(4)),
+ Insert(expression=exp.Literal.number(2)),
+ Insert(expression=exp.Literal.number(3)),
+ Insert(expression=exp.Literal.number(4)),
],
)
@@ -274,7 +319,7 @@ class TestDiff(unittest.TestCase):
source=expr_src.find(exp.Order).expressions[0],
target=expr_tgt.find(exp.Order).expressions[0],
),
- Move(parse_one("a")),
+ Move(source=expr_src.selects[0], target=expr_tgt.selects[1]),
],
)