I am trying to learn a stack of flash technologies, and for my application I use Flask-SQLAlchemy. Everything works fine, but I'm struggling with writing integration tests. I do not want to use SQLite, since in production I use PostgreSQL, and putting tons of layouts actually checks my own implementation more than the logic itself.
So, after some research, I decided to implement tests that will write data to the test database, and after each test rollback changes (to achieve maximum efficiency). In fact, I'm trying to implement something similar to this approach: http://sontek.net/blog/detail/writing-tests-for-pyramid-and-sqlalchemy .
My problem is creating the correct transaction and rolling it back. Here is the code for my base class:
from flask.ext.sqlalchemy import SQLAlchemy db = SQLAlchemy() class MyAppIntegrationTestCase(unittest.TestCase): @classmethod def setUpClass(cls): app.config['TESTING'] = True app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2:///db_test' init_app() db.app = app db.create_all(app=app) @classmethod def tearDownClass(cls): db.drop_all(app=app) def setUp(self): db.session.rollback() self.trans = db.session.begin(subtransactions=True) def tearDown(self): self.trans.rollback()
When I try to run the tests, I got the following error:
Traceback (most recent call last): File "myapp/src/core/tests/__init__.py", line 53, in tearDown self.trans.rollback() File "myapp/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 370, in rollback self._assert_active(prepared_ok=True, rollback_ok=True) File "myapp/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 203, in _assert_active raise sa_exc.ResourceClosedError(closed_msg) ResourceClosedError: This transaction is closed
I am sure that this is a problem with scoped_session and that when I run the tests, it reuses one global session for all tests, but my knowledge in SQLAlchemy is not deep enough.
Any help would be greatly appreciated! Thanks!