Repository navigation
Expand file tree
/
Copy pathtest_errors.py
More file actions
69 lines (49 loc) · 2.25 KB
/
Copy pathtest_errors.py
File metadata and controls
69 lines (49 loc) · 2.25 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
from operator import attrgetter
from typing import List
from typing import NamedTuple
import pytest
from jsonpath import JSONPathEnvironment
from jsonpath.exceptions import JSONPathSyntaxError
from jsonpath.exceptions import JSONPathTypeError
@pytest.fixture()
def env() -> JSONPathEnvironment:
return JSONPathEnvironment()
def test_unclosed_selection_list(env: JSONPathEnvironment) -> None:
with pytest.raises(JSONPathSyntaxError, match=r"unexpected end of selector list"):
env.compile("$[1,2")
def test_function_missing_param(env: JSONPathEnvironment) -> None:
with pytest.raises(JSONPathTypeError):
env.compile("$[?(length()==1)]")
def test_function_too_many_params(env: JSONPathEnvironment) -> None:
with pytest.raises(JSONPathTypeError):
env.compile("$[?(length(@.a, @.b)==1)]")
def test_non_singular_query_is_not_comparable(env: JSONPathEnvironment) -> None:
with pytest.raises(JSONPathTypeError):
env.compile("$[?@.* > 2]")
def test_unbalanced_parens(env: JSONPathEnvironment) -> None:
with pytest.raises(JSONPathSyntaxError):
env.compile("$[?((@.foo)]")
class FilterLiteralTestCase(NamedTuple):
description: str
query: str
BAD_FILTER_LITERAL_TEST_CASES: List[FilterLiteralTestCase] = [
FilterLiteralTestCase("just true", "$[?true]"),
FilterLiteralTestCase("just string", "$[?'foo']"),
FilterLiteralTestCase("just int", "$[?2]"),
FilterLiteralTestCase("just float", "$[?2.2]"),
FilterLiteralTestCase("just null", "$[?null]"),
FilterLiteralTestCase("literal and literal", "$[?true && false]"),
FilterLiteralTestCase("literal or literal", "$[?true || false]"),
FilterLiteralTestCase("comparison and literal", "$[?true == false && false]"),
FilterLiteralTestCase("comparison or literal", "$[?true == false || false]"),
FilterLiteralTestCase("literal and comparison", "$[?true && true == false]"),
FilterLiteralTestCase("literal or comparison", "$[?false || true == false]"),
]
@pytest.mark.parametrize(
"case", BAD_FILTER_LITERAL_TEST_CASES, ids=attrgetter("description")
)
def test_filter_literals_must_be_compared(
env: JSONPathEnvironment, case: FilterLiteralTestCase
) -> None:
with pytest.raises(JSONPathSyntaxError):
env.compile(case.query)