Django

Code

Changeset 4482

Show
Ignore:
Timestamp:
02/10/07 18:23:31 (2 years ago)
Author:
russellm
Message:

Fixed #3162 -- Added coded to catch and rethrow exceptions that are thrown by the views visited by the test client. Thanks, Ben <afternoon@uk2.net>.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r4479 r4482  
    5454    Esdras Beleza <linux@esdrasbeleza.com> 
    5555    James Bennett 
     56    Ben <afternoon@uk2.net> 
    5657    Paul Bissex <http://e-scribe.com/> 
    5758    Simon Blanchard 
  • django/trunk/django/contrib/auth/forms.py

    r4266 r4482  
    55from django.core import validators 
    66from django import oldforms 
     7from django.utils.translation import gettext as _ 
    78 
    89class UserCreationForm(oldforms.Manipulator): 
  • django/trunk/django/test/client.py

    r4464 r4482  
     1import sys 
    12from cStringIO import StringIO 
    23from django.conf import settings 
    34from django.core.handlers.base import BaseHandler 
    45from django.core.handlers.wsgi import WSGIRequest 
     6from django.core.signals import got_request_exception 
    57from django.dispatch import dispatcher 
    68from django.http import urlencode, SimpleCookie 
     
    101103        self.cookies = SimpleCookie() 
    102104        self.session = {} 
     105        self.exc_info = None 
     106         
     107    def store_exc_info(self, *args, **kwargs): 
     108        """ 
     109        Utility method that can be used to store exceptions when they are 
     110        generated by a view. 
     111        """ 
     112        self.exc_info = sys.exc_info() 
    103113 
    104114    def request(self, **request): 
     
    128138        on_template_render = curry(store_rendered_templates, data) 
    129139        dispatcher.connect(on_template_render, signal=signals.template_rendered) 
     140 
     141        # Capture exceptions created by the handler 
     142        dispatcher.connect(self.store_exc_info, signal=got_request_exception) 
    130143 
    131144        response = self.handler(environ) 
     
    143156                setattr(response, detail, None) 
    144157 
     158        # Look for a signalled exception and reraise it 
     159        if self.exc_info: 
     160            raise self.exc_info[1], None, self.exc_info[2] 
     161         
     162        # Update persistent cookie and session data 
    145163        if response.cookies: 
    146164            self.cookies.update(response.cookies) 
  • django/trunk/docs/testing.txt

    r4464 r4482  
    291291 
    292292.. _RFC2616: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html 
     293 
     294Exceptions 
     295~~~~~~~~~~ 
     296 
     297If you point the Test Client at a view that raises an exception, that exception 
     298will be visible in the test case. You can then use a standard ``try...catch``  
     299block, or ``unittest.TestCase.assertRaises()`` to test for exceptions. 
     300 
     301The only exceptions that are not visible in a Test Case are ``Http404``,  
     302``PermissionDenied`` and ``SystemExit``. Django catches these exceptions  
     303internally and converts them into the appropriate HTTP responses codes. 
    293304 
    294305Persistent state 
  • django/trunk/tests/modeltests/test_client/models.py

    r4464 r4482  
    115115        # Check that the session was modified 
    116116        self.assertEquals(self.client.session['tobacconist'], 'hovercraft') 
     117 
     118    def test_view_with_exception(self): 
     119        "Request a page that is known to throw an error" 
     120        self.assertRaises(KeyError, self.client.get, "/test_client/broken_view/") 
    117121         
     122        #Try the same assertion, a different way 
     123        try: 
     124            self.client.get('/test_client/broken_view/') 
     125            self.fail('Should raise an error') 
     126        except KeyError: 
     127            pass 
  • django/trunk/tests/modeltests/test_client/urls.py

    r4464 r4482  
    77    (r'^redirect_view/$', views.redirect_view), 
    88    (r'^login_protected_view/$', views.login_protected_view), 
    9     (r'^session_view/$', views.session_view) 
     9    (r'^session_view/$', views.session_view), 
     10    (r'^broken_view/$', views.broken_view) 
    1011) 
  • django/trunk/tests/modeltests/test_client/views.py

    r4464 r4482  
    3737def session_view(request): 
    3838    "A view that modifies the session" 
    39      
    4039    request.session['tobacconist'] = 'hovercraft' 
    4140     
     
    4443    c = Context() 
    4544    return HttpResponse(t.render(c)) 
    46      
     45 
     46def broken_view(request): 
     47    """A view which just raises an exception, simulating a broken view.""" 
     48    raise KeyError("Oops! Looks like you wrote some bad code.")