|
| 1 | +import pytest |
| 2 | +from dbt.tests.util import run_dbt, run_dbt_and_capture |
| 3 | +import dbt.exceptions |
| 4 | + |
| 5 | +incremental_change_history_enabled_model = """ |
| 6 | +{{ |
| 7 | + config( |
| 8 | + materialized='incremental', |
| 9 | + enable_change_history=True |
| 10 | + ) |
| 11 | +}} |
| 12 | +select 1 as id |
| 13 | +""" |
| 14 | + |
| 15 | +incremental_change_history_disabled_model = """ |
| 16 | +{{ |
| 17 | + config( |
| 18 | + materialized='incremental', |
| 19 | + enable_change_history=False |
| 20 | + ) |
| 21 | +}} |
| 22 | +select 1 as id |
| 23 | +""" |
| 24 | + |
| 25 | +table_change_history_enabled_model = """ |
| 26 | +{{ |
| 27 | + config( |
| 28 | + materialized='table', |
| 29 | + enable_change_history=True |
| 30 | + ) |
| 31 | +}} |
| 32 | +select 1 as id |
| 33 | +""" |
| 34 | + |
| 35 | +view_change_history_enabled_model = """ |
| 36 | +{{ |
| 37 | + config( |
| 38 | + materialized='view', |
| 39 | + enable_change_history=True |
| 40 | + ) |
| 41 | +}} |
| 42 | +select 1 as id |
| 43 | +""" |
| 44 | + |
| 45 | + |
| 46 | +class TestChangeHistorySuccessful: |
| 47 | + @pytest.fixture(scope="class") |
| 48 | + def models(self): |
| 49 | + return { |
| 50 | + "incremental_change_history_enabled.sql": incremental_change_history_enabled_model, |
| 51 | + "incremental_change_history_disabled.sql": incremental_change_history_disabled_model, |
| 52 | + "table_change_history_enabled.sql": table_change_history_enabled_model, |
| 53 | + } |
| 54 | + |
| 55 | + def get_is_change_history_enabled(self, project, table_name): |
| 56 | + sql = f""" |
| 57 | + select is_change_history_enabled |
| 58 | + from `{project.database}.{project.test_schema}.INFORMATION_SCHEMA.TABLES` |
| 59 | + where table_name = '{table_name}' |
| 60 | + """ |
| 61 | + results = project.run_sql(sql, fetch="one") |
| 62 | + return results[0] if results else None |
| 63 | + |
| 64 | + def test_change_history_options(self, project): |
| 65 | + results = run_dbt(["run"]) |
| 66 | + assert len(results) == 3 |
| 67 | + |
| 68 | + assert ( |
| 69 | + self.get_is_change_history_enabled(project, "incremental_change_history_enabled") |
| 70 | + == "YES" |
| 71 | + ) |
| 72 | + assert ( |
| 73 | + self.get_is_change_history_enabled(project, "incremental_change_history_disabled") |
| 74 | + == "NO" |
| 75 | + ) |
| 76 | + assert self.get_is_change_history_enabled(project, "table_change_history_enabled") == "YES" |
| 77 | + |
| 78 | + |
| 79 | +class TestChangeHistoryFail: |
| 80 | + @pytest.fixture(scope="class") |
| 81 | + def models(self): |
| 82 | + return { |
| 83 | + "view_with_flag.sql": view_change_history_enabled_model, |
| 84 | + } |
| 85 | + |
| 86 | + def test_view_with_flag_fails(self, project): |
| 87 | + _, stdout = run_dbt_and_capture(["run", "--select", "view_with_flag"], expect_pass=False) |
| 88 | + assert "`enable_change_history` is not supported for views" in stdout |
0 commit comments