Code

Ticket #4879: django_unit_test_model_signals.diff

File django_unit_test_model_signals.diff, 2.6 KB (added by George Vilches <gav@…>, 7 years ago)

Unit test for all model signals, including testing the post_save created keyword

Line 
1--- django_live/tests/modeltests/signals/models.py
2+++ django_live/tests/modeltests/signals/models.py
3@@ -0,0 +1,85 @@
4+"""
5+Testing signals before/after saving and deleting.
6+"""
7+
8+from django.db import models
9+from django.dispatch import dispatcher
10+
11+class Person(models.Model):
12+    first_name = models.CharField(max_length=20)
13+    last_name = models.CharField(max_length=20)
14+
15+    def __unicode__(self):
16+        return u"%s %s" % (self.first_name, self.last_name)
17+
18+
19+def pre_save_test(sender, instance, **kwargs):
20+    print 'pre_save signal,', instance
21+
22+def post_save_test(sender, instance, **kwargs):
23+    print 'post_save signal,', instance
24+    if 'created' in kwargs:
25+        if kwargs['created']:
26+            print 'Is created'
27+        else:
28+            print 'Is updated'
29+
30+def pre_delete_test(sender, instance, **kwargs):
31+    print 'pre_delete signal,', instance
32+    print 'instance.id is not None: %s' % (instance.id != None) 
33+
34+def post_delete_test(sender, instance, **kwargs):
35+    print 'post_delete signal,', instance
36+    print 'instance.id is None: %s' % (instance.id == None) 
37+
38+__test__ = {'API_TESTS':"""
39+
40+>>> dispatcher.connect(pre_save_test, signal=models.signals.pre_save)
41+>>> dispatcher.connect(post_save_test, signal=models.signals.post_save)
42+>>> dispatcher.connect(pre_delete_test, signal=models.signals.pre_delete)
43+>>> dispatcher.connect(post_delete_test, signal=models.signals.post_delete)
44+>>> p1 = Person(first_name='John', last_name='Smith')
45+>>> p1.save()
46+pre_save signal, John Smith
47+post_save signal, John Smith
48+Is created
49+
50+>>> p1.first_name = 'Tom'
51+>>> p1.save()
52+pre_save signal, Tom Smith
53+post_save signal, Tom Smith
54+Is updated
55+
56+>>> p1.delete()
57+pre_delete signal, Tom Smith
58+instance.id is not None: True
59+post_delete signal, Tom Smith
60+instance.id is None: True
61+
62+>>> p2 = Person(first_name='James', last_name='Jones')
63+>>> p2.id = 99999
64+>>> p2.save()
65+pre_save signal, James Jones
66+post_save signal, James Jones
67+Is created
68+
69+>>> p2.id = 99998
70+>>> p2.save()
71+pre_save signal, James Jones
72+post_save signal, James Jones
73+Is created
74+
75+>>> p2.delete()
76+pre_delete signal, James Jones
77+instance.id is not None: True
78+post_delete signal, James Jones
79+instance.id is None: True
80+
81+>>> Person.objects.all()
82+[<Person: James Jones>]
83+
84+>>> dispatcher.disconnect(post_delete_test, signal=models.signals.post_delete)
85+>>> dispatcher.disconnect(pre_delete_test, signal=models.signals.pre_delete)
86+>>> dispatcher.disconnect(post_save_test, signal=models.signals.post_save)
87+>>> dispatcher.disconnect(pre_save_test, signal=models.signals.pre_save)
88+"""}