"""Unit tests for CLI functions to run decider, worker and exec processes.""" import json import os.path from unittest.mock import MagicMock from unittest.mock import patch from analytics_aggregation import cli from analytics_aggregation.flows import base def test_run_activity_worker(mocker): """Test run activity worker.""" worker = mocker.patch('garcon.activity.ActivityWorker') cli.garcon('worker', 'spotify_sos') assert worker.called @patch('boto.swf.layer1.Layer1') @patch('analytics_aggregation.cli.swf.WorkflowType') def test_exec_source_of_stream(mock_wf_type, mock_layer1, monkeypatch): """Test exec.""" monkeypatch.setattr(base, 'get_domain', MagicMock(return_value='domain')) # test spotify_sos exec cli.garcon('exec', 'spotify_sos') mock_wf_type.assert_called_with( task_list='spotify_sos_analytics_aggregation', name='spotify_sos_analytics_aggregation', version='2.0', domain='domain') def _l_o_w_executions_se(*args, **kwargs): return {'executionInfos': ['someExecution']} @patch('boto.swf.layer1.Layer1.list_open_workflow_executions') @patch('analytics_aggregation.cli.swf.WorkflowType') def test_exec_source_of_stream_already_started( mock_wf_type, list_open_workflow_executions_mock, monkeypatch): """Test exec.""" monkeypatch.setattr(base, 'get_domain', MagicMock(return_value='domain')) list_open_workflow_executions_mock.side_effect = _l_o_w_executions_se # test spotify_sos exec cli.garcon('exec', 'spotify_sos') assert mock_wf_type.call_count == 0 @patch('boto.swf.layer1.Layer1.list_open_workflow_executions') @patch('analytics_aggregation.cli.swf.WorkflowType') def test_exec_apple_music_sos_already_started( mock_wf_type, list_open_workflow_executions_mock, monkeypatch): """Test exec.""" monkeypatch.setattr(base, 'get_domain', MagicMock(return_value='domain')) list_open_workflow_executions_mock.side_effect = _l_o_w_executions_se # test apple_music_sos exec cli.garcon('exec', 'apple_music_sos') assert mock_wf_type.call_count == 0 @patch('boto.swf.layer1.Layer1') @patch('analytics_aggregation.cli.swf.WorkflowExecution') @patch('analytics_aggregation.cli.swf.WorkflowType') def test_exec_source_of_stream_with_output( mock_wf_type, mock_wf_exec, mock_layer1, monkeypatch): """Test exec with output to file.""" monkeypatch.setattr(base, 'get_domain', MagicMock(return_value='domain')) mock_wf_exec.runId = '123456789' mock_wf_type.return_value.start.return_value = mock_wf_exec # test spotify_sos exec cli.garcon('exec', 'spotify_sos', '-o', 'out.file') mock_wf_type.assert_called_with( task_list='spotify_sos_analytics_aggregation', name='spotify_sos_analytics_aggregation', version='2.0', domain='domain') # test output file contents assert os.path.isfile('out.file') with open('out.file', 'r') as f: h_args = json.load(f) assert h_args.get( 'workflow_id') == 'spotify_sos_analytics_aggregation'