|
| 1 | +"""Pydantic round-trip + legacy backward-compat tests for the |
| 2 | +Phase 4h.2 Part 1 schema additions (issue #116). |
| 3 | +
|
| 4 | +Two new optional fields land on ``Metadata``: |
| 5 | +
|
| 6 | +- ``osap_signals_missing_from_dataset: list[str] | None = None`` |
| 7 | +- ``osap_gate_diagnostics: dict[str, OsapGateDiagnostic] | None = None`` |
| 8 | +
|
| 9 | +Plus a new nested model ``OsapGateDiagnostic`` with 4 nullable fields. |
| 10 | +All defaults are ``None`` so a legacy 0.9.0-phase4h ``metadata.json`` |
| 11 | +deserializes cleanly with the new fields set to ``None`` — the |
| 12 | +canonical "additive optional field → PATCH bump" pattern from |
| 13 | +SKILL.md L305 ("Add a new optional field (default = None) → patch"). |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +from compute.output.schemas import Metadata, OsapGateDiagnostic |
| 19 | + |
| 20 | + |
| 21 | +def _legacy_0_9_0_metadata_payload() -> dict: |
| 22 | + """A canonical 0.9.0-phase4h metadata.json payload — the production |
| 23 | + shape *before* this PR's additions. Used to assert backward-compat. |
| 24 | + """ |
| 25 | + return { |
| 26 | + "version": "0.9.0-phase4h", |
| 27 | + "last_update_utc": "2026-05-18T22:00:00Z", |
| 28 | + "next_update_utc": "2026-05-25T22:00:00Z", |
| 29 | + "universe": "sp500", |
| 30 | + "universe_size": 502, |
| 31 | + "compute_run_id": "local", |
| 32 | + "git_commit": "fbd1acf461847d835967bd701a098af93c9d2bd7", |
| 33 | + "mos_trailing_ic_smoke": 0.05, |
| 34 | + "tier2_coverage_pct": 0.97, |
| 35 | + "fundamentals_coverage_pct": 0.99, |
| 36 | + "fundamentals_latency_p50_seconds": 1.2, |
| 37 | + "fundamentals_latency_p95_seconds": 5.4, |
| 38 | + "osap_signals_used": None, |
| 39 | + "osap_excluded_signals": [ |
| 40 | + "AbnormalAccruals", "AssetGrowth", "BM", "BetaFP", "CF", |
| 41 | + ], |
| 42 | + "osap_signals_ic_12m": None, |
| 43 | + "osap_signals_coverage_pct": None, |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | +def test_osap_gate_diagnostic_round_trip_with_all_fields(): |
| 48 | + """Happy path — populate every field, round-trip through JSON.""" |
| 49 | + diag = OsapGateDiagnostic( |
| 50 | + pbo=0.45, |
| 51 | + dsr=0.12, |
| 52 | + sharpe=0.34, |
| 53 | + rejection_reason=None, |
| 54 | + ) |
| 55 | + payload = diag.model_dump() |
| 56 | + restored = OsapGateDiagnostic.model_validate(payload) |
| 57 | + assert restored == diag |
| 58 | + |
| 59 | + |
| 60 | +def test_osap_gate_diagnostic_all_fields_default_to_none(): |
| 61 | + """Empty construction (zero args) — every field is None. |
| 62 | + Per refinement #3: all 4 fields explicit ``= None`` defaults.""" |
| 63 | + diag = OsapGateDiagnostic() |
| 64 | + assert diag.pbo is None |
| 65 | + assert diag.dsr is None |
| 66 | + assert diag.sharpe is None |
| 67 | + assert diag.rejection_reason is None |
| 68 | + |
| 69 | + |
| 70 | +def test_osap_gate_diagnostic_rejection_reason_taxonomy(): |
| 71 | + """Mirrors ``compute/validation/osap_validation.py::GateResult`` |
| 72 | + rejection_reason values. The model accepts any str (no Literal |
| 73 | + constraint), but the canonical taxonomy should round-trip cleanly. |
| 74 | + """ |
| 75 | + for reason in ("high_pbo", "low_dsr", "insufficient_data", "gate_failed"): |
| 76 | + diag = OsapGateDiagnostic(rejection_reason=reason) |
| 77 | + restored = OsapGateDiagnostic.model_validate(diag.model_dump()) |
| 78 | + assert restored.rejection_reason == reason |
| 79 | + |
| 80 | + |
| 81 | +def test_metadata_round_trip_with_new_fields_populated(): |
| 82 | + """End-to-end: Metadata with both new fields populated.""" |
| 83 | + payload = _legacy_0_9_0_metadata_payload() |
| 84 | + payload["osap_signals_missing_from_dataset"] = ["AOP", "AccrualsBM", "ChEQ"] |
| 85 | + payload["osap_gate_diagnostics"] = { |
| 86 | + "BM": {"pbo": 0.6, "dsr": -0.1, "sharpe": 0.05, "rejection_reason": "high_pbo"}, |
| 87 | + "Mom12m": {"pbo": 0.4, "dsr": -0.3, "sharpe": 0.2, "rejection_reason": "low_dsr"}, |
| 88 | + } |
| 89 | + meta = Metadata.model_validate(payload) |
| 90 | + assert meta.osap_signals_missing_from_dataset == ["AOP", "AccrualsBM", "ChEQ"] |
| 91 | + assert isinstance(meta.osap_gate_diagnostics, dict) |
| 92 | + assert meta.osap_gate_diagnostics["BM"].pbo == 0.6 |
| 93 | + assert meta.osap_gate_diagnostics["BM"].rejection_reason == "high_pbo" |
| 94 | + assert meta.osap_gate_diagnostics["Mom12m"].dsr == -0.3 |
| 95 | + |
| 96 | + restored = Metadata.model_validate(meta.model_dump()) |
| 97 | + assert restored == meta |
| 98 | + |
| 99 | + |
| 100 | +def test_metadata_backward_compat_with_0_9_0_payload(): |
| 101 | + """Legacy 0.9.0-phase4h JSON (no new fields) deserializes cleanly |
| 102 | + — the backward-compat guarantee that justifies a PATCH bump per |
| 103 | + SKILL.md L305 ('Add a new optional field (default = None) → |
| 104 | + patch'). |
| 105 | + """ |
| 106 | + legacy_payload = _legacy_0_9_0_metadata_payload() |
| 107 | + assert "osap_signals_missing_from_dataset" not in legacy_payload |
| 108 | + assert "osap_gate_diagnostics" not in legacy_payload |
| 109 | + |
| 110 | + meta = Metadata.model_validate(legacy_payload) |
| 111 | + assert meta.version == "0.9.0-phase4h" |
| 112 | + assert meta.osap_signals_missing_from_dataset is None |
| 113 | + assert meta.osap_gate_diagnostics is None |
| 114 | + # Existing Phase 4h fields still populated. |
| 115 | + assert meta.osap_excluded_signals == [ |
| 116 | + "AbnormalAccruals", "AssetGrowth", "BM", "BetaFP", "CF", |
| 117 | + ] |
| 118 | + |
| 119 | + |
| 120 | +def test_metadata_new_fields_default_to_none(): |
| 121 | + """Constructing a Metadata without supplying the new fields leaves |
| 122 | + them at None — same semantics as every other Phase-4h OSAP field.""" |
| 123 | + payload = _legacy_0_9_0_metadata_payload() |
| 124 | + meta = Metadata.model_validate(payload) |
| 125 | + assert meta.osap_signals_missing_from_dataset is None |
| 126 | + assert meta.osap_gate_diagnostics is None |
| 127 | + |
| 128 | + |
| 129 | +def test_metadata_extra_forbid_rejects_unknown_fields(): |
| 130 | + """``model_config = ConfigDict(extra='forbid')`` catches typo'd |
| 131 | + field names. Locks the schema surface so future refactors that |
| 132 | + rename ``osap_signals_missing_from_dataset`` (e.g., to |
| 133 | + ``osap_manifest_missing``) raise instead of silently producing |
| 134 | + a no-op field.""" |
| 135 | + import pytest as _pytest |
| 136 | + from pydantic import ValidationError |
| 137 | + |
| 138 | + payload = _legacy_0_9_0_metadata_payload() |
| 139 | + payload["osap_signals_missing"] = ["typo_field_name"] # not the real field |
| 140 | + with _pytest.raises(ValidationError): |
| 141 | + Metadata.model_validate(payload) |
0 commit comments