Ticket #16131: signal-order-test.txt

File signal-order-test.txt, 2.7 KB (added by brodie, 4 years ago)
Line 
1# HG changeset patch
2# User Brodie Rao <brodie@bitheap.org>
3# Date 1306813701 25200
4# Branch releases/1.2.X
5# Node ID 39ddf40b9554542e7fb205ae1002b3022a5a1175
6# Parent  b5f81663991987755dbe2bc2371a62571835335c
7signals: test post_delete signal firing order when deleting an instance and relations
8
9diff --git a/tests/regressiontests/fk_post_delete/__init__.py b/tests/regressiontests/fk_post_delete/__init__.py
10new file mode 100644
11--- /dev/null
12+++ b/tests/regressiontests/fk_post_delete/__init__.py
13@@ -0,0 +1,1 @@
14+
15diff --git a/tests/regressiontests/fk_post_delete/models.py b/tests/regressiontests/fk_post_delete/models.py
16new file mode 100644
17--- /dev/null
18+++ b/tests/regressiontests/fk_post_delete/models.py
19@@ -0,0 +1,7 @@
20+from django.db import models
21+
22+class Stick(models.Model):
23+    pass
24+
25+class Marshmallow(models.Model):
26+    stick = models.ForeignKey(Stick, related_name='stick')
27diff --git a/tests/regressiontests/fk_post_delete/tests.py b/tests/regressiontests/fk_post_delete/tests.py
28new file mode 100644
29--- /dev/null
30+++ b/tests/regressiontests/fk_post_delete/tests.py
31@@ -0,0 +1,55 @@
32+"""
33+Testing foreign key behavior in post_delete signal handlers.
34+"""
35+
36+from django.db import models
37+from django.test import TestCase
38+
39+from models import Stick, Marshmallow
40+
41+class PostDeleteTest(TestCase):
42+    def post_delete_signal_receiver(self, signal, sender, **kwargs):
43+        instance = kwargs['instance']
44+        message = {
45+            'pk': instance.pk,
46+            'stick_id': instance.stick_id,
47+            'extant_stick_pk': [stick.pk for stick in
48+                                Stick.objects.filter(pk=instance.stick_id)],
49+        }
50+        self.post_delete_messages.append(message)
51+
52+    def setUp(self):
53+        self.post_delete_messages = []
54+
55+        models.signals.post_delete.connect(
56+            self.post_delete_signal_receiver, Marshmallow
57+        )
58+
59+        self.stick = Stick()
60+        self.stick.save()
61+        self.marshmallow = Marshmallow(stick=self.stick)
62+        self.marshmallow.save()
63+
64+    def tearDown(self):
65+        models.signals.post_delete.disconnect(
66+            self.post_delete_signal_receiver, Marshmallow
67+        )
68+
69+class PostDeleteIDTest(PostDeleteTest):
70+    def test_post_delete_ids(self):
71+        expected_messages = [{
72+            'pk': self.marshmallow.pk,
73+            'stick_id': self.marshmallow.stick_id,
74+            'extant_stick_pk': [self.stick.pk],
75+        }]
76+
77+        self.stick.delete()
78+        self.assertEqual(self.post_delete_messages, expected_messages)
79+
80+
81+class PostDeleteFKTest(PostDeleteTest):
82+    def post_delete_signal_receiver(self, signal, sender, **kwargs):
83+        kwargs['instance'].stick
84+
85+    def test_post_delete_fk(self):
86+        self.stick.delete()
Back to Top