Permalink
Browse files

Re-raise the exception when a testcase fails instead of replying with…

… a 500 response.
  • Loading branch information...
1 parent 79bf941 commit a94ed3b434b6975ce9f6381cb9fb5beb03055927 @pelme pelme committed with toastdriven Feb 6, 2012
Showing with 25 additions and 4 deletions.
  1. +5 −0 tastypie/resources.py
  2. +11 −2 tests/basic/api/resources.py
  3. +5 −2 tests/basic/api/urls.py
  4. +4 −0 tests/basic/tests/views.py
@@ -216,6 +216,11 @@ def wrapper(request, *args, **kwargs):
if settings.DEBUG and getattr(settings, 'TASTYPIE_FULL_DEBUG', False):
raise
+ # Re-raise the error to get a proper traceback when the error
+ # happend during a test case
+ if request.META.get('SERVER_NAME') == 'testserver':
+ raise
+
# Rather than re-raising, we're going to things similar to
# what Django does. The difference is returning a serialized
# error message.
@@ -2,7 +2,7 @@
from tastypie import fields
from tastypie.resources import ModelResource
from tastypie.authorization import Authorization
-from basic.models import Note
+from basic.models import Note, AnnotatedNote
class UserResource(ModelResource):
@@ -14,8 +14,17 @@ class Meta:
class NoteResource(ModelResource):
user = fields.ForeignKey(UserResource, 'user')
-
+
class Meta:
resource_name = 'notes'
queryset = Note.objects.all()
authorization = Authorization()
+
+
+class BustedResource(ModelResource):
+ class Meta:
+ queryset = AnnotatedNote.objects.all()
+ resource_name = 'busted'
+
+ def get_list(self, *args, **kwargs):
+ raise Exception("It's broke.")
@@ -1,9 +1,12 @@
from django.conf.urls.defaults import *
from tastypie.api import Api
-from basic.api.resources import NoteResource, UserResource
+from basic.api.resources import NoteResource, UserResource, BustedResource
api = Api(api_name='v1')
api.register(NoteResource(), canonical=True)
api.register(UserResource(), canonical=True)
-urlpatterns = api.urls
+v2_api = Api(api_name='v2')
+v2_api.register(BustedResource(), canonical=True)
+
+urlpatterns = v2_api.urls + api.urls
@@ -35,6 +35,10 @@ def test_gets(self):
self.assertEqual(len(deserialized['objects']), 2)
self.assertEqual([obj['title'] for obj in deserialized['objects']], [u'Another Post', u'First Post!'])
+ def test_get_test_client_error(self):
+ # The test server should re-raise exceptions to make debugging easier.
+ self.assertRaises(Exception, self.client.get, '/api/v2/busted/', data={'format': 'json'})
+
def test_posts(self):
request = HttpRequest()
post_data = '{"content": "A new post.", "is_active": true, "title": "New Title", "slug": "new-title", "user": "/api/v1/users/1/"}'

0 comments on commit a94ed3b

Please sign in to comment.