From f403daf78fd035898d2f20e9a2ded7ccd192ad31 Mon Sep 17 00:00:00 2001 From: braisab Date: Thu, 22 Feb 2024 11:56:22 +0100 Subject: [PATCH] [IMP]pms_housekeeping: constrains and tests for housekeeping tasks --- .../models/pms_housekeeping_task.py | 46 +++- .../models/pms_housekeeping_task_type.py | 27 +-- pms_housekeeping/tests/__init__.py | 1 + .../tests/test_pms_housekeeping_task.py | 196 ++++++++++++++++-- .../tests/test_pms_housekeeping_task_type.py | 63 ++++++ .../tests/test_pms_hr_employee.py | 7 +- .../pms_housekeeping_task_type_views.xml | 8 +- .../views/pms_housekeeping_task_views.xml | 9 +- 8 files changed, 305 insertions(+), 52 deletions(-) create mode 100644 pms_housekeeping/tests/test_pms_housekeeping_task_type.py diff --git a/pms_housekeeping/models/pms_housekeeping_task.py b/pms_housekeeping/models/pms_housekeeping_task.py index 54cd0602c..0f913ef10 100644 --- a/pms_housekeeping/models/pms_housekeeping_task.py +++ b/pms_housekeeping/models/pms_housekeeping_task.py @@ -44,14 +44,14 @@ class PmsHouseKeepingTask(models.Model): readonly=False, ) cleaning_comments = fields.Text(string="Cleaning Comments") - employee_ids = fields.Many2many( + housekeeper_ids = fields.Many2many( comodel_name="hr.employee", relation="pms_housekeeping_task_hr_employee_rel", column1="task_id", column2="employee_id", - string="Employees", + string="Housekeepers", domain="[('job_id.name', '=', 'Housekeeper')]", - compute="_compute_employee_ids", + compute="_compute_housekeeper_ids", store=True, readonly=False, ) @@ -96,6 +96,11 @@ class PmsHouseKeepingTask(models.Model): string="Is Done Allowed", compute="_compute_done_allowed", ) + allowed_housekeeper_ids = fields.Many2many( + comodel_name="hr.employee", + string="Allowed Housekeepers", + compute="_compute_allowed_housekeeper_ids", + ) @api.constrains("task_date") def _check_task_date(self): @@ -111,6 +116,16 @@ class PmsHouseKeepingTask(models.Model): if rec.parent_id.parent_id: raise ValidationError(_("Parent task cannot have a parent task")) + @api.constrains("housekeeper_ids") + def _check_housekeeper_ids(self): + for rec in self: + if rec.housekeeper_ids: + for housekeeper in rec.housekeeper_ids: + if housekeeper not in rec.allowed_housekeeper_ids: + raise ValidationError( + _("The housekeeper should belong to the room's property.") + ) + def action_cancel(self): for rec in self: rec.state = "cancel" @@ -207,20 +222,20 @@ class PmsHouseKeepingTask(models.Model): rec.done_allowed = False @api.depends("room_id", "task_type_id") - def _compute_employee_ids(self): + def _compute_housekeeper_ids(self): for rec in self: - employee_ids = False + housekeeper_ids = False if rec.room_id or rec.task_type_id: - employee_ids = self.env["hr.employee"].search( + housekeeper_ids = self.env["hr.employee"].search( [("pre_assigned_room_ids", "in", [rec.room_id.id])] ) - if not employee_ids: - employee_ids = ( + if not housekeeper_ids: + housekeeper_ids = ( self.env["pms.housekeeping.task.type"] .search([("id", "=", rec.task_type_id.id)]) .housekeeper_ids ) - rec.employee_ids = employee_ids + rec.housekeeper_ids = housekeeper_ids @api.depends("task_type_id") def _compute_priority(self): @@ -230,6 +245,19 @@ class PmsHouseKeepingTask(models.Model): else: rec.priority = False + @api.depends('room_id') + def _compute_allowed_housekeeper_ids(self): + for rec in self: + domain = [('job_id.name', '=', 'Housekeeper')] + if rec.room_id: + domain = [ + ('job_id.name', '=', 'Housekeeper'), + '|', + ('property_ids', 'in', rec.room_id.pms_property_id.ids), + ('property_ids', '=', False), + ] + rec.allowed_housekeeper_ids = self.env['hr.employee'].search(domain).ids + @api.model def create(self, vals): task_type_id = vals.get("task_type_id") diff --git a/pms_housekeeping/models/pms_housekeeping_task_type.py b/pms_housekeeping/models/pms_housekeeping_task_type.py index 056cc90df..0d3c3ca51 100644 --- a/pms_housekeeping/models/pms_housekeeping_task_type.py +++ b/pms_housekeeping/models/pms_housekeeping_task_type.py @@ -47,14 +47,6 @@ class PmsHouseKeepingTaskType(models.Model): string="Properties", ) - allowed_housekeeper_ids = fields.Many2many( - comodel_name="hr.employee", - relation="pms_housekeeping_task_type_allowed_hr_employee_rel", - column1="task_type_id", - column2="employee_id", - string="Allowed Employees", - compute="_compute_allowed_housekeeper_ids", - ) @api.constrains("is_overnight", "days_after_clean_overnight") def _check_days_after_clean_overnight(self): @@ -80,15 +72,12 @@ class PmsHouseKeepingTaskType(models.Model): _("Parent task type cannot have a parent task type") ) - @api.depends("pms_property_ids") - def _compute_allowed_housekeeper_ids(self): + @api.constrains("housekeeper_ids") + def _check_housekeeper_ids(self): for record in self: - domain = [] - if record.pms_property_ids: - domain = [ - "|", - ("property_ids", "in", record.pms_property_ids.ids), - ("property_ids", "in", False), - ] - domain.append(("job_id.name", "=", "Housekeeper")) - record.allowed_housekeeper_ids = self.env["hr.employee"].search(domain).ids + if record.housekeeper_ids: + for employee in record.housekeeper_ids: + if employee.job_id.name != 'Housekeeper': + raise ValidationError( + _("The job position should be Housekeeper.") + ) diff --git a/pms_housekeeping/tests/__init__.py b/pms_housekeeping/tests/__init__.py index b90277bc3..2e834c23c 100644 --- a/pms_housekeeping/tests/__init__.py +++ b/pms_housekeeping/tests/__init__.py @@ -1,2 +1,3 @@ from . import test_pms_housekeeping_task from . import test_pms_hr_employee +from . import test_pms_housekeeping_task_type diff --git a/pms_housekeeping/tests/test_pms_housekeeping_task.py b/pms_housekeeping/tests/test_pms_housekeeping_task.py index 089ad86f8..7647b07b9 100644 --- a/pms_housekeeping/tests/test_pms_housekeeping_task.py +++ b/pms_housekeeping/tests/test_pms_housekeeping_task.py @@ -359,7 +359,7 @@ class TestPmsHousekeepingTask(TestPms): self.assertFalse(housekeeping_task, "Housekeeping task shouldn't be created") @freeze_time("2000-01-04") - def test_create_task_type_childs(self): + def test_create_task_childs(self): # ARRANGE # create task type parent_task_type = self.env["pms.housekeeping.task.type"].create( @@ -400,7 +400,7 @@ class TestPmsHousekeepingTask(TestPms): # Verify that the housekeeping task is not created self.assertTrue(housekeeping_task, "Child housekeeping task should be created") - def test_no_create_task_type_childs(self): + def test_no_create_task_childs(self): # ARRANGE # create task type self.env["pms.housekeeping.task.type"].create( @@ -437,30 +437,190 @@ class TestPmsHousekeepingTask(TestPms): housekeeping_task.child_ids, "Child housekeeping task shouldn´t be created" ) - def test_days_after_clean_overnight_constraint(self): - # ARRANGE, ACT & ASSERT - # create task type and verify that the constraint is raised + def test_no_create_grandchild_task(self): + # ARRANGE + # create task type + task_type = self.env["pms.housekeeping.task.type"].create( + { + "name": "Task Type Parent", + "is_checkout": True, + } + ) + parent_task = self.env["pms.housekeeping.task"].create( + { + "name": "Task", + "room_id": self.room1.id, + "task_type_id": task_type.id, + "task_date": datetime.today(), + } + ) + child_task = self.env["pms.housekeeping.task"].create( + { + "name": "Child Task", + "room_id": self.room1.id, + "task_type_id": task_type.id, + "parent_id": parent_task.id, + "task_date": datetime.today(), + } + ) + # ACT & ASSERT with self.assertRaises( - ValidationError, msg="Days After Clean Overnight should be greater than 0" + ValidationError, msg="Grandchild task shouldn´t exist." ): - self.env["pms.housekeeping.task.type"].create( + self.env["pms.housekeeping.task"].create( { - "name": "Task Type 1", - "is_overnight": True, - "days_after_clean_overnight": 0, + "name": "Grandchild Task", + "room_id": self.room1.id, + "task_type_id": task_type.id, + "parent_id": child_task.id, + "task_date": datetime.today(), } ) - def test_days_after_clean_empty_constraint(self): - # ARRANGE, ACT & ASSERT - # create task type and verify that the constraint is raised + def test_create_task_with_no_housekeeper(self): + + # ARRANGE + self.job_id = self.env["hr.job"].create( + { + "name": "Test Job", + } + ) + self.employee = self.env["hr.employee"].create( + { + "name": "Test Employee", + "company_id": self.company1.id, + "job_id": self.job_id.id, + } + ) + # create task type + self.task_type = self.env["pms.housekeeping.task.type"].create( + { + "name": "Task Type 1", + "is_checkout": True, + } + ) + + # ACT & ASSERT with self.assertRaises( - ValidationError, msg="Days After Clean Overnight should be greater than 0" + ValidationError, msg="Employee should have a housekeeper job" ): - self.env["pms.housekeeping.task.type"].create( + self.env["pms.housekeeping.task"].create( { - "name": "Task Type 1", - "is_empty": True, - "days_after_clean_empty": 0, + "name": "Task", + "room_id": self.room1.id, + "task_type_id": self.task_type.id, + "task_date": datetime.today(), + "housekeeper_ids": [(6, 0, [self.employee.id])], } ) + + def test_create_task_with_housekeeper(self): + + # ARRANGE + self.employee = self.env["hr.employee"].create( + { + "name": "Test Employee", + "company_id": self.company1.id, + "job_id": self.env.ref("pms_housekeeping.housekeeping_job_id").id, + } + ) + # create task type + self.task_type = self.env["pms.housekeeping.task.type"].create( + { + "name": "Task Type 1", + "is_checkout": True, + } + ) + + # ACT + self.task = self.env["pms.housekeeping.task"].create( + { + "name": "Task", + "room_id": self.room1.id, + "task_type_id": self.task_type.id, + "task_date": datetime.today(), + "housekeeper_ids": [(6, 0, [self.employee.id])], + } + ) + + #ASSERT + self.assertTrue(self.task, "Housekeeping task should be created") + + def test_task_housekeeper_room_inconsistency(self): + + # ARRANGE + self.pms_property2 = self.env["pms.property"].create( + { + "name": "Property 2", + "company_id": self.company1.id, + "default_pricelist_id": self.pricelist1.id, + } + ) + self.room2 = self.env["pms.room"].create( + { + "name": "Room 202", + "pms_property_id": self.pms_property2.id, + "room_type_id": self.room_type1.id, + } + ) + self.employee = self.env["hr.employee"].create( + { + "name": "Test Employee", + "company_id": self.company1.id, + "job_id": self.env.ref("pms_housekeeping.housekeeping_job_id").id, + "property_ids": [(6, 0, [self.pms_property1.id])], + } + ) + # create task type + self.task_type = self.env["pms.housekeeping.task.type"].create( + { + "name": "Task Type 1", + "is_checkout": True, + } + ) + + # ACT & ASSERT + with self.assertRaises( + ValidationError, msg="The room and housekeeper should belong to the same property." + ): + self.env["pms.housekeeping.task"].create( + { + "name": "Task", + "room_id": self.room2.id, + "task_type_id": self.task_type.id, + "task_date": datetime.today(), + "housekeeper_ids": [(6, 0, [self.employee.id])], + } + ) + + def test_task_housekeeper_room_consistency(self): + # ARRANGE + self.employee = self.env["hr.employee"].create( + { + "name": "Test Employee", + "company_id": self.company1.id, + "job_id": self.env.ref("pms_housekeeping.housekeeping_job_id").id, + "property_ids": [(6, 0, [self.pms_property1.id])], + } + ) + # create task type + self.task_type = self.env["pms.housekeeping.task.type"].create( + { + "name": "Task Type 1", + "is_checkout": True, + } + ) + + # ACT + task = self.env["pms.housekeeping.task"].create( + { + "name": "Task", + "room_id": self.room1.id, + "task_type_id": self.task_type.id, + "task_date": datetime.today(), + "housekeeper_ids": [(6, 0, [self.employee.id])], + } + ) + + # ASSERT + self.assertTrue(task, "Housekeeping task should be created") diff --git a/pms_housekeeping/tests/test_pms_housekeeping_task_type.py b/pms_housekeeping/tests/test_pms_housekeeping_task_type.py new file mode 100644 index 000000000..246a3dfd1 --- /dev/null +++ b/pms_housekeeping/tests/test_pms_housekeeping_task_type.py @@ -0,0 +1,63 @@ +from .common import TestPms +from odoo.exceptions import ValidationError + + +class TestPmsHousekeepingTask(TestPms): + def setUp(self): + super().setUp() + + def test_days_after_clean_overnight_constraint(self): + # ARRANGE, ACT & ASSERT + # create task type and verify that the constraint is raised + with self.assertRaises( + ValidationError, msg="Days After Clean Overnight should be greater than 0" + ): + self.env["pms.housekeeping.task.type"].create( + { + "name": "Task Type 1", + "is_overnight": True, + "days_after_clean_overnight": 0, + } + ) + + def test_days_after_clean_empty_constraint(self): + # ARRANGE, ACT & ASSERT + # create task type and verify that the constraint is raised + with self.assertRaises( + ValidationError, msg="Days After Clean Overnight should be greater than 0" + ): + self.env["pms.housekeeping.task.type"].create( + { + "name": "Task Type 1", + "is_empty": True, + "days_after_clean_empty": 0, + } + ) + + def test_no_create_grandchild_task_type(self): + # ARRANGE + # create task type + parent_task_type = self.env["pms.housekeeping.task.type"].create( + { + "name": "Task Type Parent", + "is_checkout": True, + } + ) + child_task_type = self.env["pms.housekeeping.task.type"].create( + { + "name": "Task Type Child", + "is_checkout": True, + "parent_id": parent_task_type.id, + } + ) + # ACT & ASSERT + with self.assertRaises( + ValidationError, msg="Grandchild task type shouldn´t exist." + ): + self.env["pms.housekeeping.task.type"].create( + { + "name": "Task Type Grandchild", + "is_checkout": True, + "parent_id": child_task_type.id, + } + ) diff --git a/pms_housekeeping/tests/test_pms_hr_employee.py b/pms_housekeeping/tests/test_pms_hr_employee.py index 7440779aa..8c6a98ecc 100644 --- a/pms_housekeeping/tests/test_pms_hr_employee.py +++ b/pms_housekeeping/tests/test_pms_hr_employee.py @@ -77,11 +77,16 @@ class TestPmsHrEmployee(TestPms): def test_not_pre_assigned_room_no_housekeeper_employee(self): # ARRANGE + self.job_id = self.env["hr.job"].create( + { + "name": "Test Job", + } + ) self.hr_employee = self.env["hr.employee"].create( { "name": "Test Employee", "company_id": self.company1.id, - "job_id": self.env.ref("hr.job_trainee").id, + "job_id": self.job_id.id, } ) diff --git a/pms_housekeeping/views/pms_housekeeping_task_type_views.xml b/pms_housekeeping/views/pms_housekeeping_task_type_views.xml index 97148c78f..16c9624b5 100644 --- a/pms_housekeeping/views/pms_housekeeping_task_type_views.xml +++ b/pms_housekeeping/views/pms_housekeeping_task_type_views.xml @@ -57,10 +57,12 @@ + - - diff --git a/pms_housekeeping/views/pms_housekeeping_task_views.xml b/pms_housekeeping/views/pms_housekeeping_task_views.xml index d10acd28d..99c5a4530 100644 --- a/pms_housekeeping/views/pms_housekeeping_task_views.xml +++ b/pms_housekeeping/views/pms_housekeeping_task_views.xml @@ -11,7 +11,7 @@ - + @@ -73,7 +73,12 @@ - + +