import asyncio import pytest from httpx import AsyncClient from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine from sqlalchemy.orm import sessionmaker from app.main import app from app.db.base import Base, get_db TEST_DATABASE_URL = "postgresql+asyncpg://postgres:postgres@localhost:5432/agentworkforce_test" engine = create_async_engine(TEST_DATABASE_URL, echo=False, pool_pre_ping=True) TestSessionLocal = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False) async def override_get_db(): async with TestSessionLocal() as session: yield session app.dependency_overrides[get_db] = override_get_db @pytest.fixture(scope="session") def event_loop(): loop = asyncio.get_event_loop_policy().new_event_loop() yield loop loop.close() @pytest.fixture(scope="session", autouse=True) async def setup_database(): async with engine.begin() as conn: await conn.run_sync(Base.metadata.create_all) yield async with engine.begin() as conn: await conn.run_sync(Base.metadata.drop_all) @pytest.fixture async def client(): async with AsyncClient(app=app, base_url="http://test") as ac: yield ac @pytest.fixture async def db_session(): async with TestSessionLocal() as session: yield session await session.rollback()