diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-02-08 05:38:39 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-02-08 05:38:39 +0000 |
commit | aedf35026379f52d7e2b4c1f957691410a758089 (patch) | |
tree | 86540364259b66741173d2333387b78d6f9c31e2 /tests/test_jsonpath.py | |
parent | Adding upstream version 20.11.0. (diff) | |
download | sqlglot-aedf35026379f52d7e2b4c1f957691410a758089.tar.xz sqlglot-aedf35026379f52d7e2b4c1f957691410a758089.zip |
Adding upstream version 21.0.1.upstream/21.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/test_jsonpath.py')
-rw-r--r-- | tests/test_jsonpath.py | 39 |
1 files changed, 19 insertions, 20 deletions
diff --git a/tests/test_jsonpath.py b/tests/test_jsonpath.py index 01cd899..4daf3c1 100644 --- a/tests/test_jsonpath.py +++ b/tests/test_jsonpath.py @@ -2,7 +2,7 @@ import json import os import unittest -from sqlglot import jsonpath +from sqlglot import exp, jsonpath from sqlglot.errors import ParseError, TokenError from tests.helpers import FIXTURES_DIR @@ -11,24 +11,22 @@ class TestJsonpath(unittest.TestCase): maxDiff = None def test_jsonpath(self): + expected_expressions = [ + exp.JSONPathRoot(), + exp.JSONPathKey(this=exp.JSONPathWildcard()), + exp.JSONPathKey(this="a"), + exp.JSONPathSubscript(this=0), + exp.JSONPathKey(this="x"), + exp.JSONPathUnion(expressions=[exp.JSONPathWildcard(), "y", 1]), + exp.JSONPathKey(this="z"), + exp.JSONPathSelector(this=exp.JSONPathFilter(this="(@.a == 'b'), 1:")), + exp.JSONPathSubscript(this=exp.JSONPathSlice(start=1, end=5, step=None)), + exp.JSONPathUnion(expressions=[1, exp.JSONPathFilter(this="@.a")]), + exp.JSONPathSelector(this=exp.JSONPathScript(this="@.x)")), + ] self.assertEqual( jsonpath.parse("$.*.a[0]['x'][*, 'y', 1].z[?(@.a == 'b'), 1:][1:5][1,?@.a][(@.x)]"), - [ - {"kind": "root"}, - {"kind": "child", "value": "*"}, - {"kind": "child", "value": "a"}, - {"kind": "subscript", "value": 0}, - {"kind": "key", "value": "x"}, - {"kind": "union", "value": [{"kind": "wildcard"}, "y", 1]}, - {"kind": "child", "value": "z"}, - {"kind": "selector", "value": {"kind": "filter", "value": "(@.a == 'b'), 1:"}}, - { - "kind": "subscript", - "value": {"end": 5, "kind": "slice", "start": 1, "step": None}, - }, - {"kind": "union", "value": [1, {"kind": "filter", "value": "@.a"}]}, - {"kind": "selector", "value": {"kind": "script", "value": "@.x)"}}, - ], + exp.JSONPath(expressions=expected_expressions), ) def test_identity(self): @@ -38,7 +36,7 @@ class TestJsonpath(unittest.TestCase): ("$[((@.length-1))]", "$[((@.length-1))]"), ): with self.subTest(f"{selector} -> {expected}"): - self.assertEqual(jsonpath.generate(jsonpath.parse(selector)), expected) + self.assertEqual(jsonpath.parse(selector).sql(), f"'{expected}'") def test_cts_file(self): with open(os.path.join(FIXTURES_DIR, "jsonpath", "cts.json")) as file: @@ -46,6 +44,7 @@ class TestJsonpath(unittest.TestCase): # sqlglot json path generator rewrites to a normal form overrides = { + "$.☺": '$["☺"]', """$['a',1]""": """$["a",1]""", """$[*,'a']""": """$[*,"a"]""", """$..['a','d']""": """$..["a","d"]""", @@ -136,5 +135,5 @@ class TestJsonpath(unittest.TestCase): except (ParseError, TokenError): pass else: - nodes = jsonpath.parse(selector) - self.assertEqual(jsonpath.generate(nodes), overrides.get(selector, selector)) + path = jsonpath.parse(selector) + self.assertEqual(path.sql(), f"'{overrides.get(selector, selector)}'") |