From 4ef8fce0120ca99ab110be5f60222023d0db0e80 Mon Sep 17 00:00:00 2001 From: fkantelberg Date: Mon, 14 Aug 2023 12:28:08 +0200 Subject: [PATCH] [IMP] hr_attendance_missing_days: Add additional unittest for edge cases --- .../tests/test_attendance.py | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/hr_attendance_missing_days/tests/test_attendance.py b/hr_attendance_missing_days/tests/test_attendance.py index e81e6e7e..017b937a 100644 --- a/hr_attendance_missing_days/tests/test_attendance.py +++ b/hr_attendance_missing_days/tests/test_attendance.py @@ -1,9 +1,22 @@ # © 2023 initOS GmbH # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from datetime import date, datetime, timedelta + +import pytz + from odoo.tests import TransactionCase +def convert_tz(dt, *, from_tz=None, to_tz=None): + return ( + pytz.timezone(from_tz or "UTC") + .localize(dt) + .astimezone(pytz.timezone(to_tz or "UTC")) + .replace(tzinfo=None) + ) + + class TestAttendance(TransactionCase): @classmethod def setUpClass(cls): @@ -41,3 +54,41 @@ def test_attendance_creation_with_reason(self): attendances_new = attendances_after - attendances_before self.assertTrue(attendances_new) self.assertFalse(any(attendances_new.mapped("worked_hours"))) + + def test_attendance_creation(self): + self.env.company.attendance_missing_days_reason = self.reason + + attended = {date(2023, 7, 3 + offset) for offset in range(4)} + for tz in ["Europe/Amsterdam", "Pacific/Auckland", "America/New_York"]: + employee = self.employee.copy({"tz": tz, "name": f"Employee {tz}"}) + for offset, times in enumerate(((0, 30), (23, 30), (11, 30), (12, 30))): + # Convert the times from the employee TZ zo UTC. 3rd is monday + start = convert_tz( + datetime(2023, 7, 3 + offset, *times), + from_tz=tz, + to_tz="UTC", + ) + + # Generate a 30min attendance blocking the date + self.env["hr.attendance"].create( + { + "employee_id": employee.id, + "check_in": start, + "check_out": start + timedelta(minutes=30), + } + ) + + # Cover a huge time span + employee._create_missing_attendances( + datetime(2023, 6, 1, 0, 0), datetime(2023, 8, 1, 0, 0) + ) + + domain = [ + ("employee_id", "=", employee.id), + ("attendance_reason_ids", "=", self.reason.id), + ] + attendances = self.env["hr.attendance"].search(domain) + self.assertTrue(attendances) + for attendance in attendances: + checkin = convert_tz(attendance.check_in, to_tz=tz) + self.assertNotIn(checkin.date(), attended)