Ticket #27258: test_request_context.py

File test_request_context.py, 1.4 KB (added by Andi Albrecht, 8 years ago)

Example functions to reproduce the bug

Line 
1"""Test script to reproduce a bug with RequestContext."""
2
3from django.test import RequestFactory
4from django.template.backends.django import DjangoTemplates
5from django.template import Template, RequestContext
6
7
8# a simple template
9TEMPLATE_STRING = '{{ name }}'
10
11# a simple context_processer
12def processor_add_name(request):
13 return {'name': 'foo'}
14
15# Django template engine
16ENGINE = DjangoTemplates({
17 'DIRS': [], 'APP_DIRS': False,
18 'NAME': 'django',
19 'OPTIONS': {
20 'context_processors': [processor_add_name],
21 },
22})
23
24
25# testing functions, see run_tests() below
26def test_base_template():
27 request = RequestFactory().get('/')
28 template = Template(TEMPLATE_STRING)
29 context = RequestContext(request, {}, [processor_add_name])
30 return template.render(context)
31
32def test_django_template():
33 request = RequestFactory().get('/')
34 template = ENGINE.from_string(TEMPLATE_STRING)
35 context = RequestContext(request, {})
36 return template.render(context)
37
38def test_django_template_explicit_processors():
39 request = RequestFactory().get('/')
40 template = ENGINE.from_string(TEMPLATE_STRING)
41 context = RequestContext(request, {}, [processor_add_name])
42 return template.render(context)
43
44
45def run_tests():
46 assert test_base_template() == 'foo'
47 # The next two fail, both functions return '' (empty string).
48 assert test_django_template() == 'foo'
49 assert test_django_template_explicit_processors() == 'foo'
Back to Top