From 88c070696d654ec6edf0ee4a976fa60d3a49c8a4 Mon Sep 17 00:00:00 2001 From: JasonHomeWorkstationUbuntu Date: Wed, 21 Oct 2020 15:47:16 +1100 Subject: [PATCH] Route: GET ALL --- src/puppies/tests/test_views.py | 26 +++++++++++++++++++++++++- src/puppies/views.py | 5 +++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/puppies/tests/test_views.py b/src/puppies/tests/test_views.py index 299c5aa..fa640f1 100644 --- a/src/puppies/tests/test_views.py +++ b/src/puppies/tests/test_views.py @@ -7,4 +7,28 @@ from ..models import Puppy from ..serializers import PuppySerializer # initialize the APIClient app -client = Client() \ No newline at end of file +client = Client() + +class GetAllPuppiesTest(TestCase): + """ + Test module for GET all puppies API + """ + + def setUp(self): + Puppy.objects.create( + name='Casper', age=3, breed='Bull Dog', color='Black') + Puppy.objects.create( + name='Muffin', age=1, breed='Gradane', color='Brown') + Puppy.objects.create( + name='Rambo', age=2, breed='Labrador', color='Black') + Puppy.objects.create( + name='Ricky', age=6, breed='Labrador', color='Brown') + + def test_get_all_puppies(self): + # get API response + response = client.get(reverse('get_post_puppies')) + # 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) \ No newline at end of file diff --git a/src/puppies/views.py b/src/puppies/views.py index 870451a..f815019 100644 --- a/src/puppies/views.py +++ b/src/puppies/views.py @@ -28,6 +28,11 @@ def get_delete_update_puppy(request, pk): def get_post_puppies(request): # get all puppies if request.method == 'GET': + puppies = Puppy.objects.all() + serializer = PuppySerializer(puppies, many=True) # generate a nested list of 'edit' items + return Response(serializer.data) + # insert a new record for a puppy + elif request.method == 'POST': return Response({}) # insert a new record for a puppy elif request.method == 'POST':