Hello,
I am trying to do unit tests of my handler actions (using pyramid_handlers) and I would like the test method and the view-callable to use the same database transaction which gets rolled back after each test.
I have a test fixture that creates a database engine in the setUp method:
def setUp(self):
settings = appconfig('config:test.ini',
name=myapp,
relative_to=os.getcwd())
engine = sa.engine_from_config(settings, prefix="sqlalchemy.")
conn = engine.connect()
self.trans = conn.begin()
sqlahelper.get_session().configure(bind=conn)
self.session = sqlahelper.get_session()
In the tearDown it rollsback the transaction, closes the self.session, and resets sqlahelper.
There is a test method that calls a view-callable that modifies and saves (using Session.flush()) an SQLAlchemy model instance. The test method then issues a query using self.session, but it does not see the changes because it is not in the same transaction as the view-callable's session.
The next test methods (which are all read-only) do see the changes, but I don't know why this happens since I reset sqlahelper in the tearDown() between tests.
After all the tests finish the session that altered the model does is rolled back, but this is probably due to not calling commit in the transaction manager and not because of my explicit rollback call in tearDown.
Has anyone successfully setup testing with SQLAlchemy without rebuilding an entire database on every test?
Thanks,
Jason