forked from dbt-labs/dbt-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cache.py
491 lines (405 loc) · 20.7 KB
/
test_cache.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
from unittest import TestCase
from dbt.adapters.cache import RelationsCache
from dbt.adapters.base.relation import BaseRelation
from multiprocessing.dummy import Pool as ThreadPool
import dbt.exceptions
import random
import time
def make_relation(database, schema, identifier):
return BaseRelation.create(database=database, schema=schema,
identifier=identifier)
def make_mock_relationship(database, schema, identifier):
return BaseRelation.create(
database=database, schema=schema, identifier=identifier, type='view'
)
class TestCache(TestCase):
def setUp(self):
self.cache = RelationsCache()
def assert_relations_state(self, database, schema, identifiers):
relations = self.cache.get_relations(database, schema)
for identifier, expect in identifiers.items():
found = any(
(r.identifier == identifier and \
r.schema == schema and \
r.database == database)
for r in relations
)
msg = '{}.{}.{} was{} found in the cache!'.format(
database, schema, identifier, '' if found else ' not'
)
self.assertEqual(expect, found, msg)
def assert_relations_exist(self, database, schema, *identifiers):
self.assert_relations_state(database, schema, {k: True for k in identifiers})
def assert_relations_do_not_exist(self, database, schema, *identifiers):
self.assert_relations_state(database, schema, {k: False for k in identifiers})
class TestEmpty(TestCache):
def test_empty(self):
self.assertEqual(len(self.cache.relations), 0)
self.assertEqual(len(self.cache.get_relations('dbt', 'test')), 0)
class TestDrop(TestCache):
def setUp(self):
super().setUp()
self.cache.add(make_relation('dbt', 'foo', 'bar'))
def test_missing_identifier_ignored(self):
self.cache.drop(make_relation('dbt', 'foo', 'bar1'))
self.assert_relations_exist('dbt', 'foo', 'bar')
self.assertEqual(len(self.cache.relations), 1)
def test_missing_schema_ignored(self):
self.cache.drop(make_relation('dbt', 'foo1', 'bar'))
self.assert_relations_exist('dbt', 'foo', 'bar')
self.assertEqual(len(self.cache.relations), 1)
def test_missing_db_ignored(self):
self.cache.drop(make_relation('dbt1', 'foo', 'bar'))
self.assert_relations_exist('dbt', 'foo', 'bar')
self.assertEqual(len(self.cache.relations), 1)
def test_drop(self):
self.cache.drop(make_relation('dbt', 'foo', 'bar'))
self.assert_relations_do_not_exist('dbt', 'foo', 'bar')
self.assertEqual(len(self.cache.relations), 0)
class TestAddLink(TestCache):
def setUp(self):
super().setUp()
self.cache.add(make_relation('dbt', 'schema', 'foo'))
self.cache.add(make_relation('dbt_2', 'schema', 'bar'))
self.cache.add(make_relation('dbt', 'schema_2', 'bar'))
def test_no_src(self):
self.assert_relations_exist('dbt', 'schema', 'foo')
self.assert_relations_do_not_exist('dbt', 'schema', 'bar')
self.cache.add_link(make_relation('dbt', 'schema', 'bar'),
make_relation('dbt', 'schema', 'foo'))
self.assert_relations_exist('dbt', 'schema', 'foo', 'bar')
def test_no_dst(self):
self.assert_relations_exist('dbt', 'schema', 'foo')
self.assert_relations_do_not_exist('dbt', 'schema', 'bar')
self.cache.add_link(make_relation('dbt', 'schema', 'foo'),
make_relation('dbt', 'schema', 'bar'))
self.assert_relations_exist('dbt', 'schema', 'foo', 'bar')
class TestRename(TestCache):
def setUp(self):
super().setUp()
self.cache.add(make_relation('DBT', 'schema', 'foo'))
self.assert_relations_exist('DBT', 'schema', 'foo')
self.assertEqual(self.cache.schemas, {('dbt', 'schema')})
def test_no_source_error(self):
# dest should be created anyway (it's probably a temp table)
self.cache.rename(make_relation('DBT', 'schema', 'bar'),
make_relation('DBT', 'schema', 'baz'))
self.assertEqual(len(self.cache.relations), 2)
self.assert_relations_exist('DBT', 'schema', 'foo', 'baz')
def test_dest_exists_error(self):
foo = make_relation('DBT', 'schema', 'foo')
bar = make_relation('DBT', 'schema', 'bar')
self.cache.add(bar)
self.assert_relations_exist('DBT', 'schema', 'foo', 'bar')
with self.assertRaises(dbt.exceptions.InternalException):
self.cache.rename(foo, bar)
self.assert_relations_exist('DBT', 'schema', 'foo', 'bar')
def test_dest_different_db(self):
self.cache.rename(make_relation('DBT', 'schema', 'foo'),
make_relation('DBT_2', 'schema', 'foo'))
self.assert_relations_exist('DBT_2', 'schema', 'foo')
self.assert_relations_do_not_exist('DBT', 'schema', 'foo')
# we know about both schemas: dbt has nothing, dbt_2 has something.
self.assertEqual(self.cache.schemas, {('dbt_2', 'schema'), ('dbt', 'schema')})
self.assertEqual(len(self.cache.relations), 1)
def test_rename_identifier(self):
self.cache.rename(make_relation('DBT', 'schema', 'foo'),
make_relation('DBT', 'schema', 'bar'))
self.assert_relations_exist('DBT', 'schema', 'bar')
self.assert_relations_do_not_exist('DBT', 'schema', 'foo')
self.assertEqual(self.cache.schemas, {('dbt', 'schema')})
relation = self.cache.relations[('dbt', 'schema', 'bar')]
self.assertEqual(relation.inner.schema, 'schema')
self.assertEqual(relation.inner.identifier, 'bar')
self.assertEqual(relation.schema, 'schema')
self.assertEqual(relation.identifier, 'bar')
def test_rename_db(self):
self.cache.rename(make_relation('DBT', 'schema', 'foo'),
make_relation('DBT_2', 'schema', 'foo'))
self.assertEqual(len(self.cache.get_relations('DBT', 'schema')), 0)
self.assertEqual(len(self.cache.get_relations('DBT_2', 'schema')), 1)
self.assert_relations_exist('DBT_2', 'schema', 'foo')
self.assert_relations_do_not_exist('DBT', 'schema', 'foo')
# we know about both schemas: dbt has nothing, dbt_2 has something.
self.assertEqual(self.cache.schemas, {('dbt_2', 'schema'), ('dbt', 'schema')})
relation = self.cache.relations[('dbt_2', 'schema', 'foo')]
self.assertEqual(relation.inner.database, 'DBT_2')
self.assertEqual(relation.inner.schema, 'schema')
self.assertEqual(relation.inner.identifier, 'foo')
self.assertEqual(relation.database, 'dbt_2')
self.assertEqual(relation.schema, 'schema')
self.assertEqual(relation.identifier, 'foo')
def test_rename_schema(self):
self.cache.rename(make_relation('DBT', 'schema', 'foo'),
make_relation('DBT', 'schema_2', 'foo'))
self.assertEqual(len(self.cache.get_relations('DBT', 'schema')), 0)
self.assertEqual(len(self.cache.get_relations('DBT', 'schema_2')), 1)
self.assert_relations_exist('DBT', 'schema_2', 'foo')
self.assert_relations_do_not_exist('DBT', 'schema', 'foo')
# we know about both schemas: schema has nothing, schema_2 has something.
self.assertEqual(self.cache.schemas, {('dbt', 'schema_2'), ('dbt', 'schema')})
relation = self.cache.relations[('dbt', 'schema_2', 'foo')]
self.assertEqual(relation.inner.database, 'DBT')
self.assertEqual(relation.inner.schema, 'schema_2')
self.assertEqual(relation.inner.identifier, 'foo')
self.assertEqual(relation.database, 'dbt')
self.assertEqual(relation.schema, 'schema_2')
self.assertEqual(relation.identifier, 'foo')
class TestGetRelations(TestCache):
def setUp(self):
super().setUp()
self.relation = make_relation('dbt', 'foo', 'bar')
self.cache.add(self.relation)
def test_get_by_name(self):
relations = self.cache.get_relations('dbt', 'foo')
self.assertEqual(len(relations), 1)
self.assertIs(relations[0], self.relation)
def test_get_by_uppercase_schema(self):
relations = self.cache.get_relations('dbt', 'FOO')
self.assertEqual(len(relations), 1)
self.assertIs(relations[0], self.relation)
def test_get_by_uppercase_db(self):
relations = self.cache.get_relations('DBT', 'foo')
self.assertEqual(len(relations), 1)
self.assertIs(relations[0], self.relation)
def test_get_by_uppercase_schema_and_db(self):
relations = self.cache.get_relations('DBT', 'FOO')
self.assertEqual(len(relations), 1)
self.assertIs(relations[0], self.relation)
def test_get_by_wrong_db(self):
relations = self.cache.get_relations('dbt_2', 'foo')
self.assertEqual(len(relations), 0)
def test_get_by_wrong_schema(self):
relations = self.cache.get_relations('dbt', 'foo_2')
self.assertEqual(len(relations), 0)
class TestAdd(TestCache):
def setUp(self):
super().setUp()
self.relation = make_relation('dbt', 'foo', 'bar')
self.cache.add(self.relation)
def test_add(self):
relations = self.cache.get_relations('dbt', 'foo')
self.assertEqual(len(relations), 1)
self.assertEqual(len(self.cache.relations), 1)
self.assertIs(relations[0], self.relation)
def test_add_twice(self):
# add a new relation with same name
self.cache.add(make_relation('dbt', 'foo', 'bar'))
self.assertEqual(len(self.cache.relations), 1)
self.assertEqual(self.cache.schemas, {('dbt', 'foo')})
self.assert_relations_exist('dbt', 'foo', 'bar')
def add_uppercase_schema(self):
self.cache.add(make_relation('dbt', 'FOO', 'baz'))
self.assertEqual(len(self.cache.relations), 2)
relations = self.cache.get_relations('dbt', 'foo')
self.assertEqual(len(relations), 2)
self.assertEqual(self.cache.schemas, {('dbt', 'foo')})
self.assertIsNot(self.cache.relations[('dbt', 'foo', 'bar')].inner, None)
self.assertIsNot(self.cache.relations[('dbt', 'foo', 'baz')].inner, None)
def add_different_db(self):
self.cache.add(make_relation('dbt_2', 'foo', 'bar'))
self.assertEqual(len(self.cache.relations), 2)
self.assertEqual(len(self.cache.get_relations('dbt_2', 'foo')), 1)
self.assertEqual(len(self.cache.get_relations('dbt', 'foo')), 1)
self.assertEqual(self.cache.schemas, {('dbt', 'foo'), ('dbt_2', 'foo')})
self.assertIsNot(self.cache.relations[('dbt', 'foo', 'bar')].inner, None)
self.assertIsNot(self.cache.relations[('dbt_2', 'foo', 'bar')].inner, None)
class TestLikeDbt(TestCase):
def setUp(self):
self.cache = RelationsCache()
self._sleep = True
# add a bunch of cache entries
for ident in 'abcdef':
self.cache.add(make_relation('dbt', 'schema', ident))
# 'b' references 'a'
self.cache.add_link(make_relation('dbt', 'schema', 'a'),
make_relation('dbt', 'schema', 'b'))
# and 'c' references 'b'
self.cache.add_link(make_relation('dbt', 'schema', 'b'),
make_relation('dbt', 'schema', 'c'))
# and 'd' references 'b'
self.cache.add_link(make_relation('dbt', 'schema', 'b'),
make_relation('dbt', 'schema', 'd'))
# and 'e' references 'a'
self.cache.add_link(make_relation('dbt', 'schema', 'a'),
make_relation('dbt', 'schema', 'e'))
# and 'f' references 'd'
self.cache.add_link(make_relation('dbt', 'schema', 'd'),
make_relation('dbt', 'schema', 'f'))
# so drop propagation goes (a -> (b -> (c (d -> f))) e)
def assert_has_relations(self, expected):
current = set(r.identifier for r in self.cache.get_relations('dbt', 'schema'))
self.assertEqual(current, expected)
def test_drop_inner(self):
self.assert_has_relations(set('abcdef'))
self.cache.drop(make_relation('dbt', 'schema', 'b'))
self.assert_has_relations({'a', 'e'})
def test_rename_and_drop(self):
self.assert_has_relations(set('abcdef'))
# drop the backup/tmp
self.cache.drop(make_relation('dbt', 'schema', 'b__backup'))
self.cache.drop(make_relation('dbt', 'schema', 'b__tmp'))
self.assert_has_relations(set('abcdef'))
# create a new b__tmp
self.cache.add(make_relation('dbt', 'schema', 'b__tmp',))
self.assert_has_relations(set('abcdef') | {'b__tmp'})
# rename b -> b__backup
self.cache.rename(make_relation('dbt', 'schema', 'b'),
make_relation('dbt', 'schema', 'b__backup'))
self.assert_has_relations(set('acdef') | {'b__tmp', 'b__backup'})
# rename temp to b
self.cache.rename(make_relation('dbt', 'schema', 'b__tmp'),
make_relation('dbt', 'schema', 'b'))
self.assert_has_relations(set('abcdef') | {'b__backup'})
# drop backup, everything that used to depend on b should be gone, but
# b itself should still exist
self.cache.drop(make_relation('dbt', 'schema', 'b__backup'))
self.assert_has_relations(set('abe'))
relation = self.cache.relations[('dbt', 'schema', 'a')]
self.assertEqual(len(relation.referenced_by), 1)
def _rand_sleep(self):
if not self._sleep:
return
time.sleep(random.random() * 0.1)
def _target(self, ident):
self._rand_sleep()
self.cache.rename(make_relation('dbt', 'schema', ident),
make_relation('dbt', 'schema', ident+'__backup'))
self._rand_sleep()
self.cache.add(make_relation('dbt', 'schema', ident+'__tmp')
)
self._rand_sleep()
self.cache.rename(make_relation('dbt', 'schema', ident+'__tmp'),
make_relation('dbt', 'schema', ident))
self._rand_sleep()
self.cache.drop(make_relation('dbt', 'schema', ident+'__backup'))
return ident, self.cache.get_relations('dbt', 'schema')
def test_threaded(self):
# add three more short subchains for threads to test on
for ident in 'ghijklmno':
obj = make_mock_relationship('test_db', 'schema', ident)
self.cache.add(make_relation('dbt', 'schema', ident))
self.cache.add_link(make_relation('dbt', 'schema', 'a'),
make_relation('dbt', 'schema', 'g'))
self.cache.add_link(make_relation('dbt', 'schema', 'g'),
make_relation('dbt', 'schema', 'h'))
self.cache.add_link(make_relation('dbt', 'schema', 'h'),
make_relation('dbt', 'schema', 'i'))
self.cache.add_link(make_relation('dbt', 'schema', 'a'),
make_relation('dbt', 'schema', 'j'))
self.cache.add_link(make_relation('dbt', 'schema', 'j'),
make_relation('dbt', 'schema', 'k'))
self.cache.add_link(make_relation('dbt', 'schema', 'k'),
make_relation('dbt', 'schema', 'l'))
self.cache.add_link(make_relation('dbt', 'schema', 'a'),
make_relation('dbt', 'schema', 'm'))
self.cache.add_link(make_relation('dbt', 'schema', 'm'),
make_relation('dbt', 'schema', 'n'))
self.cache.add_link(make_relation('dbt', 'schema', 'n'),
make_relation('dbt', 'schema', 'o'))
pool = ThreadPool(4)
results = list(pool.imap_unordered(self._target, ('b', 'g', 'j', 'm')))
pool.close()
pool.join()
# at a minimum, we expect each table to "see" itself, its parent ('a'),
# and the unrelated table ('a')
min_expect = {
'b': {'a', 'b', 'e'},
'g': {'a', 'g', 'e'},
'j': {'a', 'j', 'e'},
'm': {'a', 'm', 'e'},
}
for ident, relations in results:
seen = set(r.identifier for r in relations)
self.assertTrue(min_expect[ident].issubset(seen))
self.assert_has_relations(set('abgjme'))
def test_threaded_repeated(self):
for _ in range(10):
self.setUp()
self._sleep = False
self.test_threaded()
class TestComplexCache(TestCase):
def setUp(self):
self.cache = RelationsCache()
inputs = [
('dbt', 'foo', 'table1'),
('dbt', 'foo', 'table3'),
('dbt', 'foo', 'table4'),
('dbt', 'bar', 'table2'),
('dbt', 'bar', 'table3'),
('dbt_2', 'foo', 'table1'),
('dbt_2', 'foo', 'table2'),
]
self.inputs = [make_relation(d, s, i) for d, s, i in inputs]
for relation in self.inputs:
self.cache.add(relation)
# dbt.foo.table3 references dbt.foo.table1
# (create view dbt.foo.table3 as (select * from dbt.foo.table1...))
self.cache.add_link(
make_relation('dbt', 'foo', 'table1'),
make_relation('dbt', 'foo', 'table3')
)
# dbt.bar.table3 references dbt.foo.table3
# (create view dbt.bar.table5 as (select * from dbt.foo.table3...))
self.cache.add_link(
make_relation('dbt', 'foo', 'table3'),
make_relation('dbt', 'bar', 'table3')
)
# dbt.foo.table4 also references dbt.foo.table1
self.cache.add_link(
make_relation('dbt', 'foo', 'table1'),
make_relation('dbt', 'foo', 'table4')
)
# and dbt_2.foo.table1 references dbt.foo.table1
self.cache.add_link(
make_relation('dbt', 'foo', 'table1'),
make_relation('dbt_2', 'foo', 'table1'),
)
def test_get_relations(self):
self.assertEqual(len(self.cache.get_relations('dbt', 'foo')), 3)
self.assertEqual(len(self.cache.get_relations('dbt', 'bar')), 2)
self.assertEqual(len(self.cache.get_relations('dbt_2', 'foo')), 2)
self.assertEqual(len(self.cache.relations), 7)
def test_drop_one(self):
# dropping dbt.bar.table2 should only drop itself
self.cache.drop(make_relation('dbt', 'bar', 'table2'))
self.assertEqual(len(self.cache.get_relations('dbt', 'foo')), 3)
self.assertEqual(len(self.cache.get_relations('dbt', 'bar')), 1)
self.assertEqual(len(self.cache.get_relations('dbt_2', 'foo')), 2)
self.assertEqual(len(self.cache.relations), 6)
def test_drop_many(self):
# dropping dbt.foo.table1 should drop everything but dbt.bar.table2 and
# dbt_2.foo.table2
self.cache.drop(make_relation('dbt', 'foo', 'table1'))
self.assertEqual(len(self.cache.get_relations('dbt', 'foo')), 0)
self.assertEqual(len(self.cache.get_relations('dbt', 'bar')), 1)
self.assertEqual(len(self.cache.get_relations('dbt_2', 'foo')), 1)
self.assertEqual(len(self.cache.relations), 2)
def test_rename_root(self):
self.cache.rename(make_relation('dbt', 'foo', 'table1'),
make_relation('dbt', 'bar', 'table1'))
retrieved = self.cache.relations[('dbt', 'bar', 'table1')].inner
self.assertEqual(retrieved.schema, 'bar')
self.assertEqual(retrieved.identifier, 'table1')
self.assertEqual(len(self.cache.get_relations('dbt', 'foo')), 2)
self.assertEqual(len(self.cache.get_relations('dbt', 'bar')), 3)
self.assertEqual(len(self.cache.get_relations('dbt_2', 'foo')), 2)
self.assertEqual(len(self.cache.relations), 7)
# make sure drops still cascade from the renamed table
self.cache.drop(make_relation('dbt', 'bar', 'table1'))
self.assertEqual(len(self.cache.get_relations('dbt', 'foo')), 0)
self.assertEqual(len(self.cache.get_relations('dbt', 'bar')), 1)
self.assertEqual(len(self.cache.get_relations('dbt_2', 'foo')), 1)
self.assertEqual(len(self.cache.relations), 2)
def test_rename_branch(self):
self.cache.rename(make_relation('dbt', 'foo', 'table3'),
make_relation('dbt', 'foo', 'table2'))
self.assertEqual(len(self.cache.get_relations('dbt', 'foo')), 3)
self.assertEqual(len(self.cache.get_relations('dbt', 'bar')), 2)
self.assertEqual(len(self.cache.get_relations('dbt_2', 'foo')), 2)
# make sure drops still cascade through the renamed table
self.cache.drop(make_relation('dbt', 'foo', 'table1'))
self.assertEqual(len(self.cache.get_relations('dbt', 'foo')), 0)
self.assertEqual(len(self.cache.get_relations('dbt', 'bar')), 1)
self.assertEqual(len(self.cache.get_relations('dbt_2', 'foo')), 1)
self.assertEqual(len(self.cache.relations), 2)