Adapt the rest xml tests

This commit is contained in:
Christophe de Vienne 2011-09-24 22:41:24 +02:00
parent 44e86b5cb4
commit d24f84d940
2 changed files with 40 additions and 13 deletions

View File

@ -249,8 +249,8 @@ class ProtocolTestCase(unittest.TestCase):
assert r == binarysample or r == base64.encodestring(binarysample), r assert r == binarysample or r == base64.encodestring(binarysample), r
def test_return_nested(self): def test_return_nested(self):
r = self.call('returntypes/getnested') r = self.call('returntypes/getnested', _rt=NestedOuter)
assert r == {'inner': {'aint': 0}} or r == {'inner': {'aint': '0'}}, r assert r == {'inner': {'aint': 0}}, r
def test_setstr(self): def test_setstr(self):
assert self.call('argtypes/setstr', value='astring') == 'astring' assert self.call('argtypes/setstr', value='astring') == 'astring'
@ -260,7 +260,7 @@ class ProtocolTestCase(unittest.TestCase):
_rt=unicode) == u'' _rt=unicode) == u''
def test_setint(self): def test_setint(self):
r = self.call('argtypes/setint', value=3) r = self.call('argtypes/setint', value=3, _rt=int)
assert r == 3, r assert r == 3, r
def test_setfloat(self): def test_setfloat(self):

View File

@ -1,7 +1,9 @@
import decimal import decimal
import datetime import datetime
import base64
import wsme.tests.protocol import wsme.tests.protocol
from wsme.utils import *
try: try:
import xml.etree.ElementTree as et import xml.etree.ElementTree as et
@ -11,34 +13,59 @@ except:
import wsme.restxml import wsme.restxml
def dumpxml(key, obj): def dumpxml(key, obj, datatype=None):
el = et.Element(key) el = et.Element(key)
if isinstance(obj, basestring): if isinstance(obj, tuple):
obj, datatype = obj
if datatype == wsme.types.binary:
el.text = base64.encodestring(obj)
elif isinstance(obj, basestring):
el.text = obj el.text = obj
elif type(obj) in (int, float, decimal.Decimal): elif type(obj) in (int, float, decimal.Decimal):
el.text = str(obj) el.text = str(obj)
elif type(obj) in (datetime.date, datetime.time, datetime.datetime): elif type(obj) in (datetime.date, datetime.time, datetime.datetime):
el.text = obj.isoformat() el.text = obj.isoformat()
elif hasattr(datatype, '_wsme_attributes'):
for name, attr in datatype._wsme_attributes:
if name not in obj:
continue
o = obj[name]
el.append(dumpxml(name, o, attr.datatype))
elif type(obj) == dict: elif type(obj) == dict:
for key, obj in obj.items(): for name, value in obj.items():
el.append(dumpxml(key, obj)) el.append(dumpxml(name, value))
return el return el
def loadxml(el): def loadxml(el, datatype):
print el, datatype, len(el)
if len(el): if len(el):
d = {} d = {}
for child in el: for name, attr in datatype._wsme_attributes:
d[child.tag] = loadxml(child) child = el.find(name)
print name, attr, child
if child is not None:
d[name] = loadxml(child, attr.datatype)
print d
return d return d
else: else:
return el.text if datatype == datetime.date:
return parse_isodate(el.text)
if datatype == datetime.time:
return parse_isotime(el.text)
if datatype == datetime.datetime:
return parse_isodatetime(el.text)
if datatype == wsme.types.binary:
return base64.decodestring(el.text)
if datatype is None:
return el.text
return datatype(el.text)
class TestRestXML(wsme.tests.protocol.ProtocolTestCase): class TestRestXML(wsme.tests.protocol.ProtocolTestCase):
protocol = 'REST+XML' protocol = 'REST+XML'
def call(self, fpath, **kw): def call(self, fpath, _rt=None, **kw):
el = dumpxml('parameters', kw) el = dumpxml('parameters', kw)
content = et.tostring(el) content = et.tostring(el)
res = self.app.post( res = self.app.post(
@ -58,4 +85,4 @@ class TestRestXML(wsme.tests.protocol.ProtocolTestCase):
el.find('debuginfo').text or None) el.find('debuginfo').text or None)
else: else:
return loadxml(et.fromstring(res.body)) return loadxml(et.fromstring(res.body), _rt)