Repository navigation
Expand file tree
/
Copy pathtest_walk_filter_expression_tree.py
More file actions
73 lines (59 loc) · 1.76 KB
/
Copy pathtest_walk_filter_expression_tree.py
File metadata and controls
73 lines (59 loc) · 1.76 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
"""Test that we can traverse filter expression trees."""
import dataclasses
import operator
from typing import List
import pytest
import jsonpath
from jsonpath.filter import BaseExpression
from jsonpath.filter import walk
from jsonpath.selectors import Filter as FilterSelector
@dataclasses.dataclass
class Case:
description: str
path: str
want: bool
TEST_CASES = [
Case(
description="boolean self path",
want=True,
),
Case(
description="infix left self path",
want=True,
),
Case(
description="infix left self path",
path="$some.thing[?$.bar == @.foo]",
want=True,
),
Case(
description="nested filter self path",
want=True,
),
Case(
description="self path as filter function argument",
path="$some.thing[?match(@.foo, '^bar.+')]",
want=True,
),
Case(
description="boolean root path",
path="$some.thing[?$.foo]",
want=False,
),
]
def is_volatile(expr: BaseExpression) -> bool:
return any(expr.volatile for expr in walk(expr))
@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description"))
def test_is_volatile(case: Case) -> None:
path = jsonpath.compile(case.path)
assert isinstance(path, jsonpath.JSONPath)
filter_selectors: List[FilterSelector] = []
for segment in path.segments:
for selector in segment.selectors:
if isinstance(selector, FilterSelector):
filter_selectors.append(selector)
assert len(filter_selectors) == 1
assert is_volatile(filter_selectors[0].expression) is case.want