PageRenderTime 30ms CodeModel.GetById 17ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/signals_regress/tests.py

https://code.google.com/p/mango-py/
Python | 96 lines | 72 code | 16 blank | 8 comment | 5 complexity | 8f66b7e3487875d96827d5164204718a MD5 | raw file
 1import sys
 2from StringIO import StringIO
 3from django.test import TestCase
 4
 5from django.db import models
 6from regressiontests.signals_regress.models import Author, Book
 7
 8signal_output = []
 9
10def 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
15def 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
25def 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
29def 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
33class 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.assertEqual(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.assertEqual(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.assertEqual(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.assertEqual(self.get_signal_output(setattr, b1, 'authors', [a1]), [])
96        self.assertEqual(self.get_signal_output(setattr, b1, 'authors', []), [])