[Tests] Shift Planning (#14570)

* Cancel shift assignment on cancellation of shift request

* [minor] Fix shift type test

* Modify test for shift request

* Add test case for shift assignment

* Fix syntax

* Fix codacy
This commit is contained in:
Shreya Shah 2018-06-19 10:45:33 +05:30 committed by Nabin Hait
parent b0fafa9774
commit cbff30f718
5 changed files with 47 additions and 10 deletions

View File

@ -12,7 +12,7 @@ class OverlapError(frappe.ValidationError): pass
class ShiftAssignment(Document):
def validate(self):
self.validate_overlapping_dates();
self.validate_overlapping_dates()
def validate_overlapping_dates(self):
if not self.name:

View File

@ -5,6 +5,23 @@ from __future__ import unicode_literals
import frappe
import unittest
from frappe.utils import nowdate
test_dependencies = ["Shift Type"]
class TestShiftAssignment(unittest.TestCase):
pass
def setUp(self):
frappe.db.sql("delete from `tabShift Assignment`")
def test_make_shift_assignment(self):
shift_assignment = frappe.get_doc({
"doctype": "Shift Assignment",
"shift_type": "Day Shift",
"company": "_Test Company",
"employee": "_T-Employee-00001",
"date": nowdate()
}).insert()
shift_assignment.submit()
self.assertEqual(shift_assignment.docstatus, 1)

View File

@ -12,8 +12,8 @@ class OverlapError(frappe.ValidationError): pass
class ShiftRequest(Document):
def validate(self):
self.validate_dates();
self.validate_shift_request_overlap_dates();
self.validate_dates()
self.validate_shift_request_overlap_dates()
def on_submit(self):
date_list = self.get_working_days(self.from_date, self.to_date)
@ -27,6 +27,14 @@ class ShiftRequest(Document):
assignment_doc.insert()
assignment_doc.submit()
def on_cancel(self):
shift_assignment_list = frappe.get_list("Shift Assignment", {'employee': self.employee, 'shift_request': self.name})
if shift_assignment_list:
for shift in shift_assignment_list:
shift_assignment_doc = frappe.get_doc("Shift Assignment", shift['name'])
shift_assignment_doc.cancel()
def validate_dates(self):
if self.from_date and self.to_date and (getdate(self.to_date) < getdate(self.from_date)):
frappe.throw(_("To date cannot be before from date"))

View File

@ -8,6 +8,10 @@ import unittest
from frappe.utils import nowdate
class TestShiftRequest(unittest.TestCase):
def setUp(self):
for doctype in ["Shift Request", "Shift Assignment"]:
frappe.db.sql("delete from `tab{doctype}`".format(doctype=doctype))
def test_make_shift_request(self):
shift_request = frappe.get_doc({
"doctype": "Shift Request",
@ -20,9 +24,14 @@ class TestShiftRequest(unittest.TestCase):
})
shift_request.insert()
shift_request.submit()
shift_assignment = frappe.db.sql("""select employee
from `tabShift Assignment`
where shift_request = %s""", shift_request.name)
if shift_assignment:
employee = shift_assignment[0][0]
self.assertEqual(shift_request.employee, employee)
shift_assignments = frappe.db.sql('''
SELECT shift_request, employee
FROM `tabShift Assignment`
WHERE shift_request = '{0}'
'''.format(shift_request.name), as_dict=1)
for d in shift_assignments:
employee = d.get('employee')
self.assertEqual(shift_request.employee, employee)
shift_request.cancel()
shift_assignment_doc = frappe.get_doc("Shift Assignment", {"shift_request": d.get('shift_request')})
self.assertEqual(shift_assignment_doc.docstatus, 2)

View File

@ -8,6 +8,8 @@ import unittest
class TestShiftType(unittest.TestCase):
def test_make_shift_type(self):
if frappe.db.exists("Shift Type", "Day Shift"):
return
shift_type = frappe.get_doc({
"doctype": "Shift Type",
"name": "Day Shift",
@ -15,3 +17,4 @@ class TestShiftType(unittest.TestCase):
"end_time": "18:00:00"
})
shift_type.insert()