"""Unit tests for CLI functions to run decider, worker and exec processes.""" from unittest import mock from unittest.mock import MagicMock from unittest.mock import patch from feed_ingestion import cli def test_run_activity_worker(mocker): """Test run activity worker.""" worker = mocker.patch('garcon.activity.ActivityWorker') cli.garcon('worker', 'deezer') assert worker.called def test_exec_garcon_exec(monkeypatch): """Test exec.""" commands = { 'exec': MagicMock(), } monkeypatch.setattr(cli, '_COMMANDS', commands) context_value = '{"licensor": "theorchard", "context_date": "2020-01-31"}' cli.garcon('exec', 'deezer', '-c', context_value) assert commands['exec'].call_args_list == [ mock.call(flow=mock.ANY, context=context_value, output_file=None) ] def test_exec_garcon_exec_save_output(monkeypatch): """Test exec with output to file.""" commands = { 'exec': MagicMock(), } monkeypatch.setattr(cli, '_COMMANDS', commands) context_value = '{"licensor": "theorchard", "context_date": "2020-01-31"}' cli.garcon('exec', 'deezer', '-c', context_value, '-o', 'out.file') assert commands['exec'].call_args_list == [ mock.call(flow=mock.ANY, context=context_value, output_file='out.file') ] @patch('feed_ingestion.cli.boto3') @patch('feed_ingestion.cli.swf_util') @patch('feed_ingestion.cli.generate_execution_console_url') def test_execute_flow( mock_generate_execution_console_url, mock_swf_util, mock_boto3, monkeypatch): """Test execute_flow_has_nonconcurrent_workflows_running.""" mock_flow = MagicMock() mock_flow.timeout = 1200 mock_flow.task_timeout = 3600 mock_swf = mock_boto3.client.return_value mock_swf.start_workflow_execution.return_value = {'runId': 123} mock_swf_util.has_nonconcurrent_workflows_running.return_value = False cli.execute_flow( mock_flow, '{"context_date":"2016-03-05", "reload": "True"}') assert mock_generate_execution_console_url.call_args_list == [ mock.call( swf_domain=mock_flow.domain, workflow_id=mock_flow.workflow_id.return_value, run_id=123, ) ] assert mock_swf.start_workflow_execution.call_args == mock.call( domain=mock_flow.domain, workflowType={ 'name': mock_flow.name, 'version': mock_flow.version, }, taskList={ 'name': mock_flow.name, }, workflowId=mock_flow.workflow_id.return_value, input='{"context_date":"2016-03-05", "reload": "True"}', executionStartToCloseTimeout='1200', taskStartToCloseTimeout='3600', childPolicy='TERMINATE', ) @patch('feed_ingestion.cli.boto3') @patch('feed_ingestion.cli.swf_util') @patch('feed_ingestion.cli.generate_execution_console_url') def test_execute_flow_has_nonconcurrent_workflows_running( mock_generate_execution_console_url, mock_swf_util, mock_boto3, monkeypatch): """Test execute_flow_has_nonconcurrent_workflows_running.""" mock_flow = MagicMock() mock_swf_util.has_nonconcurrent_workflows_running.return_value = True mock_swf = mock_boto3.client.return_value cli.execute_flow( mock_flow, '{"context_date":"2016-03-05", "reload": "True"}') assert mock_generate_execution_console_url.call_args_list == [] assert mock_swf.start_workflow_execution.call_args_list == []