refactor(test): minor refactor and added test to check query

This commit is contained in:
AarDG10 2026-04-25 11:09:05 +05:30
parent 55e342bbd5
commit 455595f2f5

View file

@ -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]