Skip to content

Commit b1c3fd8

Browse files
kutcodeclaude
andcommitted
Patch async_session on all router modules in test fixture
Background tasks in upload/qa/flagged routers use the module-level async_session directly (not the FastAPI dependency), so the test fixture must also patch those references to point at the test DB. Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent ed5eff6 commit b1c3fd8

1 file changed

Lines changed: 34 additions & 2 deletions

File tree

backend/tests/conftest.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
99
from docx import Document as DocxDocument
1010

11+
import app.database as _db_module
12+
import app.routers.upload as _upload_module
13+
import app.routers.qa as _qa_module
14+
import app.routers.flagged as _flagged_module
1115
from app.database import Base, get_db
1216
import app.models # noqa: F401 — ensure all models are registered with Base.metadata before create_all
1317
from app.main import app
@@ -64,17 +68,45 @@ async def db_session(db_engine):
6468

6569

6670
@pytest_asyncio.fixture
67-
async def client(db_session):
68-
"""HTTP test client with overridden DB dependency."""
71+
async def client(db_engine, db_session):
72+
"""HTTP test client with overridden DB dependency.
73+
74+
Patches both the FastAPI dependency *and* the module-level engine/session
75+
so that background tasks (which use ``async_session()`` directly) also
76+
hit the test database.
77+
"""
6978
async def override_get_db():
7079
yield db_session
7180

81+
# Patch module-level engine + session factory so background tasks use the test DB
82+
test_session_factory = async_sessionmaker(db_engine, class_=AsyncSession, expire_on_commit=False)
83+
84+
originals = {
85+
"db_engine": _db_module.engine,
86+
"db_session": _db_module.async_session,
87+
"upload_session": _upload_module.async_session,
88+
"qa_session": _qa_module.async_session,
89+
"flagged_session": _flagged_module.async_session,
90+
}
91+
_db_module.engine = db_engine
92+
_db_module.async_session = test_session_factory
93+
_upload_module.async_session = test_session_factory
94+
_qa_module.async_session = test_session_factory
95+
_flagged_module.async_session = test_session_factory
96+
7297
app.dependency_overrides[get_db] = override_get_db
7398
transport = ASGITransport(app=app)
7499
async with AsyncClient(transport=transport, base_url="http://test") as c:
75100
yield c
76101
app.dependency_overrides.clear()
77102

103+
# Restore originals
104+
_db_module.engine = originals["db_engine"]
105+
_db_module.async_session = originals["db_session"]
106+
_upload_module.async_session = originals["upload_session"]
107+
_qa_module.async_session = originals["qa_session"]
108+
_flagged_module.async_session = originals["flagged_session"]
109+
78110

79111
@pytest.fixture
80112
def make_docx(tmp_path):

0 commit comments

Comments
 (0)