test: fix holiday list creation causing flaky tests (#30260)
diff --git a/erpnext/hr/doctype/attendance/test_attendance.py b/erpnext/hr/doctype/attendance/test_attendance.py
index 6095413..585059f 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 @@
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 @@
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 @@
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 @@
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 27f98a2..7d32fd8 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 @@
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 @@
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 8cf1037..6f9031f 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 @@
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 ebad3cb..5e41b66 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 @@
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 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