From 9c14ec5c82f92f81980673c941b20402bd02c852 Mon Sep 17 00:00:00 2001 From: Jairo Llopis Date: Tue, 23 Jul 2024 13:05:29 +0100 Subject: [PATCH] [FIX] hr_contract_update_overtime: tests compatible with hr_holidays_attendance If you run this module's tests while it's being installed for the 1st time, `hr_holidays_attendance` will enter its test scope and add extra constrains to which leaves are considered for employees. If we don't follow those constrains, the tests fail. Thus here I'm using that module lazily, while not being strictly required as a dependency. If installed, I fulfill the data required for it to work, and then tests are more resilient. @moduon MT-6583 --- .../models/hr_contract_history.py | 10 +++---- .../tests/test_hr_contract_update_overtime.py | 27 +++++++++++++++++-- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/hr_contract_update_overtime/models/hr_contract_history.py b/hr_contract_update_overtime/models/hr_contract_history.py index f9acd422..30cbb1f4 100644 --- a/hr_contract_update_overtime/models/hr_contract_history.py +++ b/hr_contract_update_overtime/models/hr_contract_history.py @@ -8,19 +8,17 @@ class HrContractHistory(models.Model): def action_update_overtime(self): for record in self: - all_contracts = record.mapped("contract_ids").sorted( - "date_start", reverse=False - ) + all_contracts = record.contract_ids.sorted("date_start", reverse=False) valid_contracts = all_contracts.filtered( lambda c: c.state in {"open", "close"} ) for contract in valid_contracts: other_contracts = all_contracts - contract # Reorganize Leaves - other_contracts.mapped("resource_calendar_id").transfer_leaves_to( + other_contracts.resource_calendar_id.transfer_leaves_to( contract.resource_calendar_id, resources=contract.employee_id.resource_id, from_date=contract.date_start, ) - # Update Overtime - all_contracts.action_update_overtime() + # Update Overtime + self.contract_ids.action_update_overtime() diff --git a/hr_contract_update_overtime/tests/test_hr_contract_update_overtime.py b/hr_contract_update_overtime/tests/test_hr_contract_update_overtime.py index b85a0a85..c4d324b2 100644 --- a/hr_contract_update_overtime/tests/test_hr_contract_update_overtime.py +++ b/hr_contract_update_overtime/tests/test_hr_contract_update_overtime.py @@ -197,7 +197,7 @@ def make_dtt(days_before, h=0, m=0, to_date=False): ] ) # Create all leaves on last contract - cls.env["resource.calendar.leaves"].create( + leaves = cls.env["resource.calendar.leaves"].create( [ { "name": "Test Leave 2h", @@ -225,7 +225,30 @@ def make_dtt(days_before, h=0, m=0, to_date=False): }, ] ) - cls.overtime_model = cls.env["hr.attendance.overtime"] + # `hr_holidays_attendance` adds extra constrains when considering one + # leave valid for an employee. It wouldn't be a problem, but it's + # auto-installable. Thus, if you run this test at install time where + # that module is included for installation, it will be on scope for the + # test and break it. Therefore, we need to create the holiday request + # if it's installed, even when we don't need that dependency normally. + if "holiday_id" in leaves._fields: + leave_type = cls.env["hr.leave.type"].create( + {"name": "Beach 🏖️", "time_type": "leave"} + ) + for res_leave in leaves: + res_leave.holiday_id = ( + cls.env["hr.leave"] + .with_context(leave_skip_state_check=True) + .create( + { + "state": "validate", + "date_from": res_leave.date_from, + "date_to": res_leave.date_to, + "employee_id": cls.employee.id, + "holiday_status_id": leave_type.id, + } + ) + ) def test_overtime(self): self.assertEqual(self.contract_history.contract_count, 3)