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_valid_single_puppy(self): puppy = Puppy.objects.all().first() url = reverse('get_delete_update_puppy', kwargs={'pk': puppy.pk}) response = self.client.get(url) serializer = PuppySerializer(puppy) self.assertEqual(response.data, serializer.data) self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_get_all_puppies(self): # get API response response = self.client.get(reverse('puppies: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)
def test_create_valid_puppy(self): response = client.post(reverse('get_post_puppies'), data=json.dumps(self.valid_payload), content_type='application/json') puppy = Puppy.objects.get(pk=1) serializer = PuppySerializer(puppy) self.assertEqual(response.data, serializer.data) self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def get_delete_update_puppy(request, pk): """ :param request: :param pk: :return: """ try: puppy = Puppy.objects.get(pk=pk) except Puppy.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) # get details of a single puppy if request.method == 'GET': # return Response({}) serializer = PuppySerializer(puppy) return Response(serializer.data) # delete a single puppy elif request.method == 'DELETE': # return Response({}) puppy.delete() return Response(status=status.HTTP_204_NO_CONTENT) # update details of a single puppy elif request.method == 'PUT': # return Response({}) serializer = PuppySerializer(puppy, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_204_NO_CONTENT) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get_post_puppies(request): """ :param request: :return: """ # get all puppies if request.method == 'GET': # return Response({}) puppies = Puppy.objects.all() serializer = PuppySerializer(puppies, many=True) return Response(serializer.data) # insert a new record for a puppy elif request.method == 'POST': # return Response({}) data = { 'name': request.data.get('name'), 'age': int(request.data.get('age')), 'breed': request.data.get('breed'), 'color': request.data.get('color') } serializer = PuppySerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def test_get_all_puppies(self): resp = self.client.get(reverse('get-post-puppies')) puppies = PuppyModel.objects.all() serializer_puppies = PuppySerializer(puppies, many=True) self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(resp.data, serializer_puppies.data)
def test_get_valid_single_puppy(self, ): resp = self.client.get( reverse('get-delete-update-puppy', kwargs={'pk': self.rambo.pk})) puppy = PuppyModel.objects.get(pk=self.rambo.pk) serializer_ppuppy = PuppySerializer(puppy) self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(resp.data, serializer_ppuppy.data)
def get_post_puppies(request): if request.method == 'GET': puppies = Puppy.objects.all() serializer = PuppySerializer(puppies, many=True) return Response(serializer.data) elif request.method == 'POST': data = { 'name': request.data.get('name'), 'age': request.data.get('age'), 'breed': request.data.get('breed'), 'color': request.data.get('color') } serializer = PuppySerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get_delete_update_puppy(request, pk): try: puppy = Puppy.objects.get(pk=pk) except Puppy.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': puppy = Puppy.objects.get(pk=pk) serializer = PuppySerializer(puppy) return Response(serializer.data) if request.method == 'PUT': serializer = PuppySerializer(puppy, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_204_NO_CONTENT) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) if request.method == 'DELETE': puppy.delete() return Response(status=status.HTTP_204_NO_CONTENT)