fire-planner/tests/conftest.py
2026-05-07 17:06:19 +00:00

36 lines
1.2 KiB
Python

"""Shared pytest fixtures.
Tests run against an in-memory SQLite DB created via the SQLAlchemy ORM
metadata directly — fast, deterministic, and avoids running Alembic
end-to-end on every test (the migration is exercised separately).
"""
from collections.abc import AsyncIterator
import pytest_asyncio
from sqlalchemy.ext.asyncio import (
AsyncEngine,
AsyncSession,
async_sessionmaker,
create_async_engine,
)
from fire_planner.db import SCHEMA_NAME, Base
@pytest_asyncio.fixture
async def engine() -> AsyncIterator[AsyncEngine]:
eng = create_async_engine("sqlite+aiosqlite:///:memory:")
async with eng.begin() as conn:
# SQLite has no schema concept — attach an in-memory DB under the
# `fire_planner` name so `__table_args__ = {"schema": ...}` resolves.
await conn.exec_driver_sql(f"ATTACH DATABASE ':memory:' AS {SCHEMA_NAME}")
await conn.run_sync(Base.metadata.create_all)
yield eng
await eng.dispose()
@pytest_asyncio.fixture
async def session(engine: AsyncEngine) -> AsyncIterator[AsyncSession]:
factory = async_sessionmaker(engine, expire_on_commit=False)
async with factory() as sess:
yield sess