[FIX] hr_holidays: Fix undeterministic crashing tests
The tests in hr_holidays/tests/test_company_leave.py seem like they randomly fail if the default value for date_to is computed on a different second than the date_from (it's a dict of fields so not ordered). Cfr https://runbot.odoo.com/runbot/build/3727451 2020-07-30 08:28:32 vs 2020-07-30 08:28:31 for which date_from > date_to. I imagine the call to leave._onchange_request_parameters() is meant to fix it, but it occurs after the create() which is flushed immediately, so it's too late and the date_from/date_to SQL constraint still triggers. It could happen for real production usage too, so we might want to do something about that outside of tests. Options include: 1/ making the date_from/date_to fields writeable computed fields, with the compute method setting better defaults on both fields at once 2/ alternatively, remove the default=fields.Datetime.now and override default_get instead for deterministic defaults on both fields at once The second solution seemed to be the better one. closes odoo/odoo#55880 Taskid: 2312029 X-original-commit: 6908ea471dd18182f83c81046f0000a00898fb66 Signed-off-by: Yannick Tivisse (yti) <yti@odoo.com>
This commit is contained in:
@@ -82,6 +82,12 @@ class HolidaysRequest(models.Model):
|
||||
if 'state' in fields_list and not defaults.get('state'):
|
||||
lt = self.env['hr.leave.type'].browse(defaults.get('holiday_status_id'))
|
||||
defaults['state'] = 'confirm' if lt and lt.leave_validation_type != 'no_validation' else 'draft'
|
||||
|
||||
now = fields.Datetime.now()
|
||||
defaults.update({
|
||||
'date_from': now,
|
||||
'date_to': now,
|
||||
})
|
||||
return defaults
|
||||
|
||||
def _default_get_request_parameters(self, values):
|
||||
@@ -143,11 +149,9 @@ class HolidaysRequest(models.Model):
|
||||
# duration
|
||||
date_from = fields.Datetime(
|
||||
'Start Date', compute='_compute_date_from_to', store=True, readonly=False, index=True, copy=False, required=True, tracking=True,
|
||||
default=fields.Datetime.now,
|
||||
states={'cancel': [('readonly', True)], 'refuse': [('readonly', True)], 'validate1': [('readonly', True)], 'validate': [('readonly', True)]})
|
||||
date_to = fields.Datetime(
|
||||
'End Date', compute='_compute_date_from_to', store=True, readonly=False, copy=False, required=True, tracking=True,
|
||||
default=fields.Datetime.now,
|
||||
states={'cancel': [('readonly', True)], 'refuse': [('readonly', True)], 'validate1': [('readonly', True)], 'validate': [('readonly', True)]})
|
||||
number_of_days = fields.Float(
|
||||
'Duration (Days)', compute='_compute_number_of_days', store=True, readonly=False, copy=False, tracking=True,
|
||||
@@ -309,7 +313,7 @@ class HolidaysRequest(models.Model):
|
||||
'request_unit_half', 'request_unit_hours', 'request_unit_custom', 'employee_id')
|
||||
def _compute_date_from_to(self):
|
||||
for holiday in self:
|
||||
if holiday.request_date_from > holiday.request_date_to:
|
||||
if holiday.request_date_from and holiday.request_date_to and holiday.request_date_from > holiday.request_date_to:
|
||||
holiday.request_date_to = holiday.request_date_from
|
||||
if not holiday.request_date_from:
|
||||
holiday.date_from = False
|
||||
|
||||
@@ -168,6 +168,8 @@ class TestLeaveRequests(TestHrHolidaysCommon):
|
||||
leave_form.holiday_type = 'department'
|
||||
leave_form.department_id = self.hr_dept
|
||||
leave_form.holiday_status_id = self.holidays_type_1
|
||||
leave_form.request_date_from = date(2019, 5, 6)
|
||||
leave_form.request_date_to = date(2019, 5, 6)
|
||||
leave = leave_form.save()
|
||||
leave.action_approve()
|
||||
member_ids = self.hr_dept.member_ids.ids
|
||||
|
||||
Reference in New Issue
Block a user