From 46bc78c60b7063a4e641193cfc3c4cbd65dc6cfe Mon Sep 17 00:00:00 2001 From: Okeyo Gichana Mayaka Date: Tue, 5 Dec 2023 15:31:05 +0300 Subject: [PATCH] Refactored the hard-coded accounts in the tests for the mpesa_b2c_payment and mpesa_b2c_payments_transactions doctype. Inverted the debit and credit accounts in the journal entry when a record is auto-created on a successful payment transaction. Added the remove navari_mpesa_b2c directory in the cleanup step of the CI/CD build pipeline. --- .github/workflows/ci.yml | 4 +- .../test_mpesa_b2c_payment.py | 70 +++++++++++-------- .../mpesa_b2c_payments_transactions.py | 4 +- .../test_mpesa_b2c_payments_transactions.py | 43 ++++++------ 4 files changed, 70 insertions(+), 51 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8dc8d66..d646f1b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -81,7 +81,9 @@ jobs: mariadb --host 127.0.0.1 --port 3306 -u root -proot -e "SET GLOBAL collation_server = 'utf8mb4_unicode_ci'" - name: Clean Up Previous Installation - run: rm -rf /home/runner/frappe-bench/apps/navari-mpesa-b2c + run: | + rm -rf /home/runner/frappe-bench/apps/navari-mpesa-b2c + rm -rf /home/runner/frappe-bench/apps/navari_mpesa_b2c - name: Install working-directory: /home/runner/frappe-bench diff --git a/navari_mpesa_b2c/mpesa_b2c/doctype/mpesa_b2c_payment/test_mpesa_b2c_payment.py b/navari_mpesa_b2c/mpesa_b2c/doctype/mpesa_b2c_payment/test_mpesa_b2c_payment.py index 697eb95..89dcc81 100644 --- a/navari_mpesa_b2c/mpesa_b2c/doctype/mpesa_b2c_payment/test_mpesa_b2c_payment.py +++ b/navari_mpesa_b2c/mpesa_b2c/doctype/mpesa_b2c_payment/test_mpesa_b2c_payment.py @@ -69,6 +69,20 @@ } +EXPENSE_ACCOUNT = frappe.db.sql( + """ + select name + from `tabAccount` + where name like 'Expense%'""" +)[-1][0] +INCOME_ACCOUNT = frappe.db.sql( + """ + select name + from `tabAccount` + where name like 'Income%'""" +)[-1][0] + + def create_mpesa_b2c_payment() -> None: """Create a valid b2c payment""" if frappe.flags.test_events_created: @@ -86,8 +100,8 @@ def create_mpesa_b2c_payment() -> None: "amount": 10, "occassion": "Testing", "party_type": "Supplier", - "account_paid_from": "Cash - NVR", - "account_paid_to": "Cash - NVR", + "account_paid_from": EXPENSE_ACCOUNT, + "account_paid_to": INCOME_ACCOUNT, } ).insert() @@ -116,8 +130,8 @@ def test_invalid_receiver_number(self) -> None: "amount": 10, "occassion": "Testing", "party_type": "Supplier", - "account_paid_from": "Cash - NVR", - "account_paid_to": "Debtors - NVR", + "account_paid_from": EXPENSE_ACCOUNT, + "account_paid_to": INCOME_ACCOUNT, } ).insert() @@ -131,8 +145,8 @@ def test_invalid_receiver_number(self) -> None: "amount": 10, "occassion": "Testing", "party_type": "Supplier", - "account_paid_from": "Cash - NVR", - "account_paid_to": "Debtors - NVR", + "account_paid_from": EXPENSE_ACCOUNT, + "account_paid_to": INCOME_ACCOUNT, } ).insert() @@ -146,8 +160,8 @@ def test_invalid_receiver_number(self) -> None: "amount": 10, "occassion": "Testing", "party_type": "Supplier", - "account_paid_from": "Cash - NVR", - "account_paid_to": "Debtors - NVR", + "account_paid_from": EXPENSE_ACCOUNT, + "account_paid_to": INCOME_ACCOUNT, } ).insert() @@ -161,8 +175,8 @@ def test_invalid_receiver_number(self) -> None: "amount": 10, "occassion": "Testing", "party_type": "Supplier", - "account_paid_from": "Cash - NVR", - "account_paid_to": "Debtors - NVR", + "account_paid_from": EXPENSE_ACCOUNT, + "account_paid_to": INCOME_ACCOUNT, } ).insert() @@ -204,8 +218,8 @@ def test_insufficient_amount(self) -> None: "amount": 9.99, "occassion": "Testing", "party_type": "Supplier", - "account_paid_from": "Cash - NVR", - "account_paid_to": "Debtors - NVR", + "account_paid_from": EXPENSE_ACCOUNT, + "account_paid_to": INCOME_ACCOUNT, } ).insert() @@ -223,8 +237,8 @@ def test_arbitrarily_large_amount(self) -> None: "amount": large_number, "occassion": "Testing", "party_type": "Supplier", - "account_paid_from": "Cash - NVR", - "account_paid_to": "Debtors - NVR", + "account_paid_from": EXPENSE_ACCOUNT, + "account_paid_to": INCOME_ACCOUNT, } ).insert() @@ -240,8 +254,8 @@ def test_valid_originator_conversation_id_length(self) -> None: "amount": 10, "occassion": "Testing", "party_type": "Supplier", - "account_paid_from": "Cash - NVR", - "account_paid_to": "Debtors - NVR", + "account_paid_from": EXPENSE_ACCOUNT, + "account_paid_to": INCOME_ACCOUNT, } ).insert() @@ -260,8 +274,8 @@ def test_invalid_errored_status_no_code_or_error_description(self) -> None: "amount": 10, "occassion": "Testing", "party_type": "Supplier", - "account_paid_from": "Cash - NVR", - "account_paid_to": "Debtors - NVR", + "account_paid_from": EXPENSE_ACCOUNT, + "account_paid_to": INCOME_ACCOUNT, } ).insert() @@ -279,8 +293,8 @@ def test_status_set_to_not_initiated_when_not_supplied(self) -> None: "amount": 10, "occassion": "Testing", "party_type": "Supplier", - "account_paid_from": "Cash - NVR", - "account_paid_to": "Debtors - NVR", + "account_paid_from": EXPENSE_ACCOUNT, + "account_paid_to": INCOME_ACCOUNT, } ).insert() @@ -298,8 +312,8 @@ def test_mismatch_in_command_id_and_party_type(self) -> None: "amount": 10, "occassion": "Testing", "party_type": "Supplier", - "account_paid_from": "Cash - NVR", - "account_paid_to": "Debtors - NVR", + "account_paid_from": EXPENSE_ACCOUNT, + "account_paid_to": INCOME_ACCOUNT, } ).insert() frappe.get_doc( @@ -311,8 +325,8 @@ def test_mismatch_in_command_id_and_party_type(self) -> None: "amount": 10, "occassion": "Testing", "party_type": "Employee", - "account_paid_from": "Cash - NVR", - "account_paid_to": "Debtors - NVR", + "account_paid_from": EXPENSE_ACCOUNT, + "account_paid_to": INCOME_ACCOUNT, } ).insert() @@ -432,8 +446,8 @@ def test_update_doctype_single_values(self) -> None: "MPesa B2C Payment", { "partyb": "254708993268", - "account_paid_from": "Cash - NVR", - "account_paid_to": "Cash - NVR", + "account_paid_from": EXPENSE_ACCOUNT, + "account_paid_to": INCOME_ACCOUNT, }, ["name", "occassion"], as_dict=True, @@ -450,8 +464,8 @@ def test_update_doctype_single_values(self) -> None: "MPesa B2C Payment", { "partyb": "254708993268", - "account_paid_from": "Cash - NVR", - "account_paid_to": "Cash - NVR", + "account_paid_from": EXPENSE_ACCOUNT, + "account_paid_to": INCOME_ACCOUNT, "occassion": new_value, }, ["name", "occassion"], diff --git a/navari_mpesa_b2c/mpesa_b2c/doctype/mpesa_b2c_payments_transactions/mpesa_b2c_payments_transactions.py b/navari_mpesa_b2c/mpesa_b2c/doctype/mpesa_b2c_payments_transactions/mpesa_b2c_payments_transactions.py index 8c8f217..909997e 100644 --- a/navari_mpesa_b2c/mpesa_b2c/doctype/mpesa_b2c_payments_transactions/mpesa_b2c_payments_transactions.py +++ b/navari_mpesa_b2c/mpesa_b2c/doctype/mpesa_b2c_payments_transactions/mpesa_b2c_payments_transactions.py @@ -77,14 +77,14 @@ def on_update(self) -> None: "accounts", { "account": self.account_paid_from, - "debit_in_account_currency": self.transaction_amount, + "credit_in_account_currency": self.transaction_amount, }, ) journal_entry.append( "accounts", { "account": self.account_paid_to, - "credit_in_account_currency": self.transaction_amount, + "debit_in_account_currency": self.transaction_amount, "party_type": self.fetched_b2c_payment.party_type, "party": self.fetched_b2c_payment.party, }, diff --git a/navari_mpesa_b2c/mpesa_b2c/doctype/mpesa_b2c_payments_transactions/test_mpesa_b2c_payments_transactions.py b/navari_mpesa_b2c/mpesa_b2c/doctype/mpesa_b2c_payments_transactions/test_mpesa_b2c_payments_transactions.py index 5e1e848..966ef15 100644 --- a/navari_mpesa_b2c/mpesa_b2c/doctype/mpesa_b2c_payments_transactions/test_mpesa_b2c_payments_transactions.py +++ b/navari_mpesa_b2c/mpesa_b2c/doctype/mpesa_b2c_payments_transactions/test_mpesa_b2c_payments_transactions.py @@ -13,6 +13,19 @@ ORIGINATOR_CONVERSATION_ID = str(uuid4()) ORIGINATOR_CONVERSATION_ID_2 = str(uuid4()) +EXPENSE_ACCOUNT = frappe.db.sql( + """ + select name + from `tabAccount` + where name like 'Expense%'""" +)[-1][0] +INCOME_ACCOUNT = frappe.db.sql( + """ + select name + from `tabAccount` + where name like 'Income%'""" +)[-1][0] + def create_b2c_payment_transaction() -> None: """Create a valid b2c payment""" @@ -21,16 +34,6 @@ def create_b2c_payment_transaction() -> None: frappe.set_user("Administrator") - available_cash_accounts = frappe.db.sql( - """ - SELECT name - FROM `tabAccount` - WHERE account_type = 'Cash' - and account_currency = 'KES' - """, - as_dict=True, - ) - doc = frappe.get_doc( { "doctype": "MPesa B2C Payment", @@ -42,8 +45,8 @@ def create_b2c_payment_transaction() -> None: "amount": 10, "occassion": "Testing", "party_type": "Employee", - "account_paid_from": available_cash_accounts[0].name, - "account_paid_to": available_cash_accounts[0].name, + "account_paid_from": EXPENSE_ACCOUNT, + "account_paid_to": INCOME_ACCOUNT, } ).insert() @@ -58,8 +61,8 @@ def create_b2c_payment_transaction() -> None: "amount": 10, "occassion": "Testing", "party_type": "Employee", - "account_paid_from": available_cash_accounts[0].name, - "account_paid_to": available_cash_accounts[0].name, + "account_paid_from": EXPENSE_ACCOUNT, + "account_paid_to": INCOME_ACCOUNT, } ).insert() @@ -67,7 +70,7 @@ def create_b2c_payment_transaction() -> None: { "doctype": "MPesa B2C Payments Transactions", "b2c_payment_name": doc.name, - "transaction_id": 951753654, + "transaction_id": random.randint(100000000, 100000000000), "transaction_amount": 10, "receiver_public_name": "Jane Doe", "recipient_is_registered_customer": "Y", @@ -75,8 +78,8 @@ def create_b2c_payment_transaction() -> None: "working_acct_avlbl_funds": 1000000, "utility_acct_avlbl_funds": 10000000, "transaction_completed_datetime": datetime.datetime.now(), - "account_paid_from": available_cash_accounts[0].name, - "account_paid_to": available_cash_accounts[0].name, + "account_paid_from": EXPENSE_ACCOUNT, + "account_paid_to": INCOME_ACCOUNT, } ).insert() @@ -105,7 +108,7 @@ def test_mismatch_in_amount(self) -> None: { "doctype": "MPesa B2C Payments Transactions", "b2c_payment_name": payment.name, - "transaction_id": random.randint(1000000, 100000000), + "transaction_id": random.randint(100000000, 100000000000), "transaction_amount": 9.9999, "receiver_public_name": "Jane Doe", "recipient_is_registered_customer": "Y", @@ -129,7 +132,7 @@ def test_mismatch_in_payment_status(self) -> None: { "doctype": "MPesa B2C Payments Transactions", "b2c_payment_name": payment.name, - "transaction_id": random.randint(1000000, 100000000), + "transaction_id": random.randint(100000000, 100000000000), "transaction_amount": 9.9999, "receiver_public_name": "Jane Doe", "recipient_is_registered_customer": "Y", @@ -147,7 +150,7 @@ def test_creating_transaction_for_non_existent_payment(self) -> None: { "doctype": "MPesa B2C Payments Transactions", "b2c_payment_name": "MPESA-B2C-0000", - "transaction_id": random.randint(1000000, 100000000), + "transaction_id": random.randint(100000000, 100000000000), "transaction_amount": 9.9999, "receiver_public_name": "Jane Doe", "recipient_is_registered_customer": "Y",