Ticket #16574: 16574-3.diff

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

Fix base.LEVEL_TAGS at override_settings level

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

    diff --git a/django/contrib/messages/tests/base.py b/django/contrib/messages/tests/base.py
    index 416d16c..8d04532 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):  
    2930    storage.add(constants.SUCCESS, 'This was a triumph.')
    3031
    3132
     33class override_settings_tags(override_settings):
     34     def enable(self):
     35        super(override_settings_tags, self).enable()
     36        # LEVEL_TAGS is a constant defined in the
     37        # django.contrib.messages.storage.base module, so after changing
     38        # settings.MESSAGE_TAGS, we need to update that constant too.
     39        self.old_level_tags = base.LEVEL_TAGS
     40        base.LEVEL_TAGS = utils.get_level_tags()
     41     def disable(self):
     42        super(override_settings_tags, self).disable()
     43        base.LEVEL_TAGS = self.old_level_tags
     44
     45
    3246class BaseTest(TestCase):
    3347    storage_class = default_storage
    34     restore_settings = ['MESSAGE_LEVEL', 'MESSAGE_TAGS']
    3548    urls = 'django.contrib.messages.tests.urls'
    3649    levels = {
    3750        'debug': constants.DEBUG,
    class BaseTest(TestCase):  
    4255    }
    4356
    4457    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 = ()
     58        self.settings_override = override_settings_tags(
     59            TEMPLATE_DIRS   = (),
     60            MESSAGE_TAGS    = '',
     61            MESSAGE_STORAGE = '%s.%s' % (self.storage_class.__module__,
     62                                         self.storage_class.__name__),
     63        )
     64        self.settings_override.enable()
    6065
    6166    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)
     67        self.settings_override.disable()
    7868
    7969    def get_request(self):
    8070        return http.HttpRequest()
    class BaseTest(TestCase):  
    153143        storing = self.stored_messages_count(storage, response)
    154144        self.assertEqual(storing, 1)
    155145
     146    @override_settings(MESSAGE_LEVEL=constants.DEBUG)
    156147    def test_full_request_response_cycle(self):
    157148        """
    158149        With the message middleware enabled, tests that messages are properly
    159150        stored and then retrieved across the full request/redirect/response
    160151        cycle.
    161152        """
    162         settings.MESSAGE_LEVEL = constants.DEBUG
    163153        data = {
    164154            'messages': ['Test message %d' % x for x in xrange(10)],
    165155        }
    class BaseTest(TestCase):  
    176166            for msg in data['messages']:
    177167                self.assertContains(response, msg)
    178168
     169    @override_settings(MESSAGE_LEVEL=constants.DEBUG)
    179170    def test_with_template_response(self):
    180         settings.MESSAGE_LEVEL = constants.DEBUG
    181171        data = {
    182172            'messages': ['Test message %d' % x for x in xrange(10)],
    183173        }
    class BaseTest(TestCase):  
    196186            for msg in data['messages']:
    197187                self.assertNotContains(response, msg)
    198188
     189    @override_settings(MESSAGE_LEVEL=constants.DEBUG)
    199190    def test_multiple_posts(self):
    200191        """
    201192        Tests that messages persist properly when multiple POSTs are made
    202193        before a GET.
    203194        """
    204         settings.MESSAGE_LEVEL = constants.DEBUG
    205195        data = {
    206196            'messages': ['Test message %d' % x for x in xrange(10)],
    207197        }
    class BaseTest(TestCase):  
    219209        for msg in data['messages']:
    220210            self.assertContains(response, msg)
    221211
     212    @override_settings(
     213        INSTALLED_APPS=filter(
     214            lambda app:app!='django.contrib.messages', settings.INSTALLED_APPS),
     215        MIDDLEWARE_CLASSES=filter(
     216            lambda m:'MessageMiddleware' not in m, settings.MIDDLEWARE_CLASSES),
     217        TEMPLATE_CONTEXT_PROCESSORS=filter(
     218            lambda p:'context_processors.messages' not in p,
     219                 settings.TEMPLATE_CONTEXT_PROCESSORS),
     220        MESSAGE_LEVEL=constants.DEBUG
     221    )
    222222    def test_middleware_disabled(self):
    223223        """
    224224        Tests that, when the middleware is disabled, an exception is raised
    225225        when one attempts to store a message.
    226226        """
    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         )
    241227        data = {
    242228            'messages': ['Test message %d' % x for x in xrange(10)],
    243229        }
    class BaseTest(TestCase):  
    248234            self.assertRaises(MessageFailure, self.client.post, add_url,
    249235                              data, follow=True)
    250236
     237    @override_settings(
     238        INSTALLED_APPS=filter(
     239            lambda app:app!='django.contrib.messages', settings.INSTALLED_APPS),
     240        MIDDLEWARE_CLASSES=filter(
     241            lambda m:'MessageMiddleware' not in m, settings.MIDDLEWARE_CLASSES),
     242        TEMPLATE_CONTEXT_PROCESSORS=filter(
     243            lambda p:'context_processors.messages' not in p,
     244                 settings.TEMPLATE_CONTEXT_PROCESSORS),
     245        MESSAGE_LEVEL=constants.DEBUG
     246    )
    251247    def test_middleware_disabled_fail_silently(self):
    252248        """
    253249        Tests that, when the middleware is disabled, an exception is not
    254250        raised if 'fail_silently' = True
    255251        """
    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         )
    270252        data = {
    271253            'messages': ['Test message %d' % x for x in xrange(10)],
    272254            'fail_silently': True,
    class BaseTest(TestCase):  
    350332        add_level_messages(storage)
    351333        self.assertEqual(len(storage), 2)
    352334
     335    @override_settings(MESSAGE_LEVEL=29)
    353336    def test_settings_level(self):
    354337        request = self.get_request()
    355338        storage = self.storage_class(request)
    356339
    357         settings.MESSAGE_LEVEL = 29
    358340        self.assertEqual(get_level(request), 29)
    359341
    360342        add_level_messages(storage)
    class BaseTest(TestCase):  
    369351                         ['info', '', 'extra-tag debug', 'warning', 'error',
    370352                          'success'])
    371353
    372     def test_custom_tags(self):
    373         settings.MESSAGE_TAGS = {
     354    @override_settings_tags(MESSAGE_TAGS={
    374355            constants.INFO: 'info',
    375356            constants.DEBUG: '',
    376357            constants.WARNING: '',
    377358            constants.ERROR: 'bad',
    378359            29: 'custom',
    379360        }
    380         # LEVEL_TAGS is a constant defined in the
    381         # django.contrib.messages.storage.base module, so after changing
    382         # settings.MESSAGE_TAGS, we need to update that constant too.
    383         base.LEVEL_TAGS = utils.get_level_tags()
    384         try:
    385             storage = self.get_storage()
    386             storage.level = 0
    387             add_level_messages(storage)
    388             tags = [msg.tags for msg in storage]
    389             self.assertEqual(tags,
    390                          ['info', 'custom', 'extra-tag', '', 'bad', 'success'])
    391         finally:
    392             # 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()
     361    )
     362    def test_custom_tags(self):
     363        storage = self.get_storage()
     364        storage.level = 0
     365        add_level_messages(storage)
     366        tags = [msg.tags for msg in storage]
     367        self.assertEqual(tags,
     368                     ['info', 'custom', 'extra-tag', '', 'bad', 'success'])
Back to Top