-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from jg-rp/fake-root
Add non-standard fake root identifier.
- Loading branch information
Showing
12 changed files
with
158 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
import pytest | ||
|
||
from jsonpath import JSONPathEnvironment | ||
from jsonpath import JSONPathSyntaxError | ||
from jsonpath import JSONPathTypeError | ||
|
||
|
||
|
@@ -173,6 +174,41 @@ class MyJSONPathEnvironment(JSONPathEnvironment): | |
assert env.findall("$.foo.*", data) == [1, 2, 3] | ||
|
||
|
||
def test_custom_fake_root_identifier_token() -> None: | ||
"""Test that we can change the non-standard fake root identifier.""" | ||
|
||
class MyJSONPathEnvironment(JSONPathEnvironment): | ||
fake_root_token = "$$" | ||
|
||
env = MyJSONPathEnvironment() | ||
data = {"foo": {"a": 1, "b": 2, "c": 3}} | ||
assert env.findall("$$[[email protected] == 1]", data) == [data] | ||
assert env.findall("$$[[email protected] == 7]", data) == [] | ||
assert env.findall("$.*", data) == [{"a": 1, "b": 2, "c": 3}] | ||
|
||
|
||
def test_disable_fake_root_identifier() -> None: | ||
"""Test that we can disable the non-standard fake root identifier.""" | ||
|
||
class MyJSONPathEnvironment(JSONPathEnvironment): | ||
fake_root_token = "" | ||
|
||
env = MyJSONPathEnvironment() | ||
with pytest.raises(JSONPathSyntaxError): | ||
env.compile("^[[email protected] == 42]") | ||
|
||
|
||
def test_disable_keys_selector() -> None: | ||
"""Test that we can disable the non-standard keys selector.""" | ||
|
||
class MyJSONPathEnvironment(JSONPathEnvironment): | ||
keys_selector_token = "" | ||
|
||
env = MyJSONPathEnvironment() | ||
with pytest.raises(JSONPathSyntaxError): | ||
env.compile("*..~") | ||
|
||
|
||
def test_disable_well_typed_checks() -> None: | ||
"""Test that we can disable checks for well-typedness.""" | ||
env = JSONPathEnvironment(well_typed=True) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,24 @@ class Case: | |
}, | ||
want=[{"foo": 1}, {"foo": 2}], | ||
), | ||
Case( | ||
description="select root value using fake root", | ||
path="^[[email protected] > 7]", | ||
data={"some": {"thing": 42}}, | ||
want=[{"some": {"thing": 42}}], | ||
), | ||
Case( | ||
description="fake root in a filter query", | ||
path="^[[email protected] > value(^.*.num)]", | ||
data={"some": {"thing": 42}, "num": 7}, | ||
want=[{"some": {"thing": 42}, "num": 7}], | ||
), | ||
Case( | ||
description="recurse object keys", | ||
path="$..~", | ||
data={"some": {"thing": "else", "foo": {"bar": "baz"}}}, | ||
want=["some", "thing", "foo", "bar"], | ||
), | ||
] | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.