From 0853316545ad39c314f56ee559ce56596e578d2b Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 28 Sep 2012 16:23:46 +0100 Subject: [PATCH] Add more tests for generic views --- rest_framework/tests/generics.py | 46 +++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/rest_framework/tests/generics.py b/rest_framework/tests/generics.py index fd1060a8f..0ce297e01 100644 --- a/rest_framework/tests/generics.py +++ b/rest_framework/tests/generics.py @@ -29,14 +29,14 @@ class TestRootView(TestCase): {'id': obj.id, 'text': obj.text} for obj in self.objects.all() ] + self.view = RootView.as_view() def test_get_root_view(self): """ GET requests to RootAPIView should return list of objects. """ - view = RootView.as_view() request = factory.get('/') - response = view(request).render() + response = self.view(request).render() self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals(response.data, self.data) @@ -44,15 +44,33 @@ class TestRootView(TestCase): """ POST requests to RootAPIView should create a new object. """ - view = RootView.as_view() content = {'text': 'foobar'} request = factory.post('/', json.dumps(content), content_type='application/json') - response = view(request).render() + response = self.view(request).render() self.assertEquals(response.status_code, status.HTTP_201_CREATED) self.assertEquals(response.data, {'id': 4, 'text': u'foobar'}) created = self.objects.get(id=4) self.assertEquals(created.text, 'foobar') + def test_put_root_view(self): + """ + PUT requests to RootAPIView should not be allowed + """ + content = {'text': 'foobar'} + request = factory.put('/', json.dumps(content), content_type='application/json') + response = self.view(request).render() + self.assertEquals(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) + self.assertEquals(response.content, '{"detail": "Method \'PUT\' not allowed."}') + + def test_delete_root_view(self): + """ + DELETE requests to RootAPIView should not be allowed + """ + request = factory.delete('/') + response = self.view(request).render() + self.assertEquals(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) + self.assertEquals(response.content, '{"detail": "Method \'DELETE\' not allowed."}') + class TestInstanceView(TestCase): def setUp(self): @@ -67,25 +85,34 @@ class TestInstanceView(TestCase): {'id': obj.id, 'text': obj.text} for obj in self.objects.all() ] + self.view = InstanceView.as_view() def test_get_instance_view(self): """ GET requests to InstanceAPIView should return a single object. """ - view = InstanceView.as_view() request = factory.get('/1') - response = view(request, pk=1).render() + response = self.view(request, pk=1).render() self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals(response.data, self.data[0]) + def test_post_instance_view(self): + """ + POST requests to InstanceAPIView should not be allowed + """ + content = {'text': 'foobar'} + request = factory.post('/', json.dumps(content), content_type='application/json') + response = self.view(request).render() + self.assertEquals(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) + self.assertEquals(response.content, '{"detail": "Method \'POST\' not allowed."}') + def test_put_instance_view(self): """ PUT requests to InstanceAPIView should update an object. """ - view = InstanceView.as_view() content = {'text': 'foobar'} request = factory.put('/1', json.dumps(content), content_type='application/json') - response = view(request, pk=1).render() + response = self.view(request, pk=1).render() self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals(response.data, {'id': 1, 'text': 'foobar'}) updated = self.objects.get(id=1) @@ -95,9 +122,8 @@ class TestInstanceView(TestCase): """ DELETE requests to InstanceAPIView should delete an object. """ - view = InstanceView.as_view() request = factory.delete('/1') - response = view(request, pk=1).render() + response = self.view(request, pk=1).render() self.assertEquals(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEquals(response.content, '') ids = [obj.id for obj in self.objects.all()]