"""Unit tests for tasks of Apple podcast subs snapshot monthly Ingestion.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from feed_ingestion.flows.apple_podcasts_subscription_snapshot_monthly \ import config from feed_ingestion.flows.apple_podcasts_subscription_snapshot_monthly \ 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_subscription_snapshot_monthly.' 'tasks.garcon_feed_status.get_overall_status') def test_bootstrap_for_daily_report(mock_get_overall_status): """Check that bootstrap returns expected results for daily report.""" # Get the same structure for files dict as in bootstrap file_name = 'ApplePodcasts_SubscriptionSnapshot_90421401_20221031.txt.gz' file_info = {'files': []} file_info['files'].append(dict( file_name=file_name )) final_feed_name = '_'.join([ config.overall_feed_name, 'Daily', 'SME']) context = { 'activity': MagicMock(), 'date': '2022-10-31', 'reload': True, 'source': 'Daily', 'vendor_name': 'SME' } expected = dict( feed_name=final_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-snapshot-monthly/', source_files_dict=file_info, date='2022-10-31', source='Daily', vendor_id=90421401, vendor_name='SME' ) reply = tasks.bootstrap(**context) mock_get_overall_status.assert_called_with( final_feed_name, '2022-10-31' ) assert reply['s3_archive_path'] == expected['s3_archive_path'] assert reply == expected @patch('feed_ingestion.flows.apple_podcasts_subscription_snapshot_monthly.' 'tasks.garcon_feed_status.get_overall_status') def test_bootstrap_for_weekly_report(mock_get_overall_status): """Check that bootstrap returns expected results for weekly report.""" # Get the same structure for files dict as in bootstrap file_name = 'ApplePodcasts_SubscriptionSnapshot_90421401_20221031.txt.gz' file_info = {'files': []} file_info['files'].append(dict( file_name=file_name )) final_feed_name = '_'.join([ config.overall_feed_name, 'Weekly', 'SME']) context = { 'activity': MagicMock(), 'date': '2022-10-31', 'reload': True, 'source': 'Weekly', 'vendor_name': 'SME' } expected = dict( feed_name=final_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-snapshot-monthly/', source_files_dict=file_info, date='2022-10-31', source='Weekly', vendor_id=90421401, vendor_name='SME' ) reply = tasks.bootstrap(**context) mock_get_overall_status.assert_called_with( final_feed_name, '2022-10-31' ) assert reply['s3_archive_path'] == expected['s3_archive_path'] assert reply == expected @patch('feed_ingestion.flows.apple_podcasts_subscription_snapshot_monthly.' 'tasks.garcon_feed_status.get_overall_status') def test_bootstrap_for_monthly_report(mock_get_overall_status): """Check that bootstrap returns expected results for monthly report.""" # Get the same structure for files dict as in bootstrap file_name = 'ApplePodcasts_SubscriptionSnapshot_92655920_20221031.txt.gz' file_info = {'files': []} file_info['files'].append(dict( file_name=file_name )) final_feed_name = '_'.join([ config.overall_feed_name, 'Monthly', 'POD_SUB_LLC']) context = { 'activity': MagicMock(), 'date': '2022-10-31', 'reload': True, 'source': 'Monthly', 'vendor_name': 'POD_SUB_LLC' } expected = dict( feed_name=final_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-snapshot-monthly/', source_files_dict=file_info, date='2022-10-31', source='Monthly', vendor_id=92655920, vendor_name='POD_SUB_LLC' ) reply = tasks.bootstrap(**context) mock_get_overall_status.assert_called_with( final_feed_name, '2022-10-31' ) assert reply['s3_archive_path'] == expected['s3_archive_path'] assert reply == expected @patch('feed_ingestion.flows.apple_podcasts_subscription_snapshot_monthly.' 'tasks.ApplePodcastsSubsSnapshotMonthlySF') def test_load_subscription_snapshot_monthly_table( mock_ap_snowflake_executor, mock_check_status): """Check load_subscription_snapshot_monthly_table executes as expected.""" context = { 'activity': MagicMock(), 'feed_name': 'test-feed', 'date': '2022-10-31', 'staging_raw_table': 'staging_raw_table', 'source': 'Monthly', 'vendor_id': 12345, 'vendor_name': 'test_vendor' } tasks.load_subscription_snapshot_monthly_table(**context) mock_ap_snowflake_executor.return_value.__enter__().\ load_subscription_snapshot_monthly_table.assert_called_with( 'staging_raw_table', '2022-10-31', 'Monthly', 12345, 'test_vendor' ) mock_ap_snowflake_executor.return_value.__enter__().\ delete_from_staging_raw_subscription_snapshot_monthly.\ assert_called_with('staging_raw_table', '2022-10-31', 'Monthly', 12345) @patch('feed_ingestion.flows.apple_podcasts_subscription_snapshot_monthly.' 'tasks.ApplePodcastsSubsSnapshotMonthlySF') def test_delete_data_for_old_source( mock_ap_snowflake_executor, mock_check_status): """Check delete_data_for_old_source executes as expected.""" context = { 'activity': MagicMock(), 'feed_name': 'test-feed', 'date': '2022-10-30', 'source': 'Weekly', 'vendor_id': 12345 } tasks.delete_data_for_old_source(**context) mock_ap_snowflake_executor.return_value.__enter__().\ delete_data_for_old_source.assert_called_with( '2022-10-24', '2022-10-30', ('Daily',), 12345 ) context = { 'activity': MagicMock(), 'feed_name': 'test-feed', 'date': '2022-10-31', 'source': 'Monthly', 'vendor_id': 12345 } tasks.delete_data_for_old_source(**context) mock_ap_snowflake_executor.return_value.__enter__().\ delete_data_for_old_source.assert_called_with( '2022-10-01', '2022-10-31', ('Daily', 'Weekly'), 12345 )