Add more tests for generic views

This commit is contained in:
Tom Christie 2012-09-28 16:23:46 +01:00
parent 6cf2874cf2
commit 0853316545

View File

@ -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()]