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

Fixed #25251 -- Added cloning option to TransactionTestCase #18009

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

shangxiao
Copy link
Contributor

@shangxiao shangxiao commented Mar 24, 2024

https://code.djangoproject.com/ticket/25251
Forum post: https://forum.djangoproject.com/t/ticket-25251-opinions-required-cloning-the-test-db-as-an-alternative-to-rollback-emulation-in-transactiontestcase/29417

While looking at ticket 25251, I wondered whether cloning the test database per test in a TransactionTestCase was an option and found for my use cases it was a viable option. Experimentation & findings here: https://github.com/shangxiao/stupid-django-tricks/tree/master/clone_db_testcase

tl;dr:

  • ✓ Cloning resolves the issue of missing data loaded via migrations when --keepdb is specified
  • ✓ Cloning is much faster than rollback emulation aka serialized_rollback=True (tested on mysql, sqlite, postgres)
  • TransactionTestCase.reset_sequences doesn't really apply with cloning as sequences are naturally reset when switching back to the test database
  • TransactionTestCase.available_apps may no longer be necessary as cloning is already fast
  • TransactionTestCase.fixtures are loaded per test but could potentially be loaded per class if we do a double clone 🤷‍♂️

@shangxiao shangxiao force-pushed the transactiontestcase_with_cloning branch from 6a8b38f to 85dbba8 Compare March 24, 2024 06:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant