Ticket #5296: easy_view_testing.diff

File easy_view_testing.diff, 6.1 KB (added by Chris H. <chris@…>, 17 years ago)

Additions to TestCase and tests to exercise those additions.

  • django/test/testcases.py

     
    66from django.db.models import get_apps
    77from django.test import _doctest as doctest
    88from django.test.client import Client
     9from django.conf import settings
    910
    1011normalize_long_ints = lambda s: re.sub(r'(?<![\w])(\d+)L(?![\w])', '\\1', s)
    1112
     
    5960        self.client = Client()
    6061        self._pre_setup()
    6162        super(TestCase, self).__call__(result)
    62 
     63   
    6364    def assertRedirects(self, response, expected_path, status_code=302, target_status_code=200):
    6465        """Assert that a response redirected to a specific URL, and that the
    6566        redirect URL can be loaded.
     
    154155            self.assertNotEqual(template_name, response.template.name,
    155156                u"Template '%s' was used unexpectedly in rendering the response" % template_name)
    156157
     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

     
    66from django.newforms.forms import Form
    77from django.newforms import fields
    88from django.shortcuts import render_to_response
     9from django.conf import settings
    910
    1011def get_view(request):
    1112    "A simple view that expects a GET request, and returns a rendered template"
     
    1415   
    1516    return HttpResponse(t.render(c))
    1617
     18def 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
    1724def post_view(request):
    1825    """A view that expects a POST, and returns a different template depending
    1926    on whether any POST data is available
  • tests/modeltests/test_client/urls.py

     
    44
    55urlpatterns = patterns('',
    66    (r'^get_view/$', views.get_view),
     7    (r'^get_xml_view/$', views.get_xml_view),
    78    (r'^post_view/$', views.post_view),
    89    (r'^raw_post_view/$', views.raw_post_view),
    910    (r'^redirect_view/$', views.redirect_view),
  • tests/regressiontests/test_client_regress/models.py

     
    44"""
    55from django.test import Client, TestCase
    66from django.core import mail
     7from django.conf import settings
    78import os
    89
    910class AssertContainsTests(TestCase):
     
    213214        }
    214215        response = self.client.post('/test_client_regress/file_upload/', post_data)
    215216        self.assertEqual(response.status_code, 200)
     217
     218class 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
     242class 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
Back to Top