Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Migrated signals_regress doctests. Thanks to Stephan Jaekel.

Backport of r13896 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@13917 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 554be48d01d9aea9af9d27e7296488766f04f8c1 1 parent 025ee2c
Russell Keith-Magee authored September 27, 2010
75  tests/regressiontests/signals_regress/models.py
... ...
@@ -1,7 +1,3 @@
1  
-"""
2  
-Testing signals before/after saving and deleting.
3  
-"""
4  
-
5 1
 from django.db import models
6 2
 
7 3
 class Author(models.Model):
@@ -16,74 +12,3 @@ class Book(models.Model):
16 12
 
17 13
     def __unicode__(self):
18 14
         return self.name
19  
-
20  
-def pre_save_test(signal, sender, instance, **kwargs):
21  
-    print 'pre_save signal,', instance
22  
-    if kwargs.get('raw'):
23  
-        print 'Is raw'
24  
-
25  
-def post_save_test(signal, sender, instance, **kwargs):
26  
-    print 'post_save signal,', instance
27  
-    if 'created' in kwargs:
28  
-        if kwargs['created']:
29  
-            print 'Is created'
30  
-        else:
31  
-            print 'Is updated'
32  
-    if kwargs.get('raw'):
33  
-        print 'Is raw'
34  
-
35  
-def pre_delete_test(signal, sender, instance, **kwargs):
36  
-    print 'pre_delete signal,', instance
37  
-    print 'instance.id is not None: %s' % (instance.id != None)
38  
-
39  
-def post_delete_test(signal, sender, instance, **kwargs):
40  
-    print 'post_delete signal,', instance
41  
-    print 'instance.id is not None: %s' % (instance.id != None)
42  
-
43  
-__test__ = {'API_TESTS':"""
44  
-
45  
-# Save up the number of connected signals so that we can check at the end
46  
-# that all the signals we register get properly unregistered (#9989)
47  
->>> pre_signals = (len(models.signals.pre_save.receivers),
48  
-...                len(models.signals.post_save.receivers),
49  
-...                len(models.signals.pre_delete.receivers),
50  
-...                len(models.signals.post_delete.receivers))
51  
-
52  
->>> models.signals.pre_save.connect(pre_save_test)
53  
->>> models.signals.post_save.connect(post_save_test)
54  
->>> models.signals.pre_delete.connect(pre_delete_test)
55  
->>> models.signals.post_delete.connect(post_delete_test)
56  
-
57  
->>> a1 = Author(name='Neal Stephenson')
58  
->>> a1.save()
59  
-pre_save signal, Neal Stephenson
60  
-post_save signal, Neal Stephenson
61  
-Is created
62  
-
63  
->>> b1 = Book(name='Snow Crash')
64  
->>> b1.save()
65  
-pre_save signal, Snow Crash
66  
-post_save signal, Snow Crash
67  
-Is created
68  
-
69  
-# Assigning to m2m shouldn't generate an m2m signal
70  
->>> b1.authors = [a1]
71  
-
72  
-# Removing an author from an m2m shouldn't generate an m2m signal
73  
->>> b1.authors = []
74  
-
75  
->>> models.signals.post_delete.disconnect(post_delete_test)
76  
->>> models.signals.pre_delete.disconnect(pre_delete_test)
77  
->>> models.signals.post_save.disconnect(post_save_test)
78  
->>> models.signals.pre_save.disconnect(pre_save_test)
79  
-
80  
-# Check that all our signals got disconnected properly.
81  
->>> post_signals = (len(models.signals.pre_save.receivers),
82  
-...                 len(models.signals.post_save.receivers),
83  
-...                 len(models.signals.pre_delete.receivers),
84  
-...                 len(models.signals.post_delete.receivers))
85  
-
86  
->>> pre_signals == post_signals
87  
-True
88  
-
89  
-"""}
96  tests/regressiontests/signals_regress/tests.py
... ...
@@ -0,0 +1,96 @@
  1
+import sys
  2
+from StringIO import StringIO
  3
+from django.test import TestCase
  4
+
  5
+from django.db import models
  6
+from regressiontests.signals_regress.models import Author, Book
  7
+
  8
+signal_output = []
  9
+
  10
+def pre_save_test(signal, sender, instance, **kwargs):
  11
