"""Tests for executor.""" import importlib from unittest import mock from unittest.mock import MagicMock import pytest from accounting.bin.build_helper import executor def test_execute_wrong_command(): """Test main entry point for the Build command line helper.""" command = 'some_unexpected_one' with pytest.raises(SystemExit): executor.execute(command) def test_execute_success_command(monkeypatch): """Test main entry point for the Build command line helper.""" mock_exec = MagicMock() monkeypatch.setattr( importlib, 'import_module', mock_exec) executor.execute('reserve_payouts.dynamodb_clean') calls = [mock.call().dynamodb_clean()] mock_exec.assert_has_calls(calls) @mock.patch('accounting.bin.build_helper.executor.raven') @mock.patch('accounting.bin.build_helper.executor.os') def test_execute_command_captures_and_raises(os, raven): """Test _execute_command helper function.""" mock_client = mock.MagicMock() raven.Client.return_value = mock_client mock_command = mock.MagicMock() mock_command.side_effect = Exception with pytest.raises(Exception): executor._execute_command(mock_command) os.environ.get.assert_called_once_with('SENTRY_DSN') raven.Client.assert_has_calls([mock.call()]) mock_client.captureException.assert_has_calls([mock.call()]) mock_command.assert_has_calls([mock.call()]) @mock.patch('accounting.bin.build_helper.executor.raven') @mock.patch('accounting.bin.build_helper.executor.os') def test_execute_command_no_sentry_and_raises(os, raven): """Test _execute_command helper function.""" os.environ.get.return_value = None mock_command = mock.MagicMock() mock_command.side_effect = Exception with pytest.raises(Exception): executor._execute_command(mock_command) os.environ.get.assert_called_once_with('SENTRY_DSN') raven.Client.assert_not_called() mock_command.assert_has_calls([mock.call()]) @mock.patch('accounting.bin.build_helper.executor.raven') @mock.patch('accounting.bin.build_helper.executor.os') def test_execute_command_passes_args(os, raven): """Test _execute_command helper function.""" mock_client = mock.MagicMock() raven.Client.return_value = mock_client mock_command = mock.MagicMock() executor._execute_command(mock_command) executor._execute_command(mock_command, 1) executor._execute_command(mock_command, 1, 2, 3, 4) expected_calls = [ mock.call(), mock.call(1), mock.call(1, 2, 3, 4), ] mock_command.assert_has_calls(expected_calls)