from odoo.tests import TransactionCase, tagged @tagged('post_install', '-at_install') class TestMrpPackagingQty(TransactionCase): @classmethod def setUpClass(cls): super().setUpClass() cls.product = cls.env['product.product'].create({ 'name': 'Test Product', 'type': 'product', }) # Create mrp.packaging instead of product.uom/product.packaging cls.packaging = cls.env['mrp.packaging'].create({ 'name': 'Box of 10', 'product_tmpl_id': cls.product.product_tmpl_id.id, 'qty': 10.0, 'barcode': 'PACK10', }) cls.bom = cls.env['mrp.bom'].create({ 'product_tmpl_id': cls.product.product_tmpl_id.id, 'product_qty': 1.0, 'packaging_id': cls.packaging.id, 'packaging_qty': 2.0, }) def test_bom_packaging_calculation(self): """ Test if BOM quantity is calculated correctly based on packaging """ # Trigger onchange manually self.bom._onchange_packaging_qty() # Logic: packaging_qty (2) * packaging.qty (10) = 20 self.assertEqual(self.bom.product_qty, 20.0, "BOM Quantity should be 2 * 10 = 20") def test_mo_creation_packaging(self): """ Test if MO created from BOM gets the packaging """ mo_form = self.env['mrp.production'].create({ 'product_id': self.product.id, 'bom_id': self.bom.id, 'product_qty': 1.0, # Initial dummy value }) # Simulate onchange mo_form._onchange_bom_id() self.assertEqual(mo_form.packaging_id, self.packaging, "MO should inherit packaging from BOM") self.assertEqual(mo_form.packaging_qty, 2.0, "MO should inherit packaging qty from BOM") # Trigger calculation mo_form._onchange_packaging_qty() self.assertEqual(mo_form.product_qty, 20.0, "MO product qty should be updated based on packaging") def test_mps_packaging(self): """ Test MPS view state with packaging """ if 'mrp.production.schedule' not in self.env: return # Skip if mps not installed mps = self.env['mrp.production.schedule'].create({ 'product_id': self.product.id, 'warehouse_id': self.env['stock.warehouse'].search([], limit=1).id, 'packaging_id': self.packaging.id, }) # Test get_view_state scaling # Mock some forecast data date_range = self.env.company._get_date_range() date_start = date_range[0][0] # Set forecast quantity in packaging units (e.g. 5 packs) mps.set_forecast_qty(0, 5) # Verify stored value is 5 * 10 = 50 forecast = mps.forecast_ids.filtered(lambda f: f.date >= date_start)[:1] self.assertEqual(forecast.forecast_qty, 50.0, "Forecast qty should be stored as 5 * 10 = 50") # Verify view state returns 5 (50 / 10) view_state = mps.get_production_schedule_view_state()[0] view_forecast = view_state['forecast_ids'][0] self.assertEqual(view_forecast['forecast_qty'], 5.0, "View state should return 5.0 packs")