Skip to content

Commit 2787abb

Browse files
kurtmckeemichaelmior
authored andcommitted
Refactor tests/test_jsonpath.py
1 parent bd667aa commit 2787abb

File tree

3 files changed

+345
-302
lines changed

3 files changed

+345
-302
lines changed

tests/conftest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
3+
4+
@pytest.fixture(autouse=True)
5+
def disable_auto_id_field(monkeypatch):
6+
monkeypatch.setattr("jsonpath_ng.jsonpath.auto_id_field", None)
7+
8+
9+
@pytest.fixture()
10+
def auto_id_field(monkeypatch, disable_auto_id_field):
11+
"""Enable `jsonpath_ng.jsonpath.auto_id_field`."""
12+
13+
field_name = "id"
14+
monkeypatch.setattr("jsonpath_ng.jsonpath.auto_id_field", field_name)
15+
return field_name

tests/helpers.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
def assert_value_equality(results, expected_values):
2+
"""Assert equality between two objects.
3+
4+
*results* must be a list of results as returned by `.find()` methods.
5+
6+
If *expected_values* is a list, then value equality and ordering will be checked.
7+
If *expected_values* is a set, value equality and container length will be checked.
8+
Otherwise, the value of the results will be compared to the expected values.
9+
"""
10+
11+
left_values = [result.value for result in results]
12+
if isinstance(expected_values, list):
13+
assert left_values == expected_values
14+
elif isinstance(expected_values, set):
15+
assert len(left_values) == len(expected_values)
16+
assert set(left_values) == expected_values
17+
else:
18+
assert results.value == expected_values
19+
20+
21+
def assert_full_path_equality(results, expected_full_paths):
22+
"""Assert equality between two objects.
23+
24+
*results* must be a list of results as returned by `.find()` methods.
25+
26+
If *expected_full_paths* is a list, then path equality and ordering will be checked.
27+
If *expected_full_paths* is a set, then path equality and length will be checked.
28+
Otherwise, the full path of the result will be compared to the expected full path.
29+
"""
30+
31+
full_paths = [str(result.full_path) for result in results]
32+
if isinstance(expected_full_paths, list):
33+
assert full_paths == expected_full_paths, full_paths
34+
elif isinstance(expected_full_paths, set):
35+
assert len(full_paths) == len(expected_full_paths)
36+
assert set(full_paths) == expected_full_paths
37+
else:
38+
assert str(results.full_path) == expected_full_paths

0 commit comments

Comments
 (0)