"""Test cases for Delphi CRM tasks.""" from unittest.mock import MagicMock, patch, call import pytest from feed_ingestion.flows.delphi_crm import tasks @pytest.fixture def mock_task_status(): """Yield task status.""" task_status_path = 'feed_ingestion.tasks.task_status' with patch(task_status_path) as task_status: task_status.is_completed_task.return_value = False task_status.mark_completed_task = MagicMock() task_status.get_values = MagicMock() yield task_status @patch.object(tasks.garcon_feed_status, 'get_overall_status') def test_bootstrap_already_ingested(garcon_feed_status_mock): """Test bootstrap function.""" garcon_feed_status_mock.return_value = 'INGESTED' result = tasks.bootstrap( activity=MagicMock(), date='2024-07-01', report='complaint_tracking', reload=None, ) assert result == { 'stop': True, 'message': 'delphi_crm_complaint_tracking' ' is already ingested for 2024-07-01'} @pytest.mark.parametrize('report', ['not_existing', None]) @patch.object(tasks.garcon_feed_status, 'get_overall_status') def test_bootstrap_no_report(garcon_feed_status_mock, report): """Test bootstrap function.""" with pytest.raises(AssertionError): tasks.bootstrap( activity=MagicMock(), date='2024-07-01', report=report, reload=None, ) @patch.object(tasks, 'garcon_feed_status') def test_bootstrap(garcon_feed_status_mock): """Test bootstrap function.""" result = tasks.bootstrap( activity=MagicMock(), date='2024-07-01', report='complaint_tracking', reload=None, ) assert result == { 'date': '2024-07-01', 'drop_bucket': 'dev-feed-drop', 'drop_path': 'salesforce-marketing-cloud/', 'feed_name': 'delphi_crm_complaint_tracking', 'file_pattern': '.*\\/Complaint_Tracking_Data_Daily_2024_07_01.csv', 'report_name': 'complaint_tracking', 's3_dir_path': 's3://dev-feed-drop/salesforce-marketing-cloud/', 'secrets_path': 'delphi_crm', 'source_file_name': 'Complaint_Tracking_Data_Daily_2024_07_01.csv', 'staging_raw_table': 'SFMC_COMPLAINT_TRACKING'} @patch.object(tasks, 'DelphiCrmSnowflakeExecutor') def test_load_staging_raw_table_reports(executor_class_mock, mock_task_status): """Test load_staging_raw_table_reports function.""" activity = MagicMock() executor = executor_class_mock.return_value.__enter__.return_value tasks.load_staging_raw_table_reports( activity=activity, feed_name='delphi_crm_complaint_tracking', date='2024-07-01', report_name='complaint_tracking', s3_dir_path='s3://dev-cucumbers/delphi_crm' '/complaint_tracking/2024-07-01/', staging_raw_table_name='SFMC_COMPLAINT_TRACKING', file_name='Complaint_Tracking_Data_Daily_2024_07_01.csv', ) assert executor.method_calls == [ call.create_temp_staging_raw_table( report_name='complaint_tracking', temp_staging_raw_table='temp_SFMC_COMPLAINT_TRACKING_20240701'), call.load_temp_staging_raw_table( temp_staging_raw_table='temp_SFMC_COMPLAINT_TRACKING_20240701', file_name=['Complaint_Tracking_Data_Daily_2024_07_01.csv'], s3_dir_path='s3://dev-cucumbers/delphi_crm' '/complaint_tracking/2024-07-01/'), call.clean_staging_raw_table( staging_raw_table='SFMC_COMPLAINT_TRACKING', date='2024-07-01'), call.load_staging_raw_table( staging_raw_table='SFMC_COMPLAINT_TRACKING', temp_staging_raw_table='temp_SFMC_COMPLAINT_TRACKING_20240701', report_name='complaint_tracking', date='2024-07-01', file_name='Complaint_Tracking_Data_Daily_2024_07_01.csv'), call.drop_table( table='temp_SFMC_COMPLAINT_TRACKING_20240701') ]