Ticket #16574: 16574-2.diff

File 16574-2.diff, 9.4 KB (added by Claude Paroz, 13 years ago)

Generalize override_settings

  • django/contrib/messages/tests/base.py

    diff --git a/django/contrib/messages/tests/base.py b/django/contrib/messages/tests/base.py
    index 416d16c..b91ddbd 100644
    a b  
    11from django import http
    2 from django.test import TestCase
    32from django.conf import settings
    4 from django.utils.translation import ugettext_lazy
    5 from django.utils.unittest import skipIf
    63from django.contrib.messages import constants, utils, get_level, set_level
    74from django.contrib.messages.api import MessageFailure
    85from django.contrib.messages.storage import default_storage, base
    96from django.contrib.messages.storage.base import Message
    107from django.core.urlresolvers import reverse
     8from django.test import TestCase
     9from django.test.utils import override_settings
     10from django.utils.translation import ugettext_lazy
     11from django.utils.unittest import skipIf
    1112
    1213
    1314def skipUnlessAuthIsInstalled(func):
    def add_level_messages(storage):  
    3132
    3233class BaseTest(TestCase):
    3334    storage_class = default_storage
    34     restore_settings = ['MESSAGE_LEVEL', 'MESSAGE_TAGS']
    3535    urls = 'django.contrib.messages.tests.urls'
    3636    levels = {
    3737        'debug': constants.DEBUG,
    class BaseTest(TestCase):  
    4242    }
    4343
    4444    def setUp(self):
    45         self._remembered_settings = {}
    46         for setting in self.restore_settings:
    47             if hasattr(settings, setting):
    48                 self._remembered_settings[setting] = getattr(settings, setting)
    49                 delattr(settings._wrapped, setting)
    50         # Backup these manually because we do not want them deleted.
    51         self._middleware_classes = settings.MIDDLEWARE_CLASSES
    52         self._template_context_processors = \
    53            settings.TEMPLATE_CONTEXT_PROCESSORS
    54         self._installed_apps = settings.INSTALLED_APPS
    55         self._message_storage = settings.MESSAGE_STORAGE
    56         settings.MESSAGE_STORAGE = '%s.%s' % (self.storage_class.__module__,
    57                                               self.storage_class.__name__)
    58         self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
    59         settings.TEMPLATE_DIRS = ()
     45        self.settings_override = override_settings(
     46            TEMPLATE_DIRS   = (),
     47            MESSAGE_TAGS    = '',
     48            MESSAGE_STORAGE = '%s.%s' % (self.storage_class.__module__,
     49                                         self.storage_class.__name__),
     50        )
     51        self.settings_override.enable()
    6052
    6153    def tearDown(self):
    62         for setting in self.restore_settings:
    63             self.restore_setting(setting)
    64         # Restore these manually (see above).
    65         settings.MIDDLEWARE_CLASSES = self._middleware_classes
    66         settings.TEMPLATE_CONTEXT_PROCESSORS = \
    67            self._template_context_processors
    68         settings.INSTALLED_APPS = self._installed_apps
    69         settings.MESSAGE_STORAGE = self._message_storage
    70         settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
    71 
    72     def restore_setting(self, setting):
    73         if setting in self._remembered_settings:
    74             value = self._remembered_settings.pop(setting)
    75             setattr(settings, setting, value)
    76         elif hasattr(settings, setting):
    77             delattr(settings._wrapped, setting)
     54        self.settings_override.disable()
    7855
    7956    def get_request(self):
    8057        return http.HttpRequest()
    class BaseTest(TestCase):  
    153130        storing = self.stored_messages_count(storage, response)
    154131        self.assertEqual(storing, 1)
    155132
     133    @override_settings(MESSAGE_LEVEL=constants.DEBUG)
    156134    def test_full_request_response_cycle(self):
    157135        """
    158136        With the message middleware enabled, tests that messages are properly
    159137        stored and then retrieved across the full request/redirect/response
    160138        cycle.
    161139        """
    162         settings.MESSAGE_LEVEL = constants.DEBUG
    163140        data = {
    164141            'messages': ['Test message %d' % x for x in xrange(10)],
    165142        }
    class BaseTest(TestCase):  
    176153            for msg in data['messages']:
    177154                self.assertContains(response, msg)
    178155
     156    @override_settings(MESSAGE_LEVEL=constants.DEBUG)
    179157    def test_with_template_response(self):
    180         settings.MESSAGE_LEVEL = constants.DEBUG
    181158        data = {
    182159            'messages': ['Test message %d' % x for x in xrange(10)],
    183160        }
    class BaseTest(TestCase):  
    196173            for msg in data['messages']:
    197174                self.assertNotContains(response, msg)
    198175
     176    @override_settings(MESSAGE_LEVEL=constants.DEBUG)
    199177    def test_multiple_posts(self):
    200178        """
    201179        Tests that messages persist properly when multiple POSTs are made
    202180        before a GET.
    203181        """
    204         settings.MESSAGE_LEVEL = constants.DEBUG
    205182        data = {
    206183            'messages': ['Test message %d' % x for x in xrange(10)],
    207184        }
    class BaseTest(TestCase):  
    219196        for msg in data['messages']:
    220197            self.assertContains(response, msg)
    221198
     199    @override_settings(
     200        INSTALLED_APPS=filter(
     201            lambda app:app!='django.contrib.messages', settings.INSTALLED_APPS),
     202        MIDDLEWARE_CLASSES=filter(
     203            lambda m:'MessageMiddleware' not in m, settings.MIDDLEWARE_CLASSES),
     204        TEMPLATE_CONTEXT_PROCESSORS=filter(
     205            lambda p:'context_processors.messages' not in p,
     206                 settings.TEMPLATE_CONTEXT_PROCESSORS),
     207        MESSAGE_LEVEL=constants.DEBUG
     208    )
    222209    def test_middleware_disabled(self):
    223210        """
    224211        Tests that, when the middleware is disabled, an exception is raised
    225212        when one attempts to store a message.
    226213        """
    227         settings.MESSAGE_LEVEL = constants.DEBUG
    228         settings.INSTALLED_APPS = list(settings.INSTALLED_APPS)
    229         settings.INSTALLED_APPS.remove(
    230             'django.contrib.messages',
    231         )
    232         settings.MIDDLEWARE_CLASSES = list(settings.MIDDLEWARE_CLASSES)
    233         settings.MIDDLEWARE_CLASSES.remove(
    234             'django.contrib.messages.middleware.MessageMiddleware',
    235         )
    236         settings.TEMPLATE_CONTEXT_PROCESSORS = \
    237           list(settings.TEMPLATE_CONTEXT_PROCESSORS)
    238         settings.TEMPLATE_CONTEXT_PROCESSORS.remove(
    239             'django.contrib.messages.context_processors.messages',
    240         )
    241214        data = {
    242215            'messages': ['Test message %d' % x for x in xrange(10)],
    243216        }
    class BaseTest(TestCase):  
    248221            self.assertRaises(MessageFailure, self.client.post, add_url,
    249222                              data, follow=True)
    250223
     224    @override_settings(
     225        INSTALLED_APPS=filter(
     226            lambda app:app!='django.contrib.messages', settings.INSTALLED_APPS),
     227        MIDDLEWARE_CLASSES=filter(
     228            lambda m:'MessageMiddleware' not in m, settings.MIDDLEWARE_CLASSES),
     229        TEMPLATE_CONTEXT_PROCESSORS=filter(
     230            lambda p:'context_processors.messages' not in p,
     231                 settings.TEMPLATE_CONTEXT_PROCESSORS),
     232        MESSAGE_LEVEL=constants.DEBUG
     233    )
    251234    def test_middleware_disabled_fail_silently(self):
    252235        """
    253236        Tests that, when the middleware is disabled, an exception is not
    254237        raised if 'fail_silently' = True
    255238        """
    256         settings.MESSAGE_LEVEL = constants.DEBUG
    257         settings.INSTALLED_APPS = list(settings.INSTALLED_APPS)
    258         settings.INSTALLED_APPS.remove(
    259             'django.contrib.messages',
    260         )
    261         settings.MIDDLEWARE_CLASSES = list(settings.MIDDLEWARE_CLASSES)
    262         settings.MIDDLEWARE_CLASSES.remove(
    263             'django.contrib.messages.middleware.MessageMiddleware',
    264         )
    265         settings.TEMPLATE_CONTEXT_PROCESSORS = \
    266           list(settings.TEMPLATE_CONTEXT_PROCESSORS)
    267         settings.TEMPLATE_CONTEXT_PROCESSORS.remove(
    268             'django.contrib.messages.context_processors.messages',
    269         )
    270239        data = {
    271240            'messages': ['Test message %d' % x for x in xrange(10)],
    272241            'fail_silently': True,
    class BaseTest(TestCase):  
    350319        add_level_messages(storage)
    351320        self.assertEqual(len(storage), 2)
    352321
     322    @override_settings(MESSAGE_LEVEL=29)
    353323    def test_settings_level(self):
    354324        request = self.get_request()
    355325        storage = self.storage_class(request)
    356326
    357         settings.MESSAGE_LEVEL = 29
    358327        self.assertEqual(get_level(request), 29)
    359328
    360329        add_level_messages(storage)
    class BaseTest(TestCase):  
    369338                         ['info', '', 'extra-tag debug', 'warning', 'error',
    370339                          'success'])
    371340
    372     def test_custom_tags(self):
    373         settings.MESSAGE_TAGS = {
     341    @override_settings(MESSAGE_TAGS={
    374342            constants.INFO: 'info',
    375343            constants.DEBUG: '',
    376344            constants.WARNING: '',
    377345            constants.ERROR: 'bad',
    378346            29: 'custom',
    379347        }
     348    )
     349    def test_custom_tags(self):
    380350        # LEVEL_TAGS is a constant defined in the
    381351        # django.contrib.messages.storage.base module, so after changing
    382352        # settings.MESSAGE_TAGS, we need to update that constant too.
     353        old_level_tags = base.LEVEL_TAGS
    383354        base.LEVEL_TAGS = utils.get_level_tags()
    384355        try:
    385356            storage = self.get_storage()
    class BaseTest(TestCase):  
    390361                         ['info', 'custom', 'extra-tag', '', 'bad', 'success'])
    391362        finally:
    392363            # Ensure the level tags constant is put back like we found it.
    393             self.restore_setting('MESSAGE_TAGS')
    394             base.LEVEL_TAGS = utils.get_level_tags()
     364            base.LEVEL_TAGS = old_level_tags
Back to Top