Ticket #5296: easy_view_testing.diff
File easy_view_testing.diff, 6.1 KB (added by , 17 years ago) |
---|
-
django/test/testcases.py
6 6 from django.db.models import get_apps 7 7 from django.test import _doctest as doctest 8 8 from django.test.client import Client 9 from django.conf import settings 9 10 10 11 normalize_long_ints = lambda s: re.sub(r'(?<![\w])(\d+)L(?![\w])', '\\1', s) 11 12 … … 59 60 self.client = Client() 60 61 self._pre_setup() 61 62 super(TestCase, self).__call__(result) 62 63 63 64 def assertRedirects(self, response, expected_path, status_code=302, target_status_code=200): 64 65 """Assert that a response redirected to a specific URL, and that the 65 66 redirect URL can be loaded. … … 154 155 self.assertNotEqual(template_name, response.template.name, 155 156 u"Template '%s' was used unexpectedly in rendering the response" % template_name) 156 157 158 def assertValidResponse(self, response, content_type="text/html"): 159 "Assert that the response has a 200 status code and is of the proper content type." 160 self.assertEqual(200, response.status_code, 161 u"Expected a 200 status code, but %s was returned" % response.status_code) 162 163 expected_content_type = "%s; charset=%s" % (content_type, settings.DEFAULT_CHARSET) 164 actual_content_type = response.headers['Content-Type'] 165 self.assertEqual(expected_content_type, actual_content_type, 166 u"Expected a Content-Type of %s, but %s was returned" % (expected_content_type, actual_content_type)) 167 168 def check_response(self, response, template_name=None, text_checks=[], content_type="text/html"): 169 """Checks that the response returns a 200, the right content_type, 170 uses the right template, then checks the text of the response. 171 172 Example: 173 def test_my_view(self): 174 text_checks = ( 175 ("List of hello objects", None), 176 ("Hello Object", 3), 177 ) 178 self.check_response('/hello/world/', 'hello/hello_list.html', text_checks) #Assuming text/html and settings.DEFAULT_CHARSET 179 """ 180 self.assertValidResponse(response, content_type) 181 182 if template_name: 183 self.assertTemplateUsed(response, template_name) 184 185 for check in text_checks: 186 text, count = check 187 self.assertContains(response, text, count) 188 No newline at end of file -
tests/modeltests/test_client/views.py
6 6 from django.newforms.forms import Form 7 7 from django.newforms import fields 8 8 from django.shortcuts import render_to_response 9 from django.conf import settings 9 10 10 11 def get_view(request): 11 12 "A simple view that expects a GET request, and returns a rendered template" … … 14 15 15 16 return HttpResponse(t.render(c)) 16 17 18 def get_xml_view(request): 19 "A simple view that expects a GET request and returns a rendered template with a content type of text/xml" 20 response = get_view(request) 21 response.headers['Content-Type'] = "text/xml; charset=%s" % (settings.DEFAULT_CHARSET) 22 return response 23 17 24 def post_view(request): 18 25 """A view that expects a POST, and returns a different template depending 19 26 on whether any POST data is available -
tests/modeltests/test_client/urls.py
4 4 5 5 urlpatterns = patterns('', 6 6 (r'^get_view/$', views.get_view), 7 (r'^get_xml_view/$', views.get_xml_view), 7 8 (r'^post_view/$', views.post_view), 8 9 (r'^raw_post_view/$', views.raw_post_view), 9 10 (r'^redirect_view/$', views.redirect_view), -
tests/regressiontests/test_client_regress/models.py
4 4 """ 5 5 from django.test import Client, TestCase 6 6 from django.core import mail 7 from django.conf import settings 7 8 import os 8 9 9 10 class AssertContainsTests(TestCase): … … 213 214 } 214 215 response = self.client.post('/test_client_regress/file_upload/', post_data) 215 216 self.assertEqual(response.status_code, 200) 217 218 class AssertValidResponseTests(TestCase): 219 def test_html_response(self): 220 response = self.client.get('/test_client/get_view/') 221 self.assertValidResponse(response) 222 223 def test_xml_response(self): 224 response = self.client.get('/test_client/get_xml_view/') 225 self.assertValidResponse(response, "text/xml") 226 227 def test_bad_response(self): 228 response = self.client.get('/test_client/bad_view/') 229 try: 230 self.assertValidResponse(response) 231 except AssertionError, e: 232 self.assertEqual(str(e), "Expected a 200 status code, but 404 was returned") 233 234 def test_bad_content_type(self): 235 response = self.client.get('/test_client/get_view/') 236 try: 237 self.assertValidResponse(response, content_type="text/javascript") 238 except AssertionError, e: 239 charset = settings.DEFAULT_CHARSET 240 self.assertEqual(str(e), "Expected a Content-Type of text/javascript; charset=%s, but text/html; charset=%s was returned" % (charset, charset)) 241 242 class CheckViewTests(TestCase): 243 def test_simple_response(self): 244 response = self.client.get('/test_client/get_view/') 245 text_checks = ( 246 ('This is a test', 1), 247 ('42', 1), 248 ('is', 3), 249 ('This is not a test', 0), 250 ) 251 252 self.check_response(response, template_name='GET Template', text_checks=text_checks) 253 No newline at end of file