def test_response_builder_from_invalid_request(): invalid_req = Request() invalid_req.add_error( ErrorMessage(type_='name', message='exceed max chars')) invalid_req.add_error(ErrorMessage(type_='age', message='is not integer')) response = ResponseFailureBuilder.build_from_invalid_request(invalid_req) assert bool(response) is False assert response.status == resp_status.PARAMETERS_ERROR assert len(response.errors) == 2 assert response.errors[0].type == 'name' assert response.errors[0].message == 'exceed max chars' assert response.errors[1].type == 'age' assert response.errors[1].message == 'is not integer'
def test_definition_encoder(): raw = {'type': 'SYSTEM_ERROR', 'message': 'some error'} res = """{"type": "SYSTEM_ERROR", "message": "some error"}""" definition = ErrorMessage.from_dict(raw) data = json.dumps(definition, cls=ErrorMessageEncoder) assert data == res
def test_abs_use_case_invalid_request(): invalid_request = Request() invalid_request.add_error(ErrorMessage('someParam', 'some message')) use_case = UseCaseAbsImpl() res = use_case.execute(invalid_request) assert bool(res) is False
def test_request_multiple_errors(): req = Request() req.add_errors([ErrorMessage('field.name', 'mandatory field')]) assert bool(req) is False assert len(req.errors) == 1 assert req.errors[0].type == 'field.name' assert req.errors[0].message == 'mandatory field'
def test_response_can_add_multiples_errors(): res_type = 'Context Error' res_message = 'This is an error' response = Response() response.add_errors([ErrorMessage(res_type, res_message)]) assert bool(response) is False assert len(response.errors) == 1 assert response.errors[0].type == res_type assert response.errors[0].message == res_message
def from_dict(cls, params: Dict) -> 'ListRequest' or Request: ft = params.pop('ft', 'all') groups = grouping(params) req = cls.SCHEMA_VERIFIER_CLS(cls.SCHEMA) if req.is_valid(groups): groups['ft'] = ft try: return cls.factory(params=groups) except FilterException as e: return cls.create_error_response( [ErrorMessage(type_='FilterError', message=str(e))]) return cls.create_error_response(req.errors)
def test_response_failure_has_errors(): res_type = 'Context Error' res_message = 'This is an error' res_error_type = 'Error Type' response = Response(status=res_error_type) response.add_error(ErrorMessage(res_type, res_message)) assert bool(response) is False assert len(response.errors) == 1 assert response.status == res_error_type assert response.errors[0].type == res_type assert response.errors[0].message == res_message
def test_log_error_in_invalid_request(): logger = mock.Mock() class FooCase(UseCaseAbs): def process_request(self, req): return True def logger(self): return logger invalid_request = Request() invalid_request.add_error(ErrorMessage('someParam', 'some message')) use_case = FooCase() res = use_case.execute(invalid_request) assert bool(res) is False assert logger.debug.call_count == 1
def validate(self, definition: Dict): for error in self.validator.iter_errors(definition): path = ".".join(str(e) for e in error.path) or '.' self.errors.append(ErrorMessage(type_=path, message=error.message))
def test_request_is_not_valid(): req = Request() req.add_error(ErrorMessage('field.name', 'mandatory field')) assert bool(req) is False
def test_error_message(): err = ErrorMessage(type_='NOT FOUND', message='resource not found') assert err.type == 'NOT FOUND' assert err.message == 'resource not found' assert str(err) == 'NOT FOUND: resource not found'
def test_error_message_eq(): err1 = ErrorMessage(type_='NOT FOUND', message='resource not found') err2 = ErrorMessage(type_='NOT FOUND', message='resource not found') assert err1 == err2
def build_parameters_error(cls, type_, msg): res = Response(status=resp_status.PARAMETERS_ERROR) res.add_error(ErrorMessage(type_, msg)) return res
def build_system_error(cls, message: str) -> Response: res = Response(status=resp_status.SYSTEM_ERROR) res.add_error(ErrorMessage(resp_status.SYSTEM_ERROR, message)) return res
def build_resource_error(cls, message: str) -> Response: res = Response(status=resp_status.RESOURCE_ERROR) res.add_error(ErrorMessage(resp_status.RESOURCE_ERROR, message)) return res