Skip to content

Commit

Permalink
[REF] sale_mrp_bom: refactor tests to adopt SetUpClass approach movin…
Browse files Browse the repository at this point in the history
…g common methods to proper class
  • Loading branch information
rrebollo committed Oct 29, 2024
1 parent e017981 commit 06354bc
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 76 deletions.
4 changes: 4 additions & 0 deletions sale_mrp_bom/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ Trobz:

* Hai Lang <[email protected]>

Binhex:

* Rolando Pérez <[email protected]>

Other credits
~~~~~~~~~~~~~

Expand Down
4 changes: 4 additions & 0 deletions sale_mrp_bom/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
Trobz:

* Hai Lang <[email protected]>

Binhex:

* Rolando Pérez <[email protected]>
4 changes: 4 additions & 0 deletions sale_mrp_bom/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,10 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<ul class="simple">
<li>Hai Lang &lt;<a class="reference external" href="mailto:hailn&#64;trobz.com">hailn&#64;trobz.com</a>&gt;</li>
</ul>
<p>Binhex:</p>
<ul class="simple">
<li>Rolando Pérez &lt;<a class="reference external" href="mailto:r.perez&#64;binhex.cloud">r.perez&#64;binhex.cloud</a>&gt;</li>
</ul>
</div>
<div class="section" id="other-credits">
<h2><a class="toc-backref" href="#toc-entry-7">Other credits</a></h2>
Expand Down
75 changes: 75 additions & 0 deletions sale_mrp_bom/tests/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Copyright 2024 Binhex Rolando Pérez <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo.tests.common import TransactionCase


