Permalink
Browse files

Adding tests for delete

  • Loading branch information...
1 parent c308171 commit e6e12812a22d80286089e3242b4cf0d64ccc641c @davidbgk davidbgk committed May 27, 2010
@@ -30,6 +30,10 @@ class CreateView(ListView, FormView):
class UpdateView(DetailView, FormView):
def put(self, request, obj, *args, **kwargs):
obj = self.get_object(request, *args, **kwargs)
+ # Force evaluation to populate PUT
+ request.POST
+ request.PUT = request._post
+ del request._post
return self.post(request, obj, *args, **kwargs)
@@ -147,6 +147,7 @@ def test_update(self):
self.assertEqual(res.status_code, 200)
self.assertTemplateUsed(res, 'tests/detail.html')
+ # Modification with both POST and PUT (browser compatible)
res = self.client.post('/edit/author/1/update/',
{'name': 'Randall Munroe (xkcd)', 'slug': 'randall-munroe'})
self.assertEqual(res.status_code, 302)
@@ -157,6 +158,22 @@ def test_update(self):
self.assertEqual(res.status_code, 302)
self.assertEqual(Author.objects.get(id=1).slug, 'randall-munroe-xkcd')
+ def test_delete(self):
+ Author.objects.create(**{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
+ res = self.client.get('/edit/author/1/delete/')
+ self.assertEqual(res.status_code, 200)
+ self.assertTemplateUsed(res, 'tests/detail.html')
+
+ # Deletion with both POST and DELETE (browser compatible)
+ res = self.client.post('/edit/author/1/delete/')
+ self.assertEqual(res.status_code, 302)
+ self.assertEqual(str(Author.objects.all()), '[]')
+
+ Author.objects.create(**{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
+ res = self.client.delete('/edit/author/1/delete/')
+ self.assertEqual(res.status_code, 302)
+ self.assertEqual(str(Author.objects.all()), '[]')
+
class ArchiveViewTests(TestCase):
fixtures = ['generic-views-test-data.json']
@@ -21,6 +21,7 @@
# EditView
(r'^edit/authors/create/$', views.AuthorCreate()),
(r'^edit/author/(?P<pk>\d+)/update/$', views.AuthorUpdate()),
+ (r'^edit/author/(?P<pk>\d+)/delete/$', views.AuthorDelete()),
# ArchiveView
(r'^dates/books/$', views.BookArchive()),
@@ -40,10 +40,6 @@ class AuthorUpdate(class_based_views.UpdateView):
methods = ['GET', 'POST', 'PUT']
def get_form(self, request, obj, *args, **kwargs):
- if request.method == "PUT":
- request.POST # force evaluation
- request.PUT = request._post
- del request._post
return AuthorForm(request.REQUEST or None)
def process_form(self, request, obj, data):
@@ -55,6 +51,15 @@ def redirect_to(self, request, obj):
return reverse('author_detail', args=[obj.id,])
+class AuthorDelete(class_based_views.DeleteView):
+ queryset = Author.objects.all()
+ template_name = 'tests/detail.html'
+ methods = ['GET', 'POST', 'DELETE']
+
+ def redirect_to(self, request, obj):
+ return reverse('authors_list')
+
+
class ObjectDetail(class_based_views.DetailView):
template_name = 'tests/detail.html'
def get_object(self, request, **kwargs):

0 comments on commit e6e1281

Please sign in to comment.