-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathtest_transform.py
More file actions
379 lines (360 loc) · 13.1 KB
/
test_transform.py
File metadata and controls
379 lines (360 loc) · 13.1 KB
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
#
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
#
import json
import pytest
from airbyte_cdk.sources.utils.transform import TransformConfig, TypeTransformer
SIMPLE_SCHEMA = {"type": "object", "properties": {"value": {"type": "string"}}}
COMPLEX_SCHEMA = {
"type": "object",
"properties": {
"value": {"type": "boolean", "format": "even", "is_positive": True},
"prop": {"type": "string"},
"prop_with_null": {"type": ["string", "null"]},
"number_prop": {"type": "number"},
"int_prop": {"type": ["integer", "null"]},
"too_many_types": {"type": ["boolean", "null", "string"]},
"array": {"type": "array", "items": {"type": "string"}},
"nested": {"type": "object", "properties": {"a": {"type": "string"}}},
"list_of_lists": {
"type": "array",
"items": {"type": "array", "items": {"type": "string"}},
},
},
}
VERY_NESTED_SCHEMA = {
"type": ["null", "object"],
"properties": {
"very_nested_value": {
"type": ["null", "object"],
"properties": {
"very_nested_value": {
"type": ["null", "object"],
"properties": {
"very_nested_value": {
"type": ["null", "object"],
"properties": {
"very_nested_value": {
"type": ["null", "object"],
"properties": {
"very_nested_value": {"type": ["null", "number"]}
},
}
},
}
},
}
},
}
},
}
@pytest.mark.parametrize(
"schema, actual, expected, expected_warns",
[
(SIMPLE_SCHEMA, {"value": 12}, {"value": "12"}, None),
(
SIMPLE_SCHEMA,
{"value": 12, "unexpected_value": "unexpected"},
{"value": "12", "unexpected_value": "unexpected"},
None,
),
(
COMPLEX_SCHEMA,
{"value": 1, "array": ["111", 111, {1: 111}]},
{"value": True, "array": ["111", "111", "{1: 111}"]},
None,
),
(
COMPLEX_SCHEMA,
{"value": 1, "list_of_lists": [["111"], [111], [11], [{1: 1}]]},
{"value": True, "list_of_lists": [["111"], ["111"], ["11"], ["{1: 1}"]]},
None,
),
(
COMPLEX_SCHEMA,
{"value": 1, "nested": {"a": [1, 2, 3]}},
{"value": True, "nested": {"a": "[1, 2, 3]"}},
None,
),
(
COMPLEX_SCHEMA,
{"value": "false", "nested": {"a": [1, 2, 3]}},
{"value": False, "nested": {"a": "[1, 2, 3]"}},
None,
),
(COMPLEX_SCHEMA, {}, {}, None),
(COMPLEX_SCHEMA, {"int_prop": "12"}, {"int_prop": 12}, None),
# Skip invalid formattted field and process other fields.
(
COMPLEX_SCHEMA,
{"prop": 12, "number_prop": "aa12", "array": [12]},
{"prop": "12", "number_prop": "aa12", "array": ["12"]},
"Failed to transform value from type 'string' to type 'number' at path: 'number_prop'",
),
# Field too_many_types have ambigious type, skip formatting
(
COMPLEX_SCHEMA,
{"prop": 12, "too_many_types": 1212, "array": [12]},
{"prop": "12", "too_many_types": 1212, "array": ["12"]},
"Failed to transform value from type 'integer' to type '['boolean', 'null', 'string']' at path: 'too_many_types'",
),
# Test null field
(COMPLEX_SCHEMA, {"prop": None, "array": [12]}, {"prop": "None", "array": ["12"]}, None),
# If field can be null do not convert
(
COMPLEX_SCHEMA,
{"prop_with_null": None, "array": [12]},
{"prop_with_null": None, "array": ["12"]},
None,
),
(
VERY_NESTED_SCHEMA,
{
"very_nested_value": {
"very_nested_value": {
"very_nested_value": {"very_nested_value": {"very_nested_value": "2"}}
}
}
},
{
"very_nested_value": {
"very_nested_value": {
"very_nested_value": {"very_nested_value": {"very_nested_value": 2.0}}
}
}
},
None,
),
(
VERY_NESTED_SCHEMA,
{"very_nested_value": {"very_nested_value": None}},
{"very_nested_value": {"very_nested_value": None}},
None,
),
# Object without properties
({"type": "object"}, {"value": 12}, {"value": 12}, None),
(
# Array without items
{"type": "object", "properties": {"value": {"type": "array"}}},
{"value": [12]},
{"value": [12]},
None,
),
(
# Array without items and value is not an array
{"type": "object", "properties": {"value": {"type": "array"}}},
{"value": "12"},
{"value": ["12"]},
None,
),
(
{"type": "object", "properties": {"value": {"type": "array"}}},
{"value": 12},
{"value": [12]},
None,
),
(
{"type": "object", "properties": {"value": {"type": "array"}}},
{"value": None},
{"value": [None]},
None,
),
(
{"type": "object", "properties": {"value": {"type": ["null", "array"]}}},
{"value": None},
{"value": None},
None,
),
(
{
"type": "object",
"properties": {"value": {"type": "array", "items": {"type": ["string"]}}},
},
{"value": 10},
{"value": ["10"]},
None,
),
(
{
"type": "object",
"properties": {"value": {"type": "array", "items": {"type": ["object"]}}},
},
{"value": "string"},
{"value": "string"},
"Failed to transform value from type 'string' to type 'array' at path: 'value'",
),
(
{
"type": "object",
"properties": {"value": {"type": "array", "items": {"type": ["string"]}}},
},
{"value": {"key": "value"}},
{"value": {"key": "value"}},
"Failed to transform value from type '{'key': 'string'}' to type 'array' at path: 'value'",
),
(
# Schema root object is not an object, no convertion should happen
{"type": "integer"},
{"value": "12"},
{"value": "12"},
"Failed to transform value from type '{'value': 'string'}' to type 'integer' at path: ''",
),
(
# More than one type except null, no conversion should happen
{"type": "object", "properties": {"value": {"type": ["string", "boolean", "null"]}}},
{"value": 12},
{"value": 12},
"Failed to transform value from type 'integer' to type '['string', 'boolean', 'null']' at path: 'value'",
),
(
# Oneof not suported, no conversion for one_of_value should happen
{
"type": "object",
"properties": {
"one_of_value": {"oneOf": ["string", "boolean", "null"]},
"value_2": {"type": "string"},
},
},
{"one_of_value": 12, "value_2": 12},
{"one_of_value": 12, "value_2": "12"},
None,
),
(
# Case for #7076 issue (Facebook marketing: print tons of WARN message)
{
"properties": {
"cpc": {"type": ["null", "number"]},
},
},
{"cpc": "6.6666"},
{"cpc": 6.6666},
None,
),
(
{
"type": "object",
"properties": {"value": {"type": "array", "items": {"type": "string"}}},
},
{"value": {"key": "value"}},
{"value": {"key": "value"}},
"Failed to transform value from type '{'key': 'string'}' to type 'array' at path: 'value'",
),
(
{
"type": "object",
"properties": {
"value1": {"type": "object", "properties": {"value2": {"type": "string"}}}
},
},
{"value1": "value2"},
{"value1": "value2"},
"Failed to transform value from type 'string' to type 'object' at path: 'value1'",
),
(
{
"type": "object",
"properties": {"value": {"type": "array", "items": {"type": "object"}}},
},
{"value": ["one", "two"]},
{"value": ["one", "two"]},
"Failed to transform value from type 'string' to type 'object' at path: 'value.0'",
),
(
{"type": "string"},
None,
None,
"Failed to transform value from type 'null' to type 'string' at path: ''",
),
(
{"type": "string"},
{"a": {"b": {"c": {"d": {"e": "deep value"}}}}},
{"a": {"b": {"c": {"d": {"e": "deep value"}}}}},
"Failed to transform value from type '{'a': {'b': {'c': 'object'}}}' to type 'string' at path: ''",
),
],
ids=[
"simple_number_to_string",
"preserve_unexpected_fields",
"array_with_mixed_types",
"nested_list_conversion",
"array_in_nested_object",
"string_to_boolean_nested",
"empty_object",
"string_to_integer",
"skip_invalid_number_format",
"skip_ambiguous_types",
"null_to_string",
"preserve_null_when_allowed",
"very_nested_object_conversion",
"null_in_nested_structure",
"object_without_properties",
"array_without_items",
"non_array_to_array",
"number_to_array",
"null_to_array",
"null_preserved_for_nullable_array",
"number_to_string_array",
"string_fails_object_array",
"object_fails_array_with_string_array_items",
"non_object_root_schema",
"multiple_allowed_types",
"oneof_not_supported",
"facebook_cpc_number_conversion",
"object_fails_array_with_string_item",
"string_fails_object_conversion",
"string_fails_object_in_array",
"null_input_data",
"max_nesting_depth_protection",
],
)
def test_transform(schema, actual, expected, expected_warns, caplog):
t = TypeTransformer(TransformConfig.DefaultSchemaNormalization)
t.transform(actual, schema)
assert json.dumps(actual) == json.dumps(expected)
if expected_warns:
record = caplog.records[0]
assert record.name == "airbyte"
assert record.levelname == "WARNING"
assert record.message == expected_warns
else:
assert len(caplog.records) == 0
def test_transform_wrong_config():
with pytest.raises(Exception, match="NoTransform option cannot be combined with other flags."):
TypeTransformer(TransformConfig.NoTransform | TransformConfig.DefaultSchemaNormalization)
with pytest.raises(
Exception,
match="Please set TransformConfig.CustomSchemaNormalization config before registering custom normalizer",
):
class NotAStream:
transformer = TypeTransformer(TransformConfig.DefaultSchemaNormalization)
@transformer.registerCustomTransform
def transform_cb(instance, schema):
pass
def test_custom_transform():
class NotAStream:
transformer = TypeTransformer(TransformConfig.CustomSchemaNormalization)
@transformer.registerCustomTransform
def transform_cb(instance, schema):
# Check no default conversion applied
assert instance == 12
assert schema == SIMPLE_SCHEMA["properties"]["value"]
return "transformed"
s = NotAStream()
obj = {"value": 12}
s.transformer.transform(obj, SIMPLE_SCHEMA)
assert obj == {"value": "transformed"}
def test_custom_transform_with_default_normalization():
class NotAStream:
transformer = TypeTransformer(
TransformConfig.CustomSchemaNormalization | TransformConfig.DefaultSchemaNormalization
)
@transformer.registerCustomTransform
def transform_cb(instance, schema):
# Check default conversion applied
assert instance == "12"
assert schema == SIMPLE_SCHEMA["properties"]["value"]
return "transformed"
s = NotAStream()
obj = {"value": 12}
s.transformer.transform(obj, SIMPLE_SCHEMA)
assert obj == {"value": "transformed"}