Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Django): extra API fixes round 2 #401

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions open_prices/api/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class CustomPagination(pagination.PageNumberPagination):
- previous -> ?
"""

page_size_query_param = "size"

def get_paginated_response(self, data):
return Response(
{
Expand Down
8 changes: 6 additions & 2 deletions open_prices/api/prices/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion open_prices/api/prices/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Meta:
class PriceFullSerializer(PriceSerializer):
product = ProductFullSerializer()
location = LocationSerializer()
proof = ProofSerializer()
proof = ProofSerializer() # without location object

class Meta:
model = Price
Expand Down
46 changes: 45 additions & 1 deletion open_prices/api/prices/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions open_prices/api/proofs/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ class ProofSerializer(serializers.ModelSerializer):
class Meta:
model = Proof
# fields = "__all__"
exclude = ["source"]
exclude = ["location", "source"]


class ProofFullSerializer(ProofSerializer):
location = LocationSerializer()

class Meta:
model = Proof
exclude = ProofSerializer.Meta.exclude
exclude = ["source"] # ProofSerializer.Meta.exclude


class ProofCreateSerializer(serializers.ModelSerializer):
Expand Down
Loading