Open-source personal finance manager. Self-hosted, privacy-first.
test: stop SQLite from turning numeric-looking UUIDs into floats (#948)
Tássio Noronha9045232f96e3
diff --git a/backend/tests/conftest.py b/backend/tests/conftest.pyindex 82be07bb..7f2c6d5a 100644--- a/backend/tests/conftest.py+++ b/backend/tests/conftest.py@@ -97,10 +97,23 @@ engine = create_async_engine(TestSessionLocal = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)-# SQLite doesn't support PostgreSQL UUID type natively — SQLAlchemy handles the-# mapping automatically when we create tables via Base.metadata (it converts-# PostgreSQL UUID to CHAR(32)). We just need to make sure we use string-based-# UUID comparisons.+# The models declare ids with the PostgreSQL UUID type, and on SQLite the+# DDL comes out as a literal `UUID`. SQLite does not know that type, so the+# column gets NUMERIC affinity, and any stored text that parses as a number+# is silently converted to one. A uuid4 hex made only of digits, or of digits+# with a single "e" ("6778776194704156e000000000000016" is valid scientific+# notation), comes back as a float and uuid.UUID() raises. With tens of+# thousands of ids per run that hit roughly one run in ten, on whatever test+# happened to draw the number. Declaring the column as CHAR(32) gives it TEXT+# affinity, which is what the hex string needs. Test-only: Postgres keeps its+# native type.+from sqlalchemy.dialects.postgresql import UUID as _PgUUID # noqa: E402+from sqlalchemy.ext.compiler import compiles # noqa: E402+++@compiles(_PgUUID, "sqlite")+def _pg_uuid_as_text_on_sqlite(type_, compiler, **kw):+ return "CHAR(32)"@pytest_asyncio.fixture(scope="session", loop_scope="session", autouse=True)diff --git a/backend/tests/test_sqlite_uuid_affinity.py b/backend/tests/test_sqlite_uuid_affinity.pynew file mode 100644index 00000000..a2f8a133--- /dev/null+++ b/backend/tests/test_sqlite_uuid_affinity.py@@ -0,0 +1,41 @@+"""The test database must hand UUIDs back as UUIDs, whatever digits they hold.++SQLite gives a column declared as ``UUID`` NUMERIC affinity, so a hex string+that happens to parse as a number is stored as a float. ``uuid4`` produces+one such string roughly once in a million draws, which across a full run was+about one failure in ten, always on an unrelated test. These ids fail+deterministically without the ``CHAR(32)`` compile hook in conftest.+"""++import uuid++import pytest+from sqlalchemy import select++from app.models.category import Category++# All digits, and digits with one "e" in a spot SQLite reads as an exponent.+NUMERIC_LOOKING_IDS = [+ uuid.UUID("12345678-1234-4678-9234-567812345678"),+ uuid.UUID("67787761-9470-4156-e000-000000000016"),+]++[email protected]("category_id", NUMERIC_LOOKING_IDS, ids=["digits", "digits-with-e"])+async def test_numeric_looking_uuid_round_trips(session, test_user, test_workspace, category_id):+ session.add(+ Category(+ id=category_id,+ user_id=test_user.id,+ workspace_id=test_workspace.id,+ name=f"cat {category_id}",+ )+ )+ await session.commit()++ loaded = await session.scalar(select(Category).where(Category.id == category_id))++ assert loaded is not None+ assert loaded.id == category_id+ assert isinstance(loaded.id, uuid.UUID)