"""Tests for cli module.""" from argparse import ArgumentParser from copy import copy from unittest.mock import Mock from unittest.mock import patch from pytest import raises from flows.distribution_fee import cli from flows.flow import DatabaseParam 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.distribution_fee.cli.log') @patch('flows.distribution_fee.cli.util') def test_run_execute_explicit(mock_util, mock_log): """Test run_execute function with explicit UPCs.""" correlation_id = 'explicit_test_cid' run_id = 'run_id' explicit_upcs = ('foo', 'bar') upc_param = DatabaseParam('upcs', data=explicit_upcs) mock_util.start_swf_execution.return_value = {'runId': run_id} expected_log_params = { 'correlation_id': correlation_id, 'upcs': upc_param} expected_swf_params = copy(expected_log_params) expected_swf_params['correlation_id'] += '.1' run_response = cli.run_execute(correlation_id, explicit_upcs) 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) assert not mock_util.get_serviced_upcs.called @patch('flows.distribution_fee.cli.log') @patch('flows.distribution_fee.cli.util') def test_run_execute_default(mock_util, mock_log): """Test run_execute function with default UPCs.""" correlation_id = 'default_test_cid' run_id = 'run_id' default_upcs = ('foo', 'bar', 'baz') upc_param = DatabaseParam('upcs', data=default_upcs) mock_util.get_serviced_upcs.return_value = default_upcs mock_util.start_swf_execution.return_value = {'runId': run_id} expected_log_params = { 'correlation_id': correlation_id, 'upcs': upc_param} expected_swf_params = copy(expected_log_params) expected_swf_params['correlation_id'] += '.1' run_response = cli.run_execute(correlation_id, 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( expected_log_params['correlation_id'], run_id) 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.distribution_fee.cli.ActivityWorker') @patch('flows.distribution_fee.cli.Flow') def test_run_worker(flow, worker): """Test run_worker 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 assert worker.called worker.assert_called_with(mock_flow) assert mock_worker.run.called 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.distribution_fee.cli.DeciderWorker') @patch('flows.distribution_fee.cli.Flow') @patch('flows.distribution_fee.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