diff --git a/tests/many_to_one/tests.py b/tests/many_to_one/tests.py index 7538f238f2..a5215e58d7 100644 --- a/tests/many_to_one/tests.py +++ b/tests/many_to_one/tests.py @@ -152,6 +152,22 @@ class ManyToOneTests(TestCase): self.assertFalse(hasattr(self.r2.article_set, 'remove')) self.assertFalse(hasattr(self.r2.article_set, 'clear')) + def test_assign_fk_id_value(self): + parent = Parent.objects.create(name='jeff') + child1 = Child.objects.create(name='frank', parent=parent) + child2 = Child.objects.create(name='randy', parent=parent) + parent.bestchild = child1 + parent.save() + parent.bestchild_id = child2.pk + parent.save() + self.assertEqual(parent.bestchild_id, child2.pk) + self.assertFalse(Parent.bestchild.is_cached(parent)) + self.assertEqual(parent.bestchild, child2) + self.assertTrue(Parent.bestchild.is_cached(parent)) + # Reassigning the same value doesn't clear cached instance. + parent.bestchild_id = child2.pk + self.assertTrue(Parent.bestchild.is_cached(parent)) + def test_selects(self): self.r.article_set.create(headline="John's second story", pub_date=datetime.date(2005, 7, 29)) self.r2.article_set.create(headline="Paul's story", pub_date=datetime.date(2006, 1, 17)) diff --git a/tests/one_to_one/tests.py b/tests/one_to_one/tests.py index 51e358c7f9..78b3ff1135 100644 --- a/tests/one_to_one/tests.py +++ b/tests/one_to_one/tests.py @@ -205,6 +205,18 @@ class OneToOneTests(TestCase): # reverse cache has a value of None. p.undergroundbar = None + def test_assign_o2o_id_value(self): + b = UndergroundBar.objects.create(place=self.p1) + b.place_id = self.p2.pk + b.save() + self.assertEqual(b.place_id, self.p2.pk) + self.assertFalse(UndergroundBar.place.is_cached(b)) + self.assertEqual(b.place, self.p2) + self.assertTrue(UndergroundBar.place.is_cached(b)) + # Reassigning the same value doesn't clear a cached instance. + b.place_id = self.p2.pk + self.assertTrue(UndergroundBar.place.is_cached(b)) + def test_related_object_cache(self): """ Regression test for #6886 (the related-object cache) """