From 0fdc961a4b3fc25ade007df5e8af5bf6ad96a9e5 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Fri, 16 May 2025 15:19:31 +0530 Subject: [PATCH] refactor(test): bare bones presets for company --- erpnext/setup/demo.py | 16 +- erpnext/setup/doctype/company/test_company.py | 6 +- erpnext/tests/utils.py | 173 ++++++++++++++++++ 3 files changed, 191 insertions(+), 4 deletions(-) diff --git a/erpnext/setup/demo.py b/erpnext/setup/demo.py index 7835aeb9a9e..ec48a3e1447 100644 --- a/erpnext/setup/demo.py +++ b/erpnext/setup/demo.py @@ -57,8 +57,20 @@ def clear_demo_data(): def create_demo_company(): - company = frappe.db.get_all("Company")[0].name - company_doc = frappe.get_doc("Company", company) + if frappe.flags.in_test: + hash = frappe.generate_hash(length=3) + company_doc = frappe._dict( + { + "company_name": "Test Company" + " " + hash, + "abbr": "TC" + hash, + "default_currency": "INR", + "country": "India", + "chart_of_accounts": "Standard", + } + ) + else: + company = frappe.db.get_all("Company")[0].name + company_doc = frappe.get_doc("Company", company).as_dict() # Make a dummy company new_company = frappe.new_doc("Company") diff --git a/erpnext/setup/doctype/company/test_company.py b/erpnext/setup/doctype/company/test_company.py index 40a40445ede..0e04e24b0f9 100644 --- a/erpnext/setup/doctype/company/test_company.py +++ b/erpnext/setup/doctype/company/test_company.py @@ -21,6 +21,7 @@ class TestCompany(ERPNextTestSuite): company.default_currency = "INR" company.create_chart_of_accounts_based_on = "Existing Company" company.existing_company = "_Test Company" + company.country = "India" company.save() expected_results = { @@ -67,6 +68,7 @@ class TestCompany(ERPNextTestSuite): company.default_currency = "USD" company.create_chart_of_accounts_based_on = "Standard Template" company.chart_of_accounts = template + company.country = country company.save() account_types = [ @@ -108,11 +110,11 @@ class TestCompany(ERPNextTestSuite): max_rgt = frappe.db.sql("select max(rgt) from `tabCompany`")[0][0] if not records: - records = self.globalTestRecords["Company"][2:] + records = self.companies[2:] for company in records: lft, rgt, parent_company = frappe.db.get_value( - "Company", company["company_name"], ["lft", "rgt", "parent_company"] + "Company", company.get("company_name"), ["lft", "rgt", "parent_company"] ) if parent_company: diff --git a/erpnext/tests/utils.py b/erpnext/tests/utils.py index 2660982f54e..a2a9ba57377 100644 --- a/erpnext/tests/utils.py +++ b/erpnext/tests/utils.py @@ -5,8 +5,10 @@ import unittest from typing import Any, NewType import frappe +from frappe import _ from frappe.core.doctype.report.report import get_report_module_dotted_path from frappe.tests import IntegrationTestCase +from frappe.utils import now_datetime ReportFilters = dict[str, Any] ReportName = NewType("ReportName", str) @@ -126,9 +128,125 @@ class ERPNextTestSuite(unittest.TestCase): @classmethod def make_persistant_master_data(cls): + # presets and default are mandatory for company + cls.make_warehouse_type() + cls.make_uom() + cls.make_address_template() + cls.make_fiscal_year() cls.make_company() + cls.update_stock_settings() + frappe.db.commit() + @classmethod + def update_stock_settings(cls): + stock_settings = frappe.get_doc("Stock Settings") + stock_settings.item_naming_by = "Item Code" + stock_settings.valuation_method = "FIFO" + stock_settings.default_warehouse = frappe.db.get_value("Warehouse", {"warehouse_name": _("Stores")}) + stock_settings.stock_uom = "Nos" + stock_settings.auto_indent = 1 + stock_settings.auto_insert_price_list_rate_if_missing = 1 + stock_settings.update_price_list_based_on = "Rate" + stock_settings.set_qty_in_transactions_based_on_serial_no_input = 1 + stock_settings.enable_serial_and_batch_no_for_item = 1 + stock_settings.save() + + @classmethod + def make_price_list(cls): + records = [ + { + "doctype": "Price List", + "price_list_name": _("Standard Buying"), + "enabled": 1, + "buying": 1, + "selling": 0, + "currency": "INR", + }, + { + "doctype": "Price List", + "price_list_name": _("Standard Selling"), + "enabled": 1, + "buying": 0, + "selling": 1, + "currency": "INR", + }, + ] + cls.price_list = [] + for x in records: + if not frappe.db.exists( + "Price List", + { + "price_list_name": x.get("price_list_name"), + "enabled": x.get("enabled"), + "selling": x.get("selling"), + "buying": x.get("buying"), + "currency": x.get("currency"), + }, + ): + cls.price_list.append(frappe.get_doc(x).insert()) + else: + cls.price_list.append( + frappe.get_doc( + "Price List", + { + "price_list_name": x.get("price_list_name"), + "enabled": x.get("enabled"), + "selling": x.get("selling"), + "buying": x.get("buying"), + "currency": x.get("currency"), + }, + ) + ) + + @classmethod + def make_address_template(cls): + records = [ + { + "doctype": "Address Template", + "country": "India", + "is_default": True, + "template": """ + {{ address_line1 }}
+ {% if address_line2 %}{{ address_line2 }}
{% endif -%} + {{ city }}
+ {% if state %}{{ state }}
{% endif -%} + {% if pincode %}{{ pincode }}
{% endif -%} + {{ country }}
+
+ {% if phone %}{{ _("Phone") }}: {{ phone }}
{% endif -%} + {% if fax %}{{ _("Fax") }}: {{ fax }}
{% endif -%} + {% if email_id %}{{ _("Email") }}: {{ email_id }}
{% endif -%} + """, + } + ] + cls.address_template = [] + for x in records: + if not frappe.db.exists("Address Template", {"country": x.get("country")}): + cls.address_template.append(frappe.get_doc(x).insert()) + else: + cls.address_template.append(frappe.get_doc("Address Template", {"country": x.get("country")})) + + @classmethod + def make_uom(cls): + records = [{"doctype": "UOM", "uom_name": "Nos", "must_be_whole_number": 1, "common_code": "C62"}] + cls.uom = [] + for x in records: + if not frappe.db.exists("UOM", {"uom_name": x.get("uom_name")}): + cls.warehouse_type.append(frappe.get_doc(x).insert()) + else: + cls.warehouse_type.append(frappe.get_doc("UOM", {"uom_name": x.get("uom_name")})) + + @classmethod + def make_warehouse_type(cls): + records = [{"doctype": "Warehouse Type", "name": "Transit"}] + cls.warehouse_type = [] + for x in records: + if not frappe.db.exists("Warehouse Type", {"name": x.get("name")}): + cls.warehouse_type.append(frappe.get_doc(x).insert()) + else: + cls.warehouse_type.append(frappe.get_doc("Warehouse Type", {"name": x.get("name")})) + @classmethod def make_monthly_distribution(cls): records = [ @@ -449,3 +567,58 @@ class ERPNextTestSuite(unittest.TestCase): cls.companies.append(frappe.get_doc(x).insert()) else: cls.companies.append(frappe.get_doc("Company", {"company_name": x.get("company_name")})) + + @classmethod + def make_fiscal_year(cls): + records = [ + { + "doctype": "Fiscal Year", + "year": "_Test Short Fiscal Year 2011", + "is_short_year": 1, + "year_start_date": "2011-04-01", + "year_end_date": "2011-12-31", + } + ] + + start = 2012 + this_year = now_datetime().year + end = now_datetime().year + 25 + # The current year fails to load with the following error: + # Year start date or end date is overlapping with 2024. To avoid please set company + # This is a quick-fix: if current FY is needed, please refactor test data properly + for year in range(start, this_year): + records.append( + { + "doctype": "Fiscal Year", + "year": f"_Test Fiscal Year {year}", + "year_start_date": f"{year}-01-01", + "year_end_date": f"{year}-12-31", + } + ) + for year in range(this_year + 1, end): + records.append( + { + "doctype": "Fiscal Year", + "year": f"_Test Fiscal Year {year}", + "year_start_date": f"{year}-01-01", + "year_end_date": f"{year}-12-31", + } + ) + + cls.fiscal_year = [] + for x in records: + if not frappe.db.exists( + "Fiscal Year", + {"year_start_date": x.get("year_start_date"), "year_end_date": x.get("year_end_date")}, + ): + cls.fiscal_year.append(frappe.get_doc(x).insert()) + else: + cls.fiscal_year.append( + frappe.get_doc( + "Fiscal Year", + { + "year_start_date": x.get("year_start_date"), + "year_end_date": x.get("year_end_date"), + }, + ) + )