"""Test 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.cable_ingestion 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_ingestion.cli.log') @patch('flows.cable_ingestion.cli.date') @patch('flows.cable_ingestion.cli.util') def test_run_execute_default(mock_util, mock_date, mock_log): """Test executing the workflow with all default params.""" correlation_id = 'test_cid' run_id = 'run_id' date_today = date(2000, 1, 10) date_end = date(2000, 1, 11) date_start = date(2000, 1, 2) mock_util.start_swf_execution.return_value = {'runId': run_id} mock_date.today.return_value = date_today expected_log_params = { 'correlation_id': correlation_id, 'date_end': date_end.strftime('%Y-%m-%d'), 'date_start': date_start.strftime('%Y-%m-%d')} expected_swf_params = copy(expected_log_params) expected_swf_params['correlation_id'] += '.1' run_response = cli.run_execute(correlation_id, 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_ingestion.cli.log') @patch('flows.cable_ingestion.cli.date') @patch('flows.cable_ingestion.cli.util') def test_run_execute_explicit(mock_util, mock_date, 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'} expected_log_params = copy(cli_params) 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) 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_ingestion.cli.ActivityWorker') @patch('flows.cable_ingestion.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.cable_ingestion.cli.DeciderWorker') @patch('flows.cable_ingestion.cli.Flow') @patch('flows.cable_ingestion.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