From b53ed351b34918e337cbf26773998dafc6f82f4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anssi=20K=C3=A4=C3=A4ri=C3=A4inen?= Date: Tue, 20 Aug 2013 09:47:43 +0300 Subject: [PATCH] Fixed #14043 -- Made sure nullable o2o delete works as expected There was an old complaint about nullable one-to-one field cascading even when the o2o field was saved to None value before the deletion. Added an test to verify this doesn't happen. Also some PEP 8 cleanup. --- tests/one_to_one_regress/tests.py | 34 +++++++++++++++++++------------ 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/tests/one_to_one_regress/tests.py b/tests/one_to_one_regress/tests.py index 0e20f19acb..55836d47e2 100644 --- a/tests/one_to_one_regress/tests.py +++ b/tests/one_to_one_regress/tests.py @@ -25,7 +25,7 @@ class OneToOneRegressionTests(TestCase): # The bug in #9023: if you access the one-to-one relation *before* # setting to None and deleting, the cascade happens anyway. self.p1.undergroundbar - bar.place.name='foo' + bar.place.name = 'foo' bar.place = None bar.save() self.p1.delete() @@ -40,12 +40,12 @@ class OneToOneRegressionTests(TestCase): Check that we create models via the m2m relation if the remote model has a OneToOneField. """ - f = Favorites(name = 'Fred') + f = Favorites(name='Fred') f.save() f.restaurants = [self.r1] self.assertQuerysetEqual( - f.restaurants.all(), - [''] + f.restaurants.all(), + [''] ) def test_reverse_object_cache(self): @@ -114,23 +114,23 @@ class OneToOneRegressionTests(TestCase): misbehaving. We test both (primary_key=True & False) cases here to prevent any reappearance of the problem. """ - t = Target.objects.create() + Target.objects.create() self.assertQuerysetEqual( - Target.objects.filter(pointer=None), - [''] + Target.objects.filter(pointer=None), + [''] ) self.assertQuerysetEqual( - Target.objects.exclude(pointer=None), - [] + Target.objects.exclude(pointer=None), + [] ) self.assertQuerysetEqual( - Target.objects.filter(pointer2=None), - [''] + Target.objects.filter(pointer2=None), + [''] ) self.assertQuerysetEqual( - Target.objects.exclude(pointer2=None), - [] + Target.objects.exclude(pointer2=None), + [] ) def test_reverse_object_does_not_exist_cache(self): @@ -235,3 +235,11 @@ class OneToOneRegressionTests(TestCase): b = UndergroundBar.objects.create() with self.assertNumQueries(0), self.assertRaises(ValueError): p.undergroundbar = b + + def test_nullable_o2o_delete(self): + u = UndergroundBar.objects.create(place=self.p1) + u.place_id = None + u.save() + self.p1.delete() + self.assertTrue(UndergroundBar.objects.filter(pk=u.pk).exists()) + self.assertIsNone(UndergroundBar.objects.get(pk=u.pk).place)