Finished GET Single

master
Jason Zhu 2020-10-21 16:00:59 +11:00
parent 88c070696d
commit 219b5ae604
2 changed files with 31 additions and 3 deletions

View File

@ -32,3 +32,29 @@ class GetAllPuppiesTest(TestCase):
serializer = PuppySerializer(puppies, many=True) serializer = PuppySerializer(puppies, many=True)
self.assertEqual(response.data, serializer.data) self.assertEqual(response.data, serializer.data)
self.assertEqual(response.status_code, status.HTTP_200_OK) 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)

View File

@ -7,7 +7,7 @@ from rest_framework import status
from .models import Puppy from .models import Puppy
from .serializers import PuppySerializer from .serializers import PuppySerializer
@api_view(['GET', 'DELETE', 'PUT']) @api_view(['GET', 'DELETE', 'UPDATE'])
def get_delete_update_puppy(request, pk): def get_delete_update_puppy(request, pk):
try: try:
puppy = Puppy.objects.get(pk=pk) puppy = Puppy.objects.get(pk=pk)
@ -16,7 +16,9 @@ def get_delete_update_puppy(request, pk):
# get details of a single puppy # get details of a single puppy
if request.method == 'GET': 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 # delete a single puppy
elif request.method == 'DELETE': elif request.method == 'DELETE':
return Response({}) return Response({})