-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
225 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import gzip | ||
import shutil | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
HERE = Path(__file__).parent | ||
DATA_DIR = Path(__file__).parent / "data" | ||
SPEC_JSON_GZ = sorted(DATA_DIR.glob("*.json.gz")) | ||
|
||
|
||
@pytest.fixture(params=SPEC_JSON_GZ) | ||
def an_historic_spec_gz(request) -> Path: | ||
return request.param | ||
|
||
|
||
@pytest.fixture | ||
def an_historic_spec_json(tmp_path: Path, an_historic_spec_gz: Path) -> Path: | ||
target_path = tmp_path / "pyodide-lock.json" | ||
|
||
with gzip.open(an_historic_spec_gz) as fh_in: | ||
with target_path.open("wb") as fh_out: | ||
shutil.copyfileobj(fh_in, fh_out) | ||
|
||
return target_path |
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,59 @@ | ||
import json | ||
from pathlib import Path | ||
from typing import Any | ||
|
||
import pytest | ||
from jsonschema import ValidationError | ||
from jsonschema.validators import Draft201909Validator as Validator | ||
|
||
from pyodide_lock import PyodideLockSpec | ||
|
||
#: a schema that constrains the schema itself for schema syntax | ||
META_SCHEMA = { | ||
"type": "object", | ||
"required": ["description", "$id", "$schema"], | ||
"properties": { | ||
"description": {"type": "string"}, | ||
"$id": {"type": "string", "format": "uri"}, | ||
"$schema": {"type": "string", "format": "uri"}, | ||
"definitions": {"patternProperties": {".*": {"required": ["description"]}}}, | ||
}, | ||
} | ||
|
||
FORMAT_CHECKER = Validator.FORMAT_CHECKER | ||
|
||
|
||
@pytest.fixture | ||
def schema() -> dict[str, Any]: | ||
return PyodideLockSpec.schema() | ||
|
||
|
||
@pytest.fixture | ||
def spec_validator(schema: dict[str, Any]) -> Validator: | ||
return Validator(schema, format_checker=FORMAT_CHECKER) | ||
|
||
|
||
def test_documentation(schema: dict[str, Any]) -> None: | ||
meta_validator = Validator(META_SCHEMA, format_checker=FORMAT_CHECKER) | ||
_assert_validation_errors(meta_validator, schema) | ||
|
||
|
||
def test_validate(an_historic_spec_json: Path, spec_validator: Validator) -> None: | ||
spec_json = json.loads(an_historic_spec_json.read_text(encoding="utf-8")) | ||
_assert_validation_errors(spec_validator, spec_json) | ||
|
||
|
||
def _assert_validation_errors( | ||
validator: Validator, | ||
instance: dict[str, Any], | ||
expect_errors: list[str] | None = None, | ||
) -> None: | ||
expect_errors = expect_errors or [] | ||
expect_error_count = len(expect_errors) | ||
|
||
errors: list[ValidationError] = list(validator.iter_errors(instance)) | ||
error_count = len(errors) | ||
|
||
print("\n".join([f"""{err.json_path}: {err.message}""" for err in errors])) | ||
|
||
assert error_count == expect_error_count |
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