Skip to content

Commit

Permalink
Refactored the hard-coded accounts in the tests for the mpesa_b2c_pay…
Browse files Browse the repository at this point in the history
…ment 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.
  • Loading branch information
GichanaMayaka committed Dec 5, 2023
1 parent db5521d commit 46bc78c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 51 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()

Expand Down Expand Up @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand Down Expand Up @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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(
Expand All @@ -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()

Expand Down Expand Up @@ -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,
Expand All @@ -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"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand All @@ -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",
Expand All @@ -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()

Expand All @@ -58,25 +61,25 @@ 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()

frappe.get_doc(
{
"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",
"charges_paid_acct_avlbl_funds": 100,
"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()

Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 46bc78c

Please sign in to comment.