import pytest from sqlalchemy import inspect from sqlalchemy.ext.asyncio import AsyncEngine pytestmark = [ pytest.mark.integration, pytest.mark.postgres, pytest.mark.asyncio(loop_scope="session"), ] EXPECTED_TABLES = { "tenants", "tenant_domains", "api_keys", "source_files", "ingestion_jobs", "ingestion_job_events", "alembic_version", } async def test_migrations_create_schema_from_empty_database(postgres_engine: AsyncEngine) -> None: async with postgres_engine.connect() as connection: table_names = await connection.run_sync( lambda sync_conn: inspect(sync_conn).get_table_names() ) assert EXPECTED_TABLES.issubset(set(table_names)) async def test_tenant_domains_enforces_one_row_per_tenant_and_key( postgres_engine: AsyncEngine, ) -> None: """The unique constraint is what stops the same domain being registered twice for a tenant while still letting two tenants share a key. """ async with postgres_engine.connect() as connection: constraints = await connection.run_sync( lambda sync_conn: inspect(sync_conn).get_unique_constraints("tenant_domains") ) assert any(constraint["column_names"] == ["tenant_id", "domain"] for constraint in constraints)