From 3f9d64ec52092c4259ff424e2843a9d00153176b Mon Sep 17 00:00:00 2001 From: bahaaabed Date: Thu, 1 Sep 2022 15:18:34 +0300 Subject: [PATCH] fix(Shift Type): fix all test dependency to shift type --- .../employee_checkin/test_employee_checkin.py | 13 ++++--- .../hr/doctype/shift_type/test_records.json | 8 +++- .../hr/doctype/shift_type/test_shift_type.py | 39 ++++++++++++------- .../test_monthly_attendance_sheet.py | 9 ++++- 4 files changed, 48 insertions(+), 21 deletions(-) diff --git a/erpnext/hr/doctype/employee_checkin/test_employee_checkin.py b/erpnext/hr/doctype/employee_checkin/test_employee_checkin.py index ae54bf313ac1..37a631ef691e 100644 --- a/erpnext/hr/doctype/employee_checkin/test_employee_checkin.py +++ b/erpnext/hr/doctype/employee_checkin/test_employee_checkin.py @@ -139,12 +139,14 @@ def test_fetch_shift(self): timestamp = datetime.combine(date, get_time("08:45:00")) log = make_checkin(employee, timestamp) self.assertEqual(log.shift, shift_type.name) - + + # "begin checkin before shift time" = 60 mins, so should work for 7:00:00 timestamp = datetime.combine(date, get_time("07:00:00")) log = make_checkin(employee, timestamp) self.assertEqual(log.shift, shift_type.name) + # "allow checkout after shift end time" = 60 mins, so should work for 13:00:00 timestamp = datetime.combine(date, get_time("13:00:00")) log = make_checkin(employee, timestamp) @@ -165,8 +167,8 @@ def test_shift_start_and_end_timings(self): timestamp = datetime.combine(date, get_time("08:45:00")) log = make_checkin(employee, timestamp) - self.assertEqual(log.shift, shift_type.name) + self.assertEqual(log.shift_start, datetime.combine(date, get_time("08:00:00"))) self.assertEqual(log.shift_end, datetime.combine(date, get_time("12:00:00"))) self.assertEqual(log.shift_actual_start, datetime.combine(date, get_time("07:00:00"))) @@ -192,6 +194,7 @@ def test_fetch_shift_spanning_over_two_days(self): shift_type = setup_shift_type( shift_type="Midnight Shift", start_time="23:00:00", end_time="01:00:00" ) + date = getdate() next_day = add_days(date, 1) make_shift_assignment(shift_type.name, employee, date) @@ -199,8 +202,8 @@ def test_fetch_shift_spanning_over_two_days(self): # log falls in the first day timestamp = datetime.combine(date, get_time("23:00:00")) log = make_checkin(employee, timestamp) - self.assertEqual(log.shift, shift_type.name) + self.assertEqual(log.shift_start, datetime.combine(date, get_time("23:00:00"))) self.assertEqual(log.shift_end, datetime.combine(next_day, get_time("01:00:00"))) self.assertEqual(log.shift_actual_start, datetime.combine(date, get_time("22:00:00"))) @@ -271,7 +274,7 @@ def test_consecutive_shift_assignments_overlapping_within_grace_period(self): self.assertEqual(log.shift, shift2.name) self.assertEqual(log.shift_start, datetime.combine(date, get_time("12:30:00"))) self.assertEqual(log.shift_actual_start, datetime.combine(date, get_time("12:00:00"))) - + # log at 12:00 should set shift1 and actual end as 12 and not 1 since the next shift's grace starts timestamp = datetime.combine(date, get_time("12:00:00")) log = make_checkin(employee, timestamp) @@ -283,7 +286,7 @@ def test_consecutive_shift_assignments_overlapping_within_grace_period(self): timestamp = datetime.combine(date, get_time("12:01:00")) log = make_checkin(employee, timestamp) self.assertEqual(log.shift, shift2.name) - + def make_n_checkins(employee, n, hours_to_reverse=1): logs = [make_checkin(employee, now_datetime() - timedelta(hours=hours_to_reverse, minutes=n + 1))] diff --git a/erpnext/hr/doctype/shift_type/test_records.json b/erpnext/hr/doctype/shift_type/test_records.json index 9ba7e0d0708c..a62b291783e7 100644 --- a/erpnext/hr/doctype/shift_type/test_records.json +++ b/erpnext/hr/doctype/shift_type/test_records.json @@ -7,6 +7,12 @@ "days":[ {"day":"Sunday"}, {"day":"Monday"}, - {"day":"Tuesday"}] + {"day":"Tuesday"}, + {"day":"Wednesday"}, + {"day":"Thursday"}, + {"day":"Friday"}, + {"day":"Saturday"} + + ] } ] diff --git a/erpnext/hr/doctype/shift_type/test_shift_type.py b/erpnext/hr/doctype/shift_type/test_shift_type.py index 10678c1405c2..2932a5822df9 100644 --- a/erpnext/hr/doctype/shift_type/test_shift_type.py +++ b/erpnext/hr/doctype/shift_type/test_shift_type.py @@ -44,12 +44,15 @@ def test_mark_attendance(self): log_out = make_checkin(employee, timestamp) self.assertEqual(log_out.shift, shift_type.name) + shift_type.process_auto_attendance() attendance = frappe.db.get_value( "Attendance", {"shift": shift_type.name,"attendance_date":date}, ["status", "name"], as_dict=True ) + self.assertEqual(attendance.status, "Present") + def test_entry_and_exit_grace(self): from erpnext.hr.doctype.employee_checkin.test_employee_checkin import make_checkin @@ -71,10 +74,12 @@ def test_entry_and_exit_grace(self): log_in = make_checkin(employee, timestamp) self.assertEqual(log_in.shift, shift_type.name) + timestamp = datetime.combine(date, get_time("10:30:00")) log_out = make_checkin(employee, timestamp) self.assertEqual(log_out.shift, shift_type.name) + shift_type.process_auto_attendance() attendance = frappe.db.get_value( @@ -108,8 +113,10 @@ def test_working_hours_threshold_for_half_day(self): attendance = frappe.db.get_value( "Attendance", {"shift": shift_type.name,"attendance_date":date}, ["status", "working_hours"], as_dict=True ) + self.assertEqual(attendance.status, "Half Day") self.assertEqual(attendance.working_hours, 1.5) + def test_working_hours_threshold_for_absent(self): from erpnext.hr.doctype.employee_checkin.test_employee_checkin import make_checkin @@ -122,11 +129,11 @@ def test_working_hours_threshold_for_absent(self): timestamp = datetime.combine(date, get_time("08:00:00")) log_in = make_checkin(employee, timestamp) self.assertEqual(log_in.shift, shift_type.name) - + timestamp = datetime.combine(date, get_time("09:30:00")) log_out = make_checkin(employee, timestamp) self.assertEqual(log_out.shift, shift_type.name) - + shift_type.process_auto_attendance() attendance = frappe.db.get_value( @@ -134,6 +141,7 @@ def test_working_hours_threshold_for_absent(self): ) self.assertEqual(attendance.status, "Absent") self.assertEqual(attendance.working_hours, 1.5) + def test_working_hours_threshold_for_absent_and_half_day_1(self): # considers half day over absent @@ -145,6 +153,7 @@ def test_working_hours_threshold_for_absent_and_half_day_1(self): working_hours_threshold_for_half_day=1, working_hours_threshold_for_absent=2, ) + date = getdate() make_shift_assignment(shift_type.name, employee, date) @@ -161,9 +170,11 @@ def test_working_hours_threshold_for_absent_and_half_day_1(self): attendance = frappe.db.get_value( "Attendance", {"shift": shift_type.name,"attendance_date":date}, ["status", "working_hours"], as_dict=True ) + self.assertEqual(attendance.status, "Half Day") self.assertEqual(attendance.working_hours, 0.75) + def test_working_hours_threshold_for_absent_and_half_day_2(self): # considers absent over half day from erpnext.hr.doctype.employee_checkin.test_employee_checkin import make_checkin @@ -180,7 +191,7 @@ def test_working_hours_threshold_for_absent_and_half_day_2(self): timestamp = datetime.combine(date, get_time("08:00:00")) log_in = make_checkin(employee, timestamp) self.assertEqual(log_in.shift, shift_type.name) - + timestamp = datetime.combine(date, get_time("09:30:00")) log_out = make_checkin(employee, timestamp) self.assertEqual(log_out.shift, shift_type.name) @@ -201,8 +212,9 @@ def test_mark_absent_for_dates_with_no_attendance(self): shift_type.process_auto_attendance() attendance = frappe.db.get_value( - "Attendance", {"attendance_date": date, "employee": employee}, "status" + "Attendance", {"employee": employee}, "status" ) + self.assertEqual(attendance, "Absent") @set_holiday_list("Salary Slip Test Holiday List", "_Test Company") @@ -231,7 +243,7 @@ def test_get_start_and_end_dates(self): date = getdate() doj = add_days(date, -30) - relieving_date = add_days(date, -6) + relieving_date = add_days(date, -5) employee = make_employee( "test_employee_dates@example.com", company="_Test Company", @@ -241,6 +253,7 @@ def test_get_start_and_end_dates(self): shift_type = setup_shift_type( shift_type="Test Absent with no Attendance", process_attendance_after=add_days(doj, 2) ) + make_shift_assignment(shift_type.name, employee, add_days(date, -25)) shift_type.process_auto_attendance() @@ -254,6 +267,7 @@ def test_get_start_and_end_dates(self): attendance = frappe.db.get_value( "Attendance", {"attendance_date": relieving_date, "employee": employee}, "status" ) + self.assertEquals(attendance, "Absent") # should not mark absent after Relieving Date @@ -279,11 +293,11 @@ def test_skip_auto_attendance_for_duplicate_record(self): timestamp = datetime.combine(date, get_time("08:00:00")) log_in = make_checkin(employee, timestamp) self.assertEqual(log_in.shift, shift_type.name) - + + timestamp = datetime.combine(date, get_time("12:00:00")) log_out = make_checkin(employee, timestamp) self.assertEqual(log_out.shift, shift_type.name) - # auto attendance should skip marking shift_type.process_auto_attendance() @@ -312,7 +326,7 @@ def test_skip_auto_attendance_for_overlapping_shift(self): timestamp = datetime.combine(date, get_time("09:30:00")) log_in = make_checkin(employee, timestamp) self.assertEqual(log_in.shift, shift_2.name) - + timestamp = datetime.combine(date, get_time("11:00:00")) log_out = make_checkin(employee, timestamp) self.assertEqual(log_out.shift, shift_2.name) @@ -347,10 +361,11 @@ def setup_shift_type(**args): "days":[ {"day":"Sunday"}, {"day":"Monday"}, - {"day":"Tuesday"},], + {"day":"Tuesday"}, + {"day":"Wednesday"}, + {"day":"Thursday"}], } ) - holiday_list = "Employee Checkin Test Holiday List" if not frappe.db.exists("Holiday List", "Employee Checkin Test Holiday List"): holiday_list = frappe.get_doc( @@ -366,10 +381,8 @@ def setup_shift_type(**args): shift_type.holiday_list = holiday_list shift_type.update(args) shift_type.save() - return shift_type - - + def make_shift_assignment(shift_type, employee, start_date, end_date=None): shift_assignment = frappe.get_doc( { diff --git a/erpnext/hr/report/monthly_attendance_sheet/test_monthly_attendance_sheet.py b/erpnext/hr/report/monthly_attendance_sheet/test_monthly_attendance_sheet.py index 9533c6ab4be1..f8444a1e9ec4 100644 --- a/erpnext/hr/report/monthly_attendance_sheet/test_monthly_attendance_sheet.py +++ b/erpnext/hr/report/monthly_attendance_sheet/test_monthly_attendance_sheet.py @@ -32,7 +32,8 @@ def test_monthly_attendance_sheet_report(self): now = now_datetime() previous_month = now.month - 1 previous_month_first = now.replace(day=1).replace(month=previous_month).date() - + self.employee = make_employee("test_employee@example.com", company="_Test Company") + company = frappe.db.get_value("Employee", self.employee, "company") # mark different attendance status on first 3 days of previous month @@ -66,6 +67,7 @@ def test_monthly_attendance_sheet_with_detailed_view(self): now = now_datetime() previous_month = now.month - 1 previous_month_first = now.replace(day=1).replace(month=previous_month).date() + self.employee = make_employee("test_employee@example.com", company="_Test Company") company = frappe.db.get_value("Employee", self.employee, "company") @@ -104,6 +106,7 @@ def test_monthly_attendance_sheet_with_summarized_view(self): now = now_datetime() previous_month = now.month - 1 previous_month_first = now.replace(day=1).replace(month=previous_month).date() + self.employee = make_employee("test_employee@example.com", company="_Test Company") company = frappe.db.get_value("Employee", self.employee, "company") @@ -150,7 +153,8 @@ def test_monthly_attendance_sheet_with_summarized_view(self): @set_holiday_list("Salary Slip Test Holiday List", "_Test Company") def test_attendance_with_group_by_filter(self): from erpnext.hr.doctype.shift_type.test_shift_type import setup_shift_type - setup_shift_type(shift_type="Day Shift") + setup_shift_type() + self.employee = make_employee("test_employee@example.com", company="_Test Company") now = now_datetime() previous_month = now.month - 1 previous_month_first = now.replace(day=1).replace(month=previous_month).date() @@ -191,6 +195,7 @@ def test_attendance_with_employee_filter(self): now = now_datetime() previous_month = now.month - 1 previous_month_first = now.replace(day=1).replace(month=previous_month).date() + self.employee = make_employee("test_employee@example.com", company="_Test Company") company = frappe.db.get_value("Employee", self.employee, "company")