[IMP] stock_mts_mto_rule: Refactor tests

Use SavepointCase instead of TransactionCase
Common Test Case
This commit is contained in:
Michael Tietz
2024-05-30 00:48:19 +02:00
parent cd9cf3b539
commit 72b2456b0a
2 changed files with 92 additions and 147 deletions

View File

@@ -0,0 +1,71 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
# Copyright 2024 Michael Tietz (MT Software) <mtietz@mt-software.de>
from odoo.tests.common import SavepointCase
class TestMtoMtsRouteCommon(SavepointCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.move_obj = cls.env["stock.move"]
cls.warehouse = cls.env.ref("stock.warehouse0")
cls.uom = cls.env["uom.uom"].browse(1)
cls.customer_loc = cls.env.ref("stock.stock_location_customers")
cls.product = cls.env["product.product"].create(
{"name": "Test product", "type": "product"}
)
cls.company_partner = cls.env.ref("base.main_partner")
cls.group = cls.env["procurement.group"].create({"name": "test"})
@classmethod
def setUpRules(cls):
cls.warehouse.mto_mts_management = True
cls.procurement_vals = {"warehouse_id": cls.warehouse, "group_id": cls.group}
# Since mrp and purchase modules may not be installed, we need to
# create a dummy step to show that mts, mto, and mts+mto flows work.
# Else, if purchase/manufacture are not installed, the mto would fail.
route_vals = {
"warehouse_selectable": True,
"name": "dummy route",
}
cls.dummy_route = cls.env["stock.location.route"].create(route_vals)
rule_vals = {
"location_id": cls.env.ref("stock.stock_location_stock").id,
"location_src_id": cls.env.ref("stock.stock_location_suppliers").id,
"action": "pull",
"warehouse_id": cls.warehouse.id,
"picking_type_id": cls.env.ref("stock.picking_type_out").id,
"name": "dummy rule",
"route_id": cls.dummy_route.id,
}
cls.dummy_rule = cls.env["stock.rule"].create(rule_vals)
cls.warehouse.write({"route_ids": [(4, cls.dummy_route.id)]})
@classmethod
def _create_quant(cls, qty):
cls.quant = cls.env["stock.quant"].create(
{
"owner_id": cls.company_partner.id,
"location_id": cls.env.ref("stock.stock_location_stock").id,
"product_id": cls.product.id,
"quantity": qty,
}
)
@classmethod
def _run_procurement(cls, qty):
cls.env["procurement.group"].run(
[
cls.group.Procurement(
cls.product,
qty,
cls.uom,
cls.customer_loc,
cls.product.name,
"test",
cls.warehouse.company_id,
cls.procurement_vals,
)
]
)
return cls.move_obj.search([("group_id", "=", cls.group.id)])

View File

@@ -1,78 +1,33 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
# Copyright 2024 Michael Tietz (MT Software) <mtietz@mt-software.de>
from odoo import exceptions from odoo import exceptions
from odoo.tests.common import TransactionCase
from .common import TestMtoMtsRouteCommon
class TestMtoMtsRoute(TransactionCase): class TestMtoMtsRoute(TestMtoMtsRouteCommon):
def _create_quant(self, qty): @classmethod
self.quant = self.env["stock.quant"].create( def setUpClass(cls):
{ super().setUpClass()
"owner_id": self.company_partner.id, cls.warehouse = cls.env.ref("stock.warehouse0")
"location_id": self.env.ref("stock.stock_location_stock").id, cls.setUpRules()
"product_id": self.product.id,
"quantity": qty,
}
)
def test_standard_mto_route(self): def test_standard_mto_route(self):
mto_route = self.env.ref("stock.route_warehouse0_mto") mto_route = self.env.ref("stock.route_warehouse0_mto")
mto_route.active = True mto_route.active = True
self.product.route_ids = [(6, 0, [mto_route.id])] self.product.route_ids = [(6, 0, [mto_route.id])]
self.env["procurement.group"].run( moves = self._run_procurement(2.0)
[
self.group.Procurement(
self.product,
2.0,
self.uom,
self.customer_loc,
self.product.name,
"test",
self.warehouse.company_id,
self.procurement_vals,
)
]
)
moves = self.move_obj.search([("group_id", "=", self.group.id)])
self.assertEqual(len(moves), 2) self.assertEqual(len(moves), 2)
def test_standard_mts_route(self): def test_standard_mts_route(self):
self.env["procurement.group"].run( moves = self._run_procurement(2.0)
[
self.group.Procurement(
self.product,
2.0,
self.uom,
self.customer_loc,
self.product.name,
"test",
self.warehouse.company_id,
self.procurement_vals,
)
]
)
moves = self.move_obj.search([("group_id", "=", self.group.id)])
self.assertEqual(len(moves), 1) self.assertEqual(len(moves), 1)
def test_mts_mto_route_split(self): def test_mts_mto_route_split(self):
mto_mts_route = self.env.ref("stock_mts_mto_rule.route_mto_mts") mto_mts_route = self.env.ref("stock_mts_mto_rule.route_mto_mts")
self.product.route_ids = [(6, 0, [mto_mts_route.id])] self.product.route_ids = [(6, 0, [mto_mts_route.id])]
self._create_quant(1.0) self._create_quant(1.0)
self.env["procurement.group"].run( moves = self._run_procurement(2.0)
[
self.group.Procurement(
self.product,
2.0,
self.uom,
self.customer_loc,
self.product.name,
"test",
self.warehouse.company_id,
self.procurement_vals,
)
]
)
moves = self.env["stock.move"].search([("group_id", "=", self.group.id)])
self.assertEqual(3, len(moves)) self.assertEqual(3, len(moves))
move_mts = self.env["stock.move"].search( move_mts = self.env["stock.move"].search(
[ [
@@ -100,25 +55,8 @@ class TestMtoMtsRoute(TransactionCase):
mto_mts_route.rule_ids.mts_quantity_rule = "full" mto_mts_route.rule_ids.mts_quantity_rule = "full"
self.product.route_ids = [(6, 0, [mto_mts_route.id])] self.product.route_ids = [(6, 0, [mto_mts_route.id])]
self._create_quant(1.0) self._create_quant(1.0)
self.env["procurement.group"].run( moves = self._run_procurement(2.0).filtered(
[ lambda m: m.location_dest_id == self.customer_loc
self.group.Procurement(
self.product,
2.0,
self.uom,
self.customer_loc,
self.product.name,
"test",
self.warehouse.company_id,
self.procurement_vals,
)
]
)
moves = self.env["stock.move"].search(
[
("group_id", "=", self.group.id),
("location_dest_id", "=", self.customer_loc.id),
]
) )
self.assertEqual(1, len(moves)) self.assertEqual(1, len(moves))
self.assertEqual(2.0, moves[0].product_uom_qty) self.assertEqual(2.0, moves[0].product_uom_qty)
@@ -127,52 +65,21 @@ class TestMtoMtsRoute(TransactionCase):
def test_mts_mto_route_mto_only(self): def test_mts_mto_route_mto_only(self):
mto_mts_route = self.env.ref("stock_mts_mto_rule.route_mto_mts") mto_mts_route = self.env.ref("stock_mts_mto_rule.route_mto_mts")
self.product.route_ids = [(6, 0, [mto_mts_route.id])] self.product.route_ids = [(6, 0, [mto_mts_route.id])]
self.env["procurement.group"].run( moves = self._run_procurement(2.0).filtered(
[ lambda m: m.location_dest_id == self.customer_loc
self.group.Procurement(
self.product,
2.0,
self.uom,
self.customer_loc,
self.product.name,
"test",
self.warehouse.company_id,
self.procurement_vals,
)
]
)
moves = self.env["stock.move"].search(
[
("group_id", "=", self.group.id),
("location_dest_id", "=", self.customer_loc.id),
]
) )
self.assertEqual(1, len(moves)) self.assertEqual(1, len(moves))
self.assertEqual(2.0, moves[0].product_uom_qty) self.assertEqual(2.0, moves.product_uom_qty)
self.assertEqual("make_to_order", moves[0].procure_method) self.assertEqual("make_to_order", moves.procure_method)
def test_mts_mto_route_mts_only(self): def test_mts_mto_route_mts_only(self):
mto_mts_route = self.env.ref("stock_mts_mto_rule.route_mto_mts") mto_mts_route = self.env.ref("stock_mts_mto_rule.route_mto_mts")
self.product.route_ids = [(6, 0, [mto_mts_route.id])] self.product.route_ids = [(6, 0, [mto_mts_route.id])]
self._create_quant(3.0) self._create_quant(3.0)
self.env["procurement.group"].run( moves = self._run_procurement(2.0)
[
self.group.Procurement(
self.product,
2.0,
self.uom,
self.customer_loc,
self.product.name,
"test",
self.warehouse.company_id,
self.procurement_vals,
)
]
)
moves = self.env["stock.move"].search([("group_id", "=", self.group.id)])
self.assertEqual(1, len(moves)) self.assertEqual(1, len(moves))
self.assertEqual(2.0, moves[0].product_uom_qty) self.assertEqual(2.0, moves.product_uom_qty)
self.assertEqual("make_to_stock", moves[0].procure_method) self.assertEqual("make_to_stock", moves.procure_method)
def test_mts_mto_rule_contrains(self): def test_mts_mto_rule_contrains(self):
rule = self.env["stock.rule"].search( rule = self.env["stock.rule"].search(
@@ -240,36 +147,3 @@ class TestMtoMtsRoute(TransactionCase):
new_rule_name = rule_name.replace(self.warehouse.name, new_warehouse_name, 1) new_rule_name = rule_name.replace(self.warehouse.name, new_warehouse_name, 1)
self.warehouse.name = new_warehouse_name self.warehouse.name = new_warehouse_name
self.assertEqual(new_rule_name, self.warehouse.mts_mto_rule_id.name) self.assertEqual(new_rule_name, self.warehouse.mts_mto_rule_id.name)
def setUp(self):
super(TestMtoMtsRoute, self).setUp()
self.move_obj = self.env["stock.move"]
self.warehouse = self.env.ref("stock.warehouse0")
self.uom = self.env["uom.uom"].browse(1)
self.warehouse.mto_mts_management = True
self.customer_loc = self.env.ref("stock.stock_location_customers")
self.product = self.env["product.product"].create(
{"name": "Test product", "type": "product"}
)
self.company_partner = self.env.ref("base.main_partner")
self.group = self.env["procurement.group"].create({"name": "test"})
self.procurement_vals = {"warehouse_id": self.warehouse, "group_id": self.group}
# Since mrp and purchase modules may not be installed, we need to
# create a dummy step to show that mts, mto, and mts+mto flows work.
# Else, if purchase/manufacture are not installed, the mto would fail.
route_vals = {
"warehouse_selectable": True,
"name": "dummy route",
}
self.dummy_route = self.env["stock.location.route"].create(route_vals)
rule_vals = {
"location_id": self.env.ref("stock.stock_location_stock").id,
"location_src_id": self.env.ref("stock.stock_location_suppliers").id,
"action": "pull",
"warehouse_id": self.warehouse.id,
"picking_type_id": self.env.ref("stock.picking_type_out").id,
"name": "dummy rule",
"route_id": self.dummy_route.id,
}
self.dummy_rule = self.env["stock.rule"].create(rule_vals)
self.warehouse.write({"route_ids": [(4, self.dummy_route.id)]})