Ticket #12953: 12953_r12557.diff

File 12953_r12557.diff, 2.3 KB (added by Carl Meyer, 15 years ago)

test-case only

  • tests/regressiontests/delete_regress/models.py

    diff --git a/tests/regressiontests/delete_regress/models.py b/tests/regressiontests/delete_regress/models.py
    a b  
    33from django.db.models import sql, query
    44from django.test import TransactionTestCase
    55
     6from django.contrib.contenttypes import generic
     7from django.contrib.contenttypes.models import ContentType
     8
     9class Award(models.Model):
     10    name = models.CharField(max_length=25)
     11    object_id = models.PositiveIntegerField()
     12    content_type = models.ForeignKey(ContentType)
     13    content_object = generic.GenericForeignKey()
     14
     15class AwardNote(models.Model):
     16    award = models.ForeignKey(Award)
     17    note = models.CharField(max_length=100)
     18
     19class Person(models.Model):
     20    name = models.CharField(max_length=25)
     21    awards = generic.GenericRelation(Award)
     22
    623class Book(models.Model):
    724    pagecount = models.IntegerField()
    825
  • new file tests/regressiontests/delete_regress/tests.py

    diff --git a/tests/regressiontests/delete_regress/tests.py b/tests/regressiontests/delete_regress/tests.py
    new file mode 100644
    - +  
     1from django.test import TestCase
     2
     3from models import Award, AwardNote, Person
     4
     5class DeleteCascadeTests(TestCase):
     6    def test_generic_relation_cascade(self):
     7        """
     8        Test that Django cascades deletes through generic-related
     9        objects to their reverse relations.
     10       
     11        This might falsely succeed if the database cascades deletes
     12        itself immediately; the postgresql_psycopg2 backend does not
     13        give such a false success because ForeignKeys are created with
     14        DEFERRABLE INITIALLY DEFERRED, so its internal cascade is
     15        delayed until transaction commit.
     16
     17        """
     18        person = Person.objects.create(name='Nelson Mandela')
     19        award = Award.objects.create(name='Nobel', content_object=person)
     20        note = AwardNote.objects.create(note='a peace prize',
     21                                        award=award)
     22        self.assertEquals(AwardNote.objects.count(), 1)
     23        person.delete()
     24        self.assertEquals(Award.objects.count(), 0)
     25        # first two asserts are just sanity checks, this is the kicker:
     26        self.assertEquals(AwardNote.objects.count(), 0)
     27       
Back to Top