diff --git a/erpnext/accounts/doctype/bank_transaction/test_bank_transaction.py b/erpnext/accounts/doctype/bank_transaction/test_bank_transaction.py index 72b6893faf..d84b8e07d3 100644 --- a/erpnext/accounts/doctype/bank_transaction/test_bank_transaction.py +++ b/erpnext/accounts/doctype/bank_transaction/test_bank_transaction.py @@ -109,7 +109,7 @@ def create_bank_account(bank_name="Citi Bank", account_name="_Test Bank - _TC"): frappe.get_doc({ "doctype": "Bank", "bank_name":bank_name, - }).insert() + }).insert(ignore_if_duplicate=True) except frappe.DuplicateEntryError: pass @@ -119,7 +119,7 @@ def create_bank_account(bank_name="Citi Bank", account_name="_Test Bank - _TC"): "account_name":"Checking Account", "bank": bank_name, "account": account_name - }).insert() + }).insert(ignore_if_duplicate=True) except frappe.DuplicateEntryError: pass @@ -184,7 +184,7 @@ def add_vouchers(): "supplier_group":"All Supplier Groups", "supplier_type": "Company", "supplier_name": "Conrad Electronic" - }).insert() + }).insert(ignore_if_duplicate=True) except frappe.DuplicateEntryError: pass @@ -203,7 +203,7 @@ def add_vouchers(): "supplier_group":"All Supplier Groups", "supplier_type": "Company", "supplier_name": "Mr G" - }).insert() + }).insert(ignore_if_duplicate=True) except frappe.DuplicateEntryError: pass @@ -227,7 +227,7 @@ def add_vouchers(): "supplier_group":"All Supplier Groups", "supplier_type": "Company", "supplier_name": "Poore Simon's" - }).insert() + }).insert(ignore_if_duplicate=True) except frappe.DuplicateEntryError: pass @@ -237,7 +237,7 @@ def add_vouchers(): "customer_group":"All Customer Groups", "customer_type": "Company", "customer_name": "Poore Simon's" - }).insert() + }).insert(ignore_if_duplicate=True) except frappe.DuplicateEntryError: pass @@ -266,7 +266,7 @@ def add_vouchers(): "customer_group":"All Customer Groups", "customer_type": "Company", "customer_name": "Fayva" - }).insert() + }).insert(ignore_if_duplicate=True) except frappe.DuplicateEntryError: pass diff --git a/erpnext/accounts/report/tax_detail/test_tax_detail.py b/erpnext/accounts/report/tax_detail/test_tax_detail.py index bf668ab779..621de825ea 100644 --- a/erpnext/accounts/report/tax_detail/test_tax_detail.py +++ b/erpnext/accounts/report/tax_detail/test_tax_detail.py @@ -61,7 +61,7 @@ class TestTaxDetail(unittest.TestCase): # Create GL Entries: db_doc.submit() else: - db_doc.insert() + db_doc.insert(ignore_if_duplicate=True) except frappe.exceptions.DuplicateEntryError: pass diff --git a/erpnext/accounts/utils.py b/erpnext/accounts/utils.py index 39e84e3cef..b17b90ba6e 100644 --- a/erpnext/accounts/utils.py +++ b/erpnext/accounts/utils.py @@ -847,7 +847,7 @@ def create_payment_gateway_account(gateway, payment_channel="Email"): "payment_account": bank_account.name, "currency": bank_account.account_currency, "payment_channel": payment_channel - }).insert(ignore_permissions=True) + }).insert(ignore_permissions=True, ignore_if_duplicate=True) except frappe.DuplicateEntryError: # already exists, due to a reinstall? diff --git a/erpnext/assets/doctype/asset/test_asset.py b/erpnext/assets/doctype/asset/test_asset.py index ddbff89fc7..ffd1065efc 100644 --- a/erpnext/assets/doctype/asset/test_asset.py +++ b/erpnext/assets/doctype/asset/test_asset.py @@ -1280,7 +1280,7 @@ def create_asset(**args): if not args.do_not_save: try: - asset.save() + asset.insert(ignore_if_duplicate=True) except frappe.DuplicateEntryError: pass @@ -1321,7 +1321,7 @@ def create_fixed_asset_item(item_code=None, auto_create_assets=1, is_grouped_ass "is_grouped_asset": is_grouped_asset, "asset_naming_series": naming_series }) - item.insert() + item.insert(ignore_if_duplicate=True) except frappe.DuplicateEntryError: pass return item diff --git a/erpnext/assets/doctype/asset_category/test_asset_category.py b/erpnext/assets/doctype/asset_category/test_asset_category.py index 3d19fa39d1..2f52248edb 100644 --- a/erpnext/assets/doctype/asset_category/test_asset_category.py +++ b/erpnext/assets/doctype/asset_category/test_asset_category.py @@ -23,7 +23,7 @@ class TestAssetCategory(unittest.TestCase): }) try: - asset_category.insert() + asset_category.insert(ignore_if_duplicate=True) except frappe.DuplicateEntryError: pass diff --git a/erpnext/buying/doctype/supplier/test_supplier.py b/erpnext/buying/doctype/supplier/test_supplier.py index 13fe9df13e..0fb81b2578 100644 --- a/erpnext/buying/doctype/supplier/test_supplier.py +++ b/erpnext/buying/doctype/supplier/test_supplier.py @@ -14,151 +14,150 @@ test_records = frappe.get_test_records('Supplier') class TestSupplier(unittest.TestCase): - def test_get_supplier_group_details(self): - doc = frappe.new_doc("Supplier Group") - doc.supplier_group_name = "_Testing Supplier Group" - doc.payment_terms = "_Test Payment Term Template 3" - doc.accounts = [] - test_account_details = { - "company": "_Test Company", - "account": "Creditors - _TC", - } - doc.append("accounts", test_account_details) - doc.save() - s_doc = frappe.new_doc("Supplier") - s_doc.supplier_name = "Testing Supplier" - s_doc.supplier_group = "_Testing Supplier Group" - s_doc.payment_terms = "" - s_doc.accounts = [] - s_doc.insert() - s_doc.get_supplier_group_details() - self.assertEqual(s_doc.payment_terms, "_Test Payment Term Template 3") - self.assertEqual(s_doc.accounts[0].company, "_Test Company") - self.assertEqual(s_doc.accounts[0].account, "Creditors - _TC") - s_doc.delete() - doc.delete() + def test_get_supplier_group_details(self): + doc = frappe.new_doc("Supplier Group") + doc.supplier_group_name = "_Testing Supplier Group" + doc.payment_terms = "_Test Payment Term Template 3" + doc.accounts = [] + test_account_details = { + "company": "_Test Company", + "account": "Creditors - _TC", + } + doc.append("accounts", test_account_details) + doc.save() + s_doc = frappe.new_doc("Supplier") + s_doc.supplier_name = "Testing Supplier" + s_doc.supplier_group = "_Testing Supplier Group" + s_doc.payment_terms = "" + s_doc.accounts = [] + s_doc.insert() + s_doc.get_supplier_group_details() + self.assertEqual(s_doc.payment_terms, "_Test Payment Term Template 3") + self.assertEqual(s_doc.accounts[0].company, "_Test Company") + self.assertEqual(s_doc.accounts[0].account, "Creditors - _TC") + s_doc.delete() + doc.delete() - def test_supplier_default_payment_terms(self): - # Payment Term based on Days after invoice date - frappe.db.set_value( - "Supplier", "_Test Supplier With Template 1", "payment_terms", "_Test Payment Term Template 3") + def test_supplier_default_payment_terms(self): + # Payment Term based on Days after invoice date + frappe.db.set_value( + "Supplier", "_Test Supplier With Template 1", "payment_terms", "_Test Payment Term Template 3") - due_date = get_due_date("2016-01-22", "Supplier", "_Test Supplier With Template 1") - self.assertEqual(due_date, "2016-02-21") + due_date = get_due_date("2016-01-22", "Supplier", "_Test Supplier With Template 1") + self.assertEqual(due_date, "2016-02-21") - due_date = get_due_date("2017-01-22", "Supplier", "_Test Supplier With Template 1") - self.assertEqual(due_date, "2017-02-21") + due_date = get_due_date("2017-01-22", "Supplier", "_Test Supplier With Template 1") + self.assertEqual(due_date, "2017-02-21") - # Payment Term based on last day of month - frappe.db.set_value( - "Supplier", "_Test Supplier With Template 1", "payment_terms", "_Test Payment Term Template 1") + # Payment Term based on last day of month + frappe.db.set_value( + "Supplier", "_Test Supplier With Template 1", "payment_terms", "_Test Payment Term Template 1") - due_date = get_due_date("2016-01-22", "Supplier", "_Test Supplier With Template 1") - self.assertEqual(due_date, "2016-02-29") + due_date = get_due_date("2016-01-22", "Supplier", "_Test Supplier With Template 1") + self.assertEqual(due_date, "2016-02-29") - due_date = get_due_date("2017-01-22", "Supplier", "_Test Supplier With Template 1") - self.assertEqual(due_date, "2017-02-28") + due_date = get_due_date("2017-01-22", "Supplier", "_Test Supplier With Template 1") + self.assertEqual(due_date, "2017-02-28") - frappe.db.set_value("Supplier", "_Test Supplier With Template 1", "payment_terms", "") + frappe.db.set_value("Supplier", "_Test Supplier With Template 1", "payment_terms", "") - # Set credit limit for the supplier group instead of supplier and evaluate the due date - frappe.db.set_value("Supplier Group", "_Test Supplier Group", "payment_terms", "_Test Payment Term Template 3") + # Set credit limit for the supplier group instead of supplier and evaluate the due date + frappe.db.set_value("Supplier Group", "_Test Supplier Group", "payment_terms", "_Test Payment Term Template 3") - due_date = get_due_date("2016-01-22", "Supplier", "_Test Supplier With Template 1") - self.assertEqual(due_date, "2016-02-21") + due_date = get_due_date("2016-01-22", "Supplier", "_Test Supplier With Template 1") + self.assertEqual(due_date, "2016-02-21") - # Payment terms for Supplier Group instead of supplier and evaluate the due date - frappe.db.set_value("Supplier Group", "_Test Supplier Group", "payment_terms", "_Test Payment Term Template 1") + # Payment terms for Supplier Group instead of supplier and evaluate the due date + frappe.db.set_value("Supplier Group", "_Test Supplier Group", "payment_terms", "_Test Payment Term Template 1") - # Leap year - due_date = get_due_date("2016-01-22", "Supplier", "_Test Supplier With Template 1") - self.assertEqual(due_date, "2016-02-29") - # # Non Leap year - due_date = get_due_date("2017-01-22", "Supplier", "_Test Supplier With Template 1") - self.assertEqual(due_date, "2017-02-28") + # Leap year + due_date = get_due_date("2016-01-22", "Supplier", "_Test Supplier With Template 1") + self.assertEqual(due_date, "2016-02-29") + # # Non Leap year + due_date = get_due_date("2017-01-22", "Supplier", "_Test Supplier With Template 1") + self.assertEqual(due_date, "2017-02-28") - # Supplier with no default Payment Terms Template - frappe.db.set_value("Supplier Group", "_Test Supplier Group", "payment_terms", "") - frappe.db.set_value("Supplier", "_Test Supplier", "payment_terms", "") + # Supplier with no default Payment Terms Template + frappe.db.set_value("Supplier Group", "_Test Supplier Group", "payment_terms", "") + frappe.db.set_value("Supplier", "_Test Supplier", "payment_terms", "") - due_date = get_due_date("2016-01-22", "Supplier", "_Test Supplier") - self.assertEqual(due_date, "2016-01-22") - # # Non Leap year - due_date = get_due_date("2017-01-22", "Supplier", "_Test Supplier") - self.assertEqual(due_date, "2017-01-22") + due_date = get_due_date("2016-01-22", "Supplier", "_Test Supplier") + self.assertEqual(due_date, "2016-01-22") + # # Non Leap year + due_date = get_due_date("2017-01-22", "Supplier", "_Test Supplier") + self.assertEqual(due_date, "2017-01-22") - def test_supplier_disabled(self): - make_test_records("Item") + def test_supplier_disabled(self): + make_test_records("Item") - frappe.db.set_value("Supplier", "_Test Supplier", "disabled", 1) + frappe.db.set_value("Supplier", "_Test Supplier", "disabled", 1) - from erpnext.buying.doctype.purchase_order.test_purchase_order import create_purchase_order + from erpnext.buying.doctype.purchase_order.test_purchase_order import create_purchase_order - po = create_purchase_order(do_not_save=True) + po = create_purchase_order(do_not_save=True) - self.assertRaises(PartyDisabled, po.save) + self.assertRaises(PartyDisabled, po.save) - frappe.db.set_value("Supplier", "_Test Supplier", "disabled", 0) + frappe.db.set_value("Supplier", "_Test Supplier", "disabled", 0) - po.save() + po.save() - def test_supplier_country(self): - # Test that country field exists in Supplier DocType - supplier = frappe.get_doc('Supplier', '_Test Supplier with Country') - self.assertTrue('country' in supplier.as_dict()) + def test_supplier_country(self): + # Test that country field exists in Supplier DocType + supplier = frappe.get_doc('Supplier', '_Test Supplier with Country') + self.assertTrue('country' in supplier.as_dict()) - # Test if test supplier field record is 'Greece' - self.assertEqual(supplier.country, "Greece") + # Test if test supplier field record is 'Greece' + self.assertEqual(supplier.country, "Greece") - # Test update Supplier instance country value - supplier = frappe.get_doc('Supplier', '_Test Supplier') - supplier.country = 'Greece' - supplier.save() - self.assertEqual(supplier.country, "Greece") + # Test update Supplier instance country value + supplier = frappe.get_doc('Supplier', '_Test Supplier') + supplier.country = 'Greece' + supplier.save() + self.assertEqual(supplier.country, "Greece") - def test_party_details_tax_category(self): - from erpnext.accounts.party import get_party_details + def test_party_details_tax_category(self): + from erpnext.accounts.party import get_party_details - frappe.delete_doc_if_exists("Address", "_Test Address With Tax Category-Billing") + frappe.delete_doc_if_exists("Address", "_Test Address With Tax Category-Billing") - # Tax Category without Address - details = get_party_details("_Test Supplier With Tax Category", party_type="Supplier") - self.assertEqual(details.tax_category, "_Test Tax Category 1") + # Tax Category without Address + details = get_party_details("_Test Supplier With Tax Category", party_type="Supplier") + self.assertEqual(details.tax_category, "_Test Tax Category 1") - address = frappe.get_doc(dict( - doctype='Address', - address_title='_Test Address With Tax Category', - tax_category='_Test Tax Category 2', - address_type='Billing', - address_line1='Station Road', - city='_Test City', - country='India', - links=[dict( - link_doctype='Supplier', - link_name='_Test Supplier With Tax Category' - )] - )).insert() + address = frappe.get_doc(dict( + doctype='Address', + address_title='_Test Address With Tax Category', + tax_category='_Test Tax Category 2', + address_type='Billing', + address_line1='Station Road', + city='_Test City', + country='India', + links=[dict( + link_doctype='Supplier', + link_name='_Test Supplier With Tax Category' + )] + )).insert() - # Tax Category with Address - details = get_party_details("_Test Supplier With Tax Category", party_type="Supplier") - self.assertEqual(details.tax_category, "_Test Tax Category 2") + # Tax Category with Address + details = get_party_details("_Test Supplier With Tax Category", party_type="Supplier") + self.assertEqual(details.tax_category, "_Test Tax Category 2") - # Rollback - address.delete() + # Rollback + address.delete() def create_supplier(**args): - args = frappe._dict(args) + args = frappe._dict(args) - try: - doc = frappe.get_doc({ - "doctype": "Supplier", - "supplier_name": args.supplier_name, - "supplier_group": args.supplier_group or "Services", - "supplier_type": args.supplier_type or "Company", - "tax_withholding_category": args.tax_withholding_category - }).insert() + if frappe.db.exists("Supplier", args.supplier_name): + return frappe.get_doc("Supplier", args.supplier_name) - return doc + doc = frappe.get_doc({ + "doctype": "Supplier", + "supplier_name": args.supplier_name, + "supplier_group": args.supplier_group or "Services", + "supplier_type": args.supplier_type or "Company", + "tax_withholding_category": args.tax_withholding_category + }).insert() - except frappe.DuplicateEntryError: - return frappe.get_doc("Supplier", args.supplier_name) + return doc diff --git a/erpnext/e_commerce/shopping_cart/test_shopping_cart.py b/erpnext/e_commerce/shopping_cart/test_shopping_cart.py index 8519e68d09..6be8c94ad9 100644 --- a/erpnext/e_commerce/shopping_cart/test_shopping_cart.py +++ b/erpnext/e_commerce/shopping_cart/test_shopping_cart.py @@ -175,7 +175,7 @@ class TestShoppingCart(unittest.TestCase): def create_tax_rule(self): tax_rule = frappe.get_test_records("Tax Rule")[0] try: - frappe.get_doc(tax_rule).insert() + frappe.get_doc(tax_rule).insert(ignore_if_duplicate=True) except (frappe.DuplicateEntryError, ConflictingTaxRule): pass diff --git a/erpnext/erpnext_integrations/doctype/tally_migration/tally_migration.py b/erpnext/erpnext_integrations/doctype/tally_migration/tally_migration.py index 54ed6f7d11..26bd19f010 100644 --- a/erpnext/erpnext_integrations/doctype/tally_migration/tally_migration.py +++ b/erpnext/erpnext_integrations/doctype/tally_migration/tally_migration.py @@ -82,7 +82,7 @@ class TallyMigration(Document): "is_private": True }) try: - f.insert() + f.insert(ignore_if_duplicate=True) except frappe.DuplicateEntryError: pass setattr(self, key, f.file_url) diff --git a/erpnext/hr/doctype/employee/employee.py b/erpnext/hr/doctype/employee/employee.py index a2df26c3e2..6e52eb97ca 100755 --- a/erpnext/hr/doctype/employee/employee.py +++ b/erpnext/hr/doctype/employee/employee.py @@ -142,7 +142,7 @@ class Employee(NestedSet): "file_url": self.image, "attached_to_doctype": "User", "attached_to_name": self.user_id - }).insert() + }).insert(ignore_if_duplicate=True) except frappe.DuplicateEntryError: # already exists pass diff --git a/erpnext/hr/doctype/exit_interview/exit_interview.py b/erpnext/hr/doctype/exit_interview/exit_interview.py index 30e19f1c9b..59fb2fd9ca 100644 --- a/erpnext/hr/doctype/exit_interview/exit_interview.py +++ b/erpnext/hr/doctype/exit_interview/exit_interview.py @@ -128,4 +128,4 @@ def show_email_summary(email_success, email_failure): message += _('{0} due to missing email information for employee(s): {1}').format( frappe.bold('Sending Failed'), ', '.join(email_failure)) - frappe.msgprint(message, title=_('Exit Questionnaire'), indicator='blue', is_minimizable=True, wide=True) \ No newline at end of file + frappe.msgprint(message, title=_('Exit Questionnaire'), indicator='blue', is_minimizable=True, wide=True) diff --git a/erpnext/hr/doctype/vehicle_log/test_vehicle_log.py b/erpnext/hr/doctype/vehicle_log/test_vehicle_log.py index acd50f278c..abb288723c 100644 --- a/erpnext/hr/doctype/vehicle_log/test_vehicle_log.py +++ b/erpnext/hr/doctype/vehicle_log/test_vehicle_log.py @@ -82,7 +82,7 @@ def get_vehicle(employee_id): "vehicle_value": flt(500000) }) try: - vehicle.insert() + vehicle.insert(ignore_if_duplicate=True) except frappe.DuplicateEntryError: pass return license_plate diff --git a/erpnext/portal/doctype/homepage_section/test_homepage_section.py b/erpnext/portal/doctype/homepage_section/test_homepage_section.py index b30d983adc..c3be146bec 100644 --- a/erpnext/portal/doctype/homepage_section/test_homepage_section.py +++ b/erpnext/portal/doctype/homepage_section/test_homepage_section.py @@ -21,7 +21,7 @@ class TestHomepageSection(unittest.TestCase): {'title': 'Card 2', 'subtitle': 'Subtitle 2', 'content': 'This is test card 2', 'image': 'test.jpg'}, ], 'no_of_columns': 3 - }).insert() + }).insert(ignore_if_duplicate=True) except frappe.DuplicateEntryError: pass diff --git a/erpnext/regional/india/setup.py b/erpnext/regional/india/setup.py index 074bd527e2..e835690969 100644 --- a/erpnext/regional/india/setup.py +++ b/erpnext/regional/india/setup.py @@ -53,10 +53,7 @@ def create_hsn_codes(data, code_field): hsn_code.description = d["description"] hsn_code.hsn_code = d[code_field] hsn_code.name = d[code_field] - try: - hsn_code.db_insert() - except frappe.DuplicateEntryError: - pass + hsn_code.db_insert(ignore_if_duplicate=True) def add_custom_roles_for_reports(): for report_name in ('GST Sales Register', 'GST Purchase Register', diff --git a/erpnext/setup/utils.py b/erpnext/setup/utils.py index 4441bb9562..a4f2207f11 100644 --- a/erpnext/setup/utils.py +++ b/erpnext/setup/utils.py @@ -155,7 +155,7 @@ def insert_record(records): doc = frappe.new_doc(r.get("doctype")) doc.update(r) try: - doc.insert(ignore_permissions=True) + doc.insert(ignore_permissions=True, ignore_if_duplicate=True) except frappe.DuplicateEntryError as e: # pass DuplicateEntryError and continue if e.args and e.args[0]==doc.doctype and e.args[1]==doc.name: diff --git a/erpnext/stock/doctype/batch/test_batch.py b/erpnext/stock/doctype/batch/test_batch.py index baa03024af..613dd3f14d 100644 --- a/erpnext/stock/doctype/batch/test_batch.py +++ b/erpnext/stock/doctype/batch/test_batch.py @@ -433,14 +433,13 @@ def create_price_list_for_batch(item_code, batch, rate): def make_new_batch(**args): args = frappe._dict(args) - try: + if frappe.db.exists("Batch", args.batch_id): + batch = frappe.get_doc("Batch", args.batch_id) + else: batch = frappe.get_doc({ "doctype": "Batch", "batch_id": args.batch_id, "item": args.item_code, }).insert() - except frappe.DuplicateEntryError: - batch = frappe.get_doc("Batch", args.batch_id) - return batch