|
| 1 | +import asyncio |
| 2 | +import json |
| 3 | +import operator |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from jsonpath import JSONPathEnvironment |
| 8 | +from jsonpath import JSONPathSyntaxError |
| 9 | +from jsonpath import NodeList |
| 10 | + |
| 11 | +from ._cts_case import Case |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture() |
| 15 | +def env() -> JSONPathEnvironment: |
| 16 | + return JSONPathEnvironment(strict=False) |
| 17 | + |
| 18 | + |
| 19 | +with open("tests/pseudo_root_identifier.json", encoding="utf8") as fd: |
| 20 | + data = [Case(**case) for case in json.load(fd)["tests"]] |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize("case", data, ids=operator.attrgetter("name")) |
| 24 | +def test_pseudo_root_identifier(env: JSONPathEnvironment, case: Case) -> None: |
| 25 | + assert case.document is not None |
| 26 | + nodes = NodeList(env.finditer(case.selector, case.document)) |
| 27 | + |
| 28 | + if case.results is not None: |
| 29 | + assert case.results_paths is not None |
| 30 | + assert nodes.values() in case.results |
| 31 | + assert nodes.paths() in case.results_paths |
| 32 | + else: |
| 33 | + assert case.result_paths is not None |
| 34 | + assert nodes.values() == case.result |
| 35 | + assert nodes.paths() == case.result_paths |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.parametrize("case", data, ids=operator.attrgetter("name")) |
| 39 | +def test_pseudo_root_identifier_async(env: JSONPathEnvironment, case: Case) -> None: |
| 40 | + async def coro() -> NodeList: |
| 41 | + assert case.document is not None |
| 42 | + it = await env.finditer_async(case.selector, case.document) |
| 43 | + return NodeList([node async for node in it]) |
| 44 | + |
| 45 | + nodes = asyncio.run(coro()) |
| 46 | + |
| 47 | + if case.results is not None: |
| 48 | + assert case.results_paths is not None |
| 49 | + assert nodes.values() in case.results |
| 50 | + assert nodes.paths() in case.results_paths |
| 51 | + else: |
| 52 | + assert case.result_paths is not None |
| 53 | + assert nodes.values() == case.result |
| 54 | + assert nodes.paths() == case.result_paths |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.parametrize("case", data, ids=operator.attrgetter("name")) |
| 58 | +def test_pseudo_root_identifier_fails_in_strict_mode(case: Case) -> None: |
| 59 | + env = JSONPathEnvironment(strict=True) |
| 60 | + |
| 61 | + with pytest.raises(JSONPathSyntaxError): |
| 62 | + env.compile(case.selector) |
0 commit comments