Skip to content

Commit

Permalink
[MIG] product_secondary_unit: Migration to 18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pfranck committed Nov 13, 2024
1 parent 5e63695 commit ddb2e2e
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
98 changes: 98 additions & 0 deletions product_secondary_unit/tests/test_product_second_unit.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Copyright 2018 Tecnativa - Sergio Teruel
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
import logging

from odoo.fields import Command
from odoo.tests import TransactionCase, tagged

_logger = logging.getLogger(__name__)


@tagged("post_install", "-at_install")
class TestProductSecondaryUnit(TransactionCase):
Expand Down Expand Up @@ -39,9 +44,69 @@ def setUpClass(cls):
],
}
)
cls.woods = cls.env["product.template"].create(
{
"name": "Piece of woods",
"list_price": 2000,
"uom_id": cls.product_uom_kg.id,
"uom_po_id": cls.product_uom_kg.id,
"secondary_uom_ids": [
(
0,
0,
{
"code": "A",
"name": "unit-700",
"uom_id": cls.product_uom_unit.id,
"factor": 0.7,
},
),
(
0,
0,
{
"code": "B",
"name": "unit-900",
"uom_id": cls.product_uom_unit.id,
"factor": 0.9,
},
),
],
}
)
cls.secondary_unit = cls.env["product.secondary.unit"].search(
[("product_tmpl_id", "=", cls.product.id)], limit=1
)
cls.densitiy = cls.env["product.attribute"].create(
[
{
"name": "Density",
"sequence": 1,
"value_ids": [
Command.create(
{
"name": "Low",
"sequence": 1,
}
),
Command.create(
{
"name": "Heavy",
"sequence": 2,
}
),
],
}
]
)
cls.low, cls.heavy = cls.densitiy.value_ids
cls.density_attribute_lines = cls.env["product.template.attribute.line"].create(
{
"product_tmpl_id": cls.woods.id,
"attribute_id": cls.densitiy.id,
"value_ids": [Command.set([cls.low.id, cls.heavy.id])],
}
)

def test_product_secondary_unit_name(self):
self.assertEqual(self.secondary_unit.name_get()[0][1], "unit-700-0.7")
Expand All @@ -58,3 +123,36 @@ def test_product_secondary_unit_search(self):
self.assertEqual(len(name_get), 1)
name_get = self.env["product.secondary.unit"].name_search(name="X", args=args)
self.assertEqual(len(name_get), 0)

def test_multi_variant_product_secondary_unit(self):
first_variant = self.woods.product_variant_ids[0]
second_variant = self.woods.product_variant_ids[1]
self.assertEqual(len(self.woods.secondary_uom_ids), 2)
self.assertEqual(first_variant.secondary_uom_ids, self.woods.secondary_uom_ids)

first_variant.write(
{
"secondary_uom_ids": [
(
0,
0,
{
"code": "C",
"name": "unit-1000",
"product_id": first_variant.id,
"uom_id": self.product_uom_unit.id,
"factor": 0.1,
},
),
]
}
)
_logger.info(
f"Template 2Uoms: {self.woods.secondary_uom_ids.mapped('name')}"
f" -- Product var1 2Uoms: {first_variant.secondary_uom_ids.mapped('name')}"
f" -- Product var2 2Uoms: {second_variant.secondary_uom_ids.mapped('name')}"
)
first_variant.invalidate_recordset()
self.assertEqual(len(self.woods.secondary_uom_ids), 3)
self.assertEqual(len(first_variant.secondary_uom_ids), 3)
self.assertEqual(len(second_variant.secondary_uom_ids), 2)
27 changes: 27 additions & 0 deletions product_secondary_unit/tests/test_secondary_unit_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,23 @@ def setUpClass(cls):
"factor": 10,
},
),
(
0,
0,
{
"code": "C20",
"name": "box 20",
"dependency_type": "independent",
"uom_id": cls.product_uom_unit.id,
"factor": 20,
},
),
],
}
)
cls.secondary_unit_box_5 = cls.product_template.secondary_uom_ids[0]
cls.secondary_unit_box_10 = cls.product_template.secondary_uom_ids[1]
cls.secondary_unit_box_20 = cls.product_template.secondary_uom_ids[2]
# Fake model which inherit from
cls.secondary_unit_fake = cls.env["secondary.unit.fake"].create(
{
Expand Down Expand Up @@ -87,6 +99,21 @@ def test_product_secondary_unit_mixin(self):
fake_model._onchange_helper_product_uom_for_secondary()
self.assertEqual(fake_model.secondary_uom_qty, 12)

def test_product_secondary_unit_independent_mixin(self):
fake_model = self.secondary_unit_fake
fake_model.write(
{
"product_uom_qty": 20,
"secondary_uom_qty": 1,
"secondary_uom_id": self.secondary_unit_box_20.id,
}
)
self.assertEqual(fake_model.product_uom_qty, 20)
fake_model.invalidate_recordset()
fake_model.product_uom_id = self.product_uom_dozen
fake_model._onchange_helper_product_uom_for_secondary()
self.assertEqual(fake_model.secondary_uom_qty, 1)

def test_product_secondary_unit_mixin_no_uom(self):
# If secondary_uom_id is not informed product_qty on target model is
# not computed.
Expand Down

0 comments on commit ddb2e2e

Please sign in to comment.