|
| 1 | +""" |
| 2 | +Copyright (c) 2022, Oracle and/or its affiliates. |
| 3 | +Copyright (c) 2020, Vitor Avancini |
| 4 | +
|
| 5 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + you may not use this file except in compliance with the License. |
| 7 | + You may obtain a copy of the License at |
| 8 | +
|
| 9 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | + Unless required by applicable law or agreed to in writing, software |
| 12 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + See the License for the specific language governing permissions and |
| 15 | + limitations under the License. |
| 16 | +""" |
| 17 | + |
| 18 | +import pytest |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +from dbt.tests.util import run_dbt |
| 22 | + |
| 23 | +# seeds/my_seed.csv |
| 24 | +my_seed_csv = """ |
| 25 | +id,name,some_date |
| 26 | +1,Easton,1981-05-20T06:46:51 |
| 27 | +2,Lillian,1978-09-03T18:10:33 |
| 28 | +3,Jeremiah,1982-03-11T03:59:51 |
| 29 | +4,Nolan,1976-05-06T20:21:35 |
| 30 | +""".lstrip() |
| 31 | + |
| 32 | + |
| 33 | +cc_all_snapshot_sql = """ |
| 34 | +{% snapshot cc_all_snapshot %} |
| 35 | + {{ config( |
| 36 | + check_cols='all', |
| 37 | + unique_key='id', |
| 38 | + strategy='check', |
| 39 | + target_database=database, |
| 40 | + target_schema=schema, |
| 41 | + invalidate_hard_deletes=True |
| 42 | + ) }} |
| 43 | + SELECT * FROM {{ ref('seed') }} |
| 44 | +{% endsnapshot %} |
| 45 | +""".strip() |
| 46 | + |
| 47 | +# seeds/insert.sql |
| 48 | +seeds__insert_sql = """ |
| 49 | +INSERT ALL |
| 50 | + INTO {schema}.seed (id, name, some_date) VALUES |
| 51 | + (5, 'John Doe', TO_DATE('1982-02-03', 'YYYY-MM-DD')) |
| 52 | +SELECT * FROM dual |
| 53 | +""" |
| 54 | + |
| 55 | +# seeds/update.sql |
| 56 | +seeds__update_sql = """ |
| 57 | +UPDATE {schema}.seed |
| 58 | + SET name = 'Lord Easton' |
| 59 | + WHERE id = 1 |
| 60 | +""" |
| 61 | + |
| 62 | +# seeds/delete.sql |
| 63 | +seeds__delete_sql = """ |
| 64 | +DELETE FROM {schema}.seed WHERE id = 2 |
| 65 | +""" |
| 66 | + |
| 67 | + |
| 68 | +class TestSnapshotCheckInvalidateHardDeletes: |
| 69 | + |
| 70 | + @pytest.fixture(scope="class") |
| 71 | + def seeds(self): |
| 72 | + return { |
| 73 | + "seed.csv": my_seed_csv, |
| 74 | + "insert.sql": seeds__insert_sql, |
| 75 | + "update.sql": seeds__update_sql, |
| 76 | + "delete.sql": seeds__delete_sql |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + @pytest.fixture(scope="class") |
| 81 | + def snapshots(self): |
| 82 | + return { |
| 83 | + "cc_all_snapshot.sql": cc_all_snapshot_sql, |
| 84 | + } |
| 85 | + |
| 86 | + def test_run_dbt(self, project): |
| 87 | + """dbt seed |
| 88 | + dbt snapshot |
| 89 | + Perform insert/update/delete |
| 90 | + dbt snapshot |
| 91 | +
|
| 92 | + MERGE INTO dbt_test.cc_all_snapshot d |
| 93 | + USING o$pt_cc_all_snapshot182811 s |
| 94 | + ON (s.dbt_scd_id = d.dbt_scd_id) |
| 95 | + WHEN MATCHED |
| 96 | + THEN UPDATE |
| 97 | + SET dbt_valid_to = s.dbt_valid_to |
| 98 | + WHERE d.dbt_valid_to IS NULL |
| 99 | + AND s.dbt_change_type IN ('update', 'delete') |
| 100 | + WHEN NOT MATCHED |
| 101 | + THEN INSERT (d.id, d.name, d.some_date, d.dbt_updated_at, d.dbt_valid_from, d.dbt_valid_to, d.dbt_scd_id) |
| 102 | + VALUES (s.id, s.name, s.some_date, s.dbt_updated_at, s.dbt_valid_from, s.dbt_valid_to, s.dbt_scd_id) |
| 103 | + WHERE s.dbt_change_type = 'insert' |
| 104 | +
|
| 105 | + """ |
| 106 | + results = run_dbt(['seed']) |
| 107 | + assert len(results) == 1 |
| 108 | + |
| 109 | + # snapshot command |
| 110 | + results = run_dbt(["snapshot"]) |
| 111 | + for result in results: |
| 112 | + assert result.status == "success" |
| 113 | + |
| 114 | + project.run_sql_file(Path("seeds") / Path("insert.sql")) |
| 115 | + project.run_sql_file(Path("seeds") / Path("update.sql")) |
| 116 | + project.run_sql_file(Path("seeds") / Path("delete.sql")) |
| 117 | + |
| 118 | + # run snapshot command |
| 119 | + results = run_dbt(["snapshot"]) |
| 120 | + for result in results: |
| 121 | + assert result.status == "success" |
| 122 | + |
| 123 | + snapshot_of_updated_rows = project.run_sql(f"select * from cc_all_snapshot where id=1", fetch="all") |
| 124 | + assert len(snapshot_of_updated_rows) == 2 |
| 125 | + |
| 126 | + # Deleted record will be invalidated. r['dbt_valid_to'] is set to current timestamp. |
| 127 | + snapshot_of_deleted_rows = project.run_sql(f"select * from cc_all_snapshot where id=2", fetch="all") |
| 128 | + assert len(snapshot_of_deleted_rows) == 1 |
| 129 | + |
| 130 | + |
| 131 | + |
0 commit comments