Ticket #17895: 17895.diff
File 17895.diff, 3.4 KB (added by , 13 years ago) |
---|
-
docs/ref/signals.txt
477 477 .. data:: django.test.signals.setting_changed 478 478 :module: 479 479 480 Sent when some :ref:`settings are overridden <overriding-settings>`with the480 Sent when the value of a setting is changed with the 481 481 :meth:`django.test.TestCase.setting` context manager or the 482 482 :func:`django.test.utils.override_settings` decorator/context manager. 483 483 … … 492 492 ``value`` 493 493 The new setting value. 494 494 495 ``state`` 496 ``"pre"`` when the setting is set to a new value, ``"post"`` when it's 497 reset to its original value. 498 495 499 template_rendered 496 500 ----------------- 497 501 -
django/test/utils.py
167 167 delattr(loader, RESTORE_LOADERS_ATTR) 168 168 169 169 170 class OverrideSettingsHolder(UserSettingsHolder):171 """172 A custom setting holder that sends a signal upon change.173 """174 def __setattr__(self, name, value):175 UserSettingsHolder.__setattr__(self, name, value)176 setting_changed.send(sender=self.__class__, setting=name, value=value)177 178 179 170 class override_settings(object): 180 171 """ 181 172 Acts as either a decorator, or a context manager. If it's a decorator it … … 215 206 return inner 216 207 217 208 def enable(self): 218 override = OverrideSettingsHolder(settings._wrapped)209 override = UserSettingsHolder(settings._wrapped) 219 210 for key, new_value in self.options.items(): 220 211 setattr(override, key, new_value) 221 212 settings._wrapped = override 213 for key, new_value in self.options.items(): 214 setting_changed.send(sender=settings._wrapped.__class__, 215 setting=key, value=new_value, state='pre') 222 216 223 217 def disable(self): 224 218 settings._wrapped = self.wrapped 219 for key in self.options: 220 new_value = getattr(settings, key, None) 221 setting_changed.send(sender=settings._wrapped.__class__, 222 setting=key, value=new_value, state='post') 223 -
tests/regressiontests/settings_tests/tests.py
76 76 77 77 testvalue = None 78 78 79 def signal_callback(sender, setting, value, **kwargs):79 def signal_callback(sender, setting, value, state, **kwargs): 80 80 if setting == 'TEST': 81 81 global testvalue 82 testvalue = value82 testvalue = (value, state) 83 83 84 84 signals.setting_changed.connect(signal_callback) 85 85 … … 131 131 def test_signal_callback_context_manager(self): 132 132 self.assertRaises(AttributeError, getattr, settings, 'TEST') 133 133 with self.settings(TEST='override'): 134 self.assertEqual(testvalue, 'override') 134 self.assertEqual(testvalue, ('override', 'pre')) 135 self.assertEqual(testvalue, (None, 'post')) 135 136 136 137 @override_settings(TEST='override') 137 138 def test_signal_callback_decorator(self): 138 self.assertEqual(testvalue, 'override')139 self.assertEqual(testvalue, ('override', 'pre')) 139 140 140 141 # 141 142 # Regression tests for #10130: deleting settings.