From 5ab4be7025426c1b110978295dfa8ab019bf565f Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 7 Sep 2021 17:36:53 +1000 Subject: [PATCH] Unit test fixes --- InvenTree/stock/models.py | 1 - InvenTree/stock/tasks.py | 6 +++--- InvenTree/stock/tests.py | 12 ++++++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index b125fb55c9..3344dec0eb 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -1305,7 +1305,6 @@ class StockItem(MPTTModel): self.quantity = quantity - if quantity == 0 and self.delete_on_deplete and self.can_delete(): self.mark_for_deletion() diff --git a/InvenTree/stock/tasks.py b/InvenTree/stock/tasks.py index b86e4e8ca9..9fbd875384 100644 --- a/InvenTree/stock/tasks.py +++ b/InvenTree/stock/tasks.py @@ -30,6 +30,6 @@ def delete_old_stock_items(): items = StockItem.objects.filter(scheduled_for_deletion=True) - logger.info(f"Removing {items.count()} StockItem objects scheduled for deletion") - - items.delete() + if items.count() > 0: + logger.info(f"Removing {items.count()} StockItem objects scheduled for deletion") + items.delete() diff --git a/InvenTree/stock/tests.py b/InvenTree/stock/tests.py index 205fb417a8..52f934544f 100644 --- a/InvenTree/stock/tests.py +++ b/InvenTree/stock/tests.py @@ -332,6 +332,8 @@ class StockTest(TestCase): w1 = StockItem.objects.get(pk=100) w2 = StockItem.objects.get(pk=101) + self.assertFalse(w2.scheduled_for_depletion) + # Take 25 units from w1 (there are only 10 in stock) w1.take_stock(30, None, notes='Took 30') @@ -342,6 +344,16 @@ class StockTest(TestCase): # Take 25 units from w2 (will be deleted) w2.take_stock(30, None, notes='Took 30') + # w2 should now be marked for future deletion + w2 = StockItem.objects.get(pk=101) + self.assertTrue(w2.scheduled_for_depletion) + + from stock.tasks import delete_old_stock_items + + # Now run the "background task" to delete these stock items + delete_old_stock_items() + + # This StockItem should now have been deleted with self.assertRaises(StockItem.DoesNotExist): w2 = StockItem.objects.get(pk=101)