Skip to content

Commit 89cfb01

Browse files
committed
refactor(test): make test attendance deterministic
1 parent d783f4f commit 89cfb01

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

hrms/hr/doctype/attendance/test_attendance.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,7 @@
3535

3636
class TestAttendance(HRMSTestSuite):
3737
def setUp(self):
38-
from hrms.payroll.doctype.salary_slip.test_salary_slip import make_holiday_list
39-
40-
from_date = get_year_start(add_months(getdate(), -1))
41-
to_date = get_year_ending(getdate())
42-
self.holiday_list = make_holiday_list(from_date=from_date, to_date=to_date)
43-
frappe.db.delete("Attendance")
44-
frappe.db.delete("Employee Checkin")
38+
self.holiday_list = "Salary Slip Test Holiday List"
4539

4640
def test_duplicate_attendance(self):
4741
employee = make_employee("test_duplicate_attendance@example.com", company="_Test Company")
@@ -144,7 +138,7 @@ def test_allow_attendance_with_different_shifts(self):
144138
).insert()
145139

146140
def test_mark_absent(self):
147-
employee = make_employee("test_mark_absent@example.com")
141+
employee = make_employee("test_mark_absent@example.com", company="_Test Company")
148142
date = nowdate()
149143

150144
attendance = mark_attendance(employee, date, "Absent")
@@ -158,7 +152,9 @@ def test_unmarked_days(self):
158152
attendance_date = add_days(first_sunday, 1)
159153

160154
employee = make_employee(
161-
"test_unmarked_days@example.com", date_of_joining=add_days(attendance_date, -1)
155+
"test_unmarked_days@example.com",
156+
date_of_joining=add_days(attendance_date, -1),
157+
company="_Test Company",
162158
)
163159
frappe.db.set_value("Employee", employee, "holiday_list", self.holiday_list)
164160

@@ -182,7 +178,9 @@ def test_unmarked_days_excluding_holidays(self):
182178
attendance_date = add_days(first_sunday, 1)
183179

184180
employee = make_employee(
185-
"test_unmarked_days@example.com", date_of_joining=add_days(attendance_date, -1)
181+
"test_unmarked_days@example.com",
182+
date_of_joining=add_days(attendance_date, -1),
183+
company="_Test Company",
186184
)
187185

188186
mark_attendance(employee, attendance_date, "Present")
@@ -228,7 +226,10 @@ def test_unmarked_days_as_per_joining_and_relieving_dates(self):
228226
doj = add_days(date, 1)
229227
relieving_date = add_days(date, 5)
230228
employee = make_employee(
231-
"test_unmarked_days_as_per_doj@example.com", date_of_joining=doj, relieving_date=relieving_date
229+
"test_unmarked_days_as_per_doj@example.com",
230+
date_of_joining=doj,
231+
relieving_date=relieving_date,
232+
company="_Test Company",
232233
)
233234

234235
frappe.db.set_value("Employee", employee, "holiday_list", self.holiday_list)
@@ -270,14 +271,14 @@ def test_duplicate_attendance_when_created_from_checkins_and_tool(self):
270271
self.assertEqual(len(attendances), 1)
271272

272273
def test_get_events_returns_attendance(self):
273-
employee = make_employee("calendar.user@example.com", company="_Test Company")
274+
employee = frappe.get_doc("Employee", {"first_name": "_Test Employee"})
274275

275-
attendance_name = mark_attendance(employee, getdate(), status="Present")
276+
attendance_name = mark_attendance(employee.name, getdate(), status="Present")
276277
attendance = frappe.get_value("Attendance", attendance_name, "status")
277278

278279
self.assertEqual(attendance, "Present")
279280

280-
frappe.set_user("calendar.user@example.com")
281+
frappe.set_user(employee.user_id)
281282
try:
282283
events = get_events(start=getdate(), end=getdate())
283284
finally:
@@ -289,9 +290,6 @@ def test_get_events_returns_attendance(self):
289290
self.assertEqual(attendance_events[0].get("status"), "Present")
290291
self.assertEqual(
291292
attendance_events[0].get("employee_name"),
292-
frappe.db.get_value("Employee", employee, "employee_name"),
293+
frappe.db.get_value("Employee", employee.name, "employee_name"),
293294
)
294295
self.assertEqual(attendance_events[0].get("attendance_date"), getdate())
295-
296-
def tearDown(self):
297-
frappe.db.rollback()

hrms/hr/doctype/shift_type/shift_type.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ def _process(self, logs):
216216
)
217217

218218
# commit after processing checkin logs to avoid losing progress
219-
frappe.db.commit() # nosemgrep
219+
if not frappe.in_test:
220+
frappe.db.commit() # nosemgrep
220221

221222
assigned_employees = self.get_assigned_employees(self.process_attendance_after, True)
222223
# mark absent in batches & commit to avoid losing progress since this tries to process remaining attendance
@@ -226,7 +227,8 @@ def _process(self, logs):
226227
self.mark_absent_for_dates_with_no_attendance(employee)
227228
self.mark_absent_for_half_day_dates(employee)
228229

229-
frappe.db.commit() # nosemgrep
230+
if not frappe.in_test:
231+
frappe.db.commit() # nosemgrep
230232

231233
def is_half_holiday(self, employee, attendance_date):
232234
holiday_list = self.get_holiday_list(employee, attendance_date)

hrms/tests/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ def make_persistent_master_data(cls):
2121
cls.make_holiday_list_assignment()
2222
cls.update_system_settings()
2323
cls.update_email_account_settings()
24+
# TODO: clean up
25+
if frappe.db.get_value("Holiday List Assignment", {"assigned_to": "_Test Company"}, "docstatus") == 0:
26+
frappe.get_doc("Holiday List Assignment", {"assigned_to": "_Test Company"}).submit()
2427
frappe.db.commit()
2528

2629
@classmethod

0 commit comments

Comments
 (0)