Files
odoo_source/addons/hr_holidays/tests/test_global_leaves.py
T
Xavier-Do e9b62b47dc [FIX] *: adapt tz for Ubuntu Noble
Some of the non canononical timezones are not present in Ubuntu Noble,
it would be a better practice to only use canonical timezones in data
and tests.

Note that this is not a real fix for all cases since the database that
ran on Ubuntu Jammy and are moved to an ubuntu Noble server will have
the issue with timezones already in database.

One of the possible fix would be to manage that during upgrades, but
this isn't a verry flexible solution since upgrade are meant to manage
chyange of version, not change of server. If an old 17.0 versions needs
to be moved to a Noble server, this won't work.

Another solution would be to install package like tzdata-legacy that may
keep the old timezones but it is not the only think since TAI-10 are
also in this package. This solution is not ideal because non canonical
timezone will still be shown in the dropdown. We would need to filter
them.

A last solution would be to add the support for those old timezones by
monkeypatching the lib. This way, only new timezones would be shown but
non canonical one won't crash when used. This is not ideal either
because we may need to keep this for a while. But in combination with
the upgrade solution, it may work proprely.

Part-of: odoo/odoo#160842
2024-04-07 10:03:48 +00:00

178 lines
8.6 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from datetime import date, datetime, timedelta
from odoo.addons.hr_holidays.tests.common import TestHrHolidaysCommon
from odoo.exceptions import ValidationError
from freezegun import freeze_time
from odoo.tests import tagged
@tagged('global_leaves')
class TestGlobalLeaves(TestHrHolidaysCommon):
""" Test global leaves for a whole company, conflict resolutions """
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.calendar_1 = cls.env['resource.calendar'].create({
'name': 'Classic 40h/week',
'tz': 'UTC',
'hours_per_day': 8.0,
'attendance_ids': [
(0, 0, {'name': 'Monday Morning', 'dayofweek': '0', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
(0, 0, {'name': 'Monday Lunch', 'dayofweek': '0', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
(0, 0, {'name': 'Monday Afternoon', 'dayofweek': '0', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
(0, 0, {'name': 'Tuesday Morning', 'dayofweek': '1', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
(0, 0, {'name': 'Tuesday Lunch', 'dayofweek': '1', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
(0, 0, {'name': 'Tuesday Afternoon', 'dayofweek': '1', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
(0, 0, {'name': 'Wednesday Morning', 'dayofweek': '2', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
(0, 0, {'name': 'Wednesday Lunch', 'dayofweek': '2', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
(0, 0, {'name': 'Wednesday Afternoon', 'dayofweek': '2', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
(0, 0, {'name': 'Thursday Morning', 'dayofweek': '3', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
(0, 0, {'name': 'Thursday Lunch', 'dayofweek': '3', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
(0, 0, {'name': 'Thursday Afternoon', 'dayofweek': '3', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
(0, 0, {'name': 'Friday Morning', 'dayofweek': '4', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
(0, 0, {'name': 'Friday Lunch', 'dayofweek': '4', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
(0, 0, {'name': 'Friday Afternoon', 'dayofweek': '4', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'})
]
})
cls.calendar_2 = cls.env['resource.calendar'].create({
'name': 'Classic 20h/week',
'tz': 'UTC',
'hours_per_day': 4.0,
'attendance_ids': [
(0, 0, {'name': 'Monday Morning', 'dayofweek': '0', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
(0, 0, {'name': 'Tuesday Morning', 'dayofweek': '1', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
(0, 0, {'name': 'Wednesday Morning', 'dayofweek': '2', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
(0, 0, {'name': 'Thursday Morning', 'dayofweek': '3', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
(0, 0, {'name': 'Friday Morning', 'dayofweek': '4', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
]
})
cls.global_leave = cls.env['resource.calendar.leaves'].create({
'name': 'Global Time Off',
'date_from': date(2022, 3, 7),
'date_to': date(2022, 3, 7),
})
cls.calendar_leave = cls.env['resource.calendar.leaves'].create({
'name': 'Global Time Off',
'date_from': date(2022, 3, 8),
'date_to': date(2022, 3, 8),
'calendar_id': cls.calendar_1.id,
})
def test_leave_on_global_leave(self):
with self.assertRaises(ValidationError):
self.env['resource.calendar.leaves'].create({
'name': 'Wrong Time Off',
'date_from': date(2022, 3, 7),
'date_to': date(2022, 3, 7),
'calendar_id': self.calendar_1.id,
})
with self.assertRaises(ValidationError):
self.env['resource.calendar.leaves'].create({
'name': 'Wrong Time Off',
'date_from': date(2022, 3, 7),
'date_to': date(2022, 3, 7),
})
def test_leave_on_calendar_leave(self):
self.env['resource.calendar.leaves'].create({
'name': 'Correct Time Off',
'date_from': date(2022, 3, 8),
'date_to': date(2022, 3, 8),
'calendar_id': self.calendar_2.id,
})
with self.assertRaises(ValidationError):
self.env['resource.calendar.leaves'].create({
'name': 'Wrong Time Off',
'date_from': date(2022, 3, 8),
'date_to': date(2022, 3, 8),
})
with self.assertRaises(ValidationError):
self.env['resource.calendar.leaves'].create({
'name': 'Wrong Time Off',
'date_from': date(2022, 3, 8),
'date_to': date(2022, 3, 8),
'calendar_id': self.calendar_1.id,
})
@freeze_time('2023-05-12')
def test_global_leave_timezone(self):
"""
It is necessary to use the timezone of the calendar
for the global leaves (without resource).
"""
calendar_asia = self.env['resource.calendar'].create({
'name': 'Asia calendar',
'tz': 'Asia/Kolkata', # UTC +05:30
'hours_per_day': 8.0,
'attendance_ids': []
})
self.env.user.tz = 'Europe/Brussels'
global_leave = self.env['resource.calendar.leaves'].with_user(self.env.user).create({
'name': 'Public holiday',
'date_from': "2023-05-15 06:00:00", # utc from 8:00:00 for Europe/Brussels (UTC +02:00)
'date_to': "2023-05-15 15:00:00", # utc from 17:00:00 for Europe/Brussels (UTC +02:00)
'calendar_id': calendar_asia.id,
})
# Expectation:
# 6:00:00 in UTC (data from the browser) --> 8:00:00 for Europe/Brussel (UTC +02:00)
# 8:00:00 for Asia/Kolkata (UTC +05:30) --> 2:30:00 in UTC
self.assertEqual(global_leave.date_from, datetime(2023, 5, 15, 2, 30))
self.assertEqual(global_leave.date_to, datetime(2023, 5, 15, 11, 30))
# Note:
# The user in Europe/Brussels timezone see 4:30 and not 2:30 because he is in UTC +02:00.
# The user in Asia/Kolkata timezone (determined via the browser) see 8:00 because he is in UTC +05:30
def test_global_leave_number_of_days_with_new(self):
"""
Check that leaves stored in memory (and not in the database)
take into account global leaves.
"""
global_leave = self.env['resource.calendar.leaves'].create({
'name': 'Global Time Off',
'date_from': datetime(2024, 1, 3, 6, 0, 0),
'date_to': datetime(2024, 1, 3, 19, 0, 0),
'calendar_id': self.calendar_1.id,
})
leave_type = self.env['hr.leave.type'].create({
'name': 'Paid Time Off',
'time_type': 'leave',
'requires_allocation': 'no',
})
self.employee_emp.resource_calendar_id = self.calendar_1.id
leave = self.env['hr.leave'].create({
'name': 'Test new leave',
'employee_id': self.employee_emp.id,
'holiday_status_id': leave_type.id,
'request_date_from': global_leave.date_from,
'request_date_to': global_leave.date_to,
})
self.assertEqual(leave.number_of_days, 0, 'It is a global leave')
leave = self.env['hr.leave'].new({
'name': 'Test new leave',
'employee_id': self.employee_emp.id,
'holiday_status_id': leave_type.id,
'request_date_from': global_leave.date_from,
'request_date_to': global_leave.date_to,
})
self.assertEqual(leave.number_of_days, 0, 'It is a global leave')
leave = self.env['hr.leave'].new({
'name': 'Test new leave',
'employee_id': self.employee_emp.id,
'holiday_status_id': leave_type.id,
'request_date_from': global_leave.date_from - timedelta(days=1),
'request_date_to': global_leave.date_to + timedelta(days=1),
})
self.assertEqual(leave.number_of_days, 2, 'There is a global leave')