From c66e4a33e1a07c439f03fe47f146a6c6482bf6df Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Thu, 8 Feb 2024 06:38:42 +0100 Subject: Merging upstream version 21.0.1. Signed-off-by: Daniel Baumann --- tests/test_jsonpath.py | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) (limited to 'tests/test_jsonpath.py') 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)}'") -- cgit v1.2.3