Added DELETE option (Not Finished)

master
Jason Zhu 2020-10-21 17:17:33 +11:00
parent c0f3354f1f
commit 2db58790b1
2 changed files with 26 additions and 23 deletions

View File

@ -26,28 +26,7 @@ class GetAllPuppiesTest(TestCase):
def test_get_all_puppies(self): def test_get_all_puppies(self):
# get API response # get API response
response = client.get(reverse('get_post_puppies')) response = client.get(reverse('get_post_puppies'))return Response({})
# get data from db
puppies = Puppy.objects.all()
serializer = PuppySerializer(puppies, many=True)
self.assertEqual(response.data, serializer.data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
class GetSinglePuppyTest(TestCase):
""" Test module for GET single puppy API """
def setUp(self):
self.casper = Puppy.objects.create(
name='Casper', age=3, breed='Bull Dog', color='Black')
self.muffin = Puppy.objects.create(
name='Muffin', age=1, breed='Gradane', color='Brown')
self.rambo = Puppy.objects.create(
name='Rambo', age=2, breed='Labrador', color='Black')
self.ricky = Puppy.objects.create(
name='Ricky', age=6, breed='Labrador', color='Brown')
def test_get_valid_single_puppy(self):
response = client.get(
reverse('get_delete_update_puppy', kwargs={'pk': self.rambo.pk})) reverse('get_delete_update_puppy', kwargs={'pk': self.rambo.pk}))
puppy = Puppy.objects.get(pk=self.rambo.pk) puppy = Puppy.objects.get(pk=self.rambo.pk)
serializer = PuppySerializer(puppy) serializer = PuppySerializer(puppy)
@ -135,3 +114,24 @@ class UpdateSinglePuppyTest(TestCase):
data=json.dumps(self.invalid_payload), data=json.dumps(self.invalid_payload),
content_type='application/json') content_type='application/json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
class DeleteSinglePuppyTest(TestCase):
""" Test module for deleting an existing puppy record
To delete a single record, an ID is required
"""
def setUp(self):
self.casper = Puppy.objects.create(
name='Casper', age=3, breed='Bull Dog', color='Black')
self.muffin = Puppy.objects.create(
name='Muffy', age=1, breed='Gradane', color='Brown')
def test_valid_delete_puppy(self):
response = client.delete(
reverse('get_delete_update_puppy', kwargs={'pk': self.muffin.pk}))
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
def test_invalid_delete_puppy(self):
response = client.delete(
reverse('get_delete_update_puppy', kwargs={'pk': 30}))
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

View File

@ -24,7 +24,10 @@ def get_delete_update_puppy(request, pk):
return Response(serializer.data) return Response(serializer.data)
# delete a single puppy # delete a single puppy
elif request.method == 'DELETE': elif request.method == 'DELETE':
return Response({}) # return Response({})
puppy.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
# update details of a single puppy # update details of a single puppy
elif request.method == 'PUT': elif request.method == 'PUT':