PageRenderTime 72ms CodeModel.GetById 40ms app.highlight 16ms RepoModel.GetById 14ms app.codeStats 0ms

/tests/modeltests/signals/tests.py

https://code.google.com/p/mango-py/
Python | 176 lines | 138 code | 26 blank | 12 comment | 0 complexity | 9183d7774e5fecc2e91d505eac9c9c9a MD5 | raw file
  1from django.db.models import signals
  2from django.dispatch import receiver
  3from django.test import TestCase
  4
  5from models import Person, Car
  6
  7
  8# #8285: signals can be any callable
  9class PostDeleteHandler(object):
 10    def __init__(self, data):
 11        self.data = data
 12
 13    def __call__(self, signal, sender, instance, **kwargs):
 14        self.data.append(
 15            (instance, instance.id is None)
 16        )
 17
 18class MyReceiver(object):
 19    def __init__(self, param):
 20        self.param = param
 21        self._run = False
 22
 23    def __call__(self, signal, sender, **kwargs):
 24        self._run = True
 25        signal.disconnect(receiver=self, sender=sender)
 26
 27class SignalTests(TestCase):
 28    def test_basic(self):
 29        # Save up the number of connected signals so that we can check at the
 30        # end that all the signals we register get properly unregistered (#9989)
 31        pre_signals = (
 32            len(signals.pre_save.receivers),
 33            len(signals.post_save.receivers),
 34            len(signals.pre_delete.receivers),
 35            len(signals.post_delete.receivers),
 36        )
 37
 38        data = []
 39
 40        def pre_save_test(signal, sender, instance, **kwargs):
 41            data.append(
 42                (instance, kwargs.get("raw", False))
 43            )
 44        signals.pre_save.connect(pre_save_test)
 45
 46        def post_save_test(signal, sender, instance, **kwargs):
 47            data.append(
 48                (instance, kwargs.get("created"), kwargs.get("raw", False))
 49            )
 50        signals.post_save.connect(post_save_test)
 51
 52        def pre_delete_test(signal, sender, instance, **kwargs):
 53            data.append(
 54                (instance, instance.id is None)
 55            )
 56        signals.pre_delete.connect(pre_delete_test)
 57
 58        post_delete_test = PostDeleteHandler(data)
 59        signals.post_delete.connect(post_delete_test)
 60
 61        # throw a decorator syntax receiver into the mix
 62        @receiver(signals.pre_save)
 63        def pre_save_decorator_test(signal, sender, instance, **kwargs):
 64            data.append(instance)
 65
 66        @receiver(signals.pre_save, sender=Car)
 67        def pre_save_decorator_sender_test(signal, sender, instance, **kwargs):
 68            data.append(instance)
 69
 70        p1 = Person(first_name="John", last_name="Smith")
 71        self.assertEqual(data, [])
 72        p1.save()
 73        self.assertEqual(data, [
 74            (p1, False),
 75            p1,
 76            (p1, True, False),
 77        ])
 78        data[:] = []
 79
 80        p1.first_name = "Tom"
 81        p1.save()
 82        self.assertEqual(data, [
 83            (p1, False),
 84            p1,
 85            (p1, False, False),
 86        ])
 87        data[:] = []
 88
 89        # Car signal (sender defined)
 90        c1 = Car(make="Volkswagon", model="Passat")
 91        c1.save()
 92        self.assertEqual(data, [
 93            (c1, False),
 94            c1,
 95            c1,
 96            (c1, True, False),
 97        ])
 98        data[:] = []
 99
100        # Calling an internal method purely so that we can trigger a "raw" save.
101        p1.save_base(raw=True)
102        self.assertEqual(data, [
103            (p1, True),
104            p1,
105            (p1, False, True),
106        ])
107        data[:] = []
108
109        p1.delete()
110        self.assertEqual(data, [
111            (p1, False),
112            (p1, False),
113        ])
114        data[:] = []
115
116        p2 = Person(first_name="James", last_name="Jones")
117        p2.id = 99999
118        p2.save()
119        self.assertEqual(data, [
120            (p2, False),
121            p2,
122            (p2, True, False),
123        ])
124        data[:] = []
125
126        p2.id = 99998
127        p2.save()
128        self.assertEqual(data, [
129            (p2, False),
130            p2,
131            (p2, True, False),
132        ])
133        data[:] = []
134
135        p2.delete()
136        self.assertEqual(data, [
137            (p2, False),
138            (p2, False)
139        ])
140
141        self.assertQuerysetEqual(
142            Person.objects.all(), [
143                "James Jones",
144            ],
145            unicode
146        )
147
148        signals.post_delete.disconnect(post_delete_test)
149        signals.pre_delete.disconnect(pre_delete_test)
150        signals.post_save.disconnect(post_save_test)
151        signals.pre_save.disconnect(pre_save_test)
152        signals.pre_save.disconnect(pre_save_decorator_test)
153        signals.pre_save.disconnect(pre_save_decorator_sender_test, sender=Car)
154
155        # Check that all our signals got disconnected properly.
156        post_signals = (
157            len(signals.pre_save.receivers),
158            len(signals.post_save.receivers),
159            len(signals.pre_delete.receivers),
160            len(signals.post_delete.receivers),
161        )
162        self.assertEqual(pre_signals, post_signals)
163
164    def test_disconnect_in_dispatch(self):
165        """
166        Test that signals that disconnect when being called don't mess future
167        dispatching.
168        """
169        a, b = MyReceiver(1), MyReceiver(2)
170        signals.post_save.connect(sender=Person, receiver=a)
171        signals.post_save.connect(sender=Person, receiver=b)
172        p = Person.objects.create(first_name='John', last_name='Smith')
173
174        self.assertTrue(a._run)
175        self.assertTrue(b._run)
176        self.assertEqual(signals.post_save.receivers, [])