From 21b07385baa4855972637b07b1129cdd65196f94 Mon Sep 17 00:00:00 2001 From: Rucha Mahabal Date: Mon, 27 Dec 2021 16:57:36 +0530 Subject: [PATCH] fix: flaky HR tests (#29017) * fix(test): use root company in Expense Claim tests * fix(test): set Holiday List for Leave Allocation * fix(test): set Holiday List for company --- .../expense_claim/test_expense_claim.py | 24 +++++----- .../doctype/expense_claim/test_records.json | 1 - .../leave_allocation/test_leave_allocation.py | 45 ++++++++++++++----- 3 files changed, 48 insertions(+), 22 deletions(-) delete mode 100644 erpnext/hr/doctype/expense_claim/test_records.json diff --git a/erpnext/hr/doctype/expense_claim/test_expense_claim.py b/erpnext/hr/doctype/expense_claim/test_expense_claim.py index ec703614c8..2a079201b7 100644 --- a/erpnext/hr/doctype/expense_claim/test_expense_claim.py +++ b/erpnext/hr/doctype/expense_claim/test_expense_claim.py @@ -10,15 +10,17 @@ from erpnext.accounts.doctype.account.test_account import create_account from erpnext.hr.doctype.employee.test_employee import make_employee from erpnext.hr.doctype.expense_claim.expense_claim import make_bank_entry -test_records = frappe.get_test_records('Expense Claim') test_dependencies = ['Employee'] -company_name = '_Test Company 4' +company_name = '_Test Company 3' class TestExpenseClaim(unittest.TestCase): + def tearDown(self): + frappe.db.rollback() + def test_total_expense_claim_for_project(self): - frappe.db.sql("""delete from `tabTask` where project = "_Test Project 1" """) - frappe.db.sql("""delete from `tabProject` where name = "_Test Project 1" """) + frappe.db.sql("""delete from `tabTask`""") + frappe.db.sql("""delete from `tabProject`""") frappe.db.sql("update `tabExpense Claim` set project = '', task = ''") project = frappe.get_doc({ @@ -37,12 +39,12 @@ class TestExpenseClaim(unittest.TestCase): task_name = task.name payable_account = get_payable_account(company_name) - make_expense_claim(payable_account, 300, 200, company_name, "Travel Expenses - _TC4", project.name, task_name) + make_expense_claim(payable_account, 300, 200, company_name, "Travel Expenses - _TC3", project.name, task_name) self.assertEqual(frappe.db.get_value("Task", task_name, "total_expense_claim"), 200) self.assertEqual(frappe.db.get_value("Project", project.name, "total_expense_claim"), 200) - expense_claim2 = make_expense_claim(payable_account, 600, 500, company_name, "Travel Expenses - _TC4", project.name, task_name) + expense_claim2 = make_expense_claim(payable_account, 600, 500, company_name, "Travel Expenses - _TC3", project.name, task_name) self.assertEqual(frappe.db.get_value("Task", task_name, "total_expense_claim"), 700) self.assertEqual(frappe.db.get_value("Project", project.name, "total_expense_claim"), 700) @@ -54,7 +56,7 @@ class TestExpenseClaim(unittest.TestCase): def test_expense_claim_status(self): payable_account = get_payable_account(company_name) - expense_claim = make_expense_claim(payable_account, 300, 200, company_name, "Travel Expenses - _TC4") + expense_claim = make_expense_claim(payable_account, 300, 200, company_name, "Travel Expenses - _TC3") je_dict = make_bank_entry("Expense Claim", expense_claim.name) je = frappe.get_doc(je_dict) @@ -73,7 +75,7 @@ class TestExpenseClaim(unittest.TestCase): def test_expense_claim_gl_entry(self): payable_account = get_payable_account(company_name) taxes = generate_taxes() - expense_claim = make_expense_claim(payable_account, 300, 200, company_name, "Travel Expenses - _TC4", + expense_claim = make_expense_claim(payable_account, 300, 200, company_name, "Travel Expenses - _TC3", do_not_submit=True, taxes=taxes) expense_claim.submit() @@ -84,9 +86,9 @@ class TestExpenseClaim(unittest.TestCase): self.assertTrue(gl_entries) expected_values = dict((d[0], d) for d in [ - ['Output Tax CGST - _TC4',18.0, 0.0], + ['Output Tax CGST - _TC3',18.0, 0.0], [payable_account, 0.0, 218.0], - ["Travel Expenses - _TC4", 200.0, 0.0] + ["Travel Expenses - _TC3", 200.0, 0.0] ]) for gle in gl_entries: @@ -102,7 +104,7 @@ class TestExpenseClaim(unittest.TestCase): "payable_account": payable_account, "approval_status": "Rejected", "expenses": - [{ "expense_type": "Travel", "default_account": "Travel Expenses - _TC4", "amount": 300, "sanctioned_amount": 200 }] + [{"expense_type": "Travel", "default_account": "Travel Expenses - _TC3", "amount": 300, "sanctioned_amount": 200}] }) expense_claim.submit() diff --git a/erpnext/hr/doctype/expense_claim/test_records.json b/erpnext/hr/doctype/expense_claim/test_records.json deleted file mode 100644 index fe51488c70..0000000000 --- a/erpnext/hr/doctype/expense_claim/test_records.json +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/erpnext/hr/doctype/leave_allocation/test_leave_allocation.py b/erpnext/hr/doctype/leave_allocation/test_leave_allocation.py index 6dbe2eca32..1fe91399a0 100644 --- a/erpnext/hr/doctype/leave_allocation/test_leave_allocation.py +++ b/erpnext/hr/doctype/leave_allocation/test_leave_allocation.py @@ -12,15 +12,11 @@ from erpnext.hr.doctype.leave_type.test_leave_type import create_leave_type class TestLeaveAllocation(unittest.TestCase): @classmethod def setUpClass(cls): - from erpnext.payroll.doctype.salary_slip.test_salary_slip import make_holiday_list - frappe.db.sql("delete from `tabLeave Period`") + emp_id = make_employee("test_emp_leave_allocation@salary.com") cls.employee = frappe.get_doc("Employee", emp_id) - make_holiday_list() - frappe.db.set_value("Company", erpnext.get_default_company(), "default_holiday_list", "Salary Slip Test Holiday List") - def tearDown(self): frappe.db.rollback() @@ -90,6 +86,8 @@ class TestLeaveAllocation(unittest.TestCase): # initial leave allocation = 15 leave_allocation = create_leave_allocation( + employee=self.employee.name, + employee_name=self.employee.employee_name, leave_type="_Test_CF_leave", from_date=add_months(nowdate(), -12), to_date=add_months(nowdate(), -1), @@ -99,6 +97,8 @@ class TestLeaveAllocation(unittest.TestCase): # carry forwarded leaves considering maximum_carry_forwarded_leaves # new_leaves = 15, carry_forwarded = 10 leave_allocation_1 = create_leave_allocation( + employee=self.employee.name, + employee_name=self.employee.employee_name, leave_type="_Test_CF_leave", carry_forward=1) leave_allocation_1.submit() @@ -110,6 +110,8 @@ class TestLeaveAllocation(unittest.TestCase): # carry forwarded leaves considering max_leave_allowed # max_leave_allowed = 30, new_leaves = 25, carry_forwarded = 5 leave_allocation_2 = create_leave_allocation( + employee=self.employee.name, + employee_name=self.employee.employee_name, leave_type="_Test_CF_leave", carry_forward=1, new_leaves_allocated=25) @@ -126,6 +128,8 @@ class TestLeaveAllocation(unittest.TestCase): # initial leave allocation leave_allocation = create_leave_allocation( + employee=self.employee.name, + employee_name=self.employee.employee_name, leave_type="_Test_CF_leave_expiry", from_date=add_months(nowdate(), -24), to_date=add_months(nowdate(), -12), @@ -133,6 +137,8 @@ class TestLeaveAllocation(unittest.TestCase): leave_allocation.submit() leave_allocation = create_leave_allocation( + employee=self.employee.name, + employee_name=self.employee.employee_name, leave_type="_Test_CF_leave_expiry", from_date=add_days(nowdate(), -90), to_date=add_days(nowdate(), 100), @@ -144,6 +150,8 @@ class TestLeaveAllocation(unittest.TestCase): # leave allocation with carry forward of only new leaves allocated leave_allocation_1 = create_leave_allocation( + employee=self.employee.name, + employee_name=self.employee.employee_name, leave_type="_Test_CF_leave_expiry", carry_forward=1, from_date=add_months(nowdate(), 6), @@ -153,7 +161,10 @@ class TestLeaveAllocation(unittest.TestCase): self.assertEqual(leave_allocation_1.unused_leaves, leave_allocation.new_leaves_allocated) def test_creation_of_leave_ledger_entry_on_submit(self): - leave_allocation = create_leave_allocation() + leave_allocation = create_leave_allocation( + employee=self.employee.name, + employee_name=self.employee.employee_name + ) leave_allocation.submit() leave_ledger_entry = frappe.get_all('Leave Ledger Entry', fields='*', filters=dict(transaction_name=leave_allocation.name)) @@ -168,7 +179,10 @@ class TestLeaveAllocation(unittest.TestCase): self.assertFalse(frappe.db.exists("Leave Ledger Entry", {'transaction_name':leave_allocation.name})) def test_leave_addition_after_submit(self): - leave_allocation = create_leave_allocation() + leave_allocation = create_leave_allocation( + employee=self.employee.name, + employee_name=self.employee.employee_name + ) leave_allocation.submit() self.assertTrue(leave_allocation.total_leaves_allocated, 15) leave_allocation.new_leaves_allocated = 40 @@ -176,7 +190,10 @@ class TestLeaveAllocation(unittest.TestCase): self.assertTrue(leave_allocation.total_leaves_allocated, 40) def test_leave_subtraction_after_submit(self): - leave_allocation = create_leave_allocation() + leave_allocation = create_leave_allocation( + employee=self.employee.name, + employee_name=self.employee.employee_name + ) leave_allocation.submit() self.assertTrue(leave_allocation.total_leaves_allocated, 15) leave_allocation.new_leaves_allocated = 10 @@ -184,7 +201,15 @@ class TestLeaveAllocation(unittest.TestCase): self.assertTrue(leave_allocation.total_leaves_allocated, 10) def test_validation_against_leave_application_after_submit(self): - leave_allocation = create_leave_allocation() + from erpnext.payroll.doctype.salary_slip.test_salary_slip import make_holiday_list + + make_holiday_list() + frappe.db.set_value("Company", self.employee.company, "default_holiday_list", "Salary Slip Test Holiday List") + + leave_allocation = create_leave_allocation( + employee=self.employee.name, + employee_name=self.employee.employee_name + ) leave_allocation.submit() self.assertTrue(leave_allocation.total_leaves_allocated, 15) @@ -194,7 +219,7 @@ class TestLeaveAllocation(unittest.TestCase): "leave_type": "_Test Leave Type", "from_date": add_months(nowdate(), 2), "to_date": add_months(add_days(nowdate(), 10), 2), - "company": erpnext.get_default_company() or "_Test Company", + "company": self.employee.company, "docstatus": 1, "status": "Approved", "leave_approver": 'test@example.com'