Skip to content
Permalink
Browse files

[REF] sale_mrp: speedup tests

Before on rubot
odoo.addons.sale_mrp.tests.test_sale_mrp_flow tested in 97.08s, 117272 queries

After on runbot
odoo.addons.sale_mrp.tests.test_sale_mrp_flow tested in 50.84s, 61903 queries

closes #32537

Signed-off-by: Simon Lejeune (sle) <sle@openerp.com>
  • Loading branch information...
sle-odoo committed Apr 5, 2019
1 parent 4ad8115 commit 8c377471b6d13c56eee139bead29839310c9fef3
Showing with 70 additions and 57 deletions.
  1. +70 −57 addons/sale_mrp/tests/test_sale_mrp_flow.py
@@ -8,53 +8,54 @@
from odoo.tools import mute_logger


class TestSaleMrpFlow(common.TransactionCase):
class TestSaleMrpFlow(common.SavepointCase):

def setUp(self):
super(TestSaleMrpFlow, self).setUp()
@classmethod
def setUpClass(cls):
super(TestSaleMrpFlow, cls).setUpClass()
# Useful models
self.StockMove = self.env['stock.move']
self.UoM = self.env['uom.uom']
self.MrpProduction = self.env['mrp.production']
self.Inventory = self.env['stock.inventory']
self.InventoryLine = self.env['stock.inventory.line']
self.ProductProduce = self.env['mrp.product.produce']
self.ProductCategory = self.env['product.category']

self.categ_unit = self.env.ref('uom.product_uom_categ_unit')
self.categ_kgm = self.env.ref('uom.product_uom_categ_kgm')
self.stock_location = self.env.ref('stock.stock_location_stock')
self.warehouse = self.env.ref('stock.warehouse0')

