From f81bbee4e60d110f42788b732047d2fb1162852d Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 5 Mar 2021 10:13:31 +0000 Subject: [PATCH] added object count to my_products --- products/tests.py | 6 ++++-- products/views.py | 6 +++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/products/tests.py b/products/tests.py index 0ab5c37..392e13f 100644 --- a/products/tests.py +++ b/products/tests.py @@ -947,7 +947,8 @@ class MyProductsViewTest(APITestCase): # Assert forbidden code self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEquals(len(user_instances), len(payload)) + self.assertEquals(payload['count'], len(payload['results'])) + self.assertEquals(len(user_instances), payload['count']) def test_auth_user_can_paginate_instances(self): """authenticated user can paginate instances @@ -967,7 +968,8 @@ class MyProductsViewTest(APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) # assert only 2 instances in response payload = response.json() - self.assertEquals(2, len(payload)) + self.assertEquals(payload['count'], len(payload['results'])) + self.assertEquals(2, payload['count']) def test_anon_user_cannot_access(self): # send in request diff --git a/products/views.py b/products/views.py index 4f3e153..fa46b42 100644 --- a/products/views.py +++ b/products/views.py @@ -68,7 +68,11 @@ def my_products(request): elif limit is not None: limit = int(limit) data = data[:limit] - return Response(data=data) + # prepare response payload + payload = {} + payload['results'] = data + payload['count'] = len(payload['results']) + return Response(data=payload) @api_view(['POST',])