spacesafeml-certification-b.../tests/test_schema_validation.py

35 lines
1.4 KiB
Python

import json
import os
import pytest
# jsonschema is an optional dependency used for schema validation tests.
# Skip these tests if jsonschema is not installed in the current test environment.
jsonschema = pytest.importorskip("jsonschema", reason="jsonschema not available; skipping schema validation tests")
from jsonschema import validate, ValidationError
def load_json(path: str):
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
def test_example_contract_validates_against_schema():
schema = load_json(os.path.join(os.path.dirname(__file__), "..", "schemas", "safety_contract_schema.json"))
example = load_json(os.path.join(os.path.dirname(__file__), "..", "examples", "contracts", "example_contract.json"))
# Should not raise
validate(instance=example, schema=schema)
def test_schema_rejects_additional_properties():
schema = load_json(os.path.join(os.path.dirname(__file__), "..", "schemas", "safety_contract_schema.json"))
example = load_json(os.path.join(os.path.dirname(__file__), "..", "examples", "contracts", "example_contract.json"))
# Inject an unexpected property at top-level which is forbidden by additionalProperties=false
example_with_extra = dict(example)
example_with_extra["unexpected"] = "value"
with pytest.raises(ValidationError):
validate(instance=example_with_extra, schema=schema)