tg2devtools icon indicating copy to clipboard operation
tg2devtools copied to clipboard

Improve robustness of tests with sqlalchemy

Open nphilipp opened this issue 4 years ago • 1 comments

This PR fixes some issues with quickstarted projects:

  • model.init_model() could be called multiple times through loading the app (functional tests) or tests.models.setup_db() but wouldn't dispose of the previous session
  • mixed use of transaction and DBSession APIs to manage transactions
  • websetup assumes that transactions are in the ACTIVE state but this doesn't hold true if teardown_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.

nphilipp avatar Dec 14 '19 21:12 nphilipp

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.

amol- avatar Dec 23 '19 14:12 amol-