Ticket #12953: 12953_r12557.diff
File 12953_r12557.diff, 2.3 KB (added by , 15 years ago) |
---|
-
tests/regressiontests/delete_regress/models.py
diff --git a/tests/regressiontests/delete_regress/models.py b/tests/regressiontests/delete_regress/models.py
a b 3 3 from django.db.models import sql, query 4 4 from django.test import TransactionTestCase 5 5 6 from django.contrib.contenttypes import generic 7 from django.contrib.contenttypes.models import ContentType 8 9 class 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 15 class AwardNote(models.Model): 16 award = models.ForeignKey(Award) 17 note = models.CharField(max_length=100) 18 19 class Person(models.Model): 20 name = models.CharField(max_length=25) 21 awards = generic.GenericRelation(Award) 22 6 23 class Book(models.Model): 7 24 pagecount = models.IntegerField() 8 25 -
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
- + 1 from django.test import TestCase 2 3 from models import Award, AwardNote, Person 4 5 class 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