mirror of
https://github.com/django/django.git
synced 2024-12-28 12:06:22 +00:00
[1.8.x] Moved RequestContextTests into test_context.
Backport of 250a3d1993
from master
This commit is contained in:
parent
c1bf9e47ec
commit
d8114552a0
@ -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),
|
||||
)
|
||||
|
@ -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))
|
||||
|
Loading…
Reference in New Issue
Block a user