File: test_nts.py

package info (click to toggle)
python-jsonpath 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,024 kB
  • sloc: python: 9,462; makefile: 6
file content (51 lines) | stat: -rw-r--r-- 1,327 bytes parent folder | download
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
"""Test Python JSONPath against the Normalized Path Test Suite."""

import json
import operator
from dataclasses import dataclass
from typing import Any
from typing import List

import pytest

import jsonpath


@dataclass
class NormalizedCase:
    name: str
    query: str
    document: Any
    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.NodeList(jsonpath.finditer(case.query, case.document))
    paths = nodes.paths()
    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