Skip to content

Commit

Permalink
Merge pull request #43612 from blaggacao/refactor/alignment-in-test-r…
Browse files Browse the repository at this point in the history
…ecord-creation

test: step 2 refactor towards idempotency
  • Loading branch information
blaggacao authored Oct 12, 2024
2 parents 727b395 + 8867702 commit cc954d9
Show file tree
Hide file tree
Showing 37 changed files with 247 additions and 294 deletions.
2 changes: 1 addition & 1 deletion erpnext/accounts/doctype/account/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def test_account_balance(self):


def _make_test_records(verbose=None):
from frappe.test_runner import make_test_objects
from frappe.tests.utils import make_test_objects

accounts = [
# [account_name, parent_account, is_group]
Expand Down
5 changes: 0 additions & 5 deletions erpnext/accounts/doctype/cost_center/test_cost_center.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@
import frappe
from frappe.tests import IntegrationTestCase

test_records = frappe.get_test_records("Cost Center")


class TestCostCenter(IntegrationTestCase):
def test_cost_center_creation_against_child_node(self):
if not frappe.db.get_value("Cost Center", {"name": "_Test Cost Center 2 - _TC"}):
frappe.get_doc(test_records[1]).insert()

cost_center = frappe.get_doc(
{
"doctype": "Cost Center",
Expand Down
12 changes: 4 additions & 8 deletions erpnext/accounts/doctype/journal_entry/test_journal_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import frappe
from frappe.tests import IntegrationTestCase, UnitTestCase
from frappe.tests.utils import change_settings
from frappe.utils import flt, nowdate

from erpnext.accounts.doctype.account.test_account import get_inventory_account
Expand All @@ -25,22 +24,22 @@ class TestJournalEntry(IntegrationTestCase):
"Accounts Settings", {"unlink_payment_on_cancellation_of_invoice": 1}
)
def test_journal_entry_with_against_jv(self):
jv_invoice = frappe.copy_doc(test_records[2])
base_jv = frappe.copy_doc(test_records[0])
jv_invoice = frappe.copy_doc(self.globalTestRecords["Journal Entry"][2])
base_jv = frappe.copy_doc(self.globalTestRecords["Journal Entry"][0])
self.jv_against_voucher_testcase(base_jv, jv_invoice)

def test_jv_against_sales_order(self):
from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order

sales_order = make_sales_order(do_not_save=True)
base_jv = frappe.copy_doc(test_records[0])
base_jv = frappe.copy_doc(self.globalTestRecords["Journal Entry"][0])
self.jv_against_voucher_testcase(base_jv, sales_order)

def test_jv_against_purchase_order(self):
from erpnext.buying.doctype.purchase_order.test_purchase_order import create_purchase_order

purchase_order = create_purchase_order(do_not_save=True)
base_jv = frappe.copy_doc(test_records[1])
base_jv = frappe.copy_doc(self.globalTestRecords["Journal Entry"][1])
self.jv_against_voucher_testcase(base_jv, purchase_order)

def jv_against_voucher_testcase(self, base_jv, test_voucher):
Expand Down Expand Up @@ -589,6 +588,3 @@ def make_journal_entry(
jv.submit()

return jv


test_records = frappe.get_test_records("Journal Entry")
9 changes: 3 additions & 6 deletions erpnext/accounts/doctype/payment_entry/test_payment_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order
from erpnext.setup.doctype.employee.test_employee import make_employee

EXTRA_TEST_RECORD_DEPENDENCIES = ["Item"]
EXTRA_TEST_RECORD_DEPENDENCIES = ["Item", "Currency Exchange"]


class UnitTestPaymentEntry(UnitTestCase):
Expand Down Expand Up @@ -619,12 +619,9 @@ def test_payment_entry_against_si_usd_to_usd_with_deduction_in_base_currency(sel
self.assertEqual(flt(pe.references[0].exchange_gain_loss, 2), -94.74)

def test_payment_entry_retrieves_last_exchange_rate(self):
from erpnext.setup.doctype.currency_exchange.test_currency_exchange import (
save_new_records,
test_records,
)
from erpnext.setup.doctype.currency_exchange.test_currency_exchange import save_new_records

save_new_records(test_records)
save_new_records(self.globalTestRecords["Currency Exchange"])

pe = frappe.new_doc("Payment Entry")
pe.payment_type = "Pay"
Expand Down
10 changes: 9 additions & 1 deletion erpnext/accounts/doctype/pos_invoice/test_pos_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
class TestPOSInvoice(IntegrationTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.enterClassContext(cls.change_settings("Selling Settings", validate_selling_price=0))
make_stock_entry(target="_Test Warehouse - _TC", item_code="_Test Item", qty=800, basic_rate=100)
frappe.db.sql("delete from `tabTax Rule`")

Expand Down Expand Up @@ -248,6 +250,7 @@ def test_pos_return_for_serialized_item(self):
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_serialized_item

se = make_serialized_item(
self,
company="_Test Company",
target_warehouse="Stores - _TC",
cost_center="Main - _TC",
Expand Down Expand Up @@ -289,6 +292,7 @@ def test_partial_pos_returns(self):
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_serialized_item

se = make_serialized_item(
self,
company="_Test Company",
target_warehouse="Stores - _TC",
cost_center="Main - _TC",
Expand Down Expand Up @@ -377,6 +381,7 @@ def test_serialized_item_transaction(self):
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_serialized_item

se = make_serialized_item(
self,
company="_Test Company",
target_warehouse="Stores - _TC",
cost_center="Main - _TC",
Expand Down Expand Up @@ -431,6 +436,7 @@ def test_delivered_serialized_item_transaction(self):
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_serialized_item

se = make_serialized_item(
self,
company="_Test Company",
target_warehouse="Stores - _TC",
cost_center="Main - _TC",
Expand Down Expand Up @@ -482,6 +488,7 @@ def test_invalid_serial_no_validation(self):
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_serialized_item

se = make_serialized_item(
self,
company="_Test Company",
target_warehouse="Stores - _TC",
cost_center="Main - _TC",
Expand Down Expand Up @@ -512,6 +519,7 @@ def test_value_error_on_serial_no_validation(self):
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_serialized_item

se = make_serialized_item(
self,
company="_Test Company",
target_warehouse="Stores - _TC",
cost_center="Main - _TC",
Expand Down Expand Up @@ -901,7 +909,7 @@ def test_delivered_serial_no_case(self):

frappe.db.savepoint("before_test_delivered_serial_no_case")
try:
se = make_serialized_item()
se = make_serialized_item(self)
serial_no = get_serial_nos_from_bundle(se.get("items")[0].serial_and_batch_bundle)[0]

dn = create_delivery_note(item_code="_Test Serialized Item With Series", serial_no=[serial_no])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import frappe
from frappe.tests import IntegrationTestCase, UnitTestCase
from frappe.tests.utils import change_settings

from erpnext.accounts.doctype.pos_closing_entry.test_pos_closing_entry import init_user_and_profile
from erpnext.accounts.doctype.pos_invoice.pos_invoice import make_sales_return
Expand All @@ -28,6 +27,11 @@ class UnitTestPosInvoiceMergeLog(UnitTestCase):


class TestPOSInvoiceMergeLog(IntegrationTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.enterClassContext(cls.change_settings("Selling Settings", validate_selling_price=0))

def test_consolidated_invoice_creation(self):
frappe.db.sql("delete from `tabPOS Invoice`")

Expand Down Expand Up @@ -411,7 +415,7 @@ def test_serial_no_case_1(self):
frappe.db.sql("delete from `tabPOS Invoice`")

try:
se = make_serialized_item()
se = make_serialized_item(self)
serial_no = get_serial_nos_from_bundle(se.get("items")[0].serial_and_batch_bundle)[0]

init_user_and_profile()
Expand Down
1 change: 1 addition & 0 deletions erpnext/accounts/doctype/pricing_rule/test_pricing_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class TestPricingRule(IntegrationTestCase):
def setUp(self):
delete_existing_pricing_rules()
setup_pricing_rule_data()
self.enterClassContext(self.change_settings("Selling Settings", validate_selling_price=0))

def tearDown(self):
delete_existing_pricing_rules()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@


class TestProcessStatementOfAccounts(AccountsTestMixin, IntegrationTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.enterClassContext(cls.change_settings("Selling Settings", validate_selling_price=0))

def setUp(self):
self.create_company()
self.create_customer()
Expand Down
42 changes: 14 additions & 28 deletions erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
class TestPurchaseInvoice(IntegrationTestCase, StockTestMixin):
@classmethod
def setUpClass(self):
super().setUpClass()
unlink_payment_on_cancel_of_invoice()
frappe.db.set_single_value("Buying Settings", "allow_multiple_items", 1)

Expand Down Expand Up @@ -115,7 +116,7 @@ def test_update_received_qty_in_material_request(self):

def test_gl_entries_without_perpetual_inventory(self):
frappe.db.set_value("Company", "_Test Company", "round_off_account", "Round Off - _TC")
pi = frappe.copy_doc(test_records[0])
pi = frappe.copy_doc(self.globalTestRecords["Purchase Invoice"][0])
self.assertTrue(not cint(erpnext.is_perpetual_inventory_enabled(pi.company)))
pi.insert()
pi.submit()
Expand Down Expand Up @@ -156,7 +157,7 @@ def test_gl_entries_with_perpetual_inventory(self):
self.check_gle_for_pi(pi.name)

def test_terms_added_after_save(self):
pi = frappe.copy_doc(test_records[1])
pi = frappe.copy_doc(self.globalTestRecords["Purchase Invoice"][1])
pi.insert()
self.assertTrue(pi.payment_schedule)
self.assertEqual(pi.payment_schedule[0].due_date, pi.due_date)
Expand Down Expand Up @@ -383,13 +384,13 @@ def test_purchase_invoice_with_exchange_rate_difference(self):
self.assertEqual(discrepancy_caused_by_exchange_rate_diff, amount)

def test_purchase_invoice_change_naming_series(self):
pi = frappe.copy_doc(test_records[1])
pi = frappe.copy_doc(self.globalTestRecords["Purchase Invoice"][1])
pi.insert()
pi.naming_series = "TEST-"

self.assertRaises(frappe.CannotChangeConstantError, pi.save)

pi = frappe.copy_doc(test_records[0])
pi = frappe.copy_doc(self.globalTestRecords["Purchase Invoice"][0])
pi.insert()
pi.load_from_db()

Expand Down Expand Up @@ -429,7 +430,7 @@ def test_gl_entries_for_non_stock_items_with_perpetual_inventory(self):
self.assertEqual(expected_values[i][2], gle.credit)

def test_purchase_invoice_calculation(self):
pi = frappe.copy_doc(test_records[0])
pi = frappe.copy_doc(self.globalTestRecords["Purchase Invoice"][0])
pi.insert()
pi.load_from_db()

Expand Down Expand Up @@ -465,15 +466,11 @@ def test_purchase_invoice_calculation(self):
"Accounts Settings", {"unlink_payment_on_cancellation_of_invoice": 1}
)
def test_purchase_invoice_with_advance(self):
from erpnext.accounts.doctype.journal_entry.test_journal_entry import (
test_records as jv_test_records,
)

jv = frappe.copy_doc(jv_test_records[1])
jv = frappe.copy_doc(self.globalTestRecords["Journal Entry"][1])
jv.insert()
jv.submit()

pi = frappe.copy_doc(test_records[0])
pi = frappe.copy_doc(self.globalTestRecords["Purchase Invoice"][0])
pi.disable_rounded_total = 1
pi.allocate_advances_automatically = 0
pi.append(
Expand Down Expand Up @@ -522,15 +519,11 @@ def test_purchase_invoice_with_advance(self):
"Accounts Settings", {"unlink_payment_on_cancellation_of_invoice": 1}
)
def test_invoice_with_advance_and_multi_payment_terms(self):
from erpnext.accounts.doctype.journal_entry.test_journal_entry import (
test_records as jv_test_records,
)

jv = frappe.copy_doc(jv_test_records[1])
jv = frappe.copy_doc(self.globalTestRecords["Journal Entry"][1])
jv.insert()
jv.submit()

pi = frappe.copy_doc(test_records[0])
pi = frappe.copy_doc(self.globalTestRecords["Purchase Invoice"][0])
pi.disable_rounded_total = 1
pi.allocate_advances_automatically = 0
pi.append(
Expand Down Expand Up @@ -945,16 +938,12 @@ def test_rejected_serial_no(self):
)

def test_outstanding_amount_after_advance_jv_cancelation(self):
from erpnext.accounts.doctype.journal_entry.test_journal_entry import (
test_records as jv_test_records,
)

jv = frappe.copy_doc(jv_test_records[1])
jv = frappe.copy_doc(self.globalTestRecords["Journal Entry"][1])
jv.accounts[0].is_advance = "Yes"
jv.insert()
jv.submit()

pi = frappe.copy_doc(test_records[0])
pi = frappe.copy_doc(self.globalTestRecords["Purchase Invoice"][0])
pi.append(
"advances",
{
Expand Down Expand Up @@ -1004,7 +993,7 @@ def test_outstanding_amount_after_advance_payment_entry_cancelation(self):
pe.insert()
pe.submit()

pi = frappe.copy_doc(test_records[0])
pi = frappe.copy_doc(self.globalTestRecords["Purchase Invoice"][0])
pi.is_pos = 0
pi.append(
"advances",
Expand Down Expand Up @@ -1040,7 +1029,7 @@ def test_purchase_invoice_with_shipping_rule(self):
shipping_rule_type="Buying", shipping_rule_name="Shipping Rule - Purchase Invoice Test"
)

pi = frappe.copy_doc(test_records[0])
pi = frappe.copy_doc(self.globalTestRecords["Purchase Invoice"][0])

pi.shipping_rule = shipping_rule.name
pi.insert()
Expand Down Expand Up @@ -2662,6 +2651,3 @@ def toggle_provisional_accounting_setting(**args):
company.enable_provisional_accounting_for_non_stock_items = args.enable or 0
company.default_provisional_account = args.provisional_account
company.save()


test_records = frappe.get_test_records("Purchase Invoice")
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@


class TestRepostAccountingLedger(AccountsTestMixin, IntegrationTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.enterClassContext(cls.change_settings("Selling Settings", validate_selling_price=0))

def setUp(self):
self.create_company()
self.create_customer()
Expand Down
Loading

0 comments on commit cc954d9

Please sign in to comment.