Repository navigation
Expand file tree
/
Copy pathtest_filter_expression_caching.py
More file actions
165 lines (143 loc) · 6.52 KB
/
Copy pathtest_filter_expression_caching.py
File metadata and controls
165 lines (143 loc) · 6.52 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""Filter expression caching test cases."""
from unittest import mock
from jsonpath import JSONPath
from jsonpath import JSONPathEnvironment
from jsonpath.filter import BooleanExpression
from jsonpath.filter import CachingFilterExpression
from jsonpath.filter import FilterContextPath
from jsonpath.filter import FilterExpression
from jsonpath.filter import InfixExpression
from jsonpath.filter import IntegerLiteral
from jsonpath.filter import RootPath
from jsonpath.filter import SelfPath
from jsonpath.selectors import Filter as FilterSelector
from jsonpath.selectors import ListSelector
def test_cache_root_path() -> None:
"""Test that we wrap root paths in a caching node."""
env = JSONPathEnvironment()
assert isinstance(path, JSONPath)
selection_list = path.selectors[1]
assert isinstance(selection_list, ListSelector)
filter_selector = selection_list.items[0]
assert isinstance(filter_selector, FilterSelector)
assert filter_selector.cacheable_nodes is True
# The original expression tree without caching nodes.
expr: FilterExpression = filter_selector.expression
assert isinstance(expr, BooleanExpression)
expr = expr.expression
assert isinstance(expr, InfixExpression)
assert isinstance(expr.left, SelfPath)
assert isinstance(expr.right, RootPath)
# A caching copy of the original expression tree.
expr = filter_selector.expression.cache_tree()
assert isinstance(expr, BooleanExpression)
expr = expr.expression
assert isinstance(expr, InfixExpression)
assert isinstance(expr.left, SelfPath)
assert isinstance(expr.right, CachingFilterExpression)
assert isinstance(expr.right._expr, RootPath) # noqa: SLF001
def test_root_path_cache() -> None:
"""Test that we evaluate root paths once when caching is enabled."""
env = JSONPathEnvironment(filter_caching=True)
data = {"some": [{"a": 1}, {"a": 99}, {"a": 2}, {"a": 3}]}
with mock.patch(
"jsonpath.filter.RootPath.evaluate", return_value=10
) as mock_root_path:
rv = path.findall(data)
assert rv == [1, 2, 3]
assert mock_root_path.call_count == 1
def test_root_path_no_cache() -> None:
"""Test that we evaluate root paths once for each match when caching is disabled."""
env = JSONPathEnvironment(filter_caching=False)
data = {"some": [{"a": 1}, {"a": 99}, {"a": 2}, {"a": 3}]}
with mock.patch(
"jsonpath.filter.RootPath.evaluate", return_value=10
) as mock_root_path:
rv = path.findall(data)
assert rv == [1, 2, 3]
assert mock_root_path.call_count == 4 # noqa: PLR2004
def test_cache_context_path() -> None:
"""Test that we wrap filter context paths in a caching node."""
env = JSONPathEnvironment()
path = env.compile("$.some[?_.thing > @.a].a")
assert isinstance(path, JSONPath)
selection_list = path.selectors[1]
assert isinstance(selection_list, ListSelector)
filter_selector = selection_list.items[0]
assert isinstance(filter_selector, FilterSelector)
assert filter_selector.cacheable_nodes is True
# The original expression tree without caching nodes.
expr: FilterExpression = filter_selector.expression
assert isinstance(expr, BooleanExpression)
expr = expr.expression
assert isinstance(expr, InfixExpression)
assert isinstance(expr.left, FilterContextPath)
assert isinstance(expr.right, SelfPath)
# A caching copy of the original expression tree.
expr = filter_selector.expression.cache_tree()
assert isinstance(expr, BooleanExpression)
expr = expr.expression
assert isinstance(expr, InfixExpression)
assert isinstance(expr.left, CachingFilterExpression)
assert isinstance(expr.left._expr, FilterContextPath) # noqa: SLF001
assert isinstance(expr.right, SelfPath)
def test_context_path_cache() -> None:
"""Test that we evaluate filter context paths once when caching is enabled."""
env = JSONPathEnvironment(filter_caching=True)
data = {"some": [{"a": 1}, {"a": 99}, {"a": 2}, {"a": 3}]}
with mock.patch(
"jsonpath.filter.FilterContextPath.evaluate", return_value=10
) as mock_root_path:
path = env.compile("$.some[?_.thing > @.a].a")
rv = path.findall(data)
assert rv == [1, 2, 3]
assert mock_root_path.call_count == 1
def test_context_path_no_cache() -> None:
"""Test that we evaluate context path for each match when caching is disabled."""
env = JSONPathEnvironment(filter_caching=False)
data = {"some": [{"a": 1}, {"a": 99}, {"a": 2}, {"a": 3}]}
with mock.patch(
"jsonpath.filter.FilterContextPath.evaluate", return_value=10
) as mock_root_path:
path = env.compile("$.some[?_.thing > @.a].a")
rv = path.findall(data)
assert rv == [1, 2, 3]
assert mock_root_path.call_count == 4 # noqa: PLR2004
def test_cache_expires() -> None:
"""Test that the cache expires between calls to findall/finditer."""
env = JSONPathEnvironment(filter_caching=True)
some_data = {
"some": {"thing": [{"other": 1}, {"other": 2}, {"other": 3}]},
"foo": 10,
}
other_data = {
"some": {"thing": [{"other": 1}, {"other": 2}, {"other": 3}]},
"foo": 1,
}
assert path.findall(some_data) == [{"other": 1}, {"other": 2}, {"other": 3}]
assert path.findall(other_data) == []
def test_uncacheable_filter() -> None:
"""Test that we don't waste time caching uncacheable expressions."""
env = JSONPathEnvironment(filter_caching=True)
assert isinstance(path, JSONPath)
selection_list = path.selectors[1]
assert isinstance(selection_list, ListSelector)
filter_selector = selection_list.items[0]
assert isinstance(filter_selector, FilterSelector)
assert filter_selector.cacheable_nodes is False
# The original expression tree without caching nodes.
expr: FilterExpression = filter_selector.expression
assert isinstance(expr, BooleanExpression)
expr = expr.expression
assert isinstance(expr, InfixExpression)
assert isinstance(expr.left, InfixExpression)
assert isinstance(expr.right, InfixExpression)
assert isinstance(expr.left.left, SelfPath)
assert isinstance(expr.left.right, IntegerLiteral)
assert isinstance(expr.right.left, SelfPath)
assert isinstance(expr.right.right, IntegerLiteral)