|
| 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