"""Test cache manager worker.""" import signal from unittest.mock import call from unittest.mock import MagicMock from cache_manager import worker def test_init(monkeypatch): """Test worker init.""" # Set up mocks # For signal mock = MagicMock() monkeypatch.setattr(worker.signal, 'signal', mock) # Set expectations expected_calls = [ call(signal.SIGINT, worker.end), call(signal.SIGTERM, worker.end)] worker.init() # Asserts assert mock.mock_calls == expected_calls def test_run_with_exception(monkeypatch): """Test exception while worker is running.""" error = Exception('uh oh') sqs_poll_mock = MagicMock(side_effect=error) monkeypatch.setattr(worker.sqs_queue, 'poll', sqs_poll_mock) end_with_error_mock = MagicMock() monkeypatch.setattr(worker, 'end_with_error', end_with_error_mock) worker.run() # Asserts assert sqs_poll_mock.called end_with_error_mock.assert_called_once_with(error) def test_end(monkeypatch): """Test end worker.""" mock = MagicMock() monkeypatch.setattr(worker.sys, 'exit', mock) worker.end() # Asserts mock.assert_called_once_with(0) def test_end_with_error(monkeypatch): """Test worker ending with error.""" # Mocks # for sys.exit function exit_mock = MagicMock() monkeypatch.setattr(worker.sys, 'exit', exit_mock) # for traceback.format_exc function format_exc_mock = MagicMock(return_value='Some traceback...') monkeypatch.setattr(worker.traceback, 'format_exc', format_exc_mock) worker.end_with_error(Exception('Uh oh')) # Asserts exit_mock.assert_called_once_with(1) assert format_exc_mock.called