Skip to content

Commit 7bb3214

Browse files
committed
refactor: used enums instead of passing actual values
Signed-off-by: rafaeljohn9 <rafaeljohb@gmail.com>
1 parent d597ef4 commit 7bb3214

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

tests/integration/mpesa_ratiba/test_mpesa_ratiba_e2e.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@
1010
from mpesa_sdk.auth import TokenManager
1111
from mpesa_sdk.http_client import MpesaHttpClient
1212

13-
from mpesa_sdk.mpesa_ratiba import MpesaRatiba, StandingOrderRequest
13+
from mpesa_sdk.mpesa_ratiba import (
14+
MpesaRatiba,
15+
StandingOrderRequest,
16+
FrequencyEnum,
17+
TransactionTypeEnum,
18+
ReceiverPartyIdentifierTypeEnum,
19+
)
1420

1521
load_dotenv()
1622
pytestmark = pytest.mark.live
@@ -37,18 +43,14 @@ def test_mpesa_ratiba_e2e(mpesa_ratiba_service):
3743
start_date = os.getenv("MPESA_RATIBA_START_DATE", "20240905")
3844
end_date = os.getenv("MPESA_RATIBA_END_DATE", "20250905")
3945
business_short_code = os.getenv("MPESA_RATIBA_BUSINESS_SHORT_CODE", "174379")
40-
transaction_type = os.getenv(
41-
"MPESA_RATIBA_TRANSACTION_TYPE", "Standing Order Customer Pay Bill"
42-
)
43-
receiver_party_identifier_type = os.getenv(
44-
"MPESA_RATIBA_RECEIVER_PARTY_IDENTIFIER_TYPE", "4"
45-
)
46+
transaction_type = TransactionTypeEnum.STANDING_ORDER_CUSTOMER_PAY_BILL
47+
receiver_party_identifier_type = ReceiverPartyIdentifierTypeEnum.BUSINESS_SHORT_CODE
4648
amount = os.getenv("MPESA_RATIBA_AMOUNT", "4500")
4749
party_a = os.getenv("MPESA_RATIBA_PARTY_A", "254708374149")
4850
callback_url = os.getenv("MPESA_RATIBA_CALLBACK_URL", "https://mydomain.com/pat")
4951
account_reference = os.getenv("MPESA_RATIBA_ACCOUNT_REFERENCE", "Test")
5052
transaction_desc = os.getenv("MPESA_RATIBA_TRANSACTION_DESC", "BikeRepayment")
51-
frequency = os.getenv("MPESA_RATIBA_FREQUENCY", "2")
53+
frequency = FrequencyEnum.MONTHLY
5254

5355
request = StandingOrderRequest(
5456
StandingOrderName=standing_order_name,

tests/unit/mpesa_ratiba/test_mpesa_ratiba.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
from mpesa_sdk.mpesa_ratiba import (
1313
MpesaRatiba,
14+
FrequencyEnum,
15+
TransactionTypeEnum,
16+
ReceiverPartyIdentifierTypeEnum,
1417
StandingOrderRequest,
1518
StandingOrderResponse,
1619
StandingOrderCallback,
@@ -45,14 +48,14 @@ def valid_standing_order_request():
4548
StartDate="20240905",
4649
EndDate="20250905",
4750
BusinessShortCode="174379",
48-
TransactionType="Standing Order Customer Pay Bill",
49-
ReceiverPartyIdentifierType="4",
51+
TransactionType=TransactionTypeEnum.STANDING_ORDER_CUSTOMER_PAY_BILL,
52+
ReceiverPartyIdentifierType=ReceiverPartyIdentifierTypeEnum.MERCHANT_TILL,
5053
Amount="4500",
5154
PartyA="254708374149",
5255
CallBackURL="https://mydomain.com/pat",
5356
AccountReference="Test",
5457
TransactionDesc="Electric Bike",
55-
Frequency="2",
58+
Frequency=FrequencyEnum.DAILY,
5659
)
5760

5861

@@ -172,14 +175,14 @@ def test_standing_order_request_invalid_date_format():
172175
StartDate="202423305", # Invalid date
173176
EndDate="20250905",
174177
BusinessShortCode="174379",
175-
TransactionType="Standing Order Customer Pay Bill",
176-
ReceiverPartyIdentifierType="4",
178+
TransactionType=TransactionTypeEnum.STANDING_ORDER_CUSTOMER_PAY_BILL,
179+
ReceiverPartyIdentifierType=ReceiverPartyIdentifierTypeEnum.MERCHANT_TILL,
177180
Amount="4500",
178181
PartyA="254708374149",
179182
CallBackURL="https://mydomain.com/pat",
180183
AccountReference="Test",
181184
TransactionDesc="Electric Bike",
182-
Frequency="2",
185+
Frequency=FrequencyEnum.DAILY,
183186
)
184187
assert "Date must be in 'yyyymmdd' format" in str(excinfo.value)
185188

