"""Unit tests for SoundCloud tasks workflow.""" from unittest.mock import MagicMock from unittest.mock import patch from garcon_contrib.dynamo_feed_status import garcon_feed_status import pytest from feed_ingestion.flows.soundcloud import tasks _date = '2021-01-28' @pytest.fixture def feed_name(): """Feed name value.""" return 'soundcloud' @pytest.fixture def source_files_dict(): """Source files value.""" return { 'trackinformation': 'PLT2_D_20210128_20210128_Trackinformation.tsv', 'streamlevelreport': 'PLT2_D_20210128_20210128_Streamlevelreport.tsv', 'playlistreporting': 'PLT2_D_20210128_20210128_Playlistreporting.tsv', 'customeranalytics': 'PLT2_D_20210128_20210128_Customeranalytics.tsv', 'customerdata': 'PLT2_D_20210128_20210128_Customerdata.tsv', } @patch.object(tasks, 'garcon_feed_status') def test_bootstrap(garcon_feed_status_mock): """Test bootstrap.""" expected_bootstrap_response = { 'archive_path': 'SoundCloud/archives/2021/01/28/', 'date': '2021-01-28', 'drop_path': 'feed-drop/SoundCloud/2021/01/28/', 'feed_name': 'soundcloud_theorchard', 'licensor': 'theorchard', 'reports': ['Trackinformation', 'Streamlevelreport', 'Playlistreporting', 'Customeranalytics', 'Customerdata'], 's3_archive_path': 's3://dev-cucumbers/SoundCloud/archives/2021/01/28/', 'secrets_path': 'swf-soundcloud', 'source_files_dict': { 'Customeranalytics': 'PLT2_D_20210128_20210128_Customeranalytics.tsv', 'Customerdata': 'PLT2_D_20210128_20210128_Customerdata.tsv', 'Playlistreporting': 'PLT2_D_20210128_20210128_Playlistreporting.tsv', 'Streamlevelreport': 'PLT2_D_20210128_20210128_Streamlevelreport.tsv', 'Trackinformation': 'PLT2_D_20210128_20210128_Trackinformation.tsv' } } result = tasks.bootstrap(MagicMock(), _date, licensor='theorchard', 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.soundcloud.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.""" path = 'feed_ingestion.flows.soundcloud.' \ 'tasks.garcon_feed_status.delete_status' with patch(path) as delete_status: yield delete_status @pytest.fixture def mock_set_missing_files(): """Yield set_missing_files.""" path = ( 'feed_ingestion.flows.soundcloud.tasks.garcon_feed_status.' 'set_missing_files') with patch(path) as mock_set_missing: yield mock_set_missing @pytest.fixture def mock_s3_tasks(): """Mock s3_tasks.""" path = 'feed_ingestion.flows.soundcloud.tasks.s3_tasks' with patch(path) as mock_s3: yield mock_s3 def test_grab_drop_files( mock_s3_tasks, mock_set_overall_status, mock_task_status, mock_set_missing_files, source_files_dict): """Test grab_drop_files.""" activity = MagicMock() tasks.grab_drop_files( activity=activity, feed_name='soundcloud_theorchard', date=_date, source_files_dict=source_files_dict, drop_path='drop_path', archive_path='archive_path') mock_set_overall_status.assert_called_once_with( 'soundcloud_theorchard', _date, garcon_feed_status.STATUS_DOWNLOADED) mock_set_missing_files.assert_not_called() def test_grab_drop_files_sme( mock_s3_tasks, mock_set_overall_status, mock_task_status, mock_set_missing_files, source_files_dict): """Test grab_drop_files.""" activity = MagicMock() tasks.grab_drop_files_sme( activity=activity, feed_name='soundcloud_sme', date=_date, source_files_dict=source_files_dict, drop_path='drop_path', archive_path='archive_path') mock_set_overall_status.assert_called_once_with( 'soundcloud_sme', _date, garcon_feed_status.STATUS_DOWNLOADED) mock_set_missing_files.assert_not_called()