forked from dbt-labs/dbt-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_contracts_graph_compiled.py
More file actions
592 lines (529 loc) · 20.5 KB
/
Copy pathtest_contracts_graph_compiled.py
File metadata and controls
592 lines (529 loc) · 20.5 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
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
import pickle
import pytest
from dbt.contracts.files import FileHash
from dbt.contracts.graph.compiled import (
CompiledModelNode, InjectedCTE, CompiledGenericTestNode
)
from dbt.contracts.graph.parsed import (
DependsOn, NodeConfig, TestConfig, TestMetadata, ColumnInfo
)
from dbt.node_types import NodeType
from .utils import (
assert_symmetric,
assert_from_dict,
assert_fails_validation,
dict_replace,
replace_config,
compare_dicts,
)
@pytest.fixture
def basic_uncompiled_model():
return CompiledModelNode(
package_name='test',
root_path='/root/',
path='/root/models/foo.sql',
original_file_path='models/foo.sql',
raw_sql='select * from {{ ref("other") }}',
name='foo',
resource_type=NodeType.Model,
unique_id='model.test.foo',
fqn=['test', 'models', 'foo'],
refs=[],
sources=[],
depends_on=DependsOn(),
deferred=False,
description='',
database='test_db',
schema='test_schema',
alias='bar',
tags=[],
config=NodeConfig(),
meta={},
compiled=False,
extra_ctes=[],
extra_ctes_injected=False,
checksum=FileHash.from_contents(''),
unrendered_config={}
)
@pytest.fixture
def basic_compiled_model():
return CompiledModelNode(
package_name='test',
root_path='/root/',
path='/root/models/foo.sql',
original_file_path='models/foo.sql',
raw_sql='select * from {{ ref("other") }}',
name='foo',
resource_type=NodeType.Model,
unique_id='model.test.foo',
fqn=['test', 'models', 'foo'],
refs=[],
sources=[],
depends_on=DependsOn(),
deferred=True,
description='',
database='test_db',
schema='test_schema',
alias='bar',
tags=[],
config=NodeConfig(),
meta={},
compiled=True,
extra_ctes=[InjectedCTE('whatever', 'select * from other')],
extra_ctes_injected=True,
compiled_sql='with whatever as (select * from other) select * from whatever',
checksum=FileHash.from_contents(''),
unrendered_config={}
)
@pytest.fixture
def minimal_uncompiled_dict():
return {
'name': 'foo',
'root_path': '/root/',
'created_at': 1,
'resource_type': str(NodeType.Model),
'path': '/root/models/foo.sql',
'original_file_path': 'models/foo.sql',
'package_name': 'test',
'raw_sql': 'select * from {{ ref("other") }}',
'unique_id': 'model.test.foo',
'fqn': ['test', 'models', 'foo'],
'database': 'test_db',
'schema': 'test_schema',
'alias': 'bar',
'compiled': False,
'checksum': {'name': 'sha256', 'checksum': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'},
'unrendered_config': {}
}
@pytest.fixture
def basic_uncompiled_dict():
return {
'name': 'foo',
'root_path': '/root/',
'created_at': 1,
'resource_type': str(NodeType.Model),
'path': '/root/models/foo.sql',
'original_file_path': 'models/foo.sql',
'package_name': 'test',
'raw_sql': 'select * from {{ ref("other") }}',
'unique_id': 'model.test.foo',
'fqn': ['test', 'models', 'foo'],
'refs': [],
'sources': [],
'depends_on': {'macros': [], 'nodes': []},
'database': 'test_db',
'deferred': False,
'description': '',
'schema': 'test_schema',
'alias': 'bar',
'tags': [],
'config': {
'column_types': {},
'enabled': True,
'materialized': 'view',
'persist_docs': {},
'post-hook': [],
'pre-hook': [],
'quoting': {},
'tags': [],
'on_schema_change': 'ignore',
'meta': {},
},
'docs': {'show': True},
'columns': {},
'meta': {},
'compiled': False,
'extra_ctes': [],
'extra_ctes_injected': False,
'checksum': {'name': 'sha256', 'checksum': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'},
'unrendered_config': {}
}
@pytest.fixture
def basic_compiled_dict():
return {
'name': 'foo',
'root_path': '/root/',
'created_at': 1,
'resource_type': str(NodeType.Model),
'path': '/root/models/foo.sql',
'original_file_path': 'models/foo.sql',
'package_name': 'test',
'raw_sql': 'select * from {{ ref("other") }}',
'unique_id': 'model.test.foo',
'fqn': ['test', 'models', 'foo'],
'refs': [],
'sources': [],
'depends_on': {'macros': [], 'nodes': []},
'database': 'test_db',
'deferred': True,
'description': '',
'schema': 'test_schema',
'alias': 'bar',
'tags': [],
'config': {
'column_types': {},
'enabled': True,
'materialized': 'view',
'persist_docs': {},
'post-hook': [],
'pre-hook': [],
'quoting': {},
'tags': [],
'on_schema_change': 'ignore',
'meta': {},
},
'docs': {'show': True},
'columns': {},
'meta': {},
'compiled': True,
'extra_ctes': [{'id': 'whatever', 'sql': 'select * from other'}],
'extra_ctes_injected': True,
'compiled_sql': 'with whatever as (select * from other) select * from whatever',
'checksum': {'name': 'sha256', 'checksum': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'},
'unrendered_config': {}
}
def test_basic_uncompiled_model(minimal_uncompiled_dict, basic_uncompiled_dict, basic_uncompiled_model):
node_dict = basic_uncompiled_dict
node = basic_uncompiled_model
assert_symmetric(node, node_dict, CompiledModelNode)
assert node.empty is False
assert node.is_refable is True
assert node.is_ephemeral is False
assert_from_dict(node, minimal_uncompiled_dict, CompiledModelNode)
pickle.loads(pickle.dumps(node))
def test_basic_compiled_model(basic_compiled_dict, basic_compiled_model):
node_dict = basic_compiled_dict
node = basic_compiled_model
assert_symmetric(node, node_dict, CompiledModelNode)
assert node.empty is False
assert node.is_refable is True
assert node.is_ephemeral is False
def test_invalid_extra_fields_model(minimal_uncompiled_dict):
bad_extra = minimal_uncompiled_dict
bad_extra['notvalid'] = 'nope'
assert_fails_validation(bad_extra, CompiledModelNode)
def test_invalid_bad_type_model(minimal_uncompiled_dict):
bad_type = minimal_uncompiled_dict
bad_type['resource_type'] = str(NodeType.Macro)
assert_fails_validation(bad_type, CompiledModelNode)
unchanged_compiled_models = [
lambda u: (u, u.replace(description='a description')),
lambda u: (u, u.replace(tags=['mytag'])),
lambda u: (u, u.replace(meta={'cool_key': 'cool value'})),
# changing the final alias/schema/datbase isn't a change - could just be target changing!
lambda u: (u, u.replace(database='nope')),
lambda u: (u, u.replace(schema='nope')),
lambda u: (u, u.replace(alias='nope')),
# None -> False is a config change even though it's pretty much the same
lambda u: (u.replace(config=u.config.replace(persist_docs={'relation': False})), u.replace(
config=u.config.replace(persist_docs={'relation': False}))),
lambda u: (u.replace(config=u.config.replace(persist_docs={'columns': False})), u.replace(
config=u.config.replace(persist_docs={'columns': False}))),
# True -> True
lambda u: (u.replace(config=u.config.replace(persist_docs={'relation': True})), u.replace(
config=u.config.replace(persist_docs={'relation': True}))),
lambda u: (u.replace(config=u.config.replace(persist_docs={'columns': True})), u.replace(
config=u.config.replace(persist_docs={'columns': True}))),
# only columns docs enabled, but description changed
lambda u: (u.replace(config=u.config.replace(persist_docs={'columns': True})), u.replace(
config=u.config.replace(persist_docs={'columns': True}), description='a model description')),
# only relation docs eanbled, but columns changed
lambda u: (u.replace(config=u.config.replace(persist_docs={'relation': True})), u.replace(config=u.config.replace(
persist_docs={'relation': True}), columns={'a': ColumnInfo(name='a', description='a column description')}))
]
changed_compiled_models = [
lambda u: (u, None),
lambda u: (u, u.replace(raw_sql='select * from wherever')),
lambda u: (u, u.replace(fqn=['test', 'models', 'subdir', 'foo'],
original_file_path='models/subdir/foo.sql', path='/root/models/subdir/foo.sql')),
lambda u: (u, replace_config(u, full_refresh=True)),
lambda u: (u, replace_config(u, post_hook=['select 1 as id'])),
lambda u: (u, replace_config(u, pre_hook=['select 1 as id'])),
lambda u: (u, replace_config(
u, quoting={'database': True, 'schema': False, 'identifier': False})),
# we changed persist_docs values
lambda u: (u, replace_config(u, persist_docs={'relation': True})),
lambda u: (u, replace_config(u, persist_docs={'columns': True})),
lambda u: (u, replace_config(u, persist_docs={
'columns': True, 'relation': True})),
# None -> False is a config change even though it's pretty much the same
lambda u: (u, replace_config(u, persist_docs={'relation': False})),
lambda u: (u, replace_config(u, persist_docs={'columns': False})),
# persist docs was true for the relation and we changed the model description
lambda u: (
replace_config(u, persist_docs={'relation': True}),
replace_config(u, persist_docs={
'relation': True}, description='a model description'),
),
# persist docs was true for columns and we changed the model description
lambda u: (
replace_config(u, persist_docs={'columns': True}),
replace_config(u, persist_docs={'columns': True}, columns={
'a': ColumnInfo(name='a', description='a column description')})
),
# changing alias/schema/database on the config level is a change
lambda u: (u, replace_config(u, database='nope')),
lambda u: (u, replace_config(u, schema='nope')),
lambda u: (u, replace_config(u, alias='nope')),
]
@pytest.mark.parametrize('func', unchanged_compiled_models)
def test_compare_unchanged_model(func, basic_uncompiled_model):
node, compare = func(basic_uncompiled_model)
assert node.same_contents(compare)
@pytest.mark.parametrize('func', changed_compiled_models)
def test_compare_changed_model(func, basic_uncompiled_model):
node, compare = func(basic_uncompiled_model)
assert not node.same_contents(compare)
@pytest.fixture
def minimal_schema_test_dict():
return {
'name': 'foo',
'root_path': '/root/',
'created_at': 1,
'resource_type': str(NodeType.Test),
'path': '/root/x/path.sql',
'original_file_path': '/root/path.sql',
'package_name': 'test',
'raw_sql': 'select * from {{ ref("other") }}',
'unique_id': 'model.test.foo',
'fqn': ['test', 'models', 'foo'],
'database': 'test_db',
'schema': 'dbt_test__audit',
'alias': 'bar',
'test_metadata': {
'name': 'foo',
'kwargs': {},
},
'compiled': False,
'checksum': {'name': 'sha256', 'checksum': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'},
}
@pytest.fixture
def basic_uncompiled_schema_test_node():
return CompiledGenericTestNode(
package_name='test',
root_path='/root/',
path='/root/x/path.sql',
original_file_path='/root/path.sql',
raw_sql='select * from {{ ref("other") }}',
name='foo',
resource_type=NodeType.Test,
unique_id='model.test.foo',
fqn=['test', 'models', 'foo'],
refs=[],
sources=[],
deferred=False,
depends_on=DependsOn(),
description='',
database='test_db',
schema='dbt_test__audit',
alias='bar',
tags=[],
config=TestConfig(),
meta={},
compiled=False,
extra_ctes=[],
extra_ctes_injected=False,
test_metadata=TestMetadata(namespace=None, name='foo', kwargs={}),
checksum=FileHash.from_contents(''),
unrendered_config={}
)
@pytest.fixture
def basic_compiled_schema_test_node():
return CompiledGenericTestNode(
package_name='test',
root_path='/root/',
path='/root/x/path.sql',
original_file_path='/root/path.sql',
raw_sql='select * from {{ ref("other") }}',
name='foo',
resource_type=NodeType.Test,
unique_id='model.test.foo',
fqn=['test', 'models', 'foo'],
refs=[],
sources=[],
depends_on=DependsOn(),
deferred=False,
description='',
database='test_db',
schema='dbt_test__audit',
alias='bar',
tags=[],
config=TestConfig(severity='warn'),
meta={},
compiled=True,
extra_ctes=[InjectedCTE('whatever', 'select * from other')],
extra_ctes_injected=True,
compiled_sql='with whatever as (select * from other) select * from whatever',
column_name='id',
test_metadata=TestMetadata(namespace=None, name='foo', kwargs={}),
checksum=FileHash.from_contents(''),
unrendered_config={
'severity': 'warn',
}
)
@pytest.fixture
def basic_uncompiled_schema_test_dict():
return {
'name': 'foo',
'root_path': '/root/',
'created_at': 1,
'resource_type': str(NodeType.Test),
'path': '/root/x/path.sql',
'original_file_path': '/root/path.sql',
'package_name': 'test',
'raw_sql': 'select * from {{ ref("other") }}',
'unique_id': 'model.test.foo',
'fqn': ['test', 'models', 'foo'],
'refs': [],
'sources': [],
'depends_on': {'macros': [], 'nodes': []},
'database': 'test_db',
'description': '',
'schema': 'dbt_test__audit',
'alias': 'bar',
'tags': [],
'config': {
'enabled': True,
'materialized': 'test',
'tags': [],
'severity': 'ERROR',
'schema': 'dbt_test__audit',
'warn_if': '!= 0',
'error_if': '!= 0',
'fail_calc': 'count(*)',
'meta': {},
},
'deferred': False,
'docs': {'show': True},
'columns': {},
'meta': {},
'compiled': False,
'extra_ctes': [],
'extra_ctes_injected': False,
'test_metadata': {
'name': 'foo',
'kwargs': {},
},
'checksum': {'name': 'sha256', 'checksum': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'},
'unrendered_config': {}
}
@pytest.fixture
def basic_compiled_schema_test_dict():
return {
'name': 'foo',
'root_path': '/root/',
'created_at': 1,
'resource_type': str(NodeType.Test),
'path': '/root/x/path.sql',
'original_file_path': '/root/path.sql',
'package_name': 'test',
'raw_sql': 'select * from {{ ref("other") }}',
'unique_id': 'model.test.foo',
'fqn': ['test', 'models', 'foo'],
'refs': [],
'sources': [],
'depends_on': {'macros': [], 'nodes': []},
'deferred': False,
'database': 'test_db',
'description': '',
'schema': 'dbt_test__audit',
'alias': 'bar',
'tags': [],
'config': {
'enabled': True,
'materialized': 'test',
'tags': [],
'severity': 'warn',
'schema': 'dbt_test__audit',
'warn_if': '!= 0',
'error_if': '!= 0',
'fail_calc': 'count(*)',
'meta': {},
},
'docs': {'show': True},
'columns': {},
'meta': {},
'compiled': True,
'extra_ctes': [{'id': 'whatever', 'sql': 'select * from other'}],
'extra_ctes_injected': True,
'compiled_sql': 'with whatever as (select * from other) select * from whatever',
'column_name': 'id',
'test_metadata': {
'name': 'foo',
'kwargs': {},
},
'checksum': {'name': 'sha256', 'checksum': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'},
'unrendered_config': {
'severity': 'warn',
}
}
def test_basic_uncompiled_schema_test(basic_uncompiled_schema_test_node, basic_uncompiled_schema_test_dict, minimal_schema_test_dict):
node = basic_uncompiled_schema_test_node
node_dict = basic_uncompiled_schema_test_dict
minimum = minimal_schema_test_dict
assert_symmetric(node, node_dict, CompiledGenericTestNode)
assert node.empty is False
assert node.is_refable is False
assert node.is_ephemeral is False
assert_from_dict(node, minimum, CompiledGenericTestNode)
def test_basic_compiled_schema_test(basic_compiled_schema_test_node, basic_compiled_schema_test_dict):
node = basic_compiled_schema_test_node
node_dict = basic_compiled_schema_test_dict
assert_symmetric(node, node_dict, CompiledGenericTestNode)
assert node.empty is False
assert node.is_refable is False
assert node.is_ephemeral is False
def test_invalid_extra_schema_test_fields(minimal_schema_test_dict):
bad_extra = minimal_schema_test_dict
bad_extra['extra'] = 'extra value'
assert_fails_validation(bad_extra, CompiledGenericTestNode)
def test_invalid_resource_type_schema_test(minimal_schema_test_dict):
bad_type = minimal_schema_test_dict
bad_type['resource_type'] = str(NodeType.Model)
assert_fails_validation(bad_type, CompiledGenericTestNode)
unchanged_schema_tests = [
# for tests, raw_sql isn't a change (because it's always the same for a given test macro)
lambda u: u.replace(raw_sql='select * from wherever'),
lambda u: u.replace(description='a description'),
lambda u: u.replace(tags=['mytag']),
lambda u: u.replace(meta={'cool_key': 'cool value'}),
# these values don't even mean anything on schema tests!
lambda u: replace_config(u, alias='nope'),
lambda u: replace_config(u, database='nope'),
lambda u: replace_config(u, schema='nope'),
lambda u: u.replace(database='other_db'),
lambda u: u.replace(schema='other_schema'),
lambda u: u.replace(alias='foo'),
lambda u: replace_config(u, full_refresh=True),
lambda u: replace_config(u, post_hook=['select 1 as id']),
lambda u: replace_config(u, pre_hook=['select 1 as id']),
lambda u: replace_config(
u, quoting={'database': True, 'schema': False, 'identifier': False}),
]
changed_schema_tests = [
lambda u: None,
lambda u: u.replace(fqn=['test', 'models', 'subdir', 'foo'],
original_file_path='models/subdir/foo.sql', path='/root/models/subdir/foo.sql'),
lambda u: replace_config(u, severity='warn'),
# If we checked test metadata, these would caount. But we don't, because these changes would all change the unique ID, so it's irrelevant.
# lambda u: u.replace(test_metadata=u.test_metadata.replace(namespace='something')),
# lambda u: u.replace(test_metadata=u.test_metadata.replace(name='bar')),
# lambda u: u.replace(test_metadata=u.test_metadata.replace(kwargs={'arg': 'value'})),
]
@pytest.mark.parametrize('func', unchanged_schema_tests)
def test_compare_unchanged_schema_test(func, basic_uncompiled_schema_test_node):
value = func(basic_uncompiled_schema_test_node)
assert basic_uncompiled_schema_test_node.same_contents(value)
@pytest.mark.parametrize('func', changed_schema_tests)
def test_compare_changed_schema_test(func, basic_uncompiled_schema_test_node):
value = func(basic_uncompiled_schema_test_node)
assert not basic_uncompiled_schema_test_node.same_contents(value)
def test_compare_to_compiled(basic_uncompiled_schema_test_node, basic_compiled_schema_test_node):
# if you fix the severity, they should be the "same".
uncompiled = basic_uncompiled_schema_test_node
compiled = basic_compiled_schema_test_node
assert not uncompiled.same_contents(compiled)
fixed_config = compiled.config.replace(severity=uncompiled.config.severity)
fixed_compiled = compiled.replace(
config=fixed_config, unrendered_config=uncompiled.unrendered_config)
assert uncompiled.same_contents(fixed_compiled)