"""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.flow import DatabaseParam from flows.sales_data 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.sales_data.cli.log') @patch('flows.sales_data.cli.util') def test_run_execute_accounting_period_id(mock_util, mock_log): """Test run_execute function with accounting_period_id.""" correlation_id = 'test_cid' run_id = 'run_id' period_id = 123 mock_util.start_swf_execution.return_value = {'runId': run_id} upcs = DatabaseParam('upcs', data=('12', '34')) mock_util.get_serviced_upcs.return_value = upcs.data expected_log_params = { 'correlation_id': correlation_id, 'upcs': upcs, 'accounting_period_id': period_id} run_response = cli.run_execute(correlation_id, None, period_id) assert run_response == run_id mock_log.create.assert_called_once_with(**expected_log_params) mock_log.add_run_id.assert_called_once_with(correlation_id, run_id) mock_util.get_serviced_upcs.assert_called_once() expected_swf_params = copy(expected_log_params) expected_swf_params['correlation_id'] += '.1' execution_call = mock_util.start_swf_execution.call_args_list[0] assert execution_call[0][0] == expected_swf_params @patch('flows.sales_data.cli.log') @patch('flows.sales_data.cli.util') def test_run_execute_accounting_no_period_id(mock_util, mock_log): """Test run_execute function without accounting_period_id.""" correlation_id = 'test_cid' run_id = 'run_id' mock_util.start_swf_execution.return_value = {'runId': run_id} upcs = DatabaseParam('upcs', data=('12', '34')) mock_util.get_serviced_upcs.return_value = upcs.data expected_log_params = { 'correlation_id': correlation_id, 'upcs': upcs, 'accounting_period_id': None} run_response = cli.run_execute(correlation_id, None, None) assert run_response == run_id mock_log.create.assert_called_once_with(**expected_log_params) mock_log.add_run_id.assert_called_once_with(correlation_id, run_id) mock_util.get_serviced_upcs.assert_called_once() expected_swf_params = copy(expected_log_params) expected_swf_params['correlation_id'] += '.1' execution_call = mock_util.start_swf_execution.call_args_list[0] assert execution_call[0][0] == expected_swf_params 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.sales_data.cli.ActivityWorker') @patch('flows.sales_data.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.sales_data.cli.DeciderWorker') @patch('flows.sales_data.cli.Flow') def test_run_decider(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