+    signal_output.append('pre_save signal, %s' % instance)
  12
+    if kwargs.get('raw'):
  13
+        signal_output.append('Is raw')
  14
+
  15
+def post_save_test(signal, sender, instance, **kwargs):
  16
+    signal_output.append('post_save signal, %s' % instance)
  17
+    if 'created' in kwargs:
  18
+        if kwargs['created']:
  19
+            signal_output.append('Is created')
  20
+        else:
  21
+            signal_output.append('Is updated')
  22
+    if kwargs.get('raw'):
  23
+        signal_output.append('Is raw')
  24
+
  25
+def pre_delete_test(signal, sender, instance, **kwargs):
  26
+    signal_output.append('pre_save signal, %s' % instance)
  27
+    signal_output.append('instance.id is not None: %s' % (instance.id != None))
  28
+
  29
+def post_delete_test(signal, sender, instance, **kwargs):
  30
+    signal_output.append('post_delete signal, %s' % instance)
  31
+    signal_output.append('instance.id is not None: %s' % (instance.id != None))
  32
+
  33
+class SignalsRegressTests(TestCase):
  34
+    """
  35
+    Testing signals before/after saving and deleting.
  36
+    """
  37
+
  38
+    def get_signal_output(self, fn, *args, **kwargs):
  39
+        # Flush any existing signal output
  40
+        global signal_output
  41
+        signal_output = []
  42
+        fn(*args, **kwargs)
  43
+        return signal_output
  44
+
  45
+    def setUp(self):
  46
+        # Save up the number of connected signals so that we can check at the end
  47
+        # that all the signals we register get properly unregistered (#9989)
  48
+        self.pre_signals = (len(models.signals.pre_save.receivers),
  49
+                       len(models.signals.post_save.receivers),
  50
+                       len(models.signals.pre_delete.receivers),
  51
+                       len(models.signals.post_delete.receivers))
  52
+
  53
+        models.signals.pre_save.connect(pre_save_test)
  54
+        models.signals.post_save.connect(post_save_test)
  55
+        models.signals.pre_delete.connect(pre_delete_test)
  56
+        models.signals.post_delete.connect(post_delete_test)
  57
+
  58
+    def tearDown(self):
  59
+        models.signals.post_delete.disconnect(post_delete_test)
  60
+        models.signals.pre_delete.disconnect(pre_delete_test)
  61
+        models.signals.post_save.disconnect(post_save_test)
  62
+        models.signals.pre_save.disconnect(pre_save_test)
  63
+
  64
+        # Check that all our signals got disconnected properly.
  65
+        post_signals = (len(models.signals.pre_save.receivers),
  66
+                        len(models.signals.post_save.receivers),
  67
+                        len(models.signals.pre_delete.receivers),
  68
+                        len(models.signals.post_delete.receivers))
  69
+
  70
+        self.assertEquals(self.pre_signals, post_signals)
  71
+
  72
+    def test_model_signals(self):
  73
+        """ Model saves should throw some signals. """
  74
+        a1 = Author(name='Neal Stephenson')
  75
+        self.assertEquals(self.get_signal_output(a1.save), [
  76
+            "pre_save signal, Neal Stephenson",
  77
+            "post_save signal, Neal Stephenson",
  78
+            "Is created"
  79
+        ])
  80
+
  81
+        b1 = Book(name='Snow Crash')
  82
+        self.assertEquals(self.get_signal_output(b1.save), [
  83
+            "pre_save signal, Snow Crash",
  84
+            "post_save signal, Snow Crash",
  85
+            "Is created"
  86
+        ])
  87
+
  88
+    def test_m2m_signals(self):
  89
+        """ Assigning and removing to/from m2m shouldn't generate an m2m signal """
  90
+
  91
+        b1 = Book(name='Snow Crash')
  92
+        self.get_signal_output(b1.save)
  93
+        a1 = Author(name='Neal Stephenson')
  94
+        self.get_signal_output(a1.save)
  95
+        self.assertEquals(self.get_signal_output(setattr, b1, 'authors', [a1]), [])
  96
+        self.assertEquals(self.get_signal_output(setattr, b1, 'authors', []), [])

0 notes on commit 554be48

Please sign in to comment.
Something went wrong with that request. Please try again.