From 1f465bab7594ac5938476e86c85bf073d7a32b93 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 25 Nov 2024 11:23:34 +0000 Subject: [PATCH] Add unit test - Unit test to ensure event is fired when stock item is created OR updated - Ref: https://github.com/inventree/InvenTree/pull/8546 - Ref: https://github.com/inventree/InvenTree/issues/8452 --- src/backend/InvenTree/stock/tests.py | 39 ++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/backend/InvenTree/stock/tests.py b/src/backend/InvenTree/stock/tests.py index 27548c3806ba..c83e6bf274eb 100644 --- a/src/backend/InvenTree/stock/tests.py +++ b/src/backend/InvenTree/stock/tests.py @@ -951,6 +951,45 @@ def test_merge(self): # Final purchase price should be the weighted average self.assertAlmostEqual(s1.purchase_price.amount, 16.875, places=3) + def test_notify_low_stock(self): + """Test that the 'notify_low_stock' task is triggered correctly.""" + FUNC_NAME = 'part.tasks.notify_low_stock_if_required' + + from django_q.models import OrmQ + + # Start from a blank slate + OrmQ.objects.all().delete() + + def check_func() -> bool: + """Check that the 'notify_low_stock_if_required' task has been triggered.""" + found = False + for task in OrmQ.objects.all(): + if task.func() == FUNC_NAME: + found = True + break + + # Clear the task queue (for the next test) + OrmQ.objects.all().delete() + + return found + + self.assertFalse(check_func()) + + part = Part.objects.first() + + # Create a new stock item for this part + item = StockItem.objects.create( + part=part, quantity=100, location=StockLocation.objects.first() + ) + + self.assertTrue(check_func()) + self.assertFalse(check_func()) + + # Re-count the stock item + item.stocktake(99, None) + + self.assertTrue(check_func()) + class StockBarcodeTest(StockTestBase): """Run barcode tests for the stock app."""