-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest_add_fields.py
196 lines (191 loc) · 5.72 KB
/
test_add_fields.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
#
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
#
from typing import Any, List, Mapping, Optional, Tuple
import pytest
from airbyte_cdk.sources.declarative.transformations import AddFields
from airbyte_cdk.sources.declarative.transformations.add_fields import AddedFieldDefinition
from airbyte_cdk.sources.types import FieldPointer
@pytest.mark.parametrize(
["input_record", "field", "field_type", "condition", "kwargs", "expected"],
[
pytest.param(
{"k": "v"},
[(["path"], "static_value")],
None,
"",
{},
{"k": "v", "path": "static_value"},
id="add new static value",
),
pytest.param(
{"k": "v"},
[(["path"], "{{ 1 }}")],
None,
"",
{},
{"k": "v", "path": 1},
id="add an expression evaluated as a number",
),
pytest.param(
{"k": "v"},
[(["path"], "{{ 1 }}")],
str,
"",
{},
{"k": "v", "path": "1"},
id="add an expression evaluated as a string using the value_type field",
),
pytest.param(
{"k": "v"},
[(["path"], "static_value"), (["path2"], "static_value2")],
None,
"",
{},
{"k": "v", "path": "static_value", "path2": "static_value2"},
id="add new multiple static values",
),
pytest.param(
{"k": "v"},
[(["nested", "path"], "static_value")],
None,
"",
{},
{"k": "v", "nested": {"path": "static_value"}},
id="set static value at nested path",
),
pytest.param(
{"k": "v"},
[(["k"], "new_value")],
None,
"",
{},
{"k": "new_value"},
id="update value which already exists",
),
pytest.param(
{"k": [0, 1]},
[(["k", 3], "v")],
None,
"",
{},
{"k": [0, 1, None, "v"]},
id="Set element inside array",
),
pytest.param(
{"k": "v"},
[(["k2"], '{{ config["shop"] }}')],
None,
"",
{"config": {"shop": "in-n-out"}},
{"k": "v", "k2": "in-n-out"},
id="set a value from the config using bracket notation",
),
pytest.param(
{"k": "v"},
[(["k2"], "{{ config.shop }}")],
None,
"",
{"config": {"shop": "in-n-out"}},
{"k": "v", "k2": "in-n-out"},
id="set a value from the config using dot notation",
),
pytest.param(
{"k": "v"},
[(["k2"], '{{ stream_slice["start_date"] }}')],
None,
"",
{"stream_slice": {"start_date": "oct1"}},
{"k": "v", "k2": "oct1"},
id="set a value from the stream slice using bracket notation",
),
pytest.param(
{"k": "v"},
[(["k2"], "{{ stream_slice.start_date }}")],
None,
"",
{"stream_slice": {"start_date": "oct1"}},
{"k": "v", "k2": "oct1"},
id="set a value from the stream slice using dot notation",
),
pytest.param(
{"k": "v"},
[(["k2"], "{{ record.k }}")],
None,
"",
{},
{"k": "v", "k2": "v"},
id="set a value from a field in the record using dot notation",
),
pytest.param(
{"k": "v"},
[(["k2"], '{{ record["k"] }}')],
None,
"",
{},
{"k": "v", "k2": "v"},
id="set a value from a field in the record using bracket notation",
),
pytest.param(
{"k": {"nested": "v"}},
[(["k2"], "{{ record.k.nested }}")],
None,
"",
{},
{"k": {"nested": "v"}, "k2": "v"},
id="set a value from a nested field in the record using bracket notation",
),
pytest.param(
{"k": {"nested": "v"}},
[(["k2"], '{{ record["k"]["nested"] }}')],
None,
"",
{},
{"k": {"nested": "v"}, "k2": "v"},
id="set a value from a nested field in the record using bracket notation",
),
pytest.param(
{"k": "v"},
[(["k2"], "{{ 2 + 2 }}")],
None,
"",
{},
{"k": "v", "k2": 4},
id="set a value from a jinja expression",
),
pytest.param(
{"k": "v"},
[(["path"], "static_value")],
None,
"{{ False }}",
{},
{"k": "v"},
id="do not add any field if condition is boolean False",
),
pytest.param(
{"k": "v"},
[(["path"], "static_value")],
None,
"{{ True }}",
{},
{"k": "v", "path": "static_value"},
id="add all fields if condition is boolean True",
),
],
)
def test_add_fields(
input_record: Mapping[str, Any],
field: List[Tuple[FieldPointer, str]],
field_type: Optional[str],
condition: Optional[str],
kwargs: Mapping[str, Any],
expected: Mapping[str, Any],
):
inputs = [
AddedFieldDefinition(path=v[0], value=v[1], value_type=field_type, parameters={})
for v in field
]
AddFields(fields=inputs, condition=condition, parameters={"alas": "i live"}).transform(
input_record, **kwargs
)
assert input_record == expected