def test_readonly_three_replicas(self): set_thread_readonly_db() replica0 = 'my-replica-db0' replica1 = 'my-replica-db1' replica2 = 'my-replica-db2' pool = { replica0: 1, replica1: 1, replica2: 1, } replicas = pool.keys() with override_settings(DATABASE_POOL=pool): router = DBRouter() out0 = router.db_for_read(Submission) self.assertIn(out0, replicas) out1 = router.db_for_read(Submission) self.assertIn(out1, replicas) self.assertNotEqual(out0, out1) out2 = router.db_for_read(Submission) self.assertIn(out2, replicas) self.assertNotEqual(out1, out2) out3 = router.db_for_read(Submission) # Round-robin should have come around by now self.assertEqual(out3, out0) self.assertFalse(was_db_written())
def test_writing_sets_db_written_flag(self): router = DBRouter() router.db_for_read(Submission) self.assertFalse(was_db_written()) router.db_for_write(Submission) self.assertTrue(was_db_written()) # A read won't reset the flag router.db_for_read(Submission) self.assertTrue(was_db_written())
def test_readonly_one_replica(self): set_thread_readonly_db() replica = 'my-only-replica-db' pool = { replica: 1 } with override_settings(DATABASE_POOL=pool): router = DBRouter() out = router.db_for_read(Submission) self.assertEqual(replica, out) out = router.db_for_read(Submission) self.assertEqual(replica, out) self.assertFalse(was_db_written())
def test_readwrite(self): self.assertFalse(was_db_written()) set_thread_readwrite_db() master = 'my_master_db' with override_settings(MASTER_DATABASE=master): router = DBRouter() out = router.db_for_read(Submission) self.assertEqual(master, out) out = router.db_for_write(Submission) self.assertEqual(master, out) self.assertTrue(was_db_written())