Repository navigation
Expand file tree
/
Copy pathtest_re.py
More file actions
69 lines (55 loc) · 1.76 KB
/
Copy pathtest_re.py
File metadata and controls
69 lines (55 loc) · 1.76 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
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[?(@.thing =~ /fo[a-z]/)]",
data={"some": [{"thing": "foo"}]},
want=[{"thing": "foo"}],
),
Case(
description="regex with no match",
path="$.some[?(@.thing =~ /fo[a-z]/)]",
data={"some": [{"thing": "foO"}]},
want=[],
),
Case(
description="case insensitive match",
path="$.some[?(@.thing =~ /fo[a-z]/i)]",
data={"some": [{"thing": "foO"}]},
want=[{"thing": "foO"}],
),
Case(
description="escaped slash",
path="$.some[?(@.thing =~ /fo\\\\[a-z]/)]",
data={"some": [{"thing": "fo\\b"}]},
want=[{"thing": "fo\\b"}],
),
]
@pytest.fixture()
def env() -> JSONPathEnvironment:
return JSONPathEnvironment()
@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description"))
def test_filter_regex(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_filter_regex_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