Author: russellm
Date: 2007-02-10 18:23:31 -0600 (Sat, 10 Feb 2007)
New Revision: 4482

Modified:
   django/trunk/AUTHORS
   django/trunk/django/contrib/auth/forms.py
   django/trunk/django/test/client.py
   django/trunk/docs/testing.txt
   django/trunk/tests/modeltests/test_client/models.py
   django/trunk/tests/modeltests/test_client/urls.py
   django/trunk/tests/modeltests/test_client/views.py
Log:
Fixed #3162 -- Added coded to catch and rethrow exceptions that are thrown by 
the views visited by the test client. Thanks, Ben <[EMAIL PROTECTED]>.


Modified: django/trunk/AUTHORS
===================================================================
--- django/trunk/AUTHORS        2007-02-10 09:07:54 UTC (rev 4481)
+++ django/trunk/AUTHORS        2007-02-11 00:23:31 UTC (rev 4482)
@@ -53,6 +53,7 @@
     Shannon -jj Behrens <http://jjinux.blogspot.com/>
     Esdras Beleza <[EMAIL PROTECTED]>
     James Bennett
+    Ben <[EMAIL PROTECTED]>
     Paul Bissex <http://e-scribe.com/>
     Simon Blanchard
     Andrew Brehaut <http://brehaut.net/blog>

Modified: django/trunk/django/contrib/auth/forms.py
===================================================================
--- django/trunk/django/contrib/auth/forms.py   2007-02-10 09:07:54 UTC (rev 
4481)
+++ django/trunk/django/contrib/auth/forms.py   2007-02-11 00:23:31 UTC (rev 
4482)
@@ -4,6 +4,7 @@
 from django.template import Context, loader
 from django.core import validators
 from django import oldforms
+from django.utils.translation import gettext as _
 
 class UserCreationForm(oldforms.Manipulator):
     "A form that creates a user, with no privileges, from the given username 
and password."

Modified: django/trunk/django/test/client.py
===================================================================
--- django/trunk/django/test/client.py  2007-02-10 09:07:54 UTC (rev 4481)
+++ django/trunk/django/test/client.py  2007-02-11 00:23:31 UTC (rev 4482)
@@ -1,3 +1,4 @@
+import sys
 from cStringIO import StringIO
 from django.conf import settings
 from django.core.handlers.base import BaseHandler
@@ -2,2 +3,3 @@
 from django.core.handlers.wsgi import WSGIRequest
+from django.core.signals import got_request_exception
 from django.dispatch import dispatcher
@@ -100,6 +102,14 @@
         self.defaults = defaults
         self.cookies = SimpleCookie()
         self.session = {}
+        self.exc_info = None
+        
+    def store_exc_info(self, *args, **kwargs):
+        """
+        Utility method that can be used to store exceptions when they are
+        generated by a view.
+        """
+        self.exc_info = sys.exc_info()
 
     def request(self, **request):
         """
@@ -128,6 +138,9 @@
         on_template_render = curry(store_rendered_templates, data)
         dispatcher.connect(on_template_render, 
signal=signals.template_rendered)
 
+        # Capture exceptions created by the handler
+        dispatcher.connect(self.store_exc_info, signal=got_request_exception)
+
         response = self.handler(environ)
 
         # Add any rendered template detail to the response
@@ -142,6 +155,11 @@
             else:
                 setattr(response, detail, None)
 
+        # Look for a signalled exception and reraise it
+        if self.exc_info:
+            raise self.exc_info[1], None, self.exc_info[2]
+        
+        # Update persistent cookie and session data
         if response.cookies:
             self.cookies.update(response.cookies)
 

Modified: django/trunk/docs/testing.txt
===================================================================
--- django/trunk/docs/testing.txt       2007-02-10 09:07:54 UTC (rev 4481)
+++ django/trunk/docs/testing.txt       2007-02-11 00:23:31 UTC (rev 4482)
@@ -291,6 +291,17 @@
 
 .. _RFC2616: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
 
+Exceptions
+~~~~~~~~~~
+
+If you point the Test Client at a view that raises an exception, that exception
+will be visible in the test case. You can then use a standard ``try...catch`` 
+block, or ``unittest.TestCase.assertRaises()`` to test for exceptions.
+
+The only exceptions that are not visible in a Test Case are ``Http404``, 
+``PermissionDenied`` and ``SystemExit``. Django catches these exceptions 
+internally and converts them into the appropriate HTTP responses codes.
+
 Persistent state
 ~~~~~~~~~~~~~~~~
 

Modified: django/trunk/tests/modeltests/test_client/models.py
===================================================================
--- django/trunk/tests/modeltests/test_client/models.py 2007-02-10 09:07:54 UTC 
(rev 4481)
+++ django/trunk/tests/modeltests/test_client/models.py 2007-02-11 00:23:31 UTC 
(rev 4482)
@@ -114,4 +114,14 @@
         
         # Check that the session was modified
         self.assertEquals(self.client.session['tobacconist'], 'hovercraft')
-        
\ No newline at end of file
+
+    def test_view_with_exception(self):
+        "Request a page that is known to throw an error"
+        self.assertRaises(KeyError, self.client.get, 
"/test_client/broken_view/")
+        
+        #Try the same assertion, a different way
+        try:
+            self.client.get('/test_client/broken_view/')
+            self.fail('Should raise an error')
+        except KeyError:
+            pass

Modified: django/trunk/tests/modeltests/test_client/urls.py
===================================================================
--- django/trunk/tests/modeltests/test_client/urls.py   2007-02-10 09:07:54 UTC 
(rev 4481)
+++ django/trunk/tests/modeltests/test_client/urls.py   2007-02-11 00:23:31 UTC 
(rev 4482)
@@ -6,5 +6,6 @@
     (r'^post_view/$', views.post_view),
     (r'^redirect_view/$', views.redirect_view),
     (r'^login_protected_view/$', views.login_protected_view),
-    (r'^session_view/$', views.session_view)
+    (r'^session_view/$', views.session_view),
+    (r'^broken_view/$', views.broken_view)
 )

Modified: django/trunk/tests/modeltests/test_client/views.py
===================================================================
--- django/trunk/tests/modeltests/test_client/views.py  2007-02-10 09:07:54 UTC 
(rev 4481)
+++ django/trunk/tests/modeltests/test_client/views.py  2007-02-11 00:23:31 UTC 
(rev 4482)
@@ -36,11 +36,13 @@
 
 def session_view(request):
     "A view that modifies the session"
-    
     request.session['tobacconist'] = 'hovercraft'
     
     t = Template('This is a view that modifies the session.', 
                  name='Session Modifying View Template')
     c = Context()
     return HttpResponse(t.render(c))
-    
\ No newline at end of file
+
+def broken_view(request):
+    """A view which just raises an exception, simulating a broken view."""
+    raise KeyError("Oops! Looks like you wrote some bad code.")


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to