Skip to content

Commit 20253f5

Browse files
committed
tests: Add comprehensive tests for various endpoints and utility functions
- Implement tests for creating plans with all optional parameters. - Add tests for listing plans with filter parameters. - Enhance tests for updating plans with optional parameters. - Introduce tests for creating products with optional parameters. - Add tests for listing products with date parameters. - Implement tests for creating refunds with all optional parameters. - Enhance tests for listing refunds with date parameters and invalid parameters. - Add tests for creating subaccounts with all optional parameters. - Implement tests for listing subaccounts with date parameters. - Enhance tests for creating subscriptions with optional parameters. - Add tests for listing subscriptions with all parameters. - Implement tests for creating transfer recipients with all optional parameters. - Enhance tests for listing transfer recipients with date parameters. - Add tests for creating virtual terminals with all optional parameters. - Implement tests for listing virtual terminals with all parameters. - Add tests for validating email format and amount in utility functions. - Introduce tests for charge authorization validation.
1 parent a8d3a7b commit 20253f5

33 files changed

Lines changed: 2027 additions & 217 deletions

example.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
if not secret_key:
99
raise ValueError("PAYSTACK_SECRET_KEY environment variable not set.")
1010

11-
print("PAYSTACK_SECRET_KEY is set. Great job, Olujay!")
11+
elif secret_key:
12+
print("PAYSTACK_SECRET_KEY is set. Great job, Olujay!")
13+
14+
print(secret_key)
15+
1216

1317
client = PaystackClient(secret_key=secret_key)
1418

@@ -21,6 +25,7 @@
2125

2226
print("Transaction initialized successfully:")
2327
print(data)
28+
print(meta)
2429

2530
except APIError as e:
2631
print(f"An API error occurred: {e.message}")

paystack_client/core.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,19 @@ def request(
109109
)
110110
else:
111111
# Use the factory function to create appropriate exception
112-
raise create_error_from_response(
113-
response=resp_json,
114-
status_code=response.status_code,
115-
request_id=request_id,
116-
)
112+
# For RateLimitError, pass the original response object to access headers
113+
if response.status_code == 429:
114+
raise create_error_from_response(
115+
response=response, # Pass the original response object
116+
status_code=response.status_code,
117+
request_id=request_id,
118+
)
119+
else:
120+
raise create_error_from_response(
121+
response=resp_json,
122+
status_code=response.status_code,
123+
request_id=request_id,
124+
)
117125
except requests.exceptions.ConnectTimeout:
118126
raise NetworkError("Connection timed out - check your internet connection")
119127
except requests.exceptions.ReadTimeout:

paystack_client/endpoints/direct_debit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ def list_mandate_authorizations(
5454
if status:
5555
params["status"] = status
5656
if per_page:
57-
params["per_page"] = per_page
57+
params["perPage"] = per_page
5858

5959
return self.request("GET", "directdebit/mandate-authorizations", params=params)

paystack_client/endpoints/disputes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def list_disputes(
5757
if status:
5858
payload["status"] = status
5959

60-
return self.request("GET", "dispute", json_data=payload)
60+
return self.request("GET", "dispute", params=payload)
6161

6262
def fetch_dispute(self, dispute_id: str) -> Tuple[Dict[str, Any], Dict[str, Any]]:
6363
"""
@@ -240,4 +240,4 @@ def export_disputes(
240240
if status:
241241
payload["status"] = status
242242

243-
return self.request("GET", "dispute/export", json_data=payload)
243+
return self.request("GET", "dispute/export", params=payload)

paystack_client/endpoints/refunds.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def list_refunds(
9898
raise APIError("page must be greater than 0")
9999
payload["page"] = page
100100

101-
return self.request("GET", "refund", json_data=payload)
101+
return self.request("GET", "refund", params=payload)
102102

103103
def fetch(
104104
self, refund_id: Union[str, int]

paystack_client/endpoints/transactions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def initialize(
6363
payload["plan"] = plan
6464
if invoice_limit is not None:
6565
payload["invoice_limit"] = invoice_limit
66-
if metadata:
66+
if metadata is not None:
6767
# Convert metadata dict to JSON string as per API requirements
6868
import json
6969

paystack_client/utils/validators.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Validation helpers for Paystack API calls."""
22

33
from typing import Union
4-
from ..exceptions import APIError
4+
from ..exceptions import APIError, ValidationError
55
from .helpers import validate_email
66

77

@@ -17,10 +17,10 @@ def _validate_amount_and_email(email: str, amount: Union[int, str]):
1717
"""
1818
if not email:
1919
raise ValidationError("Email is required")
20-
if not amount:
20+
if amount is None:
2121
raise ValidationError("Amount is required")
22-
if not validate_email(email):
23-
raise ValidationError("Invalid email format")
22+
23+
validate_email(email)
2424

2525
try:
2626
amount_int = int(amount)

tests/test_client.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pytest
2+
from paystack_client import PaystackClient
3+
4+
def test_paystack_client_initialization():
5+
client = PaystackClient(secret_key="sk_test_abcdefghijklmnopqrstuvwxyz1234567890")
6+
assert client.base_url == "https://api.paystack.co/"
7+
assert hasattr(client, "transactions")
8+
assert hasattr(client, "customers")
9+
assert hasattr(client, "charge")
10+
assert hasattr(client, "plans")
11+
assert hasattr(client, "products")
12+
assert hasattr(client, "refunds")
13+
assert hasattr(client, "settlements")
14+
assert hasattr(client, "subaccounts")
15+
assert hasattr(client, "subscriptions")
16+
assert hasattr(client, "transfers")
17+
assert hasattr(client, "transfers_control")
18+
assert hasattr(client, "transfer_recipients")
19+
assert hasattr(client, "verification")
20+
assert hasattr(client, "disputes")
21+
assert hasattr(client, "payment_pages")
22+
assert hasattr(client, "payment_requests")
23+
assert hasattr(client, "bulk_charges")
24+
assert hasattr(client, "dedicated_virtual_accounts")
25+
assert hasattr(client, "direct_debit")
26+
assert hasattr(client, "apple_pay")
27+
assert hasattr(client, "terminal")
28+
assert hasattr(client, "virtual_terminal")
29+
assert hasattr(client, "transaction_splits")
30+
assert hasattr(client, "integration")
31+
assert hasattr(client, "miscellaneous")
32+
33+
# Access the integration attribute to ensure it's covered
34+
assert client.integration is not None
35+
36+
def test_paystack_client_repr():
37+
client = PaystackClient(secret_key="sk_test_abcdefghijklmnopqrstuvwxyz1234567890")
38+
assert repr(client) == "PaystackClient(base_url='https://api.paystack.co/')"

0 commit comments

Comments
 (0)