Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Fix stock allocator and add tests for it

Krzysztof Wolski1b2ea4c82b69
diff --git a/saleor/product/models/base.py b/saleor/product/models/base.py
index c75d577c3a..54bde0915d 100644
--- a/saleor/product/models/base.py
+++ b/saleor/product/models/base.py
@@ -292,7 +292,7 @@ class StockManager(models.Manager):
stock.save(update_fields=['quantity_allocated'])
def decrease_stock(self, stock, quantity):
- stock.quantity = F('quantity_allocated') - quantity
+ stock.quantity = F('quantity') - quantity
stock.quantity_allocated = F('quantity_allocated') - quantity
stock.save(update_fields=['quantity', 'quantity_allocated'])
diff --git a/tests/test_product.py b/tests/test_product.py
index a07d3ebba3..7dba1bba68 100644
--- a/tests/test_product.py
+++ b/tests/test_product.py
@@ -22,6 +22,28 @@ def test_stock_allocator(product_in_stock):
assert stock.quantity_allocated == 1
+def test_decrease_stock(product_in_stock):
+ stock = product_in_stock.variants.first().stock.first()
+ stock.quantity = 100
+ stock.quantity_allocated = 80
+ stock.save()
+ models.Stock.objects.decrease_stock(stock, 50)
+ stock.refresh_from_db()
+ assert stock.quantity == 50
+ assert stock.quantity_allocated == 30
+
+
+def test_deallocate_stock(product_in_stock):
+ stock = product_in_stock.variants.first().stock.first()
+ stock.quantity = 100
+ stock.quantity_allocated = 80
+ stock.save()
+ models.Stock.objects.deallocate_stock(stock, 50)
+ stock.refresh_from_db()
+ assert stock.quantity == 100
+ assert stock.quantity_allocated == 30
+
+
def test_product_page_redirects_to_correct_slug(client, product_in_stock):
uri = product_in_stock.get_absolute_url()
uri = uri.replace(product_in_stock.get_slug(), 'spanish-inquisition')