"""Unit tests for Theatrical flow module.""" from unittest import mock from unittest import TestCase from unittest.mock import Mock from unittest.mock import patch from flows.theatrical.flow import Flow def _schedule_download_to_db_result_mock(task_name, *args, **kwargs): if task_name == 'download_to_db': result = { 'correlation_id': '0123-4567-8910-1112', 'stop': True, 'error_file': 'test_error_file', 'error_details': 'test_error_details'} else: result = {} response = Mock() response.result = result return response class FlowTest(TestCase): """Tests for Theatrical flow class.""" def setUp(self): """Setup environment for tests.""" self.patched_log = patch('flows.theatrical.flow.log') self.log = self.patched_log.start() self.patched_owslogger = patch('flows.theatrical.flow.logger') self.owslogger = self.patched_owslogger.start() self.logger = Mock() self.owslogger.OwsLoggingAdapter.return_value = self.logger self.create = Mock() self.addCleanup(self.patched_log.stop) self.addCleanup(self.patched_owslogger.stop) def test_decider(self): """Test decider method.""" schedule = Mock() schedule.return_value = Mock(result={}) Flow('test', 'test_theatrical', '1.0').decider(schedule) schedule.assert_has_calls([ mock.call('bootstrap', mock.ANY), mock.call('download_to_db', mock.ANY, requires=mock.ANY), mock.call('clean_dynamo_status', mock.ANY, requires=mock.ANY), mock.call( 'create_theatrical_revenue_temp_table', mock.ANY, requires=mock.ANY), mock.call('insert_new_data', mock.ANY, requires=mock.ANY), mock.call( 'move_source_files_to_archive', mock.ANY, requires=mock.ANY), mock.call('send_notification', mock.ANY, requires=mock.ANY), mock.call('set_status', mock.ANY, requires=mock.ANY) ]) def test_decider_bootstrap_failure(self): """Test decider method failure on bootstrap task.""" activity = Mock() activity.result = {'stop': True} schedule = Mock() schedule.return_value = activity Flow('test', 'test_theatrical', '1.0').decider(schedule) # it should only call bootstrap assert schedule.call_count == 1 assert schedule.call_args[0][0] == 'bootstrap' def test_decider_download_to_db_failure(self): """Test decider method failure on download_to_db task.""" schedule = Mock() schedule.side_effect = _schedule_download_to_db_result_mock Flow('test', 'test_theatrical', '1.0').decider(schedule) assert schedule.call_count == 2 schedule.assert_has_calls([ mock.call('bootstrap', mock.ANY), mock.call('download_to_db', mock.ANY, requires=mock.ANY)]) error_log = self.logger.error.call_args[0][0] assert 'test_error_file' in error_log assert 'test_error_details' in error_log assert '0123-4567-8910-1112' == \ self.owslogger.OwsLoggingAdapter.call_args[0][1]['correlation_id']