def test_specified_failure(self): """ Tests that you can select a specific error from the schema and fail on it. :return nothing """ mapping = {KeyError: 404, BlahError: 400} @fails_with(select_dict([BlahError], mapping)) @succeeds_with(204) def doWork(request, log): return defer.fail(BlahError('fail')) d = doWork(self.mockRequest, self.mockLog) r = self.successResultOf(d) self.mockRequest.setResponseCode.assert_called_once_with(400) self.mockLog.bind.assert_called_once_with(code=400, uri='/', details='', message='fail', type='BlahError') self.mockLog.bind().msg.assert_called_once_with('fail') faultDoc = json.loads(r) self.assertEqual(faultDoc, { "message": "fail", "code": 400, "type": "BlahError", "details": "" }) self.flushLoggedErrors(BlahError)
class FakeApp(object): log = self.mockLog @fails_with(select_dict([KeyError], mapping)) @succeeds_with(204) def doWork(self, request): return defer.fail(blah)
def test_select_dict(self): """ Tests that the select_dict function works :return nothing """ mapping = {KeyError: 404, BlahError: 400} res = select_dict([KeyError], mapping) self.assertEqual({KeyError: 404}, res)
def test_unspecified_failure(self): """ Tests that errors that were not expected will 500 :return nothing """ mapping = {KeyError: 404, BlahError: 400} blah = BlahError('fail') @fails_with(select_dict([KeyError], mapping)) @succeeds_with(204) def doWork(request, log): return defer.fail(blah) d = doWork(self.mockRequest, self.mockLog) r = self.successResultOf(d) self.mockRequest.setResponseCode.assert_called_once_with(500) class _CmpFailure(object): def __init__(self, exception): self._exception = exception def __eq__(self, other): return isinstance(other, Failure) and other.value == self._exception self.mockLog.bind.assert_called_once_with(code=500, uri='/') self.mockLog.bind().err.assert_called_once_with( _CmpFailure(blah), 'Unhandled Error handling request') faultDoc = json.loads(r) self.assertEqual( faultDoc, { "message": "An Internal Error was encountered", "code": 500, "type": "InternalError", "details": "" }) self.flushLoggedErrors(BlahError)
def test_unspecified_failure(self): """ Tests that errors that were not expected will 500 :return nothing """ mapping = {KeyError: 404, BlahError: 400} blah = BlahError('fail') @fails_with(select_dict([KeyError], mapping)) @succeeds_with(204) def doWork(request, log): return defer.fail(blah) d = doWork(self.mockRequest, self.mockLog) r = self.successResultOf(d) self.mockRequest.setResponseCode.assert_called_once_with(500) class _CmpFailure(object): def __init__(self, exception): self._exception = exception def __eq__(self, other): return isinstance(other, Failure) and other.value == self._exception self.mockLog.bind.assert_called_once_with(code=500, uri='/') self.mockLog.bind().err.assert_called_once_with( _CmpFailure(blah), 'Unhandled Error handling request' ) faultDoc = json.loads(r) self.assertEqual(faultDoc, { "message": "An Internal Error was encountered", "code": 500, "type": "InternalError", "details": "" }) self.flushLoggedErrors(BlahError)