|
| 1 | +import asyncio |
| 2 | +import dataclasses |
| 3 | +import operator |
| 4 | +from typing import Any |
| 5 | +from typing import List |
| 6 | +from typing import Mapping |
| 7 | +from typing import Sequence |
| 8 | +from typing import Union |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from jsonpath import JSONPathEnvironment |
| 13 | + |
| 14 | + |
| 15 | +@dataclasses.dataclass |
| 16 | +class Case: |
| 17 | + description: str |
| 18 | + path: str |
| 19 | + data: Union[Sequence[Any], Mapping[str, Any]] |
| 20 | + want: Union[Sequence[Any], Mapping[str, Any]] |
| 21 | + |
| 22 | + |
| 23 | +SOME_OBJECT = object() |
| 24 | + |
| 25 | +TEST_CASES = [ |
| 26 | + Case( |
| 27 | + description="type of a string", |
| 28 | + path="$.some[?is(@.thing, 'string')]", |
| 29 | + data={"some": [{"thing": "foo"}]}, |
| 30 | + want=[{"thing": "foo"}], |
| 31 | + ), |
| 32 | + Case( |
| 33 | + description="not a string", |
| 34 | + path="$.some[?is(@.thing, 'string')]", |
| 35 | + data={"some": [{"thing": 1}]}, |
| 36 | + want=[], |
| 37 | + ), |
| 38 | + Case( |
| 39 | + description="type of undefined", |
| 40 | + path="$.some[?is(@.other, 'undefined')]", # things without `other` |
| 41 | + data={"some": [{"thing": "foo"}]}, |
| 42 | + want=[{"thing": "foo"}], |
| 43 | + ), |
| 44 | + Case( |
| 45 | + description="type of None", |
| 46 | + path="$.some[?is(@.thing, 'null')]", |
| 47 | + data={"some": [{"thing": None}]}, |
| 48 | + want=[{"thing": None}], |
| 49 | + ), |
| 50 | + Case( |
| 51 | + description="type of array-like", |
| 52 | + path="$.some[?is(@.thing, 'array')]", |
| 53 | + data={"some": [{"thing": [1, 2, 3]}]}, |
| 54 | + want=[{"thing": [1, 2, 3]}], |
| 55 | + ), |
| 56 | + Case( |
| 57 | + description="type of mapping", |
| 58 | + path="$.some[?is(@.thing, 'object')]", |
| 59 | + data={"some": [{"thing": {"other": 1}}]}, |
| 60 | + want=[{"thing": {"other": 1}}], |
| 61 | + ), |
| 62 | + Case( |
| 63 | + description="type of bool", |
| 64 | + path="$.some[?is(@.thing, 'boolean')]", |
| 65 | + data={"some": [{"thing": True}]}, |
| 66 | + want=[{"thing": True}], |
| 67 | + ), |
| 68 | + Case( |
| 69 | + description="type of int", |
| 70 | + path="$.some[?is(@.thing, 'number')]", |
| 71 | + data={"some": [{"thing": 1}]}, |
| 72 | + want=[{"thing": 1}], |
| 73 | + ), |
| 74 | + Case( |
| 75 | + description="type of float", |
| 76 | + path="$.some[?is(@.thing, 'number')]", |
| 77 | + data={"some": [{"thing": 1.1}]}, |
| 78 | + want=[{"thing": 1.1}], |
| 79 | + ), |
| 80 | + Case( |
| 81 | + description="none of the above", |
| 82 | + path="$.some[?is(@.thing, 'object')]", |
| 83 | + data={"some": [{"thing": SOME_OBJECT}]}, |
| 84 | + want=[{"thing": SOME_OBJECT}], |
| 85 | + ), |
| 86 | +] |
| 87 | + |
| 88 | + |
| 89 | +@pytest.fixture() |
| 90 | +def env() -> JSONPathEnvironment: |
| 91 | + return JSONPathEnvironment() |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description")) |
| 95 | +def test_isinstance_function(env: JSONPathEnvironment, case: Case) -> None: |
| 96 | + path = env.compile(case.path) |
| 97 | + assert path.findall(case.data) == case.want |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description")) |
| 101 | +def test_isinstance_function_async(env: JSONPathEnvironment, case: Case) -> None: |
| 102 | + path = env.compile(case.path) |
| 103 | + |
| 104 | + async def coro() -> List[object]: |
| 105 | + return await path.findall_async(case.data) |
| 106 | + |
| 107 | + assert asyncio.run(coro()) == case.want |
0 commit comments