From 9ae6a2f6fe608ecda03c4a60e5f8c81bea22a93c Mon Sep 17 00:00:00 2001 From: Christophe Simonis Date: Mon, 9 Sep 2019 16:42:02 +0000 Subject: [PATCH] [FIX] stock: correctly works on non unique recordsets Followup of 3d6e91789968719c726420554d25b25f81282a55 to avoid leaking the company of first warehouse on all others when writing on multiple records. closes odoo/odoo#36656 X-original-commit: ca5def07346ac85bfb53304d6a73aaa85882d3d8 Signed-off-by: Christophe Simonis --- addons/stock/models/stock_warehouse.py | 3 +-- addons/stock/tests/test_warehouse.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/addons/stock/models/stock_warehouse.py b/addons/stock/models/stock_warehouse.py index 2221edac80a..0bd4b0a454e 100644 --- a/addons/stock/models/stock_warehouse.py +++ b/addons/stock/models/stock_warehouse.py @@ -549,8 +549,7 @@ class Warehouse(models.Model): """ for warehouse in self: company_id = vals.get('company_id', warehouse.company_id.id) - vals.update(company_id=company_id) - sub_locations = warehouse._get_locations_values(vals, warehouse.code) + sub_locations = warehouse._get_locations_values(dict(vals, company_id=company_id), warehouse.code) missing_location = {} for location, location_values in sub_locations.items(): if not warehouse[location] and location not in vals: diff --git a/addons/stock/tests/test_warehouse.py b/addons/stock/tests/test_warehouse.py index 6c513d5c9b6..95fdd8b8129 100644 --- a/addons/stock/tests/test_warehouse.py +++ b/addons/stock/tests/test_warehouse.py @@ -2,6 +2,7 @@ from odoo.addons.stock.tests.common2 import TestStockCommon from odoo.tests import Form +from odoo.tools import mute_logger class TestWarehouse(TestStockCommon): @@ -432,6 +433,20 @@ class TestWarehouse(TestStockCommon): # Ensure there still no quants in distribution warehouse self.assertEqual(sum(self.env['stock.quant']._gather(product, warehouse_distribution_wavre.lot_stock_id).mapped('quantity')), 0) + def test_noleak(self): + # non-regression test to avoid company_id leaking to other warehouses (see blame) + wh = self.env["stock.warehouse"].search([]) + + assert len(set(wh.mapped("company_id.id"))) > 1 + + companies_before = wh.mapped(lambda w: (w.id, w.company_id)) + # writing on any field should change the company of warehouses + with mute_logger("odoo.models"): + wh.write({"whatever": "no one cares"}) + companies_after = wh.mapped(lambda w: (w.id, w.company_id)) + + self.assertEqual(companies_after, companies_before) + def test_toggle_active_warehouse_1(self): """ Basic test that create a warehouse with classic configuration. Archive it and check that locations, picking types, routes, rules are