File: test_strictness.py

package info (click to toggle)
python-jsonpath 2.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,028 kB
  • sloc: python: 9,473; makefile: 6
file content (107 lines) | stat: -rw-r--r-- 2,941 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import pytest

from jsonpath import JSONPathEnvironment
from jsonpath import JSONPathNameError


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


def test_leading_whitespace(env: JSONPathEnvironment) -> None:
    query = "  $.a"
    data = {"a": 1}
    assert env.findall(query, data) == [1]


def test_trailing_whitespace(env: JSONPathEnvironment) -> None:
    query = "$.a  "
    data = {"a": 1}
    assert env.findall(query, data) == [1]


def test_index_as_object_name(env: JSONPathEnvironment) -> None:
    query = "$.a[0]"
    data = {"a": {"0": 1}}
    assert env.findall(query, data) == [1]


def test_alternative_and(env: JSONPathEnvironment) -> None:
    query = "$[?@.a and @.b]"
    data = [{"a": True, "b": False}]
    assert env.findall(query, data) == [{"a": True, "b": False}]


def test_alternative_or(env: JSONPathEnvironment) -> None:
    query = "$[?@.a or @.c]"
    data = [{"a": True, "b": False}, {"c": 99}]
    assert env.findall(query, data) == [{"a": True, "b": False}, {"c": 99}]


def test_alternative_null(env: JSONPathEnvironment) -> None:
    query = "$[?@.a==Null]"
    data = [{"a": None, "d": "e"}, {"a": "c", "d": "f"}]
    assert env.findall(query, data) == [{"a": None, "d": "e"}]


def test_none(env: JSONPathEnvironment) -> None:
    query = "$[?@.a==None]"
    data = [{"a": None, "d": "e"}, {"a": "c", "d": "f"}]
    assert env.findall(query, data) == [{"a": None, "d": "e"}]


def test_implicit_root_identifier(
    env: JSONPathEnvironment,
) -> None:
    query = "a['p']"
    data = {
        "a": {"j": [1, 2, 3], "p": {"q": [4, 5, 6]}},
        "b": ["j", "p", "q"],
    }

    assert env.findall(query, data) == [{"q": [4, 5, 6]}]


def test_singular_path_selector_without_root_identifier(
    env: JSONPathEnvironment,
) -> None:
    query = "$.a[b[1]]"
    data = {
        "a": {"j": [1, 2, 3], "p": {"q": [4, 5, 6]}},
        "b": ["j", "p", "q"],
        "c d": {"x": {"y": 1}},
    }

    assert env.findall(query, data) == [{"q": [4, 5, 6]}]


def test_isinstance_is_disabled_in_strict_mode() -> None:
    env = JSONPathEnvironment(strict=True)

    query = "$.some[?is(@.thing, 'string')]"
    with pytest.raises(JSONPathNameError):
        env.compile(query)

    query = "$.some[?isinstance(@.thing, 'string')]"
    with pytest.raises(JSONPathNameError):
        env.compile(query)


def test_typeof_is_disabled_in_strict_mode() -> None:
    env = JSONPathEnvironment(strict=True)

    query = "$.some[?type(@.thing) == 'string']"
    with pytest.raises(JSONPathNameError):
        env.compile(query)

    query = "$.some[?typeof(@.thing) == 'string']"
    with pytest.raises(JSONPathNameError):
        env.compile(query)


def test_startswith_is_disabled_in_strict_mode() -> None:
    env = JSONPathEnvironment(strict=True)
    query = "$[?startswith(@, 'ab')]"
    with pytest.raises(JSONPathNameError):
        env.compile(query)