generated from john/python-template
50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
"""Shared test fixtures.
|
|
|
|
Every test gets a fresh in-memory SQLite database so tests are
|
|
isolated, fast, and leave no artifacts on disk.
|
|
"""
|
|
|
|
import pytest
|
|
import pytest_asyncio
|
|
from sqlmodel import Session
|
|
from sqlmodel import SQLModel
|
|
from sqlmodel import create_engine
|
|
from sqlmodel.pool import StaticPool
|
|
|
|
from transcription.config import Settings
|
|
from transcription.config import get_settings
|
|
from transcription.db.operations import create_all
|
|
from transcription.db.runtime import dispose_database_runtime
|
|
from transcription.db.runtime import get_engine
|
|
from transcription.db.runtime import get_session
|
|
|
|
|
|
@pytest.fixture
|
|
def session():
|
|
"""Provide a clean synchronous database session for sync tests."""
|
|
engine = create_engine(
|
|
"sqlite://",
|
|
connect_args={"check_same_thread": False},
|
|
poolclass=StaticPool,
|
|
)
|
|
SQLModel.metadata.create_all(engine)
|
|
with Session(engine) as sync_session:
|
|
yield sync_session
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
async def default_settings():
|
|
"""Provide default settings for tests."""
|
|
settings = get_settings(database_url="sqlite:///:memory:")
|
|
await create_all(engine=get_engine(settings=settings))
|
|
return settings
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
async def async_session(default_settings: Settings):
|
|
"""Provide a clean asynchronous database session for async tests."""
|
|
async with get_session(settings=default_settings) as async_session:
|
|
yield async_session
|
|
|
|
await dispose_database_runtime()
|