portfolio-optimiser-claude/tests/test_ingest_sql_golden.py
Kjell Tore Guttormsen 32640deffc feat(ingest): I5 — SQL D7-speil, bygget fra commons-spec alene
Speiler MAF I4 fra shared/ingest-spec.md alene: manifest → SQL-konnektor →
materialisert OKF-bundle, byte-identisk med den delte golden-fasiten. Gaten
I4→I5 verifisert løst mot ground truth (MAF-commits d7e5f2f/4f45fe6/1b7612b)
før arbeidet startet. Spec byte-identisk delt, ingen spec-endring (I4).

- ingest.py: SqlSource (type: sql, id, connection_ref); ManifestContract.source
  er nå diskriminert union FileSource | SqlSource på type (http/ukjent tag →
  fail-fast). _render_sql_cell (§5 typed: NULL→"", int→decimal, float→korteste
  round-trip, str→verbatim m/ delt _escape_cell, annet→fail — aldri stille
  coercion). _resolve_connection_ref (env-oppslag §4/§8, usatt → fail-fast).
  _read_sql (read-only sqlite file:?mode=ro, ett SELECT, max_rows §8).
  _read_extraction dispatcher på source.type; materialisering/index/replacement
  uendret fra I3.
- examples/ingest-golden-sql/: repo-lokal golden (byte-frossen kopi av I4s fasit).
- Speiltester (I4s load-bearing-sett, gjennom SQL-konnektoren, detach-bevist røde):
  sql-golden byte-fasit + mutasjonskontroller · typed-cell/NULL (NY §I5-søm) ·
  provenance/navigability/verdict-reservasjon/re-ingest-safety · SqlSource-kontrakt/
  typed-rendering/connection_ref/max_rows/read-only · spec-integritet utvidet med
  connection_ref. Stale type:"sql"-avvisningscase erstattet (sql er gyldig post-I5).
- docs/2026-07-04-I5-brief.md: brief + premiss-verifisering.

Suite 265 passed uten nøkkel/nettverk (239 + 26 nye) · ruff + mypy --strict rene.

[skip-docs] README + docs/extending.md er bevisst utsatt til I7 per sesjonsplan
(programmet batcher ingest-doc der, avgrenset til det D7 faktisk har — CSV + SQL nå,
HTTP/MCP kun pekere). Dokumentert i docs/2026-07-04-I5-brief.md.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017MM6BWb1hWmJZuXFZ7rjxT
2026-07-04 07:18:59 +02:00

105 lines
4.6 KiB
Python

"""Ingest SQL golden regression (ingest-spec §11) — D7 mirror of MAF I4.
Consumes the repo-local ``examples/ingest-golden-sql/`` (a byte-frozen copy of MAF I4's
golden — the SAME shared extraction, so this test proves D7's independent SQL connector
reproduces MAF's exact bytes from the shared spec alone). ``expected-bundle/`` is compared
file by file, byte for byte.
The ``connection_ref`` env var is pointed at the LOCAL sqlite fixture — a local source: no
network, no credentials (ingest-spec §8, §11). The mutation controls prove the net is taut:
one changed determinism input (a source row, the timestamp, the manifest bytes) must diverge.
"""
from __future__ import annotations
import shutil
import sqlite3
from pathlib import Path
import pytest
from portfolio_optimiser_claude.ingest import materialize
GOLDEN = Path(__file__).resolve().parents[1] / "examples" / "ingest-golden-sql"
CONNECTION_REF = "PORTEFOLJE_SQL_DSN"
_FIXTURE = GOLDEN / "fixture" / "portefolje.sqlite"
def _ingested_at() -> str:
return (GOLDEN / "ingested-at.txt").read_text(encoding="utf-8").strip()
def test_materializes_golden_byte_for_byte(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv(CONNECTION_REF, str(_FIXTURE))
bundle = tmp_path / "bundle"
bundle.mkdir()
materialize(GOLDEN / "manifest.json", bundle, _ingested_at())
expected = GOLDEN / "expected-bundle"
expected_names = sorted(p.name for p in expected.iterdir())
produced_names = sorted(p.name for p in bundle.iterdir())
assert produced_names == expected_names # no missing, no extra files
for name in expected_names:
assert (bundle / name).read_bytes() == (expected / name).read_bytes(), name
def test_reingest_is_idempotent(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
# ingest-spec §10: same source content, manifest, ingested_at → byte-identical.
monkeypatch.setenv(CONNECTION_REF, str(_FIXTURE))
bundle = tmp_path / "bundle"
bundle.mkdir()
materialize(GOLDEN / "manifest.json", bundle, _ingested_at())
first = {p.name: p.read_bytes() for p in bundle.iterdir()}
materialize(GOLDEN / "manifest.json", bundle, _ingested_at())
second = {p.name: p.read_bytes() for p in bundle.iterdir()}
assert first == second
class TestMutationControl:
"""One changed determinism input → divergence from the golden bytes."""
def test_changed_source_row_diverges(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
# Prove the connector actually reads the db: mutate a copied db, then diverge.
case = tmp_path / "case"
shutil.copytree(GOLDEN, case)
db = case / "fixture" / "portefolje.sqlite"
con = sqlite3.connect(db)
con.execute("UPDATE costs SET amount = 9999.9 WHERE id = 1")
con.commit()
con.close()
monkeypatch.setenv(CONNECTION_REF, str(db))
bundle = tmp_path / "bundle"
bundle.mkdir()
materialize(case / "manifest.json", bundle, _ingested_at())
golden_bytes = (GOLDEN / "expected-bundle" / "ingest-costs.md").read_bytes()
assert (bundle / "ingest-costs.md").read_bytes() != golden_bytes
def test_changed_timestamp_diverges(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setenv(CONNECTION_REF, str(_FIXTURE))
bundle = tmp_path / "bundle"
bundle.mkdir()
materialize(GOLDEN / "manifest.json", bundle, "2099-01-01T00:00:00Z")
golden_bytes = (GOLDEN / "expected-bundle" / "ingest-costs.md").read_bytes()
assert (bundle / "ingest-costs.md").read_bytes() != golden_bytes
def test_changed_manifest_bytes_change_the_stamp(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
# The ingest_manifest stamp is SHA-256 of the manifest's raw bytes (§5) —
# a whitespace-only edit still re-stamps every generated file.
monkeypatch.setenv(CONNECTION_REF, str(_FIXTURE))
case = tmp_path / "case"
shutil.copytree(GOLDEN, case)
manifest = case / "manifest.json"
manifest.write_bytes(manifest.read_bytes() + b"\n")
# The copied db lives beside the copied manifest; point the ref there too.
monkeypatch.setenv(CONNECTION_REF, str(case / "fixture" / "portefolje.sqlite"))
bundle = tmp_path / "bundle"
bundle.mkdir()
materialize(manifest, bundle, _ingested_at())
golden_bytes = (GOLDEN / "expected-bundle" / "ingest-costs.md").read_bytes()
assert (bundle / "ingest-costs.md").read_bytes() != golden_bytes