From f090e63e544aebf6b936be4e27701a26c3c51f26 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Thu, 17 Mar 2022 15:21:56 +0530 Subject: [PATCH] test: fix holiday list creation causing flaky tests (#30260) --- .../hr/doctype/attendance/test_attendance.py | 26 +++++++++--------- .../test_leave_application.py | 7 ++++- ..._based_on_employee_current_leave_policy.py | 1 + .../doctype/salary_slip/test_salary_slip.py | 27 ++++++++++--------- 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/erpnext/hr/doctype/attendance/test_attendance.py b/erpnext/hr/doctype/attendance/test_attendance.py index 6095413771..585059ff47 100644 --- a/erpnext/hr/doctype/attendance/test_attendance.py +++ b/erpnext/hr/doctype/attendance/test_attendance.py @@ -3,7 +3,7 @@ import frappe from frappe.tests.utils import FrappeTestCase -from frappe.utils import add_days, get_first_day, getdate, now_datetime, nowdate +from frappe.utils import add_days, get_year_ending, get_year_start, getdate, now_datetime, nowdate from erpnext.hr.doctype.attendance.attendance import ( get_month_map, @@ -16,6 +16,13 @@ from erpnext.hr.doctype.leave_application.test_leave_application import get_firs test_records = frappe.get_test_records('Attendance') class TestAttendance(FrappeTestCase): + def setUp(self): + from erpnext.payroll.doctype.salary_slip.test_salary_slip import make_holiday_list + + from_date = get_year_start(getdate()) + to_date = get_year_ending(getdate()) + self.holiday_list = make_holiday_list(from_date=from_date, to_date=to_date) + def test_mark_absent(self): employee = make_employee("test_mark_absent@example.com") date = nowdate() @@ -31,12 +38,9 @@ class TestAttendance(FrappeTestCase): employee = make_employee('test_unmarked_days@example.com', date_of_joining=add_days(first_day, -1)) frappe.db.delete('Attendance', {'employee': employee}) + frappe.db.set_value('Employee', employee, 'holiday_list', self.holiday_list) - 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) - - first_sunday = get_first_sunday(holiday_list, for_date=first_day) + first_sunday = get_first_sunday(self.holiday_list, for_date=first_day) mark_attendance(employee, first_day, 'Present') month_name = get_month_name(first_day) @@ -58,11 +62,9 @@ class TestAttendance(FrappeTestCase): employee = make_employee('test_unmarked_days@example.com', date_of_joining=add_days(first_day, -1)) 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) + frappe.db.set_value('Employee', employee, 'holiday_list', self.holiday_list) - first_sunday = get_first_sunday(holiday_list, for_date=first_day) + first_sunday = get_first_sunday(self.holiday_list, for_date=first_day) mark_attendance(employee, first_day, 'Present') month_name = get_month_name(first_day) @@ -87,9 +89,7 @@ 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) + frappe.db.set_value('Employee', employee, 'holiday_list', self.holiday_list) attendance_date = add_days(first_day, 2) mark_attendance(employee, attendance_date, 'Present') diff --git a/erpnext/hr/doctype/leave_application/test_leave_application.py b/erpnext/hr/doctype/leave_application/test_leave_application.py index 27f98a2659..7d32fd8865 100644 --- a/erpnext/hr/doctype/leave_application/test_leave_application.py +++ b/erpnext/hr/doctype/leave_application/test_leave_application.py @@ -82,7 +82,11 @@ class TestLeaveApplication(unittest.TestCase): set_leave_approver() frappe.db.delete("Attendance", {"employee": "_T-Employee-00001"}) - self.holiday_list = make_holiday_list() + frappe.db.set_value("Employee", "_T-Employee-00001", "holiday_list", "") + + from_date = get_year_start(getdate()) + to_date = get_year_ending(getdate()) + self.holiday_list = make_holiday_list(from_date=from_date, to_date=to_date) if not frappe.db.exists("Leave Type", "_Test Leave Type"): frappe.get_doc(dict( @@ -316,6 +320,7 @@ class TestLeaveApplication(unittest.TestCase): 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) self.assertEqual(frappe.db.count("Attendance", {"leave_application": leave_application.name}), 3) diff --git a/erpnext/patches/v13_0/create_leave_policy_assignment_based_on_employee_current_leave_policy.py b/erpnext/patches/v13_0/create_leave_policy_assignment_based_on_employee_current_leave_policy.py index 8cf1037223..6f9031fc50 100644 --- a/erpnext/patches/v13_0/create_leave_policy_assignment_based_on_employee_current_leave_policy.py +++ b/erpnext/patches/v13_0/create_leave_policy_assignment_based_on_employee_current_leave_policy.py @@ -7,6 +7,7 @@ import frappe def execute(): frappe.reload_doc('hr', 'doctype', 'leave_policy_assignment') + frappe.reload_doc('hr', 'doctype', 'employee_grade') employee_with_assignment = [] leave_policy = [] diff --git a/erpnext/payroll/doctype/salary_slip/test_salary_slip.py b/erpnext/payroll/doctype/salary_slip/test_salary_slip.py index ebad3cbf94..5e41b661f8 100644 --- a/erpnext/payroll/doctype/salary_slip/test_salary_slip.py +++ b/erpnext/payroll/doctype/salary_slip/test_salary_slip.py @@ -411,7 +411,7 @@ class TestSalarySlip(unittest.TestCase): def test_email_salary_slip(self): frappe.db.sql("delete from `tabEmail Queue`") - make_employee("test_email_salary_slip@salary.com") + make_employee("test_email_salary_slip@salary.com", company="_Test Company") ss = make_employee_salary_slip("test_email_salary_slip@salary.com", "Monthly", "Test Salary Slip Email") ss.company = "_Test Company" ss.save() @@ -1091,18 +1091,19 @@ def setup_test(): def make_holiday_list(list_name=None, from_date=None, to_date=None): fiscal_year = get_fiscal_year(nowdate(), company=erpnext.get_default_company()) name = list_name or "Salary Slip Test Holiday List" - holiday_list = frappe.db.exists("Holiday List", name) - if not holiday_list: - holiday_list = frappe.get_doc({ - "doctype": "Holiday List", - "holiday_list_name": name, - "from_date": from_date or fiscal_year[1], - "to_date": to_date or fiscal_year[2], - "weekly_off": "Sunday" - }).insert() - holiday_list.get_weekly_off_dates() - holiday_list.save() - holiday_list = holiday_list.name + + frappe.delete_doc_if_exists("Holiday List", name, force=True) + + holiday_list = frappe.get_doc({ + "doctype": "Holiday List", + "holiday_list_name": name, + "from_date": from_date or fiscal_year[1], + "to_date": to_date or fiscal_year[2], + "weekly_off": "Sunday" + }).insert() + holiday_list.get_weekly_off_dates() + holiday_list.save() + holiday_list = holiday_list.name return holiday_list