From 39383e0acb094d67a036f0e36b6ee1e8780c09ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=AD=C3=B0ir=20Valberg=20Gu=C3=B0mundsson?= Date: Wed, 27 Mar 2019 12:57:50 +0100 Subject: [PATCH] Check for orders which are cancelled=False instead of those who are paid to avoid "overselling". --- src/shop/models.py | 2 +- src/shop/tests.py | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/shop/models.py b/src/shop/models.py index 6b3cccf5..7ab42d04 100644 --- a/src/shop/models.py +++ b/src/shop/models.py @@ -418,7 +418,7 @@ class Product(CreatedUpdatedModel, UUIDModel): if self.stock_amount: sold = OrderProductRelation.objects.filter( product=self, - order__paid=True, + order__cancelled=False, ).aggregate(Sum('quantity'))['quantity__sum'] total_left = self.stock_amount - (sold or 0) diff --git a/src/shop/tests.py b/src/shop/tests.py index f28ec463..6646edef 100644 --- a/src/shop/tests.py +++ b/src/shop/tests.py @@ -22,16 +22,21 @@ class ProductAvailabilityTest(TestCase): """ If max orders have been made, the product is NOT available. """ product = ProductFactory(stock_amount=2) - for i in range(2): - opr = OrderProductRelationFactory(product=product) - order = opr.order - order.paid = True - order.save() + opr1 = OrderProductRelationFactory(product=product) + opr2 = OrderProductRelationFactory(product=product) self.assertEqual(product.left_in_stock, 0) self.assertFalse(product.is_stock_available) self.assertFalse(product.is_available()) + # Cancel one order + opr1.order.mark_as_cancelled() + + self.assertEqual(product.left_in_stock, 1) + self.assertTrue(product.is_stock_available) + self.assertTrue(product.is_available()) + + def test_product_available_by_time(self): """ The product is available if now is in the right timeframe. """ product = ProductFactory()