Question

I'm using fixtures with SQLAlchemy to create some integration tests.

I'd like to put SQLAlchemy into a "never commit" mode to prevent changes ever being written to the database, so that my tests are completely isolated from each other. Is there a way to do this?

My initial thoughts are that perhaps I could replace Session.commit with a mock object; however I'm not sure if there are other things that might have the same effect that I also need to mock if I'm going to go down that route.

Was it helpful?

Solution

The scoped session manager will by default return the same session object for each connection. Accordingly, one can replace .commit with .flush, and have that change persist across invocations to the session manager.

That will prevent commits.

To then rollback all changes, one should use session.transaction.rollback().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top