From 219b5ae6048ae66c5af3e06ddacdcaa30a378d65 Mon Sep 17 00:00:00 2001 From: JasonHomeWorkstationUbuntu Date: Wed, 21 Oct 2020 16:00:59 +1100 Subject: [PATCH] Finished GET Single --- src/puppies/tests/test_views.py | 28 +++++++++++++++++++++++++++- src/puppies/views.py | 6 ++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/puppies/tests/test_views.py b/src/puppies/tests/test_views.py index fa640f1..9ab318c 100644 --- a/src/puppies/tests/test_views.py +++ b/src/puppies/tests/test_views.py @@ -31,4 +31,30 @@ class GetAllPuppiesTest(TestCase): puppies = Puppy.objects.all() serializer = PuppySerializer(puppies, many=True) self.assertEqual(response.data, serializer.data) - self.assertEqual(response.status_code, status.HTTP_200_OK) \ No newline at end of file + 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})) + puppy = Puppy.objects.get(pk=self.rambo.pk) + serializer = PuppySerializer(puppy) + self.assertEqual(response.data, serializer.data) + self.assertEqual(response.status_code, status.HTTP_200_OK) + + def test_get_invalid_single_puppy(self): + response = client.get( + reverse('get_delete_update_puppy', kwargs={'pk': 30})) + self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) diff --git a/src/puppies/views.py b/src/puppies/views.py index f815019..53847b9 100644 --- a/src/puppies/views.py +++ b/src/puppies/views.py @@ -7,7 +7,7 @@ from rest_framework import status from .models import Puppy from .serializers import PuppySerializer -@api_view(['GET', 'DELETE', 'PUT']) +@api_view(['GET', 'DELETE', 'UPDATE']) def get_delete_update_puppy(request, pk): try: puppy = Puppy.objects.get(pk=pk) @@ -16,7 +16,9 @@ def get_delete_update_puppy(request, pk): # get details of a single puppy if request.method == 'GET': - return Response({}) + # we get the puppy using an ID. Run the tests to ensure they all pass. + serializer = PuppySerializer(puppy) + return Response(serializer.data) # delete a single puppy elif request.method == 'DELETE': return Response({})