diff options
author | Julian Berman <Julian@GrayVines.com> | 2022-12-05 13:05:46 -0500 |
---|---|---|
committer | Julian Berman <Julian@GrayVines.com> | 2022-12-05 13:05:46 -0500 |
commit | 9a4043d0c1c9eedddf16df0bd73d91ec94f9c40f (patch) | |
tree | 0f9205aa8bd48b9858bff4b2fd1360aad7909e3d /jsonschema | |
parent | e5a6686c9215aecbd7dc8afdd803fa3fc191e66e (diff) | |
download | jsonschema-9a4043d0c1c9eedddf16df0bd73d91ec94f9c40f.tar.gz |
More type sprinkling in the suite loader.
Some minor renaming to go with it.
Diffstat (limited to 'jsonschema')
-rw-r--r-- | jsonschema/tests/_suite.py | 88 | ||||
-rw-r--r-- | jsonschema/tests/test_jsonschema_test_suite.py | 72 |
2 files changed, 73 insertions, 87 deletions
diff --git a/jsonschema/tests/_suite.py b/jsonschema/tests/_suite.py index 1730924..7bcc00e 100644 --- a/jsonschema/tests/_suite.py +++ b/jsonschema/tests/_suite.py @@ -3,10 +3,10 @@ Python representations of the JSON Schema Test Suite tests. """ from __future__ import annotations -from collections.abc import Mapping +from collections.abc import Iterable, Mapping from functools import partial from pathlib import Path -from typing import Any +from typing import TYPE_CHECKING, Any import json import os import re @@ -16,6 +16,9 @@ import unittest from attrs import field, frozen +if TYPE_CHECKING: + import pyperf + from jsonschema.validators import _VALIDATORS import jsonschema @@ -41,23 +44,23 @@ def _find_suite(): @frozen class Suite: - _root = field(factory=_find_suite) + _root: Path = field(factory=_find_suite) - def _remotes(self): + def _remotes(self) -> Mapping[str, Mapping[str, Any] | bool]: jsonschema_suite = self._root.joinpath("bin", "jsonschema_suite") remotes = subprocess.check_output( [sys.executable, str(jsonschema_suite), "remotes"], ) return json.loads(remotes.decode("utf-8")) - def benchmark(self, runner): # pragma: no cover + def benchmark(self, runner: pyperf.Runner): # pragma: no cover for name, Validator in _VALIDATORS.items(): self.version(name=name).benchmark( runner=runner, Validator=Validator, ) - def version(self, name): + def version(self, name) -> Version: return Version( name=name, path=self._root.joinpath("tests", name), @@ -73,48 +76,30 @@ class Version: name: str - def benchmark(self, runner, **kwargs): # pragma: no cover - for suite in self.tests(): - for test in suite: + def benchmark(self, runner: pyperf.Runner, **kwargs): # pragma: no cover + for case in self.cases(): + for test in case: runner.bench_func( test.fully_qualified_name, partial(test.validate_ignoring_errors, **kwargs), ) - def tests(self): - return ( - test - for child in self._path.glob("*.json") - for test in self._tests_in( - subject=child.name[:-5], - path=child, - ) - ) + def cases(self) -> Iterable[Iterable[_Test]]: + return self._cases_in(paths=self._path.glob("*.json")) - def format_tests(self): - path = self._path.joinpath("optional", "format") - return ( - test - for child in path.glob("*.json") - for test in self._tests_in( - subject=child.name[:-5], - path=child, - ) - ) + def format_cases(self) -> Iterable[Iterable[_Test]]: + return self._cases_in(paths=self._path.glob("optional/format/*.json")) - def optional_tests_of(self, name): - return self._tests_in( - subject=name, - path=self._path.joinpath("optional", name + ".json"), - ) + def optional_cases_of(self, name: str) -> Iterable[Iterable[_Test]]: + return self._cases_in(paths=[self._path / "optional" / f"{name}.json"]) - def to_unittest_testcase(self, *suites, **kwargs): + def to_unittest_testcase(self, *groups, **kwargs): name = kwargs.pop("name", "Test" + self.name.title().replace("-", "")) methods = { test.method_name: test.to_unittest_method(**kwargs) - for suite in suites - for tests in suite - for test in tests + for group in groups + for case in group + for test in case } cls = type(name, (unittest.TestCase,), methods) @@ -128,18 +113,19 @@ class Version: return cls - def _tests_in(self, subject, path): - for each in json.loads(path.read_text(encoding="utf-8")): - yield ( - _Test( - version=self, - subject=subject, - case_description=each["description"], - schema=each["schema"], - remotes=self._remotes, - **test, - ) for test in each["tests"] - ) + def _cases_in(self, paths: Iterable[Path]) -> Iterable[Iterable[_Test]]: + for path in paths: + for case in json.loads(path.read_text(encoding="utf-8")): + yield ( + _Test( + version=self, + subject=path.stem, + case_description=case["description"], + schema=case["schema"], + remotes=self._remotes, + **test, + ) for test in case["tests"] + ) @frozen(repr=False) @@ -152,11 +138,11 @@ class _Test: description: str data: Any - schema: dict[str, Any] | bool + schema: Mapping[str, Any] | bool valid: bool - _remotes: dict[str, dict[str, Any] | bool] + _remotes: Mapping[str, Mapping[str, Any] | bool] comment: str | None = None diff --git a/jsonschema/tests/test_jsonschema_test_suite.py b/jsonschema/tests/test_jsonschema_test_suite.py index 42337ed..19bfdcb 100644 --- a/jsonschema/tests/test_jsonschema_test_suite.py +++ b/jsonschema/tests/test_jsonschema_test_suite.py @@ -144,11 +144,11 @@ def leap_second(test): TestDraft3 = DRAFT3.to_unittest_testcase( - DRAFT3.tests(), - DRAFT3.format_tests(), - DRAFT3.optional_tests_of(name="bignum"), - DRAFT3.optional_tests_of(name="non-bmp-regex"), - DRAFT3.optional_tests_of(name="zeroTerminatedFloats"), + DRAFT3.cases(), + DRAFT3.format_cases(), + DRAFT3.optional_cases_of(name="bignum"), + DRAFT3.optional_cases_of(name="non-bmp-regex"), + DRAFT3.optional_cases_of(name="zeroTerminatedFloats"), Validator=jsonschema.Draft3Validator, format_checker=jsonschema.Draft3Validator.FORMAT_CHECKER, skip=lambda test: ( @@ -168,12 +168,12 @@ TestDraft3 = DRAFT3.to_unittest_testcase( TestDraft4 = DRAFT4.to_unittest_testcase( - DRAFT4.tests(), - DRAFT4.format_tests(), - DRAFT4.optional_tests_of(name="bignum"), - DRAFT4.optional_tests_of(name="float-overflow"), - DRAFT4.optional_tests_of(name="non-bmp-regex"), - DRAFT4.optional_tests_of(name="zeroTerminatedFloats"), + DRAFT4.cases(), + DRAFT4.format_cases(), + DRAFT4.optional_cases_of(name="bignum"), + DRAFT4.optional_cases_of(name="float-overflow"), + DRAFT4.optional_cases_of(name="non-bmp-regex"), + DRAFT4.optional_cases_of(name="zeroTerminatedFloats"), Validator=jsonschema.Draft4Validator, format_checker=jsonschema.Draft4Validator.FORMAT_CHECKER, skip=lambda test: ( @@ -230,11 +230,11 @@ TestDraft4 = DRAFT4.to_unittest_testcase( TestDraft6 = DRAFT6.to_unittest_testcase( - DRAFT6.tests(), - DRAFT6.format_tests(), - DRAFT6.optional_tests_of(name="bignum"), - DRAFT6.optional_tests_of(name="float-overflow"), - DRAFT6.optional_tests_of(name="non-bmp-regex"), + DRAFT6.cases(), + DRAFT6.format_cases(), + DRAFT6.optional_cases_of(name="bignum"), + DRAFT6.optional_cases_of(name="float-overflow"), + DRAFT6.optional_cases_of(name="non-bmp-regex"), Validator=jsonschema.Draft6Validator, format_checker=jsonschema.Draft6Validator.FORMAT_CHECKER, skip=lambda test: ( @@ -253,12 +253,12 @@ TestDraft6 = DRAFT6.to_unittest_testcase( TestDraft7 = DRAFT7.to_unittest_testcase( - DRAFT7.tests(), - DRAFT7.format_tests(), - DRAFT7.optional_tests_of(name="bignum"), - DRAFT7.optional_tests_of(name="cross-draft"), - DRAFT7.optional_tests_of(name="float-overflow"), - DRAFT7.optional_tests_of(name="non-bmp-regex"), + DRAFT7.cases(), + DRAFT7.format_cases(), + DRAFT7.optional_cases_of(name="bignum"), + DRAFT7.optional_cases_of(name="cross-draft"), + DRAFT7.optional_cases_of(name="float-overflow"), + DRAFT7.optional_cases_of(name="non-bmp-regex"), Validator=jsonschema.Draft7Validator, format_checker=jsonschema.Draft7Validator.FORMAT_CHECKER, skip=lambda test: ( @@ -285,12 +285,12 @@ TestDraft7 = DRAFT7.to_unittest_testcase( TestDraft201909 = DRAFT201909.to_unittest_testcase( - DRAFT201909.tests(), - DRAFT201909.optional_tests_of(name="bignum"), - DRAFT201909.optional_tests_of(name="cross-draft"), - DRAFT201909.optional_tests_of(name="float-overflow"), - DRAFT201909.optional_tests_of(name="non-bmp-regex"), - DRAFT201909.optional_tests_of(name="refOfUnknownKeyword"), + DRAFT201909.cases(), + DRAFT201909.optional_cases_of(name="bignum"), + DRAFT201909.optional_cases_of(name="cross-draft"), + DRAFT201909.optional_cases_of(name="float-overflow"), + DRAFT201909.optional_cases_of(name="non-bmp-regex"), + DRAFT201909.optional_cases_of(name="refOfUnknownKeyword"), Validator=jsonschema.Draft201909Validator, skip=lambda test: ( skip( @@ -406,7 +406,7 @@ TestDraft201909 = DRAFT201909.to_unittest_testcase( TestDraft201909Format = DRAFT201909.to_unittest_testcase( - DRAFT201909.format_tests(), + DRAFT201909.format_cases(), name="TestDraft201909Format", Validator=jsonschema.Draft201909Validator, format_checker=jsonschema.Draft201909Validator.FORMAT_CHECKER, @@ -421,12 +421,12 @@ TestDraft201909Format = DRAFT201909.to_unittest_testcase( TestDraft202012 = DRAFT202012.to_unittest_testcase( - DRAFT202012.tests(), - DRAFT202012.optional_tests_of(name="bignum"), - DRAFT202012.optional_tests_of(name="cross-draft"), - DRAFT202012.optional_tests_of(name="float-overflow"), - DRAFT202012.optional_tests_of(name="non-bmp-regex"), - DRAFT202012.optional_tests_of(name="refOfUnknownKeyword"), + DRAFT202012.cases(), + DRAFT202012.optional_cases_of(name="bignum"), + DRAFT202012.optional_cases_of(name="cross-draft"), + DRAFT202012.optional_cases_of(name="float-overflow"), + DRAFT202012.optional_cases_of(name="non-bmp-regex"), + DRAFT202012.optional_cases_of(name="refOfUnknownKeyword"), Validator=jsonschema.Draft202012Validator, skip=lambda test: ( narrow_unicode_build(test) @@ -532,7 +532,7 @@ TestDraft202012 = DRAFT202012.to_unittest_testcase( TestDraft202012Format = DRAFT202012.to_unittest_testcase( - DRAFT202012.format_tests(), + DRAFT202012.format_cases(), name="TestDraft202012Format", Validator=jsonschema.Draft202012Validator, format_checker=jsonschema.Draft202012Validator.FORMAT_CHECKER, |