-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_filter.py
More file actions
108 lines (75 loc) · 3.34 KB
/
test_filter.py
File metadata and controls
108 lines (75 loc) · 3.34 KB
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
108
import pytest
import csonpath
import sys
def test_simple_filter():
dict = {"ar": [{"0": "a 0", "1": "a 1"}, {"ah": "a 0", "bh": "a 10"}]}
cp = csonpath.CsonPath('$.ar[?0="a 0"]')
ret = cp.find_first(dict)
assert ret == {"0": "a 0", "1": "a 1"}, "fail result look like: {}".format(ret)
ret = cp.find_all(dict)
assert ret == [{"0": "a 0", "1": "a 1"}], "fail result look like: {}".format(ret)
value = ["wololo"]
ref_before = sys.getrefcount(value) if hasattr(sys, 'getrefcount') else None
ret = cp.update_or_create(dict, value)
ref_after = sys.getrefcount(value) if hasattr(sys, 'getrefcount') else None
assert ret == 1
if ref_before is not None:
assert ref_before == ref_after - ret
assert dict == {
"ar": [["wololo"], {"ah": "a 0", "bh": "a 10"}]
}, "fail result look like: {}".format(dict["ar"][0])
cp.set_path('$.ar[?ah="a 0"]')
ret = cp.remove(dict)
assert ret == 1
assert dict == {"ar": [["wololo"]]}, "fail result look like: {}".format(
dict["ar"][0]
)
dict = {"ha": [{"h": "Leodagan"}, {"h": "George"}]}
cp = csonpath.CsonPath('$.ha[?h != "Leodagan"].h')
ret = cp.find_all(dict)
assert ret == ["George"]
dict = {"ha": [{"h": {"h1": "Leodagan"}}, {"h": "George"}]}
cp = csonpath.CsonPath('$.ha[?h.h1 = "Leodagan"].h.h1')
ret = cp.find_all(dict)
dict = {"ha": [{"h": {"h1": "Leodagan"}}, {"h": "George"}]}
cp = csonpath.CsonPath('$.ha[?h.h1 =~ "gan"].h.h1')
ret = cp.find_all(dict)
assert ret == ["Leodagan"], dict
dict = {"ha": [{"h": {"h1": "Leodagan"}}, {"h": "George"}]}
cp = csonpath.CsonPath('$.ha[?h.h1 =~ "Assim"].h.h1')
ret = cp.find_all(dict)
assert ret is None
dict = {"ha": [{"h": {"h1": "Leodagan"}}, {"h": "George"}]}
cp = csonpath.CsonPath('$.ha[?h.h1 =~ "Assim"].h.h1', return_empty_array=True)
ret = cp.find_all(dict)
assert ret == []
cp = csonpath.CsonPath('$.ha[?h.h1 =~ "Assim"].h.h1', True)
ret = cp.find_all(dict)
assert ret == []
def test_multiple_filters():
# cf. https://github.com/h2non/jsonpath-ng?tab=readme-ov-file#extensions, last example of filter
dict = {"knights": [{"name": "Leodagan", "laterality": "left",
"sub": {"a": 10}}, {"name": "George", "laterality": "left"}]}
cp = csonpath.CsonPath('$.knights[?laterality = "left" & name = "George"]')
ret = cp.find_all(dict)
assert ret == [{'name': 'George', 'laterality': 'left'}]
cp = csonpath.CsonPath('$.knights[?laterality = "left" & sub.a = 10]')
ret = cp.find_all(dict)
assert ret == [{'name': 'Leodagan', 'laterality': 'left', "sub": {"a": 10}}]
cp = csonpath.CsonPath('$.knights[?laterality = "left" & sub.a = 10].sub')
ret = cp.find_first(dict)
assert ret == {"a": 10}
cp = csonpath.CsonPath('$.knights[?(@.name == "Assim")].laterality')
ret = cp.find_first(dict)
assert ret is None
cp = csonpath.CsonPath('$.knights[?(@.name == "Assim")].laterality', return_empty_array=True)
ret = cp.find_first(dict)
assert ret is None
def test_filter_on_none():
cp = csonpath.CsonPath('$.ar[?(@.a==1)]')
assert cp.find_first(None) is None
assert cp.find_all(None) is None
def test_filter_on_scalar():
cp = csonpath.CsonPath('$.ar[?(@.a==1)]')
assert cp.find_first(42) is None
assert cp.find_all(42) is None