From b5bc72b08db2d265f1f8fe73abae28a7b830d99e Mon Sep 17 00:00:00 2001 From: Tonow-c2c Date: Mon, 6 Jul 2020 16:26:25 +0200 Subject: [PATCH 1/2] [IMP][stock_packaging_calculator] Add key is_unit for in the packaging calculator return --- stock_packaging_calculator/models/product.py | 7 +- .../tests/test_packaging_calc.py | 137 +++++++++++++++--- 2 files changed, 125 insertions(+), 19 deletions(-) diff --git a/stock_packaging_calculator/models/product.py b/stock_packaging_calculator/models/product.py index b81ac2107..220240884 100644 --- a/stock_packaging_calculator/models/product.py +++ b/stock_packaging_calculator/models/product.py @@ -100,7 +100,12 @@ class Product(models.Model): for pkg in pkg_by_qty: qty_per_pkg, qty = self._qty_by_pkg(pkg.qty, qty) if qty_per_pkg: - value = {"id": pkg.id, "qty": qty_per_pkg, "name": pkg.name} + value = { + "id": pkg.id, + "qty": qty_per_pkg, + "name": pkg.name, + "is_unit": pkg.is_unit, + } if with_contained: contained = None if not pkg.is_unit: diff --git a/stock_packaging_calculator/tests/test_packaging_calc.py b/stock_packaging_calculator/tests/test_packaging_calc.py index 7a6b643b3..596451e4e 100644 --- a/stock_packaging_calculator/tests/test_packaging_calc.py +++ b/stock_packaging_calculator/tests/test_packaging_calc.py @@ -39,13 +39,24 @@ class TestCalc(SavepointCase): "id": self.pkg_big_box.id, "qty": 10, "name": self.pkg_big_box.name, + "is_unit": False, }, ], str(self.pkg_big_box.id): [ - {"id": self.pkg_box.id, "qty": 4, "name": self.pkg_box.name}, + { + "id": self.pkg_box.id, + "qty": 4, + "name": self.pkg_box.name, + "is_unit": False, + }, ], str(self.pkg_box.id): [ - {"id": self.uom_unit.id, "qty": 50, "name": self.uom_unit.name}, + { + "id": self.uom_unit.id, + "qty": 50, + "name": self.uom_unit.name, + "is_unit": False, + }, ], }, ) @@ -59,13 +70,24 @@ class TestCalc(SavepointCase): "id": self.pkg_big_box.id, "qty": 20, "name": self.pkg_big_box.name, + "is_unit": False, }, ], str(self.pkg_big_box.id): [ - {"id": self.pkg_box.id, "qty": 4, "name": self.pkg_box.name}, + { + "id": self.pkg_box.id, + "qty": 4, + "name": self.pkg_box.name, + "is_unit": False, + }, ], str(self.pkg_box.id): [ - {"id": self.uom_unit.id, "qty": 50, "name": self.uom_unit.name}, + { + "id": self.uom_unit.id, + "qty": 50, + "name": self.uom_unit.name, + "is_unit": False, + }, ], }, ) @@ -73,42 +95,102 @@ class TestCalc(SavepointCase): def test_calc_1(self): """Test easy behavior 1.""" expected = [ - {"id": self.pkg_pallet.id, "qty": 1, "name": self.pkg_pallet.name}, - {"id": self.pkg_big_box.id, "qty": 3, "name": self.pkg_big_box.name}, - {"id": self.pkg_box.id, "qty": 1, "name": self.pkg_box.name}, - {"id": self.uom_unit.id, "qty": 5, "name": self.uom_unit.name}, + { + "id": self.pkg_pallet.id, + "qty": 1, + "name": self.pkg_pallet.name, + "is_unit": False, + }, + { + "id": self.pkg_big_box.id, + "qty": 3, + "name": self.pkg_big_box.name, + "is_unit": False, + }, + { + "id": self.pkg_box.id, + "qty": 1, + "name": self.pkg_box.name, + "is_unit": False, + }, + { + "id": self.uom_unit.id, + "qty": 5, + "name": self.uom_unit.name, + "is_unit": True, + }, ] self.assertEqual(self.product_a.product_qty_by_packaging(2655), expected) def test_calc_2(self): """Test easy behavior 2.""" expected = [ - {"id": self.pkg_big_box.id, "qty": 1, "name": self.pkg_big_box.name}, - {"id": self.pkg_box.id, "qty": 3, "name": self.pkg_box.name}, + { + "id": self.pkg_big_box.id, + "qty": 1, + "name": self.pkg_big_box.name, + "is_unit": False, + }, + { + "id": self.pkg_box.id, + "qty": 3, + "name": self.pkg_box.name, + "is_unit": False, + }, ] self.assertEqual(self.product_a.product_qty_by_packaging(350), expected) def test_calc_3(self): """Test easy behavior 3.""" expected = [ - {"id": self.pkg_box.id, "qty": 1, "name": self.pkg_box.name}, - {"id": self.uom_unit.id, "qty": 30, "name": self.uom_unit.name}, + { + "id": self.pkg_box.id, + "qty": 1, + "name": self.pkg_box.name, + "is_unit": False, + }, + { + "id": self.uom_unit.id, + "qty": 30, + "name": self.uom_unit.name, + "is_unit": True, + }, ] self.assertEqual(self.product_a.product_qty_by_packaging(80), expected) def test_calc_6(self): """Test fractional qty is lost.""" expected = [ - {"id": self.pkg_box.id, "qty": 1, "name": self.pkg_box.name}, + { + "id": self.pkg_box.id, + "qty": 1, + "name": self.pkg_box.name, + "is_unit": False, + }, ] self.assertEqual(self.product_a.product_qty_by_packaging(50.5), expected) def test_calc_filter(self): """Test packaging filter.""" expected = [ - {"id": self.pkg_big_box.id, "qty": 13, "name": self.pkg_big_box.name}, - {"id": self.pkg_box.id, "qty": 1, "name": self.pkg_box.name}, - {"id": self.uom_unit.id, "qty": 5, "name": self.uom_unit.name}, + { + "id": self.pkg_big_box.id, + "qty": 13, + "name": self.pkg_big_box.name, + "is_unit": False, + }, + { + "id": self.pkg_box.id, + "qty": 1, + "name": self.pkg_box.name, + "is_unit": False, + }, + { + "id": self.uom_unit.id, + "qty": 5, + "name": self.uom_unit.name, + "is_unit": True, + }, ] self.assertEqual( self.product_a.with_context( @@ -125,9 +207,20 @@ class TestCalc(SavepointCase): "id": self.pkg_big_box.id, "qty": 3, "name": "FOO " + self.pkg_big_box.name, + "is_unit": False, + }, + { + "id": self.pkg_box.id, + "qty": 1, + "name": "FOO " + self.pkg_box.name, + "is_unit": False, + }, + { + "id": self.uom_unit.id, + "qty": 5, + "name": self.uom_unit.name, + "is_unit": True, }, - {"id": self.pkg_box.id, "qty": 1, "name": "FOO " + self.pkg_box.name}, - {"id": self.uom_unit.id, "qty": 5, "name": self.uom_unit.name}, ] self.assertEqual( self.product_a.with_context( @@ -143,6 +236,7 @@ class TestCalc(SavepointCase): "id": self.pkg_pallet.id, "qty": 1, "name": self.pkg_pallet.name, + "is_unit": False, "contained": [ { "id": self.pkg_big_box.id, @@ -155,6 +249,7 @@ class TestCalc(SavepointCase): "id": self.pkg_big_box.id, "qty": 3, "name": self.pkg_big_box.name, + "is_unit": False, "contained": [ {"id": self.pkg_box.id, "qty": 4, "name": self.pkg_box.name}, ], @@ -163,6 +258,7 @@ class TestCalc(SavepointCase): "id": self.pkg_box.id, "qty": 1, "name": self.pkg_box.name, + "is_unit": False, "contained": [ {"id": self.uom_unit.id, "qty": 50, "name": self.uom_unit.name}, ], @@ -171,6 +267,7 @@ class TestCalc(SavepointCase): "id": self.uom_unit.id, "qty": 5, "name": self.uom_unit.name, + "is_unit": True, "contained": None, }, ] @@ -192,6 +289,7 @@ class TestCalc(SavepointCase): "id": self.pkg_big_box.id, "qty": 10, "name": self.pkg_big_box.name, + "is_unit": False, }, ], }, @@ -199,6 +297,7 @@ class TestCalc(SavepointCase): "id": self.pkg_big_box.id, "qty": 3, "name": self.pkg_big_box.name, + "is_unit": False, "contained": [ {"id": self.pkg_box.id, "qty": 6, "name": self.pkg_box.name}, {"id": self.uom_unit.id, "qty": 20, "name": self.uom_unit.name}, @@ -208,6 +307,7 @@ class TestCalc(SavepointCase): "id": self.pkg_box.id, "qty": 1, "name": self.pkg_box.name, + "is_unit": False, "contained": [ {"id": self.uom_unit.id, "qty": 30, "name": self.uom_unit.name}, ], @@ -216,6 +316,7 @@ class TestCalc(SavepointCase): "id": self.uom_unit.id, "qty": 25, "name": self.uom_unit.name, + "is_unit": True, "contained": None, }, ] From eb859046fb7ba3439ec108a2c6900957b8c0f717 Mon Sep 17 00:00:00 2001 From: Akim Juillerat Date: Thu, 16 Jul 2020 10:35:53 +0200 Subject: [PATCH 2/2] stock_packaging_calculator: Add hook on packaging values Fix tests --- stock_packaging_calculator/models/product.py | 16 ++++--- .../tests/test_packaging_calc.py | 48 +++++++++++++++---- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/stock_packaging_calculator/models/product.py b/stock_packaging_calculator/models/product.py index 220240884..7b5eda783 100644 --- a/stock_packaging_calculator/models/product.py +++ b/stock_packaging_calculator/models/product.py @@ -63,6 +63,7 @@ class Product(models.Model): {contained: [{id: 1, qty: 4, name: "Big box"}]} """ + self.ensure_one() return self._product_qty_by_packaging( self._ordered_packaging(), prod_qty, with_contained=with_contained, ) @@ -100,12 +101,7 @@ class Product(models.Model): for pkg in pkg_by_qty: qty_per_pkg, qty = self._qty_by_pkg(pkg.qty, qty) if qty_per_pkg: - value = { - "id": pkg.id, - "qty": qty_per_pkg, - "name": pkg.name, - "is_unit": pkg.is_unit, - } + value = self._prepare_qty_by_packaging_values(pkg, qty_per_pkg) if with_contained: contained = None if not pkg.is_unit: @@ -128,3 +124,11 @@ class Product(models.Model): qty -= pkg_qty qty_per_pkg += 1 return qty_per_pkg, qty + + def _prepare_qty_by_packaging_values(self, packaging, qty_per_pkg): + return { + "id": packaging.id, + "qty": qty_per_pkg, + "name": packaging.name, + "is_unit": packaging.is_unit, + } diff --git a/stock_packaging_calculator/tests/test_packaging_calc.py b/stock_packaging_calculator/tests/test_packaging_calc.py index 596451e4e..b7c7b37f9 100644 --- a/stock_packaging_calculator/tests/test_packaging_calc.py +++ b/stock_packaging_calculator/tests/test_packaging_calc.py @@ -55,7 +55,7 @@ class TestCalc(SavepointCase): "id": self.uom_unit.id, "qty": 50, "name": self.uom_unit.name, - "is_unit": False, + "is_unit": True, }, ], }, @@ -86,7 +86,7 @@ class TestCalc(SavepointCase): "id": self.uom_unit.id, "qty": 50, "name": self.uom_unit.name, - "is_unit": False, + "is_unit": True, }, ], }, @@ -202,7 +202,12 @@ class TestCalc(SavepointCase): def test_calc_name_get(self): """Test custom name getter.""" expected = [ - {"id": self.pkg_pallet.id, "qty": 1, "name": "FOO " + self.pkg_pallet.name}, + { + "id": self.pkg_pallet.id, + "qty": 1, + "name": "FOO " + self.pkg_pallet.name, + "is_unit": False, + }, { "id": self.pkg_big_box.id, "qty": 3, @@ -242,6 +247,7 @@ class TestCalc(SavepointCase): "id": self.pkg_big_box.id, "qty": 10, "name": self.pkg_big_box.name, + "is_unit": False, }, ], }, @@ -251,7 +257,12 @@ class TestCalc(SavepointCase): "name": self.pkg_big_box.name, "is_unit": False, "contained": [ - {"id": self.pkg_box.id, "qty": 4, "name": self.pkg_box.name}, + { + "id": self.pkg_box.id, + "qty": 4, + "name": self.pkg_box.name, + "is_unit": False, + }, ], }, { @@ -260,7 +271,12 @@ class TestCalc(SavepointCase): "name": self.pkg_box.name, "is_unit": False, "contained": [ - {"id": self.uom_unit.id, "qty": 50, "name": self.uom_unit.name}, + { + "id": self.uom_unit.id, + "qty": 50, + "name": self.uom_unit.name, + "is_unit": True, + }, ], }, { @@ -284,6 +300,7 @@ class TestCalc(SavepointCase): "id": self.pkg_pallet.id, "qty": 1, "name": self.pkg_pallet.name, + "is_unit": False, "contained": [ { "id": self.pkg_big_box.id, @@ -299,8 +316,18 @@ class TestCalc(SavepointCase): "name": self.pkg_big_box.name, "is_unit": False, "contained": [ - {"id": self.pkg_box.id, "qty": 6, "name": self.pkg_box.name}, - {"id": self.uom_unit.id, "qty": 20, "name": self.uom_unit.name}, + { + "id": self.pkg_box.id, + "qty": 6, + "name": self.pkg_box.name, + "is_unit": False, + }, + { + "id": self.uom_unit.id, + "qty": 20, + "name": self.uom_unit.name, + "is_unit": True, + }, ], }, { @@ -309,7 +336,12 @@ class TestCalc(SavepointCase): "name": self.pkg_box.name, "is_unit": False, "contained": [ - {"id": self.uom_unit.id, "qty": 30, "name": self.uom_unit.name}, + { + "id": self.uom_unit.id, + "qty": 30, + "name": self.uom_unit.name, + "is_unit": True, + }, ], }, {