self.uom_kg = self.env['uom.uom'].search([('category_id', '=', self.categ_kgm.id), ('uom_type', '=', 'reference')], limit=1)
self.uom_kg.write({
cls.StockMove = cls.env['stock.move']
cls.UoM = cls.env['uom.uom']
cls.MrpProduction = cls.env['mrp.production']
cls.Inventory = cls.env['stock.inventory']
cls.InventoryLine = cls.env['stock.inventory.line']
cls.ProductProduce = cls.env['mrp.product.produce']
cls.ProductCategory = cls.env['product.category']

cls.categ_unit = cls.env.ref('uom.product_uom_categ_unit')
cls.categ_kgm = cls.env.ref('uom.product_uom_categ_kgm')
cls.stock_location = cls.env.ref('stock.stock_location_stock')
cls.warehouse = cls.env.ref('stock.warehouse0')

cls.uom_kg = cls.env['uom.uom'].search([('category_id', '=', cls.categ_kgm.id), ('uom_type', '=', 'reference')], limit=1)
cls.uom_kg.write({
'name': 'Test-KG',
'rounding': 0.000001})
self.uom_gm = self.UoM.create({
cls.uom_gm = cls.UoM.create({
'name': 'Test-G',
'category_id': self.categ_kgm.id,
'category_id': cls.categ_kgm.id,
'uom_type': 'smaller',
'factor': 1000.0,
'rounding': 0.001})
self.uom_unit = self.env['uom.uom'].search([('category_id', '=', self.categ_unit.id), ('uom_type', '=', 'reference')], limit=1)
self.uom_unit.write({
cls.uom_unit = cls.env['uom.uom'].search([('category_id', '=', cls.categ_unit.id), ('uom_type', '=', 'reference')], limit=1)
cls.uom_unit.write({
'name': 'Test-Unit',
'rounding': 0.01})
self.uom_dozen = self.UoM.create({
cls.uom_dozen = cls.UoM.create({
'name': 'Test-DozenA',
'category_id': self.categ_unit.id,
'category_id': cls.categ_unit.id,
'factor_inv': 12,
'uom_type': 'bigger',
'rounding': 0.001})

# Creating all components
self.component_a = self._create_product('Comp A', self.uom_unit)
self.component_b = self._create_product('Comp B', self.uom_unit)
self.component_c = self._create_product('Comp C', self.uom_unit)
self.component_d = self._create_product('Comp D', self.uom_unit)
self.component_e = self._create_product('Comp E', self.uom_unit)
self.component_f = self._create_product('Comp F', self.uom_unit)
self.component_g = self._create_product('Comp G', self.uom_unit)
cls.component_a = cls._cls_create_product('Comp A', cls.uom_unit)
cls.component_b = cls._cls_create_product('Comp B', cls.uom_unit)
cls.component_c = cls._cls_create_product('Comp C', cls.uom_unit)
cls.component_d = cls._cls_create_product('Comp D', cls.uom_unit)
cls.component_e = cls._cls_create_product('Comp E', cls.uom_unit)
cls.component_f = cls._cls_create_product('Comp F', cls.uom_unit)
cls.component_g = cls._cls_create_product('Comp G', cls.uom_unit)

# Create a kit 'kit_1' :
# -----------------------
@@ -63,26 +64,26 @@ def setUp(self):
# |- component_b x1
# |- component_c x3

self.kit_1 = self._create_product('Kit 1', self.uom_unit)
cls.kit_1 = cls._cls_create_product('Kit 1', cls.uom_unit)

self.bom_kit_1 = self.env['mrp.bom'].create({
'product_tmpl_id': self.kit_1.product_tmpl_id.id,
cls.bom_kit_1 = cls.env['mrp.bom'].create({
'product_tmpl_id': cls.kit_1.product_tmpl_id.id,
'product_qty': 1.0,
'type': 'phantom'})

BomLine = self.env['mrp.bom.line']
BomLine = cls.env['mrp.bom.line']
BomLine.create({
'product_id': self.component_a.id,
'product_id': cls.component_a.id,
'product_qty': 2.0,
'bom_id': self.bom_kit_1.id})
'bom_id': cls.bom_kit_1.id})
BomLine.create({
'product_id': self.component_b.id,
'product_id': cls.component_b.id,
'product_qty': 1.0,
'bom_id': self.bom_kit_1.id})
'bom_id': cls.bom_kit_1.id})
BomLine.create({
'product_id': self.component_c.id,
'product_id': cls.component_c.id,
'product_qty': 3.0,
'bom_id': self.bom_kit_1.id})
'bom_id': cls.bom_kit_1.id})

# Create a kit 'kit_parent' :
# ---------------------------
@@ -98,58 +99,70 @@ def setUp(self):
# |- component_e x1

# Creating all kits
self.kit_2 = self._create_product('Kit 2', self.uom_unit)
self.kit_3 = self._create_product('kit 3', self.uom_unit)
self.kit_parent = self._create_product('Kit Parent', self.uom_unit)
cls.kit_2 = cls._cls_create_product('Kit 2', cls.uom_unit)
cls.kit_3 = cls._cls_create_product('kit 3', cls.uom_unit)
cls.kit_parent = cls._cls_create_product('Kit Parent', cls.uom_unit)

# Linking the kits and the components via some 'phantom' BoMs
bom_kit_2 = self.env['mrp.bom'].create({
'product_tmpl_id': self.kit_2.product_tmpl_id.id,
bom_kit_2 = cls.env['mrp.bom'].create({
'product_tmpl_id': cls.kit_2.product_tmpl_id.id,
'product_qty': 1.0,
'type': 'phantom'})

BomLine.create({
'product_id': self.component_d.id,
'product_id': cls.component_d.id,
'product_qty': 1.0,
'bom_id': bom_kit_2.id})
BomLine.create({
'product_id': self.kit_1.id,
'product_id': cls.kit_1.id,
'product_qty': 2.0,
'bom_id': bom_kit_2.id})

bom_kit_parent = self.env['mrp.bom'].create({
'product_tmpl_id': self.kit_parent.product_tmpl_id.id,
bom_kit_parent = cls.env['mrp.bom'].create({
'product_tmpl_id': cls.kit_parent.product_tmpl_id.id,
'product_qty': 1.0,
'type': 'phantom'})

BomLine.create({
'product_id': self.component_e.id,
'product_id': cls.component_e.id,
'product_qty': 1.0,
'bom_id': bom_kit_parent.id})
BomLine.create({
'product_id': self.kit_2.id,
'product_id': cls.kit_2.id,
'product_qty': 2.0,
'bom_id': bom_kit_parent.id})

bom_kit_3 = self.env['mrp.bom'].create({
'product_tmpl_id': self.kit_3.product_tmpl_id.id,
bom_kit_3 = cls.env['mrp.bom'].create({
'product_tmpl_id': cls.kit_3.product_tmpl_id.id,
'product_qty': 1.0,
'type': 'phantom'})

BomLine.create({
'product_id': self.component_f.id,
'product_id': cls.component_f.id,
'product_qty': 1.0,
'bom_id': bom_kit_3.id})
BomLine.create({
'product_id': self.component_g.id,
'product_id': cls.component_g.id,
'product_qty': 2.0,
'bom_id': bom_kit_3.id})

BomLine.create({
'product_id': self.kit_3.id,
'product_id': cls.kit_3.id,
'product_qty': 2.0,
'bom_id': bom_kit_parent.id})

@classmethod
def _cls_create_product(cls, name, uom_id, routes=()):
p = Form(cls.env['product.product'])
p.name = name
p.type = 'product'
p.uom_id = uom_id
p.uom_po_id = uom_id
p.route_ids.clear()
for r in routes:
p.route_ids.add(r)
return p.save()

def _create_product(self, name, uom_id, routes=()):
p = Form(self.env['product.product'])
p.name = name

0 comments on commit 8c37747

Please sign in to comment.
You can’t perform that action at this time.