Moved tests for render shortcuts to their own app.

This commit is contained in:
Aymeric Augustin 2014-11-22 17:52:12 +01:00
parent b38637d581
commit bf1bd0fbc9
8 changed files with 112 additions and 106 deletions

View File

View File

@ -4,67 +4,68 @@ from django.test import TestCase, override_settings
@override_settings( @override_settings(
TEMPLATE_CONTEXT_PROCESSORS=('django.core.context_processors.static',), TEMPLATE_CONTEXT_PROCESSORS=('django.core.context_processors.static',),
STATIC_URL='/path/to/static/media/', STATIC_URL='/path/to/static/media/',
ROOT_URLCONF='view_tests.generic_urls', ROOT_URLCONF='shortcuts.urls',
) )
class ShortcutTests(TestCase): class ShortcutTests(TestCase):
def test_render_to_response(self): def test_render_to_response(self):
response = self.client.get('/shortcuts/render_to_response/') response = self.client.get('/render_to_response/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'FOO.BAR..\n') self.assertEqual(response.content, b'FOO.BAR..\n')
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8') self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
def test_render_to_response_with_request_context(self): def test_render_to_response_with_request_context(self):
response = self.client.get('/shortcuts/render_to_response/request_context/') response = self.client.get('/render_to_response/request_context/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'FOO.BAR../path/to/static/media/\n') self.assertEqual(response.content, b'FOO.BAR../path/to/static/media/\n')
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8') self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
def test_render_to_response_with_content_type(self): def test_render_to_response_with_content_type(self):
response = self.client.get('/shortcuts/render_to_response/content_type/') response = self.client.get('/render_to_response/content_type/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'FOO.BAR..\n') self.assertEqual(response.content, b'FOO.BAR..\n')
self.assertEqual(response['Content-Type'], 'application/x-rendertest') self.assertEqual(response['Content-Type'], 'application/x-rendertest')
def test_render_to_response_with_dirs(self): def test_render_to_response_with_dirs(self):
response = self.client.get('/shortcuts/render_to_response/dirs/') response = self.client.get('/render_to_response/dirs/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'spam eggs\n') self.assertEqual(response.content, b'spam eggs\n')
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8') self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
def test_render(self): def test_render(self):
response = self.client.get('/shortcuts/render/') response = self.client.get('/render/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'FOO.BAR../path/to/static/media/\n') self.assertEqual(response.content, b'FOO.BAR../path/to/static/media/\n')
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8') self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
self.assertEqual(response.context.current_app, None) self.assertEqual(response.context.current_app, None)
def test_render_with_base_context(self): def test_render_with_base_context(self):
response = self.client.get('/shortcuts/render/base_context/') response = self.client.get('/render/base_context/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'FOO.BAR..\n') self.assertEqual(response.content, b'FOO.BAR..\n')
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8') self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
def test_render_with_content_type(self): def test_render_with_content_type(self):
response = self.client.get('/shortcuts/render/content_type/') response = self.client.get('/render/content_type/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'FOO.BAR../path/to/static/media/\n') self.assertEqual(response.content, b'FOO.BAR../path/to/static/media/\n')
self.assertEqual(response['Content-Type'], 'application/x-rendertest') self.assertEqual(response['Content-Type'], 'application/x-rendertest')
def test_render_with_status(self): def test_render_with_status(self):
response = self.client.get('/shortcuts/render/status/') response = self.client.get('/render/status/')
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
self.assertEqual(response.content, b'FOO.BAR../path/to/static/media/\n') self.assertEqual(response.content, b'FOO.BAR../path/to/static/media/\n')
def test_render_with_current_app(self): def test_render_with_current_app(self):
response = self.client.get('/shortcuts/render/current_app/') response = self.client.get('/render/current_app/')
self.assertEqual(response.context.current_app, "foobar_app") self.assertEqual(response.context.current_app, "foobar_app")
def test_render_with_dirs(self): def test_render_with_dirs(self):
response = self.client.get('/shortcuts/render/dirs/') response = self.client.get('/render/dirs/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'spam eggs\n') self.assertEqual(response.content, b'spam eggs\n')
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8') self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
def test_render_with_current_app_conflict(self): def test_render_with_current_app_conflict(self):
self.assertRaises(ValueError, self.client.get, '/shortcuts/render/current_app_conflict/') with self.assertRaises(ValueError):
self.client.get('/render/current_app_conflict/')

17
tests/shortcuts/urls.py Normal file
View File

@ -0,0 +1,17 @@
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^render_to_response/$', views.render_to_response_view),
url(r'^render_to_response/request_context/$', views.render_to_response_view_with_request_context),
url(r'^render_to_response/content_type/$', views.render_to_response_view_with_content_type),
url(r'^render_to_response/dirs/$', views.render_to_response_view_with_dirs),
url(r'^render/$', views.render_view),
url(r'^render/base_context/$', views.render_view_with_base_context),
url(r'^render/content_type/$', views.render_view_with_content_type),
url(r'^render/dirs/$', views.render_with_dirs),
url(r'^render/status/$', views.render_view_with_status),
url(r'^render/current_app/$', views.render_view_with_current_app),
url(r'^render/current_app_conflict/$', views.render_view_with_current_app_conflict),
]

81
tests/shortcuts/views.py Normal file
View File

@ -0,0 +1,81 @@
import os.path
from django.shortcuts import render_to_response, render
from django.template import Context, RequestContext
from django.utils._os import upath
dirs = (os.path.join(os.path.dirname(upath(__file__)), 'other_templates'),)
def render_to_response_view(request):
return render_to_response('shortcuts/render_test.html', {
'foo': 'FOO',
'bar': 'BAR',
})
def render_to_response_view_with_request_context(request):
return render_to_response('shortcuts/render_test.html', {
'foo': 'FOO',
'bar': 'BAR',
}, context_instance=RequestContext(request))
def render_to_response_view_with_content_type(request):
return render_to_response('shortcuts/render_test.html', {
'foo': 'FOO',
'bar': 'BAR',
}, content_type='application/x-rendertest')
def render_to_response_view_with_dirs(request):
return render_to_response('render_dirs_test.html', dirs=dirs)
def render_view(request):
return render(request, 'shortcuts/render_test.html', {
'foo': 'FOO',
'bar': 'BAR',
})
def render_view_with_base_context(request):
return render(request, 'shortcuts/render_test.html', {
'foo': 'FOO',
'bar': 'BAR',
}, context_instance=Context())
def render_view_with_content_type(request):
return render(request, 'shortcuts/render_test.html', {
'foo': 'FOO',
'bar': 'BAR',
}, content_type='application/x-rendertest')
def render_with_dirs(request):
return render(request, 'render_dirs_test.html', dirs=dirs)
def render_view_with_status(request):
return render(request, 'shortcuts/render_test.html', {
'foo': 'FOO',
'bar': 'BAR',
}, status=403)
def render_view_with_current_app(request):
return render(request, 'shortcuts/render_test.html', {
'foo': 'FOO',
'bar': 'BAR',
}, current_app="foobar_app")
def render_view_with_current_app_conflict(request):
# This should fail because we don't passing both a current_app and
# context_instance:
return render(request, 'shortcuts/render_test.html', {
'foo': 'FOO',
'bar': 'BAR',
}, current_app="foobar_app", context_instance=RequestContext(request))

View File

@ -44,20 +44,6 @@ urlpatterns += [
url='/中文/target/', permanent=True)), url='/中文/target/', permanent=True)),
] ]
urlpatterns += [
url(r'^shortcuts/render_to_response/$', views.render_to_response_view),
url(r'^shortcuts/render_to_response/request_context/$', views.render_to_response_view_with_request_context),
url(r'^shortcuts/render_to_response/content_type/$', views.render_to_response_view_with_content_type),
url(r'^shortcuts/render_to_response/dirs/$', views.render_to_response_view_with_dirs),
url(r'^shortcuts/render/$', views.render_view),
url(r'^shortcuts/render/base_context/$', views.render_view_with_base_context),
url(r'^shortcuts/render/content_type/$', views.render_view_with_content_type),
url(r'^shortcuts/render/status/$', views.render_view_with_status),
url(r'^shortcuts/render/current_app/$', views.render_view_with_current_app),
url(r'^shortcuts/render/dirs/$', views.render_with_dirs),
url(r'^shortcuts/render/current_app_conflict/$', views.render_view_with_current_app_conflict),
]
# json response # json response
urlpatterns += [ urlpatterns += [
url(r'^json/response/$', views.json_response_view), url(r'^json/response/$', views.json_response_view),

View File

@ -2,26 +2,22 @@ from __future__ import unicode_literals
import datetime import datetime
import decimal import decimal
import os
import sys import sys
from django.core.exceptions import PermissionDenied, SuspiciousOperation from django.core.exceptions import PermissionDenied, SuspiciousOperation
from django.core.urlresolvers import get_resolver from django.core.urlresolvers import get_resolver
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse, Http404 from django.http import HttpResponse, HttpResponseRedirect, JsonResponse, Http404
from django.shortcuts import render_to_response, render from django.shortcuts import render_to_response, render
from django.template import Context, RequestContext, TemplateDoesNotExist from django.template import TemplateDoesNotExist
from django.views.debug import technical_500_response, SafeExceptionReporterFilter from django.views.debug import technical_500_response, SafeExceptionReporterFilter
from django.views.decorators.debug import (sensitive_post_parameters, from django.views.decorators.debug import (sensitive_post_parameters,
sensitive_variables) sensitive_variables)
from django.utils._os import upath
from django.utils.log import getLogger from django.utils.log import getLogger
from django.views.generic import View from django.views.generic import View
from . import BrokenException, except_args from . import BrokenException, except_args
dirs = (os.path.join(os.path.dirname(upath(__file__)), 'other_templates'),)
def index_page(request): def index_page(request):
"""Dummy index page""" """Dummy index page"""
@ -90,81 +86,6 @@ def template_exception(request, n):
def jsi18n(request): def jsi18n(request):
return render_to_response('jsi18n.html') return render_to_response('jsi18n.html')
# Some views to exercise the shortcuts
def render_to_response_view(request):
return render_to_response('debug/render_test.html', {
'foo': 'FOO',
'bar': 'BAR',
})
def render_to_response_view_with_request_context(request):
return render_to_response('debug/render_test.html', {
'foo': 'FOO',
'bar': 'BAR',
}, context_instance=RequestContext(request))
def render_to_response_view_with_content_type(request):
return render_to_response('debug/render_test.html', {
'foo': 'FOO',
'bar': 'BAR',
}, content_type='application/x-rendertest')
def render_to_response_view_with_dirs(request):
return render_to_response('render_dirs_test.html', dirs=dirs)
def render_view(request):
return render(request, 'debug/render_test.html', {
'foo': 'FOO',
'bar': 'BAR',
})
def render_view_with_base_context(request):
return render(request, 'debug/render_test.html', {
'foo': 'FOO',
'bar': 'BAR',
}, context_instance=Context())
def render_view_with_content_type(request):
return render(request, 'debug/render_test.html', {
'foo': 'FOO',
'bar': 'BAR',
}, content_type='application/x-rendertest')
def render_view_with_status(request):
return render(request, 'debug/render_test.html', {
'foo': 'FOO',
'bar': 'BAR',
}, status=403)
def render_view_with_current_app(request):
return render(request, 'debug/render_test.html', {
'foo': 'FOO',
'bar': 'BAR',
}, current_app="foobar_app")
def render_view_with_current_app_conflict(request):
# This should fail because we don't passing both a current_app and
# context_instance:
return render(request, 'debug/render_test.html', {
'foo': 'FOO',
'bar': 'BAR',
}, current_app="foobar_app", context_instance=RequestContext(request))
def render_with_dirs(request):
return render(request, 'render_dirs_test.html', dirs=dirs)
def raises_template_does_not_exist(request, path='i_dont_exist.html'): def raises_template_does_not_exist(request, path='i_dont_exist.html'):
# We need to inspect the HTML generated by the fancy 500 debug view but # We need to inspect the HTML generated by the fancy 500 debug view but