"""Tests for transcription.db runtime and schema bootstrap behavior.""" import pytest from sqlalchemy import inspect from transcription.config import Settings from transcription.db import create_all from transcription.db import dispose_database_runtime from transcription.db import get_session from transcription.db import initialize_database_runtime @pytest.mark.asyncio async def test_create_all_creates_expected_tables(tmp_path): settings = Settings( openrouter_api_key="test-key", database_url=f"sqlite:///{tmp_path / 'schema.db'}", environment="test", ) runtime = initialize_database_runtime(settings=settings) try: await create_all(engine=runtime.engine) async with runtime.engine.connect() as conn: table_names = set(await conn.run_sync(lambda c: inspect(c).get_table_names())) assert "document" in table_names assert "job" in table_names assert "source" in table_names assert "revision" in table_names finally: await dispose_database_runtime() @pytest.mark.asyncio async def test_get_session_yields_async_session(tmp_path): settings = Settings( openrouter_api_key="test-key", database_url=f"sqlite:///{tmp_path / 'session.db'}", environment="test", ) initialize_database_runtime(settings=settings) try: async with get_session(settings=settings) as session: assert session is not None finally: await dispose_database_runtime() def test_bootstrap_policy_production_defaults_false(): settings = Settings(openrouter_api_key="test-key", environment="production") assert settings.should_bootstrap_schema is False def test_bootstrap_policy_development_defaults_true(): settings = Settings(openrouter_api_key="test-key", environment="development") assert settings.should_bootstrap_schema is True def test_bootstrap_policy_explicit_override_true(): settings = Settings( openrouter_api_key="test-key", environment="production", bootstrap_schema_on_startup=True, ) assert settings.should_bootstrap_schema is True