Files
main_backend/tests/conftest.py
T
2026-02-03 02:15:07 +03:00

45 lines
1.1 KiB
Python

"""
Shared test fixtures and configuration.
"""
from __future__ import annotations
import pytest # type: ignore[import-not-found]
from fastapi.testclient import TestClient
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from cpv3.db.base import Base
from cpv3.main import app
# Use in-memory SQLite for tests (or configure a test database)
TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:"
@pytest.fixture
def test_client():
"""Create a test client for the FastAPI app."""
with TestClient(app) as client:
yield client
@pytest.fixture
async def test_db_session():
"""Create a test database session."""
engine = create_async_engine(TEST_DATABASE_URL, echo=False)
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
async_session = async_sessionmaker(
bind=engine, class_=AsyncSession, expire_on_commit=False
)
async with async_session() as session:
yield session
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
await engine.dispose()