I'm trying to follow the Testing + SQLAlchemy method from sontek.
I'm not using py.test, but simply $ python setup.py test
I'm getting into trouble with multiple sessions. My error is.
InvalidRequestError: Object '<Role at 0x1041f2dd0>' is already attached
to session '2' (this is '3')
I've rearranged things to try to not instantiate sessions in both main()
and in the BaseTestCase.
I've also noticed that my models.py is using the ZopeTransactionExtension,
so I've tried mirroring that
in my Base Test Case.
My test looks like:
import os
from paste.deploy.loadwsgi import appconfig
from sqlalchemy import engine_from_config
from sqlalchemy.orm import sessionmaker
from zope.sqlalchemy import ZopeTransactionExtension
from server import main
from server.models import DBSession, Base
from server.models import User
import unittest
from pyramid import testing
from webtest import TestApp
import transaction
def getSettings():
here = os.path.dirname(os.path.abspath(__file__))
path = os.path.join(here, '..', 'my.ini')
return appconfig('config:%s' % path)
#
class BaseTestCase(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.engine = engine_from_config(getSettings(), prefix='sqlalchemy.')
cls.Session = sessionmaker(extension=ZopeTransactionExtension())
def setUp(self):
connection = self.engine.connect()
# begin a non-ORM transaction
self.trans = connection.begin()
# bind an individual Session to the connection
DBSession.configure(bind=connection)
self.session = self.Session(bind=connection)
Base.session = self.session
def tearDown(self):
# rollback - everything that happened with the
# Session above (including calls to commit())
# is rolled back.
testing.tearDown()
self.trans.rollback()
self.session.close()
#
#
class IntegrationTestBase(BaseTestCase):
@classmethod
def setUpClass(cls):
settings = getSettings()
cls.app = main({}, **settings)
super(IntegrationTestBase, cls).setUpClass()
def setUp(self):
self.app = TestApp(self.app)
self.config = testing.setUp()
super(IntegrationTestBase, self).setUp()
#
#
class TestUserServices(IntegrationTestBase):
def test_empty_login(self):
""" Empty login fails """
res = self.app.post('/login', {'submit': True})
assert "Invalid username or password" in res.body
assert res.status_int == 200
#
def test_valid_login(self):
""" Valid login succeeds. """
self.importer = User(username='role_test_admin3', name=u"Test Admin",
lname= u"User",
password='testuserpass', email="[email protected]",
roles=['admin'])
self.rep = User(username='role_test_rep3', name=u"Test Rep", lname=
u"User",
password='testuserpass', email="[email protected]", roles=['rep'])
self.session.add(self.importer)
self.session.add(self.rep)
self.session.flush()
self.session.commit()
res = self.app.post('/login',
{
'submit': True,
'username': 'role_test_admin3',
'password': 'testuserpass',
}
)
print res.json_body
assert res.json_body['success']
assert res.status_int == 200
#
#
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.