rentcache/tests/conftest.py
Ryan Malloy 9a06e9d059 Initial implementation of RentCache FastAPI proxy server
- Complete FastAPI proxy server with all Rentcast API endpoints
- Intelligent caching with SQLite backend and Redis support
- Rate limiting and usage tracking per API key
- CLI administration tools for key and cache management
- Comprehensive models with SQLAlchemy for persistence
- Health checks and metrics endpoints
- Production-ready configuration management
- Extensive test coverage with pytest and fixtures
- Rich CLI interface with click and rich libraries
- Soft delete caching strategy for analytics
- TTL-based cache expiration with endpoint-specific durations
- CORS, compression, and security middleware
- Structured logging with JSON format
- Cost tracking and estimation for API usage
- Background task support architecture
- Docker deployment ready
- Comprehensive documentation and setup instructions
2025-09-09 14:42:51 -06:00

80 lines
2.0 KiB
Python

"""
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
}