test_migrations.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import sqlite3
  2. from ontorefactor_governance.db import Database
  3. def test_existing_database_receives_llm_audit_columns(tmp_path):
  4. path = tmp_path / "pre-llm.db"
  5. connection = sqlite3.connect(path)
  6. connection.execute("""
  7. CREATE TABLE governance_agent_runs (
  8. id TEXT PRIMARY KEY, tenant_id TEXT NOT NULL, project_id TEXT NOT NULL,
  9. source_type TEXT NOT NULL, input_hash TEXT NOT NULL, status TEXT NOT NULL,
  10. result_json TEXT DEFAULT '{}', trace_json TEXT DEFAULT '[]', error TEXT DEFAULT '',
  11. created_at TEXT NOT NULL, completed_at TEXT
  12. )
  13. """)
  14. connection.commit()
  15. connection.close()
  16. db = Database(f"sqlite:///{path.as_posix()}")
  17. columns = {row["name"] for row in db.fetchall("PRAGMA table_info(governance_agent_runs)")}
  18. assert {
  19. "semantic_mode", "llm_provider", "llm_model", "prompt_version", "input_tokens",
  20. "output_tokens", "llm_latency_ms", "llm_request_hash", "llm_response_hash",
  21. "context_hash", "context_scope", "decision_id",
  22. } <= columns
  23. decision_tables = db.fetchall("SELECT name FROM sqlite_master WHERE type='table' AND name='governance_decisions'")
  24. assert decision_tables
  25. build_tables = {
  26. row["name"] for row in db.fetchall(
  27. "SELECT name FROM sqlite_master WHERE type='table' AND name IN ('ontology_builds','ontology_versions','ontology_reports')"
  28. )
  29. }
  30. assert build_tables == {"ontology_builds", "ontology_versions", "ontology_reports"}
  31. db.close()