Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Cleaned up several test cases, to better isolate state.

  • Loading branch information...
commit f47af9df1d3e03512364e5fe115f025c0a0be670 1 parent 72b8f8d
authored
27  tests/regressiontests/fixtures_regress/tests.py
@@ -21,18 +21,15 @@
21 21
     ExternalDependency, Thingy)
22 22
 
23 23
 
24  
-pre_save_checks = []
25  
-def animal_pre_save_check(signal, sender, instance, **kwargs):
26  
-    "A signal that is used to check the type of data loaded from fixtures"
27  
-    pre_save_checks.append(
28  
-        (
29  
-            'Count = %s (%s)' % (instance.count, type(instance.count)),
30  
-            'Weight = %s (%s)' % (instance.weight, type(instance.weight)),
  24
+class TestFixtures(TestCase):
  25
+    def animal_pre_save_check(self, signal, sender, instance, **kwargs):
  26
+        self.pre_save_checks.append(
  27
+            (
  28
+                'Count = %s (%s)' % (instance.count, type(instance.count)),
  29
+                'Weight = %s (%s)' % (instance.weight, type(instance.weight)),
  30
+            )
31 31
         )
32  
-    )
33  
-
34 32
 
35  
-class TestFixtures(TestCase):
36 33
     def test_duplicate_pk(self):
37 34
         """
38 35
         This is a regression test for ticket #3790.
@@ -111,7 +108,6 @@ def test_absolute_path(self):
111 108
         )
112 109
         self.assertEqual(Absolute.load_count, 1)
113 110
 
114  
-
115 111
     def test_unknown_format(self):
116 112
         """
117 113
         Test for ticket #4371 -- Loading data of an unknown format should fail
@@ -246,9 +242,8 @@ def test_field_value_coerce(self):
246 242
         Test for tickets #8298, #9942 - Field values should be coerced into the
247 243
         correct type by the deserializer, not as part of the database write.
248 244
         """
249  
-        global pre_save_checks
250  
-        pre_save_checks = []
251  
-        signals.pre_save.connect(animal_pre_save_check)
  245
+        self.pre_save_checks = []
  246
+        signals.pre_save.connect(self.animal_pre_save_check)
252 247
         try:
253 248
             management.call_command(
254 249
                 'loaddata',
@@ -257,14 +252,14 @@ def test_field_value_coerce(self):
257 252
                 commit=False,
258 253
             )
259 254
             self.assertEqual(
260  
-                pre_save_checks,
  255
+                self.pre_save_checks,
261 256
                 [
262 257
                     ("Count = 42 (<%s 'int'>)" % ('class' if PY3 else 'type'),
263 258
                      "Weight = 1.2 (<%s 'float'>)" % ('class' if PY3 else 'type'))
264 259
                 ]
265 260
             )
266 261
         finally:
267  
-            signals.pre_save.disconnect(animal_pre_save_check)
  262
+            signals.pre_save.disconnect(self.animal_pre_save_check)
268 263
 
269 264
     def test_dumpdata_uses_default_manager(self):
270 265
         """
22  tests/regressiontests/settings_tests/tests.py
@@ -71,16 +71,18 @@ class SettingGetter(object):
71 71
     def __init__(self):
72 72
         self.test = getattr(settings, 'TEST', 'undefined')
73 73
 
74  
-testvalue = None
75 74
 
76  
-def signal_callback(sender, setting, value, **kwargs):
77  
-    if setting == 'TEST':
78  
-        global testvalue
79  
-        testvalue = value
  75
+class SettingsTests(TestCase):
  76
+    def setUp(self):
  77
+        self.testvalue = None
  78
+        signals.setting_changed.connect(self.signal_callback)
80 79
 
81  
-signals.setting_changed.connect(signal_callback)
  80
+    def tearDown(self):
  81
+        signals.setting_changed.disconnect(self.signal_callback)
82 82
 
83  
-class SettingsTests(TestCase):
  83
+    def signal_callback(self, sender, setting, value, **kwargs):
  84
+        if setting == 'TEST':
  85
+            self.testvalue = value
84 86
 
85 87
     def test_override(self):
86 88
         settings.TEST = 'test'
@@ -128,12 +130,12 @@ def test_class_decorator(self):
128 130
     def test_signal_callback_context_manager(self):
129 131
         self.assertRaises(AttributeError, getattr, settings, 'TEST')
130 132
         with self.settings(TEST='override'):
131  
-            self.assertEqual(testvalue, 'override')
132  
-        self.assertEqual(testvalue, None)
  133
+            self.assertEqual(self.testvalue, 'override')
  134
+        self.assertEqual(self.testvalue, None)
133 135
 
134 136
     @override_settings(TEST='override')
135 137
     def test_signal_callback_decorator(self):
136  
-        self.assertEqual(testvalue, 'override')
  138
+        self.assertEqual(self.testvalue, 'override')
137 139
 
138 140
     #
139 141
     # Regression tests for #10130: deleting settings.
70  tests/regressiontests/signals_regress/tests.py
@@ -6,31 +6,6 @@
6 6
 from .models import Author, Book
7 7
 
8 8
 
9  
-signal_output = []
10  
-
11  
-def pre_save_test(signal, sender, instance, **kwargs):
12  
-    signal_output.append('pre_save signal, %s' % instance)
13  
-    if kwargs.get('raw'):
14  
-        signal_output.append('Is raw')
15  
-
16  
-def post_save_test(signal, sender, instance, **kwargs):
17  
-    signal_output.append('post_save signal, %s' % instance)
18  
-    if 'created' in kwargs:
19  
-        if kwargs['created']:
20  
-            signal_output.append('Is created')
21  
-        else:
22  
-            signal_output.append('Is updated')
23  
-    if kwargs.get('raw'):
24  
-        signal_output.append('Is raw')
25  
-
26  
-def pre_delete_test(signal, sender, instance, **kwargs):
27  
-    signal_output.append('pre_save signal, %s' % instance)
28  
-    signal_output.append('instance.id is not None: %s' % (instance.id != None))
29  
-
30  
-def post_delete_test(signal, sender, instance, **kwargs):
31  
-    signal_output.append('post_delete signal, %s' % instance)
32  
-    signal_output.append('instance.id is not None: %s' % (instance.id != None))
33  
-
34 9
 class SignalsRegressTests(TestCase):
35 10
     """
36 11
     Testing signals before/after saving and deleting.
@@ -38,12 +13,35 @@ class SignalsRegressTests(TestCase):
38 13
 
39 14
     def get_signal_output(self, fn, *args, **kwargs):
40 15
         # Flush any existing signal output
41  
-        global signal_output
42  
-        signal_output = []
  16
+        self.signal_output = []
43 17
         fn(*args, **kwargs)
44  
-        return signal_output
  18
+        return self.signal_output
  19
+
  20
+    def pre_save_test(self, signal, sender, instance, **kwargs):
  21
+        self.signal_output.append('pre_save signal, %s' % instance)
  22
+        if kwargs.get('raw'):
  23
+            self.signal_output.append('Is raw')
  24
+
  25
+    def post_save_test(self, signal, sender, instance, **kwargs):
  26
+        self.signal_output.append('post_save signal, %s' % instance)
  27
+        if 'created' in kwargs:
  28
+            if kwargs['created']:
  29
+                self.signal_output.append('Is created')
  30
+            else:
  31
+                self.signal_output.append('Is updated')
  32
+        if kwargs.get('raw'):
  33
+            self.signal_output.append('Is raw')
  34
+
  35
+    def pre_delete_test(self, signal, sender, instance, **kwargs):
  36
+        self.signal_output.append('pre_save signal, %s' % instance)
  37
+        self.signal_output.append('instance.id is not None: %s' % (instance.id != None))
  38
+
  39
+    def post_delete_test(self, signal, sender, instance, **kwargs):
  40
+        self.signal_output.append('post_delete signal, %s' % instance)
  41
+        self.signal_output.append('instance.id is not None: %s' % (instance.id != None))
45 42
 
46 43
     def setUp(self):
  44
+        self.signal_output = []
47 45
         # Save up the number of connected signals so that we can check at the end
48 46
         # that all the signals we register get properly unregistered (#9989)
49 47
         self.pre_signals = (len(models.signals.pre_save.receivers),
@@ -51,16 +49,16 @@ def setUp(self):
51 49
                        len(models.signals.pre_delete.receivers),
52 50
                        len(models.signals.post_delete.receivers))
53 51
 
54  
-        models.signals.pre_save.connect(pre_save_test)
55  
-        models.signals.post_save.connect(post_save_test)
56  
-        models.signals.pre_delete.connect(pre_delete_test)
57  
-        models.signals.post_delete.connect(post_delete_test)
  52
+        models.signals.pre_save.connect(self.pre_save_test)
  53
+        models.signals.post_save.connect(self.post_save_test)
  54
+        models.signals.pre_delete.connect(self.pre_delete_test)
  55
+        models.signals.post_delete.connect(self.post_delete_test)
58 56
 
59 57
     def tearDown(self):
60  
-        models.signals.post_delete.disconnect(post_delete_test)
61  
-        models.signals.pre_delete.disconnect(pre_delete_test)
62  
-        models.signals.post_save.disconnect(post_save_test)
63  
-        models.signals.pre_save.disconnect(pre_save_test)
  58
+        models.signals.post_delete.disconnect(self.post_delete_test)
  59
+        models.signals.pre_delete.disconnect(self.pre_delete_test)
  60
+        models.signals.post_save.disconnect(self.post_save_test)
  61
+        models.signals.pre_save.disconnect(self.pre_save_test)
64 62
 
65 63
         # Check that all our signals got disconnected properly.
66 64
         post_signals = (len(models.signals.pre_save.receivers),

0 notes on commit f47af9d

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