-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
test_config.py
1139 lines (1015 loc) · 45.2 KB
/
test_config.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
from copy import deepcopy
from contextlib import contextmanager
import json
import os
import shutil
import tempfile
import unittest
import mock
import yaml
import dbt.config
import dbt.exceptions
from dbt.adapters.postgres import PostgresCredentials
from dbt.adapters.redshift import RedshiftCredentials
from dbt.contracts.project import PackageConfig
from dbt.semver import VersionSpecifier
@contextmanager
def temp_cd(path):
current_path = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(current_path)
model_config = {
'my_package_name': {
'enabled': True,
'adwords': {
'adwords_ads': {
'materialized': 'table',
'enabled': True,
'schema': 'analytics'
}
},
'snowplow': {
'snowplow_sessions': {
'sort': 'timestamp',
'materialized': 'incremental',
'dist': 'user_id',
'sql_where': 'created_at > (select max(created_at) from {{ this }})',
'unique_key': 'id'
},
'base': {
'snowplow_events': {
'sort': ['timestamp', 'userid'],
'materialized': 'table',
'sort_type': 'interleaved',
'dist': 'userid'
}
}
}
}
}
model_fqns = frozenset((
('my_package_name', 'snowplow', 'snowplow_sessions'),
('my_package_name', 'snowplow', 'base', 'snowplow_events'),
('my_package_name', 'adwords', 'adwords_ads'),
))
class Args(object):
def __init__(self, profiles_dir=None, threads=None, profile=None,
cli_vars=None, version_check=None):
self.profile = profile
if threads is not None:
self.threads = threads
if profiles_dir is not None:
self.profiles_dir = profiles_dir
if cli_vars is not None:
self.vars = cli_vars
if version_check is not None:
self.version_check = version_check
class BaseConfigTest(unittest.TestCase):
"""Subclass this, and before calling the superclass setUp, set
profiles_dir.
"""
def setUp(self):
self.default_project_data = {
'version': '0.0.1',
'name': 'my_test_project',
'profile': 'default',
}
self.default_profile_data = {
'default': {
'outputs': {
'postgres': {
'type': 'postgres',
'host': 'postgres-db-hostname',
'port': 5555,
'user': 'db_user',
'pass': 'db_pass',
'dbname': 'postgres-db-name',
'schema': 'postgres-schema',
'threads': 7,
},
'redshift': {
'type': 'redshift',
'host': 'redshift-db-hostname',
'port': 5555,
'user': 'db_user',
'pass': 'db_pass',
'dbname': 'redshift-db-name',
'schema': 'redshift-schema',
},
'with-vars': {
'type': "{{ env_var('env_value_type') }}",
'host': "{{ env_var('env_value_host') }}",
'port': "{{ env_var('env_value_port') }}",
'user': "{{ env_var('env_value_user') }}",
'pass': "{{ env_var('env_value_pass') }}",
'dbname': "{{ env_var('env_value_dbname') }}",
'schema': "{{ env_var('env_value_schema') }}",
},
'cli-and-env-vars': {
'type': "{{ env_var('env_value_type') }}",
'host': "{{ var('cli_value_host') }}",
'port': "{{ env_var('env_value_port') }}",
'user': "{{ env_var('env_value_user') }}",
'pass': "{{ env_var('env_value_pass') }}",
'dbname': "{{ env_var('env_value_dbname') }}",
'schema': "{{ env_var('env_value_schema') }}",
}
},
'target': 'postgres',
},
'other': {
'outputs': {
'other-postgres': {
'type': 'postgres',
'host': 'other-postgres-db-hostname',
'port': 4444,
'user': 'other_db_user',
'pass': 'other_db_pass',
'dbname': 'other-postgres-db-name',
'schema': 'other-postgres-schema',
'threads': 2,
}
},
'target': 'other-postgres',
}
}
self.args = Args(profiles_dir=self.profiles_dir, cli_vars='{}',
version_check=True)
self.env_override = {
'env_value_type': 'postgres',
'env_value_host': 'env-postgres-host',
'env_value_port': '6543',
'env_value_user': 'env-postgres-user',
'env_value_pass': 'env-postgres-pass',
'env_value_dbname': 'env-postgres-dbname',
'env_value_schema': 'env-postgres-schema',
'env_value_project': 'blah',
}
class BaseFileTest(BaseConfigTest):
def setUp(self):
self.project_dir = os.path.normpath(tempfile.mkdtemp())
self.profiles_dir = os.path.normpath(tempfile.mkdtemp())
super(BaseFileTest, self).setUp()
def tearDown(self):
try:
shutil.rmtree(self.project_dir)
except EnvironmentError:
pass
try:
shutil.rmtree(self.profiles_dir)
except EnvironmentError:
pass
def proejct_path(self, name):
return os.path.join(self.project_dir, name)
def profile_path(self, name):
return os.path.join(self.profiles_dir, name)
def write_project(self, project_data=None):
if project_data is None:
project_data = self.project_data
with open(self.proejct_path('dbt_project.yml'), 'w') as fp:
yaml.dump(project_data, fp)
def write_packages(self, package_data):
with open(self.proejct_path('packages.yml'), 'w') as fp:
yaml.dump(package_data, fp)
def write_profile(self, profile_data=None):
if profile_data is None:
profile_data = self.profile_data
with open(self.profile_path('profiles.yml'), 'w') as fp:
yaml.dump(profile_data, fp)
class TestProfile(BaseConfigTest):
def setUp(self):
self.profiles_dir = '/invalid-path'
super(TestProfile, self).setUp()
def from_raw_profiles(self):
return dbt.config.Profile.from_raw_profiles(
self.default_profile_data, 'default', {}
)
def test_from_raw_profiles(self):
profile = self.from_raw_profiles()
self.assertEqual(profile.profile_name, 'default')
self.assertEqual(profile.target_name, 'postgres')
self.assertEqual(profile.threads, 7)
self.assertTrue(profile.config.send_anonymous_usage_stats)
self.assertTrue(profile.config.use_colors)
self.assertTrue(isinstance(profile.credentials, PostgresCredentials))
self.assertEqual(profile.credentials.type, 'postgres')
self.assertEqual(profile.credentials.host, 'postgres-db-hostname')
self.assertEqual(profile.credentials.port, 5555)
self.assertEqual(profile.credentials.user, 'db_user')
self.assertEqual(profile.credentials.password, 'db_pass')
self.assertEqual(profile.credentials.schema, 'postgres-schema')
self.assertEqual(profile.credentials.database, 'postgres-db-name')
def test_config_override(self):
self.default_profile_data['config'] = {
'send_anonymous_usage_stats': False,
'use_colors': False
}
profile = self.from_raw_profiles()
self.assertEqual(profile.profile_name, 'default')
self.assertEqual(profile.target_name, 'postgres')
self.assertFalse(profile.config.send_anonymous_usage_stats)
self.assertFalse(profile.config.use_colors)
def test_partial_config_override(self):
self.default_profile_data['config'] = {
'send_anonymous_usage_stats': False,
}
profile = self.from_raw_profiles()
self.assertEqual(profile.profile_name, 'default')
self.assertEqual(profile.target_name, 'postgres')
self.assertFalse(profile.config.send_anonymous_usage_stats)
self.assertTrue(profile.config.use_colors)
def test_missing_type(self):
del self.default_profile_data['default']['outputs']['postgres']['type']
with self.assertRaises(dbt.exceptions.DbtProfileError) as exc:
self.from_raw_profiles()
self.assertIn('type', str(exc.exception))
self.assertIn('postgres', str(exc.exception))
self.assertIn('default', str(exc.exception))
def test_bad_type(self):
self.default_profile_data['default']['outputs']['postgres']['type'] = 'invalid'
with self.assertRaises(dbt.exceptions.DbtProfileError) as exc:
self.from_raw_profiles()
self.assertIn('Credentials', str(exc.exception))
self.assertIn('postgres', str(exc.exception))
self.assertIn('default', str(exc.exception))
def test_invalid_credentials(self):
del self.default_profile_data['default']['outputs']['postgres']['host']
with self.assertRaises(dbt.exceptions.DbtProfileError) as exc:
self.from_raw_profiles()
self.assertIn('Credentials', str(exc.exception))
self.assertIn('postgres', str(exc.exception))
self.assertIn('default', str(exc.exception))
def test_missing_target(self):
profile = self.default_profile_data['default']
del profile['target']
profile['outputs']['default'] = profile['outputs']['postgres']
profile = self.from_raw_profiles()
self.assertEqual(profile.profile_name, 'default')
self.assertEqual(profile.target_name, 'default')
self.assertEqual(profile.credentials.type, 'postgres')
def test_profile_invalid_project(self):
with self.assertRaises(dbt.exceptions.DbtProjectError) as exc:
dbt.config.Profile.from_raw_profiles(
self.default_profile_data, 'invalid-profile', {}
)
self.assertEqual(exc.exception.result_type, 'invalid_project')
self.assertIn('Could not find', str(exc.exception))
self.assertIn('invalid-profile', str(exc.exception))
def test_profile_invalid_target(self):
with self.assertRaises(dbt.exceptions.DbtProfileError) as exc:
dbt.config.Profile.from_raw_profiles(
self.default_profile_data, 'default', {},
target_override='nope'
)
self.assertIn('nope', str(exc.exception))
self.assertIn('- postgres', str(exc.exception))
self.assertIn('- redshift', str(exc.exception))
self.assertIn('- with-vars', str(exc.exception))
def test_no_outputs(self):
with self.assertRaises(dbt.exceptions.DbtProfileError) as exc:
dbt.config.Profile.from_raw_profiles(
{'some-profile': {'target': 'blah'}}, 'some-profile', {}
)
self.assertIn('outputs not specified', str(exc.exception))
self.assertIn('some-profile', str(exc.exception))
def test_neq(self):
profile = self.from_raw_profiles()
self.assertNotEqual(profile, object())
def test_eq(self):
profile = dbt.config.Profile.from_raw_profiles(
deepcopy(self.default_profile_data), 'default', {}
)
other = dbt.config.Profile.from_raw_profiles(
deepcopy(self.default_profile_data), 'default', {}
)
self.assertEqual(profile, other)
def test_invalid_env_vars(self):
self.env_override['env_value_port'] = 'hello'
with mock.patch.dict(os.environ, self.env_override):
with self.assertRaises(dbt.exceptions.DbtProfileError) as exc:
dbt.config.Profile.from_raw_profile_info(
self.default_profile_data['default'],
'default',
{},
target_override='with-vars'
)
self.assertIn("not of type 'integer'", str(exc.exception))
class TestProfileFile(BaseFileTest):
def setUp(self):
super(TestProfileFile, self).setUp()
self.write_profile(self.default_profile_data)
def from_raw_profile_info(self, raw_profile=None, profile_name='default', **kwargs):
if raw_profile is None:
raw_profile = self.default_profile_data['default']
kw = {
'raw_profile': raw_profile,
'profile_name': profile_name,
'cli_vars': {},
}
kw.update(kwargs)
return dbt.config.Profile.from_raw_profile_info(**kw)
def from_args(self, project_profile_name='default', **kwargs):
kw = {
'args': self.args,
'project_profile_name': project_profile_name,
'cli_vars': {},
}
kw.update(kwargs)
return dbt.config.Profile.from_args(**kw)
def test_profile_simple(self):
profile = self.from_args()
from_raw = self.from_raw_profile_info()
self.assertEqual(profile.profile_name, 'default')
self.assertEqual(profile.target_name, 'postgres')
self.assertEqual(profile.threads, 7)
self.assertTrue(profile.config.send_anonymous_usage_stats)
self.assertTrue(profile.config.use_colors)
self.assertTrue(isinstance(profile.credentials, PostgresCredentials))
self.assertEqual(profile.credentials.type, 'postgres')
self.assertEqual(profile.credentials.host, 'postgres-db-hostname')
self.assertEqual(profile.credentials.port, 5555)
self.assertEqual(profile.credentials.user, 'db_user')
self.assertEqual(profile.credentials.password, 'db_pass')
self.assertEqual(profile.credentials.schema, 'postgres-schema')
self.assertEqual(profile.credentials.database, 'postgres-db-name')
self.assertEqual(profile, from_raw)
def test_profile_override(self):
self.args.profile = 'other'
self.args.threads = 3
profile = self.from_args()
from_raw = self.from_raw_profile_info(
self.default_profile_data['other'],
'other',
threads_override=3,
)
self.assertEqual(profile.profile_name, 'other')
self.assertEqual(profile.target_name, 'other-postgres')
self.assertEqual(profile.threads, 3)
self.assertTrue(profile.config.send_anonymous_usage_stats)
self.assertTrue(profile.config.use_colors)
self.assertTrue(isinstance(profile.credentials, PostgresCredentials))
self.assertEqual(profile.credentials.type, 'postgres')
self.assertEqual(profile.credentials.host, 'other-postgres-db-hostname')
self.assertEqual(profile.credentials.port, 4444)
self.assertEqual(profile.credentials.user, 'other_db_user')
self.assertEqual(profile.credentials.password, 'other_db_pass')
self.assertEqual(profile.credentials.schema, 'other-postgres-schema')
self.assertEqual(profile.credentials.database, 'other-postgres-db-name')
self.assertEqual(profile, from_raw)
def test_target_override(self):
self.args.target = 'redshift'
profile = self.from_args()
from_raw = self.from_raw_profile_info(
target_override='redshift'
)
self.assertEqual(profile.profile_name, 'default')
self.assertEqual(profile.target_name, 'redshift')
self.assertEqual(profile.threads, 1)
self.assertTrue(profile.config.send_anonymous_usage_stats)
self.assertTrue(profile.config.use_colors)
self.assertTrue(isinstance(profile.credentials, RedshiftCredentials))
self.assertEqual(profile.credentials.type, 'redshift')
self.assertEqual(profile.credentials.host, 'redshift-db-hostname')
self.assertEqual(profile.credentials.port, 5555)
self.assertEqual(profile.credentials.user, 'db_user')
self.assertEqual(profile.credentials.password, 'db_pass')
self.assertEqual(profile.credentials.schema, 'redshift-schema')
self.assertEqual(profile.credentials.database, 'redshift-db-name')
self.assertEqual(profile, from_raw)
def test_env_vars(self):
self.args.target = 'with-vars'
with mock.patch.dict(os.environ, self.env_override):
profile = self.from_args()
from_raw = self.from_raw_profile_info(
target_override='with-vars'
)
self.assertEqual(profile.profile_name, 'default')
self.assertEqual(profile.target_name, 'with-vars')
self.assertEqual(profile.threads, 1)
self.assertTrue(profile.config.send_anonymous_usage_stats)
self.assertTrue(profile.config.use_colors)
self.assertEqual(profile.credentials.type, 'postgres')
self.assertEqual(profile.credentials.host, 'env-postgres-host')
self.assertEqual(profile.credentials.port, 6543)
self.assertEqual(profile.credentials.user, 'env-postgres-user')
self.assertEqual(profile.credentials.password, 'env-postgres-pass')
self.assertEqual(profile, from_raw)
def test_env_vars_env_target(self):
self.default_profile_data['default']['target'] = "{{ env_var('env_value_target') }}"
self.write_profile(self.default_profile_data)
self.env_override['env_value_target'] = 'with-vars'
with mock.patch.dict(os.environ, self.env_override):
profile = self.from_args()
from_raw = self.from_raw_profile_info(
target_override='with-vars'
)
self.assertEqual(profile.profile_name, 'default')
self.assertEqual(profile.target_name, 'with-vars')
self.assertEqual(profile.threads, 1)
self.assertTrue(profile.config.send_anonymous_usage_stats)
self.assertTrue(profile.config.use_colors)
self.assertEqual(profile.credentials.type, 'postgres')
self.assertEqual(profile.credentials.host, 'env-postgres-host')
self.assertEqual(profile.credentials.port, 6543)
self.assertEqual(profile.credentials.user, 'env-postgres-user')
self.assertEqual(profile.credentials.password, 'env-postgres-pass')
self.assertEqual(profile, from_raw)
def test_invalid_env_vars(self):
self.env_override['env_value_port'] = 'hello'
self.args.target = 'with-vars'
with mock.patch.dict(os.environ, self.env_override):
with self.assertRaises(dbt.exceptions.DbtProfileError) as exc:
self.from_args()
self.assertIn("not of type 'integer'", str(exc.exception))
def test_cli_and_env_vars(self):
self.args.target = 'cli-and-env-vars'
self.args.vars = '{"cli_value_host": "cli-postgres-host"}'
with mock.patch.dict(os.environ, self.env_override):
profile = self.from_args(cli_vars=None)
from_raw = self.from_raw_profile_info(
target_override='cli-and-env-vars',
cli_vars={'cli_value_host': 'cli-postgres-host'},
)
self.assertEqual(profile.profile_name, 'default')
self.assertEqual(profile.target_name, 'cli-and-env-vars')
self.assertEqual(profile.threads, 1)
self.assertTrue(profile.config.send_anonymous_usage_stats)
self.assertTrue(profile.config.use_colors)
self.assertEqual(profile.credentials.type, 'postgres')
self.assertEqual(profile.credentials.host, 'cli-postgres-host')
self.assertEqual(profile.credentials.port, 6543)
self.assertEqual(profile.credentials.user, 'env-postgres-user')
self.assertEqual(profile.credentials.password, 'env-postgres-pass')
self.assertEqual(profile, from_raw)
def test_no_profile(self):
with self.assertRaises(dbt.exceptions.DbtProjectError) as exc:
self.from_args(project_profile_name=None)
self.assertIn('no profile was specified', str(exc.exception))
class TestProject(BaseConfigTest):
def setUp(self):
self.profiles_dir = '/invalid-profiles-path'
self.project_dir = '/invalid-root-path'
super(TestProject, self).setUp()
self.default_project_data['project-root'] = self.project_dir
def test_defaults(self):
project = dbt.config.Project.from_project_config(
self.default_project_data
)
self.assertEqual(project.project_name, 'my_test_project')
self.assertEqual(project.version, '0.0.1')
self.assertEqual(project.profile_name, 'default')
self.assertEqual(project.project_root, '/invalid-root-path')
self.assertEqual(project.source_paths, ['models'])
self.assertEqual(project.macro_paths, ['macros'])
self.assertEqual(project.data_paths, ['data'])
self.assertEqual(project.test_paths, ['test'])
self.assertEqual(project.analysis_paths, [])
self.assertEqual(project.docs_paths, ['models'])
self.assertEqual(project.target_path, 'target')
self.assertEqual(project.clean_targets, ['target'])
self.assertEqual(project.log_path, 'logs')
self.assertEqual(project.modules_path, 'dbt_modules')
self.assertEqual(project.quoting, {})
self.assertEqual(project.models, {})
self.assertEqual(project.on_run_start, [])
self.assertEqual(project.on_run_end, [])
self.assertEqual(project.archive, [])
self.assertEqual(project.seeds, {})
self.assertEqual(project.dbt_version,
[VersionSpecifier.from_version_string('>=0.0.0')])
self.assertEqual(project.packages, PackageConfig(packages=[]))
# just make sure str() doesn't crash anything, that's always
# embarrassing
str(project)
def test_eq(self):
project = dbt.config.Project.from_project_config(
self.default_project_data
)
other = dbt.config.Project.from_project_config(
self.default_project_data
)
self.assertEqual(project, other)
def test_neq(self):
project = dbt.config.Project.from_project_config(
self.default_project_data
)
self.assertNotEqual(project, object())
def test_implicit_overrides(self):
self.default_project_data.update({
'source-paths': ['other-models'],
'target-path': 'other-target',
})
project = dbt.config.Project.from_project_config(
self.default_project_data
)
self.assertEqual(project.docs_paths, ['other-models'])
self.assertEqual(project.clean_targets, ['other-target'])
def test_hashed_name(self):
project = dbt.config.Project.from_project_config(
self.default_project_data
)
self.assertEqual(project.hashed_name(), '754cd47eac1d6f50a5f7cd399ec43da4')
def test_all_overrides(self):
self.default_project_data.update({
'source-paths': ['other-models'],
'macro-paths': ['other-macros'],
'data-paths': ['other-data'],
'test-paths': ['other-test'],
'analysis-paths': ['analysis'],
'docs-paths': ['docs'],
'target-path': 'other-target',
'clean-targets': ['another-target'],
'log-path': 'other-logs',
'modules-path': 'other-dbt_modules',
'quoting': {'identifier': False},
'models': {
'pre-hook': ['{{ logging.log_model_start_event() }}'],
'post-hook': ['{{ logging.log_model_end_event() }}'],
'my_test_project': {
'first': {
'enabled': False,
'sub': {
'enabled': True,
}
},
'second': {
'materialized': 'table',
},
},
'third_party': {
'third': {
'materialized': 'view',
},
},
},
'on-run-start': [
'{{ logging.log_run_start_event() }}',
],
'on-run-end': [
'{{ logging.log_run_end_event() }}',
],
'archive': [
{
'source_schema': 'my_schema',
'target_schema': 'my_other_schema',
'tables': [
{
'source_table': 'my_table',
'target_table': 'my_table_archived',
'updated_at': 'updated_at_field',
'unique_key': 'table_id',
},
],
},
],
'seeds': {
'my_test_project': {
'enabled': True,
'schema': 'seed_data',
'post-hook': 'grant select on {{ this }} to bi_user',
},
},
'require-dbt-version': '>=0.1.0',
})
packages = {
'packages': [
{
'local': 'foo',
},
{
'git': 'git@example.com:fishtown-analytics/dbt-utils.git',
'revision': 'test-rev'
},
],
}
project = dbt.config.Project.from_project_config(
self.default_project_data, packages
)
self.assertEqual(project.project_name, 'my_test_project')
self.assertEqual(project.version, '0.0.1')
self.assertEqual(project.profile_name, 'default')
self.assertEqual(project.project_root, '/invalid-root-path')
self.assertEqual(project.source_paths, ['other-models'])
self.assertEqual(project.macro_paths, ['other-macros'])
self.assertEqual(project.data_paths, ['other-data'])
self.assertEqual(project.test_paths, ['other-test'])
self.assertEqual(project.analysis_paths, ['analysis'])
self.assertEqual(project.docs_paths, ['docs'])
self.assertEqual(project.target_path, 'other-target')
self.assertEqual(project.clean_targets, ['another-target'])
self.assertEqual(project.log_path, 'other-logs')
self.assertEqual(project.modules_path, 'other-dbt_modules')
self.assertEqual(project.quoting, {'identifier': False})
self.assertEqual(project.models, {
'pre-hook': ['{{ logging.log_model_start_event() }}'],
'post-hook': ['{{ logging.log_model_end_event() }}'],
'my_test_project': {
'first': {
'enabled': False,
'sub': {
'enabled': True,
}
},
'second': {
'materialized': 'table',
},
},
'third_party': {
'third': {
'materialized': 'view',
},
},
})
self.assertEqual(project.on_run_start, ['{{ logging.log_run_start_event() }}'])
self.assertEqual(project.on_run_end, ['{{ logging.log_run_end_event() }}'])
self.assertEqual(project.archive, [{
'source_schema': 'my_schema',
'target_schema': 'my_other_schema',
'tables': [
{
'source_table': 'my_table',
'target_table': 'my_table_archived',
'updated_at': 'updated_at_field',
'unique_key': 'table_id',
},
],
}])
self.assertEqual(project.seeds, {
'my_test_project': {
'enabled': True,
'schema': 'seed_data',
'post-hook': 'grant select on {{ this }} to bi_user',
},
})
self.assertEqual(project.dbt_version,
[VersionSpecifier.from_version_string('>=0.1.0')])
self.assertEqual(project.packages, PackageConfig(packages=[
{
'local': 'foo',
},
{
'git': 'git@example.com:fishtown-analytics/dbt-utils.git',
'revision': 'test-rev'
},
]))
str(project)
json.dumps(project.to_project_config())
def test_string_run_hooks(self):
self.default_project_data.update({
'on-run-start': '{{ logging.log_run_start_event() }}',
'on-run-end': '{{ logging.log_run_end_event() }}',
})
project = dbt.config.Project.from_project_config(
self.default_project_data
)
self.assertEqual(
project.on_run_start,
['{{ logging.log_run_start_event() }}']
)
self.assertEqual(
project.on_run_end,
['{{ logging.log_run_end_event() }}']
)
def test_invalid_project_name(self):
self.default_project_data['name'] = 'invalid-project-name'
with self.assertRaises(dbt.exceptions.DbtProjectError) as exc:
dbt.config.Project.from_project_config(self.default_project_data)
self.assertIn('invalid-project-name', str(exc.exception))
def test_no_project(self):
with self.assertRaises(dbt.exceptions.DbtProjectError) as exc:
dbt.config.Project.from_project_root(self.project_dir, {})
self.assertIn('no dbt_project.yml', str(exc.exception))
def test_invalid_version(self):
self.default_project_data['require-dbt-version'] = 'hello!'
with self.assertRaises(dbt.exceptions.DbtProjectError) as exc:
dbt.config.Project.from_project_config(self.default_project_data)
def test_unsupported_version(self):
self.default_project_data['require-dbt-version'] = '>99999.0.0'
# allowed, because the RuntimeConfig checks, not the Project itself
dbt.config.Project.from_project_config(self.default_project_data)
def test__no_unused_resource_config_paths(self):
self.default_project_data.update({
'models': model_config,
'seeds': {},
})
project = dbt.config.Project.from_project_config(
self.default_project_data
)
resource_fqns = {'models': model_fqns}
unused = project.get_unused_resource_config_paths(resource_fqns, [])
self.assertEqual(len(unused), 0)
def test__unused_resource_config_paths(self):
self.default_project_data.update({
'models': model_config['my_package_name'],
'seeds': {},
})
project = dbt.config.Project.from_project_config(
self.default_project_data
)
resource_fqns = {'models': model_fqns}
unused = project.get_unused_resource_config_paths(resource_fqns, [])
self.assertEqual(len(unused), 3)
def test__get_unused_resource_config_paths_empty(self):
project = dbt.config.Project.from_project_config(
self.default_project_data
)
unused = project.get_unused_resource_config_paths({'models': frozenset((
('my_test_project', 'foo', 'bar'),
('my_test_project', 'foo', 'baz'),
))}, [])
self.assertEqual(len(unused), 0)
@mock.patch.object(dbt.config.project, 'logger')
def test__warn_for_unused_resource_config_paths_empty(self, mock_logger):
project = dbt.config.Project.from_project_config(
self.default_project_data
)
unused = project.warn_for_unused_resource_config_paths({'models': frozenset((
('my_test_project', 'foo', 'bar'),
('my_test_project', 'foo', 'baz'),
))}, [])
mock_logger.info.assert_not_called()
def test_none_values(self):
self.default_project_data.update({
'models': None,
'seeds': None,
'archive': None,
'on-run-end': None,
'on-run-start': None,
})
project = dbt.config.Project.from_project_config(
self.default_project_data
)
self.assertEqual(project.models, {})
self.assertEqual(project.on_run_start, [])
self.assertEqual(project.on_run_end, [])
self.assertEqual(project.archive, [])
self.assertEqual(project.seeds, {})
def test_nested_none_values(self):
self.default_project_data.update({
'models': {'vars': None, 'pre-hook': None, 'post-hook': None},
'seeds': {'vars': None, 'pre-hook': None, 'post-hook': None, 'column_types': None},
})
project = dbt.config.Project.from_project_config(
self.default_project_data
)
self.assertEqual(project.models, {'vars': {}, 'pre-hook': [], 'post-hook': []})
self.assertEqual(project.seeds, {'vars': {}, 'pre-hook': [], 'post-hook': [], 'column_types': {}})
def test_cycle(self):
models = {}
models['models'] = models
self.default_project_data.update({
'models': models,
})
with self.assertRaises(dbt.exceptions.DbtProjectError):
dbt.config.Project.from_project_config(
self.default_project_data
)
class TestProjectWithConfigs(BaseConfigTest):
def setUp(self):
self.profiles_dir = '/invalid-profiles-path'
self.project_dir = '/invalid-root-path'
super(TestProjectWithConfigs, self).setUp()
self.default_project_data['project-root'] = self.project_dir
self.default_project_data['models'] = {
'enabled': True,
'my_test_project': {
'foo': {
'materialized': 'view',
'bar': {
'materialized': 'table',
}
},
'baz': {
'materialized': 'table',
}
}
}
self.used = {'models': frozenset((
('my_test_project', 'foo', 'bar'),
('my_test_project', 'foo', 'baz'),
))}
def test__get_unused_resource_config_paths(self):
project = dbt.config.Project.from_project_config(
self.default_project_data
)
unused = project.get_unused_resource_config_paths(self.used, [])
self.assertEqual(len(unused), 1)
self.assertEqual(unused[0], ('models', 'my_test_project', 'baz'))
@mock.patch.object(dbt.config.project, 'warn_or_error')
def test__warn_for_unused_resource_config_paths(self, warn_or_error):
project = dbt.config.Project.from_project_config(
self.default_project_data
)
unused = project.warn_for_unused_resource_config_paths(self.used, [])
warn_or_error.assert_called_once()
@mock.patch.object(dbt.config.project, 'logger')
def test__warn_for_unused_resource_config_paths_disabled(self, mock_logger):
project = dbt.config.Project.from_project_config(
self.default_project_data
)
unused = project.get_unused_resource_config_paths(
self.used,
frozenset([('my_test_project', 'baz')])
)
self.assertEqual(len(unused), 0)
class TestProjectFile(BaseFileTest):
def setUp(self):
super(TestProjectFile, self).setUp()
self.write_project(self.default_project_data)
# and after the fact, add the project root
self.default_project_data['project-root'] = self.project_dir
def test_from_project_root(self):
project = dbt.config.Project.from_project_root(self.project_dir, {})
from_config = dbt.config.Project.from_project_config(
self.default_project_data
)
self.assertEqual(project, from_config)
self.assertEqual(project.version, "0.0.1")
self.assertEqual(project.project_name, 'my_test_project')
def test_with_invalid_package(self):
self.write_packages({'invalid': ['not a package of any kind']})
with self.assertRaises(dbt.exceptions.DbtProjectError) as exc:
dbt.config.Project.from_project_root(self.project_dir, {})
class TestVariableProjectFile(BaseFileTest):
def setUp(self):
super(TestVariableProjectFile, self).setUp()
self.default_project_data['version'] = "{{ var('cli_version') }}"
self.default_project_data['name'] = "{{ env_var('env_value_project') }}"
self.write_project(self.default_project_data)
# and after the fact, add the project root
self.default_project_data['project-root'] = self.project_dir
def test_cli_and_env_vars(self):
cli_vars = '{"cli_version": "0.1.2"}'
with mock.patch.dict(os.environ, self.env_override):
project = dbt.config.Project.from_project_root(
self.project_dir,
cli_vars
)
self.assertEqual(project.version, "0.1.2")
self.assertEqual(project.project_name, 'blah')
class TestRuntimeConfig(BaseConfigTest):
def setUp(self):
self.profiles_dir = '/invalid-profiles-path'
self.project_dir = '/invalid-root-path'
super(TestRuntimeConfig, self).setUp()
self.default_project_data['project-root'] = self.project_dir
def get_project(self):
return dbt.config.Project.from_project_config(
self.default_project_data
)
def get_profile(self):
return dbt.config.Profile.from_raw_profiles(
self.default_profile_data, self.default_project_data['profile'], {}
)
def from_parts(self, exc=None):
project = self.get_project()
profile = self.get_profile()
if exc is None:
return dbt.config.RuntimeConfig.from_parts(project, profile, self.args)
with self.assertRaises(exc) as raised:
err = raised
dbt.config.RuntimeConfig.from_parts(project, profile, self.args)
return err
def test_from_parts(self):
project = self.get_project()
profile = self.get_profile()
config = dbt.config.RuntimeConfig.from_parts(project, profile, self.args)
self.assertEqual(config.cli_vars, {})
self.assertEqual(config.to_profile_info(), profile.to_profile_info())
# we should have the default quoting set in the full config, but not in
# the project
# TODO(jeb): Adapters must assert that quoting is populated?
expected_project = project.to_project_config()
self.assertEqual(expected_project['quoting'], {})
expected_project['quoting'] = {
'database': True,
'identifier': True,
'schema': True,
}
self.assertEqual(config.to_project_config(), expected_project)
def test_str(self):
project = self.get_project()