|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import typing as t |
| 5 | +from unittest.mock import call |
| 6 | + |
| 7 | +import pytest |
| 8 | +from sqlglot import exp |
| 9 | + |
| 10 | +from sqlmesh.core.engine_adapter import EngineAdapter |
| 11 | +from sqlmesh.core.engine_adapter.shared import CatalogSupport, set_catalog |
| 12 | + |
| 13 | +if t.TYPE_CHECKING: |
| 14 | + from sqlmesh.core._typing import SchemaName, TableName |
| 15 | + |
| 16 | +pytestmark = pytest.mark.engine |
| 17 | + |
| 18 | + |
| 19 | +@set_catalog() |
| 20 | +class CatalogAdapter(EngineAdapter): |
| 21 | + @property |
| 22 | + def catalog_support(self) -> CatalogSupport: |
| 23 | + return CatalogSupport.REQUIRES_SET_CATALOG |
| 24 | + |
| 25 | + def table_operation(self, table_name: TableName, callback: t.Callable) -> t.Any: |
| 26 | + return callback(table_name) |
| 27 | + |
| 28 | + def schema_operation(self, schema_name: SchemaName, callback: t.Callable) -> t.Any: |
| 29 | + return callback(schema_name) |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.parametrize("is_schema", [True, False]) |
| 33 | +@pytest.mark.parametrize("as_keyword", [True, False]) |
| 34 | +@pytest.mark.parametrize("fails", [True, False]) |
| 35 | +def test_set_catalog_operation(make_mocked_engine_adapter, mocker, is_schema, as_keyword, fails): |
| 36 | + adapter = make_mocked_engine_adapter(CatalogAdapter) |
| 37 | + mocker.patch.object(adapter, "get_current_catalog", return_value="original") |
| 38 | + switch = mocker.patch.object(adapter, "set_current_catalog") |
| 39 | + target = ( |
| 40 | + exp.table_("", db="schema", catalog="temporary") |
| 41 | + if is_schema |
| 42 | + else exp.to_table("temporary.schema.table") |
| 43 | + ) |
| 44 | + original_target = target.copy() |
| 45 | + result = object() |
| 46 | + error = RuntimeError("operation failed") |
| 47 | + callback = mocker.Mock(side_effect=error if fails else None, return_value=result) |
| 48 | + operation = adapter.schema_operation if is_schema else adapter.table_operation |
| 49 | + arguments = {"schema_name" if is_schema else "table_name": target, "callback": callback} |
| 50 | + |
| 51 | + def run(): |
| 52 | + return operation(**arguments) if as_keyword else operation(target, callback) |
| 53 | + |
| 54 | + if fails: |
| 55 | + with pytest.raises(RuntimeError) as exc_info: |
| 56 | + run() |
| 57 | + assert exc_info.value is error |
| 58 | + else: |
| 59 | + assert run() is result |
| 60 | + |
| 61 | + assert target == original_target |
| 62 | + stripped_target = original_target.copy() |
| 63 | + stripped_target.set("catalog", None) |
| 64 | + callback.assert_called_once_with(stripped_target) |
| 65 | + assert switch.call_args_list == [call("temporary"), call("original")] |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize("target", ["original.schema.table", "schema.table"]) |
| 69 | +def test_set_catalog_skips_unneeded_switch(make_mocked_engine_adapter, mocker, target): |
| 70 | + adapter = make_mocked_engine_adapter(CatalogAdapter) |
| 71 | + get_catalog = mocker.patch.object(adapter, "get_current_catalog", return_value="original") |
| 72 | + switch = mocker.patch.object(adapter, "set_current_catalog") |
| 73 | + error = RuntimeError("operation failed") |
| 74 | + callback = mocker.Mock(side_effect=error) |
| 75 | + |
| 76 | + with pytest.raises(RuntimeError) as exc_info: |
| 77 | + adapter.table_operation(target, callback) |
| 78 | + |
| 79 | + assert exc_info.value is error |
| 80 | + switch.assert_not_called() |
| 81 | + assert get_catalog.call_count == (1 if target.startswith("original.") else 0) |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.parametrize("fails", [True, False]) |
| 85 | +def test_set_catalog_nested_operations(make_mocked_engine_adapter, mocker, fails): |
| 86 | + adapter = make_mocked_engine_adapter(CatalogAdapter) |
| 87 | + current_catalog = "original" |
| 88 | + |
| 89 | + def set_current_catalog(catalog): |
| 90 | + nonlocal current_catalog |
| 91 | + current_catalog = catalog |
| 92 | + |
| 93 | + mocker.patch.object(adapter, "get_current_catalog", side_effect=lambda: current_catalog) |
| 94 | + switch = mocker.patch.object(adapter, "set_current_catalog", side_effect=set_current_catalog) |
| 95 | + result = object() |
| 96 | + error = RuntimeError("inner operation failed") |
| 97 | + |
| 98 | + def inner(table): |
| 99 | + assert current_catalog == "inner" |
| 100 | + if fails: |
| 101 | + raise error |
| 102 | + return result |
| 103 | + |
| 104 | + def outer(table): |
| 105 | + assert current_catalog == "outer" |
| 106 | + try: |
| 107 | + return adapter.table_operation("inner.schema.table", inner) |
| 108 | + finally: |
| 109 | + assert current_catalog == "outer" |
| 110 | + |
| 111 | + if fails: |
| 112 | + with pytest.raises(RuntimeError) as exc_info: |
| 113 | + adapter.table_operation("outer.schema.table", outer) |
| 114 | + assert exc_info.value is error |
| 115 | + else: |
| 116 | + assert adapter.table_operation("outer.schema.table", outer) is result |
| 117 | + |
| 118 | + assert current_catalog == "original" |
| 119 | + assert switch.call_args_list == [ |
| 120 | + call("outer"), |
| 121 | + call("inner"), |
| 122 | + call("outer"), |
| 123 | + call("original"), |
| 124 | + ] |
| 125 | + |
| 126 | + |
| 127 | +@pytest.mark.parametrize("operation_fails", [True, False]) |
| 128 | +def test_set_catalog_restore_failure(make_mocked_engine_adapter, mocker, operation_fails): |
| 129 | + adapter = make_mocked_engine_adapter(CatalogAdapter) |
| 130 | + mocker.patch.object(adapter, "get_current_catalog", return_value="original") |
| 131 | + operation_error = RuntimeError("operation failed") |
| 132 | + restore_error = RuntimeError("restore failed") |
| 133 | + switch = mocker.patch.object(adapter, "set_current_catalog", side_effect=[None, restore_error]) |
| 134 | + callback = mocker.Mock(side_effect=operation_error if operation_fails else None) |
| 135 | + |
| 136 | + with pytest.raises(RuntimeError) as exc_info: |
| 137 | + adapter.table_operation("temporary.schema.table", callback) |
| 138 | + |
| 139 | + assert exc_info.value is restore_error |
| 140 | + assert exc_info.value.__context__ is (operation_error if operation_fails else None) |
| 141 | + assert switch.call_args_list == [call("temporary"), call("original")] |
0 commit comments