Experiment: Clone DB to isolate tests#3872
Conversation
Analysis & Suggestions for Database Cloning ApproachThis is a great approach! Database cloning for test isolation is being discussed upstream in Django as a faster alternative to Issues Found1. Missing ImportsThe import sqlite3
import urllib.parse2. Settings Dict Reference Issueprev_settings[db_name] = conn.settings_dict # This is a reference, not a copy!When prev_settings[db_name] = conn.settings_dict.copy()3. Potential UnboundLocalError
test_database_name = None # Already there, good
# But in cleanup:
conn.creation.destroy_test_db(old_database_name=test_database_name) # Could be NoneConsider storing per-database: prev_db_names = {}
# ...
prev_db_names[db_name] = conn.settings_dict["NAME"]4. SQLite In-Memory Reconnection Orderconn.close()
conn.connect() # reconnect before closing so we don't lose the db
target.close()The comment says "reconnect before closing" but # Keep target connection open until Django reconnects
conn.settings_dict["NAME"] = sandbox_uri
conn.close()
conn.connect() # Now connected to the clone
# Safe to close the backup connections
source.close()
target.close()Architectural Suggestions1. Consider Using
|
|
This PR has not been worked for a long time but is usuful as inspiration for test improvement: see the conversation here |
Short description
This is one ongoing experiment trying to reign in the growing chaos in our tests
Proposed changes
snapshot_db()to clone the current database and switch out djangos connection, and reverting it afterwardsload_test_datafixture for testsThis experiment is still Work In Progress.
Side effects
Faithfulness to issue description and design
There are no intended deviations from the issue and design.
Resolved issues
Fixes: #3777
Pull Request Review Guidelines