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

A testing-only transaction manager which replaces nested transactions with savepoints #26504

Open
Tracked by #22959
roji opened this issue Nov 1, 2021 · 2 comments
Open
Tracked by #22959

Comments

@roji
Copy link
Member

roji commented Nov 1, 2021

It is currently a bit challenging to write integration tests for (user) product code which explicitly manages transactions. Since database don't support nested transactions, users can't run the entire test in a transaction and roll it back (to avoid test interference, as can be done with tests which don't manage transactions). This causes the following difficulties:

  • The database needs to be reverted to its initial state after every test runs, which can be tricky.
  • Test parallelization must be disabled.

We could provide an IDbContextTransactionManager which "implements" nested transactions as savepoints. That is, if BeginTransaction is called and a transaction already exists, we could create a savepoint instead. This would allow the test to fully manage transactions, but without ever actually committing anything, working around the issues above.

Originally discussed in npgsql/npgsql#3982

@roji
Copy link
Member Author

roji commented Nov 2, 2021

@AndriySvyryd this may not be useful in scenarios where multiple contexts are used (since the transaction needs to be shared). See:

public static void ExecuteWithStrategyInTransaction<TContext>(

@yori-s
Copy link

yori-s commented Feb 3, 2023

Is there a quick and dirty solution for code that (a) doesn't explicitly set transaction boundaries and (b) tests don't run in parallel?

I recently had a really nice experience with a rails/pg app where tests needed minimal scaffolding (thanks to factorybot), cleaned up after themselves, and ran in good time.

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

No branches or pull requests

3 participants