Skip to content

Commit fd31748

Browse files
committed
Filter comparison expression test cases.
1 parent f8cdf77 commit fd31748

5 files changed

Lines changed: 313 additions & 15 deletions

File tree

‎jsonpath/env.py‎

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
from typing import Union
1616

1717
from .exceptions import JSONPathSyntaxError
18-
from .exceptions import JSONPathTypeError
19-
2018
from .filter import UNDEFINED
21-
2219
from .lex import Lexer
2320
from .match import FilterContextVars
2421
from .parse import Parser
@@ -151,7 +148,7 @@ def is_truthy(self, obj: object) -> bool:
151148
# pylint: disable=too-many-return-statements
152149
def compare(self, left: object, operator: str, right: object) -> bool:
153150
"""Object comparison within JSONPath filters."""
154-
# TODO: better - this is temporary
151+
# TODO: better - this (whole method) is temporary
155152
if operator == "and":
156153
return self.is_truthy(left) and self.is_truthy(right)
157154
if operator == "or":
@@ -162,16 +159,24 @@ def compare(self, left: object, operator: str, right: object) -> bool:
162159
if operator in ("!=", "<>"):
163160
return bool(left != right)
164161

165-
if isinstance(right, Sequence):
166-
if operator == "in":
167-
return left in right
162+
if isinstance(right, Sequence) and operator == "in":
163+
return left in right
168164

169-
# TODO: undefined
170165
if left is UNDEFINED or right is UNDEFINED:
171-
return False
166+
return operator == "<="
172167

173168
# TODO: =~
174169

170+
if isinstance(left, str) and isinstance(right, str):
171+
if operator == "<=":
172+
return left <= right
173+
if operator == ">=":
174+
return left >= right
175+
if operator == "<":
176+
return left < right
177+
if operator == ">":
178+
return left > right
179+
175180
# This will catch booleans too.
176181
if isinstance(left, (int, float)) and isinstance(right, (int, float)):
177182
if operator == "<=":
@@ -183,7 +188,18 @@ def compare(self, left: object, operator: str, right: object) -> bool:
183188
if operator == ">":
184189
return left > right
185190

186-
raise JSONPathTypeError(
187-
"unknown operator: "
188-
f"{left.__class__.__name__} {operator} {right.__class__.__name__}"
189-
)
191+
if (
192+
isinstance(left, Mapping)
193+
and isinstance(right, Mapping)
194+
and operator == "<="
195+
):
196+
return left == right
197+
198+
if (
199+
isinstance(left, Sequence)
200+
and isinstance(right, Sequence)
201+
and operator == "<="
202+
):
203+
return left == right
204+
205+
return False

‎jsonpath/filter.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,16 @@ async def evaluate_async(self, context: FilterContext) -> None:
5353

5454
NIL = Nil()
5555

56-
UNDEFINED = object()
56+
57+
class _Undefined:
58+
def __str__(self) -> str:
59+
return "<UNDEFINED>"
60+
61+
def __repr__(self) -> str:
62+
return "<UNDEFINED>"
63+
64+
65+
UNDEFINED = _Undefined()
5766

5867

5968
class Undefined(FilterExpression):

‎pyproject.toml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,8 @@ warn_return_any = true
8181
warn_unreachable = true
8282

8383

84+
[tool.pylint.basic]
85+
good-names = ["op", "it"]
86+
8487
[tool.pylint.format]
8588
max-line-length = "88"

