forked from dbt-labs/dbt-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_contracts_graph_unparsed.py
731 lines (664 loc) · 23.9 KB
/
test_contracts_graph_unparsed.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
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
import copy
import pickle
from datetime import timedelta
from dbt.contracts.graph.unparsed import (
UnparsedNode, UnparsedRunHook, UnparsedMacro, Time, TimePeriod,
FreshnessThreshold, Quoting, UnparsedSourceDefinition,
UnparsedSourceTableDefinition, UnparsedDocumentationFile, UnparsedColumn,
UnparsedNodeUpdate, Docs, UnparsedExposure, MaturityType, ExposureOwner,
ExposureType, UnparsedMetric, MetricFilter
)
from dbt.contracts.results import FreshnessStatus
from dbt.node_types import NodeType
from .utils import ContractTestCase
class TestUnparsedMacro(ContractTestCase):
ContractType = UnparsedMacro
def test_ok(self):
macro_dict = {
'path': '/root/path.sql',
'original_file_path': '/root/path.sql',
'package_name': 'test',
'raw_sql': '{% macro foo() %}select 1 as id{% endmacro %}',
'root_path': '/root/',
'resource_type': 'macro',
}
macro = self.ContractType(
path='/root/path.sql',
original_file_path='/root/path.sql',
package_name='test',
raw_sql='{% macro foo() %}select 1 as id{% endmacro %}',
root_path='/root/',
resource_type=NodeType.Macro,
)
self.assert_symmetric(macro, macro_dict)
pickle.loads(pickle.dumps(macro))
def test_invalid_missing_field(self):
macro_dict = {
'path': '/root/path.sql',
'original_file_path': '/root/path.sql',
# 'package_name': 'test',
'raw_sql': '{% macro foo() %}select 1 as id{% endmacro %}',
'root_path': '/root/',
'resource_type': 'macro',
}
self.assert_fails_validation(macro_dict)
def test_invalid_extra_field(self):
macro_dict = {
'path': '/root/path.sql',
'original_file_path': '/root/path.sql',
'package_name': 'test',
'raw_sql': '{% macro foo() %}select 1 as id{% endmacro %}',
'root_path': '/root/',
'extra': 'extra',
'resource_type': 'macro',
}
self.assert_fails_validation(macro_dict)
class TestUnparsedNode(ContractTestCase):
ContractType = UnparsedNode
def test_ok(self):
node_dict = {
'name': 'foo',
'root_path': '/root/',
'resource_type': NodeType.Model,
'path': '/root/x/path.sql',
'original_file_path': '/root/path.sql',
'package_name': 'test',
'raw_sql': 'select * from {{ ref("thing") }}',
}
node = self.ContractType(
package_name='test',
root_path='/root/',
path='/root/x/path.sql',
original_file_path='/root/path.sql',
raw_sql='select * from {{ ref("thing") }}',
name='foo',
resource_type=NodeType.Model,
)
self.assert_symmetric(node, node_dict)
self.assertFalse(node.empty)
self.assert_fails_validation(node_dict, cls=UnparsedRunHook)
self.assert_fails_validation(node_dict, cls=UnparsedMacro)
pickle.loads(pickle.dumps(node))
def test_empty(self):
node_dict = {
'name': 'foo',
'root_path': '/root/',
'resource_type': NodeType.Model,
'path': '/root/x/path.sql',
'original_file_path': '/root/path.sql',
'package_name': 'test',
'raw_sql': ' \n',
}
node = UnparsedNode(
package_name='test',
root_path='/root/',
path='/root/x/path.sql',
original_file_path='/root/path.sql',
raw_sql=' \n',
name='foo',
resource_type=NodeType.Model,
)
self.assert_symmetric(node, node_dict)
self.assertTrue(node.empty)
self.assert_fails_validation(node_dict, cls=UnparsedRunHook)
self.assert_fails_validation(node_dict, cls=UnparsedMacro)
def test_bad_type(self):
node_dict = {
'name': 'foo',
'root_path': '/root/',
'resource_type': NodeType.Source, # not valid!
'path': '/root/x/path.sql',
'original_file_path': '/root/path.sql',
'package_name': 'test',
'raw_sql': 'select * from {{ ref("thing") }}',
}
self.assert_fails_validation(node_dict)
class TestUnparsedRunHook(ContractTestCase):
ContractType = UnparsedRunHook
def test_ok(self):
node_dict = {
'name': 'foo',
'root_path': 'test/dbt_project.yml',
'resource_type': NodeType.Operation,
'path': '/root/dbt_project.yml',
'original_file_path': '/root/dbt_project.yml',
'package_name': 'test',
'raw_sql': 'GRANT select on dbt_postgres',
'index': 4
}
node = self.ContractType(
package_name='test',
root_path='test/dbt_project.yml',
path='/root/dbt_project.yml',
original_file_path='/root/dbt_project.yml',
raw_sql='GRANT select on dbt_postgres',
name='foo',
resource_type=NodeType.Operation,
index=4,
)
self.assert_symmetric(node, node_dict)
self.assert_fails_validation(node_dict, cls=UnparsedNode)
pickle.loads(pickle.dumps(node))
def test_bad_type(self):
node_dict = {
'name': 'foo',
'root_path': 'test/dbt_project.yml',
'resource_type': NodeType.Model, # invalid
'path': '/root/dbt_project.yml',
'original_file_path': '/root/dbt_project.yml',
'package_name': 'test',
'raw_sql': 'GRANT select on dbt_postgres',
'index': 4
}
self.assert_fails_validation(node_dict)
class TestFreshnessThreshold(ContractTestCase):
ContractType = FreshnessThreshold
def test_empty(self):
empty = self.ContractType()
self.assert_symmetric(empty, {'error_after': {}, 'warn_after': {}})
self.assertEqual(empty.status(float('Inf')), FreshnessStatus.Pass)
self.assertEqual(empty.status(0), FreshnessStatus.Pass)
def test_both(self):
threshold = self.ContractType(
warn_after=Time(count=18, period=TimePeriod.hour),
error_after=Time(count=2, period=TimePeriod.day),
)
dct = {
'error_after': {'count': 2, 'period': 'day'},
'warn_after': {'count': 18, 'period': 'hour'}
}
self.assert_symmetric(threshold, dct)
error_seconds = timedelta(days=3).total_seconds()
warn_seconds = timedelta(days=1).total_seconds()
pass_seconds = timedelta(hours=3).total_seconds()
self.assertEqual(threshold.status(
error_seconds), FreshnessStatus.Error)
self.assertEqual(threshold.status(warn_seconds), FreshnessStatus.Warn)
self.assertEqual(threshold.status(pass_seconds), FreshnessStatus.Pass)
pickle.loads(pickle.dumps(threshold))
def test_merged(self):
t1 = self.ContractType(
warn_after=Time(count=36, period=TimePeriod.hour),
error_after=Time(count=2, period=TimePeriod.day),
)
t2 = self.ContractType(
warn_after=Time(count=18, period=TimePeriod.hour),
)
threshold = self.ContractType(
warn_after=Time(count=18, period=TimePeriod.hour),
error_after=Time(count=None, period=None),
)
self.assertEqual(threshold, t1.merged(t2))
warn_seconds = timedelta(days=1).total_seconds()
pass_seconds = timedelta(hours=3).total_seconds()
self.assertEqual(threshold.status(warn_seconds), FreshnessStatus.Warn)
self.assertEqual(threshold.status(pass_seconds), FreshnessStatus.Pass)
class TestQuoting(ContractTestCase):
ContractType = Quoting
def test_empty(self):
empty = self.ContractType()
self.assert_symmetric(empty, {})
def test_partial(self):
a = self.ContractType(None, True, False)
b = self.ContractType(True, False, None)
self.assert_symmetric(a, {'schema': True, 'identifier': False})
self.assert_symmetric(b, {'database': True, 'schema': False})
c = a.merged(b)
self.assertEqual(c, self.ContractType(True, False, False))
self.assert_symmetric(
c, {'database': True, 'schema': False, 'identifier': False}
)
pickle.loads(pickle.dumps(c))
class TestUnparsedSourceDefinition(ContractTestCase):
ContractType = UnparsedSourceDefinition
def test_defaults(self):
minimum = self.ContractType(name='foo')
from_dict = {'name': 'foo'}
to_dict = {
'name': 'foo',
'description': '',
'freshness': {'error_after': {}, 'warn_after': {}},
'quoting': {},
'tables': [],
'loader': '',
'meta': {},
'tags': [],
'config': {},
}
self.assert_from_dict(minimum, from_dict)
self.assert_to_dict(minimum, to_dict)
def test_contents(self):
empty = self.ContractType(
name='foo',
description='a description',
quoting=Quoting(database=False),
loader='some_loader',
freshness=FreshnessThreshold(),
tables=[],
meta={},
)
dct = {
'name': 'foo',
'description': 'a description',
'quoting': {'database': False},
'loader': 'some_loader',
'freshness': {'error_after': {}, 'warn_after': {}},
'tables': [],
'meta': {},
'tags': [],
'config': {},
}
self.assert_symmetric(empty, dct)
def test_table_defaults(self):
table_1 = UnparsedSourceTableDefinition(name='table1')
table_2 = UnparsedSourceTableDefinition(
name='table2',
description='table 2',
quoting=Quoting(database=True),
)
source = self.ContractType(
name='foo',
tables=[table_1, table_2]
)
from_dict = {
'name': 'foo',
'tables': [
{'name': 'table1'},
{
'name': 'table2',
'description': 'table 2',
'quoting': {'database': True},
},
],
}
to_dict = {
'name': 'foo',
'description': '',
'loader': '',
'freshness': {'error_after': {}, 'warn_after': {}},
'quoting': {},
'meta': {},
'tables': [
{
'name': 'table1',
'description': '',
'docs': {'show': True},
'tests': [],
'columns': [],
'quoting': {},
'freshness': {'error_after': {}, 'warn_after': {}},
'meta': {},
'tags': [],
},
{
'name': 'table2',
'description': 'table 2',
'docs': {'show': True},
'tests': [],
'columns': [],
'quoting': {'database': True},
'freshness': {'error_after': {}, 'warn_after': {}},
'meta': {},
'tags': [],
},
],
'tags': [],
'config': {},
}
self.assert_from_dict(source, from_dict)
self.assert_symmetric(source, to_dict)
pickle.loads(pickle.dumps(source))
class TestUnparsedDocumentationFile(ContractTestCase):
ContractType = UnparsedDocumentationFile
def test_ok(self):
doc = self.ContractType(
package_name='test',
root_path='/root',
path='/root/docs',
original_file_path='/root/docs/doc.md',
file_contents='blah blah blah',
)
doc_dict = {
'package_name': 'test',
'root_path': '/root',
'path': '/root/docs',
'original_file_path': '/root/docs/doc.md',
'file_contents': 'blah blah blah',
}
self.assert_symmetric(doc, doc_dict)
self.assertEqual(doc.resource_type, NodeType.Documentation)
self.assert_fails_validation(doc_dict, UnparsedNode)
pickle.loads(pickle.dumps(doc))
def test_extra_field(self):
self.assert_fails_validation({})
doc_dict = {
'package_name': 'test',
'root_path': '/root',
'path': '/root/docs',
'original_file_path': '/root/docs/doc.md',
'file_contents': 'blah blah blah',
'resource_type': 'docs',
}
self.assert_fails_validation(doc_dict)
class TestUnparsedNodeUpdate(ContractTestCase):
ContractType = UnparsedNodeUpdate
def test_defaults(self):
minimum = self.ContractType(
name='foo',
yaml_key='models',
original_file_path='/some/fake/path',
package_name='test',
)
from_dict = {
'name': 'foo',
'yaml_key': 'models',
'original_file_path': '/some/fake/path',
'package_name': 'test',
}
to_dict = {
'name': 'foo',
'yaml_key': 'models',
'original_file_path': '/some/fake/path',
'package_name': 'test',
'columns': [],
'description': '',
'docs': {'show': True},
'tests': [],
'meta': {},
'config': {},
}
self.assert_from_dict(minimum, from_dict)
self.assert_to_dict(minimum, to_dict)
def test_contents(self):
update = self.ContractType(
name='foo',
yaml_key='models',
original_file_path='/some/fake/path',
package_name='test',
description='a description',
tests=['table_test'],
meta={'key': ['value1', 'value2']},
columns=[
UnparsedColumn(
name='x',
description='x description',
meta={'key2': 'value3'},
),
UnparsedColumn(
name='y',
description='y description',
tests=[
'unique',
{'accepted_values': {'values': ['blue', 'green']}}
],
meta={},
tags=['a', 'b'],
),
],
docs=Docs(show=False),
)
dct = {
'name': 'foo',
'yaml_key': 'models',
'original_file_path': '/some/fake/path',
'package_name': 'test',
'description': 'a description',
'tests': ['table_test'],
'meta': {'key': ['value1', 'value2']},
'columns': [
{
'name': 'x',
'description': 'x description',
'docs': {'show': True},
'tests': [],
'meta': {'key2': 'value3'},
'tags': [],
},
{
'name': 'y',
'description': 'y description',
'docs': {'show': True},
'tests': [
'unique',
{'accepted_values': {'values': ['blue', 'green']}}
],
'meta': {},
'tags': ['a', 'b'],
},
],
'docs': {'show': False},
'config': {},
}
self.assert_symmetric(update, dct)
pickle.loads(pickle.dumps(update))
def test_bad_test_type(self):
dct = {
'name': 'foo',
'yaml_key': 'models',
'original_file_path': '/some/fake/path',
'package_name': 'test',
'description': 'a description',
'tests': ['table_test'],
'meta': {'key': ['value1', 'value2']},
'columns': [
{
'name': 'x',
'description': 'x description',
'docs': {'show': True},
'tests': [],
'meta': {'key2': 'value3'},
},
{
'name': 'y',
'description': 'y description',
'docs': {'show': True},
'tests': [
100,
{'accepted_values': {'values': ['blue', 'green']}}
],
'meta': {},
'yaml_key': 'models',
'original_file_path': '/some/fake/path',
},
],
'docs': {'show': True},
}
self.assert_fails_validation(dct)
dct = {
'name': 'foo',
'yaml_key': 'models',
'original_file_path': '/some/fake/path',
'package_name': 'test',
'description': 'a description',
'tests': ['table_test'],
'meta': {'key': ['value1', 'value2']},
'columns': [
# column missing a name
{
'description': 'x description',
'docs': {'show': True},
'tests': [],
'meta': {'key2': 'value3'},
},
{
'name': 'y',
'description': 'y description',
'docs': {'show': True},
'tests': [
'unique',
{'accepted_values': {'values': ['blue', 'green']}}
],
'meta': {},
'yaml_key': 'models',
'original_file_path': '/some/fake/path',
},
],
'docs': {'show': True},
}
self.assert_fails_validation(dct)
# missing a name
dct = {
'yaml_key': 'models',
'original_file_path': '/some/fake/path',
'package_name': 'test',
'description': 'a description',
'tests': ['table_test'],
'meta': {'key': ['value1', 'value2']},
'columns': [
{
'name': 'x',
'description': 'x description',
'docs': {'show': True},
'tests': [],
'meta': {'key2': 'value3'},
},
{
'name': 'y',
'description': 'y description',
'docs': {'show': True},
'tests': [
'unique',
{'accepted_values': {'values': ['blue', 'green']}}
],
'meta': {},
'yaml_key': 'models',
'original_file_path': '/some/fake/path',
},
],
'docs': {'show': True},
}
self.assert_fails_validation(dct)
class TestUnparsedExposure(ContractTestCase):
ContractType = UnparsedExposure
def get_ok_dict(self):
return {
'name': 'my_exposure',
'type': 'dashboard',
'owner': {
'email': 'name@example.com',
},
'maturity': 'medium',
'meta': {'tool': 'my_tool'},
'tags': ['my_department'],
'url': 'https://example.com/dashboards/1',
'description': 'A exposure',
'depends_on': [
'ref("my_model")',
'source("raw", "source_table")',
],
}
def test_ok(self):
exposure = self.ContractType(
name='my_exposure',
type=ExposureType.Dashboard,
owner=ExposureOwner(email='name@example.com'),
maturity=MaturityType.Medium,
url='https://example.com/dashboards/1',
description='A exposure',
meta={'tool': 'my_tool'},
tags=['my_department'],
depends_on=['ref("my_model")', 'source("raw", "source_table")'],
)
dct = self.get_ok_dict()
self.assert_symmetric(exposure, dct)
pickle.loads(pickle.dumps(exposure))
def test_ok_exposures(self):
for exposure_allowed in ('dashboard', 'notebook', 'analysis', 'ml', 'application'):
tst = self.get_ok_dict()
tst['type'] = exposure_allowed
assert self.ContractType.from_dict(tst).type == exposure_allowed
def test_bad_exposure(self):
# bad exposure: None isn't allowed
for exposure_not_allowed in (None, 'not an exposure'):
tst = self.get_ok_dict()
tst['type'] = exposure_not_allowed
self.assert_fails_validation(tst)
def test_no_exposure(self):
tst = self.get_ok_dict()
del tst['type']
self.assert_fails_validation(tst)
def test_ok_maturities(self):
for maturity_allowed in (None, 'low', 'medium', 'high'):
tst = self.get_ok_dict()
tst['maturity'] = maturity_allowed
assert self.ContractType.from_dict(
tst).maturity == maturity_allowed
tst = self.get_ok_dict()
del tst['maturity']
assert self.ContractType.from_dict(tst).maturity is None
def test_bad_maturity(self):
tst = self.get_ok_dict()
tst['maturity'] = 'invalid maturity'
self.assert_fails_validation(tst)
def test_bad_owner_missing_things(self):
tst = self.get_ok_dict()
del tst['owner']['email']
self.assert_fails_validation(tst)
del tst['owner']
self.assert_fails_validation(tst)
def test_bad_tags(self):
tst = self.get_ok_dict()
tst['tags'] = [123]
self.assert_fails_validation(tst)
class TestUnparsedMetric(ContractTestCase):
ContractType = UnparsedMetric
def get_ok_dict(self):
return {
'name': 'new_customers',
'label': 'New Customers',
'model': 'ref("dim_customers")',
'description': 'New customers',
'type': 'count',
'sql': 'user_id',
'timestamp': 'signup_date',
'time_grains': ['day', 'week', 'month'],
'dimensions': ['plan', 'country'],
'filters': [
{
"field": "is_paying",
"value": "True",
"operator": "=",
}
],
'tags': [],
'meta': {
'is_okr': True
},
}
def test_ok(self):
metric = self.ContractType(
name='new_customers',
label='New Customers',
model='ref("dim_customers")',
description="New customers",
type='count',
sql="user_id",
timestamp="signup_date",
time_grains=['day', 'week', 'month'],
dimensions=['plan', 'country'],
filters=[MetricFilter(
field="is_paying",
value='True',
operator="=",
)],
meta={'is_okr': True},
)
dct = self.get_ok_dict()
self.assert_symmetric(metric, dct)
pickle.loads(pickle.dumps(metric))
def test_bad_metric_no_type(self):
tst = self.get_ok_dict()
del tst['type']
self.assert_fails_validation(tst)
def test_bad_metric_no_model(self):
tst = self.get_ok_dict()
tst['model'] = None
self.assert_fails_validation(tst)
def test_bad_filter_missing_things(self):
tst = self.get_ok_dict()
del tst['filters'][0]['operator']
self.assert_fails_validation(tst)
def test_bad_tags(self):
tst = self.get_ok_dict()
tst['tags'] = [123]
self.assert_fails_validation(tst)