Skip to content

Commit

Permalink
[IMP] hr_attendance_missing_days: Add additional unittest for edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
fkantelberg committed Aug 14, 2023
1 parent ff9677d commit 4ef8fce
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions hr_attendance_missing_days/tests/test_attendance.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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)

0 comments on commit 4ef8fce

Please sign in to comment.