This repository has been archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathtest_db_sync.py
357 lines (311 loc) · 14.6 KB
/
test_db_sync.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
import unittest
from target_snowflake import db_sync
class TestDBSync(unittest.TestCase):
"""
Unit Tests
"""
def setUp(self):
self.config = {}
self.json_types = {
'str': {"type": ["string"]},
'str_or_null': {"type": ["string", "null"]},
'dt': {"type": ["string"], "format": "date-time"},
'dt_or_null': {"type": ["string", "null"], "format": "date-time"},
'time': {"type": ["string"], "format": "time"},
'time_or_null': {"type": ["string", "null"], "format": "time"},
'binary': {"type": ["string", "null"], "format": "binary"},
'num': {"type": ["number"]},
'int': {"type": ["integer"]},
'int_or_str': {"type": ["integer", "string"]},
'bool': {"type": ["boolean"]},
'obj': {"type": ["object"]},
'arr': {"type": ["array"]},
}
def test_config_validation(self):
"""Test configuration validator"""
validator = db_sync.validate_config
empty_config = {}
minimal_config = {
'account': "dummy-value",
'dbname': "dummy-value",
'user': "dummy-value",
'password': "dummy-value",
'warehouse': "dummy-value",
'aws_access_key_id': "dummy-value",
'aws_secret_access_key': "dummy-value",
's3_bucket': "dummy-value",
'default_target_schema': "dummy-value",
'stage': "dummy-value",
'file_format': "dummy-value"
}
# Config validator returns a list of errors
# If the list is empty then the configuration is valid otherwise invalid
# Empty configuration should fail - (nr_of_errors >= 0)
self.assertGreater(len(validator(empty_config)), 0)
# Minimal configuratino should pass - (nr_of_errors == 0)
self.assertEqual(len(validator(minimal_config)), 0)
# Configuration without schema references - (nr_of_errors >= 0)
config_with_no_schema = minimal_config.copy()
config_with_no_schema.pop('default_target_schema')
self.assertGreater(len(validator(config_with_no_schema)), 0)
# Configuration with schema mapping - (nr_of_errors >= 0)
config_with_schema_mapping = minimal_config.copy()
config_with_schema_mapping.pop('default_target_schema')
config_with_schema_mapping['schema_mapping'] = {
"dummy_stream": {
"target_schema": "dummy_schema"
}
}
self.assertEqual(len(validator(config_with_schema_mapping)), 0)
def test_column_type_mapping(self):
"""Test JSON type to Snowflake column type mappings"""
mapper = db_sync.column_type
# Snowflake column types
sf_types = {
'str': 'text',
'str_or_null': 'text',
'dt': 'timestamp_ntz',
'dt_or_null': 'timestamp_ntz',
'time': 'time',
'time_or_null': 'time',
'binary': 'binary',
'num': 'float',
'int': 'number',
'int_or_str': 'text',
'bool': 'boolean',
'obj': 'variant',
'arr': 'variant',
}
# Mapping from JSON schema types to Snowflake column types
for key, val in self.json_types.items():
self.assertEqual(mapper(val), sf_types[key])
def test_column_trans(self):
"""Test column transformation"""
trans = db_sync.column_trans
# Snowflake column transformations
sf_trans = {
'str': '',
'str_or_null': '',
'dt': '',
'dt_or_null': '',
'time': '',
'time_or_null': '',
'binary': 'to_binary',
'num': '',
'int': '',
'int_or_str': '',
'bool': '',
'obj': 'parse_json',
'arr': 'parse_json',
}
# Getting transformations for every JSON type
for key, val in self.json_types.items():
self.assertEqual(trans(val), sf_trans[key])
def test_stream_name_to_dict(self):
"""Test identifying catalog, schema and table names from fully qualified stream and table names"""
# Singer stream name format (Default '-' separator)
self.assertEqual(
db_sync.stream_name_to_dict('my_table'),
{"catalog_name": None, "schema_name": None, "table_name": "my_table"})
# Singer stream name format (Default '-' separator)
self.assertEqual(
db_sync.stream_name_to_dict('my_schema-my_table'),
{"catalog_name": None, "schema_name": "my_schema", "table_name": "my_table"})
# Singer stream name format (Default '-' separator)
self.assertEqual(
db_sync.stream_name_to_dict('my_catalog-my_schema-my_table'),
{"catalog_name": "my_catalog", "schema_name": "my_schema", "table_name": "my_table"})
# Snowflake table format (Custom '.' separator)
self.assertEqual(
db_sync.stream_name_to_dict('my_table', separator='.'),
{"catalog_name": None, "schema_name": None, "table_name": "my_table"})
# Snowflake table format (Custom '.' separator)
self.assertEqual(
db_sync.stream_name_to_dict('my_schema.my_table', separator='.'),
{"catalog_name": None, "schema_name": "my_schema", "table_name": "my_table"})
# Snowflake table format (Custom '.' separator)
self.assertEqual(
db_sync.stream_name_to_dict('my_catalog.my_schema.my_table', separator='.'),
{"catalog_name": "my_catalog", "schema_name": "my_schema", "table_name": "my_table"})
def test_flatten_schema(self):
"""Test flattening of SCHEMA messages"""
flatten_schema = db_sync.flatten_schema
# Schema with no object properties should be empty dict
schema_with_no_properties = {"type": "object"}
self.assertEqual(flatten_schema(schema_with_no_properties), {})
not_nested_schema = {
"type": "object",
"properties": {
"c_pk": {"type": ["null", "integer"]},
"c_varchar": {"type": ["null", "string"]},
"c_int": {"type": ["null", "integer"]}}}
# NO FLATTENING - Schema with simple properties should be a plain dictionary
self.assertEqual(flatten_schema(not_nested_schema), not_nested_schema['properties'])
nested_schema_with_no_properties = {
"type": "object",
"properties": {
"c_pk": {"type": ["null", "integer"]},
"c_varchar": {"type": ["null", "string"]},
"c_int": {"type": ["null", "integer"]},
"c_obj": {"type": ["null", "object"]}}}
# NO FLATTENING - Schema with object type property but without further properties should be a plain dictionary
self.assertEqual(flatten_schema(nested_schema_with_no_properties),
nested_schema_with_no_properties['properties'])
nested_schema_with_properties = {
"type": "object",
"properties": {
"c_pk": {"type": ["null", "integer"]},
"c_varchar": {"type": ["null", "string"]},
"c_int": {"type": ["null", "integer"]},
"c_obj": {
"type": ["null", "object"],
"properties": {
"nested_prop1": {"type": ["null", "string"]},
"nested_prop2": {"type": ["null", "string"]},
"nested_prop3": {
"type": ["null", "object"],
"properties": {
"multi_nested_prop1": {"type": ["null", "string"]},
"multi_nested_prop2": {"type": ["null", "string"]}
}
}
}
}
}
}
# NO FLATTENING - Schema with object type property but without further properties should be a plain dictionary
# No flattening (default)
self.assertEqual(flatten_schema(nested_schema_with_properties), nested_schema_with_properties['properties'])
# NO FLATTENING - Schema with object type property but without further properties should be a plain dictionary
# max_level: 0 : No flattening (default)
self.assertEqual(flatten_schema(nested_schema_with_properties, max_level=0),
nested_schema_with_properties['properties'])
# FLATTENING - Schema with object type property but without further properties should be a dict with
# flattened properties
self.assertEqual(flatten_schema(nested_schema_with_properties, max_level=1),
{
'c_pk': {'type': ['null', 'integer']},
'c_varchar': {'type': ['null', 'string']},
'c_int': {'type': ['null', 'integer']},
'c_obj__nested_prop1': {'type': ['null', 'string']},
'c_obj__nested_prop2': {'type': ['null', 'string']},
'c_obj__nested_prop3': {
'type': ['null', 'object'],
"properties": {
"multi_nested_prop1": {"type": ["null", "string"]},
"multi_nested_prop2": {"type": ["null", "string"]}
}
}
})
# FLATTENING - Schema with object type property but without further properties should be a dict with
# flattened properties
self.assertEqual(flatten_schema(nested_schema_with_properties, max_level=10),
{
'c_pk': {'type': ['null', 'integer']},
'c_varchar': {'type': ['null', 'string']},
'c_int': {'type': ['null', 'integer']},
'c_obj__nested_prop1': {'type': ['null', 'string']},
'c_obj__nested_prop2': {'type': ['null', 'string']},
'c_obj__nested_prop3__multi_nested_prop1': {'type': ['null', 'string']},
'c_obj__nested_prop3__multi_nested_prop2': {'type': ['null', 'string']}
})
def test_flatten_record(self):
"""Test flattening of RECORD messages"""
flatten_record = db_sync.flatten_record
empty_record = {}
# Empty record should be empty dict
self.assertEqual(flatten_record(empty_record), {})
not_nested_record = {"c_pk": 1, "c_varchar": "1", "c_int": 1}
# NO FLATTENING - Record with simple properties should be a plain dictionary
self.assertEqual(flatten_record(not_nested_record), not_nested_record)
nested_record = {
"c_pk": 1,
"c_varchar": "1",
"c_int": 1,
"c_obj": {
"nested_prop1": "value_1",
"nested_prop2": "value_2",
"nested_prop3": {
"multi_nested_prop1": "multi_value_1",
"multi_nested_prop2": "multi_value_2",
}}}
# NO FLATTENING - No flattening (default)
self.assertEqual(flatten_record(nested_record),
{
"c_pk": 1,
"c_varchar": "1",
"c_int": 1,
"c_obj": '{"nested_prop1": "value_1", "nested_prop2": "value_2", "nested_prop3": {'
'"multi_nested_prop1": "multi_value_1", "multi_nested_prop2": "multi_value_2"}}'
})
# NO FLATTENING
# max_level: 0 : No flattening (default)
self.assertEqual(flatten_record(nested_record, max_level=0),
{
"c_pk": 1,
"c_varchar": "1",
"c_int": 1,
"c_obj": '{"nested_prop1": "value_1", "nested_prop2": "value_2", "nested_prop3": {'
'"multi_nested_prop1": "multi_value_1", "multi_nested_prop2": "multi_value_2"}}'
})
# SEMI FLATTENING
# max_level: 1 : Semi-flattening (default)
self.assertEqual(flatten_record(nested_record, max_level=1),
{
"c_pk": 1,
"c_varchar": "1",
"c_int": 1,
"c_obj__nested_prop1": "value_1",
"c_obj__nested_prop2": "value_2",
"c_obj__nested_prop3": '{"multi_nested_prop1": "multi_value_1", "multi_nested_prop2": '
'"multi_value_2"}'
})
# FLATTENING
self.assertEqual(flatten_record(nested_record, max_level=10),
{
"c_pk": 1,
"c_varchar": "1",
"c_int": 1,
"c_obj__nested_prop1": "value_1",
"c_obj__nested_prop2": "value_2",
"c_obj__nested_prop3__multi_nested_prop1": "multi_value_1",
"c_obj__nested_prop3__multi_nested_prop2": "multi_value_2"
})
def test_flatten_record_with_flatten_schema(self):
flatten_record = db_sync.flatten_record
flatten_schema = {
"id": {
"type": [
"object",
"array",
"null"
]
}
}
test_cases = [
(
True,
{
"id": 1,
"data": "xyz"
},
{
"id": "1",
"data": "xyz"
}
),
(
False,
{
"id": 1,
"data": "xyz"
},
{
"id": 1,
"data": "xyz"
}
)
]
for idx, (should_use_flatten_schema, record, expected_output) in enumerate(test_cases):
output = flatten_record(record, flatten_schema if should_use_flatten_schema else None)
self.assertEqual(output, expected_output, f"Test {idx} failed. Testcase: {test_cases[idx]}")