|
| 1 | +from unittest.mock import Mock |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from tests.unit.macros.base import MacroTestBase |
| 6 | + |
| 7 | + |
| 8 | +class TestInsertOverwriteMacros(MacroTestBase): |
| 9 | + @pytest.fixture(scope="class") |
| 10 | + def template_name(self) -> str: |
| 11 | + return "strategies.sql" |
| 12 | + |
| 13 | + @pytest.fixture(scope="class") |
| 14 | + def macro_folders_to_load(self) -> list: |
| 15 | + return ["macros/materializations/incremental"] |
| 16 | + |
| 17 | + @pytest.fixture(scope="class") |
| 18 | + def spark_template_names(self) -> list: |
| 19 | + # Need spark templates for partition_cols macro |
| 20 | + return ["adapters.sql"] |
| 21 | + |
| 22 | + @pytest.fixture(autouse=True) |
| 23 | + def setup_mock_columns(self, context): |
| 24 | + """Mock the adapter methods needed for the macro""" |
| 25 | + # Mock get_columns_in_relation to return some test columns |
| 26 | + mock_column_a = Mock() |
| 27 | + mock_column_a.quoted = "a" |
| 28 | + mock_column_b = Mock() |
| 29 | + mock_column_b.quoted = "b" |
| 30 | + |
| 31 | + context["adapter"].get_columns_in_relation.return_value = [mock_column_a, mock_column_b] |
| 32 | + |
| 33 | + def test_get_insert_overwrite_sql__legacy_dbr_version(self, template, context, config): |
| 34 | + """Test that DBR < 16.3 uses traditional INSERT OVERWRITE syntax""" |
| 35 | + # Negative return value means DBR < 16.3 |
| 36 | + context["adapter"].compare_dbr_version.return_value = -1 |
| 37 | + config["partition_by"] = ["partition_col"] |
| 38 | + |
| 39 | + source_relation = Mock() |
| 40 | + source_relation.__str__ = lambda self: "source_table" |
| 41 | + target_relation = Mock() |
| 42 | + target_relation.__str__ = lambda self: "target_table" |
| 43 | + |
| 44 | + result = self.run_macro_raw( |
| 45 | + template, |
| 46 | + "get_insert_overwrite_sql", |
| 47 | + source_relation, |
| 48 | + target_relation, |
| 49 | + ) |
| 50 | + |
| 51 | + # Verify it uses legacy INSERT OVERWRITE syntax |
| 52 | + expected_sql = """insert overwrite table target_table |
| 53 | + partition (partition_col) |
| 54 | + select a, b from source_table""" |
| 55 | + |
| 56 | + self.assert_sql_equal(result, expected_sql) |
| 57 | + |
| 58 | + def test_get_insert_overwrite_sql__modern_dbr_version(self, template, context, config): |
| 59 | + """Test that DBR >= 16.3 uses REPLACE USING syntax""" |
| 60 | + # Positive return value means DBR > 16.3 |
| 61 | + context["adapter"].compare_dbr_version.return_value = 1 |
| 62 | + config["partition_by"] = ["partition_col"] |
| 63 | + |
| 64 | + source_relation = Mock() |
| 65 | + source_relation.__str__ = lambda self: "source_table" |
| 66 | + target_relation = Mock() |
| 67 | + target_relation.__str__ = lambda self: "target_table" |
| 68 | + |
| 69 | + result = self.run_macro_raw( |
| 70 | + template, |
| 71 | + "get_insert_overwrite_sql", |
| 72 | + source_relation, |
| 73 | + target_relation, |
| 74 | + ) |
| 75 | + |
| 76 | + # Verify it uses REPLACE USING syntax |
| 77 | + expected_sql = """insert into table target_table |
| 78 | + replace using (partition_col) |
| 79 | + select a, b from source_table""" |
| 80 | + |
| 81 | + self.assert_sql_equal(result, expected_sql) |
| 82 | + |
| 83 | + @pytest.mark.parametrize("dbr_version_return", [-1, 0, 1]) |
| 84 | + def test_get_insert_overwrite_sql__no_partitions( |
| 85 | + self, template, context, config, dbr_version_return |
| 86 | + ): |
| 87 | + """Test that empty partition_by falls back to INSERT OVERWRITE regardless of DBR version""" |
| 88 | + context["adapter"].compare_dbr_version.return_value = dbr_version_return |
| 89 | + # No partition_by set in config |
| 90 | + |
| 91 | + source_relation = Mock() |
| 92 | + source_relation.__str__ = lambda self: "source_table" |
| 93 | + target_relation = Mock() |
| 94 | + target_relation.__str__ = lambda self: "target_table" |
| 95 | + |
| 96 | + # Run the macro |
| 97 | + result = self.run_macro_raw( |
| 98 | + template, |
| 99 | + "get_insert_overwrite_sql", |
| 100 | + source_relation, |
| 101 | + target_relation, |
| 102 | + ) |
| 103 | + |
| 104 | + # Verify it uses regular INSERT OVERWRITE syntax |
| 105 | + expected_sql = """insert overwrite table target_table select a, b from source_table""" |
| 106 | + |
| 107 | + self.assert_sql_equal(result, expected_sql) |
0 commit comments