from collections.abc import Iterable from typing import Any from dmp.adapters.fivetran.models import ( StandardConfig, StandardConfigUpdate, ) from dmp.app_connections.constants import APP_CONNECTION_ENABLED_TABLES from dmp.app_connections.enums import AppConnectionPlatform from dmp.app_connections.services import AppConnectionService base_schema_config: dict[str, Any] = { "enable_new_by_default": True, "schema_change_handling": "ALLOW_ALL", "schemas": { "store_7123_test_label_001": { "enabled": True, "name_in_destination": "store_7123_test_label_001_manual", "tables": { "abandoned_checkout_applied_discount": { "enabled": True, "enabled_patch_settings": { "allowed": False, "reason_code": "NOT_SELECTABLE_CHILD_TABLE", }, "name_in_destination": "abandoned_checkout_applied_discount", }, "tender_transaction": { "enabled": True, "enabled_patch_settings": {"allowed": True}, "name_in_destination": "tender_transaction", }, }, } }, } base_schema_for_patch: dict[str, Any] = { "schemas": { "store_7123_test_label_001": { "tables": { "tender_transaction": {"enabled": False}, } } } } default_disabled_table_state: dict[str, Any] = { "enabled": False, "enabled_patch_settings": {"allowed": True}, } default_enabled_table_state: dict[str, Any] = { "enabled": True, "enabled_patch_settings": {"allowed": True}, } class TestAppConnectionService: def test_build_connection_schema_config_method( self, service: AppConnectionService ) -> None: enabled_tables: Iterable[str] = APP_CONNECTION_ENABLED_TABLES.get( AppConnectionPlatform.SHOPIFY, () ) existed_schema_config = base_schema_config.copy() existed_schema_config["schemas"]["store_7123_test_label_001"]["tables"].update( **{ table.lower(): { **default_disabled_table_state, "name_in_destination": table.lower(), } for table in enabled_tables } ) schema_for_patch = base_schema_for_patch.copy() schema_for_patch["schemas"]["store_7123_test_label_001"]["tables"].update( **{table.lower(): {"enabled": True} for table in enabled_tables} ) existed_schema_conf = StandardConfig.model_validate(existed_schema_config) standard_config_update = StandardConfigUpdate.model_validate(schema_for_patch) schema = service._build_schema_config(existed_schema_conf, enabled_tables) assert schema == standard_config_update def test_is_config_changed_method(self, service: AppConnectionService) -> None: enabled_tables: Iterable[str] = APP_CONNECTION_ENABLED_TABLES.get( AppConnectionPlatform.SHOPIFY, () ) existed_schema_config = base_schema_config.copy() existed_schema_config["schemas"]["store_7123_test_label_001"]["tables"].update( **{ table.lower(): { **default_enabled_table_state, "name_in_destination": table.lower(), } for table in enabled_tables } ) schema_for_patch = base_schema_for_patch.copy() schema_for_patch["schemas"]["store_7123_test_label_001"]["tables"].update( **{table.lower(): {"enabled": True} for table in enabled_tables} ) existed_schema_conf = StandardConfig.model_validate(existed_schema_config) standard_config_update = StandardConfigUpdate.model_validate(schema_for_patch) config_changed = service._is_config_schema_changed( existed_schema_conf, standard_config_update ) assert config_changed def test_is_config_changed_method_without_changes( self, service: AppConnectionService ) -> None: existed_schema_config = base_schema_config.copy() enabled_tables: Iterable[str] = APP_CONNECTION_ENABLED_TABLES.get( AppConnectionPlatform.SHOPIFY, () ) existed_schema_config["schemas"]["store_7123_test_label_001"]["tables"].update( **{ table.lower(): { **default_enabled_table_state, "name_in_destination": table.lower(), } for table in enabled_tables } ) schema_for_patch = base_schema_for_patch.copy() schema_for_patch["schemas"]["store_7123_test_label_001"]["tables"].update( **{table.lower(): {"enabled": True} for table in enabled_tables} ) schema_for_patch["schemas"]["store_7123_test_label_001"]["tables"][ "tender_transaction" ] = {"enabled": True} existed_schema_conf = StandardConfig.model_validate(existed_schema_config) standard_config_update = StandardConfigUpdate.model_validate(schema_for_patch) config_changed = service._is_config_schema_changed( existed_schema_conf, standard_config_update ) assert not config_changed