Skip to content

Commit

Permalink
Updated the tests to use webob, removed the 'called' thing and just u…
Browse files Browse the repository at this point in the history
…se return values instead.
  • Loading branch information
Eric Day committed Aug 18, 2010
1 parent fd56f6a commit 02592d5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 86 deletions.
43 changes: 17 additions & 26 deletions nova/api/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,49 +22,40 @@
import unittest

import stubout
import webob
import webob.dec

from nova import api
from nova import wsgi_test


class Test(unittest.TestCase):

def setUp(self): # pylint: disable-msg=C0103
self.called = False
self.stubs = stubout.StubOutForTesting()

def tearDown(self): # pylint: disable-msg=C0103
self.stubs.UnsetAll()

def test_rackspace(self):
self.stubs.Set(api.rackspace, 'API', get_api_stub(self))
api.API()(wsgi_test.get_environ({'PATH_INFO': '/v1.0/cloud'}),
wsgi_test.start_response)
self.assertTrue(self.called)
self.stubs.Set(api.rackspace, 'API', APIStub)
result = webob.Request.blank('/v1.0/cloud').get_response(api.API())
self.assertEqual(result.body, "/cloud")

def test_ec2(self):
self.stubs.Set(api.ec2, 'API', get_api_stub(self))
api.API()(wsgi_test.get_environ({'PATH_INFO': '/ec2/cloud'}),
wsgi_test.start_response)
self.assertTrue(self.called)
self.stubs.Set(api.ec2, 'API', APIStub)
result = webob.Request.blank('/ec2/cloud').get_response(api.API())
self.assertEqual(result.body, "/cloud")

def test_not_found(self):
self.stubs.Set(api.ec2, 'API', get_api_stub(self))
self.stubs.Set(api.rackspace, 'API', get_api_stub(self))
api.API()(wsgi_test.get_environ({'PATH_INFO': '/'}),
wsgi_test.start_response)
self.assertFalse(self.called)
self.stubs.Set(api.ec2, 'API', APIStub)
self.stubs.Set(api.rackspace, 'API', APIStub)
result = webob.Request.blank('/test/cloud').get_response(api.API())
self.assertNotEqual(result.body, "/cloud")


def get_api_stub(test_object):
"""Get a stub class that verifies next part of the request."""
class APIStub(object):
"""Class to verify request and mark it was called."""

class APIStub(object):
"""Class to verify request and mark it was called."""
test = test_object

def __call__(self, environ, start_response):
self.test.assertEqual(environ['PATH_INFO'], '/cloud')
self.test.called = True

return APIStub
@webob.dec.wsgify
def __call__(self, req):
return req.path_info
83 changes: 23 additions & 60 deletions nova/wsgi_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,34 @@
import unittest

import routes
import webob

from nova import wsgi


class Test(unittest.TestCase):

def setUp(self): # pylint: disable-msg=C0103
self.called = False

def test_debug(self):

class Application(wsgi.Application):
"""Dummy application to test debug."""
test = self

def __call__(self, environ, test_start_response):
test_start_response("200", [("X-Test", "checking")])
self.test.called = True
return ['Test response']
def __call__(self, environ, start_response):
start_response("200", [("X-Test", "checking")])
return ['Test result']

app = wsgi.Debug(Application())(get_environ(), start_response)
self.assertTrue(self.called)
for _ in app:
pass
application = wsgi.Debug(Application())
result = webob.Request.blank('/').get_response(application)
self.assertEqual(result.body, "Test result")

def test_router(self):

class Application(wsgi.Application):
"""Test application to call from router."""
test = self

def __call__(self, environ, test_start_response):
test_start_response("200", [])
self.test.called = True
return []
def __call__(self, environ, start_response):
start_response("200", [])
return ['Router result']

class Router(wsgi.Router):
"""Test router."""
Expand All @@ -68,23 +61,22 @@ def __init__(self):
mapper.connect("/test", controller=Application())
super(Router, self).__init__(mapper)

Router()(get_environ({'PATH_INFO': '/test'}), start_response)
self.assertTrue(self.called)
self.called = False
Router()(get_environ({'PATH_INFO': '/bad'}), start_response)
self.assertFalse(self.called)
result = webob.Request.blank('/test').get_response(Router())
self.assertEqual(result.body, "Router result")
result = webob.Request.blank('/bad').get_response(Router())
self.assertNotEqual(result.body, "Router result")

def test_controller(self):

class Controller(wsgi.Controller):
"""Test controller to call from router."""
test = self

def show(self, **kwargs):
"""Mark that this has been called."""
self.test.called = True
self.test.assertEqual(kwargs['id'], '123')
return "Test"
def show(self, req, id): # pylint: disable-msg=W0622,C0103
"""Default action called for requests with an ID."""
self.test.assertEqual(req.path_info, '/tests/123')
self.test.assertEqual(id, '123')
return id

class Router(wsgi.Router):
"""Test router."""
Expand All @@ -94,40 +86,11 @@ def __init__(self):
mapper.resource("test", "tests", controller=Controller())
super(Router, self).__init__(mapper)

Router()(get_environ({'PATH_INFO': '/tests/123'}), start_response)
self.assertTrue(self.called)
self.called = False
Router()(get_environ({'PATH_INFO': '/test/123'}), start_response)
self.assertFalse(self.called)
result = webob.Request.blank('/tests/123').get_response(Router())
self.assertEqual(result.body, "123")
result = webob.Request.blank('/test/123').get_response(Router())
self.assertNotEqual(result.body, "123")

def test_serializer(self):
# TODO(eday): Placeholder for serializer testing.
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

0 comments on commit 02592d5

Please sign in to comment.