def test_args_from_args_array_type(self): fake_type = ArrayType(MyBaseType) fd = FunctionDefinition(FunctionDefinition) fd.arguments.append(FunctionArgument('fake-arg', fake_type, True, [])) try: args_from_args(fd, [['invalid-argument']], {}) except InvalidInput as e: assert ArrayType.__name__ in str(e) else: self.fail('Should have thrown an InvalidInput')
def test_args_from_args_usertype(self): class FakeType(UserType): name = "fake-type" basetype = int fake_type = FakeType() fd = FunctionDefinition(FunctionDefinition) fd.arguments.append(FunctionArgument("fake-arg", fake_type, True, 0)) new_args = args_from_args(fd, [1], {}) self.assertEqual([1], new_args[0]) # can't convert str to int try: args_from_args(fd, ["invalid-argument"], {}) except InvalidInput as e: assert fake_type.name in str(e) else: self.fail("Should have thrown an InvalidInput")
def test_args_from_args_usertype(self): class FakeType(UserType): name = 'fake-type' basetype = int fake_type = FakeType() fd = FunctionDefinition(FunctionDefinition) fd.arguments.append(FunctionArgument('fake-arg', fake_type, True, 0)) new_args = args_from_args(fd, [1], {}) self.assertEqual([1], new_args[0]) # can't convert str to int try: args_from_args(fd, ['invalid-argument'], {}) except InvalidInput as e: assert fake_type.name in str(e) else: self.fail('Should have thrown an InvalidInput')
def test_args_from_args_custom_exc(self): class FakeType(UserType): name = 'fake-type' basetype = int def validate(self, value): if value < 10: raise ValueError('should be greater than 10') def frombasetype(self, value): self.validate(value) fake_type = FakeType() fd = FunctionDefinition(FunctionDefinition) fd.arguments.append(FunctionArgument('fake-arg', fake_type, True, 0)) try: args_from_args(fd, [9], {}) except InvalidInput as e: assert fake_type.name in str(e) assert 'Error: should be greater than 10' in str(e) else: self.fail('Should have thrown an InvalidInput')
def callfunction(*args): if with_self: if len(args) == 1: self = args[0] request = self.request elif len(args) == 2: self, request = args else: raise ValueError("Cannot do anything with these arguments") else: request = args[0] request.override_renderer = 'wsme' + get_outputformat(request) try: args, kwargs = combine_args(funcdef, ( args_from_args(funcdef, (), request.matchdict), args_from_params(funcdef, request.params), args_from_body(funcdef, request.body, request.content_type) )) wsme.runtime.check_arguments(funcdef, args, kwargs) if funcdef.pass_request: kwargs[funcdef.pass_request] = request if with_self: args.insert(0, self) result = f(*args, **kwargs) return { 'datatype': funcdef.return_type, 'result': result } except Exception: try: exception_info = sys.exc_info() orig_exception = exception_info[1] orig_code = getattr(orig_exception, 'code', None) data = wsme.api.format_exception(exception_info) if orig_code is not None: data['orig_code'] = orig_code return data finally: del exception_info
def callfunction(*args): if with_self: if len(args) == 1: self = args[0] request = self.request elif len(args) == 2: self, request = args else: raise ValueError("Cannot do anything with these arguments") else: request = args[0] request.override_renderer = 'wsme' + get_outputformat(request) try: args, kwargs = combine_args(funcdef, ( args_from_args(funcdef, (), request.matchdict), args_from_params(funcdef, request.params), args_from_body(funcdef, request.body, request.content_type) )) wsme.runtime.check_arguments(funcdef, args, kwargs) if funcdef.pass_request: kwargs[funcdef.pass_request] = request if with_self: args.insert(0, self) result = f(*args, **kwargs) return { 'datatype': funcdef.return_type, 'result': result } except: try: exception_info = sys.exc_info() orig_exception = exception_info[1] orig_code = getattr(orig_exception, 'code', None) data = wsme.api.format_exception(exception_info) if orig_code is not None: data['orig_code'] = orig_code return data finally: del exception_info