From 0ff75c9a9c862fe5a9c013cda76ccbaeb1020d95 Mon Sep 17 00:00:00 2001 From: ArnauCForgeFlow Date: Tue, 10 Dec 2024 12:09:47 +0100 Subject: [PATCH] [IMP] stock_inventory_lockdown: unlink test, clean code --- .../models/stock_move_line.py | 10 ++-- .../tests/test_stock_inventory_lockdown.py | 47 ++++++++++--------- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/stock_inventory_lockdown/models/stock_move_line.py b/stock_inventory_lockdown/models/stock_move_line.py index db10b9655..f371da40a 100644 --- a/stock_inventory_lockdown/models/stock_move_line.py +++ b/stock_inventory_lockdown/models/stock_move_line.py @@ -35,10 +35,10 @@ class StockMoveLine(models.Model): location_names = locked_location_ids.mapped("complete_name") raise ValidationError( _( - "Inventory adjusment underway at " - "the following location(s):\n- %s\n" - "Moving products to or from these locations is " - "not allowed until the inventory adjustment is complete." + "Inventory adjustment underway at the following " + "location(s):\n- %(locations)s\n Moving products to " + "or from these locations is not allowed until the " + "inventory adjustment is complete.", + locations="\n - ".join(location_names), ) - % "\n - ".join(location_names) ) diff --git a/stock_inventory_lockdown/tests/test_stock_inventory_lockdown.py b/stock_inventory_lockdown/tests/test_stock_inventory_lockdown.py index d44fa2dcb..47fcf36a3 100644 --- a/stock_inventory_lockdown/tests/test_stock_inventory_lockdown.py +++ b/stock_inventory_lockdown/tests/test_stock_inventory_lockdown.py @@ -8,47 +8,48 @@ from odoo.addons.stock.tests.common import TestStockCommon class StockInventoryLocationTest(TestStockCommon): - def setUp(self): - super(StockInventoryLocationTest, self).setUp() + @classmethod + def setUpClass(cls): + super().setUpClass() # Make a new location with a parent and a child. - self.new_parent_location = self.env["stock.location"].create( + cls.new_parent_location = cls.env["stock.location"].create( {"name": "Test parent location", "usage": "internal"} ) - self.new_location = self.env["stock.location"].create( + cls.new_location = cls.env["stock.location"].create( { "name": "Test location", "usage": "internal", - "location_id": self.new_parent_location.id, + "location_id": cls.new_parent_location.id, } ) - self.new_sublocation = self.env["stock.location"].create( + cls.new_sublocation = cls.env["stock.location"].create( { "name": "Test sublocation", "usage": "internal", - "location_id": self.new_location.id, + "location_id": cls.new_location.id, } ) # Input goods - self.env["stock.quant"].create( + cls.env["stock.quant"].create( { - "location_id": self.new_location.id, - "product_id": self.productA.id, + "location_id": cls.new_location.id, + "product_id": cls.productA.id, "quantity": 10.0, } ) - self.env["stock.quant"].create( + cls.env["stock.quant"].create( { - "location_id": self.new_parent_location.id, - "product_id": self.productB.id, + "location_id": cls.new_parent_location.id, + "product_id": cls.productB.id, "quantity": 5.0, } ) # Prepare an inventory - self.inventory = self.env["stock.inventory"].create( - {"name": "Lock down location", "location_ids": [(4, self.new_location.id)]} + cls.inventory = cls.env["stock.inventory"].create( + {"name": "Lock down location", "location_ids": [(4, cls.new_location.id)]} ) - self.inventory.action_state_to_in_progress() - self.assertTrue(self.inventory.stock_quant_ids, "The inventory is empty.") + cls.inventory.action_state_to_in_progress() + cls.assertTrue(cls.inventory.stock_quant_ids, "The inventory is empty.") def create_stock_move(self, product, origin_id=False, dest_id=False): return self.env["stock.move"].create( @@ -117,10 +118,6 @@ class StockInventoryLocationTest(TestStockCommon): inventory_subloc.action_state_to_in_progress() self.assertTrue(inventory_subloc.stock_quant_ids) - - # for line in inventory_subloc.stock_quant_ids: - # line._apply_inventory() - self.assertEqual( line.product_id.with_context(location=line.location_id.id).qty_available, 22.0, @@ -164,3 +161,11 @@ class StockInventoryLocationTest(TestStockCommon): move2._action_assign() move2.move_line_ids[0].qty_done = 10.0 move2._action_done() + + def test_unlink(self): + """We can't unlink a location if an inventory is in progress.""" + inventory = self.env["stock.inventory"].search( + [("state", "=", "in_progress")], limit=1 + ) + with self.assertRaises(ValidationError): + inventory.location_ids.unlink()