Repository navigation
Expand file tree
/
Copy pathtest_match_function.py
More file actions
60 lines (44 loc) · 1.43 KB
/
Copy pathtest_match_function.py
File metadata and controls
60 lines (44 loc) · 1.43 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
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="match a regex",
path="$.some[?match(@.thing, 'fo[a-z]')]",
data={"some": [{"thing": "foo"}]},
want=[{"thing": "foo"}],
),
Case(
description="regex with no match",
path="$.some[?match(@.thing, 'fo[a-z]')]",
data={"some": [{"thing": "foO"}]},
want=[],
),
]
@pytest.fixture()
def env() -> JSONPathEnvironment:
return JSONPathEnvironment()
@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description"))
def test_match_function(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_match_function_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
# TODO: test error conditions