[IMP] stock_request_mrp: Refactor tests

This commit is contained in:
Víctor Martínez
2022-10-21 12:25:55 +02:00
parent cd7da198fd
commit c76e95757b
2 changed files with 53 additions and 161 deletions

View File

@@ -1,4 +1,5 @@
# Copyright 2017 ForgeFlow S.L.
# Copyright 2022 Tecnativa - Víctor Martínez
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl-3.0).
from collections import Counter
@@ -11,11 +12,9 @@ from odoo.tests import common, new_test_user
class TestStockRequest(common.TransactionCase):
def setUp(self):
super().setUp()
# common models
self.stock_request = self.env["stock.request"]
self.request_order = self.env["stock.request.order"]
# refs
self.stock_request_user_group = self.env.ref(
"stock_request.group_stock_request_user"
@@ -24,7 +23,6 @@ class TestStockRequest(common.TransactionCase):
self.warehouse = self.env.ref("stock.warehouse0")
self.categ_unit = self.env.ref("uom.product_uom_categ_unit")
self.virtual_loc = self.env.ref("stock.stock_location_customers")
# common data
self.company_2 = self.env["res.company"].create(
{"name": "Comp2", "parent_id": self.main_company.id}
@@ -61,45 +59,22 @@ class TestStockRequest(common.TransactionCase):
self.product_company_2 = self._create_product(
"SH_2", "Shoes", self.company_2.id
)
self.ressuply_loc = self.env["stock.location"].create(
{
"name": "Ressuply",
"location_id": self.warehouse.view_location_id.id,
"usage": "internal",
"company_id": self.main_company.id,
}
self.ressuply_loc = self._create_location(
name="Ressuply",
location_id=self.warehouse.view_location_id.id,
company_id=self.main_company.id,
)
self.ressuply_loc_2 = self.env["stock.location"].create(
{
"name": "Ressuply",
"location_id": self.wh2.view_location_id.id,
"usage": "internal",
"company_id": self.company_2.id,
}
self.ressuply_loc_2 = self._create_location(
name="Ressuply",
location_id=self.wh2.view_location_id.id,
company_id=self.company_2.id,
)
self.route = self.env["stock.location.route"].create(
{
"name": "Transfer",
"product_categ_selectable": False,
"product_selectable": True,
"company_id": self.main_company.id,
"sequence": 10,
}
self.route = self._create_location_route(
name="Transfer", company_id=self.main_company.id
)
self.route_2 = self.env["stock.location.route"].create(
{
"name": "Transfer",
"product_categ_selectable": False,
"product_selectable": True,
"company_id": self.company_2.id,
"sequence": 10,
}
self.route_2 = self._create_location_route(
name="Transfer", company_id=self.company_2.id
)
self.uom_dozen = self.env["uom.uom"].create(
{
"name": "Test-DozenA",
@@ -109,7 +84,6 @@ class TestStockRequest(common.TransactionCase):
"rounding": 0.001,
}
)
self.env["stock.rule"].create(
{
"name": "Transfer",
@@ -123,7 +97,6 @@ class TestStockRequest(common.TransactionCase):
"company_id": self.main_company.id,
}
)
self.env["stock.rule"].create(
{
"name": "Transfer",
@@ -137,7 +110,6 @@ class TestStockRequest(common.TransactionCase):
"company_id": self.company_2.id,
}
)
self.env["ir.config_parameter"].sudo().set_param(
"stock.no_auto_scheduler", "True"
)
@@ -154,6 +126,19 @@ class TestStockRequest(common.TransactionCase):
)
)
def _create_location(self, **vals):
return self.env["stock.location"].create(dict(usage="internal", **vals))
def _create_location_route(self, **vals):
return self.env["stock.location.route"].create(
dict(
product_categ_selectable=False,
product_selectable=True,
sequence=10,
**vals
)
)
class TestStockRequestBase(TestStockRequest):
def setUp(self):

View File

@@ -1,92 +1,32 @@
# Copyright 2016-20 ForgeFlow S.L. (https://www.forgeflow.com)
# Copyright 2022 Tecnativa - Pedro M. Baeza
# Copyright 2022 Tecnativa - Víctor Martínez
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl-3.0).
from odoo import fields
from odoo.tests import Form, common
from odoo.tests import Form
from odoo.addons.stock_request.tests.test_stock_request import TestStockRequest
class TestStockRequestMrp(common.TransactionCase):
class TestStockRequestMrp(TestStockRequest):
def setUp(self):
super().setUp()
# common models
self.stock_request = self.env["stock.request"]
# refs
self.stock_request_user_group = self.env.ref(
"stock_request.group_stock_request_user"
)
self.stock_request_manager_group = self.env.ref(
"stock_request.group_stock_request_manager"
)
self.mrp_user_group = self.env.ref("mrp.group_mrp_user")
self.main_company = self.env.ref("base.main_company")
self.warehouse = self.env.ref("stock.warehouse0")
self.categ_unit = self.env.ref("uom.product_uom_categ_unit")
# common data
self.company_2 = self.env["res.company"].create({"name": "Comp2"})
self.wh2 = self.env["stock.warehouse"].search(
[("company_id", "=", self.company_2.id)], limit=1
)
self.stock_request_user = self._create_user(
"stock_request_user",
[self.stock_request_user_group.id, self.mrp_user_group.id],
[self.main_company.id, self.company_2.id],
)
self.stock_request_manager = self._create_user(
"stock_request_manager",
[self.stock_request_manager_group.id, self.mrp_user_group.id],
[self.main_company.id, self.company_2.id],
)
self.stock_request_user.write({"groups_id": [(4, self.mrp_user_group.id)]})
self.stock_request_manager.write({"groups_id": [(4, self.mrp_user_group.id)]})
self.route_manufacture = self.warehouse.manufacture_pull_id.route_id
self.product = self._create_product(
"SH", "Shoes", False, self.route_manufacture.ids
)
self.raw_1 = self._create_product("SL", "Sole", False, [])
self.product.write({"route_ids": [(6, 0, self.route_manufacture.ids)]})
self.raw_1 = self._create_product("SL", "Sole", False)
self._update_qty_in_location(self.warehouse.lot_stock_id, self.raw_1, 10)
self.raw_2 = self._create_product("LC", "Lace", False, [])
self.raw_2 = self._create_product("LC", "Lace", False)
self._update_qty_in_location(self.warehouse.lot_stock_id, self.raw_2, 10)
self.bom = self._create_mrp_bom(self.product, [self.raw_1, self.raw_2])
self.uom_pair = self.env["uom.uom"].create(
{
"name": "Test-Pair",
"category_id": self.categ_unit.id,
"factor_inv": 2,
"uom_type": "bigger",
"rounding": 0.001,
}
)
def _update_qty_in_location(self, location, product, quantity):
self.env["stock.quant"]._update_available_quantity(product, location, quantity)
def _create_user(self, name, group_ids, company_ids):
return (
self.env["res.users"]
.with_context({"no_reset_password": True})
.create(
{
"name": name,
"password": "demo",
"login": name,
"email": str(name) + "@test.com",
"groups_id": [(6, 0, group_ids)],
"company_ids": [(6, 0, company_ids)],
}
)
)
def _create_product(self, default_code, name, company_id, route_ids):
return self.env["product.product"].create(
{
"name": name,
"default_code": default_code,
"uom_id": self.env.ref("uom.product_uom_unit").id,
"company_id": company_id,
"type": "product",
"route_ids": [(6, 0, route_ids)],
}
)
def _create_mrp_bom(self, product_id, raw_materials):
bom = self.env["mrp.bom"].create(
{
@@ -109,35 +49,24 @@ class TestStockRequestMrp(common.TransactionCase):
mo_form.qty_producing = qty
mo_form.save()
def _create_stock_request(self, user, products):
order_form = Form(
self.request_order.with_user(user).with_context(
default_company_id=self.main_company.id,
default_warehouse_id=self.warehouse.id,
default_location_id=self.warehouse.lot_stock_id,
)
)
order_form.expected_date = fields.Datetime.now()
for product_data in products:
with order_form.stock_request_ids.new() as item_form:
item_form.product_id = product_data[0]
item_form.product_uom_qty = product_data[1]
return order_form.save()
def test_create_request_01(self):
"""Single Stock request with buy rule"""
expected_date = fields.Datetime.now()
vals = {
"company_id": self.main_company.id,
"warehouse_id": self.warehouse.id,
"location_id": self.warehouse.lot_stock_id.id,
"expected_date": expected_date,
"stock_request_ids": [
(
0,
0,
{
"product_id": self.product.id,
"product_uom_id": self.product.uom_id.id,
"product_uom_qty": 5.0,
"company_id": self.main_company.id,
"warehouse_id": self.warehouse.id,
"location_id": self.warehouse.lot_stock_id.id,
"expected_date": expected_date,
},
)
],
}
order = (
self.env["stock.request.order"]
.with_user(self.stock_request_user)
.create(vals)
)
order = self._create_stock_request(self.stock_request_user, [(self.product, 5)])
order.action_confirm()
self.assertEqual(order.state, "open")
self.assertEqual(order.stock_request_ids.state, "open")
@@ -159,29 +88,7 @@ class TestStockRequestMrp(common.TransactionCase):
self.assertEqual(order.stock_request_ids.qty_done, 5.0)
def test_view_actions(self):
expected_date = fields.Datetime.now()
vals = {
"company_id": self.main_company.id,
"warehouse_id": self.warehouse.id,
"location_id": self.warehouse.lot_stock_id.id,
"expected_date": expected_date,
"stock_request_ids": [
(
0,
0,
{
"product_id": self.product.id,
"product_uom_id": self.product.uom_id.id,
"product_uom_qty": 5.0,
"company_id": self.main_company.id,
"warehouse_id": self.warehouse.id,
"location_id": self.warehouse.lot_stock_id.id,
"expected_date": expected_date,
},
)
],
}
order = self.env["stock.request.order"].create(vals)
order = self._create_stock_request(self.stock_request_user, [(self.product, 5)])
order.action_confirm()
stock_request = order.stock_request_ids
action = stock_request.action_view_mrp_production()