From ac728ecf93bd8fbfa775ca4636a093cb079f8435 Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Sat, 15 Oct 2011 23:10:57 +0200 Subject: [PATCH] More test coverage + code cleaning --- wsme/controller.py | 4 +- wsme/rest.py | 2 - wsme/tests/protocol.py | 12 +++++ wsme/tests/test_controller.py | 89 ++++++++++++++++++++++++++++++++++- 4 files changed, 102 insertions(+), 5 deletions(-) diff --git a/wsme/controller.py b/wsme/controller.py index 0d4346f..4a28894 100644 --- a/wsme/controller.py +++ b/wsme/controller.py @@ -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: diff --git a/wsme/rest.py b/wsme/rest.py index 4f0907c..8c9b22e 100644 --- a/wsme/rest.py +++ b/wsme/rest.py @@ -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) diff --git a/wsme/tests/protocol.py b/wsme/tests/protocol.py index 0238d5a..f03a1cf 100644 --- a/wsme/tests/protocol.py +++ b/wsme/tests/protocol.py @@ -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') diff --git a/wsme/tests/test_controller.py b/wsme/tests/test_controller.py index d597951..fb00953 100644 --- a/wsme/tests/test_controller.py +++ b/wsme/tests/test_controller.py @@ -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 "

" + + @pexpose(None, "text/xml") + def ufunc(self): + return u"

é

" + + 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 == "

", res.body + res = app.get('/ufunc') + assert res.unicode_body == u"

é

", 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