Repository navigation
Expand file tree
/
Copy pathtest_find.py
More file actions
81 lines (67 loc) · 1.95 KB
/
Copy pathtest_find.py
File metadata and controls
81 lines (67 loc) · 1.95 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
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}],
),
]
@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