From 4afdebbb7e919878966a3b894ce99e913cf046e4 Mon Sep 17 00:00:00 2001 From: Yannick Tivisse Date: Tue, 4 Aug 2020 08:47:21 +0000 Subject: [PATCH] [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) --- addons/hr_holidays/models/hr_leave.py | 10 +++++++--- addons/hr_holidays/tests/test_leave_requests.py | 2 ++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/addons/hr_holidays/models/hr_leave.py b/addons/hr_holidays/models/hr_leave.py index 8952af28b5c..2525ad737ac 100644 --- a/addons/hr_holidays/models/hr_leave.py +++ b/addons/hr_holidays/models/hr_leave.py @@ -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 diff --git a/addons/hr_holidays/tests/test_leave_requests.py b/addons/hr_holidays/tests/test_leave_requests.py index 0baf2be736b..fdbf6c17bdc 100644 --- a/addons/hr_holidays/tests/test_leave_requests.py +++ b/addons/hr_holidays/tests/test_leave_requests.py @@ -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