[IMP] mrp_bom_attribute_match: improve setUpClass

This commit is contained in:
Ivàn Todorovich
2023-01-31 13:58:47 -03:00
committed by Ilyas
parent 762f31bb21
commit 8054deee56

View File

@@ -1,3 +1,5 @@
from odoo import Command
from odoo.models import BaseModel
from odoo.tests import Form, TransactionCase from odoo.tests import Form, TransactionCase
@@ -5,130 +7,162 @@ class TestMrpBomAttributeMatchBase(TransactionCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
super().setUpClass() super().setUpClass()
cls._create_products(cls) cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
cls._create_boms(cls) cls.warehouse = cls.env.ref("stock.warehouse0")
cls.route_manufacture = cls.warehouse.manufacture_pull_id.route_id
def _create_products(self): # Create products
self.warehouse = self.env.ref("stock.warehouse0") cls.product_sword = cls.env["product.template"].create(
route_manufacture = self.warehouse.manufacture_pull_id.route_id.id
self.product_sword = self.env["product.template"].create(
{ {
"name": "Plastic Sword", "name": "Plastic Sword",
"type": "product", "type": "product",
} }
) )
self.product_surf = self.env["product.template"].create( cls.product_surf = cls.env["product.template"].create(
{ {
"name": "Surf", "name": "Surf",
"type": "product", "type": "product",
} }
) )
self.product_fin = self.env["product.template"].create( cls.product_fin = cls.env["product.template"].create(
{ {
"name": "Surf Fin", "name": "Surf Fin",
"type": "product", "type": "product",
} }
) )
self.product_plastic = self.env["product.template"].create( cls.product_plastic = cls.env["product.template"].create(
{ {
"name": "Plastic Component", "name": "Plastic Component",
"type": "product", "type": "product",
} }
) )
self.p1 = self.env["product.template"].create( cls.p1 = cls.env["product.template"].create(
{ {
"name": "P1", "name": "P1",
"type": "product", "type": "product",
"route_ids": [(6, 0, [route_manufacture])], "route_ids": [Command.link(cls.route_manufacture.id)],
} }
) )
self.p2 = self.env["product.template"].create( cls.p2 = cls.env["product.template"].create(
{ {
"name": "P2", "name": "P2",
"type": "product", "type": "product",
"route_ids": [(6, 0, [route_manufacture])], "route_ids": [Command.link(cls.route_manufacture.id)],
} }
) )
self.p3 = self.env["product.template"].create( cls.p3 = cls.env["product.template"].create(
{ {
"name": "P3", "name": "P3",
"type": "product", "type": "product",
"route_ids": [(6, 0, [route_manufacture])], "route_ids": [Command.link(cls.route_manufacture.id)],
} }
) )
self.product_9 = self.env["product.product"].create( cls.product_9 = cls.env["product.product"].create(
{ {
"name": "Paper", "name": "Paper",
} }
) )
self.product_10 = self.env["product.product"].create( cls.product_10 = cls.env["product.product"].create(
{ {
"name": "Stone", "name": "Stone",
} }
) )
self.product_attribute = self.env["product.attribute"].create( cls.product_attribute = cls.env["product.attribute"].create(
{"name": "Colour", "display_type": "radio", "create_variant": "always"} {"name": "Colour", "display_type": "radio", "create_variant": "always"}
) )
self.attribute_value_ids = self.env["product.attribute.value"].create( cls.attribute_value_ids = cls.env["product.attribute.value"].create(
[ [
{"name": "Cyan", "attribute_id": self.product_attribute.id}, {"name": "Cyan", "attribute_id": cls.product_attribute.id},
{"name": "Magenta", "attribute_id": self.product_attribute.id}, {"name": "Magenta", "attribute_id": cls.product_attribute.id},
] ]
) )
self.plastic_attrs = self.env["product.template.attribute.line"].create( cls.plastic_attrs = cls.env["product.template.attribute.line"].create(
{ {
"attribute_id": self.product_attribute.id, "attribute_id": cls.product_attribute.id,
"product_tmpl_id": self.product_plastic.id, "product_tmpl_id": cls.product_plastic.id,
"value_ids": [(6, 0, self.product_attribute.value_ids.ids)], "value_ids": [Command.set(cls.product_attribute.value_ids.ids)],
} }
) )
self.sword_attrs = self.env["product.template.attribute.line"].create( cls.sword_attrs = cls.env["product.template.attribute.line"].create(
{ {
"attribute_id": self.product_attribute.id, "attribute_id": cls.product_attribute.id,
"product_tmpl_id": self.product_sword.id, "product_tmpl_id": cls.product_sword.id,
"value_ids": [(6, 0, self.product_attribute.value_ids.ids)], "value_ids": [Command.set(cls.product_attribute.value_ids.ids)],
} }
) )
# Create boms
cls.bom_id = cls._create_bom(
cls.product_sword,
[
dict(
component_template_id=cls.product_plastic.id,
product_qty=1,
),
],
)
cls.fin_bom_id = cls._create_bom(
cls.product_fin,
[
dict(
product_id=cls.product_plastic.product_variant_ids[0],
product_qty=1,
),
],
)
cls.surf_bom_id = cls._create_bom(
cls.product_surf,
[
dict(
product_id=cls.product_fin.product_variant_ids[0],
product_qty=1,
),
],
)
cls.p1_bom_id = cls._create_bom(
cls.p1,
[
dict(
product_id=cls.p2.product_variant_ids[0],
product_qty=1,
),
],
)
cls.p2_bom_id = cls._create_bom(
cls.p2,
[
dict(
product_id=cls.p3.product_variant_ids[0],
product_qty=1,
),
],
)
cls.p3_bom_id = cls._create_bom(
cls.p3,
[
dict(
product_id=cls.p1.product_variant_ids[0],
product_qty=1,
),
],
)
def _create_boms(self): @classmethod
mrp_bom_form = Form(self.env["mrp.bom"]) def _create_bom(cls, product, line_form_vals):
mrp_bom_form.product_tmpl_id = self.product_sword if product._name == "product.template":
with mrp_bom_form.bom_line_ids.new() as line_form: template = product
line_form.component_template_id = self.product_plastic product = cls.env["product.product"]
line_form.product_qty = 1 else:
self.bom_id = mrp_bom_form.save() template = product.product_tmpl_id
with Form(cls.env["mrp.bom"]) as form:
mrp_bom_form = Form(self.env["mrp.bom"]) form.product_tmpl_id = template
mrp_bom_form.product_tmpl_id = self.product_fin form.product_id = product
with mrp_bom_form.bom_line_ids.new() as line_form: for vals in line_form_vals:
line_form.product_id = self.product_plastic.product_variant_ids[0] with form.bom_line_ids.new() as line_form:
line_form.product_qty = 1 for key, value in vals.items():
self.fin_bom_id = mrp_bom_form.save() field = line_form._model._fields.get(key)
if field and field.relational: # pragma: no cover
mrp_bom_form = Form(self.env["mrp.bom"]) if value and not isinstance(value, BaseModel):
mrp_bom_form.product_tmpl_id = self.product_surf value = cls.env[field.comodel_name].browse(value)
with mrp_bom_form.bom_line_ids.new() as line_form: elif not value:
line_form.product_id = self.product_fin.product_variant_ids[0] value = cls.env[field.comodel_name]
line_form.product_qty = 1 setattr(line_form, key, value)
self.surf_bom_id = mrp_bom_form.save() return form.save()
mrp_bom_form = Form(self.env["mrp.bom"])
mrp_bom_form.product_tmpl_id = self.p1
with mrp_bom_form.bom_line_ids.new() as line_form:
line_form.product_id = self.p2.product_variant_ids[0]
line_form.product_qty = 1
self.p1_bom_id = mrp_bom_form.save()
mrp_bom_form = Form(self.env["mrp.bom"])
mrp_bom_form.product_tmpl_id = self.p2
with mrp_bom_form.bom_line_ids.new() as line_form:
line_form.product_id = self.p3.product_variant_ids[0]
line_form.product_qty = 1
self.p2_bom_id = mrp_bom_form.save()
mrp_bom_form = Form(self.env["mrp.bom"])
mrp_bom_form.product_tmpl_id = self.p3
with mrp_bom_form.bom_line_ids.new() as line_form:
line_form.product_id = self.p1.product_variant_ids[0]
line_form.product_qty = 1
self.p3_bom_id = mrp_bom_form.save()