Skip to content

Commit f6909ca

Browse files
committed
More tidy of test cases
1 parent e087f70 commit f6909ca

19 files changed

+274
-341
lines changed

tests/_cts_case.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from typing import Sequence
1111
from typing import Union
1212

13+
from jsonpath import NodeList
14+
1315

1416
@dataclass
1517
class Case:
@@ -45,3 +47,14 @@ def as_dict(self) -> Dict[str, Any]:
4547
rv["tags"] = self.tags
4648

4749
return rv
50+
51+
def assert_nodes(self, nodes: NodeList) -> None:
52+
"""Assert that `nodes` matches this test case."""
53+
if self.results is not None:
54+
assert self.results_paths is not None
55+
assert nodes.values() in self.results
56+
assert nodes.paths() in self.results_paths
57+
else:
58+
assert self.result_paths is not None
59+
assert nodes.values() == self.result
60+
assert nodes.paths() == self.result_paths

tests/consensus.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
We've deliberately named this file so as to exclude it when running `pytest` or
99
`hatch run test`. Target it specifically using `pytest tests/consensus.py`.
1010
"""
11+
1112
import operator
1213
import unittest
1314
from dataclasses import dataclass
@@ -44,7 +45,7 @@ class Query:
4445
}
4546

4647
SKIP = {
47-
"bracket_notation_with_number_on_object": "We support unquoted property names",
48+
# "bracket_notation_with_number_on_object": "We support unquoted property names",
4849
"dot_notation_with_number_-1": "conflict with compliance",
4950
"dot_notation_with_number_on_object": "conflict with compliance",
5051
}

tests/query_intersection.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"tests": [
3+
{
4+
"name": "intersection of two paths, no common items",
5+
"selector": "$.some & $.thing",
6+
"document": {
7+
"some": [1, 2, 3],
8+
"thing": [4, 5, 6],
9+
"other": ["a", "b", "c"]
10+
},
11+
"result": [],
12+
"result_paths": [],
13+
"tags": ["extra"]
14+
},
15+
{
16+
"name": "intersection of two paths, with common items",
17+
"selector": "$.some & $.thing",
18+
"document": {
19+
"some": [1, 2, 3],
20+
"thing": [1, 2, 3],
21+
"other": ["a", "b", "c"]
22+
},
23+
"result": [[1, 2, 3]],
24+
"result_paths": ["$['some']"],
25+
"tags": ["extra"]
26+
}
27+
]
28+
}

tests/query_union.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"tests": [
3+
{
4+
"name": "union of two paths",
5+
"selector": "$.some | $.thing",
6+
"document": {
7+
"some": [1, 2, 3],
8+
"thing": [4, 5, 6],
9+
"other": ["a", "b", "c"]
10+
},
11+
"result": [
12+
[1, 2, 3],
13+
[4, 5, 6]
14+
],
15+
"result_paths": ["$['some']", "$['thing']"],
16+
"tags": ["extra"]
17+
},
18+
{
19+
"name": "union of three paths",
20+
"selector": "$.some | $.thing | $.other",
21+
"document": {
22+
"some": [1, 2, 3],
23+
"thing": [4, 5, 6],
24+
"other": ["a", "b", "c"]
25+
},
26+
"result": [
27+
[1, 2, 3],
28+
[4, 5, 6],
29+
["a", "b", "c"]
30+
],
31+
"result_paths": ["$['some']", "$['thing']", "$['other']"],
32+
"tags": ["extra"]
33+
}
34+
]
35+
}

tests/regex_operator.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"tests": [
3+
{
4+
"name": "regex literal, match",
5+
"selector": "$.some[?(@.thing =~ /fo[a-z]/)]",
6+
"document": { "some": [{ "thing": "foo" }] },
7+
"result": [{ "thing": "foo" }],
8+
"result_paths": ["$['some'][0]"],
9+
"tags": ["extra"]
10+
},
11+
{
12+
"name": "regex literal, no match",
13+
"selector": "$.some[?(@.thing =~ /fo[a-z]/)]",
14+
"document": { "some": [{ "thing": "foO" }] },
15+
"result": [],
16+
"result_paths": [],
17+
"tags": ["extra"]
18+
},
19+
{
20+
"name": "regex literal, case insensitive match",
21+
"selector": "$.some[?(@.thing =~ /fo[a-z]/i)]",
22+
"document": { "some": [{ "thing": "foO" }] },
23+
"result": [{ "thing": "foO" }],
24+
"result_paths": ["$['some'][0]"],
25+
"tags": ["extra"]
26+
},
27+
{
28+
"name": "regex literal, escaped slash",
29+
"selector": "$.some[?(@.thing =~ /fo\\\\[a-z]/)]",
30+
"document": { "some": [{ "thing": "fo\\b" }] },
31+
"result": [{ "thing": "fo\\b" }],
32+
"result_paths": ["$['some'][0]"],
33+
"tags": ["extra"]
34+
}
35+
]
36+
}

tests/test_concrete_path.py

Lines changed: 0 additions & 65 deletions
This file was deleted.

tests/test_current_key_identifier.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,7 @@ def env() -> JSONPathEnvironment:
2424
def test_current_key_identifier(env: JSONPathEnvironment, case: Case) -> None:
2525
assert case.document is not None
2626
nodes = NodeList(env.finditer(case.selector, case.document))
27-
28-
if case.results is not None:
29-
assert case.results_paths is not None
30-
assert nodes.values() in case.results
31-
assert nodes.paths() in case.results_paths
32-
else:
33-
assert case.result_paths is not None
34-
assert nodes.values() == case.result
35-
assert nodes.paths() == case.result_paths
27+
case.assert_nodes(nodes)
3628

3729

3830
@pytest.mark.parametrize("case", data, ids=operator.attrgetter("name"))
@@ -43,15 +35,7 @@ async def coro() -> NodeList:
4335
return NodeList([node async for node in it])
4436

4537
nodes = asyncio.run(coro())
46-
47-
if case.results is not None:
48-
assert case.results_paths is not None
49-
assert nodes.values() in case.results
50-
assert nodes.paths() in case.results_paths
51-
else:
52-
assert case.result_paths is not None
53-
assert nodes.values() == case.result
54-
assert nodes.paths() == case.result_paths
38+
case.assert_nodes(nodes)
5539

5640

5741
@pytest.mark.parametrize("case", data, ids=operator.attrgetter("name"))

tests/test_find_compound_path.py

Lines changed: 0 additions & 80 deletions
This file was deleted.

tests/test_key_selector.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,7 @@ def env() -> JSONPathEnvironment:
2424
def test_key_selector(env: JSONPathEnvironment, case: Case) -> None:
2525
assert case.document is not None
2626
nodes = NodeList(env.finditer(case.selector, case.document))
27-
28-
if case.results is not None:
29-
assert case.results_paths is not None
30-
assert nodes.values() in case.results
31-
assert nodes.paths() in case.results_paths
32-
else:
33-
assert case.result_paths is not None
34-
assert nodes.values() == case.result
35-
assert nodes.paths() == case.result_paths
27+
case.assert_nodes(nodes)
3628

3729

3830
@pytest.mark.parametrize("case", data, ids=operator.attrgetter("name"))
@@ -43,15 +35,7 @@ async def coro() -> NodeList:
4335
return NodeList([node async for node in it])
4436

4537
nodes = asyncio.run(coro())
46-
47-
if case.results is not None:
48-
assert case.results_paths is not None
49-
assert nodes.values() in case.results
50-
assert nodes.paths() in case.results_paths
51-
else:
52-
assert case.result_paths is not None
53-
assert nodes.values() == case.result
54-
assert nodes.paths() == case.result_paths
38+
case.assert_nodes(nodes)
5539

5640

5741
@pytest.mark.parametrize("case", data, ids=operator.attrgetter("name"))

tests/test_keys_filter_selector.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,7 @@ def env() -> JSONPathEnvironment:
2424
def test_keys_filter_selector(env: JSONPathEnvironment, case: Case) -> None:
2525
assert case.document is not None
2626
nodes = NodeList(env.finditer(case.selector, case.document))
27-
28-
if case.results is not None:
29-
assert case.results_paths is not None
30-
assert nodes.values() in case.results
31-
assert nodes.paths() in case.results_paths
32-
else:
33-
assert case.result_paths is not None
34-
assert nodes.values() == case.result
35-
assert nodes.paths() == case.result_paths
27+
case.assert_nodes(nodes)
3628

3729

3830
@pytest.mark.parametrize("case", data, ids=operator.attrgetter("name"))
@@ -43,15 +35,7 @@ async def coro() -> NodeList:
4335
return NodeList([node async for node in it])
4436

4537
nodes = asyncio.run(coro())
46-
47-
if case.results is not None:
48-
assert case.results_paths is not None
49-
assert nodes.values() in case.results
50-
assert nodes.paths() in case.results_paths
51-
else:
52-
assert case.result_paths is not None
53-
assert nodes.values() == case.result
54-
assert nodes.paths() == case.result_paths
38+
case.assert_nodes(nodes)
5539

5640

5741
@pytest.mark.parametrize("case", data, ids=operator.attrgetter("name"))

0 commit comments

Comments
 (0)