Updated the tests to use webob, removed the 'called' thing and just use return values instead.

This commit is contained in:
Eric Day 2010-08-18 15:00:20 -07:00
parent fd56f6ad60
commit 02592d584c
2 changed files with 40 additions and 86 deletions

View File

@ -22,49 +22,40 @@ Test for the root WSGI middleware for all API controllers.
import unittest import unittest
import stubout import stubout
import webob
import webob.dec
from nova import api from nova import api
from nova import wsgi_test
class Test(unittest.TestCase): class Test(unittest.TestCase):
def setUp(self): # pylint: disable-msg=C0103 def setUp(self): # pylint: disable-msg=C0103
self.called = False
self.stubs = stubout.StubOutForTesting() self.stubs = stubout.StubOutForTesting()
def tearDown(self): # pylint: disable-msg=C0103 def tearDown(self): # pylint: disable-msg=C0103
self.stubs.UnsetAll() self.stubs.UnsetAll()
def test_rackspace(self): def test_rackspace(self):
self.stubs.Set(api.rackspace, 'API', get_api_stub(self)) self.stubs.Set(api.rackspace, 'API', APIStub)
api.API()(wsgi_test.get_environ({'PATH_INFO': '/v1.0/cloud'}), result = webob.Request.blank('/v1.0/cloud').get_response(api.API())
wsgi_test.start_response) self.assertEqual(result.body, "/cloud")
self.assertTrue(self.called)
def test_ec2(self): def test_ec2(self):
self.stubs.Set(api.ec2, 'API', get_api_stub(self)) self.stubs.Set(api.ec2, 'API', APIStub)
api.API()(wsgi_test.get_environ({'PATH_INFO': '/ec2/cloud'}), result = webob.Request.blank('/ec2/cloud').get_response(api.API())
wsgi_test.start_response) self.assertEqual(result.body, "/cloud")
self.assertTrue(self.called)
def test_not_found(self): def test_not_found(self):
self.stubs.Set(api.ec2, 'API', get_api_stub(self)) self.stubs.Set(api.ec2, 'API', APIStub)
self.stubs.Set(api.rackspace, 'API', get_api_stub(self)) self.stubs.Set(api.rackspace, 'API', APIStub)
api.API()(wsgi_test.get_environ({'PATH_INFO': '/'}), result = webob.Request.blank('/test/cloud').get_response(api.API())
wsgi_test.start_response) self.assertNotEqual(result.body, "/cloud")
self.assertFalse(self.called)
def get_api_stub(test_object):
"""Get a stub class that verifies next part of the request."""
class APIStub(object): class APIStub(object):
"""Class to verify request and mark it was called.""" """Class to verify request and mark it was called."""
test = test_object
def __call__(self, environ, start_response): @webob.dec.wsgify
self.test.assertEqual(environ['PATH_INFO'], '/cloud') def __call__(self, req):
self.test.called = True return req.path_info
return APIStub

View File

@ -24,41 +24,34 @@ Test WSGI basics and provide some helper functions for other WSGI tests.
import unittest import unittest
import routes import routes
import webob
from nova import wsgi from nova import wsgi
class Test(unittest.TestCase): class Test(unittest.TestCase):
def setUp(self): # pylint: disable-msg=C0103
self.called = False
def test_debug(self): def test_debug(self):
class Application(wsgi.Application): class Application(wsgi.Application):
"""Dummy application to test debug.""" """Dummy application to test debug."""
test = self
def __call__(self, environ, test_start_response): def __call__(self, environ, start_response):
test_start_response("200", [("X-Test", "checking")]) start_response("200", [("X-Test", "checking")])
self.test.called = True return ['Test result']
return ['Test response']
app = wsgi.Debug(Application())(get_environ(), start_response) application = wsgi.Debug(Application())
self.assertTrue(self.called) result = webob.Request.blank('/').get_response(application)
for _ in app: self.assertEqual(result.body, "Test result")
pass
def test_router(self): def test_router(self):
class Application(wsgi.Application): class Application(wsgi.Application):
"""Test application to call from router.""" """Test application to call from router."""
test = self
def __call__(self, environ, test_start_response): def __call__(self, environ, start_response):
test_start_response("200", []) start_response("200", [])
self.test.called = True return ['Router result']
return []
class Router(wsgi.Router): class Router(wsgi.Router):
"""Test router.""" """Test router."""
@ -68,11 +61,10 @@ class Test(unittest.TestCase):
mapper.connect("/test", controller=Application()) mapper.connect("/test", controller=Application())
super(Router, self).__init__(mapper) super(Router, self).__init__(mapper)
Router()(get_environ({'PATH_INFO': '/test'}), start_response) result = webob.Request.blank('/test').get_response(Router())
self.assertTrue(self.called) self.assertEqual(result.body, "Router result")
self.called = False result = webob.Request.blank('/bad').get_response(Router())
Router()(get_environ({'PATH_INFO': '/bad'}), start_response) self.assertNotEqual(result.body, "Router result")
self.assertFalse(self.called)
def test_controller(self): def test_controller(self):
@ -80,11 +72,11 @@ class Test(unittest.TestCase):
"""Test controller to call from router.""" """Test controller to call from router."""
test = self test = self
def show(self, **kwargs): def show(self, req, id): # pylint: disable-msg=W0622,C0103
"""Mark that this has been called.""" """Default action called for requests with an ID."""
self.test.called = True self.test.assertEqual(req.path_info, '/tests/123')
self.test.assertEqual(kwargs['id'], '123') self.test.assertEqual(id, '123')
return "Test" return id
class Router(wsgi.Router): class Router(wsgi.Router):
"""Test router.""" """Test router."""
@ -94,40 +86,11 @@ class Test(unittest.TestCase):
mapper.resource("test", "tests", controller=Controller()) mapper.resource("test", "tests", controller=Controller())
super(Router, self).__init__(mapper) super(Router, self).__init__(mapper)
Router()(get_environ({'PATH_INFO': '/tests/123'}), start_response) result = webob.Request.blank('/tests/123').get_response(Router())
self.assertTrue(self.called) self.assertEqual(result.body, "123")
self.called = False result = webob.Request.blank('/test/123').get_response(Router())
Router()(get_environ({'PATH_INFO': '/test/123'}), start_response) self.assertNotEqual(result.body, "123")
self.assertFalse(self.called)
def test_serializer(self): def test_serializer(self):
# TODO(eday): Placeholder for serializer testing. # TODO(eday): Placeholder for serializer testing.
pass pass
def get_environ(overwrite={}): # pylint: disable-msg=W0102
"""Get a WSGI environment, overwriting any entries given."""
environ = {'SERVER_PROTOCOL': 'HTTP/1.1',
'GATEWAY_INTERFACE': 'CGI/1.1',
'wsgi.version': (1, 0),
'SERVER_PORT': '443',
'SERVER_NAME': '127.0.0.1',
'REMOTE_ADDR': '127.0.0.1',
'wsgi.run_once': False,
'wsgi.errors': None,
'wsgi.multiprocess': False,
'SCRIPT_NAME': '',
'wsgi.url_scheme': 'https',
'wsgi.input': None,
'REQUEST_METHOD': 'GET',
'PATH_INFO': '/',
'CONTENT_TYPE': 'text/plain',
'wsgi.multithread': True,
'QUERY_STRING': '',
'eventlet.input': None}
return dict(environ, **overwrite)
def start_response(_status, _headers):
"""Dummy start_response to use with WSGI tests."""
pass