"""Unit tests for Beatport tasks workflow.""" from unittest.mock import MagicMock from unittest.mock import patch from botocore.exceptions import ClientError from garcon_contrib.dynamo_feed_status import garcon_feed_status import pytest from feed_ingestion.flows.beatport import tasks _date = '2022-06-10' @pytest.fixture def feed_name(): """Feed name value.""" return 'beatport' @pytest.fixture def source_file_name(): """Source files value.""" return 'beatport_salesexport_report_2022-06-10.csv' @pytest.fixture def expected_bootstrap_response(): """Response for bootstrap task.""" return { 'feed_name': 'beatport', 'date': '2022-06-10', 'archive_path': 'beatport/archives/2022-06-10/', 'source_file_name': 'beatport_salesexport_report_2022-06-10.csv', 's3_archive_path': 's3://dev-cucumbers/beatport/archives/2022-06-10/', 'key_dir': 's3://dev-cucumbers/beatport/archives/2022-06-10/' 'beatport_salesexport_report_2022-06-10.csv', 'secrets_path': 'swf-beatport', 'staging_raw_table': 'staging_raw_beatport', 'temp_staging_raw_table': 'temp_staging_raw_beatport_2022_06_10' } def test_bootstrap(expected_bootstrap_response): """Test bootstrap.""" result = tasks.bootstrap(MagicMock(), _date, reload=False) assert result == expected_bootstrap_response @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 @pytest.fixture def mock_set_overall_status(): """Yield overall status.""" overall_status_path = ( 'feed_ingestion.flows.beatport.tasks.garcon_feed_status.' 'set_overall_status') with patch(overall_status_path) as overall_status: yield overall_status @pytest.fixture def mock_set_delete_status(): """Yield delete status.""" delete_status_path = ( 'feed_ingestion.flows.beatport.tasks.garcon_feed_status.' 'delete_status') with patch(delete_status_path) as delete_status: yield delete_status @pytest.fixture def mock_set_missing_files(): """Yield set_missing_files.""" path = ( 'feed_ingestion.flows.beatport.tasks.garcon_feed_status.' 'set_missing_files') with patch(path) as mock_set_missing: yield mock_set_missing @pytest.fixture def mock_boto3(): """Mock boto3.""" boto3_path = 'feed_ingestion.flows.beatport.tasks.boto3' with patch(boto3_path) as boto3: mock_client = MagicMock() boto3.client.return_value = mock_client yield mock_client @pytest.fixture def mock_download_file(): """Mock download_file.""" path = 'feed_ingestion.flows.beatport.tasks.BeatportAPI.download_file' with patch(path) as download_file: yield download_file def test_grab_drop_files( mock_boto3, mock_set_overall_status, mock_task_status, mock_download_file, mock_set_delete_status, mock_set_missing_files, source_file_name ): """Test grab_drop_files.""" activity = MagicMock() tasks.grab_drop_files( activity=activity, feed_name='beatport', date=_date, source_file_name=source_file_name, archive_path='archive_path') mock_download_file.assert_called_once() mock_boto3.upload_file.assert_called_once() def test_grab_drop_files_no_file( mock_boto3, mock_set_overall_status, mock_task_status, mock_download_file, mock_set_delete_status, mock_set_missing_files, source_file_name ): """Test grab_drop_files if there is no file.""" mock_boto3.upload_file.side_effect = \ ClientError(MagicMock(status=404), 'not found') activity = MagicMock() with pytest.raises(Exception): tasks.grab_drop_files( activity=activity, feed_name='beatport', date=_date, source_file_name=source_file_name, archive_path='archive_path') mock_download_file.assert_called_once() mock_boto3.upload_file.assert_called_once() mock_set_overall_status.assert_called_once_with( 'beatport', _date, garcon_feed_status.STATUS_NOT_AVAILABLE) mock_set_missing_files.assert_called_once_with( 'beatport', _date, [source_file_name])