fix: usage of frappe.db.exists

This commit is contained in:
barredterra 2022-03-21 19:27:42 +01:00 committed by Ankush Menat
parent 3eb5440aa9
commit 5c4eabf075
4 changed files with 44 additions and 41 deletions

View File

@ -494,7 +494,7 @@ class POSInvoice(SalesInvoice):
} }
pr = frappe.db.exists(args) pr = frappe.db.exists(args)
if pr: if pr:
return frappe.get_doc('Payment Request', pr[0][0]) return frappe.get_doc('Payment Request', pr)
@frappe.whitelist() @frappe.whitelist()
def get_stock_availability(item_code, warehouse): def get_stock_availability(item_code, warehouse):

View File

@ -229,5 +229,5 @@ def _get_employee_from_user(user):
{'doctype': 'Employee', 'user_id': user}) {'doctype': 'Employee', 'user_id': user})
if employee_docname: if employee_docname:
# frappe.db.exists returns a tuple of a tuple # frappe.db.exists returns a tuple of a tuple
return frappe.get_doc('Employee', employee_docname[0][0]) return frappe.get_doc('Employee', employee_docname)
return None return None

View File

@ -8,50 +8,53 @@ import frappe
def create_test_lead(): def create_test_lead():
test_lead = frappe.db.exists({'doctype': 'Lead', 'email_id':'test@example.com'}) test_lead = frappe.db.exists({"doctype": "Lead", "email_id": "test@example.com"})
if test_lead: if test_lead:
return frappe.get_doc('Lead', test_lead[0][0]) return frappe.get_doc("Lead", test_lead)
test_lead = frappe.get_doc({ test_lead = frappe.get_doc(
'doctype': 'Lead', {"doctype": "Lead", "lead_name": "Test Lead", "email_id": "test@example.com"}
'lead_name': 'Test Lead', )
'email_id': 'test@example.com' test_lead.insert(ignore_permissions=True)
}) return test_lead
test_lead.insert(ignore_permissions=True)
return test_lead
def create_test_appointments(): def create_test_appointments():
test_appointment = frappe.db.exists( test_appointment = frappe.db.exists(
{'doctype': 'Appointment', 'scheduled_time':datetime.datetime.now(),'email':'test@example.com'}) {
if test_appointment: "doctype": "Appointment",
return frappe.get_doc('Appointment', test_appointment[0][0]) "scheduled_time": datetime.datetime.now(),
test_appointment = frappe.get_doc({ "email": "test@example.com",
'doctype': 'Appointment', }
'email': 'test@example.com', )
'status': 'Open', if test_appointment:
'customer_name': 'Test Lead', return frappe.get_doc("Appointment", test_appointment)
'customer_phone_number': '666', test_appointment = frappe.get_doc(
'customer_skype': 'test', {
'customer_email': 'test@example.com', "doctype": "Appointment",
'scheduled_time': datetime.datetime.now() "email": "test@example.com",
}) "status": "Open",
test_appointment.insert() "customer_name": "Test Lead",
return test_appointment "customer_phone_number": "666",
"customer_skype": "test",
"customer_email": "test@example.com",
"scheduled_time": datetime.datetime.now(),
}
)
test_appointment.insert()
return test_appointment
class TestAppointment(unittest.TestCase): class TestAppointment(unittest.TestCase):
test_appointment = test_lead = None test_appointment = test_lead = None
def setUp(self): def setUp(self):
self.test_lead = create_test_lead() self.test_lead = create_test_lead()
self.test_appointment = create_test_appointments() self.test_appointment = create_test_appointments()
def test_calendar_event_created(self): def test_calendar_event_created(self):
cal_event = frappe.get_doc( cal_event = frappe.get_doc("Event", self.test_appointment.calendar_event)
'Event', self.test_appointment.calendar_event) self.assertEqual(cal_event.starts_on, self.test_appointment.scheduled_time)
self.assertEqual(cal_event.starts_on,
self.test_appointment.scheduled_time)
def test_lead_linked(self): def test_lead_linked(self):
lead = frappe.get_doc('Lead', self.test_lead.name) lead = frappe.get_doc("Lead", self.test_lead.name)
self.assertIsNotNone(lead) self.assertIsNotNone(lead)

View File

@ -936,7 +936,7 @@ def get_leave_period():
"company": "_Test Company" "company": "_Test Company"
}) })
if leave_period_name: if leave_period_name:
return frappe.get_doc("Leave Period", leave_period_name[0][0]) return frappe.get_doc("Leave Period", leave_period_name)
else: else:
return frappe.get_doc(dict( return frappe.get_doc(dict(
name = 'Test Leave Period', name = 'Test Leave Period',