-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathtest_simple_dependency.py
More file actions
338 lines (265 loc) · 10 KB
/
test_simple_dependency.py
File metadata and controls
338 lines (265 loc) · 10 KB
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
329
330
331
332
333
334
335
336
337
338
import os
import pytest
import tempfile
from pathlib import Path
from dbt.tests.util import (
check_relations_equal,
run_dbt,
)
models__disabled_one = """
{{config(enabled=False)}}
select 1
"""
models__disabled_two = """
{{config(enabled=False)}}
select * from {{ref('disabled_one')}}
"""
models__empty = """
"""
models__view_summary = """
{{
config(
materialized='view'
)
}}
with t as (
select * from {{ ref('view_model') }}
)
select date_trunc('year', updated_at) as year,
count(*)
from t
group by 1
"""
class SimpleDependencyBase(object):
@pytest.fixture(scope="class", autouse=True)
def setUp(self, project):
project.run_sql_file(project.test_data_dir / Path("seed.sql"))
@pytest.fixture(scope="class")
def models(self):
return {
"empty.sql": models__empty,
"view_summary.sql": models__view_summary,
"view_summary.sql": models__view_summary,
}
@pytest.fixture(scope="class")
def packages(self):
return {
"packages": [
{
"git": "https://github.com/dbt-labs/dbt-integration-project",
"revision": "1.1",
}
]
}
# These two functions included to enable override in ...NoProfile derived test class
@pytest.fixture(scope="class")
def run_deps(self, project):
return run_dbt(["deps"])
@pytest.fixture(scope="function")
def run_clean(self, project):
yield
# clear test schema
assert os.path.exists("target")
run_dbt(["clean"])
assert not os.path.exists("target")
class TestSimpleDependency(SimpleDependencyBase):
def test_simple_dependency(self, run_deps, project, run_clean):
"""dependencies should draw from a changing base table"""
results = run_dbt()
assert len(results) == 4
check_relations_equal(project.adapter, ["seed", "table_model"])
check_relations_equal(project.adapter, ["seed", "view_model"])
check_relations_equal(project.adapter, ["seed", "incremental"])
check_relations_equal(project.adapter, ["seed_summary", "view_summary"])
project.run_sql_file(project.test_data_dir / Path("update.sql"))
results = run_dbt()
assert len(results) == 4
check_relations_equal(project.adapter, ["seed", "table_model"])
check_relations_equal(project.adapter, ["seed", "view_model"])
check_relations_equal(project.adapter, ["seed", "incremental"])
class TestSimpleDependencyWithDependenciesFile(SimpleDependencyBase):
@pytest.fixture(scope="class")
def packages(self):
return {}
@pytest.fixture(scope="class")
def dependencies(self):
return {
"packages": [
{
"git": "https://github.com/dbt-labs/dbt-integration-project",
"warn-unpinned": True,
}
]
}
def test_dependency_with_dependencies_file(self, run_deps, project):
# Tests that "packages" defined in a dependencies.yml file works
results = run_dbt()
assert len(results) == 4
class TestSimpleDependencyWithEmptyPackagesFile(SimpleDependencyBase):
@pytest.fixture(scope="class")
def packages(self):
return " "
def test_dependency_with_empty_packages_file(self, run_deps, project):
# Tests that an empty packages file doesn't fail with a Python error
run_dbt(["deps"])
class TestSimpleDependencyNoProfile(SimpleDependencyBase):
"""dbt deps and clean commands should not require a profile."""
@pytest.fixture(scope="class")
def run_deps(self, project):
with tempfile.TemporaryDirectory() as tmpdir:
result = run_dbt(["deps", "--profiles-dir", tmpdir])
return result
@pytest.fixture(scope="class")
def run_clean(self, project):
with tempfile.TemporaryDirectory() as tmpdir:
result = run_dbt(["clean", "--profiles-dir", tmpdir])
return result
def test_simple_dependency_no_profile(self, project, run_deps, run_clean):
"""only need fixtures as opposed to any model assertions since those are
irrelevant and won't occur within the same runtime as a dbt run -s ..."""
pass
class TestSimpleDependencyWithModels(SimpleDependencyBase):
def test_simple_dependency_with_models(self, run_deps, project, run_clean):
results = run_dbt(["run", "--models", "view_model+"])
len(results) == 2
check_relations_equal(project.adapter, ["seed", "view_model"])
check_relations_equal(project.adapter, ["seed_summary", "view_summary"])
created_models = project.get_tables_in_schema()
assert "table_model" not in created_models
assert "incremental" not in created_models
assert created_models["view_model"] == "view"
assert created_models["view_summary"] == "view"
class TestSimpleDependencyUnpinned(object):
@pytest.fixture(scope="class", autouse=True)
def setUp(self, project):
project.run_sql_file(project.test_data_dir / Path("seed.sql"))
@pytest.fixture(scope="class")
def packages(self):
return {
"packages": [
{
"git": "https://github.com/dbt-labs/dbt-integration-project",
"warn-unpinned": True,
}
]
}
def test_simple_dependency(self, project):
run_dbt(["deps"])
class TestSimpleDependencyWithDuplicates(object):
# dbt should convert these into a single dependency internally
@pytest.fixture(scope="class")
def packages(self):
return {
"packages": [
{
"git": "https://github.com/dbt-labs/dbt-integration-project",
"revision": "dbt/1.0.0",
},
{
"git": "https://github.com/dbt-labs/dbt-integration-project.git",
"revision": "dbt/1.0.0",
},
]
}
def test_simple_dependency_deps(self, project):
run_dbt(["deps"])
class TestRekeyedDependencyWithSubduplicates(object):
# this revision of dbt-integration-project requires dbt-utils.git@0.5.0, which the
# package config handling should detect
@pytest.fixture(scope="class")
def packages(self):
return {
"packages": [
{
"git": "https://github.com/dbt-labs/dbt-integration-project",
"revision": "config-1.0.0-deps",
},
{
"git": "https://github.com/dbt-labs/dbt-utils",
"revision": "0.5.0",
},
]
}
def test_simple_dependency_deps(self, project):
run_dbt(["deps"])
assert len(os.listdir("dbt_packages")) == 2
class DependencyBranchBase(object):
@pytest.fixture(scope="class", autouse=True)
def setUp(self, project):
project.run_sql_file(project.test_data_dir / Path("seed.sql"))
@pytest.fixture(scope="class")
def packages(self):
return {
"packages": [
{
"git": "https://github.com/dbt-labs/dbt-integration-project",
"revision": "dbt/1.0.0",
},
]
}
def deps_run_assert_equality(self, project):
run_dbt(["deps"])
results = run_dbt()
assert len(results) == 4
check_relations_equal(project.adapter, ["seed", "table_model"])
check_relations_equal(project.adapter, ["seed", "view_model"])
check_relations_equal(project.adapter, ["seed", "incremental"])
created_models = project.get_tables_in_schema()
assert created_models["table_model"] == "table"
assert created_models["view_model"] == "view"
assert created_models["view_summary"] == "view"
assert created_models["incremental"] == "table"
class TestSimpleDependencyBranch(DependencyBranchBase):
@pytest.fixture(scope="class")
def models(self):
return {
"view_summary.sql": models__view_summary,
}
def test_simple_dependency(self, project):
self.deps_run_assert_equality(project)
check_relations_equal(project.adapter, ["seed_summary", "view_summary"])
project.run_sql_file(project.test_data_dir / Path("update.sql"))
self.deps_run_assert_equality(project)
class TestSimpleDependencyBranchWithEmpty(DependencyBranchBase):
@pytest.fixture(scope="class")
def models(self):
"""extra models included"""
return {
"disabled_one.sql": models__disabled_one,
"disabled_two.sql": models__disabled_two,
"view_summary.sql": models__view_summary,
"empty.sql": models__empty,
}
def test_empty_models_not_compiled_in_dependencies(self, project):
self.deps_run_assert_equality(project)
models = project.get_tables_in_schema()
assert "empty" not in models.keys()
class TestSimpleDependencyBadProfile(object):
@pytest.fixture(scope="class")
def project_config_update(self):
return {
"config-version": 2,
"models": {
"+any_config": "{{ target.name }}",
"+enabled": "{{ target.name in ['redshift', 'postgres'] | as_bool }}",
},
}
# Write out the profile data as a yaml file
@pytest.fixture(scope="class", autouse=True)
def dbt_profile_target(self):
# Need to set the environment variable here initially because
# the unittest setup does a load_config.
os.environ["PROFILE_TEST_HOST"] = "localhost"
return {
"type": "postgres",
"threads": 4,
"host": "{{ env_var('PROFILE_TEST_HOST') }}",
"port": 5432,
"user": "root",
"pass": "password",
"dbname": "dbt",
}
def test_deps_bad_profile(self, project):
del os.environ["PROFILE_TEST_HOST"]
run_dbt(["deps"])
run_dbt(["clean"])