-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathtest_spec.py
More file actions
272 lines (260 loc) · 11 KB
/
test_spec.py
File metadata and controls
272 lines (260 loc) · 11 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
#
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
#
import pytest
from airbyte_cdk.models import (
AdvancedAuth as model_advanced_auth,
)
from airbyte_cdk.models import (
AuthFlowType as model_auth_flow_type,
)
from airbyte_cdk.models import (
ConnectorSpecification as model_connector_spec,
)
from airbyte_cdk.models import (
OAuthConfigSpecification as model_declarative_oauth_spec,
)
from airbyte_cdk.models import (
OauthConnectorInputSpecification as model_declarative_oauth_connector_input_spec,
)
from airbyte_cdk.models import (
State as model_declarative_oauth_state,
)
from airbyte_cdk.sources.declarative.models.declarative_component_schema import (
AuthFlow as component_auth_flow,
)
from airbyte_cdk.sources.declarative.models.declarative_component_schema import (
AuthFlowType as component_auth_flow_type,
)
from airbyte_cdk.sources.declarative.models.declarative_component_schema import (
OAuthConfigSpecification as component_declarative_oauth_config_spec,
)
from airbyte_cdk.sources.declarative.models.declarative_component_schema import (
OauthConnectorInputSpecification as component_declarative_oauth_connector_input_spec,
)
from airbyte_cdk.sources.declarative.models.declarative_component_schema import (
State as component_declarative_oauth_state,
)
from airbyte_cdk.sources.declarative.spec.spec import ConfigMigration
from airbyte_cdk.sources.declarative.spec.spec import Spec as component_spec
from airbyte_cdk.sources.declarative.transformations.add_fields import AddedFieldDefinition
from airbyte_cdk.sources.declarative.transformations.config_transformations.add_fields import (
ConfigAddFields,
)
from airbyte_cdk.sources.declarative.transformations.config_transformations.remap_field import (
ConfigRemapField,
)
from airbyte_cdk.sources.declarative.transformations.config_transformations.remove_fields import (
ConfigRemoveFields,
)
from airbyte_cdk.sources.declarative.validators.dpath_validator import DpathValidator
from airbyte_cdk.sources.declarative.validators.validate_adheres_to_schema import (
ValidateAdheresToSchema,
)
@pytest.mark.parametrize(
"spec, expected_connection_specification",
[
(
component_spec(connection_specification={"client_id": "my_client_id"}, parameters={}),
model_connector_spec(connectionSpecification={"client_id": "my_client_id"}),
),
(
component_spec(
connection_specification={"client_id": "my_client_id"},
parameters={},
documentation_url="https://airbyte.io",
),
model_connector_spec(
connectionSpecification={"client_id": "my_client_id"},
documentationUrl="https://airbyte.io",
),
),
(
component_spec(
connection_specification={"client_id": "my_client_id"},
parameters={},
advanced_auth=component_auth_flow(
auth_flow_type=component_auth_flow_type.oauth2_0,
predicate_key=None,
predicate_value=None,
),
),
model_connector_spec(
connectionSpecification={"client_id": "my_client_id"},
advanced_auth=model_advanced_auth(
auth_flow_type=model_auth_flow_type.oauth2_0,
),
),
),
(
component_spec(
connection_specification={},
parameters={},
advanced_auth=component_auth_flow(
auth_flow_type=component_auth_flow_type.oauth2_0,
predicate_key=None,
predicate_value=None,
oauth_config_specification=component_declarative_oauth_config_spec(
oauth_connector_input_specification=component_declarative_oauth_connector_input_spec(
consent_url="https://domain.host.com/endpoint/oauth?{client_id_key}={{client_id_key}}&{redirect_uri_key}={urlEncoder:{{redirect_uri_key}}}&{state_key}={{state_key}}",
scope="reports:read campaigns:read",
access_token_headers={"Content-Type": "application/json"},
access_token_params={"{auth_code_key}": "{{auth_code_key}}"},
access_token_url="https://domain.host.com/endpoint/v1/oauth2/access_token/",
extract_output=["data.access_token"],
state=component_declarative_oauth_state(min=7, max=27),
client_id_key="my_client_id_key",
client_secret_key="my_client_secret_key",
scope_key="my_scope_key",
state_key="my_state_key",
auth_code_key="my_auth_code_key",
redirect_uri_key="callback_uri",
),
oauth_user_input_from_connector_config_specification=None,
complete_oauth_output_specification=None,
complete_oauth_server_input_specification=None,
complete_oauth_server_output_specification=None,
),
),
),
model_connector_spec(
connectionSpecification={},
advanced_auth=model_advanced_auth(
auth_flow_type=model_auth_flow_type.oauth2_0,
predicate_key=None,
predicate_value=None,
oauth_config_specification=model_declarative_oauth_spec(
oauth_connector_input_specification=model_declarative_oauth_connector_input_spec(
consent_url="https://domain.host.com/endpoint/oauth?{client_id_key}={{client_id_key}}&{redirect_uri_key}={urlEncoder:{{redirect_uri_key}}}&{state_key}={{state_key}}",
scope="reports:read campaigns:read",
access_token_headers={"Content-Type": "application/json"},
access_token_params={"{auth_code_key}": "{{auth_code_key}}"},
access_token_url="https://domain.host.com/endpoint/v1/oauth2/access_token/",
extract_output=["data.access_token"],
state=model_declarative_oauth_state(min=7, max=27),
client_id_key="my_client_id_key",
client_secret_key="my_client_secret_key",
scope_key="my_scope_key",
state_key="my_state_key",
auth_code_key="my_auth_code_key",
redirect_uri_key="callback_uri",
),
),
),
),
),
],
ids=[
"test_only_connection_specification",
"test_with_doc_url",
"test_auth_flow",
"test_declarative_oauth_flow",
],
)
def test_spec(spec, expected_connection_specification) -> None:
assert spec.generate_spec() == expected_connection_specification
def test_given_list_of_transformations_when_transform_config_then_config_is_transformed() -> None:
input_config = {"planet_code": "CRSC"}
expected_config = {
"planet_name": "Coruscant",
"planet_population": 3_000_000_000_000,
}
spec = component_spec(
connection_specification={},
parameters={},
config_transformations=[
ConfigAddFields(
fields=[
AddedFieldDefinition(
path=["planet_name"],
value="{{ config['planet_code'] }}",
value_type=None,
parameters={},
),
AddedFieldDefinition(
path=["planet_population"],
value="{{ config['planet_code'] }}",
value_type=None,
parameters={},
),
]
),
ConfigRemapField(
map={
"CRSC": "Coruscant",
},
field_path=["planet_name"],
config=input_config,
),
ConfigRemapField(
map={
"CRSC": 3_000_000_000_000,
},
field_path=["planet_population"],
config=input_config,
),
ConfigRemoveFields(
field_pointers=["planet_code"],
),
],
)
spec.transform_config(input_config)
assert input_config == expected_config
def test_given_valid_config_value_when_validating_then_no_exception_is_raised() -> None:
spec = component_spec(
connection_specification={},
parameters={},
config_validations=[
DpathValidator(
field_path=["test_field"],
strategy=ValidateAdheresToSchema(
schema={
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Test Spec",
"type": "object",
"required": [],
"additionalProperties": False,
"properties": {
"field_to_validate": {
"type": "string",
"title": "Name",
"description": "The name of the test spec",
"airbyte_secret": False,
}
},
}
),
)
],
)
input_config = {"test_field": {"field_to_validate": "test"}}
spec.validate_config(input_config)
def test_given_invalid_config_value_when_validating_then_exception_is_raised() -> None:
spec = component_spec(
connection_specification={},
parameters={},
config_validations=[
DpathValidator(
field_path=["test_field"],
strategy=ValidateAdheresToSchema(
schema={
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Test Spec",
"type": "object",
"required": [],
"properties": {
"field_to_validate": {
"type": "string",
"title": "Name",
"description": "The name of the test spec",
"airbyte_secret": False,
}
},
}
),
)
],
)
input_config = {"test_field": {"field_to_validate": 123}}
with pytest.raises(Exception):
spec.validate_config(input_config)