Skip to content

Commit

Permalink
[FIX] hr_contract_update_overtime: tests compatible with hr_holidays_…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
yajo committed Jul 23, 2024
1 parent d86f2d0 commit 9c14ec5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
10 changes: 4 additions & 6 deletions hr_contract_update_overtime/models/hr_contract_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9c14ec5

Please sign in to comment.