""" Configuration pytest pour les tests du backend v2. """ import pytest from httpx import AsyncClient, ASGITransport from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker from api.main import app from infrastructure.database.session import get_async_session from infrastructure.database.models import Base # Base de données de test (en mémoire) TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:" test_engine = create_async_engine( TEST_DATABASE_URL, echo=False, ) TestSessionLocal = async_sessionmaker( bind=test_engine, class_=AsyncSession, expire_on_commit=False, ) async def override_get_async_session(): """Override de la session pour les tests.""" async with TestSessionLocal() as session: yield session @pytest.fixture async def test_db(): """Fixture pour créer les tables de test.""" async with test_engine.begin() as conn: await conn.run_sync(Base.metadata.create_all) yield async with test_engine.begin() as conn: await conn.run_sync(Base.metadata.drop_all) @pytest.fixture async def client(test_db): """Fixture pour le client HTTP de test.""" app.dependency_overrides[get_async_session] = override_get_async_session async with AsyncClient( transport=ASGITransport(app=app), base_url="http://test" ) as ac: yield ac app.dependency_overrides.clear() @pytest.fixture async def db_session(test_db): """Fixture pour une session de base de données de test.""" async with TestSessionLocal() as session: yield session