How to test that nothing in a django-accessed database has changed?
I'm refactoring some code and adding an
atomic.transaction block. We have some code that performs auto-updates en-masse, after the atomic block. (It can't be in the atomic block, because it traverses some many-related tables, which requires queries since the ORM doesn't store the distinct relations.)
According to my reading of the code, it seems impossible that any changes could occur, but having a test to prove it would be nice in case someone makes a bad code edit in the future.
So how would I go about writing a test that proves nothing in the database has changed between the start of the test and the end of the test?
The database is postgres. We currently have it configured for 2 databases (the database proper and a "validation" database which I'd created after we discovered the loading code had side-effects in dry-run mode. I'm in the middle of a refactor that has fixed the side-effects issue and I just added the atomic transaction block. So I would like to write a test like this:
def test_no_side_effects_in_dry_run_mode(self): orig_db_state = self.get_db_state() # How do I write this? call_command( "load_animals_and_samples", animal_and_sample_table_filename=("data.xlsx"), dry_run=True, ) post_db_state = self.get_db_state() # How do I write this? self.assertNoDatabaseChange(orig_db_state, post_db_state, msg="Oops, database changed unexpectedly.") # How do I write this?
I have previously written a test that saves the counts of all the records in every table and saves them in a dict and then compared the dicts, but that doesn't account for fields changed in a record, and that's something I'd like to check. Besides that, a complementary insert/delete would be missed.
So is there like a file LMD I can check to see if anything at all in the database has changed?