Django

Code

Changeset 5183

Show
Ignore:
Timestamp:
05/10/07 10:51:59 (2 years ago)
Author:
bouldersprinters
Message:

boulder-oracle-sprint: Merged to [5182]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/boulder-oracle-sprint/django/test/simple.py

    r4855 r5183  
    5050        else: 
    5151            # The module exists, so there must be an import error in the  
    52             # test module itself. We don't need the module; close the file 
    53             # handle returned by find_module. 
    54             mod[0].close() 
     52            # test module itself. We don't need the module; so if the 
     53            # module was a single file module (i.e., tests.py), close the file 
     54            # handle returned by find_module. Otherwise, the test module 
     55            # is a directory, and there is nothing to close. 
     56            if mod[0]: 
     57                mod[0].close() 
    5558            raise 
    5659             
  • django/branches/boulder-oracle-sprint/django/test/testcases.py

    r5174 r5183  
    5757        super(TestCase, self).run(result) 
    5858 
    59     def assertRedirects(self, response, expected_path): 
    60         """Assert that a response redirected to a specific URL, and that the  
     59    def assertRedirects(self, response, expected_path, status_code=302, target_status_code=200): 
     60        """Assert that a response redirected to a specific URL, and that the 
    6161        redirect URL can be loaded. 
    6262         
    6363        """ 
    64         self.assertEqual(response.status_code, 302,  
    65             "Response didn't redirect: Reponse code was %d" % response.status_code) 
     64        self.assertEqual(response.status_code, status_code,  
     65            "Response didn't redirect: Reponse code was %d (expected %d)" %  
     66                (response.status_code, status_code)) 
    6667        scheme, netloc, path, params, query, fragment = urlparse(response['Location']) 
    6768        self.assertEqual(path, expected_path,  
    6869            "Response redirected to '%s', expected '%s'" % (path, expected_path)) 
    6970        redirect_response = self.client.get(path) 
    70         self.assertEqual(redirect_response.status_code, 200,  
    71             "Couldn't retrieve redirection page '%s'" % path) 
     71        self.assertEqual(redirect_response.status_code, target_status_code,  
     72            "Couldn't retrieve redirection page '%s': response code was %d (expected %d)" %  
     73                (path, response.status_code, status_code)) 
    7274     
    73     def assertContains(self, response, text, count=1): 
     75    def assertContains(self, response, text, count=1, status_code=200): 
    7476        """Assert that a response indicates that a page was retreived successfully, 
    75         (i.e., the HTTP status code was 200), and that ``text`` occurs ``count``  
     77        (i.e., the HTTP status code was as expected), and that ``text`` occurs ``count`` 
    7678        times in the content of the response. 
    7779         
    7880        """ 
    79         self.assertEqual(response.status_code, 200, 
    80             "Couldn't retrieve page'") 
     81        self.assertEqual(response.status_code, status_code, 
     82            "Couldn't retrieve page: Response code was %d (expected %d)'" %  
     83                (response.status_code, status_code)) 
    8184        real_count = response.content.count(text) 
    8285        self.assertEqual(real_count, count, 
    83             "Could only find %d of %d instances of '%s' in response" % (real_count, count, text)) 
    84  
     86            "Found %d instances of '%s' in response (expected %d)" % (real_count, text, count)) 
     87     
    8588    def assertFormError(self, response, form, field, errors): 
    8689        "Assert that a form used to render the response has a specific field error" 
     
    103106            if form in context: 
    104107                found_form = True 
    105                 try
    106                     for err in errors
    107                         if field
     108                for err in errors
     109                    if field
     110                        if field in context[form].errors
    108111                            self.assertTrue(err in context[form].errors[field],  
    109                                 "The field '%s' on form '%s' in context %d does not contain the error '%s' (actual errors: %s)" %  
    110                                     (field, form, i, err, list(context[form].errors[field]))) 
     112                            "The field '%s' on form '%s' in context %d does not contain the error '%s' (actual errors: %s)" %  
     113                                (field, form, i, err, list(context[form].errors[field]))) 
     114                        elif field in context[form].fields: 
     115                            self.fail("The field '%s' on form '%s' in context %d contains no errors" %  
     116                                (field, form, i)) 
    111117                        else: 
    112                             self.assertTrue(err in context[form].non_field_errors(),  
    113                                 "The form '%s' in context %d does not contain the non-field error '%s' (actual errors: %s)" %  
    114                                     (form, i, err, list(context[form].non_field_errors()))) 
    115                 except KeyError: 
    116                     self.fail("The form '%s' in context %d does not contain the field '%s'" % (form, i, field)) 
     118                            self.fail("The form '%s' in context %d does not contain the field '%s'" % (form, i, field)) 
     119                    else: 
     120                        self.assertTrue(err in context[form].non_field_errors(),  
     121                            "The form '%s' in context %d does not contain the non-field error '%s' (actual errors: %s)" %  
     122                                (form, i, err, list(context[form].non_field_errors()))) 
    117123        if not found_form: 
    118124            self.fail("The form '%s' was not used to render the response" % form) 
  • django/branches/boulder-oracle-sprint/docs/testing.txt

    r5174 r5183  
    473473that can be useful in testing the behavior of web sites. 
    474474 
    475 ``assertContains(response, text, count=1)`` 
    476     Assert that a response indicates that a page was retrieved successfully, 
    477     (i.e., the HTTP status code was 200), and that ``text`` occurs ``count`` 
     475``assertContains(response, text, count=1, status_code=200)`` 
     476    Assert that a response indicates that a page could be retrieved and 
     477    produced the nominated status code, and that ``text`` occurs ``count`` 
    478478    times in the content of the response. 
    479479 
     
    494494    the response. 
    495495 
    496 ``assertRedirects(response, expected_path)`` 
    497     Assert that the response received redirects the browser to the provided 
    498     path, and that the expected_path can be retrieved. 
     496``assertRedirects(response, expected_path, status_code=302, target_status_code=200)`` 
     497    Assert that the response received produced the nominated status code, 
     498    redirects the browser to the provided path, and that retrieving the provided 
     499    path yields a response with the target status code. 
    499500 
    500501``assertTemplateUsed(response, template_name)`` 
  • django/branches/boulder-oracle-sprint/tests/modeltests/test_client/models.py

    r5174 r5183  
    3535        self.assertEqual(response.template.name, 'GET Template') 
    3636 
    37     def test_no_template_view(self): 
    38         "Check that template usage assersions work then templates aren't in use" 
    39         response = self.client.get('/test_client/no_template_view/') 
    40  
    41         # Check that the no template case doesn't mess with the template assertions 
    42         self.assertTemplateNotUsed(response, 'GET Template') 
    43          
    4437    def test_get_post_view(self): 
    4538        "GET a view that normally expects POSTs" 
     
    7669         
    7770    def test_raw_post(self): 
     71        "POST raw data (with a content type) to a view" 
    7872        test_doc = """<?xml version="1.0" encoding="utf-8"?><library><book><title>Blink</title><author>Malcolm Gladwell</author></book></library>""" 
    7973        response = self.client.post("/test_client/raw_post_view/", test_doc, 
     
    8983        # Check that the response was a 302 (redirect) 
    9084        self.assertRedirects(response, '/test_client/get_view/') 
     85 
     86    def test_permanent_redirect(self): 
     87        "GET a URL that redirects permanently elsewhere" 
     88        response = self.client.get('/test_client/permanent_redirect_view/') 
     89         
     90        # Check that the response was a 301 (permanent redirect) 
     91        self.assertRedirects(response, '/test_client/get_view/', status_code=301) 
     92 
     93    def test_redirect_to_strange_location(self): 
     94        "GET a URL that redirects to a non-200 page" 
     95        response = self.client.get('/test_client/double_redirect_view/') 
     96         
     97        # Check that the response was a 302, and that 
     98        # the attempt to get the redirection location returned 301 when retrieved 
     99        self.assertRedirects(response, '/test_client/permanent_redirect_view/', target_status_code=301) 
     100 
     101    def test_notfound_response(self): 
     102        "GET a URL that responds as '404:Not Found'" 
     103        response = self.client.get('/test_client/bad_view/') 
     104         
     105        # Check that the response was a 404, and that the content contains MAGIC 
     106        self.assertContains(response, 'MAGIC', status_code=404) 
    91107 
    92108    def test_valid_form(self): 
  • django/branches/boulder-oracle-sprint/tests/modeltests/test_client/urls.py

    r5174 r5183  
    11from django.conf.urls.defaults import * 
     2from django.views.generic.simple import redirect_to 
    23import views 
    34 
    45urlpatterns = patterns('', 
    5     (r'^no_template_view/$', views.no_template_view), 
    66    (r'^get_view/$', views.get_view), 
    77    (r'^post_view/$', views.post_view), 
    88    (r'^raw_post_view/$', views.raw_post_view), 
    99    (r'^redirect_view/$', views.redirect_view), 
     10    (r'^permanent_redirect_view/$', redirect_to, { 'url': '/test_client/get_view/' }), 
     11    (r'^double_redirect_view/$', views.double_redirect_view), 
     12    (r'^bad_view/$', views.bad_view), 
    1013    (r'^form_view/$', views.form_view), 
    1114    (r'^form_view_with_template/$', views.form_view_with_template), 
  • django/branches/boulder-oracle-sprint/tests/modeltests/test_client/views.py

    r5174 r5183  
    22from django.core.mail import EmailMessage, SMTPConnection 
    33from django.template import Context, Template 
    4 from django.http import HttpResponse, HttpResponseRedirect 
     4from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound 
    55from django.contrib.auth.decorators import login_required 
    66from django.newforms.forms import Form 
    77from django.newforms import fields 
    88from django.shortcuts import render_to_response 
    9  
    10 def no_template_view(request): 
    11     "A simple view that expects a GET request, and returns a rendered template" 
    12     return HttpResponse("No template used") 
    139 
    1410def get_view(request): 
     
    5450    "A view that redirects all requests to the GET view" 
    5551    return HttpResponseRedirect('/test_client/get_view/') 
     52 
     53def double_redirect_view(request): 
     54    "A view that redirects all requests to a redirection view" 
     55    return HttpResponseRedirect('/test_client/permanent_redirect_view/') 
     56 
     57def bad_view(request): 
     58    "A view that returns a 404 with some error content" 
     59    return HttpResponseNotFound('Not found!. This page contains some MAGIC content') 
    5660 
    5761TestChoices = ( 
  • django/branches/boulder-oracle-sprint/tests/urls.py

    r4695 r5183  
    44    # test_client modeltest urls 
    55    (r'^test_client/', include('modeltests.test_client.urls')), 
     6    (r'^test_client_regress/', include('regressiontests.test_client_regress.urls')), 
    67 
    78    # Always provide the auth system login and logout views