|
| 1 | +import pytest |
| 2 | + |
| 3 | +from dbt.tests.util import run_dbt |
| 4 | + |
| 5 | + |
| 6 | +class BaseTransactionsEnabled: |
| 7 | + @pytest.fixture(scope="class") |
| 8 | + def project_config_update(self): |
| 9 | + return {"flags": {"dbt_sqlserver_use_dbt_transactions": True}} |
| 10 | + |
| 11 | + |
| 12 | +class TestTableMaterializationTransactionsOn(BaseTransactionsEnabled): |
| 13 | + @pytest.fixture(scope="class") |
| 14 | + def models(self): |
| 15 | + return { |
| 16 | + "table_model.sql": """ |
| 17 | +{{ config(materialized='table') }} |
| 18 | +select 1 as id, 'hello' as name |
| 19 | +""", |
| 20 | + } |
| 21 | + |
| 22 | + def test_table_materialization(self, project): |
| 23 | + results = run_dbt(["run"]) |
| 24 | + assert len(results) == 1 |
| 25 | + |
| 26 | + rows = project.run_sql("select id, name from {schema}.table_model", fetch="all") |
| 27 | + assert len(rows) == 1 |
| 28 | + assert rows[0][0] == 1 |
| 29 | + assert rows[0][1] == "hello" |
| 30 | + |
| 31 | + |
| 32 | +class TestViewMaterializationTransactionsOn(BaseTransactionsEnabled): |
| 33 | + @pytest.fixture(scope="class") |
| 34 | + def models(self): |
| 35 | + return { |
| 36 | + "view_model.sql": """ |
| 37 | +{{ config(materialized='view') }} |
| 38 | +select 42 as answer |
| 39 | +""", |
| 40 | + } |
| 41 | + |
| 42 | + def test_view_materialization(self, project): |
| 43 | + results = run_dbt(["run"]) |
| 44 | + assert len(results) == 1 |
| 45 | + |
| 46 | + rows = project.run_sql("select answer from {schema}.view_model", fetch="all") |
| 47 | + assert len(rows) == 1 |
| 48 | + assert rows[0][0] == 42 |
| 49 | + |
| 50 | + |
| 51 | +class TestIncrementalMaterializationTransactionsOn(BaseTransactionsEnabled): |
| 52 | + @pytest.fixture(scope="class") |
| 53 | + def models(self): |
| 54 | + return { |
| 55 | + "incremental_model.sql": """ |
| 56 | +{{ config(materialized='incremental', unique_key='id') }} |
| 57 | +select 1 as id, 'first' as value |
| 58 | +{% if is_incremental() %} |
| 59 | +union all |
| 60 | +select 2 as id, 'second' as value |
| 61 | +{% endif %} |
| 62 | +""", |
| 63 | + } |
| 64 | + |
| 65 | + def test_incremental_materialization(self, project): |
| 66 | + results = run_dbt(["run"]) |
| 67 | + assert len(results) == 1 |
| 68 | + |
| 69 | + rows = project.run_sql( |
| 70 | + "select count(*) as cnt from {schema}.incremental_model", fetch="one" |
| 71 | + ) |
| 72 | + assert rows[0] == 1 |
| 73 | + |
| 74 | + results = run_dbt(["run"]) |
| 75 | + assert len(results) == 1 |
| 76 | + |
| 77 | + rows = project.run_sql( |
| 78 | + "select count(*) as cnt from {schema}.incremental_model", fetch="one" |
| 79 | + ) |
| 80 | + assert rows[0] == 2 |
| 81 | + |
| 82 | + |
| 83 | +class BaseFailingModelWithSideEffect: |
| 84 | + @pytest.fixture(scope="class") |
| 85 | + def models(self): |
| 86 | + return { |
| 87 | + "failing_model.sql": """ |
| 88 | +{{ config( |
| 89 | + materialized='table', |
| 90 | + pre_hook=[ |
| 91 | + "INSERT INTO {{ this.schema }}.audit_log " |
| 92 | + "(msg, created_at) VALUES ('from_model', getdate())" |
| 93 | + ] |
| 94 | +) }} |
| 95 | +select 1/0 as boom |
| 96 | +""", |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | +class TestRollbackWithoutFlag(BaseFailingModelWithSideEffect): |
| 101 | + @pytest.fixture(scope="class") |
| 102 | + def project_config_update(self): |
| 103 | + return {"flags": {"dbt_sqlserver_use_dbt_transactions": False}} |
| 104 | + |
| 105 | + @pytest.mark.xfail( |
| 106 | + strict=True, |
| 107 | + reason="Without transactions flag, DML in pre-hooks is auto-committed and not rolled back," |
| 108 | + " remove after migration to always use transactions.", |
| 109 | + ) |
| 110 | + def test_side_effect_rolled_back(self, project): |
| 111 | + project.run_sql("CREATE TABLE {schema}.audit_log (msg varchar(100), created_at datetime)") |
| 112 | + run_dbt(["run", "-m", "failing_model"], expect_pass=False) |
| 113 | + rows = project.run_sql("SELECT COUNT(*) FROM {schema}.audit_log", fetch="one") |
| 114 | + assert rows[0] == 0 |
| 115 | + |
| 116 | + |
| 117 | +class TestRollbackWithFlag(BaseFailingModelWithSideEffect): |
| 118 | + @pytest.fixture(scope="class") |
| 119 | + def project_config_update(self): |
| 120 | + return {"flags": {"dbt_sqlserver_use_dbt_transactions": True}} |
| 121 | + |
| 122 | + def test_side_effect_rolled_back(self, project): |
| 123 | + project.run_sql("CREATE TABLE {schema}.audit_log (msg varchar(100), created_at datetime)") |
| 124 | + run_dbt(["run", "-m", "failing_model"], expect_pass=False) |
| 125 | + rows = project.run_sql("SELECT COUNT(*) FROM {schema}.audit_log", fetch="one") |
| 126 | + assert rows[0] == 0 |
| 127 | + |
| 128 | + |
| 129 | +class TestAfterCommitModelHookTransactionsOn(BaseTransactionsEnabled): |
| 130 | + @pytest.fixture(scope="class") |
| 131 | + def project_config_update(self): |
| 132 | + return { |
| 133 | + "flags": {"dbt_sqlserver_use_dbt_transactions": True}, |
| 134 | + "models": { |
| 135 | + "test": { |
| 136 | + "post-hook": [ |
| 137 | + {"sql": "select 1", "transaction": False}, |
| 138 | + ], |
| 139 | + } |
| 140 | + }, |
| 141 | + } |
| 142 | + |
| 143 | + @pytest.fixture(scope="class") |
| 144 | + def models(self): |
| 145 | + return {"after_commit_hook_model.sql": "select 1 as id"} |
| 146 | + |
| 147 | + def test_after_commit_post_hook_does_not_double_commit(self, project): |
| 148 | + run_dbt() |
| 149 | + |
| 150 | + |
| 151 | +class TestFailedModelThenSuccessTransactionsOn(BaseTransactionsEnabled): |
| 152 | + @pytest.fixture(scope="class") |
| 153 | + def models(self): |
| 154 | + return { |
| 155 | + "good_model.sql": """ |
| 156 | +{{ config(materialized='table') }} |
| 157 | +select 1 as id |
| 158 | +""", |
| 159 | + "bad_model.sql": """ |
| 160 | +{{ config(materialized='table') }} |
| 161 | +select 1/0 as boom |
| 162 | +""", |
| 163 | + } |
| 164 | + |
| 165 | + def test_failed_then_successful_run(self, project): |
| 166 | + results = run_dbt(["run", "-m", "bad_model"], expect_pass=False) |
| 167 | + assert len(results) == 1 |
| 168 | + assert results[0].status == "error" |
| 169 | + |
| 170 | + results = run_dbt(["run", "-m", "good_model"]) |
| 171 | + assert len(results) == 1 |
| 172 | + assert results[0].status == "success" |
| 173 | + |
| 174 | + rows = project.run_sql("select id from {schema}.good_model", fetch="all") |
| 175 | + assert len(rows) == 1 |
| 176 | + assert rows[0][0] == 1 |
| 177 | + |
| 178 | + |
| 179 | +_snapshot_seed_csv = """id,name,updated_at |
| 180 | +1,alice,2024-01-01 00:00:00 |
| 181 | +2,bob,2024-01-01 00:00:00 |
| 182 | +""" |
| 183 | + |
| 184 | +_snapshot_sql = """ |
| 185 | +{% snapshot snap %} |
| 186 | +{{ config( |
| 187 | + target_schema=schema, |
| 188 | + unique_key='id', |
| 189 | + strategy='timestamp', |
| 190 | + updated_at='updated_at', |
| 191 | +) }} |
| 192 | +select * from {{ ref('snap_seed') }} |
| 193 | +{% endsnapshot %} |
| 194 | +""" |
| 195 | + |
| 196 | + |
| 197 | +class TestSnapshotTransactionsOn(BaseTransactionsEnabled): |
| 198 | + @pytest.fixture(scope="class") |
| 199 | + def seeds(self): |
| 200 | + return {"snap_seed.csv": _snapshot_seed_csv} |
| 201 | + |
| 202 | + @pytest.fixture(scope="class") |
| 203 | + def snapshots(self): |
| 204 | + return {"snap.sql": _snapshot_sql} |
| 205 | + |
| 206 | + @pytest.fixture(scope="class") |
| 207 | + def models(self): |
| 208 | + return {} |
| 209 | + |
| 210 | + def test_snapshot_create_and_merge(self, project): |
| 211 | + run_dbt(["seed"]) |
| 212 | + results = run_dbt(["snapshot"]) |
| 213 | + assert len(results) == 1 |
| 214 | + assert results[0].status == "success" |
| 215 | + |
| 216 | + rows = project.run_sql("select count(*) from {schema}.snap", fetch="one") |
| 217 | + assert rows[0] == 2 |
| 218 | + |
| 219 | + results = run_dbt(["snapshot"]) |
| 220 | + assert len(results) == 1 |
| 221 | + assert results[0].status == "success" |
0 commit comments