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
|
import dataclasses
import operator
import pytest
from jsonpath import JSONPathEnvironment
@dataclasses.dataclass
class Case:
description: str
path: str
want: str
TEST_CASES = [
Case(
description="union of two paths",
path="$.some | $.thing",
want="$['some'] | $['thing']",
),
Case(
description="union of three paths",
path="$.some | $.thing | [0]",
want="$['some'] | $['thing'] | $[0]",
),
Case(
description="intersection of two paths",
path="$.some.* & $.thing.*",
want="$['some'][*] & $['thing'][*]",
),
Case(
description="intersection then union",
path="$.some.* & $.thing.* | $.other",
want="$['some'][*] & $['thing'][*] | $['other']",
),
]
@pytest.fixture()
def env() -> JSONPathEnvironment:
return JSONPathEnvironment()
@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description"))
def test_parse_compound_path(env: JSONPathEnvironment, case: Case) -> None:
path = env.compile(case.path)
assert str(path) == case.want
|