summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStéphane Bisinger <stephane.bisinger@gmail.com>2015-04-30 17:44:02 +0200
committerStéphane Bisinger <stephane.bisinger@gmail.com>2015-05-07 17:05:09 +0200
commitd2f8f8fb1c894bef307f9661f04af7e87cf149ef (patch)
tree28c416d84e0f325645d97450acc6c50373dcde23
parent9a0d3c1461c272567653defdfec1c3680939dbdb (diff)
downloadwsme-d2f8f8fb1c894bef307f9661f04af7e87cf149ef.tar.gz
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
-rw-r--r--wsme/tests/protocol.py11
-rw-r--r--wsme/tests/test_restjson.py40
-rw-r--r--wsme/tests/test_spore.py2
-rw-r--r--wsmeext/tests/test_soap.py2
4 files changed, 52 insertions, 3 deletions
diff --git a/wsme/tests/protocol.py b/wsme/tests/protocol.py
index b70f02d..e706ffb 100644
--- a/wsme/tests/protocol.py
+++ b/wsme/tests/protocol.py
@@ -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):
diff --git a/wsme/tests/test_restjson.py b/wsme/tests/test_restjson.py
index 08af1c0..23731d6 100644
--- a/wsme/tests/test_restjson.py
+++ b/wsme/tests/test_restjson.py
@@ -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')
diff --git a/wsme/tests/test_spore.py b/wsme/tests/test_spore.py
index 60afdc9..d6509f3 100644
--- a/wsme/tests/test_spore.py
+++ b/wsme/tests/test_spore.py
@@ -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']
diff --git a/wsmeext/tests/test_soap.py b/wsmeext/tests/test_soap.py
index 9fdfebc..edb51cf 100644
--- a/wsmeext/tests/test_soap.py
+++ b/wsmeext/tests/test_soap.py
@@ -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)