portfolio-optimiser/.claude/projects/2026-07-04-i4-ingest-sql-maf/build_fixture.py
Kjell Tore Guttormsen 1b7612b22d docs(i4): brief + plan + golden fixture builder (I4)
Session brief (scope, non-goals, pinned SQL type/number decisions, verification) + TDD plan + build_fixture.py (regenerates the sqlite golden; the committed db bytes need not reproduce — the golden compares the derived bundle).
2026-07-04 06:56:08 +02:00

53 lines
1.9 KiB
Python

"""Build the I4 sql golden fixture: examples/ingest-golden-sql/fixture/portefolje.sqlite
+ ingested-at.txt.
Regenerable; the committed db BYTES need not be reproduced bit-for-bit — the golden test
compares the derived bundle (from db CONTENT via the manifest queries), not the db file.
Parameterized inserts keep the backslash/pipe escape-probe values exact (no SQL string-literal
surprises: 'c:\\temp' would be a tab if written as a Python literal). The data is chosen to
discriminate a typed §5 implementation from naive ones — SQL NULL, an integral REAL (4200.0),
non-integral REALs, and pipe/backslash text.
"""
from __future__ import annotations
import sqlite3
from pathlib import Path
GOLDEN = Path(__file__).resolve().parents[3] / "examples" / "ingest-golden-sql"
def main() -> None:
fixture = GOLDEN / "fixture"
fixture.mkdir(parents=True, exist_ok=True)
db = fixture / "portefolje.sqlite"
if db.exists():
db.unlink()
conn = sqlite3.connect(db)
try:
conn.execute("CREATE TABLE costs (id INTEGER, item TEXT, amount REAL, note TEXT)")
conn.executemany(
"INSERT INTO costs VALUES (?, ?, ?, ?)",
[
(1, "LED-armatur", 1200.5, "godkjent"),
(2, "sensor", 89.9, None), # NULL → empty cell (discriminator)
(3, "kabel", 4200.0, "rest"), # integral REAL → "4200.0", not "4200"
],
)
conn.execute("CREATE TABLE meta (k TEXT, v TEXT)")
conn.executemany(
"INSERT INTO meta VALUES (?, ?)",
[
("owner", "anlegg | drift"), # pipe → escaped \|
("path", "c:\\temp"), # backslash → escaped \\
],
)
conn.commit()
finally:
conn.close()
(GOLDEN / "ingested-at.txt").write_bytes(b"2026-07-04T12:00:00Z\n")
print(f"built {db} ({db.stat().st_size} bytes) + ingested-at.txt")
if __name__ == "__main__":
main()