From 841f2f4a36d984d9745146f656f2465abe183e1e Mon Sep 17 00:00:00 2001 From: marination Date: Tue, 3 Nov 2020 21:51:37 +0530 Subject: [PATCH] chore: Rename Doctype Test and more explicit comment - Better decription of why the fix is done, what case it handles - Test for Renaming Doctype and Record having same name as DocType --- frappe/model/rename_doc.py | 10 ++++++++-- frappe/tests/test_document.py | 37 ++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/frappe/model/rename_doc.py b/frappe/model/rename_doc.py index e04d59ab6a..789a7f51cf 100644 --- a/frappe/model/rename_doc.py +++ b/frappe/model/rename_doc.py @@ -251,8 +251,14 @@ def update_link_field_values(link_fields, old, new, doctype): else: parent = field['parent'] - # because the table hasn't been renamed yet! - if field['parent'] == new and doctype == "DocType": + # Handles the case where one of the link fields belongs to + # the DocType being renamed. + # Here this field could have the current DocType as its value too. + + # In this case while updating link field value, the field's parent + # or the current DocType table name hasn't been renamed yet, + # so consider it's old name. + if parent == new and doctype == "DocType": parent = old frappe.db.sql(""" diff --git a/frappe/tests/test_document.py b/frappe/tests/test_document.py index c96076cfba..4e9984e89a 100644 --- a/frappe/tests/test_document.py +++ b/frappe/tests/test_document.py @@ -288,4 +288,39 @@ class TestDocument(unittest.TestCase): self.assertEqual(merged_todo_doc.priority, second_todo_doc.priority) for docname in available_documents: - frappe.delete_doc(doctype, docname) \ No newline at end of file + frappe.delete_doc(doctype, docname) + + def test_rename_doctype(self): + from frappe.core.doctype.doctype.test_doctype import new_doctype + + fields =[{ + "label": "Linked To", + "fieldname": "linked_to_doctype", + "fieldtype": "Link", + "options": "DocType", + "unique": 0 + }] + if not frappe.db.exists("DocType", "Rename This"): + new_doctype("Rename This", unique=0, fields=fields).insert() + + to_rename_record = frappe.get_doc({ + "doctype": "Rename This", + "linked_to_doctype": "Rename This" + }) + to_rename_record.insert() + + # Rename doctype + self.assertEqual("Renamed Doc", frappe.rename_doc("DocType", "Rename This", "Renamed Doc", force=True)) + + # Test if Doctype value has changed in Link field + renamed_doctype_record = frappe.get_doc("Renamed Doc", to_rename_record.name) + self.assertEqual(renamed_doctype_record.linked_to_doctype, "Renamed Doc") + + # Test if there are conflicts between a record and a DocType + # having the same name + old_name = to_rename_record.name + new_name = "ToDo" + self.assertEqual(new_name, frappe.rename_doc("Renamed Doc", old_name, new_name, force=True)) + + frappe.delete_doc_if_exists("Renamed Doc", "ToDo") + frappe.delete_doc_if_exists("DocType", "Renamed Doc") \ No newline at end of file