diff options
author | Lucas Alvares Gomes <lucasagomes@gmail.com> | 2014-01-02 19:25:45 +0000 |
---|---|---|
committer | Lucas Alvares Gomes <lucasagomes@gmail.com> | 2014-01-03 12:35:57 +0000 |
commit | 228bfbd084ff2c4653740696d26ed10748aaa33c (patch) | |
tree | 5022188e0762ff796af39b429e292bbc0e8b6988 | |
parent | e26d1b608cc5a05940c0b6b7fc176a0d587ba611 (diff) | |
download | wsme-228bfbd084ff2c4653740696d26ed10748aaa33c.tar.gz |
args_from_args() to work with an instance of UserType
When the args_from_args() function fails to convert a value to a certain
type it tries to get the name of that type by using the __name__ attribute
of that type, UserType object instances doesn't have the __name__
attribute so the function fails when it tries to access it, this patch
makes WSME to check if it's an UserType instance before getting it's name.
Change-Id: If78ce0e642997421c97559cc28604421f5c03922
Closes-Bug: #1265590
-rw-r--r-- | wsme/rest/args.py | 6 | ||||
-rw-r--r-- | wsme/tests/test_protocols_commons.py | 25 |
2 files changed, 29 insertions, 2 deletions
diff --git a/wsme/rest/args.py b/wsme/rest/args.py index 1492222..4574cdd 100644 --- a/wsme/rest/args.py +++ b/wsme/rest/args.py @@ -171,10 +171,14 @@ def args_from_args(funcdef, args, kwargs): try: newargs.append(from_param(argdef.datatype, arg)) except Exception: + if isinstance(argdef.datatype, UserType): + datatype_name = argdef.datatype.name + else: + datatype_name = argdef.datatype.__name__ raise InvalidInput( argdef.name, arg, - "unable to convert to %s" % argdef.datatype.__name__) + "unable to convert to %s" % datatype_name) newkwargs = {} for argname, value in kwargs.items(): newkwargs[argname] = from_param( diff --git a/wsme/tests/test_protocols_commons.py b/wsme/tests/test_protocols_commons.py index b496de6..19908b2 100644 --- a/wsme/tests/test_protocols_commons.py +++ b/wsme/tests/test_protocols_commons.py @@ -3,7 +3,9 @@ import datetime import unittest -from wsme.rest.args import from_param, from_params +from wsme.api import FunctionArgument, FunctionDefinition +from wsme.rest.args import from_param, from_params, args_from_args +from wsme.exc import InvalidInput from wsme.types import UserType, Unset, ArrayType, DictType @@ -52,3 +54,24 @@ class TestProtocolsCommons(unittest.TestCase): def test_from_params_dict_unset(self): assert from_params(DictType(int, str), {}, 'a', set()) is Unset + + 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') |