Refs #21230 -- removed direct settings manipulation from template tests

This commit is contained in:
Unai Zalakain 2013-11-28 14:22:07 +01:00 committed by Tim Graham
parent 2080bce695
commit 2688462f91
1 changed files with 31 additions and 40 deletions

View File

@ -544,7 +544,11 @@ class TemplateRegressionTests(TestCase):
t.render(Context({})) t.render(Context({}))
@override_settings(MEDIA_URL="/media/", STATIC_URL="/static/") # Set ALLOWED_INCLUDE_ROOTS so that ssi works.
@override_settings(MEDIA_URL="/media/", STATIC_URL="/static/",
TEMPLATE_DEBUG=False, ALLOWED_INCLUDE_ROOTS=(
os.path.dirname(os.path.abspath(upath(__file__))),),
)
class TemplateTests(TransRealMixin, TestCase): class TemplateTests(TransRealMixin, TestCase):
def test_templates(self): def test_templates(self):
template_tests = self.get_template_tests() template_tests = self.get_template_tests()
@ -565,19 +569,9 @@ class TemplateTests(TransRealMixin, TestCase):
failures = [] failures = []
tests = sorted(template_tests.items()) tests = sorted(template_tests.items())
# Turn TEMPLATE_DEBUG off, because tests assume that.
old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, False
# Set TEMPLATE_STRING_IF_INVALID to a known string. # Set TEMPLATE_STRING_IF_INVALID to a known string.
old_invalid = settings.TEMPLATE_STRING_IF_INVALID
expected_invalid_str = 'INVALID' expected_invalid_str = 'INVALID'
# Set ALLOWED_INCLUDE_ROOTS so that ssi works.
old_allowed_include_roots = settings.ALLOWED_INCLUDE_ROOTS
settings.ALLOWED_INCLUDE_ROOTS = (
os.path.dirname(os.path.abspath(upath(__file__))),
)
# Warm the URL reversing cache. This ensures we don't pay the cost # Warm the URL reversing cache. This ensures we don't pay the cost
# warming the cache during one of the tests. # warming the cache during one of the tests.
urlresolvers.reverse('template_tests.views.client_action', urlresolvers.reverse('template_tests.views.client_action',
@ -613,34 +607,34 @@ class TemplateTests(TransRealMixin, TestCase):
(expected_invalid_str, False, invalid_string_result), (expected_invalid_str, False, invalid_string_result),
('', True, template_debug_result) ('', True, template_debug_result)
]: ]:
settings.TEMPLATE_STRING_IF_INVALID = invalid_str with override_settings(TEMPLATE_STRING_IF_INVALID=invalid_str,
settings.TEMPLATE_DEBUG = template_debug TEMPLATE_DEBUG=template_debug):
for is_cached in (False, True): for is_cached in (False, True):
try:
try: try:
with warnings.catch_warnings(): try:
# Ignore pending deprecations of the old syntax of the 'cycle' and 'firstof' tags. with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning, module='django.template.base') # Ignore pending deprecations of the old syntax of the 'cycle' and 'firstof' tags.
test_template = loader.get_template(name) warnings.filterwarnings("ignore", category=DeprecationWarning, module='django.template.base')
except ShouldNotExecuteException: test_template = loader.get_template(name)
failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Template loading invoked method that shouldn't have been invoked." % (is_cached, invalid_str, template_debug, name)) except ShouldNotExecuteException:
failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Template loading invoked method that shouldn't have been invoked." % (is_cached, invalid_str, template_debug, name))
try: try:
output = self.render(test_template, vals) output = self.render(test_template, vals)
except ShouldNotExecuteException: except ShouldNotExecuteException:
failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Template rendering invoked method that shouldn't have been invoked." % (is_cached, invalid_str, template_debug, name)) failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Template rendering invoked method that shouldn't have been invoked." % (is_cached, invalid_str, template_debug, name))
except ContextStackException: except ContextStackException:
failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Context stack was left imbalanced" % (is_cached, invalid_str, template_debug, name)) failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Context stack was left imbalanced" % (is_cached, invalid_str, template_debug, name))
continue continue
except Exception: except Exception:
exc_type, exc_value, exc_tb = sys.exc_info() exc_type, exc_value, exc_tb = sys.exc_info()
if exc_type != result: if exc_type != result:
tb = '\n'.join(traceback.format_exception(exc_type, exc_value, exc_tb)) tb = '\n'.join(traceback.format_exception(exc_type, exc_value, exc_tb))
failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Got %s, exception: %s\n%s" % (is_cached, invalid_str, template_debug, name, exc_type, exc_value, tb)) failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Got %s, exception: %s\n%s" % (is_cached, invalid_str, template_debug, name, exc_type, exc_value, tb))
continue continue
if output != result: if output != result:
failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Expected %r, got %r" % (is_cached, invalid_str, template_debug, name, result, output)) failures.append("Template test (Cached='%s', TEMPLATE_STRING_IF_INVALID='%s', TEMPLATE_DEBUG=%s): %s -- FAILED. Expected %r, got %r" % (is_cached, invalid_str, template_debug, name, result, output))
cache_loader.reset() cache_loader.reset()
if 'LANGUAGE_CODE' in vals[1]: if 'LANGUAGE_CODE' in vals[1]:
deactivate() deactivate()
@ -651,9 +645,6 @@ class TemplateTests(TransRealMixin, TestCase):
restore_template_loaders() restore_template_loaders()
deactivate() deactivate()
settings.TEMPLATE_DEBUG = old_td
settings.TEMPLATE_STRING_IF_INVALID = old_invalid
settings.ALLOWED_INCLUDE_ROOTS = old_allowed_include_roots
self.assertEqual(failures, [], "Tests failed:\n%s\n%s" % self.assertEqual(failures, [], "Tests failed:\n%s\n%s" %
('-' * 70, ("\n%s\n" % ('-' * 70)).join(failures))) ('-' * 70, ("\n%s\n" % ('-' * 70)).join(failures)))