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
This commit is contained in:
Claude Paroz 2012-04-09 13:24:57 +00:00
parent 36ab8ae270
commit 883c38c499
7 changed files with 33 additions and 73 deletions

View File

@ -30,13 +30,9 @@ class AuthContextProcessorTests(TestCase):
Tests that the session is not accessed simply by including Tests that the session is not accessed simply by including
the auth context processor the auth context processor
""" """
context._standard_context_processors = None
response = self.client.get('/auth_processor_no_attr_access/') response = self.client.get('/auth_processor_no_attr_access/')
self.assertContains(response, "Session not accessed") self.assertContains(response, "Session not accessed")
context._standard_context_processors = None
@override_settings( @override_settings(
MIDDLEWARE_CLASSES=global_settings.MIDDLEWARE_CLASSES, MIDDLEWARE_CLASSES=global_settings.MIDDLEWARE_CLASSES,
TEMPLATE_CONTEXT_PROCESSORS=global_settings.TEMPLATE_CONTEXT_PROCESSORS, 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 Tests that the session is accessed if the auth context processor
is used and relevant attributes accessed. is used and relevant attributes accessed.
""" """
context._standard_context_processors = None
response = self.client.get('/auth_processor_attr_access/') response = self.client.get('/auth_processor_attr_access/')
self.assertContains(response, "Session accessed") self.assertContains(response, "Session accessed")
context._standard_context_processors = None
def test_perms_attrs(self): def test_perms_attrs(self):
self.client.login(username='super', password='secret') self.client.login(username='super', password='secret')
response = self.client.get('/auth_processor_perms/') response = self.client.get('/auth_processor_perms/')

View File

@ -259,8 +259,7 @@ class BaseTest(TestCase):
args=(level,)) args=(level,))
response = self.client.post(add_url, data, follow=True) response = self.client.post(add_url, data, follow=True)
self.assertRedirects(response, show_url) self.assertRedirects(response, show_url)
self.assertTrue('messages' in response.context) self.assertFalse('messages' in response.context)
self.assertEqual(list(response.context['messages']), [])
def stored_messages_count(self, storage, response): def stored_messages_count(self, storage, response):
""" """

View File

@ -1,11 +1,13 @@
from django.conf import settings from django.conf import settings
from django.db import connections 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"]) template_rendered = Signal(providing_args=["template", "context"])
setting_changed = Signal(providing_args=["setting", "value"]) setting_changed = Signal(providing_args=["setting", "value"])
@receiver(setting_changed)
def update_connections_time_zone(**kwargs): def update_connections_time_zone(**kwargs):
if kwargs['setting'] == 'USE_TZ' and settings.TIME_ZONE != 'UTC': if kwargs['setting'] == 'USE_TZ' and settings.TIME_ZONE != 'UTC':
USE_TZ, TIME_ZONE = kwargs['value'], settings.TIME_ZONE USE_TZ, TIME_ZONE = kwargs['value'], settings.TIME_ZONE
@ -20,4 +22,7 @@ def update_connections_time_zone(**kwargs):
if tz_sql: if tz_sql:
conn.cursor().execute(tz_sql, [tz]) 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

View File

@ -6,7 +6,7 @@ import re
import datetime import datetime
import urlparse import urlparse
from django.conf import settings from django.conf import settings, global_settings
from django.core import mail from django.core import mail
from django.core.exceptions import SuspiciousOperation from django.core.exceptions import SuspiciousOperation
from django.core.files import temp as tempfile 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.auth.models import Group, User, Permission, UNUSABLE_PASSWORD
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.forms.util import ErrorList from django.forms.util import ErrorList
from django.template import context as context_module
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
from django.test import TestCase from django.test import TestCase
from django.utils import formats, translation, unittest from django.utils import formats, translation, unittest
@ -3364,25 +3363,17 @@ class ValidXHTMLTests(TestCase):
urlbit = 'admin' urlbit = 'admin'
def setUp(self): 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') self.client.login(username='super', password='secret')
def tearDown(self): def tearDown(self):
self.client.logout() 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): def testLangNamePresent(self):
response = self.client.get('/test_admin/%s/admin_views/' % self.urlbit) response = self.client.get('/test_admin/%s/admin_views/' % self.urlbit)
self.assertFalse(' lang=""' in response.content) self.assertFalse(' lang=""' in response.content)

