diff --git a/wsme/tests/protocol.py b/wsme/tests/protocol.py index 9f5e27c..76f05d7 100644 --- a/wsme/tests/protocol.py +++ b/wsme/tests/protocol.py @@ -49,6 +49,8 @@ class NestedInner(object): class NestedOuter(object): inner = NestedInner + inner_array = wsme.types.wsattr([NestedInner]) + inner_dict = {wsme.types.text: NestedInner} def __init__(self): self.inner = NestedInner(0) @@ -127,6 +129,21 @@ class ReturnTypes(object): def getnesteddict(self): return {b('a'): NestedOuter(), b('b'): NestedOuter()} + @expose(NestedOuter) + def getobjectarrayattribute(self): + obj = NestedOuter() + obj.inner_array = [NestedInner(12), NestedInner(13)] + return obj + + @expose(NestedOuter) + def getobjectdictattribute(self): + obj = NestedOuter() + obj.inner_dict = { + '12': NestedInner(12), + '13': NestedInner(13) + } + return obj + @expose(myenumtype) def getenum(self): return b('v2') @@ -407,6 +424,25 @@ class ProtocolTestCase(unittest.TestCase): b('a'): {'inner': {'aint': 0}}, b('b'): {'inner': {'aint': 0}}}, r + def test_return_objectarrayattribute(self): + r = self.call('returntypes/getobjectarrayattribute', + _rt=NestedOuter) + assert r == { + 'inner': {'aint': 0}, + 'inner_array': [{'aint': 12}, {'aint': 13}] + }, r + + def test_return_objectdictattribute(self): + r = self.call('returntypes/getobjectdictattribute', + _rt=NestedOuter) + assert r == { + 'inner': {'aint': 0}, + 'inner_dict': { + '12': {'aint': 12}, + '13': {'aint': 13} + } + }, r + def test_return_enum(self): r = self.call('returntypes/getenum', _rt=myenumtype) assert r == b('v2'), r diff --git a/wsme/tests/test_restjson.py b/wsme/tests/test_restjson.py index 90d7712..9e62749 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 from wsme.utils import parse_isodatetime, parse_isotime, parse_isodate -from wsme.types import isusertype, register_type +from wsme.types import isarray, isdict, isusertype, register_type from wsme.rest import expose, validate @@ -53,12 +53,20 @@ def prepare_result(value, datatype): datatype = datatype.basetype if isinstance(datatype, list): return [prepare_result(item, datatype[0]) for item in value] + if isarray(datatype): + return [prepare_result(item, datatype.item_type) for item in value] if isinstance(datatype, dict): return dict(( (prepare_result(item[0], list(datatype.keys())[0]), prepare_result(item[1], list(datatype.values())[0])) for item in value.items() )) + if isdict(datatype): + return dict(( + (prepare_result(item[0], datatype.key_type), + prepare_result(item[1], datatype.value_type)) + for item in value.items() + )) if datatype == datetime.date: return parse_isodate(value) if datatype == datetime.time: @@ -74,6 +82,7 @@ def prepare_result(value, datatype): if datatype == wsme.types.bytes: return value.encode('ascii') if type(value) != datatype: + print(type(value), datatype) return datatype(value) return value diff --git a/wsme/tests/test_restxml.py b/wsme/tests/test_restxml.py index 982df7f..4ec0d1b 100644 --- a/wsme/tests/test_restxml.py +++ b/wsme/tests/test_restxml.py @@ -7,14 +7,14 @@ import six import wsme.tests.protocol from wsme.utils import parse_isodatetime, parse_isodate, parse_isotime -from wsme.types import isusertype, register_type +from wsme.types import isarray, isdict, isusertype, register_type from wsme.rest.xml import fromxml, toxml try: import xml.etree.ElementTree as et except: - import cElementTree as et + import cElementTree as et # noqa def dumpxml(key, obj, datatype=None): @@ -60,6 +60,10 @@ def loadxml(el, datatype): return None if isinstance(datatype, list): return [loadxml(item, datatype[0]) for item in el.findall('item')] + elif isarray(datatype): + return [ + loadxml(item, datatype.item_type) for item in el.findall('item') + ] elif isinstance(datatype, dict): key_type, value_type = list(datatype.items())[0] return dict(( @@ -67,6 +71,12 @@ def loadxml(el, datatype): loadxml(item.find('value'), value_type)) for item in el.findall('item') )) + elif isdict(datatype): + return dict(( + (loadxml(item.find('key'), datatype.key_type), + loadxml(item.find('value'), datatype.value_type)) + for item in el.findall('item') + )) elif len(el): d = {} for attr in datatype._wsme_attributes: diff --git a/wsme/tests/test_spore.py b/wsme/tests/test_spore.py index 0200df4..e8b061f 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']) == 41, str(len(spore['methods'])) + assert len(spore['methods']) == 43, str(len(spore['methods'])) m = spore['methods']['argtypes_setbytesarray'] assert m['path'] == 'argtypes/setbytesarray', m['path']