Django

Code

Changeset 6044

Show
Ignore:
Timestamp:
09/03/07 19:50:06 (1 year ago)
Author:
gwilson
Message:

Cleaned up some imports.
Fixed long lines.
Changed some docstrings to use "action words".

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/test/testcases.py

    r6041 r6044  
    1 import re, unittest 
     1import re 
     2import unittest 
    23from urlparse import urlsplit 
     4 
    35from django.http import QueryDict 
    46from django.db import transaction 
    57from django.core import mail 
    68from django.core.management import call_command 
    7 from django.db.models import get_apps 
    89from django.test import _doctest as doctest 
    910from django.test.client import Client 
     
    3435            return normalize_long_ints(want) == normalize_long_ints(got) 
    3536        return ok 
    36                    
     37 
    3738class DocTestRunner(doctest.DocTestRunner): 
    3839    def __init__(self, *args, **kwargs): 
    3940        doctest.DocTestRunner.__init__(self, *args, **kwargs) 
    4041        self.optionflags = doctest.ELLIPSIS 
    41          
     42 
    4243    def report_unexpected_exception(self, out, test, example, exc_info): 
    43         doctest.DocTestRunner.report_unexpected_exception(self,out,test,example,exc_info) 
    44          
     44        doctest.DocTestRunner.report_unexpected_exception(self, out, test, 
     45                                                          example, exc_info) 
    4546        # Rollback, in case of database errors. Otherwise they'd have 
    4647        # side effects on other tests. 
    47         from django.db import transaction 
    4848        transaction.rollback_unless_managed() 
    4949 
    50 class TestCase(unittest.TestCase):     
     50class TestCase(unittest.TestCase): 
    5151    def _pre_setup(self): 
    52         """Perform any pre-test setup. This includes: 
    53          
    54             * If the Test Case class has a 'fixtures' member, clearing the  
    55             database and installing the named fixtures at the start of each test. 
     52        """Performs any pre-test setup. This includes: 
     53 
     54            * If the Test Case class has a 'fixtures' member, clearing the 
     55              database and installing the named fixtures at the start of each 
     56              test. 
    5657            * Clearing the mail test outbox. 
    57              
    5858        """ 
    5959        call_command('flush', verbosity=0, interactive=False) 
     
    7474        super(TestCase, self).__call__(result) 
    7575 
    76     def assertRedirects(self, response, expected_url, status_code=302, target_status_code=200): 
    77         """Assert that a response redirected to a specific URL, and that the 
     76    def assertRedirects(self, response, expected_url, status_code=302, 
     77                        target_status_code=200): 
     78        """Asserts that a response redirected to a specific URL, and that the 
    7879        redirect URL can be loaded. 
    79          
    80         Note that assertRedirects won't work for external links since it uses  
     80 
     81        Note that assertRedirects won't work for external links since it uses 
    8182        TestClient to do a request. 
    8283        """ 
    83         self.assertEqual(response.status_code, status_code,  
    84             "Response didn't redirect as expected: Response code was %d (expected %d)" %  
    85                 (response.status_code, status_code)) 
     84        self.assertEqual(response.status_code, status_code, 
     85            ("Response didn't redirect as expected: Response code was %d" 
     86             " (expected %d)" % (response.status_code, status_code))) 
    8687        scheme, netloc, path, query, fragment = urlsplit(response['Location']) 
    8788        url = path 
     
    9091        if fragment: 
    9192            url += '#' + fragment 
    92         self.assertEqual(url, expected_url,  
     93        self.assertEqual(url, expected_url, 
    9394            "Response redirected to '%s', expected '%s'" % (url, expected_url)) 
    94          
     95 
    9596        # Get the redirection page, using the same client that was used 
    9697        # to obtain the original response. 
    9798        redirect_response = response.client.get(path, QueryDict(query)) 
    98         self.assertEqual(redirect_response.status_code, target_status_code,  
    99             "Couldn't retrieve redirection page '%s': response code was %d (expected %d)" %  
    100                 (path, redirect_response.status_code, target_status_code)) 
    101      
     99        self.assertEqual(redirect_response.status_code, target_status_code, 
     100            ("Couldn't retrieve redirection page '%s': response code was %d" 
     101             " (expected %d)") % 
     102                 (path, redirect_response.status_code, target_status_code)) 
     103 
    102104    def assertContains(self, response, text, count=None, status_code=200): 
    103         """Assert that a response indicates that a page was retreived successfully, 
    104         (i.e., the HTTP status code was as expected), and that ``text`` occurs ``count`` 
    105         times in the content of the response. If ``count`` is None, the count doesn't 
    106         matter - the assertion is true if the text occurs at least once in the response. 
    107          
     105        """ 
     106        Asserts that a response indicates that a page was retreived 
     107        successfully, (i.e., the HTTP status code was as expected), and that 
     108        ``text`` occurs ``count`` times in the content of the response. 
     109        If ``count`` is None, the count doesn't matter - the assertion is true 
     110        if the text occurs at least once in the response. 
    108111        """ 
    109112        self.assertEqual(response.status_code, status_code, 
    110             "Couldn't retrieve page: Response code was %d (expected %d)'" %  
     113            "Couldn't retrieve page: Response code was %d (expected %d)'" % 
    111114                (response.status_code, status_code)) 
    112115        real_count = response.content.count(text) 
    113116        if count is not None: 
    114117            self.assertEqual(real_count, count, 
    115                 "Found %d instances of '%s' in response (expected %d)" % (real_count, text, count)) 
     118                "Found %d instances of '%s' in response (expected %d)" % 
     119                    (real_count, text, count)) 
    116120        else: 
    117             self.failUnless(real_count != 0, "Couldn't find '%s' in response" % text) 
    118                  
     121            self.failUnless(real_count != 0, 
     122                            "Couldn't find '%s' in response" % text) 
     123 
    119124    def assertFormError(self, response, form, field, errors): 
    120         "Assert that a form used to render the response has a specific field error" 
     125        """ 
     126        Asserts that a form used to render the response has a specific field 
     127        error. 
     128        """ 
    121129        # Put context(s) into a list to simplify processing. 
    122         contexts = to_list(response.context)  
     130        contexts = to_list(response.context) 
    123131        if not contexts: 
    124132            self.fail('Response did not use any contexts to render the' 
     
    127135        # Put error(s) into a list to simplify processing. 
    128136        errors = to_list(errors) 
    129          
     137 
    130138        # Search all contexts for the error. 
    131139        found_form = False 
    132140        for i,context in enumerate(contexts): 
    133             if form in context: 
    134                 found_form = True 
    135                 for err in errors: 
    136                     if field: 
    137                         if field in context[form].errors: 
    138                             self.failUnless(err in context[form].errors[field],  
    139                             "The field '%s' on form '%s' in context %d does not contain the error '%s' (actual errors: %s)" %  
    140                                 (field, form, i, err, list(context[form].errors[field]))) 
    141                         elif field in context[form].fields: 
    142                             self.fail("The field '%s' on form '%s' in context %d contains no errors" %  
    143                                 (field, form, i)) 
    144                         else: 
    145                             self.fail("The form '%s' in context %d does not contain the field '%s'" % (form, i, field)) 
     141            if form not in context: 
     142                continue 
     143            found_form = True 
     144            for err in errors: 
     145                if field: 
     146                    if field in context[form].errors: 
     147                        field_errors = context[form].errors[field] 
     148                        self.failUnless(err in field_errors, 
     149                                        "The field '%s' on form '%s' in" 
     150                                        " context %d does not contain the" 
     151                                        " error '%s' (actual errors: %s)" % 
     152                                            (field, form, i, err, 
     153                                             list(field_errors))) 
     154                    elif field in context[form].fields: 
     155                        self.fail("The field '%s' on form '%s' in context %d" 
     156                                  " contains no errors" % (field, form, i)) 
    146157                    else: 
    147                         self.failUnless(err in context[form].non_field_errors(),  
    148                             "The form '%s' in context %d does not contain the non-field error '%s' (actual errors: %s)" %  
    149                                 (form, i, err, list(context[form].non_field_errors()))) 
     158                        self.fail("The form '%s' in context %d does not" 
     159                                  " contain the field '%s'" % 
     160                                      (form, i, field)) 
     161                else: 
     162                    non_field_errors = context[form].non_field_errors() 
     163                    self.failUnless(err in non_field_errors, 
     164                        "The form '%s' in context %d does not contain the" 
     165                        " non-field error '%s' (actual errors: %s)" % 
     166                            (form, i, err, non_field_errors)) 
    150167        if not found_form: 
    151             self.fail("The form '%s' was not used to render the response" % form) 
    152              
     168            self.fail("The form '%s' was not used to render the response" % 
     169                          form) 
     170 
    153171    def assertTemplateUsed(self, response, template_name): 
    154         "Assert that the template with the provided name was used in rendering the response" 
     172        """ 
     173        Asserts that the template with the provided name was used in rendering 
     174        the response. 
     175        """ 
    155176        template_names = [t.name for t in to_list(response.template)] 
    156177        if not template_names: 
     
    158179        self.failUnless(template_name in template_names, 
    159180            (u"Template '%s' was not a template used to render the response." 
    160               " Actual template(s) used: %s") % (template_name, 
     181             u" Actual template(s) used: %s") % (template_name, 
    161182                                                 u', '.join(template_names))) 
    162183 
    163184    def assertTemplateNotUsed(self, response, template_name): 
    164         "Assert that the template with the provided name was NOT used in rendering the response" 
     185        """ 
     186        Asserts that the template with the provided name was NOT used in 
     187        rendering the response. 
     188        """ 
    165189        template_names = [t.name for t in to_list(response.template)] 
    166190        self.failIf(template_name in template_names, 
    167191            (u"Template '%s' was used unexpectedly in rendering the" 
    168               " response") % template_name) 
     192             u" response") % template_name) 
  • django/trunk/tests/modeltests/test_client/views.py

    r6031 r6044  
    11from xml.dom.minidom import parseString 
     2 
    23from django.core.mail import EmailMessage, SMTPConnection 
    34from django.template import Context, Template 
     
    56from django.contrib.auth.decorators import login_required 
    67from django.newforms.forms import Form 
    7 from django.newforms import fields 
     8from django.newforms import fields, ValidationError 
    89from django.shortcuts import render_to_response 
    910