Code

Ticket #17895: 17895.diff

File 17895.diff, 3.4 KB (added by aaugustin, 2 years ago)
Line 
1Index: docs/ref/signals.txt
2===================================================================
3--- docs/ref/signals.txt        (revision 17703)
4+++ docs/ref/signals.txt        (working copy)
5@@ -477,7 +477,7 @@
6 .. data:: django.test.signals.setting_changed
7    :module:
8 
9-Sent when some :ref:`settings are overridden <overriding-settings>` with the
10+Sent when the value of a setting is changed with the
11 :meth:`django.test.TestCase.setting` context manager or the
12 :func:`django.test.utils.override_settings` decorator/context manager.
13 
14@@ -492,6 +492,10 @@
15 ``value``
16     The new setting value.
17 
18+``state``
19+    ``"pre"`` when the setting is set to a new value, ``"post"`` when it's
20+    reset to its original value.
21+
22 template_rendered
23 -----------------
24 
25Index: django/test/utils.py
26===================================================================
27--- django/test/utils.py        (revision 17703)
28+++ django/test/utils.py        (working copy)
29@@ -167,15 +167,6 @@
30     delattr(loader, RESTORE_LOADERS_ATTR)
31 
32 
33-class OverrideSettingsHolder(UserSettingsHolder):
34-    """
35-    A custom setting holder that sends a signal upon change.
36-    """
37-    def __setattr__(self, name, value):
38-        UserSettingsHolder.__setattr__(self, name, value)
39-        setting_changed.send(sender=self.__class__, setting=name, value=value)
40-
41-
42 class override_settings(object):
43     """
44     Acts as either a decorator, or a context manager. If it's a decorator it
45@@ -215,10 +206,18 @@
46         return inner
47 
48     def enable(self):
49-        override = OverrideSettingsHolder(settings._wrapped)
50+        override = UserSettingsHolder(settings._wrapped)
51         for key, new_value in self.options.items():
52             setattr(override, key, new_value)
53         settings._wrapped = override
54+        for key, new_value in self.options.items():
55+            setting_changed.send(sender=settings._wrapped.__class__,
56+                                 setting=key, value=new_value, state='pre')
57 
58     def disable(self):
59         settings._wrapped = self.wrapped
60+        for key in self.options:
61+            new_value = getattr(settings, key, None)
62+            setting_changed.send(sender=settings._wrapped.__class__,
63+                                 setting=key, value=new_value, state='post')
64+
65Index: tests/regressiontests/settings_tests/tests.py
66===================================================================
67--- tests/regressiontests/settings_tests/tests.py       (revision 17703)
68+++ tests/regressiontests/settings_tests/tests.py       (working copy)
69@@ -76,10 +76,10 @@
70 
71 testvalue = None
72 
73-def signal_callback(sender, setting, value, **kwargs):
74+def signal_callback(sender, setting, value, state, **kwargs):
75     if setting == 'TEST':
76         global testvalue
77-        testvalue = value
78+        testvalue = (value, state)
79 
80 signals.setting_changed.connect(signal_callback)
81 
82@@ -131,11 +131,12 @@
83     def test_signal_callback_context_manager(self):
84         self.assertRaises(AttributeError, getattr, settings, 'TEST')
85         with self.settings(TEST='override'):
86-            self.assertEqual(testvalue, 'override')
87+            self.assertEqual(testvalue, ('override', 'pre'))
88+        self.assertEqual(testvalue, (None, 'post'))
89 
90     @override_settings(TEST='override')
91     def test_signal_callback_decorator(self):
92-        self.assertEqual(testvalue, 'override')
93+        self.assertEqual(testvalue, ('override', 'pre'))
94 
95     #
96     # Regression tests for #10130: deleting settings.