Skip to content

Commit 0a00bef

Browse files
committed
test coverage
1 parent f211b7a commit 0a00bef

3 files changed

Lines changed: 74 additions & 1 deletion

File tree

‎jsonpath/__init__.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from .exceptions import JSONPathNameError
99
from .exceptions import JSONPathSyntaxError
1010
from .exceptions import JSONPathTypeError
11+
from .exceptions import JSONPointerError
12+
from .exceptions import JSONPointerIndexError
1113
from .filter import UNDEFINED
1214
from .lex import Lexer
1315
from .match import JSONPathMatch
@@ -32,6 +34,8 @@
3234
"JSONPathSyntaxError",
3335
"JSONPathTypeError",
3436
"JSONPointer",
37+
"JSONPointerError",
38+
"JSONPointerIndexError",
3539
"Lexer",
3640
"Parser",
3741
"UNDEFINED",

‎jsonpath/pointer.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ def from_match(
140140
"""Return a JSON Pointer for the path from a JSONPathMatch instance."""
141141
# A rfc6901 string representation of match.parts.
142142
return cls(
143-
"/".join(str(p).replace("~", "~0").replace("/", "~1") for p in match.parts),
143+
"/"
144+
+ "/".join(
145+
str(p).replace("~", "~0").replace("/", "~1") for p in match.parts
146+
),
144147
parts=match.parts,
145148
unicode_escape=False,
146149
uri_decode=False,

‎tests/test_json_pointer.py‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"""JSONPointer test cases."""
2+
import pytest
3+
24
import jsonpath
5+
from jsonpath import JSONPointer
36

47

58
def test_match_to_pointer() -> None:
@@ -10,3 +13,66 @@ def test_match_to_pointer() -> None:
1013
pointer = match.pointer()
1114
assert pointer.resolve(data) == match.obj
1215
assert pointer.resolve({"some": {"thing": "foo"}}) == "foo"
16+
17+
18+
def test_pointer_repr() -> None:
19+
data = {"some": {"thing": "else"}}
20+
matches = list(jsonpath.finditer("$.some.thing", data))
21+
assert len(matches) == 1
22+
match = matches[0]
23+
pointer = match.pointer()
24+
assert str(pointer) == "/some/thing"
25+
26+
27+
def test_pointer_index_out_fo_range() -> None:
28+
max_plus_one = JSONPointer.max_int_index + 1
29+
min_minus_one = JSONPointer.min_int_index - 1
30+
31+
with pytest.raises(jsonpath.JSONPointerError):
32+
JSONPointer(f"/some/thing/{max_plus_one}")
33+
34+
with pytest.raises(jsonpath.JSONPointerError):
35+
JSONPointer(f"/some/thing/{min_minus_one}")
36+
37+
38+
def test_resolve_int_key() -> None:
39+
data = {"some": {"1": "thing"}}
40+
pointer = JSONPointer("/some/1")
41+
assert pointer.resolve(data) == "thing"
42+
43+
44+
def test_resolve_int_missing_key() -> None:
45+
data = {"some": {"1": "thing"}}
46+
pointer = JSONPointer("/some/2")
47+
with pytest.raises(KeyError):
48+
pointer.resolve(data)
49+
50+
51+
def test_resolve_str_index() -> None:
52+
data = {"some": ["a", "b", "c"]}
53+
pointer = JSONPointer("/some/1", parts=("some", "1"))
54+
assert pointer.resolve(data) == "b"
55+
56+
57+
def test_keys_selector() -> None:
58+
data = {"some": {"thing": "else"}}
59+
matches = list(jsonpath.finditer("$.some.~", data))
60+
assert len(matches) == 1
61+
match = matches[0]
62+
pointer = match.pointer()
63+
assert str(pointer) == "/some/~0thing"
64+
assert pointer.resolve(data) == "thing"
65+
66+
67+
def test_mapping_key_error() -> None:
68+
data = {"some": {"thing": "else"}}
69+
pointer = JSONPointer("/some/other")
70+
with pytest.raises(KeyError):
71+
pointer.resolve(data)
72+
73+
74+
def test_sequence_type_error() -> None:
75+
data = {"some": ["a", "b", "c"]}
76+
pointer = JSONPointer("/some/thing")
77+
with pytest.raises(TypeError):
78+
pointer.resolve(data)

0 commit comments

Comments
 (0)