"""Unit tests for tasks of YouTube Red Marketshare Workflow.""" from unittest.mock import call, MagicMock from unittest.mock import patch import pytest from feed_ingestion.flows.youtube_red_marketshare import config from feed_ingestion.flows.youtube_red_marketshare import tasks @patch.object(tasks.garcon_feed_status, 'get_overall_status') @pytest.mark.parametrize( 'context, expected', [ ( dict(date='2018-05-06', reload=None), { 'feed_name': config.feed_name, 'secrets_path': config.secrets_path, 'date': '2018-05-01', 'report_start_date': '2018-05-01', 'report_end_date': '2018-05-31', 'monthly_staging_raw_tables': config.youtube_monthly_reports_and_tables, 'staging_raw_table': config.staging_raw_table } ) ]) def test_bootstrap(get_overall_status_mock, context, expected): """Test bootstrap task.""" context.update({'activity': MagicMock()}) result = tasks.bootstrap(**context) assert result == expected get_overall_status_mock.assert_called_with( 'youtube_red_marketshare', expected['report_start_date'] ) @patch.object(tasks.garcon_feed_status, 'get_overall_status') @pytest.mark.parametrize('context', [({ 'feed_name': 'youtube_red_marketshare', 'date': '2018-05-01', 'staging_raw_tables': { 'red_summary_v1_1': {'staging_raw_red_summary_music': 'staging_raw_youtube_monthly_red_summary_v1_1__music_summary'} }})]) def test_check_youtube_monthly_reports_status(get_overall_status_mock, context): """Test bootstrap task.""" context.update({'activity': MagicMock()}) tasks.check_youtube_monthly_reports_status(**context) get_overall_status_mock.assert_called_with( 'youtube_monthly_red_summary_v1_1', '2018-05-01' ) @patch.object(tasks, 'YouTubeRedMarketshareSF') def test_load_staging_raw_table(mock_executor_class): """Test load staging raw.""" activity = MagicMock() executor_mock = mock_executor_class.return_value.__enter__.return_value start_date = '2018-05-01' end_date = '2018-05-31' monthly_staging_raw_tables = { 'red_summary_v1_1': {'staging_raw_red_summary_music': 'test_red_summary_v1_1__music_summary'}, 'red_label_summary_v1_1': {'staging_raw_red_label_summary_music': 'test_red_label_summary_v1_1__music_summary', 'staging_raw_red_label_summary_subscribers': 'test_red_label_summary_v1_1__subscriber_summary'} } tasks.load_staging_raw_table( activity, feed_name='test_feed_name', start_date=start_date, end_date=end_date, monthly_staging_raw_tables=monthly_staging_raw_tables ) assert executor_mock.clean_staging_raw_table.call_args_list == [ call(start_date='2018-05-01')] assert executor_mock.load_staging_raw_table.call_args_list == [ call( staging_raw_red_summary_music='test_red_summary_v1_1__music_summary', # noqa: E501 staging_raw_red_label_summary_music='test_red_label_summary_v1_1__music_summary', # noqa: E501 staging_raw_red_label_summary_subscribers='test_red_label_summary_v1_1__subscriber_summary', # noqa: E501 first_day='2018-05-01', last_day='2018-05-31' ) ]