fix: flaky tests (#30154)

This commit is contained in:
Rucha Mahabal 2022-03-10 14:22:12 +05:30 committed by GitHub
parent 5193a63781
commit 9fae89ff61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 11 deletions

View File

@ -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)

View File

@ -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()
@ -782,9 +792,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