Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for nested transactions #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

smvv
Copy link

@smvv smvv commented May 29, 2018

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 the on_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's setup_method using database.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.

@codecov
Copy link

codecov bot commented May 30, 2018

Codecov Report

Merging #2 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master     #2   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           3      3           
  Lines          40     48    +8     
  Branches        1      4    +3     
=====================================
+ Hits           40     48    +8
Impacted Files Coverage Δ
apistar_sqlalchemy/components.py 100% <100%> (ø) ⬆️
apistar_sqlalchemy/event_hooks.py 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 87274fa...0667e66. Read the comment docs.

@perdy
Copy link
Owner

perdy commented Jun 4, 2018

I'm happy with the changes but I'd like to keep the tests at a higher level. Maybe can you modify the current test_end_to_end.py file to test plain and nested transactions behavior? In that case we wouldn't need to extract the model into a different file.

Thanks for that !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants