Code

Ticket #17895: 17895-more-docs-improved-api.diff

File 17895-more-docs-improved-api.diff, 3.8 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,21 +477,29 @@
6 .. data:: django.test.signals.setting_changed
7    :module:
8 
9-Sent when some :ref:`settings are overridden <overriding-settings>` with the
10+This signal is sent when the value of a setting is changed through the
11 :meth:`django.test.TestCase.setting` context manager or the
12 :func:`django.test.utils.override_settings` decorator/context manager.
13 
14+It's actually sent twice: when the new value is applied (``"setup"``) and when
15+the original value is restored (``"teardown"``).
16+
17 Arguments sent with this signal:
18 
19 ``sender``
20     The settings handler.
21 
22 ``setting``
23-    Same as sender
24+    The name of the setting.
25 
26 ``value``
27-    The new setting value.
28+    The value of the setting after the change. For settings that initially
29+    don't exist, in the teardown phase, ``value`` is ``None``.
30 
31+``phase``
32+    ``"setup"`` when the setting is set to its new value, ``"teardown"`` when
33+    it's reset to its original value.
34+
35 template_rendered
36 -----------------
37 
38Index: tests/regressiontests/settings_tests/tests.py
39===================================================================
40--- tests/regressiontests/settings_tests/tests.py       (revision 17703)
41+++ tests/regressiontests/settings_tests/tests.py       (working copy)
42@@ -76,10 +76,10 @@
43 
44 testvalue = None
45 
46-def signal_callback(sender, setting, value, **kwargs):
47+def signal_callback(sender, setting, value, state, **kwargs):
48     if setting == 'TEST':
49         global testvalue
50-        testvalue = value
51+        testvalue = (value, state)
52 
53 signals.setting_changed.connect(signal_callback)
54 
55@@ -131,11 +131,12 @@
56     def test_signal_callback_context_manager(self):
57         self.assertRaises(AttributeError, getattr, settings, 'TEST')
58         with self.settings(TEST='override'):
59-            self.assertEqual(testvalue, 'override')
60+            self.assertEqual(testvalue, ('override', 'setup'))
61+        self.assertEqual(testvalue, (None, 'teardown'))
62 
63     @override_settings(TEST='override')
64     def test_signal_callback_decorator(self):
65-        self.assertEqual(testvalue, 'override')
66+        self.assertEqual(testvalue, ('override', 'setup'))
67 
68     #
69     # Regression tests for #10130: deleting settings.
70Index: django/test/utils.py
71===================================================================
72--- django/test/utils.py        (revision 17703)
73+++ django/test/utils.py        (working copy)
74@@ -167,15 +167,6 @@
75     delattr(loader, RESTORE_LOADERS_ATTR)
76 
77 
78-class OverrideSettingsHolder(UserSettingsHolder):
79-    """
80-    A custom setting holder that sends a signal upon change.
81-    """
82-    def __setattr__(self, name, value):
83-        UserSettingsHolder.__setattr__(self, name, value)
84-        setting_changed.send(sender=self.__class__, setting=name, value=value)
85-
86-
87 class override_settings(object):
88     """
89     Acts as either a decorator, or a context manager. If it's a decorator it
90@@ -215,10 +206,18 @@
91         return inner
92 
93     def enable(self):
94-        override = OverrideSettingsHolder(settings._wrapped)
95+        override = UserSettingsHolder(settings._wrapped)
96         for key, new_value in self.options.items():
97             setattr(override, key, new_value)
98         settings._wrapped = override
99+        for key, new_value in self.options.items():
100+            setting_changed.send(sender=settings._wrapped.__class__,
101+                                 setting=key, value=new_value, state='setup')
102 
103     def disable(self):
104         settings._wrapped = self.wrapped
105+        for key in self.options:
106+            new_value = getattr(settings, key, None)
107+            setting_changed.send(sender=settings._wrapped.__class__,
108+                                 setting=key, value=new_value, state='teardown')
109+