from typing import Any from unittest import mock import pytest from dmp.adapters.fivetran.models import StandardConfig from dmp.app_connections.enums import AppConnectionStatus from dmp.tiktok.handlers import ( SyncTikTokFivetranTablesStateHandler, SyncTikTokFivetranTablesStateRequest, ) from dmp.tiktok.models import TikTokAdReportingConnection from tests.unit.faker import FakerTyped from tests.unit.types import BuildModel, CreateModel test_schema_config_actual_tables: dict[str, Any] = { "enable_new_by_default": True, "schema_change_handling": "ALLOW_ALL", "schemas": { "tiktok_001": { "enabled": True, "name_in_destination": "tiktok_001", "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", }, "ad_history": { "enabled": True, "enabled_patch_settings": {"allowed": True}, "name_in_destination": "tender_transaction", }, }, } }, } test_schema_config_disable_table: dict[str, Any] = { "enable_new_by_default": True, "schema_change_handling": "ALLOW_ALL", "schemas": { "tiktok_001": { "enabled": True, "name_in_destination": "tiktok_001", "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", }, "ad_history": { "enabled": False, "enabled_patch_settings": {"allowed": True}, "name_in_destination": "tender_transaction", }, }, } }, } class TestSyncTikTokFivetranTablesStateHandler: @pytest.mark.db def test_sync_fivetran_tables_for_single_connection_patch_tables( self, handler: SyncTikTokFivetranTablesStateHandler, fivetran_client_mock: mock.MagicMock, create_model: CreateModel, build_model: BuildModel, fake: FakerTyped, ) -> None: connection_id = fake.pystr() connector_schema = build_model( StandardConfig, schemas=test_schema_config_disable_table.get("schemas") ) create_model( TikTokAdReportingConnection, status=AppConnectionStatus.CONNECTED, fivetran_connector_id=connection_id, ) fivetran_client_mock.get_connection_schema_config.return_value = ( connector_schema ) response = handler.handle( SyncTikTokFivetranTablesStateRequest( fivetran_connection_id=connection_id, ) ) assert response == {connection_id} fivetran_client_mock.patch_connection_schema_config.assert_called_once() @pytest.mark.db def test_sync_fivetran_tables_for_single_connection_without_patch_tables( self, handler: SyncTikTokFivetranTablesStateHandler, fivetran_client_mock: mock.MagicMock, create_model: CreateModel, build_model: BuildModel, fake: FakerTyped, ) -> None: connection_id = fake.pystr() connector_schema = build_model( StandardConfig, schemas=test_schema_config_actual_tables.get("schemas") ) create_model( TikTokAdReportingConnection, status=AppConnectionStatus.CONNECTED, fivetran_connector_id=connection_id, ) fivetran_client_mock.get_connection_schema_config.return_value = ( connector_schema ) response = handler.handle( SyncTikTokFivetranTablesStateRequest( fivetran_connection_id=connection_id, ) ) assert response == {connection_id} fivetran_client_mock.patch_connection_schema_config.assert_not_called() @pytest.mark.db def test_sync_fivetran_tables_for_all_connections( self, handler: SyncTikTokFivetranTablesStateHandler, fivetran_client_mock: mock.MagicMock, create_model: CreateModel, build_model: BuildModel, fake: FakerTyped, ) -> None: connection_id = fake.pystr() connector_schema = build_model( StandardConfig, schemas=test_schema_config_disable_table.get("schemas") ) create_model( TikTokAdReportingConnection, status=AppConnectionStatus.CONNECTED, fivetran_connector_id=connection_id, ) fivetran_client_mock.get_connection_schema_config.return_value = ( connector_schema ) response = handler.handle( SyncTikTokFivetranTablesStateRequest( fivetran_connection_id=None, ) ) assert response == {connection_id} fivetran_client_mock.patch_connection_schema_config.assert_called_once()