-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
test_graph_selector_methods.py
1213 lines (1011 loc) · 43.6 KB
/
test_graph_selector_methods.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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import copy
import pytest
from unittest import mock
from pathlib import Path
from dbt.contracts.files import FileHash
from dbt.contracts.graph.nodes import (
DependsOn,
MacroDependsOn,
NodeConfig,
Macro,
ModelNode,
Exposure,
Metric,
SeedNode,
SingularTestNode,
GenericTestNode,
SourceDefinition,
TestConfig,
TestMetadata,
ColumnInfo,
)
from dbt.contracts.graph.manifest import Manifest
from dbt.contracts.graph.unparsed import ExposureType, ExposureOwner, MetricFilter,MetricTime
from dbt.contracts.state import PreviousState
from dbt.node_types import NodeType
from dbt.graph.selector_methods import (
MethodManager,
QualifiedNameSelectorMethod,
WildcardSelectorMethod,
TagSelectorMethod,
SourceSelectorMethod,
PathSelectorMethod,
FileSelectorMethod,
PackageSelectorMethod,
ConfigSelectorMethod,
TestNameSelectorMethod,
TestTypeSelectorMethod,
StateSelectorMethod,
ExposureSelectorMethod,
MetricSelectorMethod,
)
import dbt.exceptions
import dbt.contracts.graph.nodes
from .utils import replace_config
def make_model(pkg, name, sql, refs=None, sources=None, tags=None, path=None, alias=None, config_kwargs=None, fqn_extras=None, depends_on_macros=None):
if refs is None:
refs = []
if sources is None:
sources = []
if tags is None:
tags = []
if path is None:
path = f'{name}.sql'
if alias is None:
alias = name
if config_kwargs is None:
config_kwargs = {}
if depends_on_macros is None:
depends_on_macros = []
if fqn_extras is None:
fqn_extras = []
fqn = [pkg] + fqn_extras + [name]
depends_on_nodes = []
source_values = []
ref_values = []
for ref in refs:
ref_values.append([ref.name])
depends_on_nodes.append(ref.unique_id)
for src in sources:
source_values.append([src.source_name, src.name])
depends_on_nodes.append(src.unique_id)
return ModelNode(
language='sql',
raw_code=sql,
database='dbt',
schema='dbt_schema',
alias=alias,
name=name,
fqn=fqn,
unique_id=f'model.{pkg}.{name}',
package_name=pkg,
path=path,
original_file_path=f'models/{path}',
config=NodeConfig(**config_kwargs),
tags=tags,
refs=ref_values,
sources=source_values,
depends_on=DependsOn(
nodes=depends_on_nodes,
macros=depends_on_macros,
),
resource_type=NodeType.Model,
checksum=FileHash.from_contents(''),
)
def make_seed(pkg, name, path=None, loader=None, alias=None, tags=None, fqn_extras=None, checksum=None):
if alias is None:
alias = name
if tags is None:
tags = []
if path is None:
path = f'{name}.csv'
if fqn_extras is None:
fqn_extras = []
if checksum is None:
checksum = FileHash.from_contents('')
fqn = [pkg] + fqn_extras + [name]
return SeedNode(
database='dbt',
schema='dbt_schema',
alias=alias,
name=name,
fqn=fqn,
unique_id=f'seed.{pkg}.{name}',
package_name=pkg,
path=path,
original_file_path=f'data/{path}',
tags=tags,
resource_type=NodeType.Seed,
checksum=FileHash.from_contents(''),
)
def make_source(pkg, source_name, table_name, path=None, loader=None, identifier=None, fqn_extras=None):
if path is None:
path = 'models/schema.yml'
if loader is None:
loader = 'my_loader'
if identifier is None:
identifier = table_name
if fqn_extras is None:
fqn_extras = []
fqn = [pkg] + fqn_extras + [source_name, table_name]
return SourceDefinition(
fqn=fqn,
database='dbt',
schema='dbt_schema',
unique_id=f'source.{pkg}.{source_name}.{table_name}',
package_name=pkg,
path=path,
original_file_path=path,
name=table_name,
source_name=source_name,
loader='my_loader',
identifier=identifier,
resource_type=NodeType.Source,
loaded_at_field='loaded_at',
tags=[],
source_description='',
)
def make_macro(pkg, name, macro_sql, path=None, depends_on_macros=None):
if path is None:
path = 'macros/macros.sql'
if depends_on_macros is None:
depends_on_macros = []
return Macro(
name=name,
macro_sql=macro_sql,
unique_id=f'macro.{pkg}.{name}',
package_name=pkg,
path=path,
original_file_path=path,
resource_type=NodeType.Macro,
depends_on=MacroDependsOn(macros=depends_on_macros),
)
def make_unique_test(pkg, test_model, column_name, path=None, refs=None, sources=None, tags=None):
return make_schema_test(pkg, 'unique', test_model, {}, column_name=column_name)
def make_not_null_test(pkg, test_model, column_name, path=None, refs=None, sources=None, tags=None):
return make_schema_test(pkg, 'not_null', test_model, {}, column_name=column_name)
def make_schema_test(pkg, test_name, test_model, test_kwargs, path=None, refs=None, sources=None, tags=None, column_name=None):
kwargs = test_kwargs.copy()
ref_values = []
source_values = []
# this doesn't really have to be correct
if isinstance(test_model, SourceDefinition):
kwargs['model'] = "{{ source('" + test_model.source_name + \
"', '" + test_model.name + "') }}"
source_values.append([test_model.source_name, test_model.name])
else:
kwargs['model'] = "{{ ref('" + test_model.name + "')}}"
ref_values.append([test_model.name])
if column_name is not None:
kwargs['column_name'] = column_name
# whatever
args_name = test_model.search_name.replace(".", "_")
if column_name is not None:
args_name += '_' + column_name
node_name = f'{test_name}_{args_name}'
raw_code = '{{ config(severity="ERROR") }}{{ test_' + \
test_name + '(**dbt_schema_test_kwargs) }}'
name_parts = test_name.split('.')
if len(name_parts) == 2:
namespace, test_name = name_parts
macro_depends = f'macro.{namespace}.test_{test_name}'
elif len(name_parts) == 1:
namespace = None
macro_depends = f'macro.dbt.test_{test_name}'
else:
assert False, f'invalid test name: {test_name}'
if path is None:
path = 'schema.yml'
if tags is None:
tags = ['schema']
if refs is None:
refs = []
if sources is None:
sources = []
depends_on_nodes = []
for ref in refs:
ref_values.append([ref.name])
depends_on_nodes.append(ref.unique_id)
for source in sources:
source_values.append([source.source_name, source.name])
depends_on_nodes.append(source.unique_id)
return GenericTestNode(
language='sql',
raw_code=raw_code,
test_metadata=TestMetadata(
namespace=namespace,
name=test_name,
kwargs=kwargs,
),
database='dbt',
schema='dbt_postgres',
name=node_name,
alias=node_name,
fqn=['minimal', 'schema_test', node_name],
unique_id=f'test.{pkg}.{node_name}',
package_name=pkg,
path=f'schema_test/{node_name}.sql',
original_file_path=f'models/{path}',
resource_type=NodeType.Test,
tags=tags,
refs=ref_values,
sources=[],
depends_on=DependsOn(
macros=[macro_depends],
nodes=depends_on_nodes
),
column_name=column_name,
checksum=FileHash.from_contents(''),
)
def make_data_test(pkg, name, sql, refs=None, sources=None, tags=None, path=None, config_kwargs=None):
if refs is None:
refs = []
if sources is None:
sources = []
if tags is None:
tags = ['data']
if path is None:
path = f'{name}.sql'
if config_kwargs is None:
config_kwargs = {}
fqn = ['minimal', 'data_test', name]
depends_on_nodes = []
source_values = []
ref_values = []
for ref in refs:
ref_values.append([ref.name])
depends_on_nodes.append(ref.unique_id)
for src in sources:
source_values.append([src.source_name, src.name])
depends_on_nodes.append(src.unique_id)
return SingularTestNode(
language='sql',
raw_code=sql,
database='dbt',
schema='dbt_schema',
name=name,
alias=name,
fqn=fqn,
unique_id=f'test.{pkg}.{name}',
package_name=pkg,
path=path,
original_file_path=f'tests/{path}',
config=TestConfig(**config_kwargs),
tags=tags,
refs=ref_values,
sources=source_values,
depends_on=DependsOn(nodes=depends_on_nodes, macros=[]),
resource_type=NodeType.Test,
checksum=FileHash.from_contents(''),
)
def make_exposure(pkg, name, path=None, fqn_extras=None, owner=None):
if path is None:
path = 'schema.yml'
if fqn_extras is None:
fqn_extras = []
if owner is None:
owner = ExposureOwner(email='test@example.com')
fqn = [pkg, 'exposures'] + fqn_extras + [name]
return Exposure(
name=name,
resource_type=NodeType.Exposure,
type=ExposureType.Notebook,
fqn=fqn,
unique_id=f'exposure.{pkg}.{name}',
package_name=pkg,
path=path,
original_file_path=path,
owner=owner,
)
def make_metric(pkg, name, path=None):
if path is None:
path = 'schema.yml'
return Metric(
name=name,
resource_type=NodeType.Metric,
path='schema.yml',
package_name=pkg,
original_file_path=path,
unique_id=f'metric.{pkg}.{name}',
fqn=[pkg, 'metrics', name],
label='New Customers',
model='ref("multi")',
description="New customers",
calculation_method='count',
expression="user_id",
timestamp="signup_date",
time_grains=['day', 'week', 'month'],
dimensions=['plan', 'country'],
filters=[MetricFilter(
field="is_paying",
value=True,
operator="=",
)],
window=MetricTime(),
meta={'is_okr': True},
tags=['okrs'],
)
@pytest.fixture
def macro_test_unique():
return make_macro(
'dbt',
'test_unique',
'blablabla',
depends_on_macros=['macro.dbt.default__test_unique']
)
@pytest.fixture
def macro_default_test_unique():
return make_macro(
'dbt',
'default__test_unique',
'blablabla'
)
@pytest.fixture
def macro_test_not_null():
return make_macro(
'dbt',
'test_not_null',
'blablabla',
depends_on_macros=['macro.dbt.default__test_not_null']
)
@pytest.fixture
def macro_default_test_not_null():
return make_macro(
'dbt',
'default__test_not_null',
'blabla'
)
@pytest.fixture
def seed():
return make_seed(
'pkg',
'seed'
)
@pytest.fixture
def source():
return make_source(
'pkg',
'raw',
'seed',
identifier='seed'
)
@pytest.fixture
def ephemeral_model(source):
return make_model(
'pkg',
'ephemeral_model',
'select * from {{ source("raw", "seed") }}',
config_kwargs={'materialized': 'ephemeral'},
sources=[source],
)
@pytest.fixture
def view_model(ephemeral_model):
return make_model(
'pkg',
'view_model',
'select * from {{ ref("ephemeral_model") }}',
config_kwargs={'materialized': 'view'},
refs=[ephemeral_model],
tags=['uses_ephemeral'],
)
@pytest.fixture
def table_model(ephemeral_model):
return make_model(
'pkg',
'table_model',
'select * from {{ ref("ephemeral_model") }}',
config_kwargs={
'materialized': 'table',
'meta': {
# Other properties to test in test_select_config_meta
'string_property': 'some_string',
'truthy_bool_property': True,
'falsy_bool_property': False,
'list_property': ['some_value', True, False]
},
},
refs=[ephemeral_model],
tags=['uses_ephemeral'],
path='subdirectory/table_model.sql'
)
@pytest.fixture
def table_model_py(seed):
return make_model(
'pkg',
'table_model_py',
'select * from {{ ref("seed") }}',
config_kwargs={'materialized': 'table'},
refs=[seed],
tags=[],
path='subdirectory/table_model.py'
)
@pytest.fixture
def table_model_csv(seed):
return make_model(
'pkg',
'table_model_csv',
'select * from {{ ref("seed") }}',
config_kwargs={'materialized': 'table'},
refs=[seed],
tags=[],
path='subdirectory/table_model.csv'
)
@pytest.fixture
def ext_source():
return make_source(
'ext',
'ext_raw',
'ext_source',
)
@pytest.fixture
def ext_source_2():
return make_source(
'ext',
'ext_raw',
'ext_source_2',
)
@pytest.fixture
def ext_source_other():
return make_source(
'ext',
'raw',
'ext_source',
)
@pytest.fixture
def ext_source_other_2():
return make_source(
'ext',
'raw',
'ext_source_2',
)
@pytest.fixture
def ext_model(ext_source):
return make_model(
'ext',
'ext_model',
'select * from {{ source("ext_raw", "ext_source") }}',
sources=[ext_source],
)
@pytest.fixture
def union_model(seed, ext_source):
return make_model(
'pkg',
'union_model',
'select * from {{ ref("seed") }} union all select * from {{ source("ext_raw", "ext_source") }}',
config_kwargs={'materialized': 'table'},
refs=[seed],
sources=[ext_source],
fqn_extras=['unions'],
path='subdirectory/union_model.sql',
tags=['unions'],
)
@pytest.fixture
def table_id_unique(table_model):
return make_unique_test('pkg', table_model, 'id')
@pytest.fixture
def table_id_not_null(table_model):
return make_not_null_test('pkg', table_model, 'id')
@pytest.fixture
def view_id_unique(view_model):
return make_unique_test('pkg', view_model, 'id')
@pytest.fixture
def ext_source_id_unique(ext_source):
return make_unique_test('ext', ext_source, 'id')
@pytest.fixture
def view_test_nothing(view_model):
return make_data_test('pkg', 'view_test_nothing', 'select * from {{ ref("view_model") }} limit 0', refs=[view_model])
# Support dots as namespace separators
@pytest.fixture
def namespaced_seed():
return make_seed(
'pkg',
'mynamespace.seed'
)
@pytest.fixture
def namespace_model(source):
return make_model(
'pkg',
'mynamespace.ephemeral_model',
'select * from {{ source("raw", "seed") }}',
config_kwargs={'materialized': 'ephemeral'},
sources=[source],
)
@pytest.fixture
def namespaced_union_model(seed, ext_source):
return make_model(
'pkg',
'mynamespace.union_model',
'select * from {{ ref("mynamespace.seed") }} union all select * from {{ ref("mynamespace.ephemeral_model") }}',
config_kwargs={'materialized': 'table'},
refs=[seed],
sources=[ext_source],
fqn_extras=['unions'],
path='subdirectory/union_model.sql',
tags=['unions'],
)
@pytest.fixture
def manifest(seed, source, ephemeral_model, view_model, table_model, table_model_py, table_model_csv, ext_source, ext_model, union_model, ext_source_2,
ext_source_other, ext_source_other_2, table_id_unique, table_id_not_null, view_id_unique, ext_source_id_unique,
view_test_nothing, namespaced_seed, namespace_model, namespaced_union_model, macro_test_unique, macro_default_test_unique,
macro_test_not_null, macro_default_test_not_null):
nodes = [seed, ephemeral_model, view_model, table_model, table_model_py, table_model_csv, union_model, ext_model,
table_id_unique, table_id_not_null, view_id_unique, ext_source_id_unique, view_test_nothing,
namespaced_seed, namespace_model, namespaced_union_model]
sources = [source, ext_source, ext_source_2,
ext_source_other, ext_source_other_2]
macros = [macro_test_unique, macro_default_test_unique, macro_test_not_null, macro_default_test_not_null]
manifest = Manifest(
nodes={n.unique_id: n for n in nodes},
sources={s.unique_id: s for s in sources},
macros={m.unique_id: m for m in macros},
docs={},
files={},
exposures={},
metrics={},
disabled=[],
selectors={},
)
return manifest
def search_manifest_using_method(manifest, method, selection):
selected = method.search(set(manifest.nodes) | set(
manifest.sources) | set(manifest.exposures) | set(manifest.metrics), selection)
results = {manifest.expect(uid).search_name for uid in selected}
return results
def test_select_fqn(manifest):
methods = MethodManager(manifest, None)
method = methods.get_method('fqn', [])
assert isinstance(method, QualifiedNameSelectorMethod)
assert method.arguments == []
assert search_manifest_using_method(
manifest, method, 'pkg.unions') == {'union_model', 'mynamespace.union_model'}
assert not search_manifest_using_method(manifest, method, 'ext.unions')
# sources don't show up, because selection pretends they have no FQN. Should it?
assert search_manifest_using_method(manifest, method, 'pkg') == {
'union_model', 'table_model', 'table_model_py', 'table_model_csv', 'view_model', 'ephemeral_model', 'seed',
'mynamespace.union_model', 'mynamespace.ephemeral_model', 'mynamespace.seed'}
assert search_manifest_using_method(
manifest, method, 'ext') == {'ext_model'}
def test_select_wildcard(manifest):
methods = MethodManager(manifest, None)
# TODO: this is just here so the PR reviewers can see the parity between the two methods
# we may merge these test cases later or rename this to test_select_advanced_fqn
method = methods.get_method('fqn', [])
assert isinstance(method, (QualifiedNameSelectorMethod, WildcardSelectorMethod))
assert method.arguments == []
# useful example
assert search_manifest_using_method(manifest, method, '*.*.*_model') == {
'mynamespace.union_model', 'mynamespace.ephemeral_model', 'union_model'}
# multiple wildcards
assert search_manifest_using_method(
manifest, method, '*unions*') == {'union_model', 'mynamespace.union_model'}
# negation
assert not search_manifest_using_method(manifest, method, '!pkg*')
# single wildcard
assert search_manifest_using_method(manifest, method, 'pkg.t*') == {
'table_model', 'table_model_py', 'table_model_csv'}
# wildcard and ? (matches exactly one character)
assert search_manifest_using_method(
manifest, method, '*ext_m?del') == {'ext_model'}
# multiple ?
assert search_manifest_using_method(manifest, method, '*.?????_model') == {
'union_model', 'table_model', 'mynamespace.union_model'}
# multiple ranges
assert search_manifest_using_method(manifest, method, '*.[t-u][a-n][b-i][l-o][e-n]_model') == {
'union_model', 'table_model', 'mynamespace.union_model'}
def test_select_tag(manifest):
methods = MethodManager(manifest, None)
method = methods.get_method('tag', [])
assert isinstance(method, TagSelectorMethod)
assert method.arguments == []
assert search_manifest_using_method(manifest, method, 'uses_ephemeral') == {
'view_model', 'table_model'}
assert not search_manifest_using_method(manifest, method, 'missing')
def test_select_source(manifest):
methods = MethodManager(manifest, None)
method = methods.get_method('source', [])
assert isinstance(method, SourceSelectorMethod)
assert method.arguments == []
# the lookup is based on how many components you provide: source, source.table, package.source.table
assert search_manifest_using_method(manifest, method, 'raw') == {
'raw.seed', 'raw.ext_source', 'raw.ext_source_2'}
assert search_manifest_using_method(
manifest, method, 'raw.seed') == {'raw.seed'}
assert search_manifest_using_method(
manifest, method, 'pkg.raw.seed') == {'raw.seed'}
assert search_manifest_using_method(
manifest, method, 'pkg.*.*') == {'raw.seed'}
assert search_manifest_using_method(
manifest, method, 'raw.*') == {'raw.seed', 'raw.ext_source', 'raw.ext_source_2'}
assert search_manifest_using_method(
manifest, method, 'ext.raw.*') == {'raw.ext_source', 'raw.ext_source_2'}
assert not search_manifest_using_method(manifest, method, 'missing')
assert not search_manifest_using_method(manifest, method, 'raw.missing')
assert not search_manifest_using_method(
manifest, method, 'missing.raw.seed')
assert search_manifest_using_method(manifest, method, 'ext.*.*') == {
'ext_raw.ext_source', 'ext_raw.ext_source_2', 'raw.ext_source', 'raw.ext_source_2'}
assert search_manifest_using_method(manifest, method, 'ext_raw') == {
'ext_raw.ext_source', 'ext_raw.ext_source_2'}
assert search_manifest_using_method(
manifest, method, 'ext.ext_raw.*') == {'ext_raw.ext_source', 'ext_raw.ext_source_2'}
assert not search_manifest_using_method(manifest, method, 'pkg.ext_raw.*')
# TODO: this requires writing out files
@pytest.mark.skip('TODO: write manifest files to disk')
def test_select_path(manifest):
methods = MethodManager(manifest, None)
method = methods.get_method('path', [])
assert isinstance(method, PathSelectorMethod)
assert method.arguments == []
assert search_manifest_using_method(
manifest, method, 'subdirectory/*.sql') == {'union_model', 'table_model'}
assert search_manifest_using_method(
manifest, method, 'subdirectory/union_model.sql') == {'union_model'}
assert search_manifest_using_method(
manifest, method, 'models/*.sql') == {'view_model', 'ephemeral_model'}
assert not search_manifest_using_method(manifest, method, 'missing')
assert not search_manifest_using_method(
manifest, method, 'models/missing.sql')
assert not search_manifest_using_method(
manifest, method, 'models/missing*')
def test_select_file(manifest):
methods = MethodManager(manifest, None)
method = methods.get_method('file', [])
assert isinstance(method, FileSelectorMethod)
assert method.arguments == []
assert search_manifest_using_method(
manifest, method, 'table_model.sql') == {'table_model'}
assert search_manifest_using_method(
manifest, method, 'table_model.py') == {'table_model_py'}
assert search_manifest_using_method(
manifest, method, 'table_model.csv') == {'table_model_csv'}
assert search_manifest_using_method(
manifest, method, 'union_model.sql') == {'union_model', 'mynamespace.union_model'}
assert not search_manifest_using_method(
manifest, method, 'missing.sql')
assert not search_manifest_using_method(
manifest, method, 'missing.py')
def test_select_package(manifest):
methods = MethodManager(manifest, None)
method = methods.get_method('package', [])
assert isinstance(method, PackageSelectorMethod)
assert method.arguments == []
assert search_manifest_using_method(manifest, method, 'pkg') == {'union_model', 'table_model', 'table_model_py', 'table_model_csv', 'view_model', 'ephemeral_model',
'seed', 'raw.seed', 'unique_table_model_id', 'not_null_table_model_id', 'unique_view_model_id', 'view_test_nothing',
'mynamespace.seed', 'mynamespace.ephemeral_model', 'mynamespace.union_model',
}
assert search_manifest_using_method(manifest, method, 'ext') == {
'ext_model', 'ext_raw.ext_source', 'ext_raw.ext_source_2', 'raw.ext_source', 'raw.ext_source_2', 'unique_ext_raw_ext_source_id'}
assert not search_manifest_using_method(manifest, method, 'missing')
def test_select_config_materialized(manifest):
methods = MethodManager(manifest, None)
method = methods.get_method('config', ['materialized'])
assert isinstance(method, ConfigSelectorMethod)
assert method.arguments == ['materialized']
assert search_manifest_using_method(manifest, method, 'view') == {
'view_model', 'ext_model'}
assert search_manifest_using_method(manifest, method, 'table') == {
'table_model', 'table_model_py', 'table_model_csv', 'union_model', 'mynamespace.union_model'}
def test_select_config_meta(manifest):
methods = MethodManager(manifest, None)
string_method = methods.get_method('config', ['meta', 'string_property'])
assert search_manifest_using_method(manifest, string_method, 'some_string') == {'table_model'}
assert not search_manifest_using_method(manifest, string_method, 'other_string') == {'table_model'}
truthy_bool_method = methods.get_method('config', ['meta', 'truthy_bool_property'])
assert search_manifest_using_method(manifest, truthy_bool_method, 'true') == {'table_model'}
assert not search_manifest_using_method(manifest, truthy_bool_method, 'false') == {'table_model'}
assert not search_manifest_using_method(manifest, truthy_bool_method, 'other') == {'table_model'}
falsy_bool_method = methods.get_method('config', ['meta', 'falsy_bool_property'])
assert search_manifest_using_method(manifest, falsy_bool_method, 'false') == {'table_model'}
assert not search_manifest_using_method(manifest, falsy_bool_method, 'true') == {'table_model'}
assert not search_manifest_using_method(manifest, falsy_bool_method, 'other') == {'table_model'}
list_method = methods.get_method('config', ['meta', 'list_property'])
assert search_manifest_using_method(manifest, list_method, 'some_value') == {'table_model'}
assert search_manifest_using_method(manifest, list_method, 'true') == {'table_model'}
assert search_manifest_using_method(manifest, list_method, 'false') == {'table_model'}
assert not search_manifest_using_method(manifest, list_method, 'other') == {'table_model'}
def test_select_test_name(manifest):
methods = MethodManager(manifest, None)
method = methods.get_method('test_name', [])
assert isinstance(method, TestNameSelectorMethod)
assert method.arguments == []
assert search_manifest_using_method(manifest, method, 'unique') == {
'unique_table_model_id', 'unique_view_model_id', 'unique_ext_raw_ext_source_id'}
assert search_manifest_using_method(manifest, method, 'not_null') == {
'not_null_table_model_id'}
assert not search_manifest_using_method(manifest, method, 'notatest')
def test_select_test_type(manifest):
methods = MethodManager(manifest, None)
method = methods.get_method('test_type', [])
assert isinstance(method, TestTypeSelectorMethod)
assert method.arguments == []
assert search_manifest_using_method(manifest, method, 'generic') == {
'unique_table_model_id', 'not_null_table_model_id', 'unique_view_model_id', 'unique_ext_raw_ext_source_id'}
assert search_manifest_using_method(manifest, method, 'singular') == {
'view_test_nothing'}
# test backwards compatibility
assert search_manifest_using_method(manifest, method, 'schema') == {
'unique_table_model_id', 'not_null_table_model_id', 'unique_view_model_id', 'unique_ext_raw_ext_source_id'}
assert search_manifest_using_method(manifest, method, 'data') == {
'view_test_nothing'}
def test_select_exposure(manifest):
exposure = make_exposure('test', 'my_exposure')
manifest.exposures[exposure.unique_id] = exposure
methods = MethodManager(manifest, None)
method = methods.get_method('exposure', [])
assert isinstance(method, ExposureSelectorMethod)
assert search_manifest_using_method(
manifest, method, 'my_exposure') == {'my_exposure'}
assert not search_manifest_using_method(
manifest, method, 'not_my_exposure')
def test_select_metric(manifest):
metric = make_metric('test', 'my_metric')
manifest.metrics[metric.unique_id] = metric
methods = MethodManager(manifest, None)
method = methods.get_method('metric', [])
assert isinstance(method, MetricSelectorMethod)
assert search_manifest_using_method(
manifest, method, 'my_metric') == {'my_metric'}
assert not search_manifest_using_method(
manifest, method, 'not_my_metric')
@pytest.fixture
def previous_state(manifest):
writable = copy.deepcopy(manifest).writable_manifest()
state = PreviousState(path=Path('/path/does/not/exist'),current_path=Path('/path/does/not/exist'))
state.manifest = writable
return state
def add_node(manifest, node):
manifest.nodes[node.unique_id] = node
def add_macro(manifest, macro):
manifest.macros[macro.unique_id] = macro
def change_node(manifest, node, change=None):
if change is not None:
node = change(node)
manifest.nodes[node.unique_id] = node
def statemethod(manifest, previous_state):
methods = MethodManager(manifest, previous_state)
method = methods.get_method('state', [])
assert isinstance(method, StateSelectorMethod)
assert method.arguments == []
return method
def test_select_state_no_change(manifest, previous_state):
method = statemethod(manifest, previous_state)
assert not search_manifest_using_method(manifest, method, 'modified')
assert not search_manifest_using_method(manifest, method, 'new')
assert not search_manifest_using_method(manifest, method, 'new')
assert not search_manifest_using_method(manifest, method, 'modified.configs')
assert not search_manifest_using_method(manifest, method, 'modified.persisted_descriptions')
assert not search_manifest_using_method(manifest, method, 'modified.relation')
assert not search_manifest_using_method(manifest, method, 'modified.macros')
def test_select_state_nothing(manifest, previous_state):
previous_state.manifest = None
method = statemethod(manifest, previous_state)
with pytest.raises(dbt.exceptions.DbtRuntimeError) as exc:
search_manifest_using_method(manifest, method, 'modified')
assert 'no comparison manifest' in str(exc.value)
with pytest.raises(dbt.exceptions.DbtRuntimeError) as exc:
search_manifest_using_method(manifest, method, 'new')
assert 'no comparison manifest' in str(exc.value)
def test_select_state_added_model(manifest, previous_state):
add_node(manifest, make_model('pkg', 'another_model', 'select 1 as id'))
method = statemethod(manifest, previous_state)
assert search_manifest_using_method(
manifest, method, 'modified') == {'another_model'}
assert search_manifest_using_method(
manifest, method, 'new') == {'another_model'}
assert search_manifest_using_method(
manifest, method, 'modified.body') == {'another_model'}
def test_select_state_changed_model_sql(manifest, previous_state, view_model):
change_node(manifest, view_model.replace(raw_code='select 1 as id'))
method = statemethod(manifest, previous_state)
# both of these
assert search_manifest_using_method(
manifest, method, 'modified') == {'view_model'}
assert search_manifest_using_method(
manifest, method, 'modified.body') == {'view_model'}
# none of these
assert not search_manifest_using_method(manifest, method, 'new')
assert not search_manifest_using_method(manifest, method, 'modified.configs')
assert not search_manifest_using_method(manifest, method, 'modified.persisted_descriptions')
assert not search_manifest_using_method(manifest, method, 'modified.relation')
assert not search_manifest_using_method(manifest, method, 'modified.macros')
def test_select_state_changed_model_fqn(manifest, previous_state, view_model):
change_node(manifest, view_model.replace(
fqn=view_model.fqn[:-1]+['nested']+view_model.fqn[-1:]))
method = statemethod(manifest, previous_state)
assert search_manifest_using_method(
manifest, method, 'modified') == {'view_model'}
assert not search_manifest_using_method(manifest, method, 'new')
def test_select_state_added_seed(manifest, previous_state):
add_node(manifest, make_seed('pkg', 'another_seed'))
method = statemethod(manifest, previous_state)
assert search_manifest_using_method(
manifest, method, 'modified') == {'another_seed'}
assert search_manifest_using_method(
manifest, method, 'new') == {'another_seed'}
def test_select_state_changed_seed_checksum_sha_to_sha(manifest, previous_state, seed):
change_node(manifest, seed.replace(
checksum=FileHash.from_contents('changed')))
method = statemethod(manifest, previous_state)
assert search_manifest_using_method(
manifest, method, 'modified') == {'seed'}
assert not search_manifest_using_method(manifest, method, 'new')
def test_select_state_changed_seed_checksum_path_to_path(manifest, previous_state, seed):
change_node(previous_state.manifest, seed.replace(
checksum=FileHash(name='path', checksum=seed.original_file_path)))
change_node(manifest, seed.replace(checksum=FileHash(