"""Unit tests for SME Latam tasks workflow.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from feed_ingestion.flows.sme_latam import tasks _date = '2023-01-09' @pytest.fixture def expected_bootstrap_response(): """Response for bootstrap task.""" filename = 'BMAT - Sony - Sony Latam Unified Weekly Chart - Week 2.xlsx' processed_filename = ( 'BMAT - Sony - Sony Latam Unified Weekly Chart - Week 2.csv') return { 'feed_name': 'sme_latam', 'date': '2023-01-09', 'secrets_path': 'sme_latam', 'archive_path': f'sme_latam/archives/2023/2/{filename}', 'drop_path': f'ftp/sme-data-drop/2023/{filename}', 'processed_path': f'sme_latam/process/2023/{processed_filename}', 'staging_raw_table': 'staging_raw_sme_latam_weekly_unified_chart' } @pytest.fixture def mock_get_overall_status(): """Yield get overall status.""" overall_status_path = ( 'feed_ingestion.flows.sme_latam.tasks.garcon_feed_status.' 'get_overall_status') with patch(overall_status_path) as overall_status: yield overall_status @pytest.fixture def mock_set_overall_status(): """Yield overall status.""" overall_status_path = ( 'feed_ingestion.flows.sme_latam.tasks.garcon_feed_status.' 'set_overall_status') with patch(overall_status_path) as overall_status: yield overall_status @pytest.fixture def mock_delete_status(): """Yield delete overall status.""" overall_status_path = ( 'feed_ingestion.flows.sme_latam.tasks.garcon_feed_status.' 'delete_status') with patch(overall_status_path) as overall_status: yield overall_status @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() yield task_status def test_bootstrap_reload( expected_bootstrap_response, mock_get_overall_status, mock_delete_status): """Test bootstrap task when reload = True.""" result = tasks.bootstrap(MagicMock(), _date, reload='True') assert result == expected_bootstrap_response mock_delete_status.assert_called_with('sme_latam', _date) def test_bootstrap( expected_bootstrap_response, mock_get_overall_status, mock_delete_status): """Test bootstrap task.""" result = tasks.bootstrap(MagicMock(), _date, reload=False) assert result == expected_bootstrap_response mock_delete_status.assert_not_called() @pytest.fixture def mock_s3_tasks(): """Yield overall status.""" path = 'feed_ingestion.flows.sme_latam.tasks.s3_tasks' with patch(path) as mock_s3: yield mock_s3 def test_fetch_from_drop_location_file_is_not_available( mock_task_status, mock_set_overall_status, mock_s3_tasks): """Test test_grab_drop_files when a file is not available.""" mock_s3_tasks.copy_file.return_value = {} result = tasks.fetch_from_drop_location( MagicMock(), 'feed_name', _date, drop_path='ftp/sme-data-drop/2023/filename.xlsx', archive_path='sme_latam/archives/2023/2/filename.xlsx') assert result == { 'stop': True, 'message': 'Missing file ftp/sme-data-drop/2023/filename.xlsx' } mock_set_overall_status.assert_called_with( 'feed_name', _date, 'NOT_AVAILABLE') def test_fetch_from_drop_location_file_is_available( mock_task_status, mock_set_overall_status, mock_s3_tasks): """Test test_grab_drop_files when a file is available.""" mock_s3_tasks.copy_file.return_value = {'filename.xlsx': True} result = tasks.fetch_from_drop_location( MagicMock(), 'feed_name', _date, drop_path='ftp/sme-data-drop/2023/filename.xlsx', archive_path='sme_latam/archives/2023/2/filename.xlsx') assert result == {'filename.xlsx': True} mock_set_overall_status.assert_called_with( 'feed_name', _date, 'DOWNLOADED')