From fa725f1a0eb8b80402a74657aa72f1e8646f7e07 Mon Sep 17 00:00:00 2001 From: Indiana Date: Mon, 20 Jul 2026 15:22:41 +0000 Subject: [PATCH] fix: move NullPool test fix from db.py into a conftest-local test engine MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Production code shouldn't branch on `"pytest" in sys.modules` — it's fragile and couples db.py to test tooling. Instead, conftest.py now builds its own dedicated NullPool engine directly from settings, used only for the drop_all/create_all reset and the get_db override. The production engine in app/db.py is untouched and never exercised during tests, so this fully preserves the event-loop fix while keeping prod code test-agnostic. --- backend/app/db.py | 16 +--------------- backend/tests/conftest.py | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/backend/app/db.py b/backend/app/db.py index e44dd35..fa51df5 100644 --- a/backend/app/db.py +++ b/backend/app/db.py @@ -1,23 +1,9 @@ -import sys - from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine from sqlalchemy.orm import DeclarativeBase -from sqlalchemy.pool import NullPool from app.config import settings -# pytest-asyncio gives each test function its own event loop by default, but -# this engine (and its connection pool) is a module-level singleton created -# once and reused across every test. asyncpg connections are bound to the -# event loop they were opened on, so pooling them across per-test loops -# raises "got Future attached to a different loop". NullPool sidesteps this -# by opening a fresh connection per checkout instead of reusing one from a -# prior loop. Only applied under pytest; production keeps normal pooling. -_engine_kwargs = {"echo": False} -if "pytest" in sys.modules: - _engine_kwargs["poolclass"] = NullPool - -engine = create_async_engine(settings.database_url, **_engine_kwargs) +engine = create_async_engine(settings.database_url, echo=False) async_session_maker = async_sessionmaker(engine, expire_on_commit=False) diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index f372944..df46c59 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -1,16 +1,24 @@ import pytest_asyncio from httpx import ASGITransport, AsyncClient -from sqlalchemy.ext.asyncio import async_sessionmaker +from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine +from sqlalchemy.pool import NullPool -from app.db import Base, engine, get_db +from app.config import settings +from app.db import Base, get_db from app.main import app -TestSessionLocal = async_sessionmaker(engine, expire_on_commit=False) +# pytest-asyncio gives each test function its own event loop by default; +# asyncpg connections are bound to the loop they were opened on, so a pooled +# connection from one test's loop breaks the next test. NullPool sidesteps +# this by opening a fresh connection per checkout — scoped to this dedicated +# test engine so app.db.engine (used by production) is unaffected. +test_engine = create_async_engine(settings.database_url, poolclass=NullPool) +TestSessionLocal = async_sessionmaker(test_engine, expire_on_commit=False) @pytest_asyncio.fixture(autouse=True) async def _reset_db(): - async with engine.begin() as conn: + async with test_engine.begin() as conn: await conn.run_sync(Base.metadata.drop_all) await conn.run_sync(Base.metadata.create_all) yield