Skip to content

Commit 7881f9f

Browse files
committed
add tests for RemapField
1 parent 54f9f9f commit 7881f9f

File tree

2 files changed

+113
-0
lines changed
  • airbyte_cdk/sources/declarative/transformations/config_transformations
  • unit_tests/sources/declarative/transformations/config_transformations

2 files changed

+113
-0
lines changed

airbyte_cdk/sources/declarative/transformations/config_transformations/remap_field.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class RemapField(ConfigTransformation):
2121
field_path: List[Union[InterpolatedString, str]]
2222

2323
def __post_init__(self) -> None:
24+
if not self.field_path:
25+
raise Exception("field_path cannot be empty.")
2426
self._field_path = [
2527
InterpolatedString.create(path, parameters={}) for path in self.field_path
2628
]
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import pytest
2+
from unittest import TestCase
3+
from copy import deepcopy
4+
5+
from airbyte_cdk.sources.declarative.transformations.config_transformations.remap_field import (
6+
RemapField,
7+
)
8+
9+
10+
class TestRemapField(TestCase):
11+
def test_given_valid_inputs_when_transform_then_field_is_remapped(self):
12+
remap_transform = RemapField(
13+
field_path=["authorization", "auth_type"],
14+
map={"client_credentials": "oauth2", "api_key": "key_auth"},
15+
)
16+
17+
config = {
18+
"authorization": {
19+
"auth_type": "client_credentials",
20+
"client_id": "12345",
21+
"client_secret": "secret",
22+
}
23+
}
24+
original_config = deepcopy(config)
25+
26+
remap_transform.transform(config)
27+
28+
assert config["authorization"]["auth_type"] == "oauth2"
29+
assert original_config["authorization"]["auth_type"] == "client_credentials"
30+
assert config["authorization"]["client_id"] == original_config["authorization"]["client_id"]
31+
assert (
32+
config["authorization"]["client_secret"]
33+
== original_config["authorization"]["client_secret"]
34+
)
35+
36+
def test_given_value_not_in_map_when_transform_then_field_unchanged(self):
37+
remap_transform = RemapField(
38+
field_path=["authorization", "auth_type"],
39+
map={"client_credentials": "oauth2", "api_key": "key_auth"},
40+
)
41+
42+
config = {
43+
"authorization": {"auth_type": "basic_auth", "username": "user", "password": "pass"}
44+
}
45+
original_config = deepcopy(config)
46+
remap_transform.transform(config)
47+
48+
assert config["authorization"]["auth_type"] == "basic_auth"
49+
assert config == original_config
50+
51+
def test_given_field_path_not_in_config_when_transform_then_config_unchanged(self):
52+
remap_transform = RemapField(
53+
field_path=["authentication", "type"], # Not in config
54+
map={"client_credentials": "oauth2"},
55+
)
56+
config = {
57+
"authorization": { # Different key
58+
"auth_type": "client_credentials"
59+
}
60+
}
61+
original_config = deepcopy(config)
62+
63+
remap_transform.transform(config)
64+
65+
assert config == original_config
66+
67+
def test_given_interpolated_path_when_transform_then_field_is_remapped(self):
68+
remap_transform = RemapField(
69+
field_path=["auth_data", "{{ config['auth_field_name'] }}"],
70+
map={"basic": "basic_auth", "token": "bearer"},
71+
)
72+
73+
config = {
74+
"auth_field_name": "type",
75+
"auth_data": {"type": "token", "token_value": "abc123"},
76+
}
77+
78+
remap_transform.transform(config)
79+
80+
assert config["auth_data"]["type"] == "bearer"
81+
82+
def test_given_empty_map_when_transform_then_config_unchanged(self):
83+
remap_transform = RemapField(field_path=["authorization", "auth_type"], map={})
84+
85+
config = {"authorization": {"auth_type": "client_credentials", "client_id": "12345"}}
86+
original_config = deepcopy(config)
87+
88+
remap_transform.transform(config)
89+
90+
assert config == original_config
91+
92+
def test_given_empty_field_path_when_transform_then_raises_exception(self):
93+
with pytest.raises(Exception):
94+
RemapField(field_path=[], map={"old_value": "new_value"})
95+
96+
def test_multiple_transformations_applied_in_sequence(self):
97+
auth_type_transform = RemapField(
98+
field_path=["auth", "type"], map={"api_key": "key_auth", "oauth": "oauth2"}
99+
)
100+
101+
env_transform = RemapField(
102+
field_path=["environment"], map={"dev": "development", "prod": "production"}
103+
)
104+
105+
config = {"auth": {"type": "oauth", "credentials": "secret"}, "environment": "dev"}
106+
107+
auth_type_transform.transform(config)
108+
env_transform.transform(config)
109+
110+
assert config["auth"]["type"] == "oauth2"
111+
assert config["environment"] == "development"

0 commit comments

Comments
 (0)