Skip to content

Commit

Permalink
Adding basic update test, hard to test put with django for now
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbgk committed May 27, 2010
1 parent b1dbb0b commit eb9b029
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
20 changes: 18 additions & 2 deletions class_based_views/tests/tests.py
Expand Up @@ -129,17 +129,33 @@ def test_invalid_queryset(self):
self.assertRaises(ImproperlyConfigured, self.client.get, '/detail/author/invalid/qs/')

class EditViewTests(TestCase):
fixtures = ['generic-views-test-data.json']
urls = 'class_based_views.tests.urls'

def test_creation(self):
def test_create(self):
res = self.client.get('/edit/authors/create/')
self.assertEqual(res.status_code, 200)
self.assertTemplateUsed(res, 'tests/list.html')

res = self.client.post('/edit/authors/create/',
{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
self.assertEqual(res.status_code, 302)
self.assertEqual(str(Author.objects.all()), "[<Author: Randall Munroe>]")

def test_update(self):
Author.objects.create(**{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
res = self.client.get('/edit/author/1/update/')
self.assertEqual(res.status_code, 200)
self.assertTemplateUsed(res, 'tests/detail.html')

res = self.client.post('/edit/author/1/update/',
{'name': 'Randall Munroe (xkcd)', 'slug': 'randall-munroe'})
self.assertEqual(res.status_code, 302)
self.assertEqual(str(Author.objects.all()), "[<Author: Randall Munroe (xkcd)>]")

#res = self.client.put('/edit/author/1/update/',
# {'name': 'Randall Munroe (xkcd)', 'slug': 'randall-munroe-xkcd'})
#self.assertEqual(res.status_code, 302)
#self.assertEqual(Author.objects.get(id=1).slug, 'randall-munroe-xkcd')


class ArchiveViewTests(TestCase):
Expand Down
2 changes: 1 addition & 1 deletion class_based_views/tests/urls.py
Expand Up @@ -13,7 +13,7 @@

# DetailView
(r'^detail/obj/$', views.ObjectDetail()),
(r'^detail/author/(?P<pk>\d+)/$', views.AuthorDetail()),
url(r'^detail/author/(?P<pk>\d+)/$', views.AuthorDetail(), name="author_detail"),
(r'^detail/author/byslug/(?P<slug>[\w-]+)/$', views.AuthorDetail()),
(r'^detail/author/invalid/url/$', views.AuthorDetail()),
(r'^detail/author/invalid/qs/$', views.AuthorDetail(queryset=None)),
Expand Down
14 changes: 14 additions & 0 deletions class_based_views/tests/views.py
Expand Up @@ -36,6 +36,20 @@ def redirect_to(self, request, obj):

class AuthorUpdate(class_based_views.UpdateView):
queryset = Author.objects.all()
template_name = 'tests/detail.html'
methods = ['GET', 'POST', 'PUT']

def get_form(self, request, obj, *args, **kwargs):
return AuthorForm(request.POST or None)

def process_form(self, request, obj, data):
for k, v in data.iteritems():
setattr(obj, k, v)
obj.save()

def redirect_to(self, request, obj):
return reverse('author_detail', args=[obj.id,])


class ObjectDetail(class_based_views.DetailView):
template_name = 'tests/detail.html'
Expand Down

0 comments on commit eb9b029

Please sign in to comment.