"""Unit tests for tasks of Apple Podcast Ratings Ingestion Workflow.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from feed_ingestion.flows.apple_podcasts_sales_summary import config from feed_ingestion.flows.apple_podcasts_sales_summary import tasks @pytest.fixture def mock_check_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 @patch('feed_ingestion.flows.apple_podcasts_sales_summary.' 'tasks.garcon_feed_status.get_overall_status') def test_bootstrap(mock_get_overall_status): """Check that bootstrap returns expected results.""" # Get the same structure for files dict as in bootstrap file_name = 'ApplePodcasts_SalesSummary_90421401_20200121.txt.gz' file_info = {'files': []} file_info['files'].append(dict( file_name=file_name )) context = { 'activity': MagicMock(), 'date': '2020-01-21', 'reload': True, 'vendor_name': 'SME' } expected = dict( feed_name=config.overall_feed_name, secrets_path=config.secrets_path, expected_file_name=file_name, staging_raw_table=config.staging_raw_table, s3_archive_path='s3://dev-apple-podcasts-reports/' 'subscription-sales-summary/', source_files_dict=file_info, date='2020-01-21', vendor_id=90421401, vendor_name='SME' ) reply = tasks.bootstrap(**context) mock_get_overall_status.assert_called_with( config.overall_feed_name, '2020-01-21' ) assert reply['s3_archive_path'] == expected['s3_archive_path'] assert reply == expected @patch('feed_ingestion.flows.apple_podcasts_sales_summary.tasks.' 'ApplePodcastsSalesSummarySF') def test_load_sales_summary_table( mock_ap_snowflake_executor, mock_check_status): """Check that load_sales_summary_table executes as expected.""" context = { 'activity': MagicMock(), 'date': '2020-01-21', 'staging_raw_table': 'staging_raw_table', 'vendor_id': 12345, 'vendor_name': 'test_vendor' } tasks.load_sales_summary_table(**context) mock_ap_snowflake_executor.return_value.__enter__().\ load_sales_summary_table.assert_called_with( 'staging_raw_table', '2020-01-21', 12345, 'test_vendor' ) mock_ap_snowflake_executor.return_value.__enter__().\ delete_from_staging_raw_sales_summary.assert_called_with( 'staging_raw_table', '2020-01-21', 12345 )