"""Test cli module.""" from argparse import ArgumentParser from copy import copy from datetime import date from datetime import timedelta from unittest.mock import Mock from unittest.mock import patch from pytest import raises from flows.cable_calculation import cli def test_init_execute_parser(): """Test init_execute_parser function.""" parser = Mock(spec=ArgumentParser()) cli.init_execute_parser(parser) assert parser.add_argument.called @patch('flows.cable_calculation.cli.log') @patch('flows.cable_calculation.cli.DatabaseParam') @patch('flows.cable_calculation.cli.date') @patch('flows.cable_calculation.cli.util') def test_run_execute_default( mock_util, mock_date, mock_database_param, mock_log): """Test executing the workflow with all default params.""" correlation_id = 'test_cid' run_id = 'run_id' date_today = date(2000, 1, 1) date_end = date_today + timedelta(days=1) date_start = date_today - timedelta(weeks=10) upc_param = Mock() mock_util.start_swf_execution.return_value = {'runId': run_id} mock_date.today.return_value = date_today mock_database_param.return_value = upc_param expected_log_params = { 'correlation_id': correlation_id, 'date_end': date_end.strftime('%Y-%m-%d'), 'date_start': date_start.strftime('%Y-%m-%d'), 'upcs': upc_param} expected_swf_params = copy(expected_log_params) expected_swf_params['correlation_id'] += '.1' mock_util.get_serviced_upcs.return_value = expected_swf_params['upcs'] run_response = cli.run_execute(correlation_id, None, None, None) assert run_response == run_id execution_call = mock_util.start_swf_execution.call_args_list[0] assert execution_call[0][0] == expected_swf_params mock_log.create.assert_called_once_with(**expected_log_params) mock_log.add_run_id.assert_called_once_with(correlation_id, run_id) @patch('flows.cable_calculation.cli.log') @patch('flows.cable_calculation.cli.DatabaseParam') @patch('flows.cable_calculation.cli.date') @patch('flows.cable_calculation.cli.util') def test_run_execute_explicit( mock_util, mock_date, mock_database_param, mock_log): """Test executing the workflow with explicit params.""" run_id = 'run_id' cli_params = { 'correlation_id': 'another_test_cid', 'date_end': '1999-12-31', 'date_start': '1900-01-01', 'upcs': ('foo', 'bar', 'baz')} upc_param = Mock() upc_param.data_json = 'upcs in json form' mock_database_param.return_value = upc_param expected_log_params = copy(cli_params) expected_log_params['upcs'] = upc_param expected_swf_params = copy(expected_log_params) expected_swf_params['correlation_id'] += '.1' mock_util.start_swf_execution.return_value = {'runId': run_id} run_response = cli.run_execute(**cli_params) assert run_response == run_id execution_call = mock_util.start_swf_execution.call_args_list[0] assert execution_call[0][0] == expected_swf_params mock_log.create.assert_called_once_with(**expected_log_params) mock_log.add_run_id.assert_called_once_with( cli_params['correlation_id'], run_id) mock_database_param.assert_called_once_with( 'upcs', data=cli_params['upcs']) def test_init_decider_parser(): """Test init_decider_parser function.""" parser = Mock() cli.init_decider_parser(parser) assert not parser.called # no changes to cli arguments @patch('flows.cable_calculation.cli.DeciderWorker') @patch('flows.cable_calculation.cli.Flow') @patch('flows.cable_calculation.cli.sleep') def test_run_decider(sleep, flow, worker): """Test run_decider function.""" mock_worker = Mock() # the decider runs in an infinite loop, this is a way to break out of it mock_worker.run.side_effect = [None, EnvironmentError] worker.return_value = mock_worker mock_flow = Mock() flow.return_value = mock_flow with raises(EnvironmentError): cli.run_decider() assert flow.called worker.assert_called_with(mock_flow) assert mock_worker.run.called def test_init_worker_parser(): """Test init_worker_parser function.""" parser = Mock() cli.init_worker_parser(parser) assert not parser.called # no changes to cli arguments @patch('flows.cable_calculation.cli.ActivityWorker') @patch('flows.cable_calculation.cli.Flow') def test_run_worker(flow, worker): """Test run function.""" mock_worker = Mock() worker.return_value = mock_worker mock_flow = Mock() flow.return_value = mock_flow cli.run_worker() assert flow.called assert worker.called worker.assert_called_with(mock_flow) assert mock_worker.run.called