From 421db61f21b89b04ea244d580d633fb4f3c07144 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 28 Feb 2022 23:09:57 +1100 Subject: [PATCH] Adding unit testing for new features --- InvenTree/order/models.py | 5 +- InvenTree/order/serializers.py | 1 + InvenTree/order/test_api.py | 102 +++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 2 deletions(-) diff --git a/InvenTree/order/models.py b/InvenTree/order/models.py index 3b1cad2ff5..7f5c6b8164 100644 --- a/InvenTree/order/models.py +++ b/InvenTree/order/models.py @@ -440,8 +440,9 @@ class PurchaseOrder(Order): # Determine if we should individually serialize the items, or not if type(serials) is list and len(serials) > 0: - quantity = 1 + serialize = True else: + serialize = False serials = [None] for sn in serials: @@ -450,7 +451,7 @@ class PurchaseOrder(Order): part=line.part.part, supplier_part=line.part, location=location, - quantity=quantity, + quantity=1 if serialize else quantity, purchase_order=self, status=status, batch=batch_code, diff --git a/InvenTree/order/serializers.py b/InvenTree/order/serializers.py index 216bc63b11..cf8e701c68 100644 --- a/InvenTree/order/serializers.py +++ b/InvenTree/order/serializers.py @@ -329,6 +329,7 @@ class POLineItemReceiveSerializer(serializers.Serializer): return data + class POReceiveSerializer(serializers.Serializer): """ Serializer for receiving items against a purchase order diff --git a/InvenTree/order/test_api.py b/InvenTree/order/test_api.py index 2ea2086431..b6fee8a218 100644 --- a/InvenTree/order/test_api.py +++ b/InvenTree/order/test_api.py @@ -529,6 +529,108 @@ class PurchaseOrderReceiveTest(OrderTest): self.assertTrue(StockItem.objects.filter(uid='MY-UNIQUE-BARCODE-123').exists()) self.assertTrue(StockItem.objects.filter(uid='MY-UNIQUE-BARCODE-456').exists()) + def test_batch_code(self): + """ + Test that we can supply a 'batch code' when receiving items + """ + + line_1 = models.PurchaseOrderLineItem.objects.get(pk=1) + line_2 = models.PurchaseOrderLineItem.objects.get(pk=2) + + self.assertEqual(StockItem.objects.filter(supplier_part=line_1.part).count(), 0) + self.assertEqual(StockItem.objects.filter(supplier_part=line_2.part).count(), 0) + + data = { + 'items': [ + { + 'line_item': 1, + 'quantity': 10, + 'batch_code': 'abc-123', + }, + { + 'line_item': 2, + 'quantity': 10, + 'batch_code': 'xyz-789', + } + ], + 'location': 1, + } + + n = StockItem.objects.count() + + self.post( + self.url, + data, + expected_code=201, + ) + + # Check that two new stock items have been created! + self.assertEqual(n + 2, StockItem.objects.count()) + + item_1 = StockItem.objects.filter(supplier_part=line_1.part).first() + item_2 = StockItem.objects.filter(supplier_part=line_2.part).first() + + self.assertEqual(item_1.batch, 'abc-123') + self.assertEqual(item_2.batch, 'xyz-789') + + def test_serial_numbers(self): + """ + Test that we can supply a 'serial number' when receiving items + """ + + line_1 = models.PurchaseOrderLineItem.objects.get(pk=1) + line_2 = models.PurchaseOrderLineItem.objects.get(pk=2) + + self.assertEqual(StockItem.objects.filter(supplier_part=line_1.part).count(), 0) + self.assertEqual(StockItem.objects.filter(supplier_part=line_2.part).count(), 0) + + data = { + 'items': [ + { + 'line_item': 1, + 'quantity': 10, + 'batch_code': 'abc-123', + 'serial_numbers': '100+', + }, + { + 'line_item': 2, + 'quantity': 10, + 'batch_code': 'xyz-789', + } + ], + 'location': 1, + } + + n = StockItem.objects.count() + + self.post( + self.url, + data, + expected_code=201, + ) + + # Check that the expected number of stock items has been created + self.assertEqual(n + 11, StockItem.objects.count()) + + # 10 serialized stock items created for the first line item + self.assertEqual(StockItem.objects.filter(supplier_part=line_1.part).count(), 10) + + # Check that the correct serial numbers have been allocated + for i in range(100, 110): + item = StockItem.objects.get(serial_int=i) + self.assertEqual(item.serial, str(i)) + self.assertEqual(item.quantity, 1) + self.assertEqual(item.batch, 'abc-123') + + # A single stock item (quantity 10) created for the second line item + items = StockItem.objects.filter(supplier_part=line_2.part) + self.assertEqual(items.count(), 1) + + item = items.first() + + self.assertEqual(item.quantity, 10) + self.assertEqual(item.batch, 'xyz-789') + class SalesOrderTest(OrderTest): """