"""Unit tests for tasks of YouTube Video Report Workflow.""" from unittest.mock import MagicMock from unittest.mock import patch from freezegun import freeze_time import pytest from feed_ingestion.flows.youtube_video import config from feed_ingestion.flows.youtube_video import tasks _date = '2018-11-16' @pytest.fixture def mock_executor_context(): """Yield executor context.""" sf_executor_class_path = \ 'feed_ingestion.flows.youtube_video.tasks.YoutubeVideo' with patch(sf_executor_class_path) as sf_executor: mock_executor_context = sf_executor.return_value.__enter__.return_value yield mock_executor_context class TestBootstrap(object): """Test bootstrap.""" @pytest.fixture def expected_bootstrap_response(self): """Response for bootstrap task.""" return { 'date': '2018-11-16', 'feed_name': 'youtube_video_theorchard', 'licensor': 'theorchard', 'credentials_path': config.credentials_paths.get('theorchard'), 'replace_archive_files': True, 'report_name': 'content_owner_video_metadata_a4', 's3_archive_path': 'Youtube_video_report/archives' '/2018-11-16/theorchard/', 's3_bucket': 'dev-cucumbers', 's3_dir_path': 's3://dev-cucumbers/Youtube_video_report' '/archives/2018-11-16/theorchard/', 's3_download_path': 'Youtube_video_report/downloads/', 'cms_dict': config.cms_dict, 'secrets_path': 'youtube_video', 'skip_corrupted_rows': 'False', 'source_files_path': 'Youtube_video_report' '/archives/2018-11-16/theorchard/', 'source_files_pattern': '^content_owner_video_metadata_a4\\.\\w+\\.csv\\.gz$', 'channel_names_table': 'dim_youtube_channel_names', 'temp_staging_raw_table': 'temp_staging_raw_youtube_video_report_theorchard_20181116' } @freeze_time(_date) @patch('feed_ingestion.flows.youtube_video.tasks.garcon_feed_status') def test_bootstrap_no_date_is_passed( self, feed_status_mock, expected_bootstrap_response): """Date should be set to today if it is not provided.""" context = { 'activity': MagicMock(), 'date': None, 'reload': 'False', 'skip_corrupted_rows': 'False'} result = tasks.bootstrap(**context) assert result == expected_bootstrap_response @patch('feed_ingestion.flows.youtube_video.tasks.garcon_feed_status') def test_bootstrap_date_is_passed( self, feed_status_mock, expected_bootstrap_response): """Date should be returned if it is provided.""" result = tasks.bootstrap(MagicMock(), date=_date, reload='False', skip_corrupted_rows='False') assert result == expected_bootstrap_response @patch('feed_ingestion.flows.youtube_video.tasks.YoutubeVideo') @patch('feed_ingestion.flows.youtube_video.tasks.get_sf_config') def test_clean_staging_raw_table(mock_get_sf_config, mock_sf_executor_class): """Test clean_staging_raw_table task.""" mock_sf_config = {'db': 'DB', 'schema': 'SCHEMA'} mock_get_sf_config.return_value = mock_sf_config mock_sf_executor = MagicMock() mock_sf_executor_class.return_value.__enter__.return_value = \ mock_sf_executor activity = MagicMock() tasks.clean_staging_raw_table( activity=activity, sfdb_params=mock_sf_config, feed_name='youtube_video', licensor='theorchard') mock_sf_executor.clean_staging_raw_table.assert_called_with('theorchard') @patch('feed_ingestion.flows.youtube_video.tasks.YoutubeVideo') @patch('feed_ingestion.flows.youtube_video.tasks.get_sf_config') def test_load_staging_raw_table(mock_get_sf_config, mock_sf_executor_class): """Test drop_temp_table task.""" mock_sf_config = {'db': 'DB', 'schema': 'SCHEMA'} mock_get_sf_config.return_value = mock_sf_config mock_sf_executor = MagicMock() mock_sf_executor_class.return_value.__enter__.return_value = \ mock_sf_executor activity = MagicMock() tasks.load_staging_raw_table(activity=activity, feed_name='youtube_video', sfdb_params=mock_sf_config, temp_staging_raw_table='test_table') mock_sf_executor.load_staging_raw_table.assert_called_with('test_table') @patch('feed_ingestion.flows.youtube_video.tasks.YoutubeVideo') @patch('feed_ingestion.flows.youtube_video.tasks.get_sf_config') def test_update_staging_raw_table(mock_get_sf_config, mock_sf_executor_class): """Test clean_staging_raw_table task.""" mock_sf_config = {'db': 'DB', 'schema': 'SCHEMA'} mock_get_sf_config.return_value = mock_sf_config mock_sf_executor = MagicMock() mock_sf_executor_class.return_value.__enter__.return_value = \ mock_sf_executor activity = MagicMock() tasks.update_staging_raw_table( activity=activity, date=_date, feed_name='youtube_video', sfdb_params=mock_sf_config, licensor='theorchard') mock_sf_executor.update_staging_raw_table.assert_called_with( '2018-11-16', licensor='theorchard') @patch('feed_ingestion.flows.youtube_video.tasks.YoutubeVideo') @patch('feed_ingestion.flows.youtube_video.tasks.get_sf_config') def test_drop_temp_table(mock_get_sf_config, mock_sf_executor_class): """Test drop_temp_table task.""" mock_sf_config = {'db': 'DB', 'schema': 'SCHEMA'} mock_get_sf_config.return_value = mock_sf_config mock_sf_executor = MagicMock() mock_sf_executor_class.return_value.__enter__.return_value = \ mock_sf_executor activity = MagicMock() tasks.drop_temp_table(activity=activity, temp_staging_raw_table='test_table') mock_sf_executor.drop_table.assert_called_with('test_table')