Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Setting the flag
nested=True
on the transaction hook allows nested sqlalchemy transactions.Instead of creating and destroying all SQL tables before/after a unit test, it is faster to start a transaction and perform a rollback after the unit test. The problem is that without the
nested=True
flag, the transaction hook will commit the changes in theon_response
method. When rolling the transaction back, the transaction is already committed and that causes the rollback to fail.Another problem is that the transaction hook will remove the session, which will commit the started transaction outside of the transaction hook. When setting
nested=True
, it is required that the session is removed during the unit test'ssetup_method
usingdatabase.Session.remove()
. Otherwise, sqlalchemy will report a warning about a session scope that is already in use.edit: Appearently, python 3.5 fails with
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such savepoint: sa_savepoint_2 [SQL: 'RELEASE SAVEPOINT sa_savepoint_2']
. Sqlite in Python 3.5 has a default setting that doesn't work as expected and that setting has been changed in python 3.6: https://docs.python.org/3/library/sqlite3.html#sqlite3-controlling-transactions.See also the related Python stdlib issue: https://bugs.python.org/issue10740.