generated from john/python-template
Update job detail page to track revisions and display document image next to transcription text.
This commit is contained in:
+30
-55
@@ -1,96 +1,71 @@
|
||||
"""Tests for transcription.db — schema bootstrap and session factory."""
|
||||
"""Tests for transcription.db — async schema bootstrap/runtime behavior."""
|
||||
|
||||
from sqlalchemy import inspect, text
|
||||
from sqlmodel import Session, SQLModel, create_engine
|
||||
from sqlmodel.pool import StaticPool
|
||||
|
||||
|
||||
def _in_memory_engine():
|
||||
"""Create a fresh in-memory SQLite engine for isolated db tests."""
|
||||
return create_engine(
|
||||
"sqlite://",
|
||||
connect_args={"check_same_thread": False},
|
||||
poolclass=StaticPool,
|
||||
)
|
||||
from sqlalchemy import inspect
|
||||
from sqlalchemy import text
|
||||
import pytest
|
||||
|
||||
|
||||
class TestSchemaBootstrap:
|
||||
"""Verify create_all produces the expected table set."""
|
||||
"""Verify async create_all produces the expected table set."""
|
||||
|
||||
def test_create_all_creates_expected_tables(self):
|
||||
"""After create_all(), document, job, and transcript tables exist."""
|
||||
engine = _in_memory_engine()
|
||||
# Ensure models are imported so metadata is populated
|
||||
from transcription.models import Document, Job, Transcript # noqa: F401
|
||||
@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
|
||||
|
||||
import transcription.db as db_module
|
||||
from transcription.db.operations import create_all
|
||||
from transcription.db.runtime import get_engine
|
||||
|
||||
db_module.create_all(engine=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()))
|
||||
|
||||
inspector = inspect(engine)
|
||||
table_names = set(inspector.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 get_session yields and cleans up sessions."""
|
||||
"""Verify async get_session yields a usable AsyncSession."""
|
||||
|
||||
def test_get_session_yields_session(self):
|
||||
"""get_session() yields a usable Session object."""
|
||||
engine = _in_memory_engine()
|
||||
SQLModel.metadata.create_all(engine)
|
||||
@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
|
||||
|
||||
import transcription.db as db_module
|
||||
|
||||
with db_module.get_session(engine=engine) as session:
|
||||
assert isinstance(session, Session)
|
||||
|
||||
def test_session_is_closed_after_generator_exit(self):
|
||||
"""After the context manager exits, the session is closed."""
|
||||
engine = _in_memory_engine()
|
||||
SQLModel.metadata.create_all(engine)
|
||||
|
||||
import transcription.db as db_module
|
||||
|
||||
with db_module.get_session(engine=engine) as session:
|
||||
# Session is usable inside the context
|
||||
session.execute(text("SELECT 1"))
|
||||
captured = session
|
||||
|
||||
# After exiting, the session's internal connection is released
|
||||
# (no active transaction bound to the session)
|
||||
assert captured._transaction is None
|
||||
async with get_session(settings=default_settings) as session:
|
||||
result = await session.exec(text("SELECT 1"))
|
||||
assert result.first()[0] == 1
|
||||
|
||||
|
||||
class TestBootstrapPolicy:
|
||||
"""Verify schema bootstrap policy defaults and overrides."""
|
||||
"""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
|
||||
from transcription.db import should_bootstrap_schema
|
||||
|
||||
settings = Settings(openrouter_api_key="test-key", environment="production")
|
||||
assert should_bootstrap_schema(settings) is False
|
||||
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
|
||||
from transcription.db import should_bootstrap_schema
|
||||
|
||||
settings = Settings(openrouter_api_key="test-key", environment="development")
|
||||
assert should_bootstrap_schema(settings) is True
|
||||
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
|
||||
from transcription.db import should_bootstrap_schema
|
||||
|
||||
settings = Settings(
|
||||
openrouter_api_key="test-key",
|
||||
environment="production",
|
||||
bootstrap_schema_on_startup=True,
|
||||
)
|
||||
assert should_bootstrap_schema(settings) is True
|
||||
assert settings.should_bootstrap_schema is True
|
||||
|
||||
Reference in New Issue
Block a user