Ensure UserType objects are converted to basetype

Add some tests to verify that UserType objects are correctly converted
to the specified basetype. Bug #1228040 was based on a wrong usage of
the UserType base class, so these also stand as a further example of
correct usage.

Related-Bug: #1228040
Change-Id: I7d50164930c2ae7abeddcade4f876eef5b273b6b
This commit is contained in:
Stéphane Bisinger 2015-04-30 17:44:02 +02:00
parent 9a0d3c1461
commit d2f8f8fb1c
4 changed files with 52 additions and 3 deletions

View File

@ -70,6 +70,11 @@ class CustomObject(object):
name = wsme.types.text
class ExtendedInt(wsme.types.UserType):
basetype = int
name = "Extended integer"
class NestedInnerApi(object):
@expose(bool)
def deepfunction(self):
@ -324,6 +329,12 @@ class ArgTypes(object):
self.assertIsInstance(value.aint, int)
return value
@expose(ExtendedInt())
@validate(ExtendedInt())
def setextendedint(self, value):
self.assertEquals(isinstance(value, ExtendedInt.basetype), True)
return value
class BodyTypes(object):
def assertEquals(self, a, b):

View File

@ -11,7 +11,7 @@ except:
from wsme.rest.json import fromjson, tojson, parse
from wsme.utils import parse_isodatetime, parse_isotime, parse_isodate
from wsme.types import isarray, isdict, isusertype, register_type
from wsme.types import isarray, isdict, isusertype, register_type, UserType
from wsme.rest import expose, validate
from wsme.exc import ClientSideError, InvalidInput
@ -90,6 +90,11 @@ def prepare_result(value, datatype):
return value
class CustomInt(UserType):
basetype = int
name = "custom integer"
class Obj(wsme.types.Base):
id = int
name = wsme.types.text
@ -279,6 +284,15 @@ class TestRestJson(wsme.tests.protocol.RestOnlyProtocolTestCase):
self.assertEqual(r.status_int, 200)
self.assertEqual(r.json, {'aint': 2, 'name': 'test'})
def test_set_extended_int(self):
r = self.app.post(
'/argtypes/setextendedint',
'{"value": 3}',
headers={"Content-Type": "application/json"}
)
self.assertEqual(r.status_int, 200)
self.assertEqual(r.json, 3)
def test_unset_attrs(self):
class AType(object):
attr = int
@ -438,6 +452,30 @@ class TestRestJson(wsme.tests.protocol.RestOnlyProtocolTestCase):
self.assertIsInstance(i['a'], bool)
self.assertFalse(i['a'])
def test_valid_simple_custom_type_fromjson(self):
value = 2
for ba in True, False:
jd = '"%d"' if ba else '{"a": "%d"}'
i = parse(jd % value, {'a': CustomInt()}, ba)
self.assertEqual(i, {'a': 2})
self.assertIsInstance(i['a'], int)
def test_invalid_simple_custom_type_fromjson(self):
value = '2b'
for ba in True, False:
jd = '"%s"' if ba else '{"a": "%s"}'
try:
i = parse(jd % value, {'a': CustomInt()}, ba)
self.assertEqual(i, {'a': 2})
except ClientSideError as e:
self.assertIsInstance(e, InvalidInput)
self.assertEqual(e.fieldname, 'a')
self.assertEqual(e.value, value)
self.assertEqual(
e.msg,
"invalid literal for int() with base 10: '%s'" % value
)
def test_nest_result(self):
self.root.protocols[0].nest_result = True
r = self.app.get('/returntypes/getint.json')

View File

@ -18,7 +18,7 @@ class TestSpore(unittest.TestCase):
spore = json.loads(spore)
assert len(spore['methods']) == 50, str(len(spore['methods']))
assert len(spore['methods']) == 51, str(len(spore['methods']))
m = spore['methods']['argtypes_setbytesarray']
assert m['path'] == 'argtypes/setbytesarray', m['path']

View File

@ -397,7 +397,7 @@ class TestSOAP(wsme.tests.protocol.ProtocolTestCase):
assert len(sd.ports) == 1
port, methods = sd.ports[0]
self.assertEquals(len(methods), 50)
self.assertEquals(len(methods), 51)
methods = dict(methods)