# HG changeset patch # User Daniele Nicolodi # Date 1313431585 -7200 # Node ID 3d5850d656032736f9059bff5b05a8d7406a7a96 # Parent 90c1fa07f6a7c25c623abbc0dc41177091bc1a2c Factor out test case setup code for tests requiring the application request context. diff -r 90c1fa07f6a7 -r 3d5850d65603 src/ltpdarepo/tests/test_users.py --- a/src/ltpdarepo/tests/test_users.py Mon Aug 15 20:05:59 2011 +0200 +++ b/src/ltpdarepo/tests/test_users.py Mon Aug 15 20:06:25 2011 +0200 @@ -1,6 +1,7 @@ import unittest import MySQLdb as mysql +from ltpdarepo.tests.utils import RequestContextTestCase from ltpdarepo.user import User, _generate_password @@ -31,27 +32,17 @@ self.assertEqual(u['name'], 'Foo') -class DatabaseTestCase(unittest.TestCase): +class DatabaseTestCase(RequestContextTestCase): def setUp(self): from ltpdarepo.admin import wipe, install, useradd wipe() install() useradd('u1', 'u1') - - from ltpdarepo import app - self.app = app - self.app.config.update(HOSTNAME='localhost') - # fake request - self.ctx = self.app.test_request_context() - self.ctx.push() - # execute before request handlers - self.app.preprocess_request() + super(DatabaseTestCase, self).setUp() def tearDown(self): - # execute after request handlers - self.app.process_response(self.app.response_class()) - self.ctx.pop() + super(DatabaseTestCase, self).tearDown() from ltpdarepo.admin import wipe wipe() diff -r 90c1fa07f6a7 -r 3d5850d65603 src/ltpdarepo/tests/utils.py --- a/src/ltpdarepo/tests/utils.py Mon Aug 15 20:05:59 2011 +0200 +++ b/src/ltpdarepo/tests/utils.py Mon Aug 15 20:06:25 2011 +0200 @@ -1,5 +1,23 @@ +import unittest2 as unittest import zope.testbrowser.wsgi +class RequestContextTestCase(unittest.TestCase): + + def setUp(self): + # application + from ltpdarepo import app + self.app = app + # fake request + self.ctx = self.app.test_request_context() + self.ctx.push() + # execute before request handlers + self.app.preprocess_request() + + def tearDown(self): + # execute after request handlers + self.app.process_response(self.app.response_class()) + self.ctx.pop() + class Browser(zope.testbrowser.wsgi.Browser): def __init__(self, url='http://localhost/'): @@ -13,3 +31,9 @@ app.logger.addHandler(handler) super(Browser, self).__init__(url, wsgi_app=app) + + def login(self, username, password): + self.open('/login') + self.getControl(name='username').value = username + self.getControl(name='password').value = password + self.getControl(name='login').click()