portfolio-optimiser-claude/tests/test_ingest_sql.py
Kjell Tore Guttormsen a7e8ffecb8 chore(deps): re-pin llm-ingestion-okf to v0.3.1 + migrate tests to stable error codes
Pin dae0bd1a -> v0.3.1 (=692f2df) on the public Forgejo mirror; uv.lock pins
the exact commit behind the tag.

- Drop the mypy override: the library ships py.typed from v0.2.0, so strict
  mode now follows its real types instead of follow_untyped_imports.
- Migrate 8 library-error assertions from pytest.raises(match=...) to
  exc.value.code — message text is explicitly unstable from v0.3.0, the
  codes are the stability contract.
- Fix a real breakage the bump surfaced: IngestResult gained a required
  `stamp` field (d3a3bcc), which the delegation fake did not construct.
- The read-only SQL test loses resolution under the code contract
  (`sql_failed` is generic), so it now proves read-onlyness by effect —
  the write never lands — instead of by message wording.
- Correct the guard plan: G1's persist-gate anchor (ingest.py:372-387) died
  with the 2026-07-16 adoption. Door A is ungated by the library's own
  README, so gating stays our responsibility at the call site.

Verified: 426 tests green, golden output byte-exact unchanged, full gate
clean (ruff + format + mypy strict).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RmNAgbRXUgvoSKxVK4Bevv
2026-07-20 07:22:09 +02:00

132 lines
5.3 KiB
Python

"""Ingest SQL unit + fail-fast contract tests (ingest-spec §4, §5, §8).
Pins the ``sql`` manifest contract, the §5 typed-cell fail-fast (a BLOB is never silently
coerced), the runtime ``connection_ref`` resolution, the §8 size cap, and read-only access
enforcement. Everything is local: a tmp sqlite fixture, no network, no credentials.
Since the adoption, every seam is proven THROUGH the consumer entry points
(``load_manifest``/``materialize``) — the connector internals are unit-owned by the
llm-ingestion-okf suite. The §5 typed-cell happy paths stay bound here in the repo by the
sql golden (int/float/text) and test_ingest_sql_loadbearing.py (NULL/REAL).
"""
from __future__ import annotations
import json
import sqlite3
from pathlib import Path
import pytest
from portfolio_optimiser_claude.ingest import (
ManifestError,
RenderError,
SourceError,
SqlSource,
load_manifest,
materialize,
)
INGESTED_AT = "2026-07-04T12:00:00Z"
def _db(tmp_path: Path, rows: list[tuple[object, ...]]) -> Path:
db = tmp_path / "src.sqlite"
con = sqlite3.connect(db)
con.execute("CREATE TABLE t (a INTEGER, b REAL, c TEXT, d BLOB)")
con.executemany("INSERT INTO t VALUES (?, ?, ?, ?)", rows)
con.commit()
con.close()
return db
def _sql_manifest(query: str = "SELECT a FROM t ORDER BY a") -> dict:
return {
"manifest_version": 1,
"source": {"type": "sql", "id": "db", "connection_ref": "SRC_DSN"},
"bundle_summary": "s",
"extractions": [
{"id": "e", "title": "T", "query": query, "okf_type": "dataset", "max_rows": 5}
],
}
def _write_manifest(tmp_path: Path, manifest: dict) -> Path:
path = tmp_path / "manifest.json"
path.write_text(json.dumps(manifest), encoding="utf-8")
return path
class TestSqlSourceContract:
"""§4: the sql source is a valid discriminated variant; the reference is required."""
def test_valid_sql_source_loads(self, tmp_path: Path) -> None:
manifest = load_manifest(_write_manifest(tmp_path, _sql_manifest()))
assert isinstance(manifest.source, SqlSource)
assert manifest.source.connection_ref == "SRC_DSN"
def test_connection_ref_is_required(self, tmp_path: Path) -> None:
manifest = _sql_manifest()
del manifest["source"]["connection_ref"]
with pytest.raises(ManifestError):
load_manifest(_write_manifest(tmp_path, manifest))
def test_sql_source_id_grammar_enforced(self, tmp_path: Path) -> None:
manifest = _sql_manifest()
manifest["source"]["id"] = "Bad Id"
with pytest.raises(ManifestError):
load_manifest(_write_manifest(tmp_path, manifest))
class TestTypedCellFailFast:
"""§5: an unsupported cell type (a BLOB, say) MUST fail — never a silent coercion."""
def test_blob_cell_fails_typed(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
db = _db(tmp_path, [(1, 1.0, "x", b"\x00\x01")])
manifest_path = _write_manifest(tmp_path, _sql_manifest("SELECT d FROM t"))
monkeypatch.setenv("SRC_DSN", str(db))
with pytest.raises(RenderError) as exc:
materialize(manifest_path, tmp_path / "bundle", INGESTED_AT)
assert exc.value.code == "unsupported_cell_type"
class TestConnectorRuntime:
"""§8: reference resolution, size cap, and read-only access — all fail-fast."""
def test_unset_connection_ref_fails_fast(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.delenv("SRC_DSN", raising=False)
manifest_path = _write_manifest(tmp_path, _sql_manifest())
with pytest.raises(SourceError) as exc:
materialize(manifest_path, tmp_path / "bundle", INGESTED_AT)
assert exc.value.code == "connection_ref_unset"
def test_max_rows_cap_enforced_fail_fast(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
db = _db(tmp_path, [(1, 1.0, "x", None), (2, 2.0, "y", None)])
manifest = _sql_manifest()
manifest["extractions"][0]["max_rows"] = 1 # 2 rows > 1
manifest_path = _write_manifest(tmp_path, manifest)
monkeypatch.setenv("SRC_DSN", str(db))
with pytest.raises(SourceError) as exc:
materialize(manifest_path, tmp_path / "bundle", INGESTED_AT)
assert exc.value.code == "max_rows_exceeded"
def test_connection_is_read_only(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
db = _db(tmp_path, [(1, 1.0, "x", None)])
# The connector opens read-only (§4 SHOULD): a write statement is refused at the
# database and surfaces as a typed SourceError.
manifest_path = _write_manifest(tmp_path, _sql_manifest("UPDATE t SET a = 9"))
monkeypatch.setenv("SRC_DSN", str(db))
with pytest.raises(SourceError) as exc:
materialize(manifest_path, tmp_path / "bundle", INGESTED_AT)
# `sql_failed` is the generic statement-failure code; read-onlyness is proven by
# the effect, not the message (message text is unstable from library v0.3.0).
assert exc.value.code == "sql_failed"
con = sqlite3.connect(db)
try:
assert con.execute("SELECT a FROM t").fetchall() == [(1,)] # write never landed
finally:
con.close()