From f14a906c361e6ac2d9a5997422c29a457593e09f Mon Sep 17 00:00:00 2001 From: Simone Orsi Date: Tue, 11 May 2021 16:09:28 +0200 Subject: [PATCH] s_packaging_calculator: include barcode --- stock_packaging_calculator/models/product.py | 7 +- .../tests/test_packaging_calc.py | 322 ++++-------------- stock_packaging_calculator/tests/utils.py | 24 ++ 3 files changed, 99 insertions(+), 254 deletions(-) create mode 100644 stock_packaging_calculator/tests/utils.py diff --git a/stock_packaging_calculator/models/product.py b/stock_packaging_calculator/models/product.py index a4327076c..13ae10f3d 100644 --- a/stock_packaging_calculator/models/product.py +++ b/stock_packaging_calculator/models/product.py @@ -9,7 +9,7 @@ from odoo.tools import float_compare from odoo.addons.base_sparse_field.models.fields import Serialized # Unify records as we mix up w/ UoM -Packaging = namedtuple("Packaging", "id name qty is_unit") +Packaging = namedtuple("Packaging", "id name qty barcode is_unit") class Product(models.Model): @@ -83,7 +83,7 @@ class Product(models.Model): name_getter = self.env.context.get("_packaging_name_getter", lambda x: x.name) packagings = sorted( [ - Packaging(x.id, name_getter(x), x.qty, False) + Packaging(x.id, name_getter(x), x.qty, x.barcode, False) for x in self.packaging_ids.filtered(custom_filter) # Exclude the ones w/ zero qty as they are useless for the math if x.qty @@ -96,7 +96,7 @@ class Product(models.Model): # NOTE: the ID here could clash w/ one of the packaging's. # If you create a mapping based on IDs, keep this in mind. # You can use `is_unit` to check this. - Packaging(self.uom_id.id, self.uom_id.name, self.uom_id.factor, True) + Packaging(self.uom_id.id, self.uom_id.name, self.uom_id.factor, None, True) ) return packagings @@ -140,4 +140,5 @@ class Product(models.Model): "qty": qty_per_pkg, "name": packaging.name, "is_unit": packaging.is_unit, + "barcode": packaging.barcode, } diff --git a/stock_packaging_calculator/tests/test_packaging_calc.py b/stock_packaging_calculator/tests/test_packaging_calc.py index 1bc721086..99470229b 100644 --- a/stock_packaging_calculator/tests/test_packaging_calc.py +++ b/stock_packaging_calculator/tests/test_packaging_calc.py @@ -2,11 +2,14 @@ # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl) from odoo.tests import SavepointCase +from .utils import make_pkg_values + class TestCalc(SavepointCase): at_install = False post_install = True + maxDiff = None @classmethod def setUpClass(cls): @@ -21,43 +24,32 @@ class TestCalc(SavepointCase): } ) cls.pkg_box = cls.env["product.packaging"].create( - {"name": "Box", "product_id": cls.product_a.id, "qty": 50} + {"name": "Box", "product_id": cls.product_a.id, "qty": 50, "barcode": "BOX"} ) cls.pkg_big_box = cls.env["product.packaging"].create( - {"name": "Big Box", "product_id": cls.product_a.id, "qty": 200} + { + "name": "Big Box", + "product_id": cls.product_a.id, + "qty": 200, + "barcode": "BIGBOX", + } ) cls.pkg_pallet = cls.env["product.packaging"].create( - {"name": "Pallet", "product_id": cls.product_a.id, "qty": 2000} + { + "name": "Pallet", + "product_id": cls.product_a.id, + "qty": 2000, + "barcode": "PALLET", + } ) def test_contained_mapping(self): self.assertEqual( self.product_a.packaging_contained_mapping, { - str(self.pkg_pallet.id): [ - { - "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, - "is_unit": False, - }, - ], - str(self.pkg_box.id): [ - { - "id": self.uom_unit.id, - "qty": 50, - "name": self.uom_unit.name, - "is_unit": True, - }, - ], + str(self.pkg_pallet.id): [make_pkg_values(self.pkg_big_box, qty=10)], + str(self.pkg_big_box.id): [make_pkg_values(self.pkg_box, qty=4)], + str(self.pkg_box.id): [make_pkg_values(self.uom_unit, qty=50)], }, ) # Update pkg qty @@ -65,132 +57,51 @@ class TestCalc(SavepointCase): self.assertEqual( self.product_a.packaging_contained_mapping, { - str(self.pkg_pallet.id): [ - { - "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, - "is_unit": False, - }, - ], - str(self.pkg_box.id): [ - { - "id": self.uom_unit.id, - "qty": 50, - "name": self.uom_unit.name, - "is_unit": True, - }, - ], + str(self.pkg_pallet.id): [make_pkg_values(self.pkg_big_box, qty=20)], + str(self.pkg_big_box.id): [make_pkg_values(self.pkg_box, qty=4)], + str(self.pkg_box.id): [make_pkg_values(self.uom_unit, qty=50)], }, ) def test_calc_1(self): """Test easy behavior 1.""" expected = [ - { - "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, - }, + make_pkg_values(self.pkg_pallet, qty=1), + make_pkg_values(self.pkg_big_box, qty=3), + make_pkg_values(self.pkg_box, qty=1), + make_pkg_values(self.uom_unit, qty=5), ] 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, - "is_unit": False, - }, - { - "id": self.pkg_box.id, - "qty": 3, - "name": self.pkg_box.name, - "is_unit": False, - }, + make_pkg_values(self.pkg_big_box, qty=1), + make_pkg_values(self.pkg_box, qty=3), ] 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, - "is_unit": False, - }, - { - "id": self.uom_unit.id, - "qty": 30, - "name": self.uom_unit.name, - "is_unit": True, - }, + make_pkg_values(self.pkg_box, qty=1), + make_pkg_values(self.uom_unit, qty=30), ] 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, - "is_unit": False, - }, + make_pkg_values(self.pkg_box, qty=1), ] 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, - "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, - }, + make_pkg_values(self.pkg_big_box, qty=13), + make_pkg_values(self.pkg_box, qty=1), + make_pkg_values(self.uom_unit, qty=5), ] self.assertEqual( self.product_a.with_context( @@ -202,30 +113,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, - "is_unit": False, - }, - { - "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, - }, + make_pkg_values(self.pkg_pallet, qty=1, name="FOO " + self.pkg_pallet.name), + make_pkg_values( + self.pkg_big_box, qty=3, name="FOO " + self.pkg_big_box.name + ), + make_pkg_values(self.pkg_box, qty=1, name="FOO " + self.pkg_box.name), + make_pkg_values(self.uom_unit, qty=5, name=self.uom_unit.name), ] self.assertEqual( self.product_a.with_context( @@ -255,55 +148,20 @@ class TestCalc(SavepointCase): def test_calc_sub1(self): """Test contained packaging behavior 1.""" expected = [ - { - "id": self.pkg_pallet.id, - "qty": 1, - "name": self.pkg_pallet.name, - "is_unit": False, - "contained": [ - { - "id": self.pkg_big_box.id, - "qty": 10, - "name": self.pkg_big_box.name, - "is_unit": False, - }, - ], - }, - { - "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, - "is_unit": False, - }, - ], - }, - { - "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, - "is_unit": True, - }, - ], - }, - { - "id": self.uom_unit.id, - "qty": 5, - "name": self.uom_unit.name, - "is_unit": True, - "contained": None, - }, + make_pkg_values( + self.pkg_pallet, + qty=1, + contained=[make_pkg_values(self.pkg_big_box, qty=10)], + ), + make_pkg_values( + self.pkg_big_box, + qty=3, + contained=[make_pkg_values(self.pkg_box, qty=4)], + ), + make_pkg_values( + self.pkg_box, qty=1, contained=[make_pkg_values(self.uom_unit, qty=50)], + ), + make_pkg_values(self.uom_unit, qty=5, contained=None), ] self.assertEqual( self.product_a.product_qty_by_packaging(2655, with_contained=True), @@ -311,64 +169,26 @@ class TestCalc(SavepointCase): ) def test_calc_sub2(self): - """Test contained packaging behavior 1.""" + """Test contained packaging behavior 2.""" self.pkg_box.qty = 30 expected = [ - { - "id": self.pkg_pallet.id, - "qty": 1, - "name": self.pkg_pallet.name, - "is_unit": False, - "contained": [ - { - "id": self.pkg_big_box.id, - "qty": 10, - "name": self.pkg_big_box.name, - "is_unit": False, - }, + make_pkg_values( + self.pkg_pallet, + qty=1, + contained=[make_pkg_values(self.pkg_big_box, qty=10)], + ), + make_pkg_values( + self.pkg_big_box, + qty=3, + contained=[ + make_pkg_values(self.pkg_box, qty=6), + make_pkg_values(self.uom_unit, qty=20), ], - }, - { - "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, - "is_unit": False, - }, - { - "id": self.uom_unit.id, - "qty": 20, - "name": self.uom_unit.name, - "is_unit": True, - }, - ], - }, - { - "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, - "is_unit": True, - }, - ], - }, - { - "id": self.uom_unit.id, - "qty": 25, - "name": self.uom_unit.name, - "is_unit": True, - "contained": None, - }, + ), + make_pkg_values( + self.pkg_box, qty=1, contained=[make_pkg_values(self.uom_unit, qty=30)], + ), + make_pkg_values(self.uom_unit, qty=25, contained=None), ] self.assertEqual( self.product_a.product_qty_by_packaging(2655, with_contained=True), diff --git a/stock_packaging_calculator/tests/utils.py b/stock_packaging_calculator/tests/utils.py new file mode 100644 index 000000000..cd9e12560 --- /dev/null +++ b/stock_packaging_calculator/tests/utils.py @@ -0,0 +1,24 @@ +# Copyright 2021 Camptocamp SA +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl) + + +def make_pkg_values(record, **kw): + """Helper to generate test values for packaging. + """ + if record._name == "uom.uom": + is_unit = True + barcode = None + qty = record.factor + elif record._name == "product.packaging": + qty = record.qty + is_unit = False + barcode = record.barcode + values = { + "id": record.id, + "name": record.name, + "qty": qty, + "barcode": barcode, + "is_unit": is_unit, + } + values.update(kw) + return values