File: test_compare.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 (94 lines) | stat: -rw-r--r-- 1,818 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
"""Default filter expression comparison test cases."""
import dataclasses
import operator

import pytest

from jsonpath import JSONPathEnvironment


@dataclasses.dataclass
class Case:
    description: str
    left: object
    op: str
    right: object
    want: bool


TEST_CASES = [
    Case(
        description="true and true",
        left=True,
        op="&&",
        right=True,
        want=True,
    ),
    Case(
        description="left in right",
        left="thing",
        op="in",
        right=["some", "thing"],
        want=True,
    ),
    Case(
        description="right contains left",
        left=["some", "thing"],
        op="contains",
        right="thing",
        want=True,
    ),
    Case(
        description="string >= string",
        left="thing",
        op=">=",
        right="thing",
        want=True,
    ),
    Case(
        description="string < string",
        left="abc",
        op="<",
        right="bcd",
        want=True,
    ),
    Case(
        description="string > string",
        left="bcd",
        op=">",
        right="abcd",
        want=True,
    ),
    Case(
        description="int >= int",
        left=2,
        op=">=",
        right=1,
        want=True,
    ),
    Case(
        description="nil >= nil",
        left=None,
        op=">=",
        right=None,
        want=True,
    ),
    Case(
        description="nil <= nil",
        left=None,
        op="<=",
        right=None,
        want=True,
    ),
]


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


@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description"))
def test_compare(env: JSONPathEnvironment, case: Case) -> None:
    result = env.compare(case.left, case.op, case.right)
    assert result == case.want