More test coverage + code cleaning

This commit is contained in:
Christophe de Vienne
2011-10-15 23:10:57 +02:00
parent 3850d3dffb
commit ac728ecf93
4 changed files with 102 additions and 5 deletions

View File

@@ -232,7 +232,7 @@ class WSRoot(object):
def addprotocol(self, protocol, **options):
"""
Enable a new protocol on the controller.
:param protocol: A registered protocol name or an instance
of a protocol.
"""
@@ -244,7 +244,7 @@ class WSRoot(object):
def getapi(self):
"""
Returns the api description.
:rtype: list of :class:`FunctionDefinition`
"""
if self._api is None:

View File

@@ -40,8 +40,6 @@ class RestProtocol(object):
for arg in funcdef.arguments:
if arg.name not in parsed_args:
if arg.mandatory:
raise MissingArgument(arg.name)
continue
value = parsed_args.pop(arg.name)

View File

@@ -240,6 +240,18 @@ class ProtocolTestCase(unittest.TestCase):
print e
assert e.faultcode == 'Server'
assert e.faultstring == u'integer division or modulo by zero'
assert e.debuginfo is not None
def test_serverside_error_nodebug(self):
self.root._debug = False
try:
res = self.call('witherrors/divide_by_zero')
assert "No error raised"
except CallException, e:
print e
assert e.faultcode == 'Server'
assert e.faultstring == u'integer division or modulo by zero'
assert e.debuginfo is None
def test_touch(self):
r = self.call('touch')

View File

@@ -1,10 +1,13 @@
# encoding=utf8
import unittest
import webob
from webob.dec import wsgify
import webtest
from wsme import *
from wsme.controller import scan_api
from wsme.controller import getprotocol, scan_api, pexpose
from wsme.controller import FunctionArgument, FunctionDefinition
import wsme.wsgi
@@ -33,6 +36,46 @@ class DummyProtocol(object):
return str(infos)
def test_getprotocol():
try:
getprotocol('invalid')
assert False, "ValueError was not raised"
except ValueError, e:
pass
def test_pexpose():
class Proto(DummyProtocol):
def extract_path(self, request):
if request.path.endswith('ufunc'):
return ['_protocol', 'dummy', 'ufunc']
else:
return ['_protocol', 'dummy', 'func']
@pexpose(None, "text/xml")
def func(self):
return "<p></p>"
@pexpose(None, "text/xml")
def ufunc(self):
return u"<p>é</p>"
assert FunctionDefinition.get(Proto.func).return_type is None
assert FunctionDefinition.get(Proto.func).protocol_specific
assert FunctionDefinition.get(Proto.func).contenttype == "text/xml"
p = Proto()
r = WSRoot()
r.addprotocol(p)
app = webtest.TestApp(wsme.wsgi.adapt(r))
res = app.get('/func')
assert res.status_int == 200
assert res.body == "<p></p>", res.body
res = app.get('/ufunc')
assert res.unicode_body == u"<p>é</p>", res.body
class TestController(unittest.TestCase):
def test_expose(self):
class MyWS(WSRoot):
@@ -100,6 +143,16 @@ class TestController(unittest.TestCase):
assert fd.path == ['ns']
assert fd.name == 'multiply'
def test_scan_subclass(self):
class MyRoot(WSRoot):
class SubClass(object):
pass
r = MyRoot()
api = list(scan_api(r))
assert len(api) == 0
def test_scan_api_too_deep(self):
class Loop(object):
loop = None
@@ -137,6 +190,19 @@ class TestController(unittest.TestCase):
assert p.lastreq.path == '/touch'
assert p.hits == 2
class NoPathProto(DummyProtocol):
def extract_path(self, request):
return None
p = NoPathProto()
r = MyRoot(protocols=[p])
app = webtest.TestApp(wsme.wsgi.adapt(r))
res = app.get('/', expect_errors=True)
print res.status, res.body
assert res.status_int == 400
def test_no_available_protocol(self):
r = WSRoot()
@@ -165,3 +231,24 @@ class TestController(unittest.TestCase):
'Accept': 'text/plain'})
assert res.status_int == 400
assert res.content_type == 'text/plain', res.content_type
def test_getapi(self):
class MyRoot(WSRoot):
pass
r = MyRoot()
api = r.getapi()
assert r.getapi() is api
class TestFunctionDefinition(unittest.TestCase):
def test_get_arg(self):
def myfunc(self):
pass
fd = FunctionDefinition(FunctionDefinition)
fd.arguments.append(FunctionArgument('a', int, True, None))
assert fd.get_arg('a').datatype is int
assert fd.get_arg('b') is None