portfolio-optimiser/tests/test_ingest_golden.py

39 lines
1.8 KiB
Python

"""I2 golden regression — the §11 golden extraction case, byte-for-byte.
The golden case pins every determinism decision the unit tests exercise as RULES: the
text-verbatim CSV reading (the ``007``/``1.50`` cells discriminate it from a
number-normalizing implementation — their normalized forms differ byte-wise), the escape
order, the §5 frontmatter layout, and the §6 index shape. RED when ANY byte of the expected
bundle diverges (spec §11 "Golden regression" seam). The fixture layout is the shared
convention (``manifest.json``, ``fixture/``, ``ingested-at.txt``, ``expected-bundle/``);
authored LOCALLY in I2 — sharing via commons is a later gated round.
"""
from __future__ import annotations
from pathlib import Path
from portfolio_optimiser.ingest import materialize
GOLDEN = Path(__file__).resolve().parents[1] / "examples" / "ingest-golden-file"
def _bundle_bytes(directory: Path) -> dict[str, bytes]:
return {p.name: p.read_bytes() for p in sorted(directory.iterdir()) if p.is_file()}
def test_golden_extraction_is_bit_deterministic(tmp_path: Path) -> None:
ingested_at = (GOLDEN / "ingested-at.txt").read_text(encoding="utf-8").strip()
out = tmp_path / "bundle"
materialize(GOLDEN / "manifest.json", out, ingested_at=ingested_at)
expected = _bundle_bytes(GOLDEN / "expected-bundle")
actual = _bundle_bytes(out)
# File-SET equality first — catches extra AND missing files, not just diverging bytes.
assert actual.keys() == expected.keys()
for name, content in expected.items():
assert actual[name] == content, f"golden byte divergence in {name}"
# §10: a second run over the same output leaves every byte unchanged.
materialize(GOLDEN / "manifest.json", out, ingested_at=ingested_at)
assert _bundle_bytes(out) == expected