diff --git a/maintenance_plan/models/maintenance_plan.py b/maintenance_plan/models/maintenance_plan.py index 05786f0f4..649e7e2ac 100644 --- a/maintenance_plan/models/maintenance_plan.py +++ b/maintenance_plan/models/maintenance_plan.py @@ -101,7 +101,9 @@ class MaintenancePlan(models.Model): @api.model def _search_search_equipment(self, operator, value): - if operator != "=" or (not value and not isinstance(value, models.NewId)): + if operator not in ("=", "in") or ( + not value and not isinstance(value, models.NewId) + ): raise ValueError(self.env._("Unsupported search operator")) plans = self.search([("generate_with_domain", "=", True)]) plan_ids = [] @@ -113,7 +115,7 @@ def _search_search_equipment(self, operator, value): ) ): plan_ids.append(plan.id) - return ["|", ("equipment_id", "=", value), ("id", "in", plan_ids)] + return ["|", ("equipment_id", operator, value), ("id", "in", plan_ids)] @api.depends("equipment_id") def _compute_search_equipment(self): diff --git a/maintenance_plan/readme/CONTRIBUTORS.md b/maintenance_plan/readme/CONTRIBUTORS.md index 844c15a08..0f23ccebe 100644 --- a/maintenance_plan/readme/CONTRIBUTORS.md +++ b/maintenance_plan/readme/CONTRIBUTORS.md @@ -7,3 +7,4 @@ - Enric Tobella \<\> - Alexei Rivera \<\> - Yann Papouin \<\> +- Yannick Payot \<\> diff --git a/maintenance_plan/tests/__init__.py b/maintenance_plan/tests/__init__.py index def774ebc..69d1cd78c 100644 --- a/maintenance_plan/tests/__init__.py +++ b/maintenance_plan/tests/__init__.py @@ -1,2 +1,3 @@ from . import test_maintenance_plan from . import test_maintenance_plan_domain +from . import test_equipment_count diff --git a/maintenance_plan/tests/test_equipment_count.py b/maintenance_plan/tests/test_equipment_count.py new file mode 100644 index 000000000..598e97bc1 --- /dev/null +++ b/maintenance_plan/tests/test_equipment_count.py @@ -0,0 +1,87 @@ +# Copyright 2026 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from .common import TestMaintenanceBase + + +class TestEquipmentCount(TestMaintenanceBase): + """Test _compute_search_maintenance_plan_count in maintenance.equipment""" + + def test_search_maintenance_plan_count_zero(self): + """Test search_maintenance_plan_count when no plans exist""" + # Create a new equipment with no maintenance plans + equipment = self.MaintenanceEquipment.create( + { + "name": "Equipment No Plans", + } + ) + equipment._compute_search_maintenance_plan_count() + self.assertEqual( + equipment.search_maintenance_plan_count, + 0, + "search_maintenance_plan_count should be 0 when no plans exist", + ) + + def test_search_maintenance_plan_count_one(self): + """Test search_maintenance_plan_count with one active plan""" + # Create a maintenance plan for the equipment + self.MaintenancePlan.create( + { + "equipment_id": self.equipment.id, + "maintenance_kind_id": self.maintenance_kind.id, + } + ) + self.equipment._compute_search_maintenance_plan_count() + self.assertGreaterEqual( + self.equipment.search_maintenance_plan_count, + 1, + "search_maintenance_plan_count should be at least 1 when plan exists", + ) + + def test_search_maintenance_plan_count_multiple(self): + """Test search_maintenance_plan_count with multiple plans""" + # Create a second maintenance kind + kind2 = self.MaintenanceKind.create( + { + "name": "Second Kind", + "code": "SECOND_KIND", + "priority": "optional", + } + ) + # Create multiple maintenance plans for the equipment with different kinds + self.MaintenancePlan.create( + [ + { + "equipment_id": self.equipment.id, + "maintenance_kind_id": self.maintenance_kind.id, + }, + { + "equipment_id": self.equipment.id, + "maintenance_kind_id": kind2.id, + }, + ] + ) + + self.equipment._compute_search_maintenance_plan_count() + self.assertGreaterEqual( + self.equipment.search_maintenance_plan_count, + 2, + "search_maintenance_plan_count should be at least 2 when multiple plans exist", + ) + + def test_search_maintenance_plan_count_with_inactive(self): + """Test search_maintenance_plan_count includes inactive plans""" + # Create an inactive plan + self.MaintenancePlan.create( + { + "equipment_id": self.equipment.id, + "maintenance_kind_id": self.maintenance_kind.id, + "active": False, + } + ) + self.equipment._compute_search_maintenance_plan_count() + self.assertGreaterEqual( + self.equipment.search_maintenance_plan_count, + 1, + "search_maintenance_plan_count should include inactive plans", + )