fix: stop patching wiregui.db globally — broke e2e tests
Some checks failed
Dev / test (push) Failing after 4m3s
Dev / docker (push) Has been skipped

Remove module-level engine/session replacement that affected all tests
including e2e. The test engine is now only used via the session fixture,
so e2e tests keep using the real DB the app writes to.
This commit is contained in:
Stefano Bertelli 2026-03-31 22:14:34 -05:00
parent 8cf16c7f91
commit 554da599ba

View file

@ -1,11 +1,9 @@
"""Shared test fixtures — async DB session using a test database. """Shared test fixtures — async DB session using a test database.
The module-level code below replaces ``wiregui.db.engine`` and Unit tests use the ``session`` fixture, which provides a per-test
``wiregui.db.async_session`` with instances pointing at the **test** database savepoint-isolated session on a dedicated test engine. E2E tests do NOT
*before* any test (or other module) can grab a reference to the originals. use this fixture and are therefore unaffected they keep using the real
This means every ``from wiregui.db import async_session`` whether in test ``wiregui.db.async_session`` that talks to the app's database.
files or in production code like ``wiregui.utils.server_key`` will get the
test-database session maker.
""" """
import os import os
@ -13,10 +11,9 @@ from collections.abc import AsyncGenerator
import pytest_asyncio import pytest_asyncio
from sqlalchemy import text from sqlalchemy import text
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlmodel import SQLModel from sqlmodel import SQLModel
import wiregui.db as _db_module
from wiregui.config import get_settings from wiregui.config import get_settings
# All models must be imported so SQLModel.metadata knows about them # All models must be imported so SQLModel.metadata knows about them
@ -60,21 +57,14 @@ def _ensure_test_db_sync():
_ensure_test_db_sync() _ensure_test_db_sync()
# --------------------------------------------------------------------------- # Test engine — only used by the ``session`` fixture and unit tests.
# Replace the production engine/session in wiregui.db at import time so that # NOT assigned to wiregui.db so e2e tests are unaffected.
# every module that does ``from wiregui.db import async_session`` picks up the
# test database. This MUST happen before test modules are collected (which
# triggers their top-level imports).
# ---------------------------------------------------------------------------
_test_engine = create_async_engine(TEST_DATABASE_URL) _test_engine = create_async_engine(TEST_DATABASE_URL)
_test_session_factory = async_sessionmaker(_test_engine, expire_on_commit=False)
_db_module.engine = _test_engine
_db_module.async_session = _test_session_factory
@pytest_asyncio.fixture(scope="session", autouse=True) @pytest_asyncio.fixture(scope="session")
async def _setup_test_tables(): async def _test_tables():
"""Create all tables once at the start of the test session, drop at end.""" """Create all tables once per test session, drop at end."""
async with _test_engine.begin() as conn: async with _test_engine.begin() as conn:
await conn.run_sync(SQLModel.metadata.create_all) await conn.run_sync(SQLModel.metadata.create_all)
yield yield
@ -84,7 +74,7 @@ async def _setup_test_tables():
@pytest_asyncio.fixture @pytest_asyncio.fixture
async def session() -> AsyncGenerator[AsyncSession]: async def session(_test_tables) -> AsyncGenerator[AsyncSession]:
"""Per-test session with transaction isolation. """Per-test session with transaction isolation.
The session is bound to a connection-level transaction that is always The session is bound to a connection-level transaction that is always