From 4febcd86f80488a90359d9c6b7e3590332a3cb31 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 10 Mar 2022 14:38:08 +0530 Subject: [PATCH] fix: flaky tests (backport #30154) (#30161) Co-authored-by: Rucha Mahabal --- .../hr/doctype/attendance/test_attendance.py | 16 +++++++++--- .../test_leave_application.py | 25 +++++++++++++------ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/erpnext/hr/doctype/attendance/test_attendance.py b/erpnext/hr/doctype/attendance/test_attendance.py index c74967d213e..6095413771c 100644 --- a/erpnext/hr/doctype/attendance/test_attendance.py +++ b/erpnext/hr/doctype/attendance/test_attendance.py @@ -25,7 +25,9 @@ class TestAttendance(FrappeTestCase): self.assertEqual(attendance, fetch_attendance) def test_unmarked_days(self): - first_day = get_first_day(getdate()) + now = now_datetime() + previous_month = now.month - 1 + first_day = now.replace(day=1).replace(month=previous_month).date() employee = make_employee('test_unmarked_days@example.com', date_of_joining=add_days(first_day, -1)) frappe.db.delete('Attendance', {'employee': employee}) @@ -34,7 +36,7 @@ class TestAttendance(FrappeTestCase): holiday_list = make_holiday_list() frappe.db.set_value('Employee', employee, 'holiday_list', holiday_list) - first_sunday = get_first_sunday(holiday_list) + first_sunday = get_first_sunday(holiday_list, for_date=first_day) mark_attendance(employee, first_day, 'Present') month_name = get_month_name(first_day) @@ -49,7 +51,9 @@ class TestAttendance(FrappeTestCase): self.assertIn(first_sunday, unmarked_days) def test_unmarked_days_excluding_holidays(self): - first_day = get_first_day(getdate()) + now = now_datetime() + previous_month = now.month - 1 + first_day = now.replace(day=1).replace(month=previous_month).date() employee = make_employee('test_unmarked_days@example.com', date_of_joining=add_days(first_day, -1)) frappe.db.delete('Attendance', {'employee': employee}) @@ -58,7 +62,7 @@ class TestAttendance(FrappeTestCase): holiday_list = make_holiday_list() frappe.db.set_value('Employee', employee, 'holiday_list', holiday_list) - first_sunday = get_first_sunday(holiday_list) + first_sunday = get_first_sunday(holiday_list, for_date=first_day) mark_attendance(employee, first_day, 'Present') month_name = get_month_name(first_day) @@ -83,6 +87,10 @@ class TestAttendance(FrappeTestCase): relieving_date=relieving_date) frappe.db.delete('Attendance', {'employee': employee}) + from erpnext.payroll.doctype.salary_slip.test_salary_slip import make_holiday_list + holiday_list = make_holiday_list() + frappe.db.set_value('Employee', employee, 'holiday_list', holiday_list) + attendance_date = add_days(first_day, 2) mark_attendance(employee, attendance_date, 'Present') month_name = get_month_name(first_day) diff --git a/erpnext/hr/doctype/leave_application/test_leave_application.py b/erpnext/hr/doctype/leave_application/test_leave_application.py index 38b8eef0c9b..c9f377dfaca 100644 --- a/erpnext/hr/doctype/leave_application/test_leave_application.py +++ b/erpnext/hr/doctype/leave_application/test_leave_application.py @@ -133,7 +133,9 @@ class TestLeaveApplication(unittest.TestCase): holiday_list = make_holiday_list() employee = get_employee() - frappe.db.set_value("Company", employee.company, "default_holiday_list", holiday_list) + original_holiday_list = employee.holiday_list + frappe.db.set_value("Employee", employee.name, "holiday_list", holiday_list) + first_sunday = get_first_sunday(holiday_list) leave_application = make_leave_application(employee.name, first_sunday, add_days(first_sunday, 3), leave_type.name) @@ -143,6 +145,8 @@ class TestLeaveApplication(unittest.TestCase): leave_application.cancel() + frappe.db.set_value("Employee", employee.name, "holiday_list", original_holiday_list) + def test_attendance_update_for_exclude_holidays(self): # Case 2: leave type with 'Include holidays within leaves as leaves' disabled frappe.delete_doc_if_exists("Leave Type", "Test Do Not Include Holidays", force=1) @@ -157,7 +161,8 @@ class TestLeaveApplication(unittest.TestCase): holiday_list = make_holiday_list() employee = get_employee() - frappe.db.set_value("Company", employee.company, "default_holiday_list", holiday_list) + original_holiday_list = employee.holiday_list + frappe.db.set_value("Employee", employee.name, "holiday_list", holiday_list) first_sunday = get_first_sunday(holiday_list) # already marked attendance on a holiday should be deleted in this case @@ -177,7 +182,7 @@ class TestLeaveApplication(unittest.TestCase): attendance.flags.ignore_validate = True attendance.save() - leave_application = make_leave_application(employee.name, first_sunday, add_days(first_sunday, 3), leave_type.name) + leave_application = make_leave_application(employee.name, first_sunday, add_days(first_sunday, 3), leave_type.name, employee.company) leave_application.reload() # holiday should be excluded while marking attendance self.assertEqual(leave_application.total_leave_days, 3) @@ -189,6 +194,8 @@ class TestLeaveApplication(unittest.TestCase): # attendance on non-holiday updated self.assertEqual(frappe.db.get_value("Attendance", attendance.name, "status"), "On Leave") + frappe.db.set_value("Employee", employee.name, "holiday_list", original_holiday_list) + def test_block_list(self): self._clear_roles() @@ -327,7 +334,8 @@ class TestLeaveApplication(unittest.TestCase): employee = get_employee() default_holiday_list = make_holiday_list() - frappe.db.set_value("Company", employee.company, "default_holiday_list", default_holiday_list) + original_holiday_list = employee.holiday_list + frappe.db.set_value("Employee", employee.name, "holiday_list", default_holiday_list) first_sunday = get_first_sunday(default_holiday_list) optional_leave_date = add_days(first_sunday, 1) @@ -378,6 +386,8 @@ class TestLeaveApplication(unittest.TestCase): # check leave balance is reduced self.assertEqual(get_leave_balance_on(employee.name, leave_type, optional_leave_date), 9) + frappe.db.set_value("Employee", employee.name, "holiday_list", original_holiday_list) + def test_leaves_allowed(self): employee = get_employee() leave_period = get_leave_period() @@ -781,9 +791,10 @@ def allocate_leaves(employee, leave_period, leave_type, new_leaves_allocated, el allocate_leave.submit() -def get_first_sunday(holiday_list): - month_start_date = get_first_day(nowdate()) - month_end_date = get_last_day(nowdate()) +def get_first_sunday(holiday_list, for_date=None): + date = for_date or getdate() + month_start_date = get_first_day(date) + month_end_date = get_last_day(date) first_sunday = frappe.db.sql(""" select holiday_date from `tabHoliday` where parent = %s