Skip to content

Commit

Permalink
Add order filter to ProductViewSet as products order changed after ad…
Browse files Browse the repository at this point in the history
…ding of rating annotated field, update promotion filter tests
  • Loading branch information
earlinn committed Dec 23, 2023
1 parent 090d484 commit 8114dce
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
5 changes: 3 additions & 2 deletions backend/api/products_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
ValidationErrorResponseSerializer,
)
from drf_yasg.utils import swagger_auto_schema
from rest_framework import decorators, permissions, response, status, viewsets
from rest_framework import decorators, filters, permissions, response, status, viewsets

from .filters import ProductFilter
from .mixins import MESSAGE_ON_DELETE, DestroyWithPayloadMixin
Expand Down Expand Up @@ -512,8 +512,9 @@ class ProductViewSet(DestroyWithPayloadMixin, viewsets.ModelViewSet):
)
serializer_class = ProductSerializer
permission_classes = [IsAdminOrReadOnly]
filter_backends = [rf_filters.DjangoFilterBackend]
filter_backends = [rf_filters.DjangoFilterBackend, filters.OrderingFilter]
filterset_class = ProductFilter
ordering = ["pk"]
pagination_class = CustomPageNumberPagination

def get_serializer_class(self):
Expand Down
6 changes: 2 additions & 4 deletions backend/tests/api_tests/test_product_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,10 @@ def test_product_tags_filter_fail_invalid_slug(client, tags):
assert response.data["errors"][0]["attr"] == "tags"


@pytest.mark.skip(reason="Update it as the filter was changed")
@pytest.mark.django_db
def test_product_promotions_filter(client, products, promotions):
products[1].promotions.set([promotions[0]])
filter = f"?promotions={promotions[0].pk}"
filter = f"?promotions={promotions[0].slug}"
response = client.get(reverse("api:product-list") + filter)

assert response.status_code == 200
Expand All @@ -258,12 +257,11 @@ def test_product_promotions_filter(client, products, promotions):
)


@pytest.mark.skip(reason="Update it as the filter was changed")
@pytest.mark.django_db
def test_product_promotions_filter_multiple(client, products, promotions):
products[0].promotions.set([promotions[1]])
products[1].promotions.set([promotions[0]])
filter = f"?promotions={promotions[0].pk}&promotions={promotions[1].pk}"
filter = f"?promotions={promotions[0].slug}&promotions={promotions[1].slug}"
response = client.get(reverse("api:product-list") + filter)

assert response.status_code == 200
Expand Down
8 changes: 7 additions & 1 deletion backend/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@
PROMOTION_NAME_1 = "Birthday Discount 15%"
PROMOTION_NAME_2 = "Black Friday"

PROMOTION_SLUG_1 = "birthday-15"
PROMOTION_SLUG_2 = "black-friday"

PROMOTION_DISCOUNT_1 = 15
PROMOTION_DISCOUNT_2 = 20

Expand Down Expand Up @@ -214,9 +217,12 @@ def promotions():
Promotion.objects.create(
promotion_type=Promotion.BIRTHDAY,
name=PROMOTION_NAME_1,
slug=PROMOTION_SLUG_1,
discount=PROMOTION_DISCOUNT_1,
)
Promotion.objects.create(name=PROMOTION_NAME_2, discount=PROMOTION_DISCOUNT_2)
Promotion.objects.create(
name=PROMOTION_NAME_2, slug=PROMOTION_SLUG_2, discount=PROMOTION_DISCOUNT_2
)
return Promotion.objects.all()


Expand Down

0 comments on commit 8114dce

Please sign in to comment.