""" Pytest configuration for RentCache tests. """ import asyncio import pytest import pytest_asyncio from httpx import AsyncClient from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker from sqlalchemy.pool import StaticPool from rentcache.models import Base from rentcache.server import app, get_db # Test database URL TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:" @pytest.fixture(scope="session") def event_loop(): """Create an instance of the default event loop for the test session.""" loop = asyncio.new_event_loop() yield loop loop.close() @pytest_asyncio.fixture(scope="function") async def test_engine(): """Create a test database engine.""" engine = create_async_engine( TEST_DATABASE_URL, echo=False, poolclass=StaticPool, connect_args={"check_same_thread": False}, ) async with engine.begin() as conn: await conn.run_sync(Base.metadata.create_all) yield engine await engine.dispose() @pytest_asyncio.fixture(scope="function") async def test_session(test_engine): """Create a test database session.""" SessionLocal = async_sessionmaker( bind=test_engine, class_=AsyncSession, expire_on_commit=False ) async with SessionLocal() as session: yield session @pytest_asyncio.fixture(scope="function") async def test_client(test_session): """Create a test HTTP client with database override.""" def override_get_db(): yield test_session 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 sample_api_key_data(): """Sample data for creating API keys in tests.""" return { "key_name": "test_key", "rentcast_api_key": "test_rentcast_key_123", "daily_limit": 1000, "monthly_limit": 10000 }