"""Check that contract convention copies stay identical to api/core. The OpenAPI 3.2 compatibility fixture and the ML contract cannot reference api/core files: ogen rejects external references and the ML contract is a separate specification. They therefore carry copies of the api/core conventions. These tests read the bundled api/core contract, so run them after `make -C api bundle`. """ from __future__ import annotations import unittest from pathlib import Path from typing import Any import yaml API_DIR = Path(__file__).resolve().parents[1] / "api" CORE_BUNDLE = API_DIR / "core" / "openapi.bundle.yml" ML_BUNDLE = API_DIR / "ml" / "openapi.bundle.yml" FIXTURE = API_DIR / "testdata" / "openapi-3.2.0.yml" SHARED_SECTIONS = ("schemas", "parameters", "headers") # Every api/core convention component that the fixture must exercise. EXPECTED_FIXTURE_COPIES = { ("schemas", "Problem"), ("schemas", "ProblemCode"), ("schemas", "ProblemFieldError"), ("schemas", "ProblemFieldLocation"), ("schemas", "Timestamp"), ("schemas", "PageInfo"), ("parameters", "IdempotencyKey"), ("parameters", "IfMatch"), ("parameters", "PageLimit"), ("parameters", "PageCursor"), ("headers", "ETag"), } PROBLEM_SCHEMAS = ("Problem", "ProblemCode", "ProblemFieldError", "ProblemFieldLocation") def load_components(path: Path) -> dict[str, Any]: with path.open(encoding="utf-8") as stream: document = yaml.safe_load(stream) components = document.get("components") if not isinstance(components, dict): raise AssertionError(f"{path} has no components mapping") return components def without_descriptions(value: Any) -> Any: """Return a copy of a schema without description text.""" if isinstance(value, dict): return { key: without_descriptions(item) for key, item in value.items() if key != "description" } if isinstance(value, list): return [without_descriptions(item) for item in value] return value def without_examples(response: dict[str, Any]) -> dict[str, Any]: """Return a copy of a response without media type examples.""" content = { media_type: {key: item for key, item in media.items() if key != "examples"} for media_type, media in response.get("content", {}).items() } return {**response, "content": content} class ContractCopiesTest(unittest.TestCase): def setUp(self) -> None: self.core = load_components(CORE_BUNDLE) def test_fixture_copies_match_core_definitions(self) -> None: fixture = load_components(FIXTURE) compared = set() for section in SHARED_SECTIONS: for name, definition in fixture.get(section, {}).items(): if name not in self.core.get(section, {}): continue compared.add((section, name)) with self.subTest(section=section, name=name): self.assertEqual(definition, self.core[section][name]) self.assertEqual(compared, EXPECTED_FIXTURE_COPIES) def test_fixture_responses_match_core_without_examples(self) -> None: fixture_responses = load_components(FIXTURE)["responses"] for name, definition in fixture_responses.items(): with self.subTest(name=name): self.assertIn(name, self.core["responses"]) expected = without_examples(self.core["responses"][name]) self.assertEqual(definition, expected) def test_ml_problem_schemas_match_core_except_descriptions(self) -> None: ml_schemas = load_components(ML_BUNDLE)["schemas"] for name in PROBLEM_SCHEMAS: with self.subTest(name=name): self.assertIn(name, ml_schemas) self.assertEqual( without_descriptions(ml_schemas[name]), without_descriptions(self.core["schemas"][name]), ) if __name__ == "__main__": unittest.main()