-
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #11 from jg-rp/fix-canonical-paths
Fix normalized and canonical paths
- Loading branch information
Showing
10 changed files
with
119 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
[submodule "tests/cts"] | ||
path = tests/cts | ||
url = git@github.com:jsonpath-standard/jsonpath-compliance-test-suite.git | ||
[submodule "tests/nts"] | ||
path = tests/nts | ||
url = git@github.com:jg-rp/jsonpath-compliance-normalized-paths.git |
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 |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.1.3" | ||
__version__ = "0.1.4" |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"""Helper functions for serializing compiled JSONPath queries.""" | ||
|
||
import json | ||
|
||
|
||
def canonical_string(value: str) -> str: | ||
"""Return _value_ as a canonically formatted string literal.""" | ||
single_quoted = json.dumps(value)[1:-1].replace('\\"', '"').replace("'", "\\'") | ||
return f"'{single_quoted}'" |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"""Test Python JSONPath against the Normalized Path Test Suite.""" | ||
|
||
import json | ||
import operator | ||
from dataclasses import dataclass | ||
from typing import List | ||
|
||
import pytest | ||
|
||
import jsonpath_rfc9535 as jsonpath | ||
from jsonpath_rfc9535.environment import JSONValue | ||
|
||
|
||
@dataclass | ||
class NormalizedCase: | ||
name: str | ||
query: str | ||
document: JSONValue | ||
paths: List[str] | ||
|
||
|
||
def normalized_cases() -> List[NormalizedCase]: | ||
with open("tests/nts/normalized_paths.json", encoding="utf8") as fd: | ||
data = json.load(fd) | ||
return [NormalizedCase(**case) for case in data["tests"]] | ||
|
||
|
||
@pytest.mark.parametrize("case", normalized_cases(), ids=operator.attrgetter("name")) | ||
def test_nts_normalized_paths(case: NormalizedCase) -> None: | ||
nodes = jsonpath.find(case.query, case.document) | ||
paths = [node.path() for node in nodes] | ||
assert paths == case.paths | ||
|
||
|
||
@dataclass | ||
class CanonicalCase: | ||
name: str | ||
query: str | ||
canonical: str | ||
|
||
|
||
def canonical_cases() -> List[CanonicalCase]: | ||
with open("tests/nts/canonical_paths.json", encoding="utf8") as fd: | ||
data = json.load(fd) | ||
return [CanonicalCase(**case) for case in data["tests"]] | ||
|
||
|
||
@pytest.mark.parametrize("case", canonical_cases(), ids=operator.attrgetter("name")) | ||
def test_nts_canonical_paths(case: CanonicalCase) -> None: | ||
query = jsonpath.compile(case.query) | ||
assert str(query) == case.canonical |
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