-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_startswith_function.py
More file actions
51 lines (41 loc) · 1.26 KB
/
test_startswith_function.py
File metadata and controls
51 lines (41 loc) · 1.26 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
import dataclasses
import operator
from typing import Any
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="current value start with string",
path="$[?startswith(@, 'ab')]",
data={"x": "abc", "y": "abx", "z": "bcd", "-": "ab"},
want=["abc", "abx", "ab"],
),
Case(
description="current key start with string",
path="$[?startswith(#, 'ab')]",
data={"abc": 1, "abx": 2, "bcd": 3, "ab": 4},
want=[1, 2, 4],
),
Case(
description="value is not a string",
path="$[?startswith(@, 'ab')]",
data={"abc": 1, "abx": 2, "bcd": 3, "ab": 4},
want=[],
),
]
@pytest.fixture()
def env() -> JSONPathEnvironment:
return JSONPathEnvironment()
@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description"))
def test_isinstance_function(env: JSONPathEnvironment, case: Case) -> None:
path = env.compile(case.path)
assert path.findall(case.data) == case.want