def test_cart_increase(self): current_quantity = self.cart_item.quantity if self.cart_item else 0 CartView.update_item( pk=self.cart_item.item.id if self.cart_item else 4, action='increase', user=self.user) self.cart_item = CartItem.objects.get(user=self.user) self.assertEqual(self.cart_item.quantity, current_quantity + 1)
def test_cart_full_update(self): # Increase 1 time, (add the item) CartView.update_item(pk=4, user=self.user, action='increase') self.assertEqual(len(CartItem.objects.filter(user=self.user)), 1) c = CartItem.objects.get(user=self.user) # Increase to 2 CartView.update_item(pk=4, user=self.user, action='increase') c.refresh_from_db() self.assertEqual(c.quantity, 2) # Decrease back to 1 CartView.update_item(pk=4, user=self.user, action='decrease') c.refresh_from_db() self.assertEqual(c.quantity, 1) # Delete the instance, as quantity is 0 CartView.update_item(pk=4, user=self.user, action='decrease') self.assertEqual(len(CartItem.objects.filter(user=self.user)), 0)
from django.urls import path, include from rest_framework import routers from api.views import ProductViewSet, CartView, OrderApiView, get_token_view router = routers.DefaultRouter() router.register(r'products', ProductViewSet) app_name = 'api' urlpatterns = [ path('', include(router.urls)), path('orders/', OrderApiView.as_view()), path('cart/', CartView.as_view()), path('token/', get_token_view), path('api-auth/', include('rest_framework.urls', namespace='rest_framework')), ]
def test_add_new_to_cart(self): CartView.update_item(pk=4, user=self.user, action='increase') self.assertEqual(len(CartItem.objects.filter(user=self.user)), 1)