34 lines
877 B
Python
34 lines
877 B
Python
|
|
"""Test configuration."""
|
||
|
|
import pytest
|
||
|
|
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
||
|
|
from sqlalchemy.orm import sessionmaker
|
||
|
|
|
||
|
|
from app.models.base import Base
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.fixture(scope="function")
|
||
|
|
async def db():
|
||
|
|
"""Create a fresh in-memory SQLite database for each test."""
|
||
|
|
engine = create_async_engine(
|
||
|
|
"sqlite+aiosqlite:///:memory:",
|
||
|
|
future=True,
|
||
|
|
echo=False,
|
||
|
|
)
|
||
|
|
async with engine.begin() as conn:
|
||
|
|
await conn.run_sync(Base.metadata.create_all)
|
||
|
|
|
||
|
|
AsyncSessionLocal = sessionmaker(
|
||
|
|
engine,
|
||
|
|
class_=AsyncSession,
|
||
|
|
expire_on_commit=False,
|
||
|
|
autoflush=False,
|
||
|
|
autocommit=False,
|
||
|
|
)
|
||
|
|
|
||
|
|
async with AsyncSessionLocal() as session:
|
||
|
|
yield session
|
||
|
|
|
||
|
|
async with engine.begin() as conn:
|
||
|
|
await conn.run_sync(Base.metadata.drop_all)
|
||
|
|
await engine.dispose()
|