Skip to content

Commit

Permalink
Fixed #14043 -- Made sure nullable o2o delete works as expected
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
akaariai authored and andrewgodwin committed Aug 21, 2013
1 parent 6337816 commit b773ef8
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions tests/one_to_one_regress/tests.py
Expand Up @@ -25,7 +25,7 @@ def test_reverse_relationship_cache_cascade(self):
# The bug in #9023: if you access the one-to-one relation *before* # The bug in #9023: if you access the one-to-one relation *before*
# setting to None and deleting, the cascade happens anyway. # setting to None and deleting, the cascade happens anyway.
self.p1.undergroundbar self.p1.undergroundbar
bar.place.name='foo' bar.place.name = 'foo'
bar.place = None bar.place = None
bar.save() bar.save()
self.p1.delete() self.p1.delete()
Expand All @@ -40,12 +40,12 @@ def test_create_models_m2m(self):
Check that we create models via the m2m relation if the remote model Check that we create models via the m2m relation if the remote model
has a OneToOneField. has a OneToOneField.
""" """
f = Favorites(name = 'Fred') f = Favorites(name='Fred')
f.save() f.save()
f.restaurants = [self.r1] f.restaurants = [self.r1]
self.assertQuerysetEqual( self.assertQuerysetEqual(
f.restaurants.all(), f.restaurants.all(),
['<Restaurant: Demon Dogs the restaurant>'] ['<Restaurant: Demon Dogs the restaurant>']
) )


def test_reverse_object_cache(self): def test_reverse_object_cache(self):
Expand Down Expand Up @@ -114,23 +114,23 @@ def test_filter_one_to_one_relations(self):
misbehaving. We test both (primary_key=True & False) cases here to misbehaving. We test both (primary_key=True & False) cases here to
prevent any reappearance of the problem. prevent any reappearance of the problem.
""" """
t = Target.objects.create() Target.objects.create()


self.assertQuerysetEqual( self.assertQuerysetEqual(
Target.objects.filter(pointer=None), Target.objects.filter(pointer=None),
['<Target: Target object>'] ['<Target: Target object>']
) )
self.assertQuerysetEqual( self.assertQuerysetEqual(
Target.objects.exclude(pointer=None), Target.objects.exclude(pointer=None),
[] []
) )
self.assertQuerysetEqual( self.assertQuerysetEqual(
Target.objects.filter(pointer2=None), Target.objects.filter(pointer2=None),
['<Target: Target object>'] ['<Target: Target object>']
) )
self.assertQuerysetEqual( self.assertQuerysetEqual(
Target.objects.exclude(pointer2=None), Target.objects.exclude(pointer2=None),
[] []
) )


def test_reverse_object_does_not_exist_cache(self): def test_reverse_object_does_not_exist_cache(self):
Expand Down Expand Up @@ -235,3 +235,11 @@ def test_set_reverse_on_unsaved_object(self):
b = UndergroundBar.objects.create() b = UndergroundBar.objects.create()
with self.assertNumQueries(0), self.assertRaises(ValueError): with self.assertNumQueries(0), self.assertRaises(ValueError):
p.undergroundbar = b 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)

0 comments on commit b773ef8

Please sign in to comment.