from __future__ import annotations import pytest from app.config import Settings from app.db import ( EmbedIndexError, VecLoadError, connect, delete_receipt, init_schema, insert_receipt, ) from app.schemas import ReceiptExtract, ReceiptStatus def test_schema_and_meta(settings: Settings) -> None: try: con = connect(settings.db_path) init_schema(con, settings) except VecLoadError: pytest.skip("sqlite-vec not loadable") tables = { row[0] for row in con.execute("SELECT name FROM sqlite_master WHERE type IN ('table','view')") } assert "receipts" in tables assert "catalog" in tables rid = insert_receipt( con, source_path="x.jpg", sha256="abc", status=ReceiptStatus.needs_review, extract=ReceiptExtract(vendor="A", category="dining"), ) assert rid == 1 other = settings.model_copy(update={"embed_dim": 3840, "embed_model": "other"}) with pytest.raises(EmbedIndexError): init_schema(con, other) assert delete_receipt(con, rid, unlink_file=False) is True assert con.execute("SELECT COUNT(*) AS n FROM receipts").fetchone()["n"] == 0 assert delete_receipt(con, rid, unlink_file=False) is False con.close()