from collections.abc import AsyncGenerator import pytest from httpx import ASGITransport, AsyncClient from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine from sqlalchemy.pool import NullPool from api.core.config import settings from api.database.session import get_db_session from api.main import app # Engine with NullPool prevents cross-loop asyncpg connection clashes in pytest test_engine = create_async_engine( settings.DATABASE_URL, poolclass=NullPool, echo=False, ) TestAsyncSessionLocal = async_sessionmaker( bind=test_engine, class_=AsyncSession, expire_on_commit=False, autocommit=False, autoflush=False, ) async def override_get_db_session() -> AsyncGenerator[AsyncSession, None]: async with TestAsyncSessionLocal() as session: try: yield session finally: await session.close() app.dependency_overrides[get_db_session] = override_get_db_session @pytest.fixture async def async_client() -> AsyncGenerator[AsyncClient, None]: """ Test client for FastAPI ASGI endpoints. """ transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: yield client