@@ -192,14 +195,14 @@ def test_standing_order_request_invalid_date_value():
192195
StartDate="20241305", # Invalid month
193196
EndDate="20250905",
194197
BusinessShortCode="174379",
195-
TransactionType="Standing Order Customer Pay Bill",
196-
ReceiverPartyIdentifierType="4",
198+
TransactionType=TransactionTypeEnum.STANDING_ORDER_CUSTOMER_PAY_BILL,
199+
ReceiverPartyIdentifierType=ReceiverPartyIdentifierTypeEnum.MERCHANT_TILL,
197200
Amount="4500",
198201
PartyA="254708374149",
199202
CallBackURL="https://mydomain.com/pat",
200203
AccountReference="Test",
201204
TransactionDesc="Electric Bike",
202-
Frequency="2",
205+
Frequency=FrequencyEnum.WEEKLY,
203206
)
204207
assert "Date must be in 'yyyymmdd' format" in str(excinfo.value)
205208

@@ -211,14 +214,14 @@ def test_standing_order_request_other_date_value():
211214
StartDate="2024-12-05", # With separators
212215
EndDate="20250905",
213216
BusinessShortCode="174379",
214-
TransactionType="Standing Order Customer Pay Bill",
215-
ReceiverPartyIdentifierType="4",
217+
TransactionType=TransactionTypeEnum.STANDING_ORDER_CUSTOMER_PAY_BILL,
218+
ReceiverPartyIdentifierType=ReceiverPartyIdentifierTypeEnum.MERCHANT_TILL,
216219
Amount="4500",
217220
PartyA="254708374149",
218221
CallBackURL="https://mydomain.com/pat",
219222
AccountReference="Test",
220223
TransactionDesc="Electric Bike",
221-
Frequency="2",
224+
Frequency=FrequencyEnum.MONTHLY,
222225
)
223226
assert request.StartDate == "20241205" # Should normalize to yyyymmdd format
224227

@@ -231,13 +234,13 @@ def test_invalid_phone_number():
231234
StartDate="20240905",
232235
EndDate="20250905",
233236
BusinessShortCode="174379",
234-
TransactionType="Standing Order Customer Pay Bill",
235-
ReceiverPartyIdentifierType="4",
237+
TransactionType=TransactionTypeEnum.STANDING_ORDER_CUSTOMER_PAY_BILL,
238+
ReceiverPartyIdentifierType=ReceiverPartyIdentifierTypeEnum.MERCHANT_TILL,
236239
Amount="4500",
237240
PartyA="123456789", # Invalid phone number
238241
CallBackURL="https://mydomain.com/pat",
239242
AccountReference="Test",
240243
TransactionDesc="Electric Bike",
241-
Frequency="2",
244+
Frequency=FrequencyEnum.MONTHLY,
242245
)
243246
assert "Invalid PartyA phone number" in str(excinfo.value)

0 commit comments

Comments
 (0)