Ticket #15561: 15561-1.diff

File 15561-1.diff, 3.2 KB (added by Claude Paroz, 14 years ago)

Add settings manipulation functions in django test utilities

  • 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
    - +  
     1from django.test.utils import alter_django_settings, restore_django_settings
     2from django.utils.functional import wraps
     3
     4
     5def 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():  
    9797    del mail.outbox
    9898
    9999
     100def 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
     112def 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
    100120def get_warnings_state():
    101121    """
    102122    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,  
    1616from django.core.mail.backends import console, dummy, locmem, filebased, smtp
    1717from django.core.mail.message import BadHeaderError
    1818from django.test import TestCase
     19from django.test.decorators import with_django_settings
     20from django.test.utils import alter_django_settings, restore_django_settings
    1921from django.utils.translation import ugettext_lazy
    20 from django.utils.functional import wraps
    21 
    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, nonexistant
    33 
    34 
    35 def restore_django_settings(state):
    36     oldvalues, nonexistant = state
    37     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_test
    53     return decorator
    5422
    5523
    5624class MailTests(TestCase):
Back to Top