klen / mixer

Mixer -- Is a fixtures replacement. Supported Django, Flask, SqlAlchemy and custom python objects.
Other
944 stars 95 forks source link

Sqlalchemy backend should create a transaction if it is going to call session.commit #115

Open daack93 opened 5 years ago

daack93 commented 5 years ago

See https://docs.sqlalchemy.org/en/13/orm/session_transaction.html http://alextechrants.blogspot.com/2013/08/unit-testing-sqlalchemy-apps.html

L312 of https://github.com/klen/mixer/blob/develop/mixer/backend/sqlalchemy.py will end the session's current transaction and commit them.

This will break things that rely on nested sessions, such as the example above which use nested transaction to encapsulate tests. It seems like a good argument not to use nested transactions that way, but I think you should also try to avoid interfering with user transactions if possible.

Ie. I think you should either drop the session.commit() line, and let users interact with sessions how they want, or call session.begin_nested() before session.add, so that the commit will not effect parent transactions.