agritheory / test_utils

Common Test Utilities and Fixtures for AgriTheory Projects
4 stars 4 forks source link

feat: add alternative chart of accounts #49

Closed HKuz closed 4 months ago

HKuz commented 5 months ago

21

HKuz commented 5 months ago

I'm opening this up for feedback. For testing, I ended up creating a dummy ERPNext app that I installed on a fresh version-15 bench/site, pip-installed my branch of test_utils into the environment, then ran a simplified tests/setup script to check creation of each different option for the COA. Full instructions in the README if that's something that could help anyone else.

For the IFRS COA, it generally used the Stonecrop example with a few modifications:

The way the functions are called is open for discussion. Currently, the way things work is that the setup_complete args still need to specify either "Standard with Numbers" or "Standard", then if you want to install an alternative COA ("Farm" or "IFRS"), that's specified in create_test_data. The COA is created before settings are defined since settings needs the default bank account for the company in place.

I included the create_bank_and_bank_account code in this feature, but I left out the Mode of Payment stuff (the "ACH/EFT" option needs the app to have a Mode of Payment Type of "Electronic" in place to work, which is app-specific). I did include the special Electronic Payments account creation code, but given it's app-specific (and assumes "Standard with Numbers" option) I can remove that if we want.

Example setup.py script to run this code:

# other frappe/erpnext imports

from test_utils.utils.chart_of_accounts import setup_chart_of_accounts, create_bank_and_bank_account

def before_test():
    frappe.clear_cache()
    today = frappe.utils.getdate()
    setup_complete(
        {
            "currency": "USD",
            "full_name": "Administrator",
            "company_name": "Chelsea Fruit Co",
            "timezone": "America/New_York",
            "company_abbr": "CFC",
            "domains": ["Distribution"],
            "country": "United States",
            "fy_start_date": today.replace(month=1, day=1).isoformat(),
            "fy_end_date": today.replace(month=12, day=31).isoformat(),
            "language": "english",
            "company_tagline": "Chelsea Fruit Co",
            "email": "support@agritheory.dev",
            "password": "admin",
            "chart_of_accounts": "Standard with Numbers",  # should be either Standard or Standard with Numbers
            "bank_account": "Primary Checking",
        }
    )
    # enable_all_roles_and_domains()
    set_defaults_for_tests()
    frappe.db.commit()
    create_test_data()
    for modu in frappe.get_all("Module Onboarding"):
        frappe.db.set_value("Module Onboarding", modu, "is_complete", 1)
    frappe.set_value("Website Settings", "Website Settings", "home_page", "login")
    frappe.db.commit()

def create_test_data():
    today = frappe.utils.getdate()

    chart_of_accounts = "IFRS"  # if None, defaults to "Standard with Numbers", otherwise where to specify "Farm" or "IFRS"
    company = frappe.defaults.get_defaults().get("company")
    setup_chart_of_accounts(company=company, chart_template=chart_of_accounts)

    settings = frappe._dict(
        {
            "day": today.replace(month=1, day=1),
            "company": company,
            "company_account": frappe.get_value(  # Needs the COA set up to grab this account
                "Account",
                {
                    "account_type": "Bank",
                    "company": company,
                    "is_group": 0,
                },
            ),
            "warehouse": frappe.get_value(
                "Warehouse",
                {
                    "warehouse_name": "Finished Goods",
                    "company": company,
                },
            ),
        }
    )

    create_bank_and_bank_account(settings)