forked from dbt-labs/dbt-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
371 lines (291 loc) · 10.8 KB
/
utils.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
"""Unit test utility functions.
Note that all imports should be inside the functions to avoid import/mocking
issues.
"""
import string
import os
from unittest import mock
from unittest import TestCase
import agate
import pytest
from dbt.dataclass_schema import ValidationError
def normalize(path):
"""On windows, neither is enough on its own:
>>> normcase('C:\\documents/ALL CAPS/subdir\\..')
'c:\\documents\\all caps\\subdir\\..'
>>> normpath('C:\\documents/ALL CAPS/subdir\\..')
'C:\\documents\\ALL CAPS'
>>> normpath(normcase('C:\\documents/ALL CAPS/subdir\\..'))
'c:\\documents\\all caps'
"""
return os.path.normcase(os.path.normpath(path))
class Obj:
which = 'blah'
single_threaded = False
def mock_connection(name, state='open'):
conn = mock.MagicMock()
conn.name = name
conn.state = state
return conn
def profile_from_dict(profile, profile_name, cli_vars='{}'):
from dbt.config import Profile
from dbt.config.renderer import ProfileRenderer
from dbt.context.base import generate_base_context
from dbt.config.utils import parse_cli_vars
if not isinstance(cli_vars, dict):
cli_vars = parse_cli_vars(cli_vars)
renderer = ProfileRenderer(generate_base_context(cli_vars))
return Profile.from_raw_profile_info(
profile,
profile_name,
renderer,
)
def project_from_dict(project, profile, packages=None, selectors=None, cli_vars='{}'):
from dbt.context.target import generate_target_context
from dbt.config import Project
from dbt.config.renderer import DbtProjectYamlRenderer
from dbt.config.utils import parse_cli_vars
if not isinstance(cli_vars, dict):
cli_vars = parse_cli_vars(cli_vars)
renderer = DbtProjectYamlRenderer(generate_target_context(profile, cli_vars))
project_root = project.pop('project-root', os.getcwd())
return Project.render_from_dict(
project_root, project, packages, selectors, renderer
)
def config_from_parts_or_dicts(project, profile, packages=None, selectors=None, cli_vars='{}'):
from dbt.config import Project, Profile, RuntimeConfig
from copy import deepcopy
if isinstance(project, Project):
profile_name = project.profile_name
else:
profile_name = project.get('profile')
if not isinstance(profile, Profile):
profile = profile_from_dict(
deepcopy(profile),
profile_name,
cli_vars,
)
if not isinstance(project, Project):
project = project_from_dict(
deepcopy(project),
profile,
packages,
selectors,
cli_vars,
)
args = Obj()
args.vars = cli_vars
args.profile_dir = '/dev/null'
return RuntimeConfig.from_parts(
project=project,
profile=profile,
args=args
)
def inject_plugin(plugin):
from dbt.adapters.factory import FACTORY
key = plugin.adapter.type()
FACTORY.plugins[key] = plugin
def inject_plugin_for(config):
# from dbt.adapters.postgres import Plugin, PostgresAdapter
from dbt.adapters.factory import FACTORY
FACTORY.load_plugin(config.credentials.type)
adapter = FACTORY.get_adapter(config)
return adapter
def inject_adapter(value, plugin):
"""Inject the given adapter into the adapter factory, so your hand-crafted
artisanal adapter will be available from get_adapter() as if dbt loaded it.
"""
inject_plugin(plugin)
from dbt.adapters.factory import FACTORY
key = value.type()
FACTORY.adapters[key] = value
def clear_plugin(plugin):
from dbt.adapters.factory import FACTORY
key = plugin.adapter.type()
FACTORY.plugins.pop(key, None)
FACTORY.adapters.pop(key, None)
class ContractTestCase(TestCase):
ContractType = None
def setUp(self):
self.maxDiff = None
super().setUp()
def assert_to_dict(self, obj, dct):
self.assertEqual(obj.to_dict(omit_none=True), dct)
def assert_from_dict(self, obj, dct, cls=None):
if cls is None:
cls = self.ContractType
cls.validate(dct)
self.assertEqual(cls.from_dict(dct), obj)
def assert_symmetric(self, obj, dct, cls=None):
self.assert_to_dict(obj, dct)
self.assert_from_dict(obj, dct, cls)
def assert_fails_validation(self, dct, cls=None):
if cls is None:
cls = self.ContractType
with self.assertRaises(ValidationError):
cls.validate(dct)
cls.from_dict(dct)
def compare_dicts(dict1, dict2):
first_set = set(dict1.keys())
second_set = set(dict2.keys())
print(f"--- Difference between first and second keys: {first_set.difference(second_set)}")
print(f"--- Difference between second and first keys: {second_set.difference(first_set)}")
common_keys = set(first_set).intersection(set(second_set))
found_differences = False
for key in common_keys:
if dict1[key] != dict2[key] :
print(f"--- --- first dict: {key}: {str(dict1[key])}")
print(f"--- --- second dict: {key}: {str(dict2[key])}")
found_differences = True
if found_differences:
print("--- Found differences in dictionaries")
else:
print("--- Found no differences in dictionaries")
def assert_from_dict(obj, dct, cls=None):
if cls is None:
cls = obj.__class__
cls.validate(dct)
obj_from_dict = cls.from_dict(dct)
if hasattr(obj, 'created_at'):
obj_from_dict.created_at = 1
obj.created_at = 1
assert obj_from_dict == obj
def assert_to_dict(obj, dct):
obj_to_dict = obj.to_dict(omit_none=True)
if 'created_at' in obj_to_dict:
obj_to_dict['created_at'] = 1
if 'created_at' in dct:
dct['created_at'] = 1
assert obj_to_dict == dct
def assert_symmetric(obj, dct, cls=None):
assert_to_dict(obj, dct)
assert_from_dict(obj, dct, cls)
def assert_fails_validation(dct, cls):
with pytest.raises(ValidationError):
cls.validate(dct)
cls.from_dict(dct)
def generate_name_macros(package):
from dbt.contracts.graph.parsed import ParsedMacro
from dbt.node_types import NodeType
name_sql = {}
for component in ('database', 'schema', 'alias'):
if component == 'alias':
source = 'node.name'
else:
source = f'target.{component}'
name = f'generate_{component}_name'
sql = f'{{% macro {name}(value, node) %}} {{% if value %}} {{{{ value }}}} {{% else %}} {{{{ {source} }}}} {{% endif %}} {{% endmacro %}}'
name_sql[name] = sql
for name, sql in name_sql.items():
pm = ParsedMacro(
name=name,
resource_type=NodeType.Macro,
unique_id=f'macro.{package}.{name}',
package_name=package,
original_file_path=normalize('macros/macro.sql'),
root_path='./dbt_modules/root',
path=normalize('macros/macro.sql'),
macro_sql=sql,
)
yield pm
class TestAdapterConversions(TestCase):
def _get_tester_for(self, column_type):
from dbt.clients import agate_helper
if column_type is agate.TimeDelta: # dbt never makes this!
return agate.TimeDelta()
for instance in agate_helper.DEFAULT_TYPE_TESTER._possible_types:
if type(instance) is column_type:
return instance
raise ValueError(f'no tester for {column_type}')
def _make_table_of(self, rows, column_types):
column_names = list(string.ascii_letters[:len(rows[0])])
if isinstance(column_types, type):
column_types = [self._get_tester_for(column_types) for _ in column_names]
else:
column_types = [self._get_tester_for(typ) for typ in column_types]
table = agate.Table(rows, column_names=column_names, column_types=column_types)
return table
def MockMacro(package, name='my_macro', **kwargs):
from dbt.contracts.graph.parsed import ParsedMacro
from dbt.node_types import NodeType
mock_kwargs = dict(
resource_type=NodeType.Macro,
package_name=package,
unique_id=f'macro.{package}.{name}',
original_file_path='/dev/null',
)
mock_kwargs.update(kwargs)
macro = mock.MagicMock(
spec=ParsedMacro,
**mock_kwargs
)
macro.name = name
return macro
def MockMaterialization(package, name='my_materialization', adapter_type=None, **kwargs):
if adapter_type is None:
adapter_type = 'default'
kwargs['adapter_type'] = adapter_type
return MockMacro(package, f'materialization_{name}_{adapter_type}', **kwargs)
def MockGenerateMacro(package, component='some_component', **kwargs):
name = f'generate_{component}_name'
return MockMacro(package, name=name, **kwargs)
def MockSource(package, source_name, name, **kwargs):
from dbt.node_types import NodeType
from dbt.contracts.graph.parsed import ParsedSourceDefinition
src = mock.MagicMock(
__class__=ParsedSourceDefinition,
resource_type=NodeType.Source,
source_name=source_name,
package_name=package,
unique_id=f'source.{package}.{source_name}.{name}',
search_name=f'{source_name}.{name}',
**kwargs
)
src.name = name
return src
def MockNode(package, name, resource_type=None, **kwargs):
from dbt.node_types import NodeType
from dbt.contracts.graph.parsed import ParsedModelNode, ParsedSeedNode
if resource_type is None:
resource_type = NodeType.Model
if resource_type == NodeType.Model:
cls = ParsedModelNode
elif resource_type == NodeType.Seed:
cls = ParsedSeedNode
else:
raise ValueError(f'I do not know how to handle {resource_type}')
node = mock.MagicMock(
__class__=cls,
resource_type=resource_type,
package_name=package,
unique_id=f'{str(resource_type)}.{package}.{name}',
search_name=name,
**kwargs
)
node.name = name
return node
def MockDocumentation(package, name, **kwargs):
from dbt.node_types import NodeType
from dbt.contracts.graph.parsed import ParsedDocumentation
doc = mock.MagicMock(
__class__=ParsedDocumentation,
resource_type=NodeType.Documentation,
package_name=package,
search_name=name,
unique_id=f'{package}.{name}',
**kwargs
)
doc.name = name
return doc
def load_internal_manifest_macros(config, macro_hook = lambda m: None):
from dbt.parser.manifest import ManifestLoader
return ManifestLoader.load_macros(config, macro_hook)
def dict_replace(dct, **kwargs):
dct = dct.copy()
dct.update(kwargs)
return dct
def replace_config(n, **kwargs):
return n.replace(
config=n.config.replace(**kwargs),
unrendered_config=dict_replace(n.unrendered_config, **kwargs),
)