Files
hms-licht-ton/backend/tests/conftest.py
T
A0 Implementation Engineer e62ece1c06 feat: T03 backend core – FastAPI + DB models + Equipment API + Redis cache + health + tests
- FastAPI app with CORS, lifespan handlers
- Pydantic Settings config (DB, Redis, CORS, SMTP, JWT, Rentman)
- SQLAlchemy async engine + session (DeclarativeBase)
- 6 DB models: EquipmentCache, RentalRequest, RentalRequestItem, Contact, AdminUser, SyncLog
- Pydantic schemas: EquipmentItem, EquipmentDetail, PaginatedEquipment, ContactCreate, ContactResponse
- Redis cache helper: set/get/delete_pattern, rate limiting, equipment key builders
- Equipment router: list (search/category/sort/pagination), detail, categories – all cached
- Contact router: POST with Pydantic validation + rate limiting (5/min)
- Health router: GET /api/health with DB + Redis status
- 28 pytest tests (all pass, 90% coverage)
- Dockerfile, requirements.txt, pytest.ini, test_report.md
2026-07-09 01:26:45 +02:00

69 lines
2.1 KiB
Python

"""Pytest fixtures for async tests."""
import asyncio
from typing import AsyncGenerator
import fakeredis.aioredis
import pytest
import pytest_asyncio
from httpx import ASGITransport, AsyncClient
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
import app.cache as cache_module
from app.database import Base, get_db
from app.main import app
@pytest.fixture(scope="session")
def event_loop():
"""Create a single event loop for all tests."""
loop = asyncio.new_event_loop()
yield loop
loop.close()
@pytest_asyncio.fixture
async def test_engine():
"""Create a fresh in-memory SQLite engine for each test."""
engine = create_async_engine("sqlite+aiosqlite:///:memory:", echo=False)
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
yield engine
await engine.dispose()
@pytest_asyncio.fixture
async def db_session(test_engine) -> AsyncGenerator[AsyncSession, None]:
"""Yield a DB session bound to the test engine."""
session_factory = async_sessionmaker(test_engine, class_=AsyncSession, expire_on_commit=False)
async with session_factory() as session:
yield session
@pytest_asyncio.fixture
async def fake_redis():
"""Provide a fakeredis instance and patch the cache module global."""
fake = fakeredis.aioredis.FakeRedis(decode_responses=True)
cache_module._cache_client = fake
yield fake
cache_module._cache_client = None
await fake.flushall()
@pytest_asyncio.fixture
async def client(test_engine, fake_redis) -> AsyncGenerator[AsyncClient, None]:
"""Provide an async HTTP client with test DB and fake Redis."""
session_factory = async_sessionmaker(test_engine, class_=AsyncSession, expire_on_commit=False)
async def override_get_db() -> AsyncGenerator[AsyncSession, None]:
async with session_factory() as session:
yield session
app.dependency_overrides[get_db] = override_get_db
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as ac:
yield ac
app.dependency_overrides.clear()