def test_success(self, mock_load_and_validate_message, mock_call_task, message_data, message): mock_load_and_validate_message.return_value = message receipt = str(uuid.uuid4()) message_handler(json.dumps(message_data), receipt) mock_load_and_validate_message.assert_called_once_with(message_data) mock_call_task.assert_called_once_with(message)
def test_fails_on_validation_error(self, mock_load_and_validate_message, mock_call_task, message_data): error_message = 'Invalid message body' mock_load_and_validate_message.side_effect = ValidationError( error_message) with pytest.raises(ValidationError): message_handler(json.dumps(message_data), None) mock_call_task.assert_not_called()
def test_special_handling_retry_error(self, mock_load_and_validate_message, mock_call_task, message_data, message): mock_load_and_validate_message.return_value = message mock_call_task.side_effect = RetryException with pytest.raises(mock_call_task.side_effect), mock.patch.object( consumer.logger, 'info') as logging_mock: message_handler(json.dumps(message_data), None) logging_mock.assert_called_once()
def test_special_handling_ignore_exception(self, mock_load_and_validate_message, mock_call_task, message_data, message): mock_load_and_validate_message.return_value = message mock_call_task.side_effect = IgnoreException # no exception raised with mock.patch.object(consumer.logger, 'info') as logging_mock: message_handler(json.dumps(message_data), None) logging_mock.assert_called_once()
def test_post_deserialize_hook(self, mock_load_and_validate_message, mock_call_task, message_data, message, settings): settings.HEDWIG_POST_DESERIALIZE_HOOK = 'tests.test_consumer.post_deserialize_hook' mock_load_and_validate_message.return_value = message receipt = str(uuid.uuid4()) message_handler(json.dumps(message_data), receipt) mock_load_and_validate_message.assert_called_once_with(message_data) mock_call_task.assert_called_once_with(message) post_deserialize_hook.assert_called_once_with( message_data=message_data)
def test_special_handling_logging_error(self, mock_load_and_validate_message, mock_call_task, message_data, message): mock_load_and_validate_message.return_value = message mock_call_task.side_effect = LoggingException( 'foo', extra={'mickey': 'mouse'}) with pytest.raises(LoggingException), mock.patch.object( consumer.logger, 'exception') as logging_mock: message_handler(json.dumps(message_data), None) logging_mock.assert_called_once_with('foo', extra={'mickey': 'mouse'})
def test_fails_on_task_failure(self, mock_load_and_validate_message, mock_call_task, message_data, message): mock_load_and_validate_message.return_value = message mock_call_task.side_effect = Exception with pytest.raises(mock_call_task.side_effect): message_handler(json.dumps(message_data), None)
def test_fails_on_invalid_json(self, *mocks): with pytest.raises(ValueError): message_handler("bad json", None)