From d2d59e07095af36b8b29b04e03186a53547f76ef Mon Sep 17 00:00:00 2001 From: Bobbe <34186858+30350n@users.noreply.github.com> Date: Sun, 14 Jan 2024 02:44:34 +0100 Subject: [PATCH] Add tests for get_purchase_orders logic (#6236) --- .../plugin/base/barcodes/test_barcode.py | 141 ++++++++++++++++++ 1 file changed, 141 insertions(+) diff --git a/InvenTree/plugin/base/barcodes/test_barcode.py b/InvenTree/plugin/base/barcodes/test_barcode.py index 7b20a684b9..f698590884 100644 --- a/InvenTree/plugin/base/barcodes/test_barcode.py +++ b/InvenTree/plugin/base/barcodes/test_barcode.py @@ -8,6 +8,8 @@ from InvenTree.unit_test import InvenTreeAPITestCase from part.models import Part from stock.models import StockItem +from .mixins import SupplierBarcodeMixin + class BarcodeAPITest(InvenTreeAPITestCase): """Tests for barcode api.""" @@ -354,3 +356,142 @@ class SOAllocateTest(InvenTreeAPITestCase): self.line_item.refresh_from_db() self.assertEqual(self.line_item.allocated_quantity(), 10) self.assertTrue(self.line_item.is_fully_allocated()) + + +class SupplierBarcodeMixinTest(InvenTreeAPITestCase): + """Unit tests for the SupplierBarcodeMixin class.""" + + @classmethod + def setUpTestData(cls): + """Setup for all tests.""" + super().setUpTestData() + + cls.supplier = company.models.Company.objects.create( + name='Supplier Barcode Mixin Test Company', is_supplier=True + ) + + cls.supplier_other = company.models.Company.objects.create( + name='Other Supplier Barcode Mixin Test Company', is_supplier=True + ) + + cls.supplier_no_orders = company.models.Company.objects.create( + name='Supplier Barcode Mixin Test Company with no Orders', is_supplier=True + ) + + cls.purchase_order_pending = order.models.PurchaseOrder.objects.create( + status=order.models.PurchaseOrderStatus.PENDING.value, + supplier=cls.supplier, + supplier_reference='ORDER#1337', + ) + + cls.purchase_order_1 = order.models.PurchaseOrder.objects.create( + status=order.models.PurchaseOrderStatus.PLACED.value, + supplier=cls.supplier, + supplier_reference='ORDER#1338', + ) + + cls.purchase_order_duplicate_1 = order.models.PurchaseOrder.objects.create( + status=order.models.PurchaseOrderStatus.PLACED.value, + supplier=cls.supplier, + supplier_reference='ORDER#1339', + ) + + cls.purchase_order_duplicate_2 = order.models.PurchaseOrder.objects.create( + status=order.models.PurchaseOrderStatus.PLACED.value, + supplier=cls.supplier_other, + supplier_reference='ORDER#1339', + ) + + def setUp(self): + """Setup method for each test.""" + super().setUp() + + def test_order_not_placed(self): + """Check that purchase order which has not been placed doesn't get returned.""" + purchase_orders = SupplierBarcodeMixin.get_purchase_orders( + self.purchase_order_pending.reference, None + ) + self.assertIsNone(purchase_orders.first()) + + purchase_orders = SupplierBarcodeMixin.get_purchase_orders( + None, self.purchase_order_pending.supplier_reference + ) + self.assertIsNone(purchase_orders.first()) + + def test_order_simple(self): + """Check that we can get a purchase order by either reference, supplier_reference or both.""" + purchase_orders = SupplierBarcodeMixin.get_purchase_orders( + self.purchase_order_1.reference, None + ) + self.assertEqual(purchase_orders.count(), 1) + self.assertEqual(purchase_orders.first(), self.purchase_order_1) + + purchase_orders = SupplierBarcodeMixin.get_purchase_orders( + None, self.purchase_order_1.supplier_reference + ) + self.assertEqual(purchase_orders.count(), 1) + self.assertEqual(purchase_orders.first(), self.purchase_order_1) + + purchase_orders = SupplierBarcodeMixin.get_purchase_orders( + self.purchase_order_1.reference, self.purchase_order_1.supplier_reference + ) + self.assertEqual(purchase_orders.count(), 1) + self.assertEqual(purchase_orders.first(), self.purchase_order_1) + + purchase_orders = SupplierBarcodeMixin.get_purchase_orders( + self.purchase_order_1.reference, + self.purchase_order_1.supplier_reference, + supplier=self.supplier, + ) + self.assertEqual(purchase_orders.count(), 1) + self.assertEqual(purchase_orders.first(), self.purchase_order_1) + + def test_wrong_supplier_order(self): + """Check that no orders get returned if the wrong supplier is specified.""" + purchase_orders = SupplierBarcodeMixin.get_purchase_orders( + self.purchase_order_1.reference, None, supplier=self.supplier_no_orders + ) + self.assertIsNone(purchase_orders.first()) + + purchase_orders = SupplierBarcodeMixin.get_purchase_orders( + None, + self.purchase_order_1.supplier_reference, + supplier=self.supplier_no_orders, + ) + self.assertIsNone(purchase_orders.first()) + + def test_supplier_order_duplicate(self): + """Test getting purchase_orders with the same supplier_reference works correctly.""" + purchase_orders = SupplierBarcodeMixin.get_purchase_orders( + None, self.purchase_order_duplicate_1.supplier_reference + ) + self.assertEqual(purchase_orders.count(), 2) + self.assertEqual( + set(purchase_orders), + {self.purchase_order_duplicate_1, self.purchase_order_duplicate_2}, + ) + + purchase_orders = SupplierBarcodeMixin.get_purchase_orders( + self.purchase_order_duplicate_1.reference, + self.purchase_order_duplicate_1.supplier_reference, + ) + self.assertEqual(purchase_orders.count(), 1) + self.assertEqual(purchase_orders.first(), self.purchase_order_duplicate_1) + + # check that mixing the reference and supplier_reference doesn't work + + purchase_orders = SupplierBarcodeMixin.get_purchase_orders( + self.purchase_order_duplicate_1.supplier_reference, + self.purchase_order_duplicate_1.reference, + ) + self.assertIsNone(purchase_orders.first()) + + # check that specifying the right supplier works + + purchase_orders = SupplierBarcodeMixin.get_purchase_orders( + None, + self.purchase_order_duplicate_1.supplier_reference, + supplier=self.supplier_other, + ) + self.assertEqual(purchase_orders.count(), 1) + self.assertEqual(purchase_orders.first(), self.purchase_order_duplicate_2)