"""Unit tests for YouTube Bulk Reports Workflow.""" from unittest import mock from unittest.mock import MagicMock from feed_ingestion.flows.youtube_bulk_reports.flow import Flow from feed_ingestion.flows.youtube_bulk_reports.flow import GroupFlow def test_decider_theorchard(): """Test normal decider execution for theorchard licensor.""" schedule = MagicMock() schedule_result_object = MagicMock() schedule_result_object.result = {'bootstrap.licensor': 'theorchard'} schedule.return_value = schedule_result_object flow = Flow() flow.decider(schedule) schedule.assert_has_calls([ mock.call('bootstrap', mock.ANY), mock.call('grab_reports_files', mock.ANY, requires=mock.ANY), mock.call('load_staging_raw_table', mock.ANY, requires=mock.ANY), mock.call('set_status_to_ingested', mock.ANY, requires=mock.ANY) ]) def test_decider_sme(): """Test normal decider execution for sme licensor.""" schedule = MagicMock() schedule_result_object = MagicMock() schedule_result_object.result = {'bootstrap.licensor': 'sme'} schedule.return_value = schedule_result_object flow = Flow() flow.decider(schedule) schedule.assert_has_calls([ mock.call('bootstrap', mock.ANY), mock.call('sme_copy_from_athena_to_s3', mock.ANY, requires=mock.ANY), mock.call('load_staging_raw_table', mock.ANY, requires=mock.ANY), mock.call('set_status_to_ingested', mock.ANY, requires=mock.ANY) ]) def test_decider_only_download_theorchard(): """Test normal decider execution for theorchard licensor.""" schedule = MagicMock() schedule_result_object = MagicMock() schedule_result_object.result = { 'bootstrap.licensor': 'theorchard', 'bootstrap.only_download': 'True', } schedule.return_value = schedule_result_object flow = Flow() flow.decider(schedule) schedule.assert_has_calls([ mock.call('bootstrap', mock.ANY), mock.call('grab_reports_files', mock.ANY, requires=mock.ANY), ]) def test_decider_only_download_sme(): """Test normal decider execution for sme licensor.""" schedule = MagicMock() schedule_result_object = MagicMock() schedule_result_object.result = { 'bootstrap.licensor': 'sme', 'bootstrap.only_download': 'True', } schedule.return_value = schedule_result_object flow = Flow() flow.decider(schedule) schedule.assert_has_calls([ mock.call('bootstrap', mock.ANY), mock.call('sme_copy_from_athena_to_s3', mock.ANY, requires=mock.ANY), ]) def test_decider_should_stop_when_bootstrap_return_stop(): """Decider quits if data is already ingested.""" bootstrap = MagicMock() bootstrap.result = {'bootstrap.stop': True} schedule = MagicMock(return_value=bootstrap) flow = Flow() result = flow.decider(schedule) assert result is None schedule.assert_has_calls([mock.call('bootstrap', mock.ANY)]) def test_group_decider_sme(): """Test normal group decider execution.""" class TestGroupFlow(GroupFlow): """Test GroupFlow class.""" @property def bootstrap(self): return {} schedule = MagicMock() schedule_result_object = MagicMock() schedule_result_object.result = {'bootstrap.licensor': 'sme'} schedule.return_value = schedule_result_object flow = TestGroupFlow() flow.decider(schedule) schedule.assert_has_calls([ mock.call('bootstrap', mock.ANY), mock.call('sme_copy_from_athena_to_s3', mock.ANY, requires=mock.ANY), mock.call( 'load_staging_raw_table', mock.ANY, requires=mock.ANY), mock.call('set_status_to_ingested', mock.ANY, requires=mock.ANY) ]) def test_group_decider_theorchard(): """Test normal group decider execution.""" class TestGroupFlow(GroupFlow): """Test GroupFlow class.""" @property def bootstrap(self): return {} schedule = MagicMock() schedule_result_object = MagicMock() schedule_result_object.result = {'bootstrap.licensor': 'theorchard'} schedule.return_value = schedule_result_object flow = TestGroupFlow() flow.decider(schedule) schedule.assert_has_calls([ mock.call('bootstrap', mock.ANY), mock.call('grab_reports_files', mock.ANY, requires=mock.ANY), mock.call( 'load_staging_raw_table', mock.ANY, requires=mock.ANY), mock.call('set_status_to_ingested', mock.ANY, requires=mock.ANY) ]) def test_group_decider_should_stop_when_data_already_ingested(): """Group Decider quits if data is already ingested.""" class TestGroupFlow(GroupFlow): """Test GroupFlow class.""" @property def bootstrap(self): return {} bootstrap = MagicMock() bootstrap.result = {'bootstrap.stop': True} schedule = MagicMock(return_value=bootstrap) flow = TestGroupFlow() result = flow.decider(schedule) assert result is None schedule.assert_has_calls([mock.call('bootstrap', mock.ANY)])