class TestSaleMrpBomCommon(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.partner = cls.env.ref("base.res_partner_2")
cls.warehouse = cls.env.ref("stock.warehouse0")
route_manufacture = cls.warehouse.manufacture_pull_id.route_id.id
route_mto = cls.warehouse.mto_pull_id.route_id.id
cls.product_a = cls.create_product(
"Product A", route_ids=[(6, 0, [route_manufacture, route_mto])]
)
cls.product_b = cls.create_product(
"Product B", route_ids=[(6, 0, [route_manufacture, route_mto])]
)
cls.component_a = cls.create_product("Component A", route_ids=[])
cls.component_b = cls.create_product("Component B", route_ids=[])
cls.product_attr_color = cls.env["product.attribute"].create({"name": "Color"})
cls.product_attr_val_red, cls.product_attr_val_green = cls.env[
"product.attribute.value"
].create(
[
{
"name": "red",
"attribute_id": cls.product_attr_color.id,
"sequence": 1,
},
{
"name": "blue",
"attribute_id": cls.product_attr_color.id,
"sequence": 2,
},
]
)

@classmethod
def create_product(cls, name, route_ids):
return cls.env["product.product"].create(
{"name": name, "type": "product", "route_ids": route_ids}
)

@classmethod
def create_sale_order(cls, client_ref):
return cls.env["sale.order"].create(
{"partner_id": cls.partner.id, "client_order_ref": client_ref}
)

@classmethod
def create_bom(cls, template):
return cls.env["mrp.bom"].create(
{"product_tmpl_id": template.id, "type": "normal"}
)

@classmethod
def create_bom_line(cls, bom, product, qty):
cls.env["mrp.bom.line"].create(
{"bom_id": bom.id, "product_id": product.id, "product_qty": qty}
)

@classmethod
def create_sale_order_line(cls, sale_order, product, qty, price, bom):
cls.env["sale.order.line"].create(
{
"order_id": sale_order.id,
"product_id": product.id,
"price_unit": price,
"product_uom_qty": qty,
"bom_id": bom.id,
}
)
104 changes: 28 additions & 76 deletions sale_mrp_bom/tests/test_sale_mrp_bom.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,24 @@
# Copyright 2020 Akretion Renato Lima <[email protected]>
# Copyright 2024 Binhex Rolando Pérez <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo.exceptions import ValidationError
from odoo.tests.common import TransactionCase

from .common import TestSaleMrpBomCommon

class TestSaleMrpLink(TransactionCase):
def setUp(self):
super().setUp()
self.partner = self.env.ref("base.res_partner_2")
self.warehouse = self.env.ref("stock.warehouse0")
route_manufacture = self.warehouse.manufacture_pull_id.route_id.id
route_mto = self.warehouse.mto_pull_id.route_id.id
self.product_a = self._create_product(
"Product A", route_ids=[(6, 0, [route_manufacture, route_mto])]
)
self.product_b = self._create_product(
"Product B", route_ids=[(6, 0, [route_manufacture, route_mto])]
)
self.component_a = self._create_product("Component A", route_ids=[])
self.component_b = self._create_product("Component B", route_ids=[])

def _prepare_bom_lines(self):
class TestSaleMrpLink(TestSaleMrpBomCommon):
def prepare_boms(self):
# Create BOMs
bom_a_v1 = self._create_bom(self.product_a.product_tmpl_id)
self._create_bom_line(bom_a_v1, self.component_a, 1)
bom_a_v2 = self._create_bom(self.product_a.product_tmpl_id)
self._create_bom_line(bom_a_v2, self.component_a, 2)
bom_a_v1 = self.create_bom(self.product_a.product_tmpl_id)
self.create_bom_line(bom_a_v1, self.component_a, 1)
bom_a_v2 = self.create_bom(self.product_a.product_tmpl_id)
self.create_bom_line(bom_a_v2, self.component_a, 2)

bom_b_v1 = self._create_bom(self.product_b.product_tmpl_id)
self._create_bom_line(bom_b_v1, self.component_b, 1)
bom_b_v2 = self._create_bom(self.product_b.product_tmpl_id)
self._create_bom_line(bom_b_v2, self.component_b, 2)
bom_b_v1 = self.create_bom(self.product_b.product_tmpl_id)
self.create_bom_line(bom_b_v1, self.component_b, 1)
bom_b_v2 = self.create_bom(self.product_b.product_tmpl_id)
self.create_bom_line(bom_b_v2, self.component_b, 2)

bom_a, bom_b = bom_a_v2, bom_b_v2
self.boms = {
Expand All @@ -40,66 +27,35 @@ def _prepare_bom_lines(self):
}
return bom_a, bom_b

def _prepare_so(self):
bom_a_v2, bom_b_v2 = self._prepare_bom_lines()
def prepare_so(self):
bom_a_v2, bom_b_v2 = self.prepare_boms()

# Create Sale Order
so = self._create_sale_order(self.partner, "SO1")
self._create_sale_order_line(so, self.product_a, 1, 10.0, bom_a_v2)
self._create_sale_order_line(so, self.product_b, 1, 10.0, bom_b_v2)
so = self.create_sale_order("SO1")
self.create_sale_order_line(so, self.product_a, 1, 10.0, bom_a_v2)
self.create_sale_order_line(so, self.product_b, 1, 10.0, bom_b_v2)
so.action_confirm()
return so, bom_a_v2, bom_b_v2

def _create_bom(self, template):
return self.env["mrp.bom"].create(
{"product_tmpl_id": template.id, "type": "normal"}
)

def _create_bom_line(self, bom, product, qty):
self.env["mrp.bom.line"].create(
{"bom_id": bom.id, "product_id": product.id, "product_qty": qty}
)

def _create_product(self, name, route_ids):
return self.env["product.product"].create(
{"name": name, "type": "product", "route_ids": route_ids}
)

def _create_sale_order(self, partner, client_ref):
return self.env["sale.order"].create(
{"partner_id": partner.id, "client_order_ref": client_ref}
)

def _create_sale_order_line(self, sale_order, product, qty, price, bom):
self.env["sale.order.line"].create(
{
"order_id": sale_order.id,
"product_id": product.id,
"price_unit": price,
"product_uom_qty": qty,
"bom_id": bom.id,
}
)

def test_define_bom_in_sale_line(self):
"""Check manufactured order is created with BOM definied in Sale."""
so, bom_a, bom_b = self._prepare_so()
so, bom_a, bom_b = self.prepare_so()

# Check manufacture order
mos = self.env["mrp.production"].search([("origin", "=", so.name)])
for mo in mos:
self.assertEqual(mo.bom_id, self.boms.get(mo.product_id.id))

Check warning on line 47 in sale_mrp_bom/tests/test_sale_mrp_bom.py

View check run for this annotation

Codecov / codecov/patch

sale_mrp_bom/tests/test_sale_mrp_bom.py#L47

Added line #L47 was not covered by tests

def test_pick_a_pack_confirm(self):
so, bom_a, bom_b = self._prepare_so()
so, bom_a, bom_b = self.prepare_so()
picking, boms = so.picking_ids[0], (bom_a, bom_b)

for i, line in enumerate(picking.move_lines):
values = line._prepare_procurement_values()
self.assertEqual(values["bom_id"], boms[i])

def test_mismatch_product_variant_ids(self):
so, bom_a, bom_b = self._prepare_so()
so, bom_a, bom_b = self.prepare_so()
line_a = so.order_line[0]
self.assertEqual(line_a.bom_id, bom_a)
with self.assertRaises(ValidationError):
Expand All @@ -108,30 +64,26 @@ def test_mismatch_product_variant_ids(self):
def test_accept_bom_with_no_variant(self):
# make variants for template of product A
product_tmpl_a = self.product_a.product_tmpl_id
prod_att_color = self.env["product.attribute"].create({"name": "Color"})
product_attr_val_red, product_attr_val_green = self.env[
"product.attribute.value"
].create(
[
{"name": "red", "attribute_id": prod_att_color.id, "sequence": 1},
{"name": "blue", "attribute_id": prod_att_color.id, "sequence": 2},
]
prod_attr_color = self.product_attr_color
product_attr_val_red, product_attr_val_green = (
self.product_attr_val_red,
self.product_attr_val_green,
)
product_tmpl_a.attribute_line_ids = [
(
0,
0,
{
"attribute_id": prod_att_color.id,
"attribute_id": prod_attr_color.id,
"value_ids": [
(6, 0, [product_attr_val_red.id, product_attr_val_green.id])
],
},
)
]
product_a = product_tmpl_a.product_variant_ids[0]
bom_no_variant = self._create_bom(product_tmpl_a)
so = self._create_sale_order(self.partner, "SO2")
self._create_sale_order_line(so, product_a, 2, 25, bom_no_variant)
bom_no_variant = self.create_bom(product_tmpl_a)
so = self.create_sale_order("SO2")
self.create_sale_order_line(so, product_a, 2, 25, bom_no_variant)
line_a = so.order_line[0]
line_a.bom_id = bom_no_variant

0 comments on commit 06354bc

Please sign in to comment.