Repository navigation
Expand file tree
/
Copy pathtest_find.py
More file actions
175 lines (161 loc) · 4.41 KB
/
Copy pathtest_find.py
File metadata and controls
175 lines (161 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import asyncio
import dataclasses
import operator
from typing import Any
from typing import List
from typing import Mapping
from typing import Sequence
from typing import Union
import pytest
from jsonpath import JSONPathEnvironment
@dataclasses.dataclass
class Case:
description: str
path: str
data: Union[Sequence[Any], Mapping[str, Any]]
want: Union[Sequence[Any], Mapping[str, Any]]
TEST_CASES = [
Case(
description="property key that looks like an index",
path="$[some][0]",
data={"some": {"0": "thing"}},
want=["thing"],
),
Case(
description="slice a mapping",
path="$.some[0:4]",
data={"some": {"thing": "else"}},
want=[],
),
Case(
description="keys from a mapping",
path="$.some[~]",
data={"some": {"thing": "else"}},
want=["thing"],
),
Case(
description="keys from a sequence",
path="$.some.~",
data={"some": ["thing", "else"]},
want=[],
),
Case(
description="match key pattern",
path="$.some[?match(#, 'thing[0-9]+')]",
data={
"some": {
"thing1": {"foo": 1},
"thing2": {"foo": 2},
"other": {"foo": 3},
}
},
want=[{"foo": 1}, {"foo": 2}],
),
Case(
description="select root value using fake root",
data={"some": {"thing": 42}},
want=[{"some": {"thing": 42}}],
),
Case(
description="fake root in a filter query",
data={"some": {"thing": 42}, "num": 7},
want=[{"some": {"thing": 42}, "num": 7}],
),
Case(
description="recurse object keys",
path="$..~",
data={"some": {"thing": "else", "foo": {"bar": "baz"}}},
want=["some", "thing", "foo", "bar"],
),
Case(
description="logical expr existence tests",
data=[{"a": True, "b": False}],
want=[{"a": True, "b": False}],
),
Case(
description="logical expr existence tests, alternate and",
data=[{"a": True, "b": False}],
want=[{"a": True, "b": False}],
),
Case(
description="array contains literal",
data=[{"a": ["foo", "bar"]}, {"a": ["bar"]}],
want=[
{
"a": ["foo", "bar"],
}
],
),
Case(
description="object contains literal",
data=[{"a": {"foo": "bar"}}, {"a": {"bar": "baz"}}],
want=[
{
"a": {"foo": "bar"},
}
],
),
Case(
description="literal in array",
path="$[?'foo' in @.a]",
data=[{"a": ["foo", "bar"]}, {"a": ["bar"]}],
want=[
{
"a": ["foo", "bar"],
}
],
),
Case(
description="literal in object",
path="$[?'foo' in @.a]",
data=[{"a": {"foo": "bar"}}, {"a": {"bar": "baz"}}],
want=[
{
"a": {"foo": "bar"},
}
],
),
Case(
description="issue 72, orders",
path="orders",
data={"orders": [1, 2, 3]},
want=[[1, 2, 3]],
),
Case(
description="issue 72, andy",
path="andy",
data={"andy": [1, 2, 3]},
want=[[1, 2, 3]],
),
Case(
description="quoted reserved word, and",
path="['and']",
data={"and": [1, 2, 3]},
want=[[1, 2, 3]],
),
Case(
description="quoted reserved word, or",
path="['or']",
data={"or": [1, 2, 3]},
want=[[1, 2, 3]],
),
]
@pytest.fixture()
def env() -> JSONPathEnvironment:
return JSONPathEnvironment()
@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description"))
def test_find(env: JSONPathEnvironment, case: Case) -> None:
path = env.compile(case.path)
assert path.findall(case.data) == case.want
@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description"))
def test_find_async(env: JSONPathEnvironment, case: Case) -> None:
path = env.compile(case.path)
async def coro() -> List[object]:
return await path.findall_async(case.data)
assert asyncio.run(coro()) == case.want