From 883c38c499d58ec1c8182933de88f7d1164387e3 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Mon, 9 Apr 2012 13:24:57 +0000 Subject: [PATCH] Fixed #17848 -- Added setting_changed signal for cases when TEMPLATE_CONTEXT_PROCESSORS is overriden in tests. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17885 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- .../contrib/auth/tests/context_processors.py | 8 ---- django/contrib/messages/tests/base.py | 3 +- django/test/signals.py | 9 ++++- tests/regressiontests/admin_views/tests.py | 23 ++++------- tests/regressiontests/templates/response.py | 39 +++++-------------- tests/regressiontests/templates/tests.py | 6 +-- .../regressiontests/views/tests/shortcuts.py | 18 +++------ 7 files changed, 33 insertions(+), 73 deletions(-) diff --git a/django/contrib/auth/tests/context_processors.py b/django/contrib/auth/tests/context_processors.py index bba2046b31..31a56f406c 100644 --- a/django/contrib/auth/tests/context_processors.py +++ b/django/contrib/auth/tests/context_processors.py @@ -30,13 +30,9 @@ class AuthContextProcessorTests(TestCase): Tests that the session is not accessed simply by including the auth context processor """ - context._standard_context_processors = None - response = self.client.get('/auth_processor_no_attr_access/') self.assertContains(response, "Session not accessed") - context._standard_context_processors = None - @override_settings( MIDDLEWARE_CLASSES=global_settings.MIDDLEWARE_CLASSES, TEMPLATE_CONTEXT_PROCESSORS=global_settings.TEMPLATE_CONTEXT_PROCESSORS, @@ -46,13 +42,9 @@ class AuthContextProcessorTests(TestCase): Tests that the session is accessed if the auth context processor is used and relevant attributes accessed. """ - context._standard_context_processors = None - response = self.client.get('/auth_processor_attr_access/') self.assertContains(response, "Session accessed") - context._standard_context_processors = None - def test_perms_attrs(self): self.client.login(username='super', password='secret') response = self.client.get('/auth_processor_perms/') diff --git a/django/contrib/messages/tests/base.py b/django/contrib/messages/tests/base.py index 8d0453245c..4a00324a3d 100644 --- a/django/contrib/messages/tests/base.py +++ b/django/contrib/messages/tests/base.py @@ -259,8 +259,7 @@ class BaseTest(TestCase): args=(level,)) response = self.client.post(add_url, data, follow=True) self.assertRedirects(response, show_url) - self.assertTrue('messages' in response.context) - self.assertEqual(list(response.context['messages']), []) + self.assertFalse('messages' in response.context) def stored_messages_count(self, storage, response): """ diff --git a/django/test/signals.py b/django/test/signals.py index 01d55817a2..ed16831d42 100644 --- a/django/test/signals.py +++ b/django/test/signals.py @@ -1,11 +1,13 @@ from django.conf import settings from django.db import connections -from django.dispatch import Signal +from django.dispatch import receiver, Signal +from django.template import context template_rendered = Signal(providing_args=["template", "context"]) setting_changed = Signal(providing_args=["setting", "value"]) +@receiver(setting_changed) def update_connections_time_zone(**kwargs): if kwargs['setting'] == 'USE_TZ' and settings.TIME_ZONE != 'UTC': USE_TZ, TIME_ZONE = kwargs['value'], settings.TIME_ZONE @@ -20,4 +22,7 @@ def update_connections_time_zone(**kwargs): if tz_sql: conn.cursor().execute(tz_sql, [tz]) -setting_changed.connect(update_connections_time_zone) +@receiver(setting_changed) +def clear_context_processors_cache(**kwargs): + if kwargs['setting'] == 'TEMPLATE_CONTEXT_PROCESSORS': + context._standard_context_processors = None diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index 2a389fd5ec..7a0a3bf868 100644 --- a/tests/regressiontests/admin_views/tests.py +++ b/tests/regressiontests/admin_views/tests.py @@ -6,7 +6,7 @@ import re import datetime import urlparse -from django.conf import settings +from django.conf import settings, global_settings from django.core import mail from django.core.exceptions import SuspiciousOperation from django.core.files import temp as tempfile @@ -23,7 +23,6 @@ from django.contrib.auth import REDIRECT_FIELD_NAME from django.contrib.auth.models import Group, User, Permission, UNUSABLE_PASSWORD from django.contrib.contenttypes.models import ContentType from django.forms.util import ErrorList -from django.template import context as context_module from django.template.response import TemplateResponse from django.test import TestCase from django.utils import formats, translation, unittest @@ -3364,25 +3363,17 @@ class ValidXHTMLTests(TestCase): urlbit = 'admin' def setUp(self): - self._context_processors = None - self._use_i18n, settings.USE_I18N = settings.USE_I18N, False - if 'django.core.context_processors.i18n' in settings.TEMPLATE_CONTEXT_PROCESSORS: - self._context_processors = settings.TEMPLATE_CONTEXT_PROCESSORS - cp = list(settings.TEMPLATE_CONTEXT_PROCESSORS) - cp.remove('django.core.context_processors.i18n') - settings.TEMPLATE_CONTEXT_PROCESSORS = tuple(cp) - # Force re-evaluation of the contex processor list - context_module._standard_context_processors = None self.client.login(username='super', password='secret') def tearDown(self): self.client.logout() - if self._context_processors is not None: - settings.TEMPLATE_CONTEXT_PROCESSORS = self._context_processors - # Force re-evaluation of the contex processor list - context_module._standard_context_processors = None - settings.USE_I18N = self._use_i18n + @override_settings( + TEMPLATE_CONTEXT_PROCESSORS=filter( + lambda t:t!='django.core.context_processors.i18n', + global_settings.TEMPLATE_CONTEXT_PROCESSORS), + USE_I18N=False, + ) def testLangNamePresent(self): response = self.client.get('/test_admin/%s/admin_views/' % self.urlbit) self.assertFalse(' lang=""' in response.content) diff --git a/tests/regressiontests/templates/response.py b/tests/regressiontests/templates/response.py index 7c59da2b32..3c45b7a9d4 100644 --- a/tests/regressiontests/templates/response.py +++ b/tests/regressiontests/templates/response.py @@ -3,13 +3,12 @@ import pickle import time from datetime import datetime -from django.utils import unittest from django.test import RequestFactory, TestCase from django.conf import settings -import django.template.context from django.template import Template, Context from django.template.response import (TemplateResponse, SimpleTemplateResponse, ContentNotRenderedError) +from django.test.utils import override_settings def test_processor(request): return {'processors': 'yes'} @@ -22,32 +21,7 @@ class CustomURLConfMiddleware(object): request.urlconf = 'regressiontests.templates.alternate_urls' -class BaseTemplateResponseTest(unittest.TestCase): - # tests rely on fact that global context - # processors should only work when RequestContext is used. - - def setUp(self): - self.factory = RequestFactory() - self._old_processors = settings.TEMPLATE_CONTEXT_PROCESSORS - self._old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS - settings.TEMPLATE_CONTEXT_PROCESSORS = [test_processor_name] - settings.TEMPLATE_DIRS = ( - os.path.join( - os.path.dirname(__file__), - 'templates' - ), - ) - # Force re-evaluation of the contex processor list - django.template.context._standard_context_processors = None - - def tearDown(self): - settings.TEMPLATE_DIRS = self._old_TEMPLATE_DIRS - settings.TEMPLATE_CONTEXT_PROCESSORS = self._old_processors - # Force re-evaluation of the contex processor list - django.template.context._standard_context_processors = None - - -class SimpleTemplateResponseTest(BaseTemplateResponseTest): +class SimpleTemplateResponseTest(TestCase): def _response(self, template='foo', *args, **kwargs): return SimpleTemplateResponse(Template(template), *args, **kwargs) @@ -213,7 +187,14 @@ class SimpleTemplateResponseTest(BaseTemplateResponseTest): unpickled_response = pickle.loads(pickled_response) repickled_response = pickle.dumps(unpickled_response) -class TemplateResponseTest(BaseTemplateResponseTest): +@override_settings( + TEMPLATE_CONTEXT_PROCESSORS=[test_processor_name], + TEMPLATE_DIRS=(os.path.join(os.path.dirname(__file__),'templates')), +) +class TemplateResponseTest(TestCase): + + def setUp(self): + self.factory = RequestFactory() def _response(self, template='foo', *args, **kwargs): return TemplateResponse(self.factory.get('/'), Template(template), diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index e166f7e15b..e40ffcbdc4 100644 --- a/tests/regressiontests/templates/tests.py +++ b/tests/regressiontests/templates/tests.py @@ -37,8 +37,8 @@ from .parser import ParserTests from .unicode import UnicodeTests from .nodelist import NodelistTest, ErrorIndexTest from .smartif import SmartIfTests -from .response import (TemplateResponseTest, BaseTemplateResponseTest, - CacheMiddlewareTest, SimpleTemplateResponseTest, CustomURLConfTest) +from .response import (TemplateResponseTest, CacheMiddlewareTest, + SimpleTemplateResponseTest, CustomURLConfTest) try: from .loaders import RenderToStringTest, EggLoaderTest @@ -1738,7 +1738,7 @@ class TemplateTagLoading(unittest.TestCase): t = template.Template(ttext) -class RequestContextTests(BaseTemplateResponseTest): +class RequestContextTests(unittest.TestCase): def setUp(self): templates = { diff --git a/tests/regressiontests/views/tests/shortcuts.py b/tests/regressiontests/views/tests/shortcuts.py index 24bf6bbae4..d20b6d1d4e 100644 --- a/tests/regressiontests/views/tests/shortcuts.py +++ b/tests/regressiontests/views/tests/shortcuts.py @@ -2,7 +2,12 @@ import warnings from django.conf import settings from django.test import TestCase +from django.test.utils import override_settings +@override_settings( + TEMPLATE_CONTEXT_PROCESSORS=('django.core.context_processors.static',), + STATIC_URL='/path/to/static/media/', +) class ShortcutTests(TestCase): urls = 'regressiontests.views.generic_urls' @@ -11,21 +16,9 @@ class ShortcutTests(TestCase): warnings.filterwarnings('ignore', category=DeprecationWarning, module='django.views.generic.simple') - self.old_STATIC_URL = settings.STATIC_URL - self.old_TEMPLATE_CONTEXT_PROCESSORS = settings.TEMPLATE_CONTEXT_PROCESSORS - - settings.STATIC_URL = '/path/to/static/media/' - settings.TEMPLATE_CONTEXT_PROCESSORS = ( - 'django.core.context_processors.static' - ) - def tearDown(self): self.restore_warnings_state() - def tearDown(self): - settings.STATIC_URL = self.old_STATIC_URL - settings.TEMPLATE_CONTEXT_PROCESSORS = self.old_TEMPLATE_CONTEXT_PROCESSORS - def test_render_to_response(self): response = self.client.get('/shortcuts/render_to_response/') self.assertEqual(response.status_code, 200) @@ -74,4 +67,3 @@ class ShortcutTests(TestCase): def test_render_with_current_app_conflict(self): self.assertRaises(ValueError, self.client.get, '/shortcuts/render/current_app_conflict/') -