Created
January 16, 2011 02:36
-
-
Save shazow/781492 to your computer and use it in GitHub Desktop.
Slightly-modified Pylons project unit tests root module.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Pylons application test package | |
This package assumes the Pylons environment is already loaded, such as | |
when this script is imported from the `nosetests --with-pylons=test.ini` | |
command. | |
This module initializes the application via ``websetup`` (`paster | |
setup-app`) and provides the base testing objects. | |
""" | |
from unittest import TestCase | |
from paste.deploy import loadapp | |
from paste.script.appinstall import SetupCommand | |
from pylons import url | |
from routes.util import URLGenerator | |
from webtest import TestApp | |
import pylons.test | |
__all__ = ['environ', 'url', 'TestController', 'TestModel', 'model', 'Session'] | |
# Invoke websetup with the current config file | |
SetupCommand('setup-app').run([pylons.test.pylonsapp.config['__file__']]) | |
environ = {} | |
from myproject import model | |
Session = model.Session | |
class TestModel(TestCase): | |
def setUp(self): | |
model.metadata.create_all(bind=Session.bind) | |
def tearDown(self): | |
Session.rollback() | |
model.metadata.drop_all(bind=Session.bind) | |
class TestController(TestModel): | |
def _populate(self, num=1, is_admin=False): | |
# FIXME: Is there any point in returning the full user object instead of the user.id? | |
r = [model.User.create(email="{0}@localhost.com".format(i), is_admin=False) for i in xrange(num)] | |
[u.set_password('test') for u in r] | |
Session.commit() | |
return [u.id for u in r] | |
def _do_login(self, email=None, user=None, id=None, password='test'): | |
if id: | |
user = model.User.get(id) | |
if user: | |
email = user.email | |
return self.app.post('/account/login', {'email': email, 'password': password}).follow() | |
def _do_logout(self): | |
return self.app.get('/account/logout').follow() | |
def __init__(self, *args, **kwargs): | |
wsgiapp = pylons.test.pylonsapp | |
config = wsgiapp.config | |
self.app = TestApp(wsgiapp) | |
self.app.extra_environ['REMOTE_ADDR'] = '127.0.0.1' | |
url._push_object(URLGenerator(config['routes.map'], environ)) | |
TestCase.__init__(self, *args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment