"""Unit tests for tasks of YouTube Video and Claim Summary Workflow.""" from datetime import date 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.youtube_video_and_claim_summary import config, tasks @pytest.fixture def context(): """Context for YouTube Video and Claim Summary tasks.""" return { 'activity': MagicMock(), 'date': '2017-11-01', 'report_name': 'test_report', 'reload': False} @pytest.fixture def expected_bootstrap_response(context): """Response for bootstrap task.""" return { 'date': '2017-11-01', 'feed_name': config.feed_name, 's3_archive_path': 's3://dev-cucumbers/Youtube_weekly/archives_v1_1/2017-11-01/', 's3_download_path': 's3://dev-cucumbers/Youtube_weekly/downloads_v1_1/2017-11-01/', 'source_files_dict': {'files': [ {'file_name': 'YouTube_theorchardmusic_' 'W_20171101_test_report_v1-1.csv.gz'}]}, 'report_name': 'test_report', 'staging_raw_table': 'staging_raw_youtube_test_report', 'report_status_name': 'youtube_video_and_claim_summary_weekly_test_report'} @patch('feed_ingestion.flows.youtube_video_and_claim_summary.' 'tasks.garcon_feed_status.get_overall_status', return_value=garcon_feed_status.STATUS_NOT_INGESTED) def test_bootstrap( mock_get_overall_status, context, expected_bootstrap_response): """Test bootstrap task.""" result = tasks.bootstrap(**context) assert result == expected_bootstrap_response @patch('feed_ingestion.flows.youtube_video_and_claim_summary' '.tasks.date_module') @patch('feed_ingestion.flows.youtube_video_and_claim_summary.' 'tasks.garcon_feed_status.get_overall_status', return_value=garcon_feed_status.STATUS_NOT_INGESTED) def test_bootstrap_no_date_is_passed( mock_get_overall_status, mock_date_module, context): """Date should be set to today if it is not provided.""" context['date'] = None mock_date_module.today.return_value = date(2000, 1, 1) assert tasks.bootstrap(**context)['date'] == '2000-01-01' @patch('feed_ingestion.flows.youtube_video_and_claim_summary.' 'tasks.garcon_feed_status.get_overall_status', return_value=garcon_feed_status.STATUS_NOT_INGESTED) def test_bootstrap_date_is_passed( mock_get_overall_status, context, expected_bootstrap_response): """Date should be returned if it is provided.""" context['date'] = '2017-11-01' expected_bootstrap_response['date'] = '2017-11-01' assert tasks.bootstrap(**context) == expected_bootstrap_response @patch('feed_ingestion.flows.youtube_video_and_claim_summary.' 'tasks.garcon_feed_status.get_overall_status', return_value=garcon_feed_status.STATUS_INGESTED) def test_bootstrap_should_return_stop_if_already_ingested( mock_get_overall_status, context): """Return stop when overall feed status is already ingested.""" expected_bootstrap_response = {'stop': True} result = tasks.bootstrap(**context) mock_get_overall_status.assert_called_with( '_'.join([config.feed_name, 'test_report']), context['date']) assert result == expected_bootstrap_response