def test_router(self): # Ham should go in the default DB: self.assertEqual(dj_db.router.db_for_read(HamModel), "default") # Reading one shouldn't cause it to pin: self.assertEqual(pindb.is_pinned("default"), False) # The delegate router has no opinion on Hams, so they should write to # the default DB: self.assertEqual(dj_db.router.db_for_write(HamModel), "default") # The above should cause it to pin (even though default has no slaves # in this case): self.assertEqual(pindb.is_pinned("default"), True) # Eggs are stored in a replicated DB set, so we should read from a replica: self.assertTrue(dj_db.router.db_for_read(EggModel) in ["egg-0", "egg-1"]) # Reading shouldn't pin: self.assertEqual(pindb.is_pinned("egg"), False) # Writes should go to master: self.assertEqual(dj_db.router.db_for_write(EggModel), "egg") # And should cause a pin: self.assertEqual(pindb.is_pinned("egg"), True) pindb.unpin_all() # Making a Ham should pin to the master of the default set... ham1 = HamModel.objects.create() self.assertEqual(pindb.is_pinned("default"), True) # ...but not the egg set: self.assertEqual(pindb.is_pinned("egg"), False) # Making an Egg should pin the egg set as well: egg1 = EggModel.objects.create() self.assertEqual(pindb.is_pinned("egg"), True) pindb.unpin_all() ham1a = HamModel.objects.get(pk=ham1.pk) egg1a = EggModel.objects.get(pk=egg1.pk) # no longer bewm
def _post_teardown(self): """Delete the databases after the superclass' method has closed the connections. We must do the DB deletion in post-teardown, because that's when the superclass closes its connection, which inadvertantly re-creates the sqlite file if we had previously tried to delete it (like in a plain old tearDown method). """ super(PinDbTestCase, self)._post_teardown() pindb.unpin_all() self.teardown_databases(self.old_config)
def test_misdirected_save(self): """Test that saves route to the right DB after a fetch. At some point in production, we had an error that looked like fetching a model instance (from a slave) and then saving it failed, because it was trying to to write to the slave. It sure doesn't seem to to that [anymore]. """ # Set up the pre-existing model instance: egg = EggModel.objects.create() egg_id = egg.id pindb.unpin_all() egg = EggModel.objects.get(id=egg_id) self.assertEqual(dj_db.router.db_for_write(EggModel), "egg")
def test_pinning(self): # pinning is reflected in is_pinned for master in settings.MASTER_DATABASES: self.assertFalse(pindb.is_pinned(master)) pindb.pin(master) self.assertTrue(pindb.is_pinned(master)) # unpin_all resets the state. pindb.unpin_all() for master in settings.MASTER_DATABASES: self.assertFalse(pindb.is_pinned(master)) pindb.pin("default") # pinning state is available as a set. pinned1 = pindb.get_pinned() self.assertTrue('default' in pinned1) # unpinning doesn't affect the previous set. pindb.unpin_all() pinned2 = pindb.get_pinned() self.assertTrue('default' in pinned1) self.assertFalse('default' in pinned2) # we can keep track of new pins vs. carried-over pins # i.e. pins that stick for the replication lag period. self.assertEqual(0, len(pindb.get_newly_pinned())) pindb.pin("default") self.assertEqual(1, len(pindb.get_newly_pinned())) pindb.unpin_all() self.assertEqual(0, len(pindb.get_newly_pinned())) pindb.pin("default", count_as_new=False) self.assertEqual(0, len(pindb.get_newly_pinned())) # it counts as pinned even if we don't count it as new. self.assertEqual(1, len(pindb.get_pinned()))