|
| 1 | +"""Tests for multiple cart items functionality""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from django.test import override_settings |
| 5 | +from django.urls import reverse |
| 6 | +from rest_framework import status |
| 7 | + |
| 8 | +from ecommerce.factories import BasketFactory, BasketItemFactory, ProductFactory |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.django_db |
| 12 | +class TestMultipleCartItems: |
| 13 | + """Test class for multiple cart items functionality""" |
| 14 | + |
| 15 | + @override_settings(ENABLE_MULTIPLE_CART_ITEMS=False) |
| 16 | + def test_add_to_cart_single_item_mode_default(self, user_drf_client, user): |
| 17 | + """Test that with feature flag disabled, adding items replaces existing items""" |
| 18 | + # Create a product and basket with existing item |
| 19 | + existing_product = ProductFactory.create() |
| 20 | + new_product = ProductFactory.create() |
| 21 | + |
| 22 | + basket = BasketFactory.create(user=user) |
| 23 | + BasketItemFactory.create(basket=basket, product=existing_product) |
| 24 | + |
| 25 | + assert basket.basket_items.count() == 1 |
| 26 | + |
| 27 | + # Add new product to cart |
| 28 | + response = user_drf_client.post( |
| 29 | + reverse("checkout_api-add_to_cart"), |
| 30 | + data={"product_id": new_product.id}, |
| 31 | + ) |
| 32 | + |
| 33 | + assert response.status_code == status.HTTP_200_OK |
| 34 | + assert response.data["message"] == "Product added to cart" |
| 35 | + |
| 36 | + # Verify that only the new product is in the basket |
| 37 | + basket.refresh_from_db() |
| 38 | + assert basket.basket_items.count() == 1 |
| 39 | + assert basket.basket_items.first().product == new_product |
| 40 | + |
| 41 | + @override_settings(ENABLE_MULTIPLE_CART_ITEMS=True) |
| 42 | + def test_add_to_cart_multiple_items_mode_new_product(self, user_drf_client, user): |
| 43 | + """Test that with feature flag enabled, adding new items keeps existing items""" |
| 44 | + # Create products and basket with existing item |
| 45 | + existing_product = ProductFactory.create() |
| 46 | + new_product = ProductFactory.create() |
| 47 | + |
| 48 | + basket = BasketFactory.create(user=user) |
| 49 | + BasketItemFactory.create(basket=basket, product=existing_product) |
| 50 | + |
| 51 | + assert basket.basket_items.count() == 1 |
| 52 | + |
| 53 | + # Add new product to cart |
| 54 | + response = user_drf_client.post( |
| 55 | + reverse("checkout_api-add_to_cart"), |
| 56 | + data={"product_id": new_product.id}, |
| 57 | + ) |
| 58 | + |
| 59 | + assert response.status_code == status.HTTP_200_OK |
| 60 | + assert response.data["message"] == "Product added to cart" |
| 61 | + |
| 62 | + # Verify that both products are in the basket |
| 63 | + basket.refresh_from_db() |
| 64 | + assert basket.basket_items.count() == 2 |
| 65 | + products_in_basket = {item.product for item in basket.basket_items.all()} |
| 66 | + assert products_in_basket == {existing_product, new_product} |
| 67 | + |
| 68 | + @override_settings(ENABLE_MULTIPLE_CART_ITEMS=True) |
| 69 | + def test_add_to_cart_nonexistent_product(self, user_drf_client): |
| 70 | + """Test that adding a non-existent product returns 404""" |
| 71 | + # Add non-existent product to cart |
| 72 | + response = user_drf_client.post( |
| 73 | + reverse("checkout_api-add_to_cart"), |
| 74 | + data={"product_id": 99999}, |
| 75 | + ) |
| 76 | + |
| 77 | + assert response.status_code == status.HTTP_404_NOT_FOUND |
| 78 | + assert response.data["message"] == "Product not found" |
| 79 | + |
| 80 | + @override_settings(ENABLE_MULTIPLE_CART_ITEMS=True) |
| 81 | + def test_cart_with_multiple_items_pricing(self, user_drf_client, user): |
| 82 | + """Test that cart pricing works correctly with multiple items""" |
| 83 | + # Create products with different prices |
| 84 | + product1 = ProductFactory.create(price=50.00) |
| 85 | + product2 = ProductFactory.create(price=30.00) |
| 86 | + |
| 87 | + basket = BasketFactory.create(user=user) |
| 88 | + BasketItemFactory.create(basket=basket, product=product1, quantity=2) |
| 89 | + BasketItemFactory.create(basket=basket, product=product2, quantity=1) |
| 90 | + |
| 91 | + # Get cart info |
| 92 | + response = user_drf_client.get(reverse("checkout_api-cart")) |
| 93 | + |
| 94 | + assert response.status_code == status.HTTP_200_OK |
| 95 | + cart_data = response.data |
| 96 | + |
| 97 | + # Verify total price calculation (2 * $50 + 1 * $30 = $130) |
| 98 | + assert float(cart_data["total_price"]) == 130.00 |
| 99 | + assert len(cart_data["basket_items"]) == 2 |
| 100 | + |
| 101 | + def test_basket_items_count_endpoint(self, user_drf_client, user): |
| 102 | + """Test that basket items count endpoint works with multiple items""" |
| 103 | + # Create products and add to basket |
| 104 | + product1 = ProductFactory.create() |
| 105 | + product2 = ProductFactory.create() |
| 106 | + |
| 107 | + basket = BasketFactory.create(user=user) |
| 108 | + BasketItemFactory.create(basket=basket, product=product1, quantity=2) |
| 109 | + BasketItemFactory.create(basket=basket, product=product2, quantity=1) |
| 110 | + |
| 111 | + # Get basket items count |
| 112 | + response = user_drf_client.get(reverse("checkout_api-basket_items_count")) |
| 113 | + |
| 114 | + assert response.status_code == status.HTTP_200_OK |
| 115 | + # Should return count of distinct items, not total quantity |
| 116 | + assert response.data == 2 |
| 117 | + |
| 118 | + @override_settings(ENABLE_MULTIPLE_CART_ITEMS=False) |
| 119 | + def test_existing_basket_item_viewset_still_works(self, user_drf_client, user): |
| 120 | + """Test that the existing BasketItemViewSet still works regardless of feature flag""" |
| 121 | + product1 = ProductFactory.create() |
| 122 | + product2 = ProductFactory.create() |
| 123 | + |
| 124 | + basket = BasketFactory.create(user=user) |
| 125 | + BasketItemFactory.create(basket=basket, product=product1) |
| 126 | + |
| 127 | + # Add item using the existing ViewSet endpoint |
| 128 | + response = user_drf_client.post( |
| 129 | + f"/api/baskets/{basket.id}/items/", |
| 130 | + data={"product": product2.id}, |
| 131 | + ) |
| 132 | + |
| 133 | + assert response.status_code == status.HTTP_201_CREATED |
| 134 | + |
| 135 | + # Verify both items are in basket |
| 136 | + assert basket.basket_items.count() == 2 |
0 commit comments