""" Pytest configuration and fixtures for testing. """ import pytest import asyncio from httpx import AsyncClient from sqlalchemy import create_engine from sqlalchemy.pool import StaticPool from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession from sqlalchemy.orm import sessionmaker from app.main import app from app.db.database import get_db, Base from app.core.config import settings # Test database URL TEST_DATABASE_URL = "sqlite+aiosqlite:///./test.db" @pytest.fixture(scope="session") def event_loop(): """Create an instance of the default event loop for the test session.""" loop = asyncio.get_event_loop_policy().new_event_loop() yield loop loop.close() @pytest.fixture(scope="session") async def test_engine(): """Create test database engine.""" engine = create_async_engine( TEST_DATABASE_URL, connect_args={"check_same_thread": False}, poolclass=StaticPool, ) # Create tables async with engine.begin() as conn: await conn.run_sync(Base.metadata.create_all) yield engine # Cleanup async with engine.begin() as conn: await conn.run_sync(Base.metadata.drop_all) await engine.dispose() @pytest.fixture async def test_db(test_engine): """Create test database session.""" async_session = sessionmaker( test_engine, class_=AsyncSession, expire_on_commit=False ) async with async_session() as session: yield session @pytest.fixture async def client(test_db): """Create test client.""" async def override_get_db(): yield test_db app.dependency_overrides[get_db] = override_get_db async with AsyncClient(app=app, base_url="http://test") as client: yield client app.dependency_overrides.clear() @pytest.fixture def test_user_data(): """Test user data.""" return { "email": "test@example.com", "username": "testuser", "full_name": "Test User", "password": "testpassword123" } @pytest.fixture def test_api_key_data(): """Test API key data.""" return { "name": "Test API Key", "scopes": ["llm.chat", "llm.embeddings"], "budget_limit": 100.0, "budget_period": "monthly" }