"""Tests for cli module.""" from argparse import ArgumentParser from copy import copy from datetime import date from unittest.mock import Mock from unittest.mock import patch from pytest import raises from flows.digital 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.digital.cli.log') @patch('flows.digital.cli.date') @patch('flows.digital.cli.util') def test_run_execute_default(mock_util, mock_date, mock_log): """Test run_execute function with default UPCs.""" correlation_id = 'test_cid' run_id = 'run_id' mock_util.start_swf_execution.return_value = {'runId': run_id} mock_date.today.return_value = date(2000, 1, 20) upcs = DatabaseParam('upcs', data=('foo', 'bar')) expected_log_params = { 'correlation_id': correlation_id, 'date_end': '2000-01-21', 'date_start': '2000-01-06', 'upcs': upcs} expected_swf_params = copy(expected_log_params) expected_swf_params['correlation_id'] += '.1' mock_util.get_serviced_upcs.return_value = upcs.data 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) mock_util.get_serviced_upcs.assert_called_once() @patch('flows.digital.cli.log') @patch('flows.digital.cli.date') @patch('flows.digital.cli.util') def test_run_execute_explicit(mock_util, mock_date, mock_log): """Test run_execute function with explicit UPCs.""" run_id = 'run_id' mock_util.start_swf_execution.return_value = {'runId': run_id} mock_date.today.return_value = date(2000, 1, 20) upcs = DatabaseParam('upcs', data=('foo', 'bar', 'baz')) cli_params = { 'correlation_id': 'another_test_cid', 'date_end': '1999-12-31', 'date_start': '1900-01-01', 'upcs': upcs.data} expected_log_params = copy(cli_params) expected_log_params['upcs'] = upcs expected_swf_params = copy(expected_log_params) expected_swf_params['correlation_id'] += '.1' 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_util.get_serviced_upcs.assert_not_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.digital.cli.ActivityWorker') @patch('flows.digital.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.digital.cli.DeciderWorker') @patch('flows.digital.cli.Flow') @patch('flows.digital.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