File: test_keys_selector.py

package info (click to toggle)
python-jsonpath 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,024 kB
  • sloc: python: 9,462; makefile: 6
file content (46 lines) | stat: -rw-r--r-- 1,370 bytes parent folder | download
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
import asyncio
import json
import operator

import pytest

from jsonpath import JSONPathEnvironment
from jsonpath import JSONPathSyntaxError
from jsonpath import NodeList

from ._cts_case import Case


@pytest.fixture()
def env() -> JSONPathEnvironment:
    return JSONPathEnvironment(strict=False)


with open("tests/keys_selector.json", encoding="utf8") as fd:
    data = [Case(**case) for case in json.load(fd)["tests"]]


@pytest.mark.parametrize("case", data, ids=operator.attrgetter("name"))
def test_keys_selector(env: JSONPathEnvironment, case: Case) -> None:
    assert case.document is not None
    nodes = NodeList(env.finditer(case.selector, case.document))
    case.assert_nodes(nodes)


@pytest.mark.parametrize("case", data, ids=operator.attrgetter("name"))
def test_keys_selector_async(env: JSONPathEnvironment, case: Case) -> None:
    async def coro() -> NodeList:
        assert case.document is not None
        it = await env.finditer_async(case.selector, case.document)
        return NodeList([node async for node in it])

    nodes = asyncio.run(coro())
    case.assert_nodes(nodes)


@pytest.mark.parametrize("case", data, ids=operator.attrgetter("name"))
def test_keys_selector_fails_in_strict_mode(case: Case) -> None:
    env = JSONPathEnvironment(strict=True)

    with pytest.raises(JSONPathSyntaxError):
        env.compile(case.selector)