View File

@ -3,13 +3,12 @@ import pickle
import time import time
from datetime import datetime from datetime import datetime
from django.utils import unittest
from django.test import RequestFactory, TestCase from django.test import RequestFactory, TestCase
from django.conf import settings from django.conf import settings
import django.template.context
from django.template import Template, Context from django.template import Template, Context
from django.template.response import (TemplateResponse, SimpleTemplateResponse, from django.template.response import (TemplateResponse, SimpleTemplateResponse,
ContentNotRenderedError) ContentNotRenderedError)
from django.test.utils import override_settings
def test_processor(request): def test_processor(request):
return {'processors': 'yes'} return {'processors': 'yes'}
@ -22,32 +21,7 @@ class CustomURLConfMiddleware(object):
request.urlconf = 'regressiontests.templates.alternate_urls' request.urlconf = 'regressiontests.templates.alternate_urls'
class BaseTemplateResponseTest(unittest.TestCase): class SimpleTemplateResponseTest(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):
def _response(self, template='foo', *args, **kwargs): def _response(self, template='foo', *args, **kwargs):
return SimpleTemplateResponse(Template(template), *args, **kwargs) return SimpleTemplateResponse(Template(template), *args, **kwargs)
@ -213,7 +187,14 @@ class SimpleTemplateResponseTest(BaseTemplateResponseTest):
unpickled_response = pickle.loads(pickled_response) unpickled_response = pickle.loads(pickled_response)
repickled_response = pickle.dumps(unpickled_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): def _response(self, template='foo', *args, **kwargs):
return TemplateResponse(self.factory.get('/'), Template(template), return TemplateResponse(self.factory.get('/'), Template(template),

View File

@ -37,8 +37,8 @@ from .parser import ParserTests
from .unicode import UnicodeTests from .unicode import UnicodeTests
from .nodelist import NodelistTest, ErrorIndexTest from .nodelist import NodelistTest, ErrorIndexTest
from .smartif import SmartIfTests from .smartif import SmartIfTests
from .response import (TemplateResponseTest, BaseTemplateResponseTest, from .response import (TemplateResponseTest, CacheMiddlewareTest,
CacheMiddlewareTest, SimpleTemplateResponseTest, CustomURLConfTest) SimpleTemplateResponseTest, CustomURLConfTest)
try: try:
from .loaders import RenderToStringTest, EggLoaderTest from .loaders import RenderToStringTest, EggLoaderTest
@ -1738,7 +1738,7 @@ class TemplateTagLoading(unittest.TestCase):
t = template.Template(ttext) t = template.Template(ttext)
class RequestContextTests(BaseTemplateResponseTest): class RequestContextTests(unittest.TestCase):
def setUp(self): def setUp(self):
templates = { templates = {

View File

@ -2,7 +2,12 @@ import warnings
from django.conf import settings from django.conf import settings
from django.test import TestCase 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): class ShortcutTests(TestCase):
urls = 'regressiontests.views.generic_urls' urls = 'regressiontests.views.generic_urls'
@ -11,21 +16,9 @@ class ShortcutTests(TestCase):
warnings.filterwarnings('ignore', category=DeprecationWarning, warnings.filterwarnings('ignore', category=DeprecationWarning,
module='django.views.generic.simple') 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): def tearDown(self):
self.restore_warnings_state() 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): def test_render_to_response(self):
response = self.client.get('/shortcuts/render_to_response/') response = self.client.get('/shortcuts/render_to_response/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
@ -74,4 +67,3 @@ class ShortcutTests(TestCase):
def test_render_with_current_app_conflict(self): def test_render_with_current_app_conflict(self):
self.assertRaises(ValueError, self.client.get, '/shortcuts/render/current_app_conflict/') self.assertRaises(ValueError, self.client.get, '/shortcuts/render/current_app_conflict/')