diff options
Diffstat (limited to 'tests/template_tests/test_context.py')
| -rw-r--r-- | tests/template_tests/test_context.py | 83 |
1 files changed, 71 insertions, 12 deletions
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(SimpleTestCase): -class ContextTests(TestCase): 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), + ) |
