From 455595f2f52dafc93200bb583a8dd248bbee9739 Mon Sep 17 00:00:00 2001 From: AarDG10 Date: Sat, 25 Apr 2026 11:09:05 +0530 Subject: [PATCH] refactor(test): minor refactor and added test to check query --- frappe/tests/test_db_query.py | 36 ++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/frappe/tests/test_db_query.py b/frappe/tests/test_db_query.py index e25d24a7f0..27f9a1b201 100644 --- a/frappe/tests/test_db_query.py +++ b/frappe/tests/test_db_query.py @@ -1180,7 +1180,7 @@ class TestDBQuery(IntegrationTestCase): self.assertEqual(len(data["values"]), 1) def test_self_referential_link_joins(self): - from frappe.desk.reportview import get + """Test that joined aliases are distinct, when a DocType has multiple links to itself.""" if not frappe.db.exists("DocType", "Self Linked DocType"): frappe.get_doc( @@ -1215,29 +1215,31 @@ class TestDBQuery(IntegrationTestCase): else: frappe.db.delete("Self Linked DocType") - root = frappe.get_doc({"doctype": "Self Linked DocType", "title": "Root"}).insert() - sibling = frappe.get_doc({"doctype": "Self Linked DocType", "title": "Sibling"}).insert() + first_link = frappe.get_doc({"doctype": "Self Linked DocType", "title": "Reference1"}).insert() + second_link = frappe.get_doc({"doctype": "Self Linked DocType", "title": "Reference2"}).insert() frappe.get_doc( { "doctype": "Self Linked DocType", - "title": "Child", - "parent_ref": root.name, - "sibling_ref": sibling.name, + "title": "Linked Doc", + "parent_ref": first_link.name, + "sibling_ref": second_link.name, } ).insert() - frappe.form_dict.doctype = "Self Linked DocType" - frappe.form_dict.fields = [ - "`tabSelf Linked DocType`.`name`", - "`tabSelf Linked DocType`.`parent_ref`", - "`tabSelf Linked DocType`.`sibling_ref`", - "parent_ref.title as parent_title", - "sibling_ref.title as sibling_title", - ] + fields = ["name", "parent_ref.title as parent_title", "sibling_ref.title as sibling_title"] + data = frappe.get_all( + "Self Linked DocType", + fields=fields, + ) + self.assertEqual(len(data), 3) - # Shouldn't raise pymysql.err.OperationalError: (1066, "Not unique table/alias: 'tabSelf Linked DocType2'") - data = get() - self.assertEqual(len(data["values"]), 3) + query = frappe.qb.get_query( + "Self Linked DocType", + fields=fields, + ).get_sql() + + self.assertIn("LEFT JOIN `tabSelf Linked DocType` `tabSelf Linked DocType_parent_ref`", query) + self.assertIn("LEFT JOIN `tabSelf Linked DocType` `tabSelf Linked DocType_sibling_ref`", query) def test_select_star_expansion(self): count = frappe.get_list("Language", [{"SUM": 1}, {"COUNT": "*"}], as_list=1, order_by=None)[0]