Added tests for using attnames to assign OneToOne and Foreign Key fields.

This commit is contained in:
Jon Dufresne 2019-07-26 10:18:24 +02:00 committed by Mariusz Felisiak
parent 4b4e68a7a6
commit 619c9a4f49
2 changed files with 28 additions and 0 deletions

View File

@ -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))

View File

@ -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) """