From cf0b4c4ea3b7986d982546df947752d8e249b7a1 Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Wed, 28 Nov 2012 21:02:58 +0100 Subject: [PATCH 1/3] Remove useless imports --- wsme/rest/xml.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/wsme/rest/xml.py b/wsme/rest/xml.py index 90f156d..7e05278 100644 --- a/wsme/rest/xml.py +++ b/wsme/rest/xml.py @@ -2,14 +2,12 @@ from __future__ import absolute_import import datetime -from six import u import six import xml.etree.ElementTree as et from simplegeneric import generic -from wsme.rest.protocol import RestProtocol import wsme.types from wsme.exc import UnknownArgument From a8be9c213c7f923725304e4aac6dc07ef1778690 Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Wed, 28 Nov 2012 22:21:39 +0100 Subject: [PATCH 2/3] Fix the __eq__ operator for ArrayType --- wsme/types.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/wsme/types.py b/wsme/types.py index 314fc59..02a5f84 100644 --- a/wsme/types.py +++ b/wsme/types.py @@ -31,7 +31,8 @@ class ArrayType(object): return hash(self.item_type) def __eq__(self, other): - return self.item_type == other.item_type + return isinstance(other, ArrayType) \ + and self.item_type == other.item_type @property def item_type(self): From e2892482caced9ec5a8b5258009ef1930b49a9c1 Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Wed, 28 Nov 2012 22:40:53 +0100 Subject: [PATCH 3/3] Add unittests for returning array and dict of objects as attributes. --- wsme/tests/protocol.py | 36 ++++++++++++++++++++++++++++++++++++ wsme/tests/test_restjson.py | 11 ++++++++++- wsme/tests/test_restxml.py | 14 ++++++++++++-- wsme/tests/test_spore.py | 2 +- 4 files changed, 59 insertions(+), 4 deletions(-) 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']