-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathtest_basic_snapshot.py
328 lines (254 loc) · 9.6 KB
/
test_basic_snapshot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import os
from datetime import datetime
import pytz
import pytest
from dbt.tests.util import run_dbt, check_relations_equal, relation_from_name
from tests.functional.simple_snapshot.fixtures import (
models__schema_yml,
models__ref_snapshot_sql,
seeds__seed_newcol_csv,
seeds__seed_csv,
snapshots_pg__snapshot_sql,
macros__test_no_overlaps_sql,
macros_custom_snapshot__custom_sql,
snapshots_pg_custom_namespaced__snapshot_sql,
snapshots_pg_custom__snapshot_sql,
)
snapshots_check_col__snapshot_sql = """
{% snapshot snapshot_actual %}
{{
config(
target_database=var('target_database', database),
target_schema=schema,
unique_key='id || ' ~ "'-'" ~ ' || first_name',
strategy='check',
check_cols=['email'],
)
}}
select * from {{target.database}}.{{schema}}.seed
{% endsnapshot %}
{# This should be exactly the same #}
{% snapshot snapshot_checkall %}
{{
config(
target_database=var('target_database', database),
target_schema=schema,
unique_key='id || ' ~ "'-'" ~ ' || first_name',
strategy='check',
check_cols='all',
)
}}
select * from {{target.database}}.{{schema}}.seed
{% endsnapshot %}
"""
snapshots_check_col_noconfig__snapshot_sql = """
{% snapshot snapshot_actual %}
select * from {{target.database}}.{{schema}}.seed
{% endsnapshot %}
{# This should be exactly the same #}
{% snapshot snapshot_checkall %}
{{ config(check_cols='all') }}
select * from {{target.database}}.{{schema}}.seed
{% endsnapshot %}
"""
def snapshot_setup(project, num_snapshot_models=1):
path = os.path.join(project.test_data_dir, "seed_pg.sql")
project.run_sql_file(path)
results = run_dbt(["snapshot"])
assert len(results) == num_snapshot_models
run_dbt(["test"])
check_relations_equal(project.adapter, ["snapshot_actual", "snapshot_expected"])
path = os.path.join(project.test_data_dir, "invalidate_postgres.sql")
project.run_sql_file(path)
path = os.path.join(project.test_data_dir, "update.sql")
project.run_sql_file(path)
results = run_dbt(["snapshot"])
assert len(results) == num_snapshot_models
run_dbt(["test"])
check_relations_equal(project.adapter, ["snapshot_actual", "snapshot_expected"])
def ref_setup(project, num_snapshot_models=1):
path = os.path.join(project.test_data_dir, "seed_pg.sql")
project.run_sql_file(path)
results = run_dbt(["snapshot"])
assert len(results) == num_snapshot_models
results = run_dbt(["run"])
assert len(results) == 1
class Basic:
@pytest.fixture(scope="class")
def snapshots(self):
return {"snapshot.sql": snapshots_pg__snapshot_sql}
@pytest.fixture(scope="class")
def models(self):
return {
"schema.yml": models__schema_yml,
"ref_snapshot.sql": models__ref_snapshot_sql,
}
@pytest.fixture(scope="class")
def macros(self):
return {"test_no_overlaps.sql": macros__test_no_overlaps_sql}
@pytest.fixture(scope="class")
def seeds(self):
return {"seed_newcol.csv": seeds__seed_newcol_csv, "seed.csv": seeds__seed_csv}
class TestBasicSnapshot(Basic):
def test_basic_snapshot(self, project):
snapshot_setup(project, num_snapshot_models=1)
class TestBasicRef(Basic):
def test_basic_ref(self, project):
ref_setup(project, num_snapshot_models=1)
class CustomNamespace:
@pytest.fixture(scope="class")
def snapshots(self):
return {"snapshot.sql": snapshots_pg_custom_namespaced__snapshot_sql}
@pytest.fixture(scope="class")
def macros(self):
return {
"test_no_overlaps.sql": macros__test_no_overlaps_sql,
"custom.sql": macros_custom_snapshot__custom_sql,
}
@pytest.fixture(scope="class")
def models(self):
return {
"schema.yml": models__schema_yml,
"ref_snapshot.sql": models__ref_snapshot_sql,
}
@pytest.fixture(scope="class")
def seeds(self):
return {"seed_newcol.csv": seeds__seed_newcol_csv, "seed.csv": seeds__seed_csv}
class TestBasicCustomNamespace(CustomNamespace):
def test_custom_namespace_snapshot(self, project):
snapshot_setup(project, num_snapshot_models=1)
class TestRefCustomNamespace(CustomNamespace):
def test_custom_namespace_ref(self, project):
ref_setup(project, num_snapshot_models=1)
class CustomSnapshot:
@pytest.fixture(scope="class")
def snapshots(self):
return {"snapshot.sql": snapshots_pg_custom__snapshot_sql}
@pytest.fixture(scope="class")
def macros(self):
return {
"test_no_overlaps.sql": macros__test_no_overlaps_sql,
"custom.sql": macros_custom_snapshot__custom_sql,
}
@pytest.fixture(scope="class")
def models(self):
return {
"schema.yml": models__schema_yml,
"ref_snapshot.sql": models__ref_snapshot_sql,
}
@pytest.fixture(scope="class")
def seeds(self):
return {"seed_newcol.csv": seeds__seed_newcol_csv, "seed.csv": seeds__seed_csv}
class TestBasicCustomSnapshot(CustomSnapshot):
def test_custom_snapshot(self, project):
snapshot_setup(project, num_snapshot_models=1)
class TestRefCustomSnapshot(CustomSnapshot):
def test_custom_ref(self, project):
ref_setup(project, num_snapshot_models=1)
class CheckCols:
@pytest.fixture(scope="class")
def snapshots(self):
return {"snapshot.sql": snapshots_check_col__snapshot_sql}
@pytest.fixture(scope="class")
def models(self):
return {
"schema.yml": models__schema_yml,
"ref_snapshot.sql": models__ref_snapshot_sql,
}
@pytest.fixture(scope="class")
def macros(self):
return {"test_no_overlaps.sql": macros__test_no_overlaps_sql}
@pytest.fixture(scope="class")
def seeds(self):
return {"seed_newcol.csv": seeds__seed_newcol_csv, "seed.csv": seeds__seed_csv}
class TestBasicCheckCols(CheckCols):
def test_basic_snapshot(self, project):
snapshot_setup(project, num_snapshot_models=2)
class TestRefCheckCols(CheckCols):
def test_check_cols_ref(self, project):
ref_setup(project, num_snapshot_models=2)
class ConfiguredCheckCols:
@pytest.fixture(scope="class")
def snapshots(self):
return {"snapshot.sql": snapshots_check_col_noconfig__snapshot_sql}
@pytest.fixture(scope="class")
def models(self):
return {
"schema.yml": models__schema_yml,
"ref_snapshot.sql": models__ref_snapshot_sql,
}
@pytest.fixture(scope="class")
def macros(self):
return {"test_no_overlaps.sql": macros__test_no_overlaps_sql}
@pytest.fixture(scope="class")
def seeds(self):
return {"seed_newcol.csv": seeds__seed_newcol_csv, "seed.csv": seeds__seed_csv}
@pytest.fixture(scope="class")
def project_config_update(self):
snapshot_config = {
"snapshots": {
"test": {
"target_schema": "{{ target.schema }}",
"unique_key": "id || '-' || first_name",
"strategy": "check",
"check_cols": ["email"],
}
}
}
return snapshot_config
class TestBasicConfiguredCheckCols(ConfiguredCheckCols):
def test_configured_snapshot(self, project):
snapshot_setup(project, num_snapshot_models=2)
class TestRefConfiguredCheckCols(ConfiguredCheckCols):
def test_configured_ref(self, project):
ref_setup(project, num_snapshot_models=2)
class UpdatedAtCheckCols:
@pytest.fixture(scope="class")
def snapshots(self):
return {"snapshot.sql": snapshots_check_col_noconfig__snapshot_sql}
@pytest.fixture(scope="class")
def models(self):
return {
"schema.yml": models__schema_yml,
"ref_snapshot.sql": models__ref_snapshot_sql,
}
@pytest.fixture(scope="class")
def macros(self):
return {"test_no_overlaps.sql": macros__test_no_overlaps_sql}
@pytest.fixture(scope="class")
def seeds(self):
return {"seed_newcol.csv": seeds__seed_newcol_csv, "seed.csv": seeds__seed_csv}
@pytest.fixture(scope="class")
def project_config_update(self):
snapshot_config = {
"snapshots": {
"test": {
"target_schema": "{{ target.schema }}",
"unique_key": "id || '-' || first_name",
"strategy": "check",
"check_cols": "all",
"updated_at": "updated_at",
}
}
}
return snapshot_config
class TestBasicUpdatedAtCheckCols(UpdatedAtCheckCols):
def test_updated_at_snapshot(self, project):
snapshot_setup(project, num_snapshot_models=2)
snapshot_expected_relation = relation_from_name(project.adapter, "snapshot_expected")
revived_records = project.run_sql(
"""
select id, updated_at, dbt_valid_from from {}
""".format(
snapshot_expected_relation
),
fetch="all",
)
for result in revived_records:
# result is a tuple, the updated_at is second and dbt_valid_from is latest
assert isinstance(result[1], datetime)
assert isinstance(result[2], datetime)
assert result[1].replace(tzinfo=pytz.UTC) == result[2].replace(tzinfo=pytz.UTC)
class TestRefUpdatedAtCheckCols(UpdatedAtCheckCols):
def test_updated_at_ref(self, project):
ref_setup(project, num_snapshot_models=2)