from __future__ import annotations import importlib import unittest from unittest.mock import patch from alembic.migration import MigrationContext from alembic.operations import Operations from sqlalchemy import create_mock_engine from sqlalchemy.dialects import postgresql class ExternalSystemMigrationTests(unittest.TestCase): def test_0027_upgrade_compiles_as_postgresql_ddl(self): statements: list[str] = [] def capture(sql, *multiparams, **params): statements.append(str(sql.compile(dialect=postgresql.dialect()))) engine = create_mock_engine("postgresql+psycopg://", capture) connection = engine.connect() operations = Operations(MigrationContext.configure(connection)) migration = importlib.import_module( "db.migrations.versions.20260807_1000_0027_external_system_governance" ) with patch.object(migration, "op", operations): migration.upgrade() rendered = "\n".join(statements) self.assertIn("external_system_grants", rendered) self.assertIn("external_system_audits", rendered) self.assertIn("operation_policies", rendered) self.assertIn("operation_mode", rendered) if __name__ == "__main__": unittest.main()