def test_ensure_schema_exists_creates_missing_schema( monkeypatch: MonkeyPatch, caplog: LogCaptureFixture) -> None: new_schema = "new_schema" mock_get_schema_names = Mock(return_value=["old_schema"]) internal_database = Database(DatabaseType.internal) internal_database._database_schema = new_schema with internal_database.transaction_context() as session: original_connection = session.connection() def mock_execute_create_schema( connection: Connection, statement: Executable) -> Optional[Connection]: if isinstance(statement, CreateSchema): assert statement.element == new_schema return None return cast(Connection, original_connection(connection, statement)) monkeypatch.setattr(MSDialect_pyodbc, "get_schema_names", mock_get_schema_names) monkeypatch.setattr(Connection, "execute", mock_execute_create_schema) with caplog.at_level(logging.INFO): ensure_schema_exists(internal_database) assert f"Creating schema: {new_schema} in internal database" in caplog.messages mock_get_schema_names.assert_called_once()
def test_ensure_schema_exists(monkeypatch: MonkeyPatch, caplog: LogCaptureFixture) -> None: existing_schema = "existing_schema" mock_get_schema_names = Mock(return_value=[existing_schema]) monkeypatch.setattr(MSDialect_pyodbc, "get_schema_names", mock_get_schema_names) internal_database = Database(DatabaseType.internal) internal_database._database_schema = existing_schema with caplog.at_level(logging.INFO): ensure_schema_exists(internal_database) assert f"Found schema: {existing_schema} in internal database" in caplog.messages mock_get_schema_names.assert_called_once()