-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
test_simple_seed.py
288 lines (228 loc) · 8.29 KB
/
test_simple_seed.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
import os
from test.integration.base import DBTIntegrationTest, use_profile
class TestSimpleSeed(DBTIntegrationTest):
def setUp(self):
DBTIntegrationTest.setUp(self)
self.run_sql_file("seed.sql")
@property
def schema(self):
return "simple_seed_005"
@property
def models(self):
return "models-downstream-seed"
@property
def project_config(self):
return {
'config-version': 2,
"data-paths": ['data'],
'seeds': {
'quote_columns': False,
}
}
def use_full_refresh_project(self, full_refresh: bool):
overrides = {
'seeds': {
'quote_columns': False,
'full_refresh': full_refresh,
}
}
self.use_default_project(overrides)
def _seed_and_run(self):
assert len(self.run_dbt(['seed'])) == 1
self.assertTablesEqual('seed_actual', 'seed_expected')
assert len(self.run_dbt(['run'])) == 1
self.assertTablesEqual('model', 'seed_expected')
def _after_seed_model_state(self, cmd, exists: bool):
assert len(self.run_dbt(cmd)) == 1
self.assertTablesEqual('seed_actual', 'seed_expected')
if exists:
self.assertTableDoesExist('model')
else:
self.assertTableDoesNotExist('model')
@use_profile('postgres')
def test_postgres_simple_seed(self):
self._seed_and_run()
# this should truncate the seed_actual table, then re-insert.
self._after_seed_model_state(['seed'], exists=True)
@use_profile('postgres')
def test_postgres_simple_seed_full_refresh_flag(self):
self._seed_and_run()
# this should drop the seed_actual table, then re-create it, so the
# model won't exist.
self._after_seed_model_state(['seed', '--full-refresh'], exists=False)
@use_profile('postgres')
def test_postgres_simple_seed_full_refresh_config(self):
self._seed_and_run()
# set the full_refresh config to False
self.use_full_refresh_project(False)
self._after_seed_model_state(['seed'], exists=True)
# make sure we ignore the full-refresh flag (the config is higher
# priority than the flag)
self._after_seed_model_state(['seed', '--full-refresh'], exists=True)
# this should drop the seed_actual table, then re-create it, so the
# model won't exist.
self.use_full_refresh_project(True)
self._after_seed_model_state(['seed'], exists=False)
class TestSimpleSeedCustomSchema(DBTIntegrationTest):
def setUp(self):
super().setUp()
self.run_sql_file("seed.sql")
self._created_schemas.add(
self._get_schema_fqn(self.default_database, self.custom_schema_name())
)
@property
def schema(self):
return "simple_seed_005"
@property
def models(self):
return "models"
@property
def project_config(self):
return {
'config-version': 2,
"data-paths": ['data'],
'seeds': {
"schema": "custom_schema",
'quote_columns': False,
},
}
def custom_schema_name(self):
return "{}_{}".format(self.unique_schema(), 'custom_schema')
@use_profile('postgres')
def test_postgres_simple_seed_with_schema(self):
schema_name = self.custom_schema_name()
results = self.run_dbt(["seed"])
self.assertEqual(len(results), 1)
self.assertTablesEqual("seed_actual","seed_expected", table_a_schema=schema_name)
# this should truncate the seed_actual table, then re-insert
results = self.run_dbt(["seed"])
self.assertEqual(len(results), 1)
self.assertTablesEqual("seed_actual","seed_expected", table_a_schema=schema_name)
@use_profile('postgres')
def test_postgres_simple_seed_with_drop_and_schema(self):
schema_name = "{}_{}".format(self.unique_schema(), 'custom_schema')
results = self.run_dbt(["seed"])
self.assertEqual(len(results), 1)
self.assertTablesEqual("seed_actual","seed_expected", table_a_schema=schema_name)
# this should drop the seed table, then re-create
results = self.run_dbt(["seed", "--full-refresh"])
self.assertEqual(len(results), 1)
self.assertTablesEqual("seed_actual","seed_expected", table_a_schema=schema_name)
class TestSimpleSeedDisabled(DBTIntegrationTest):
@property
def schema(self):
return "simple_seed_005"
@property
def models(self):
return "models"
@property
def project_config(self):
return {
'config-version': 2,
"data-paths": ['data-config'],
'seeds': {
"test": {
"seed_enabled": {
"enabled": True
},
"seed_disabled": {
"enabled": False
}
},
'quote_columns': False,
},
}
@use_profile('postgres')
def test_postgres_simple_seed_with_disabled(self):
results = self.run_dbt(["seed"])
self.assertEqual(len(results), 2)
self.assertTableDoesExist('seed_enabled')
self.assertTableDoesNotExist('seed_disabled')
@use_profile('postgres')
def test_postgres_simple_seed_selection(self):
results = self.run_dbt(['seed', '--select', 'seed_enabled'])
self.assertEqual(len(results), 1)
self.assertTableDoesExist('seed_enabled')
self.assertTableDoesNotExist('seed_disabled')
self.assertTableDoesNotExist('seed_tricky')
@use_profile('postgres')
def test_postgres_simple_seed_exclude(self):
results = self.run_dbt(['seed', '--exclude', 'seed_enabled'])
self.assertEqual(len(results), 1)
self.assertTableDoesNotExist('seed_enabled')
self.assertTableDoesNotExist('seed_disabled')
self.assertTableDoesExist('seed_tricky')
class TestSeedParsing(DBTIntegrationTest):
def setUp(self):
super().setUp()
self.run_sql_file("seed.sql")
@property
def schema(self):
return "simple_seed_005"
@property
def models(self):
return "models-exist"
@property
def project_config(self):
return {
'config-version': 2,
"data-paths": ['data-bad'],
'seeds': {
'quote_columns': False,
},
}
@use_profile('postgres')
def test_postgres_dbt_run_skips_seeds(self):
# run does not try to parse the seed files
self.assertEqual(len(self.run_dbt(['run'])), 1)
# make sure 'dbt seed' fails, otherwise our test is invalid!
self.run_dbt(['seed'], expect_pass=False)
class TestSimpleSeedWithBOM(DBTIntegrationTest):
def setUp(self):
DBTIntegrationTest.setUp(self)
self.run_sql_file("seed.sql")
@property
def schema(self):
return "simple_seed_005"
@property
def models(self):
return "models"
@property
def project_config(self):
return {
'config-version': 2,
"data-paths": ['data-bom'],
'seeds': {
'quote_columns': False,
},
}
@use_profile('postgres')
def test_postgres_simple_seed(self):
# first make sure nobody "fixed" the file by accident
seed_path = os.path.join(self.config.data_paths[0], 'seed_bom.csv')
# 'data-bom/seed_bom.csv'
with open(seed_path, encoding='utf-8') as fp:
self.assertEqual(fp.read(1), u'\ufeff')
results = self.run_dbt(["seed"])
self.assertEqual(len(results), 1)
self.assertTablesEqual("seed_bom", "seed_expected")
class TestSimpleSeedWithUnicode(DBTIntegrationTest):
@property
def schema(self):
return "simple_seed_005"
@property
def models(self):
return "models"
@property
def project_config(self):
return {
'config-version': 2,
"data-paths": ['data-unicode'],
'seeds': {
'quote_columns': False,
}
}
@use_profile('postgres')
def test_postgres_simple_seed(self):
results = self.run_dbt(["seed"])
self.assertEqual(len(results), 1)