‎tests/test_ietf.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class Case:
9999
# want=[1, 2, 1, 2],
100100
# ),
101101
Case(
102-
description="wildcard selector - double wild",
102+
description="wildcard selector - dot property wild",
103103
path="$.a[*]",
104104
data={"o": {"j": 1, "k": 2}, "a": [5, 3]},
105105
want=[5, 3],

‎tests/test_ietf_comparison.py‎

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
"""Default filter expression comparison test cases from examples in
2+
draft-ietf-jsonpath-base-11.
3+
4+
The test cases defined here are taken from version 11 of the JSONPath
5+
internet draft, draft-ietf-jsonpath-base-11. In accordance with
6+
https://trustee.ietf.org/license-info, Revised BSD License text
7+
is included bellow.
8+
9+
See https://datatracker.ietf.org/doc/html/draft-ietf-jsonpath-base-11
10+
11+
Copyright (c) 2023 IETF Trust and the persons identified as authors
12+
of the code. All rights reserved.Redistribution and use in source and
13+
binary forms, with or without modification, are permitted provided
14+
that the following conditions are met:
15+
16+
- Redistributions of source code must retain the above copyright
17+
notice, this list of conditions and the following disclaimer.
18+
- Redistributions in binary form must reproduce the above copyright
19+
notice, this list of conditions and the following disclaimer in the
20+
documentation and/or other materials provided with the
21+
distribution.
22+
- Neither the name of Internet Society, IETF or IETF Trust, nor the
23+
names of specific contributors, may be used to endorse or promote
24+
products derived from this software without specific prior written
25+
permission.
26+
27+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28+
“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38+
"""
39+
# pylint: disable=missing-class-docstring, missing-function-docstring
40+
41+
import dataclasses
42+
import operator
43+
44+
import pytest
45+
46+
from jsonpath import JSONPathEnvironment
47+
from jsonpath.filter import UNDEFINED
48+
49+
50+
@dataclasses.dataclass
51+
class Case:
52+
description: str
53+
left: object
54+
op: str
55+
right: object
56+
want: bool
57+
58+
59+
DATA = {"obj": {"x": "y"}, "arr": [2, 3]}
60+
61+
TEST_CASES = [
62+
Case(
63+
description="$.absent1 == $.absent2",
64+
left=UNDEFINED,
65+
op="==",
66+
right=UNDEFINED,
67+
want=True,
68+
),
69+
Case(
70+
description="$.absent1 <= $.absent2",
71+
left=UNDEFINED,
72+
op="<=",
73+
right=UNDEFINED,
74+
want=True,
75+
),
76+
Case(
77+
description="$.absent == 'g'",
78+
left=UNDEFINED,
79+
op="==",
80+
right="g",
81+
want=False,
82+
),
83+
Case(
84+
description="$.absent1 != $.absent2",
85+
left=UNDEFINED,
86+
op="!=",
87+
right=UNDEFINED,
88+
want=False,
89+
),
90+
Case(
91+
description="$.absent != 'g'",
92+
left=UNDEFINED,
93+
op="!=",
94+
right="g",
95+
want=True,
96+
),
97+
Case(
98+
description="1 <= 2",
99+
left=1,
100+
op="<=",
101+
right=2,
102+
want=True,
103+
),
104+
Case(
105+
description="1 > 2",
106+
left=1,
107+
op=">",
108+
right=2,
109+
want=False,
110+
),
111+
Case(
112+
description="13 == '13'",
113+
left=13,
114+
op="==",
115+
right="13",
116+
want=False,
117+
),
118+
Case(
119+
description="'a' <= 'b'",
120+
left="a",
121+
op="<=",
122+
right="b",
123+
want=True,
124+
),
125+
Case(
126+
description="'a' > 'b'",
127+
left="a",
128+
op=">",
129+
right="b",
130+
want=False,
131+
),
132+
Case(
133+
description="$.obj == $.arr",
134+
left=DATA["obj"],
135+
op="==",
136+
right=DATA["arr"],
137+
want=False,
138+
),
139+
Case(
140+
description="$.obj != $.arr",
141+
left=DATA["obj"],
142+
op="!=",
143+
right=DATA["arr"],
144+
want=True,
145+
),
146+
Case(
147+
description="$.obj == $.obj",
148+
left=DATA["obj"],
149+
op="==",
150+
right=DATA["obj"],
151+
want=True,
152+
),
153+
Case(
154+
description="$.obj != $.obj",
155+
left=DATA["obj"],
156+
op="!=",
157+
right=DATA["obj"],
158+
want=False,
159+
),
160+
Case(
161+
description="$.arr == $.arr",
162+
left=DATA["arr"],
163+
op="==",
164+
right=DATA["arr"],
165+
want=True,
166+
),
167+
Case(
168+
description="$.arr != $.arr",
169+
left=DATA["arr"],
170+
op="!=",
171+
right=DATA["arr"],
172+
want=False,
173+
),
174+
Case(
175+
description="$.arr == 17",
176+
left=DATA["arr"],
177+
op="==",
178+
right=17,
179+
want=False,
180+
),
181+
Case(
182+
description="$.arr != 17",
183+
left=DATA["arr"],
184+
op="!=",
185+
right=17,
186+
want=True,
187+
),
188+
Case(
189+
description="$.obj <= $.arr",
190+
left=DATA["obj"],
191+
op="<=",
192+
right=DATA["arr"],
193+
want=False,
194+
),
195+
Case(
196+
description="$.obj < $.arr",
197+
left=DATA["obj"],
198+
op="<",
199+
right=DATA["arr"],
200+
want=False,
201+
),
202+
Case(
203+
description="$.obj <= $.obj",
204+
left=DATA["obj"],
205+
op="<=",
206+
right=DATA["obj"],
207+
want=True,
208+
),
209+
Case(
210+
description="$.arr <= $.arr",
211+
left=DATA["arr"],
212+
op="<=",
213+
right=DATA["arr"],
214+
want=True,
215+
),
216+
Case(
217+
description="1 <= $.arr",
218+
left=1,
219+
op="<=",
220+
right=DATA["arr"],
221+
want=False,
222+
),
223+
Case(
224+
description="1 >= $.arr",
225+
left=1,
226+
op=">=",
227+
right=DATA["arr"],
228+
want=False,
229+
),
230+
Case(
231+
description="1 > $.arr",
232+
left=1,
233+
op=">",
234+
right=DATA["arr"],
235+
want=False,
236+
),
237+
Case(
238+
description="1 < $.arr",
239+
left=1,
240+
op="<",
241+
right=DATA["arr"],
242+
want=False,
243+
),
244+
Case(
245+
description="true <= true",
246+
left=True,
247+
op="<=",
248+
right=True,
249+
want=True,
250+
),
251+
Case(
252+
description="true > true",
253+
left=True,
254+
op=">",
255+
right=True,
256+
want=False,
257+
),
258+
]
259+
260+
261+
@pytest.fixture
262+
def env() -> JSONPathEnvironment:
263+
return JSONPathEnvironment()
264+
265+
266+
# pylint: disable=redefined-outer-name
267+
@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description"))
268+
def test_compare_ieft(env: JSONPathEnvironment, case: Case) -> None:
269+
result = env.compare(case.left, case.op, case.right)
270+
assert result == case.want

0 commit comments

Comments
 (0)