adde total_results to search response, for better frontend pagination
This commit is contained in:
@@ -478,6 +478,7 @@ class ProductSearchTest(TestCase):
|
|||||||
url = f"{self.endpoint}?query_string={query_string}"
|
url = f"{self.endpoint}?query_string={query_string}"
|
||||||
# send in request
|
# send in request
|
||||||
response = self.client.get(url)
|
response = self.client.get(url)
|
||||||
|
# import ipdb; ipdb.set_trace()
|
||||||
|
|
||||||
# check response
|
# check response
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
@@ -521,8 +522,9 @@ class ProductSearchTest(TestCase):
|
|||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
# load response data
|
# load response data
|
||||||
payload = response.json()
|
payload = response.json()
|
||||||
# check for object creation
|
|
||||||
self.assertEquals(len(payload['products']), limit)
|
self.assertEquals(len(payload['products']), limit)
|
||||||
|
self.assertEquals(payload['total_results'], len(expected_instances))
|
||||||
|
|
||||||
|
|
||||||
class MyProductsViewTest(APITestCase):
|
class MyProductsViewTest(APITestCase):
|
||||||
"""my_products tests
|
"""my_products tests
|
||||||
|
|||||||
@@ -174,6 +174,8 @@ def product_search(request):
|
|||||||
ranked_products = sorted(result_list, key= lambda rank:rank.rank, reverse=True)
|
ranked_products = sorted(result_list, key= lambda rank:rank.rank, reverse=True)
|
||||||
serializer = SearchResultSerializer(ranked_products, many=True)
|
serializer = SearchResultSerializer(ranked_products, many=True)
|
||||||
product_results = [dict(i) for i in serializer.data]
|
product_results = [dict(i) for i in serializer.data]
|
||||||
|
total_results = len(product_results)
|
||||||
|
|
||||||
# check for pagination
|
# check for pagination
|
||||||
limit = request.GET.get('limit', None)
|
limit = request.GET.get('limit', None)
|
||||||
offset = request.GET.get('offset', None)
|
offset = request.GET.get('offset', None)
|
||||||
@@ -185,6 +187,6 @@ def product_search(request):
|
|||||||
limit = int(limit)
|
limit = int(limit)
|
||||||
product_results = product_results[:limit]
|
product_results = product_results[:limit]
|
||||||
|
|
||||||
return Response(data={"filters": filters, "products": product_results})
|
return Response(data={"filters": filters, "total_results": total_results, "products": product_results})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return Response({"errors": {"details": str(e)}}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
return Response({"errors": {"details": str(e)}}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
|
|||||||
Reference in New Issue
Block a user