I've tried several approaches to do unit testing of views that have Model access sqlalchemy, but nothing seems to work. A problem always arises when running the second test. I am using the internal memory 'sqlite://' The first test works, because I need initialized models prior to call calling the tests the second test fails because drop_all and/or rollback to do not appears to work and the models can not be recreated. Below is example code based on the tutorial with sqlalchemy. with this code it fails with IntegrityError: (IntegrityError) column name is not unique u'INSERT INTO models (name, value) VALUES (?, ?)' (u'root', 55) while running the second test.
import unittest from pyramid.config import Configurator from pyramid import testing from tutorial.models import MyModel, DBSession, Base import transaction def _initTestingDB(): print "initTestingDB" from sqlalchemy import create_engine engine = create_engine('sqlite://') DBSession.configure(bind=engine) Base.metadata.bind = engine Base.metadata.drop_all(engine) Base.metadata.create_all(engine) create_models(DBSession) return DBSession def create_models(dbSession): session = dbSession() model = MyModel(name=u'root', value=55) session.add(model) session.flush() transaction.commit() def clear_models(dbSession): session = dbSession() old_object = session.query(MyModel) session.delete(old_object) session.flush() transaction.commit() print "clear_models" class TestMyView(unittest.TestCase): def setUp(self): self.config = testing.setUp() self.dbSession=_initTestingDB() def tearDown(self): testing.tearDown() clear_models(self.dbSession) print "rollback" self.dbSession.rollback() def test_it(self): from tutorial.views import my_view request = testing.DummyRequest() info = my_view(request) self.assertEqual(info['root'].name, 'root') self.assertEqual(info['project'], 'tutorial') def test_it2(self): from tutorial.views import my_view request = testing.DummyRequest() info = my_view(request) self.assertEqual(info['root'].name, 'root') self.assertEqual(info['project'], 'tutorial') -- You received this message because you are subscribed to the Google Groups "pylons-devel" group. To post to this group, send email to pylons-devel@googlegroups.com. To unsubscribe from this group, send email to pylons-devel+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-devel?hl=en.