From 5c4eabf0758611047ca6800e490df12772861913 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Mon, 21 Mar 2022 19:27:42 +0100 Subject: [PATCH] fix: usage of frappe.db.exists --- .../doctype/pos_invoice/pos_invoice.py | 2 +- .../crm/doctype/appointment/appointment.py | 2 +- .../doctype/appointment/test_appointment.py | 79 ++++++++++--------- .../test_leave_application.py | 2 +- 4 files changed, 44 insertions(+), 41 deletions(-) diff --git a/erpnext/accounts/doctype/pos_invoice/pos_invoice.py b/erpnext/accounts/doctype/pos_invoice/pos_invoice.py index 91c07ade7f..2b88fe7e54 100644 --- a/erpnext/accounts/doctype/pos_invoice/pos_invoice.py +++ b/erpnext/accounts/doctype/pos_invoice/pos_invoice.py @@ -494,7 +494,7 @@ class POSInvoice(SalesInvoice): } pr = frappe.db.exists(args) if pr: - return frappe.get_doc('Payment Request', pr[0][0]) + return frappe.get_doc('Payment Request', pr) @frappe.whitelist() def get_stock_availability(item_code, warehouse): diff --git a/erpnext/crm/doctype/appointment/appointment.py b/erpnext/crm/doctype/appointment/appointment.py index 20fb987c60..c36faaf79b 100644 --- a/erpnext/crm/doctype/appointment/appointment.py +++ b/erpnext/crm/doctype/appointment/appointment.py @@ -229,5 +229,5 @@ def _get_employee_from_user(user): {'doctype': 'Employee', 'user_id': user}) if employee_docname: # 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 diff --git a/erpnext/crm/doctype/appointment/test_appointment.py b/erpnext/crm/doctype/appointment/test_appointment.py index f4086dc37c..7183737bea 100644 --- a/erpnext/crm/doctype/appointment/test_appointment.py +++ b/erpnext/crm/doctype/appointment/test_appointment.py @@ -8,50 +8,53 @@ import frappe def create_test_lead(): - test_lead = frappe.db.exists({'doctype': 'Lead', 'email_id':'test@example.com'}) - if test_lead: - return frappe.get_doc('Lead', test_lead[0][0]) - test_lead = frappe.get_doc({ - 'doctype': 'Lead', - 'lead_name': 'Test Lead', - 'email_id': 'test@example.com' - }) - test_lead.insert(ignore_permissions=True) - return test_lead + test_lead = frappe.db.exists({"doctype": "Lead", "email_id": "test@example.com"}) + if test_lead: + return frappe.get_doc("Lead", test_lead) + test_lead = frappe.get_doc( + {"doctype": "Lead", "lead_name": "Test Lead", "email_id": "test@example.com"} + ) + test_lead.insert(ignore_permissions=True) + return test_lead def create_test_appointments(): - test_appointment = frappe.db.exists( - {'doctype': 'Appointment', 'scheduled_time':datetime.datetime.now(),'email':'test@example.com'}) - if test_appointment: - return frappe.get_doc('Appointment', test_appointment[0][0]) - test_appointment = frappe.get_doc({ - 'doctype': 'Appointment', - 'email': 'test@example.com', - 'status': 'Open', - 'customer_name': 'Test Lead', - 'customer_phone_number': '666', - 'customer_skype': 'test', - 'customer_email': 'test@example.com', - 'scheduled_time': datetime.datetime.now() - }) - test_appointment.insert() - return test_appointment + test_appointment = frappe.db.exists( + { + "doctype": "Appointment", + "scheduled_time": datetime.datetime.now(), + "email": "test@example.com", + } + ) + if test_appointment: + return frappe.get_doc("Appointment", test_appointment) + test_appointment = frappe.get_doc( + { + "doctype": "Appointment", + "email": "test@example.com", + "status": "Open", + "customer_name": "Test Lead", + "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): - test_appointment = test_lead = None + test_appointment = test_lead = None - def setUp(self): - self.test_lead = create_test_lead() - self.test_appointment = create_test_appointments() + def setUp(self): + self.test_lead = create_test_lead() + self.test_appointment = create_test_appointments() - def test_calendar_event_created(self): - cal_event = frappe.get_doc( - 'Event', self.test_appointment.calendar_event) - self.assertEqual(cal_event.starts_on, - self.test_appointment.scheduled_time) + def test_calendar_event_created(self): + cal_event = frappe.get_doc("Event", self.test_appointment.calendar_event) + self.assertEqual(cal_event.starts_on, self.test_appointment.scheduled_time) - def test_lead_linked(self): - lead = frappe.get_doc('Lead', self.test_lead.name) - self.assertIsNotNone(lead) + def test_lead_linked(self): + lead = frappe.get_doc("Lead", self.test_lead.name) + self.assertIsNotNone(lead) diff --git a/erpnext/hr/doctype/leave_application/test_leave_application.py b/erpnext/hr/doctype/leave_application/test_leave_application.py index 7d32fd8865..e01bdf6e84 100644 --- a/erpnext/hr/doctype/leave_application/test_leave_application.py +++ b/erpnext/hr/doctype/leave_application/test_leave_application.py @@ -936,7 +936,7 @@ def get_leave_period(): "company": "_Test Company" }) 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: return frappe.get_doc(dict( name = 'Test Leave Period',