Skip to content

Commit

Permalink
Merge pull request #82 from pymasterspl/feature/cart-api-refactor
Browse files Browse the repository at this point in the history
Cart api with tests updated/refactored
  • Loading branch information
yanazPL authored Feb 20, 2024
2 parents 725f559 + 3dae593 commit 4847298
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
3 changes: 3 additions & 0 deletions Dshop/apps/products_catalogue/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from rest_framework import viewsets
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import AllowAny
from rest_framework import status
from dj_shop_cart.cart import get_cart_class
from .models import Product
Expand All @@ -17,7 +18,9 @@ class ProductViewSet(viewsets.ModelViewSet):


class CartAPIView(APIView):
permission_classes = (AllowAny ,)
serializer_class = CartReadSerializer

def post(self, request):
write_serializer = CartWriteSerializer(data=request.data, context={'request': request})
if write_serializer.is_valid():
Expand Down
29 changes: 17 additions & 12 deletions Dshop/apps/products_catalogue/tests/test_api_persistent_cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,18 @@ def assert_data_empty(data):


@pytest.mark.django_db
def test_get_cart_empty(api_client):
response = api_client.get(reverse("api_cart"))
def test_get_cart_empty():
response = APIClient().get(reverse("api_cart"))
assert response.status_code == status.HTTP_200_OK
assert_data_empty(response.data)


@pytest.mark.django_db
def test_add(api_client, tv_product):
def test_add(tv_product):
data = {
'items': [ {'product_pk': tv_product.pk, 'quantity': 10} ]
}
api_client = APIClient()
response = api_client.post(reverse("api_cart"), data)
assert_products_data(response.data, [tv_product], [10])
assert response.status_code == status.HTTP_201_CREATED
Expand Down Expand Up @@ -72,7 +73,7 @@ def test_add_relogin_get(tv_product):


@pytest.mark.django_db
def test_add_ten_and_get(api_client, ten_tv_products):
def test_add_ten_and_get(ten_tv_products):
quantities = [1, 1, 6, 8, 3, 4, 2, 26, 1, 10]

data = {
Expand All @@ -81,6 +82,7 @@ def test_add_ten_and_get(api_client, ten_tv_products):
for product, quantity in zip(ten_tv_products, quantities)
]
}
api_client = APIClient()
response = api_client.post(reverse("api_cart"), data)
assert response.status_code == status.HTTP_201_CREATED
assert_products_data(response.data, ten_tv_products, quantities)
Expand All @@ -90,14 +92,15 @@ def test_add_ten_and_get(api_client, ten_tv_products):


@pytest.mark.django_db
def test_add_ten_replace_with_one(api_client, ten_tv_products, tv_product):
def test_add_ten_replace_with_one(ten_tv_products, tv_product):
quantities = [11, 1, 3, 8, 4, 5, 6, 7, 1, 10]
data = {
'items': [
{'product_pk': product.pk, 'quantity': quantity}
for product, quantity in zip(ten_tv_products, quantities)
]
}
api_client = APIClient()
response = api_client.post(reverse("api_cart"), data)
assert response.status_code == status.HTTP_201_CREATED

Expand All @@ -115,14 +118,15 @@ def test_add_ten_replace_with_one(api_client, ten_tv_products, tv_product):


@pytest.mark.django_db
def test_delete_ten(api_client, ten_tv_products):
def test_delete_ten(ten_tv_products):
quantities = [1, 1, 6, 8, 3, 4, 2, 26, 1, 10]
data = {
'items': [
{'product_pk': product.pk, 'quantity': quantity}
for product, quantity in zip(ten_tv_products, quantities)
]
}
api_client = APIClient()
response = api_client.post(reverse("api_cart"), data)
assert response.status_code == status.HTTP_201_CREATED
response = api_client.post(reverse("api_cart"), {})
Expand All @@ -133,41 +137,42 @@ def test_delete_ten(api_client, ten_tv_products):


@pytest.mark.django_db
def test_get_non_unique_pks(api_client, tv_product):
def test_get_non_unique_pks(tv_product):
data = {
'items':[
{'product_pk': tv_product.pk, 'quantity': 2},
{'product_pk': tv_product.pk, 'quantity': 3}
]
}
response = api_client.post(reverse("api_cart"), data)
response = APIClient().post(reverse("api_cart"), data)
assert str(response.data['items'][0]) == "product_pk must be unique within items."
assert response.status_code == status.HTTP_400_BAD_REQUEST


@pytest.mark.django_db
def test_get_non_existing_pks(api_client, tv_product):
def test_get_non_existing_pks(tv_product):
NON_EXISTING_ID = 999999
data = {
'items':[
{'product_pk': tv_product.pk, 'quantity': 2},
{'product_pk': NON_EXISTING_ID, 'quantity': 3}
]
}
api_client = APIClient()
response = api_client.post(reverse("api_cart"), data)
assert response.status_code == status.HTTP_404_NOT_FOUND
response = api_client.get(reverse("api_cart"))
assert_data_empty(response.data)
assert_products_data(response.data, [tv_product], [2])


@pytest.mark.django_db
def test_get_zero_quantities(api_client, tv_product):
def test_get_zero_quantities(tv_product):
data = {
'items':[
{'product_pk': tv_product.pk, 'quantity': 0}
]
}
response = api_client.post(reverse("api_cart"), data)
response = APIClient().post(reverse("api_cart"), data)
error_str = str(response.data['items'][0]['quantity'][0])
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert error_str == 'Ensure this value is greater than or equal to 1.'

0 comments on commit 4847298

Please sign in to comment.