diff --git a/open_prices/api/pagination.py b/open_prices/api/pagination.py index 9c7c3eaa..8bda766e 100644 --- a/open_prices/api/pagination.py +++ b/open_prices/api/pagination.py @@ -11,6 +11,8 @@ class CustomPagination(pagination.PageNumberPagination): - previous -> ? """ + page_size_query_param = "size" + def get_paginated_response(self, data): return Response( { diff --git a/open_prices/api/prices/filters.py b/open_prices/api/prices/filters.py index bf8ce317..89557898 100644 --- a/open_prices/api/prices/filters.py +++ b/open_prices/api/prices/filters.py @@ -23,8 +23,12 @@ class PriceFilter(django_filters.FilterSet): date__lte = django_filters.DateFilter(field_name="date", lookup_expr="lte") date__year = django_filters.NumberFilter(field_name="date", lookup_expr="year") date__month = django_filters.NumberFilter(field_name="date", lookup_expr="month") - # created__gte = django_filters.DateTimeFilter(field_name="created", lookup_expr="gte") # noqa - # created__lte = django_filters.DateTimeFilter(field_name="created", lookup_expr="lte") # noqa + created__gte = django_filters.DateTimeFilter( + field_name="created", lookup_expr="gte" + ) # noqa + created__lte = django_filters.DateTimeFilter( + field_name="created", lookup_expr="lte" + ) # noqa class Meta: model = Price diff --git a/open_prices/api/prices/serializers.py b/open_prices/api/prices/serializers.py index 022d8c2d..54b52c1c 100644 --- a/open_prices/api/prices/serializers.py +++ b/open_prices/api/prices/serializers.py @@ -29,7 +29,7 @@ class Meta: class PriceFullSerializer(PriceSerializer): product = ProductFullSerializer() location = LocationSerializer() - proof = ProofSerializer() + proof = ProofSerializer() # without location object class Meta: model = Price diff --git a/open_prices/api/prices/tests.py b/open_prices/api/prices/tests.py index 1c88a85b..4b147c7e 100644 --- a/open_prices/api/prices/tests.py +++ b/open_prices/api/prices/tests.py @@ -39,6 +39,38 @@ def test_price_list(self): self.assertEqual(response.data["total"], 3) self.assertEqual(len(response.data["items"]), 3) self.assertTrue("id" in response.data["items"][0]) + self.assertEqual(response.data["items"][0]["price"], 15.00) # default order + + +class PriceListPaginationApiTest(TestCase): + @classmethod + def setUpTestData(cls): + cls.url = reverse("api:prices-list") + PriceFactory(price=15) + PriceFactory(price=0) + PriceFactory(price=50) + + def test_price_list_size(self): + # default + response = self.client.get(self.url) + for PAGINATION_KEY in ["items", "page", "pages", "size", "total"]: + with self.subTest(PAGINATION_KEY=PAGINATION_KEY): + self.assertTrue(PAGINATION_KEY in response.data) + self.assertEqual(response.data["total"], 3) + self.assertEqual(len(response.data["items"]), 3) + self.assertEqual(response.data["page"], 1) + self.assertEqual(response.data["pages"], 1) + self.assertEqual(response.data["size"], 100) + self.assertEqual(response.data["items"][0]["price"], 15.00) # default order + # size=1 + url = self.url + "?size=1" + response = self.client.get(url) + self.assertEqual(response.data["total"], 3) + self.assertEqual(len(response.data["items"]), 1) + self.assertEqual(response.data["page"], 1) + self.assertEqual(response.data["pages"], 3) + self.assertEqual(response.data["size"], 1) + self.assertEqual(response.data["items"][0]["price"], 15.00) # default order class PriceListOrderApiTest(TestCase): @@ -53,7 +85,7 @@ def test_price_list_order_by(self): url = self.url + "?order_by=-price" response = self.client.get(url) self.assertEqual(response.data["total"], 3) - self.assertEqual(response.data["items"][0]["price"], 50.00) # default order + self.assertEqual(response.data["items"][0]["price"], 50.00) class PriceListFilterApiTest(TestCase): @@ -89,6 +121,10 @@ def setUpTestData(cls): owner="user_2", ) + def test_price_list_without_filter(self): + response = self.client.get(self.url) + self.assertEqual(response.data["total"], 3) + def test_price_list_filter_by_product(self): # product_code url = self.url + "?product_code=8001505005707" @@ -193,6 +229,14 @@ def test_price_list_filter_by_owner(self): response = self.client.get(url) self.assertEqual(response.data["total"], 2) + def test_price_list_filter_by_created(self): + url = self.url + "?created__gte=2024-01-01T00:00:00Z" + response = self.client.get(url) + self.assertEqual(response.data["total"], 3) + url = self.url + "?created__lte=2024-01-01T00:00:00Z" + response = self.client.get(url) + self.assertEqual(response.data["total"], 0) + class PriceDetailApiTest(TestCase): @classmethod diff --git a/open_prices/api/proofs/serializers.py b/open_prices/api/proofs/serializers.py index 671fe29b..826fa159 100644 --- a/open_prices/api/proofs/serializers.py +++ b/open_prices/api/proofs/serializers.py @@ -13,7 +13,7 @@ class ProofSerializer(serializers.ModelSerializer): class Meta: model = Proof # fields = "__all__" - exclude = ["source"] + exclude = ["location", "source"] class ProofFullSerializer(ProofSerializer): @@ -21,7 +21,7 @@ class ProofFullSerializer(ProofSerializer): class Meta: model = Proof - exclude = ProofSerializer.Meta.exclude + exclude = ["source"] # ProofSerializer.Meta.exclude class ProofCreateSerializer(serializers.ModelSerializer):