|
| 1 | +import asyncio |
| 2 | +import dataclasses |
| 3 | +import operator |
| 4 | +from typing import Any |
| 5 | +from typing import List |
| 6 | +from typing import Mapping |
| 7 | +from typing import Sequence |
| 8 | +from typing import Union |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from jsonpath import JSONPathEnvironment |
| 13 | + |
| 14 | + |
| 15 | +@dataclasses.dataclass |
| 16 | +class Case: |
| 17 | + description: str |
| 18 | + path: str |
| 19 | + data: Union[Sequence[Any], Mapping[str, Any]] |
| 20 | + want: Union[Sequence[Any], Mapping[str, Any]] |
| 21 | + want_paths: List[str] |
| 22 | + |
| 23 | + |
| 24 | +TEST_CASES = [ |
| 25 | + Case( |
| 26 | + description="key selector, key of nested object", |
| 27 | + path="$.a[0].~c", |
| 28 | + data={ |
| 29 | + "a": [{"b": "x", "c": "z"}, {"b": "y"}], |
| 30 | + }, |
| 31 | + want=["c"], |
| 32 | + want_paths=["$['a'][0][~'c']"], |
| 33 | + ), |
| 34 | + Case( |
| 35 | + description="key selector, key does not exist", |
| 36 | + path="$.a[1].~c", |
| 37 | + data={ |
| 38 | + "a": [{"b": "x", "c": "z"}, {"b": "y"}], |
| 39 | + }, |
| 40 | + want=[], |
| 41 | + want_paths=[], |
| 42 | + ), |
| 43 | + Case( |
| 44 | + description="key selector, descendant, single quoted key", |
| 45 | + path="$..[~'b']", |
| 46 | + data={ |
| 47 | + "a": [{"b": "x", "c": "z"}, {"b": "y"}], |
| 48 | + }, |
| 49 | + want=["b", "b"], |
| 50 | + want_paths=["$['a'][0][~'b']", "$['a'][1][~'b']"], |
| 51 | + ), |
| 52 | + Case( |
| 53 | + description="key selector, descendant, double quoted key", |
| 54 | + path='$..[~"b"]', |
| 55 | + data={ |
| 56 | + "a": [{"b": "x", "c": "z"}, {"b": "y"}], |
| 57 | + }, |
| 58 | + want=["b", "b"], |
| 59 | + want_paths=["$['a'][0][~'b']", "$['a'][1][~'b']"], |
| 60 | + ), |
| 61 | + Case( |
| 62 | + description="keys selector, object key", |
| 63 | + path="$.a[0].~", |
| 64 | + data={ |
| 65 | + "a": [{"b": "x", "c": "z"}, {"b": "y"}], |
| 66 | + }, |
| 67 | + want=["b", "c"], |
| 68 | + want_paths=["$['a'][0][~'b']", "$['a'][0][~'c']"], |
| 69 | + ), |
| 70 | + Case( |
| 71 | + description="keys selector, array key", |
| 72 | + path="$.a.~", |
| 73 | + data={ |
| 74 | + "a": [{"b": "x", "c": "z"}, {"b": "y"}], |
| 75 | + }, |
| 76 | + want=[], |
| 77 | + want_paths=[], |
| 78 | + ), |
| 79 | + Case( |
| 80 | + description="keys selector, descendant keys", |
| 81 | + path="$..[~]", |
| 82 | + data={ |
| 83 | + "a": [{"b": "x", "c": "z"}, {"b": "y"}], |
| 84 | + }, |
| 85 | + want=["a", "b", "c", "b"], |
| 86 | + want_paths=["$[~'a']", "$['a'][0][~'b']", "$['a'][0][~'c']", "$['a'][1][~'b']"], |
| 87 | + ), |
| 88 | + Case( |
| 89 | + description="keys filter selector, conditionally select object keys", |
| 90 | + path="$.*[~?length(@) > 2]", |
| 91 | + data=[{"a": [1, 2, 3], "b": [4, 5]}, {"c": {"x": [1, 2]}}, {"d": [1, 2, 3]}], |
| 92 | + want=["a", "d"], |
| 93 | + want_paths=["$[0][~'a']", "$[2][~'d']"], |
| 94 | + ), |
| 95 | + Case( |
| 96 | + description="keys filter selector, existence test", |
| 97 | + |
| 98 | + data=[{"a": [1, 2, 3], "b": [4, 5]}, {"c": {"x": [1, 2]}}, {"d": [1, 2, 3]}], |
| 99 | + want=["c"], |
| 100 | + want_paths=["$[1][~'c']"], |
| 101 | + ), |
| 102 | + Case( |
| 103 | + description="keys filter selector, keys from an array", |
| 104 | + path="$[~?(true == true)]", |
| 105 | + data=[{"a": [1, 2, 3], "b": [4, 5]}, {"c": {"x": [1, 2]}}, {"d": [1, 2, 3]}], |
| 106 | + want=[], |
| 107 | + want_paths=[], |
| 108 | + ), |
| 109 | + Case( |
| 110 | + description="current key identifier, match on object names", |
| 111 | + path="$[?match(#, '^ab.*') && length(@) > 0 ]", |
| 112 | + data={"abc": [1, 2, 3], "def": [4, 5], "abx": [6], "aby": []}, |
| 113 | + want=[[1, 2, 3], [6]], |
| 114 | + want_paths=["$['abc']", "$['abx']"], |
| 115 | + ), |
| 116 | + Case( |
| 117 | + description="current key identifier, compare current array index", |
| 118 | + path="$.abc[?(# >= 1)]", |
| 119 | + data={"abc": [1, 2, 3], "def": [4, 5], "abx": [6], "aby": []}, |
| 120 | + want=[2, 3], |
| 121 | + want_paths=["$['abc'][1]", "$['abc'][2]"], |
| 122 | + ), |
| 123 | +] |
| 124 | + |
| 125 | + |
| 126 | +@pytest.fixture() |
| 127 | +def env() -> JSONPathEnvironment: |
| 128 | + return JSONPathEnvironment() |
| 129 | + |
| 130 | + |
| 131 | +@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description")) |
| 132 | +def test_find_extra_examples(env: JSONPathEnvironment, case: Case) -> None: |
| 133 | + path = env.compile(case.path) |
| 134 | + assert path.findall(case.data) == case.want |
| 135 | + assert list(path.query(case.data).locations()) == case.want_paths |
| 136 | + |
| 137 | + |
| 138 | +@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description")) |
| 139 | +def test_find_extra_async(env: JSONPathEnvironment, case: Case) -> None: |
| 140 | + path = env.compile(case.path) |
| 141 | + |
| 142 | + async def coro() -> List[object]: |
| 143 | + return await path.findall_async(case.data) |
| 144 | + |
| 145 | + assert asyncio.run(coro()) == case.want |
0 commit comments