diff --git a/tests/template_tests/test_context.py b/tests/template_tests/test_context.py index c7cd70ef6f..8331904f64 100644 --- a/tests/template_tests/test_context.py +++ b/tests/template_tests/test_context.py @@ -1,15 +1,15 @@ # -*- coding: utf-8 -*- -from unittest import TestCase - from django.http import HttpRequest from django.template import ( - Context, RequestContext, Variable, VariableDoesNotExist, + Context, RequestContext, Template, Variable, VariableDoesNotExist, ) from django.template.context import RenderContext +from django.test import RequestFactory, SimpleTestCase, override_settings -class ContextTests(TestCase): +class ContextTests(SimpleTestCase): + def test_context(self): c = Context({"a": 1, "b": "xyzzy"}) self.assertEqual(c["a"], 1) @@ -31,13 +31,21 @@ class ContextTests(TestCase): self.assertEqual(c['a'], 1) def test_resolve_on_context_method(self): - # Regression test for #17778 + """ + #17778 -- Variable shouldn't resolve RequestContext methods + """ empty_context = Context() - self.assertRaises(VariableDoesNotExist, - Variable('no_such_variable').resolve, empty_context) - self.assertRaises(VariableDoesNotExist, - Variable('new').resolve, empty_context) - self.assertEqual(Variable('new').resolve(Context({'new': 'foo'})), 'foo') + + with self.assertRaises(VariableDoesNotExist): + Variable('no_such_variable').resolve(empty_context) + + with self.assertRaises(VariableDoesNotExist): + Variable('new').resolve(empty_context) + + self.assertEqual( + Variable('new').resolve(Context({'new': 'foo'})), + 'foo', + ) def test_render_context(self): test_context = RenderContext({'fruit': 'papaya'}) @@ -65,11 +73,14 @@ class ContextTests(TestCase): }) def test_context_comparable(self): + """ + #21765 -- equality comparison should work + """ + test_data = {'x': 'y', 'v': 'z', 'd': {'o': object, 'a': 'b'}} self.assertEqual(Context(test_data), Context(test_data)) - # Regression test for #21765 a = Context() b = Context() self.assertEqual(a, b) @@ -88,5 +99,53 @@ class ContextTests(TestCase): self.assertEqual(a, b) def test_copy_request_context_twice(self): - # Regression test for #24273 - this doesn't raise an exception + """ + #24273 -- Copy twice shouldn't raise an exception + """ RequestContext(HttpRequest()).new().new() + + +class RequestContextTests(SimpleTestCase): + + @override_settings(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'OPTIONS': { + 'loaders': [ + ('django.template.loaders.locmem.Loader', { + 'child': '{{ var|default:"none" }}', + }), + ], + }, + }]) + def test_include_only(self): + """ + #15721 -- ``{% include %}`` and ``RequestContext`` should work + together. + """ + request = RequestFactory().get('/') + ctx = RequestContext(request, {'var': 'parent'}) + self.assertEqual(Template('{% include "child" %}').render(ctx), 'parent') + self.assertEqual(Template('{% include "child" only %}').render(ctx), 'none') + + def test_stack_size(self): + """ + #7116 -- Optimize RequetsContext construction + """ + request = RequestFactory().get('/') + ctx = RequestContext(request, {}) + # The stack should now contain 3 items: + # [builtins, supplied context, context processor] + self.assertEqual(len(ctx.dicts), 3) + + def test_context_comparable(self): + # Create an engine without any context processors. + test_data = {'x': 'y', 'v': 'z', 'd': {'o': object, 'a': 'b'}} + + # test comparing RequestContext to prevent problems if somebody + # adds __eq__ in the future + request = RequestFactory().get('/') + + self.assertEqual( + RequestContext(request, dict_=test_data), + RequestContext(request, dict_=test_data), + ) diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py index 62902773d3..9d1055749c 100644 --- a/tests/template_tests/tests.py +++ b/tests/template_tests/tests.py @@ -3,16 +3,14 @@ from __future__ import unicode_literals import os import sys -import unittest -from django import template from django.contrib.auth.models import Group from django.core import urlresolvers from django.template import ( - Context, RequestContext, Template, TemplateSyntaxError, - base as template_base, engines, loader, + Context, Template, TemplateSyntaxError, base as template_base, engines, + loader, ) -from django.test import RequestFactory, SimpleTestCase, override_settings +from django.test import SimpleTestCase, override_settings from django.utils._os import upath TEMPLATES_DIR = os.path.join(os.path.dirname(upath(__file__)), 'templates') @@ -180,54 +178,3 @@ class TemplateRegressionTests(SimpleTestCase): child = engines['django'].from_string( '{% extends parent %}{% block content %}child{% endblock %}') self.assertEqual(child.render({'parent': parent}), 'child') - - -class RequestContextTests(unittest.TestCase): - - def setUp(self): - self.fake_request = RequestFactory().get('/') - - @override_settings(TEMPLATES=[{ - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'OPTIONS': { - 'loaders': [ - ('django.template.loaders.locmem.Loader', { - 'child': '{{ var|default:"none" }}', - }), - ], - }, - }]) - def test_include_only(self): - """ - Regression test for #15721, ``{% include %}`` and ``RequestContext`` - not playing together nicely. - """ - ctx = RequestContext(self.fake_request, {'var': 'parent'}) - self.assertEqual( - template.Template('{% include "child" %}').render(ctx), - 'parent' - ) - self.assertEqual( - template.Template('{% include "child" only %}').render(ctx), - 'none' - ) - - def test_stack_size(self): - """ - Regression test for #7116, Optimize RequetsContext construction - """ - ctx = RequestContext(self.fake_request, {}) - # The stack should now contain 3 items: - # [builtins, supplied context, context processor] - self.assertEqual(len(ctx.dicts), 3) - - def test_context_comparable(self): - test_data = {'x': 'y', 'v': 'z', 'd': {'o': object, 'a': 'b'}} - - # test comparing RequestContext to prevent problems if somebody - # adds __eq__ in the future - request = RequestFactory().get('/') - - self.assertEqual( - RequestContext(request, dict_=test_data), - RequestContext(request, dict_=test_data))