Fix SQLite CI test failures: UUID binding, func.now() defaults, F402 lint
CI / lint (pull_request) Failing after 4s
CI / typecheck (pull_request) Failing after 28s
CI / test (pull_request) Failing after 2m37s
CI / build-and-push (pull_request) Has been skipped
CI / deploy-dev (pull_request) Has been skipped
CI / deploy-uat (pull_request) Has been skipped

Three fixes from PR #35 review:

1. Fix F402: rename loop var 'table' → 'metadata_table' in test_encrypted_json.py
2. Strip func.now() server_defaults in conftest.py engine/db_engine fixtures
3. Add aiosqlite UUID adapter for async engine

Model changes to provide Python-side defaults for SQLite compatibility:
- TimestampMixin: add default=_utcnow for created_at/updated_at
- UUIDPrimaryKeyMixin: use GuidType for cross-DB UUID handling
- User.id: use GuidType() instead of Text, Mapped[uuid.UUID]
- User.email_verified: add default=False
- Purchase.ingested_at: add default=_utcnow
- types.py: add GuidType TypeDecorator for UUID→String conversion

Fixes: CAR-1012
This commit is contained in:
Flea Flicker
2026-05-30 09:08:46 +00:00
parent 41a887a73b
commit 9fbab62717
6 changed files with 71 additions and 17 deletions
+15 -4
View File
@@ -8,6 +8,7 @@ import secrets
import uuid
from datetime import UTC, datetime, timedelta
import aiosqlite
import pytest
from httpx import ASGITransport, AsyncClient
from sqlalchemy import create_engine, event, text
@@ -19,6 +20,8 @@ from cartsnitch_api.database import get_db
from cartsnitch_api.main import create_app
from cartsnitch_api.models import Base
aiosqlite.register_adapter(uuid.UUID, lambda u: str(u))
TEST_JWT_SECRET = secrets.token_urlsafe(32)
TEST_SERVICE_KEY = secrets.token_urlsafe(32)
TEST_FERNET_KEY = "7reF42nmTwbdN21PBoubGp7h_FU8qSimstmlaMLoRK8="
@@ -58,15 +61,22 @@ def engine():
"""
eng = create_engine("sqlite:///:memory:")
for table in Base.metadata.tables.values():
for col in table.columns.values():
@event.listens_for(eng, "connect")
def set_sqlite_pragma(dbapi_connection, connection_record):
cursor = dbapi_connection.cursor()
cursor.execute("PRAGMA foreign_keys=ON")
cursor.close()
for metadata_table in Base.metadata.tables.values():
for col in metadata_table.columns.values():
sd = col.server_default
if sd is not None:
if not hasattr(sd, "expression"):
col.server_default = None
continue
expr_str = str(sd.expression).lower()
if "gen_random_uuid" in expr_str or "gen_random_bytes" in expr_str:
_pg_fns = ("gen_random_uuid", "gen_random_bytes", "now()")
if any(pg_fn in expr_str for pg_fn in _pg_fns):
col.server_default = None
Base.metadata.create_all(eng)
@@ -100,7 +110,8 @@ async def db_engine():
col.server_default = None
continue
expr_str = str(sd.expression).lower()
if "gen_random_uuid" in expr_str or "gen_random_bytes" in expr_str:
_pg_fns = ("gen_random_uuid", "gen_random_bytes", "now()")
if any(pg_fn in expr_str for pg_fn in _pg_fns):
col.server_default = None
async with engine.begin() as conn:
+4 -3
View File
@@ -18,15 +18,16 @@ from cartsnitch_api.models.user import User, UserStoreAccount
def engine():
eng = create_engine("sqlite:///:memory:")
for table in Base.metadata.tables.values():
for col in table.columns.values():
for metadata_table in Base.metadata.tables.values():
for col in metadata_table.columns.values():
sd = col.server_default
if sd is not None:
if not hasattr(sd, "expression"):
col.server_default = None
continue
expr_str = str(sd.expression).lower()
if "gen_random_uuid" in expr_str or "gen_random_bytes" in expr_str:
_pg_fns = ("gen_random_uuid", "gen_random_bytes", "now()")
if any(pg_fn in expr_str for pg_fn in _pg_fns):
col.server_default = None
Base.metadata.create_all(eng)