generated from john/python-template
72 lines
2.7 KiB
Python
72 lines
2.7 KiB
Python
"""Tests for transcription.db — async schema bootstrap/runtime behavior."""
|
|
|
|
from sqlalchemy import inspect
|
|
from sqlalchemy import text
|
|
import pytest
|
|
|
|
|
|
class TestSchemaBootstrap:
|
|
"""Verify async create_all produces the expected table set."""
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_all_creates_expected_tables(self, default_settings):
|
|
"""After async create_all(), document/job/transcript/revision tables exist."""
|
|
# Ensure models are imported so metadata is populated.
|
|
from transcription.models import Document, Job, Transcript, TranscriptRevision # noqa: F401
|
|
|
|
from transcription.db.operations import create_all
|
|
from transcription.db.runtime import get_engine
|
|
|
|
engine = get_engine(settings=default_settings)
|
|
await create_all(engine=engine)
|
|
|
|
async with engine.begin() as connection:
|
|
table_names = set(await connection.run_sync(lambda sync_conn: inspect(sync_conn).get_table_names()))
|
|
|
|
assert "document" in table_names
|
|
assert "job" in table_names
|
|
assert "transcript" in table_names
|
|
assert "transcriptrevision" in table_names
|
|
|
|
|
|
class TestSessionFactory:
|
|
"""Verify async get_session yields a usable AsyncSession."""
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_session_yields_session(self, default_settings):
|
|
"""get_session() yields an AsyncSession with a live connection."""
|
|
from transcription.db.runtime import get_session
|
|
|
|
async with get_session(settings=default_settings) as session:
|
|
result = await session.exec(text("SELECT 1"))
|
|
assert result.first()[0] == 1
|
|
|
|
|
|
class TestBootstrapPolicy:
|
|
"""Verify startup schema bootstrap policy via Settings property."""
|
|
|
|
def test_production_defaults_to_no_bootstrap(self):
|
|
"""Production defaults to explicit non-bootstrap startup behavior."""
|
|
from transcription.config import Settings
|
|
|
|
settings = Settings(openrouter_api_key="test-key", environment="production")
|
|
assert settings.should_bootstrap_schema is False
|
|
|
|
def test_development_defaults_to_bootstrap(self):
|
|
"""Development defaults to schema bootstrap for local workflows."""
|
|
from transcription.config import Settings
|
|
|
|
settings = Settings(openrouter_api_key="test-key", environment="development")
|
|
assert settings.should_bootstrap_schema is True
|
|
|
|
def test_explicit_override_wins(self):
|
|
"""Explicit bootstrap_schema_on_startup overrides environment default."""
|
|
from transcription.config import Settings
|
|
|
|
settings = Settings(
|
|
openrouter_api_key="test-key",
|
|
environment="production",
|
|
bootstrap_schema_on_startup=True,
|
|
)
|
|
assert settings.should_bootstrap_schema is True
|