Ticket #15561: 15561-1.diff
File 15561-1.diff, 3.2 KB (added by , 14 years ago) |
---|
-
new file django/test/decorators.py
diff --git a/django/test/decorators.py b/django/test/decorators.py new file mode 100644 index 0000000..146e859
- + 1 from django.test.utils import alter_django_settings, restore_django_settings 2 from django.utils.functional import wraps 3 4 5 def with_django_settings(**kwargs): 6 def decorator(test): 7 @wraps(test) 8 def decorated_test(self): 9 state = alter_django_settings(**kwargs) 10 try: 11 return test(self) 12 finally: 13 restore_django_settings(state) 14 return decorated_test 15 return decorator -
django/test/utils.py
diff --git a/django/test/utils.py b/django/test/utils.py index 90a8b3e..8cd9035 100644
a b def teardown_test_environment(): 97 97 del mail.outbox 98 98 99 99 100 def alter_django_settings(**kwargs): 101 oldvalues = {} 102 nonexistant = [] 103 for setting, newvalue in kwargs.iteritems(): 104 try: 105 oldvalues[setting] = getattr(settings, setting) 106 except AttributeError: 107 nonexistant.append(setting) 108 setattr(settings, setting, newvalue) 109 return oldvalues, nonexistant 110 111 112 def restore_django_settings(state): 113 oldvalues, nonexistant = state 114 for setting, oldvalue in oldvalues.iteritems(): 115 setattr(settings, setting, oldvalue) 116 for setting in nonexistant: 117 delattr(settings, setting) 118 119 100 120 def get_warnings_state(): 101 121 """ 102 122 Returns an object containing the state of the warnings module -
tests/regressiontests/mail/tests.py
diff --git a/tests/regressiontests/mail/tests.py b/tests/regressiontests/mail/tests.py index ffb86db..e73c9d3 100644
a b from django.core.mail import (EmailMessage, mail_admins, mail_managers, 16 16 from django.core.mail.backends import console, dummy, locmem, filebased, smtp 17 17 from django.core.mail.message import BadHeaderError 18 18 from django.test import TestCase 19 from django.test.decorators import with_django_settings 20 from django.test.utils import alter_django_settings, restore_django_settings 19 21 from django.utils.translation import ugettext_lazy 20 from django.utils.functional import wraps21 22 23 def alter_django_settings(**kwargs):24 oldvalues = {}25 nonexistant = []26 for setting, newvalue in kwargs.iteritems():27 try:28 oldvalues[setting] = getattr(settings, setting)29 except AttributeError:30 nonexistant.append(setting)31 setattr(settings, setting, newvalue)32 return oldvalues, nonexistant33 34 35 def restore_django_settings(state):36 oldvalues, nonexistant = state37 for setting, oldvalue in oldvalues.iteritems():38 setattr(settings, setting, oldvalue)39 for setting in nonexistant:40 delattr(settings, setting)41 42 43 def with_django_settings(**kwargs):44 def decorator(test):45 @wraps(test)46 def decorated_test(self):47 state = alter_django_settings(**kwargs)48 try:49 return test(self)50 finally:51 restore_django_settings(state)52 return decorated_test53 return decorator54 22 55 23 56 24 class MailTests(TestCase):