Skip to content

Commit

Permalink
fix(Shift Type): fix all test dependency to shift type
Browse files Browse the repository at this point in the history
  • Loading branch information
bahaaabed authored and alaa-alsalehi committed Sep 4, 2022
1 parent 2c457c0 commit 3f9d64e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 21 deletions.
13 changes: 8 additions & 5 deletions erpnext/hr/doctype/employee_checkin/test_employee_checkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")))
Expand All @@ -192,15 +194,16 @@ 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)

# 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")))
Expand Down Expand Up @@ -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)
Expand All @@ -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))]
Expand Down
8 changes: 7 additions & 1 deletion erpnext/hr/doctype/shift_type/test_records.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
"days":[
{"day":"Sunday"},
{"day":"Monday"},
{"day":"Tuesday"}]
{"day":"Tuesday"},
{"day":"Wednesday"},
{"day":"Thursday"},
{"day":"Friday"},
{"day":"Saturday"}

]
}
]
39 changes: 26 additions & 13 deletions erpnext/hr/doctype/shift_type/test_shift_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -122,18 +129,19 @@ 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(
"Attendance", {"shift": shift_type.name,"attendance_date":date}, ["status", "working_hours"], as_dict=True
)
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
Expand All @@ -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)

Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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(
"[email protected]",
company="_Test Company",
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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()

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand All @@ -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(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("[email protected]", company="_Test Company")

company = frappe.db.get_value("Employee", self.employee, "company")

# mark different attendance status on first 3 days of previous month
Expand Down Expand Up @@ -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("[email protected]", company="_Test Company")

company = frappe.db.get_value("Employee", self.employee, "company")

Expand Down Expand Up @@ -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("[email protected]", company="_Test Company")

company = frappe.db.get_value("Employee", self.employee, "company")

Expand Down Expand Up @@ -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("[email protected]", company="_Test Company")
now = now_datetime()
previous_month = now.month - 1
previous_month_first = now.replace(day=1).replace(month=previous_month).date()
Expand Down Expand Up @@ -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("[email protected]", company="_Test Company")

company = frappe.db.get_value("Employee", self.employee, "company")

Expand Down

0 comments on commit 3f9d64e

Please sign in to comment.