tg2devtools
tg2devtools copied to clipboard
Improve robustness of tests with sqlalchemy
This PR fixes some issues with quickstarted projects:
-
model.init_model()
could be called multiple times through loading the app (functional tests) ortests.models.setup_db()
but wouldn't dispose of the previous session - mixed use of
transaction
andDBSession
APIs to manage transactions -
websetup
assumes that transactions are in theACTIVE
state but this doesn't hold true ifteardown_db()
was called before
This would cause functional tests to fail setting up the test application if setup_db()
was called in a previous test, which can easily be tested by renaming the tests.functional
to e.g. tests.zzz_functional
in a quickstarted project and then running the test suite.
NB: Having slept a night over it, a different way to address this would be to have exactly one test app, and one call to setup_db()
prior to all tests but I'm not sure how feasible this is.
DBSession.rollback()
was used because in theory you can have a project with SQLalchemy but without the transaction manager. I'm not sure if that's still working as expected as I have never done that, but it was in theory an option.
I'll try to reproduce locally the behaviour to have a better understanding of what's going on. I'm fairly ok with the changes, but I want to understand what causes them to be needed before merging them.