Skip to content

Commit 6e1f3b7

Browse files
committed
Separate test cases for non-standard sytnax in to JSON files WIP
1 parent 7e09eaf commit 6e1f3b7

21 files changed

Lines changed: 589 additions & 377 deletions

jsonpath/lex.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ def compile_strict_rules(self) -> Pattern[str]:
235235
(TOKEN_LG, r"<>"),
236236
(TOKEN_LE, r"<="),
237237
(TOKEN_GE, r">="),
238-
(TOKEN_RE, r"=~"),
239238
(TOKEN_LT, r"<"),
240239
(TOKEN_GT, r">"),
241240
(TOKEN_NOT, r"!"), # Must go after "!="

tests/_cts_case.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""A dataclass for a test case suitable for the CTS JSON schema."""
2+
3+
from dataclasses import dataclass
4+
from dataclasses import field
5+
from typing import Any
6+
from typing import Dict
7+
from typing import List
8+
from typing import Mapping
9+
from typing import Optional
10+
from typing import Sequence
11+
from typing import Union
12+
13+
14+
@dataclass
15+
class Case:
16+
name: str
17+
selector: str
18+
document: Union[Mapping[str, Any], Sequence[Any], None] = None
19+
result: Any = None
20+
results: Optional[List[Any]] = None
21+
result_paths: Optional[List[str]] = None
22+
results_paths: Optional[List[List[str]]] = None
23+
invalid_selector: Optional[bool] = None
24+
tags: List[str] = field(default_factory=list)
25+
26+
def as_dict(self) -> Dict[str, Any]:
27+
rv: Dict[str, Any] = {
28+
"name": self.name,
29+
"selector": self.selector,
30+
}
31+
32+
if self.document is not None:
33+
rv["document"] = self.document
34+
35+
if self.result is not None:
36+
rv["result"] = self.result
37+
rv["result_paths"] = self.result_paths
38+
else:
39+
rv["results"] = self.results
40+
rv["results_paths"] = self.results_paths
41+
else:
42+
assert self.invalid_selector
43+
rv["invalid_selector"] = True
44+
45+
rv["tags"] = self.tags
46+
47+
return rv

tests/current_key_identifier.json

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"tests": [
3+
{
4+
"name": "current key of an object",
5+
"selector": "$.some[?match(#, '^b.*')]",
6+
"document": {
7+
"some": { "foo": "a", "bar": "b", "baz": "c", "qux": "d" }
8+
},
9+
"result": ["b", "c"],
10+
"result_paths": ["$['some']['bar']", "$['some']['baz']"]
11+
},
12+
{
13+
"name": "current key of an array",
14+
"selector": "$.some[?# > 1]",
15+
"document": { "some": ["other", "thing", "foo", "bar"] },
16+
"result": ["foo", "bar"],
17+
"result_paths": ["$['some'][2]", "$['some'][3]"]
18+
},
19+
{
20+
"name": "current key of a string selects nothing",
21+
"selector": "$.some[?# > 1]",
22+
"document": { "some": "thing" },
23+
"result": [],
24+
"result_paths": []
25+
},
26+
{
27+
"name": "current key of an object",
28+
"selector": "$.some[?match(#, '^b.*')]",
29+
"document": {
30+
"some": { "foo": "a", "bar": "b", "baz": "c", "qux": "d" }
31+
},
32+
"result": ["b", "c"],
33+
"result_paths": ["$['some']['bar']", "$['some']['baz']"],
34+
"tags": ["extra"]
35+
},
36+
{
37+
"name": "current key of an array",
38+
"selector": "$.some[?# > 1]",
39+
"document": { "some": ["other", "thing", "foo", "bar"] },
40+
"result": ["foo", "bar"],
41+
"result_paths": ["$['some'][2]", "$['some'][3]"],
42+
"tags": ["extra"]
43+
},
44+
{
45+
"name": "current key identifier, match on object names",
46+
"selector": "$[?match(#, '^ab.*') && length(@) > 0 ]",
47+
"document": { "abc": [1, 2, 3], "def": [4, 5], "abx": [6], "aby": [] },
48+
"result": [[1, 2, 3], [6]],
49+
"result_paths": ["$['abc']", "$['abx']"],
50+
"tags": ["extra"]
51+
},
52+
{
53+
"name": "current key identifier, compare current array index",
54+
"selector": "$.abc[?(# >= 1)]",
55+
"document": { "abc": [1, 2, 3], "def": [4, 5], "abx": [6], "aby": [] },
56+
"result": [2, 3],
57+
"result_paths": ["$['abc'][1]", "$['abc'][2]"],
58+
"tags": ["extra"]
59+
}
60+
]
61+
}

tests/key_selector.json

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{
2+
"tests": [
3+
{
4+
"name": "singular key from an object",
5+
"selector": "$.some[~'other']",
6+
"document": { "some": { "other": "foo", "thing": "bar" } },
7+
"result": ["other"],
8+
"result_paths": ["$['some'][~'other']"],
9+
"tags": ["extra"]
10+
},
11+
{
12+
"name": "singular key from an object, does not exist",
13+
"selector": "$.some[~'else']",
14+
"document": { "some": { "other": "foo", "thing": "bar" } },
15+
"result": [],
16+
"result_paths": [],
17+
"tags": ["extra"]
18+
},
19+
{
20+
"name": "singular key from an array",
21+
"selector": "$.some[~'1']",
22+
"document": { "some": ["foo", "bar"] },
23+
"result": [],
24+
"result_paths": [],
25+
"tags": ["extra"]
26+
},
27+
{
28+
"name": "singular key from an object, shorthand",
29+
"selector": "$.some.~other",
30+
"document": { "some": { "other": "foo", "thing": "bar" } },
31+
"result": ["other"],
32+
"result_paths": ["$['some'][~'other']"],
33+
"tags": ["extra"]
34+
},
35+
{
36+
"name": "recursive key from an object",
37+
"selector": "$.some..[~'other']",
38+
"document": {
39+
"some": { "other": "foo", "thing": "bar", "else": { "other": "baz" } }
40+
},
41+
"result": ["other", "other"],
42+
"result_paths": ["$['some'][~'other']", "$['some']['else'][~'other']"],
43+
"tags": ["extra"]
44+
},
45+
{
46+
"name": "recursive key from an object, shorthand",
47+
"selector": "$.some..~other",
48+
"document": {
49+
"some": { "other": "foo", "thing": "bar", "else": { "other": "baz" } }
50+
},
51+
"result": ["other", "other"],
52+
"result_paths": ["$['some'][~'other']", "$['some']['else'][~'other']"],
53+
"tags": ["extra"]
54+
},
55+
{
56+
"name": "recursive key from an object, does not exist",
57+
"selector": "$.some..[~'nosuchthing']",
58+
"document": {
59+
"some": { "other": "foo", "thing": "bar", "else": { "other": "baz" } }
60+
},
61+
"result": [],
62+
"result_paths": [],
63+
"tags": ["extra"]
64+
},
65+
{
66+
"name": "key of nested object",
67+
"selector": "$.a[0].~c",
68+
"document": { "a": [{ "b": "x", "c": "z" }, { "b": "y" }] },
69+
"result": ["c"],
70+
"result_paths": ["$['a'][0][~'c']"],
71+
"tags": ["extra"]
72+
},
73+
{
74+
"name": "key does not exist",
75+
"selector": "$.a[1].~c",
76+
"document": { "a": [{ "b": "x", "c": "z" }, { "b": "y" }] },
77+
"result": [],
78+
"result_paths": [],
79+
"tags": ["extra"]
80+
},
81+
{
82+
"name": "descendant, single quoted key",
83+
"selector": "$..[~'b']",
84+
"document": { "a": [{ "b": "x", "c": "z" }, { "b": "y" }] },
85+
"result": ["b", "b"],
86+
"result_paths": ["$['a'][0][~'b']", "$['a'][1][~'b']"],
87+
"tags": ["extra"]
88+
},
89+
{
90+
"name": "descendant, double quoted key",
91+
"selector": "$..[~\"b\"]",
92+
"document": { "a": [{ "b": "x", "c": "z" }, { "b": "y" }] },
93+
"result": ["b", "b"],
94+
"result_paths": ["$['a'][0][~'b']", "$['a'][1][~'b']"],
95+
"tags": ["extra"]
96+
}
97+
]
98+
}

tests/keys_filter_selector.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"tests": [
3+
{
4+
"name": "filter keys from an object",
5+
"selector": "$.some[~?match(@, '^b.*')]",
6+
"document": { "some": { "other": "foo", "thing": "bar" } },
7+
"result": ["thing"],
8+
"result_paths": ["$['some'][~'thing']"],
9+
"tags": ["extra"]
10+
},
11+
{
12+
"name": "keys filter selector, conditionally select object keys",
13+
"selector": "$.*[~?length(@) > 2]",
14+
"document": [
15+
{ "a": [1, 2, 3], "b": [4, 5] },
16+
{ "c": { "x": [1, 2] } },
17+
{ "d": [1, 2, 3] }
18+
],
19+
"result": ["a", "d"],
20+
"result_paths": ["$[0][~'a']", "$[2][~'d']"],
21+
"tags": ["extra"]
22+
},
23+
{
24+
"name": "keys filter selector, existence test",
25+
"selector": "$.*[~?@.x]",
26+
"document": [
27+
{ "a": [1, 2, 3], "b": [4, 5] },
28+
{ "c": { "x": [1, 2] } },
29+
{ "d": [1, 2, 3] }
30+
],
31+
"result": ["c"],
32+
"result_paths": ["$[1][~'c']"],
33+
"tags": ["extra"]
34+
},
35+
{
36+
"name": "keys filter selector, keys from an array",
37+
"selector": "$[~?(true == true)]",
38+
"document": [
39+
{ "a": [1, 2, 3], "b": [4, 5] },
40+
{ "c": { "x": [1, 2] } },
41+
{ "d": [1, 2, 3] }
42+
],
43+
"result": [],
44+
"result_paths": [],
45+
"tags": ["extra"]
46+
}
47+
]
48+
}

tests/keys_selector.json

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"tests": [
3+
{
4+
"name": "keys from an object",
5+
"selector": "$.some[~]",
6+
"document": { "some": { "other": "foo", "thing": "bar" } },
7+
"result": ["other", "thing"],
8+
"result_paths": ["$['some'][~'other']", "$['some'][~'thing']"],
9+
"tags": ["extra"]
10+
},
11+
{
12+
"name": "shorthand keys from an object",
13+
"selector": "$.some.~",
14+
"document": { "some": { "other": "foo", "thing": "bar" } },
15+
"result": ["other", "thing"],
16+
"result_paths": ["$['some'][~'other']", "$['some'][~'thing']"],
17+
"tags": ["extra"]
18+
},
19+
{
20+
"name": "keys from an array",
21+
"selector": "$.some[~]",
22+
"document": { "some": ["other", "thing"] },
23+
"result": [],
24+
"result_paths": [],
25+
"tags": ["extra"]
26+
},
27+
{
28+
"name": "shorthand keys from an array",
29+
"selector": "$.some.~",
30+
"document": { "some": ["other", "thing"] },
31+
"result": [],
32+
"result_paths": [],
33+
"tags": ["extra"]
34+
},
35+
{
36+
"name": "recurse object keys",
37+
"selector": "$..~",
38+
"document": { "some": { "thing": "else", "foo": { "bar": "baz" } } },
39+
"result": ["some", "thing", "foo", "bar"],
40+
"result_paths": [
41+
"$[~'some']",
42+
"$['some'][~'thing']",
43+
"$['some'][~'foo']",
44+
"$['some']['foo'][~'bar']"
45+
],
46+
"tags": ["extra"]
47+
},
48+
{
49+
"name": "keys selector, object key",
50+
"selector": "$.a[0].~",
51+
"document": { "a": [{ "b": "x", "c": "z" }, { "b": "y" }] },
52+
"result": ["b", "c"],
53+
"result_paths": ["$['a'][0][~'b']", "$['a'][0][~'c']"],
54+
"tags": ["extra"]
55+
},
56+
{
57+
"name": "keys selector, array key",
58+
"selector": "$.a.~",
59+
"document": { "a": [{ "b": "x", "c": "z" }, { "b": "y" }] },
60+
"result": [],
61+
"result_paths": [],
62+
"tags": ["extra"]
63+
},
64+
{
65+
"name": "keys selector, descendant keys",
66+
"selector": "$..[~]",
67+
"document": { "a": [{ "b": "x", "c": "z" }, { "b": "y" }] },
68+
"result": ["a", "b", "c", "b"],
69+
"result_paths": [
70+
"$[~'a']",
71+
"$['a'][0][~'b']",
72+
"$['a'][0][~'c']",
73+
"$['a'][1][~'b']"
74+
],
75+
"tags": ["extra"]
76+
}
77+
]
78+
}

tests/membership_operators.json

Whitespace-only changes.

tests/pseudo_root_identifier.json

Whitespace-only changes.

tests/query_intersection.json

Whitespace-only changes.

tests/query_union.json

Whitespace-only changes.

0 commit comments

Comments
 (0)