From bf1bd0fbc9d4920327ad998ad11facf842492e23 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sat, 22 Nov 2014 17:52:12 +0100 Subject: [PATCH] Moved tests for render shortcuts to their own app. --- tests/shortcuts/__init__.py | 0 .../other_templates/render_dirs_test.html | 0 .../templates/shortcuts}/render_test.html | 0 .../test_shortcuts.py => shortcuts/tests.py} | 25 +++--- tests/shortcuts/urls.py | 17 ++++ tests/shortcuts/views.py | 81 +++++++++++++++++++ tests/view_tests/generic_urls.py | 14 ---- tests/view_tests/views.py | 81 +------------------ 8 files changed, 112 insertions(+), 106 deletions(-) create mode 100644 tests/shortcuts/__init__.py rename tests/{view_tests => shortcuts}/other_templates/render_dirs_test.html (100%) rename tests/{view_tests/templates/debug => shortcuts/templates/shortcuts}/render_test.html (100%) rename tests/{view_tests/tests/test_shortcuts.py => shortcuts/tests.py} (75%) create mode 100644 tests/shortcuts/urls.py create mode 100644 tests/shortcuts/views.py diff --git a/tests/shortcuts/__init__.py b/tests/shortcuts/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/view_tests/other_templates/render_dirs_test.html b/tests/shortcuts/other_templates/render_dirs_test.html similarity index 100% rename from tests/view_tests/other_templates/render_dirs_test.html rename to tests/shortcuts/other_templates/render_dirs_test.html diff --git a/tests/view_tests/templates/debug/render_test.html b/tests/shortcuts/templates/shortcuts/render_test.html similarity index 100% rename from tests/view_tests/templates/debug/render_test.html rename to tests/shortcuts/templates/shortcuts/render_test.html diff --git a/tests/view_tests/tests/test_shortcuts.py b/tests/shortcuts/tests.py similarity index 75% rename from tests/view_tests/tests/test_shortcuts.py rename to tests/shortcuts/tests.py index d6a796aff4..4835b40797 100644 --- a/tests/view_tests/tests/test_shortcuts.py +++ b/tests/shortcuts/tests.py @@ -4,67 +4,68 @@ from django.test import TestCase, override_settings @override_settings( TEMPLATE_CONTEXT_PROCESSORS=('django.core.context_processors.static',), STATIC_URL='/path/to/static/media/', - ROOT_URLCONF='view_tests.generic_urls', + ROOT_URLCONF='shortcuts.urls', ) class ShortcutTests(TestCase): 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.content, b'FOO.BAR..\n') self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8') 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.content, b'FOO.BAR../path/to/static/media/\n') self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8') 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.content, b'FOO.BAR..\n') self.assertEqual(response['Content-Type'], 'application/x-rendertest') 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.content, b'spam eggs\n') self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8') def test_render(self): - response = self.client.get('/shortcuts/render/') + response = self.client.get('/render/') self.assertEqual(response.status_code, 200) 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.context.current_app, None) 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.content, b'FOO.BAR..\n') self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8') 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.content, b'FOO.BAR../path/to/static/media/\n') self.assertEqual(response['Content-Type'], 'application/x-rendertest') 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.content, b'FOO.BAR../path/to/static/media/\n') 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") 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.content, b'spam eggs\n') self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8') 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/') diff --git a/tests/shortcuts/urls.py b/tests/shortcuts/urls.py new file mode 100644 index 0000000000..cd18c8a01b --- /dev/null +++ b/tests/shortcuts/urls.py @@ -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), +] diff --git a/tests/shortcuts/views.py b/tests/shortcuts/views.py new file mode 100644 index 0000000000..afae78d4d8 --- /dev/null +++ b/tests/shortcuts/views.py @@ -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)) diff --git a/tests/view_tests/generic_urls.py b/tests/view_tests/generic_urls.py index 1150172ab9..ed15f55486 100644 --- a/tests/view_tests/generic_urls.py +++ b/tests/view_tests/generic_urls.py @@ -44,20 +44,6 @@ urlpatterns += [ 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 urlpatterns += [ url(r'^json/response/$', views.json_response_view), diff --git a/tests/view_tests/views.py b/tests/view_tests/views.py index 003ea76b1f..16bee28248 100644 --- a/tests/view_tests/views.py +++ b/tests/view_tests/views.py @@ -2,26 +2,22 @@ from __future__ import unicode_literals import datetime import decimal -import os import sys from django.core.exceptions import PermissionDenied, SuspiciousOperation from django.core.urlresolvers import get_resolver from django.http import HttpResponse, HttpResponseRedirect, JsonResponse, Http404 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.decorators.debug import (sensitive_post_parameters, sensitive_variables) -from django.utils._os import upath from django.utils.log import getLogger from django.views.generic import View from . import BrokenException, except_args -dirs = (os.path.join(os.path.dirname(upath(__file__)), 'other_templates'),) - def index_page(request): """Dummy index page""" @@ -90,81 +86,6 @@ def template_exception(request, n): def jsi18n(request): 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'): # We need to inspect the HTML generated by the fancy 500 debug view but