Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add tests to verify products availability
Adam Bogdał724b58530d5f
diff --git a/saleor/cart/test_cart.py b/saleor/cart/test_cart.pyindex e455a7411d..f39c33d525 100755--- a/saleor/cart/test_cart.py+++ b/saleor/cart/test_cart.py@@ -12,6 +12,7 @@ from satchless.item import InsufficientStockfrom . import Cart, SessionCartfrom . import formsfrom .forms import ReplaceCartLineForm, ReplaceCartLineFormSet+from ..cart.utils import has_available_products, remove_unavailable_productsfrom ..product.models import (ProductVariant, Product)@@ -164,3 +165,33 @@ def test_session_cart_returns_correct_prices():cart_price = cart[0].get_price_per_item()sessioncart_price = cart.session_cart[0].get_price_per_item()assert cart_price == sessioncart_price+++def test_cart_has_unavailable_products():+ cart = Cart(session_cart=SessionCart())+ cart.add(non_stocked_variant, quantity=100)+ cart.add(stocked_variant, quantity=12, check_quantity=False)+ assert not has_available_products(cart)+++def test_cart_has_available_products():+ cart = Cart(session_cart=SessionCart())+ cart.add(non_stocked_variant, quantity=100)+ cart.add(stocked_variant, quantity=10, check_quantity=False)+ assert has_available_products(cart)+++def test_cart_contains_products_on_stock():+ cart = Cart(session_cart=SessionCart())+ cart.add(stocked_variant, quantity=12, check_quantity=False)+ updated_cart = remove_unavailable_products(cart)+ assert cart.count() == 12+ assert updated_cart.count() == 10+++def test_cart_doesnt_contain_empty_products():+ stocked_variant.get_stock_quantity = MagicMock(return_value=0)+ cart = Cart(session_cart=SessionCart())+ cart.add(stocked_variant, quantity=10, check_quantity=False)+ updated_cart = remove_unavailable_products(cart)+ assert len(updated_cart) == 0diff --git a/saleor/cart/utils.py b/saleor/cart/utils.pynew file mode 100644index 0000000000..840853a63b--- /dev/null+++ b/saleor/cart/utils.py@@ -0,0 +1,7 @@++def has_available_products(cart):+ return True+++def remove_unavailable_products(cart):+ return cartdiff --git a/saleor/cart/views.py b/saleor/cart/views.pyindex 1cafedfb44..edde22ec37 100755--- a/saleor/cart/views.py+++ b/saleor/cart/views.py@@ -7,11 +7,17 @@ from django.utils.translation import ugettext as _from .forms import ReplaceCartLineFormSetfrom . import Cart+from ..cart.utils import has_available_products, remove_unavailable_productsdef index(request):cart = Cart.for_session_cart(request.cart, discounts=request.discounts)cart_partitioner = cart.partition()+ if not has_available_products(cart):+ msg = _('Sorry. We don\'t have that many items in stock. '+ 'Quantity was set to maximum available for now.')+ messages.warning(request, msg)+ cart = remove_unavailable_products(cart)formset = ReplaceCartLineFormSet(request.POST or None,cart=cart)if formset.is_valid():diff --git a/saleor/checkout/views.py b/saleor/checkout/views.pyindex 890a7647f5..22d5111312 100644--- a/saleor/checkout/views.py+++ b/saleor/checkout/views.py@@ -2,10 +2,11 @@ from django.http.response import Http404from django.shortcuts import redirectfrom . import Checkout+from ..cart.utils import has_available_productsdef details(request, step):- if not request.cart:+ if not request.cart or not has_available_products(request.cart):return redirect('cart:index')checkout = Checkout(request)if not step:diff --git a/setup.py b/setup.pyindex ee9309c774..8e2af701bb 100755--- a/setup.py+++ b/setup.py@@ -46,7 +46,7 @@ setup('django-model-utils>=2.0.0,<2.1a0','django-mptt>=0.7.1','django-offsite-storage>=0.0.5',- 'django-payments>=0.6.1,<0.7a0',+ 'django-payments>=0.7.0,<0.8a0','django-prices>=0.4.0,<0.5a0','djangorestframework>=3.1,<3.2a0','django-selectable==0.8.0',