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

View File

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