From 7c4d2e81dc05f447498c2c133a882dd6067c084d Mon Sep 17 00:00:00 2001 From: Ben Napper Date: Tue, 31 Dec 2024 17:37:26 +1100 Subject: [PATCH] Improve typing --- payway/client.py | 51 +++++++----- payway/customers.py | 4 +- payway/exceptions.py | 12 +-- payway/model.py | 156 ++++++++++++++++++------------------- payway/test_utils.py | 3 +- pyproject.toml | 2 +- tests/test_client.py | 31 ++++---- tests/test_customers.py | 17 ++-- tests/test_transactions.py | 5 +- 9 files changed, 145 insertions(+), 136 deletions(-) diff --git a/payway/client.py b/payway/client.py index 0fab6e8..39038f0 100644 --- a/payway/client.py +++ b/payway/client.py @@ -2,7 +2,7 @@ import json from logging import getLogger -from typing import NoReturn +from typing import Any import requests @@ -46,7 +46,7 @@ def __init__( bank_account_id: str, secret_api_key: str, publishable_api_key: str, - ) -> NoReturn: + ) -> None: """ :param merchant_id : str = PayWay Merchant ID :param bank_account_id : str = PayWay Bank Account ID @@ -66,8 +66,7 @@ def __init__( session = requests.Session() session.auth = (self.secret_api_key, "") - headers = {"content-type": "application/x-www-form-urlencoded"} - session.headers = headers + session.headers = {"content-type": "application/x-www-form-urlencoded"} self.session = session session_no_headers = requests.Session() session_no_headers.auth = session.auth @@ -79,7 +78,7 @@ def _validate_credentials( bank_account_id: str, secret_api_key: str, publishable_api_key: str, - ) -> NoReturn: + ) -> None: if not merchant_id or not bank_account_id or not secret_api_key or not publishable_api_key: if not secret_api_key or not publishable_api_key: logger.error("PayWay API keys not found") @@ -99,7 +98,7 @@ def get_request(self, endpoint: str) -> requests.Response: return requests.get(url=endpoint, auth=(self.secret_api_key, ""), timeout=30) def post_request( - self, endpoint: str, data: dict, auth: tuple | None = None, idempotency_key: str | None = None + self, endpoint: str, data: dict[str, Any], auth: tuple[str, str] | None = None, idempotency_key: str | None = None ) -> requests.Response: """ Supply an idempotency_key to avoid duplicate POSTs @@ -112,7 +111,7 @@ def post_request( headers["Idempotency-Key"] = idempotency_key return requests.post(url=endpoint, auth=auth, data=data, headers=headers, timeout=30) - def put_request(self, endpoint: str, data: dict) -> requests.Response: + def put_request(self, endpoint: str, data: dict[str, Any]) -> requests.Response: return requests.put( url=endpoint, auth=(self.secret_api_key, ""), @@ -123,7 +122,7 @@ def put_request(self, endpoint: str, data: dict) -> requests.Response: def create_token( self, payway_obj: BankAccount | PayWayCard, payment_method: str, idempotency_key: str | None = None - ) -> tuple[TokenResponse, list]: + ) -> tuple[TokenResponse | None, list[PaymentError] | None]: """ Creates a single use token for a Customer's payment setup (credit card or bank account) :param payway_obj: object: one of model.PayWayCard or model.BankAccount object @@ -160,14 +159,18 @@ def create_token( token_response = TokenResponse().from_dict(response.json()) return token_response, errors - def create_card_token(self, card: PayWayCard, idempotency_key: str | None = None) -> tuple[TokenResponse, list]: + def create_card_token( + self, card: PayWayCard, idempotency_key: str | None = None + ) -> tuple[TokenResponse | None, list[PaymentError] | None]: """ :param card: PayWayCard object represents a customer's credit card details :param idempotency_key: str: unique value to avoid duplicate POSTs """ return self.create_token(card, "card", idempotency_key=idempotency_key) - def create_bank_account_token(self, bank_account: BankAccount, idempotency_key: str | None = None) -> str: + def create_bank_account_token( + self, bank_account: BankAccount, idempotency_key: str | None = None + ) -> tuple[TokenResponse | None, list[PaymentError] | None]: """ :param bank_account: BankAccount object represents a customer's bank account :param idempotency_key: str: unique value to avoid duplicate POSTs @@ -179,7 +182,9 @@ def create_bank_account_token(self, bank_account: BankAccount, idempotency_key: idempotency_key=idempotency_key, ) - def create_customer(self, customer: PayWayCustomer, idempotency_key: str | None = None) -> tuple[PayWayCustomer | None, list]: + def create_customer( + self, customer: PayWayCustomer, idempotency_key: str | None = None + ) -> tuple[PayWayCustomer | None, list[PaymentError] | None]: """ Create a customer in PayWay system @@ -217,7 +222,9 @@ def create_customer(self, customer: PayWayCustomer, idempotency_key: str | None customer = PayWayCustomer().from_dict(response.json()) return customer, errors - def process_payment(self, payment: PayWayPayment, idempotency_key: str | None = None) -> tuple[PayWayTransaction, list]: + def process_payment( + self, payment: PayWayPayment, idempotency_key: str | None = None + ) -> tuple[PayWayTransaction | None, list[PaymentError] | None]: """ Process an individual payment against a Customer with active Recurring Billing setup. :param payment: PayWayPayment object (see model.PayWayPayment) @@ -234,7 +241,7 @@ def process_payment(self, payment: PayWayPayment, idempotency_key: str | None = transaction = PayWayTransaction.from_dict(response.json()) return transaction, errors - def _validate_response(self, response: requests.Response) -> list | None: + def _validate_response(self, response: requests.Response) -> list[PaymentError] | None: """ Validates all responses from PayWay to catch documented PayWay errors. :param response: requests response object @@ -254,7 +261,7 @@ def _validate_response(self, response: requests.Response) -> list | None: 503, ]: http_error_msg = f"{response.status_code} Client Error: {response.reason} for url: {response.url}" - raise PaywayError(code=response.status_code, message=http_error_msg) + raise PaywayError(code=str(response.status_code), message=http_error_msg) if response.status_code in [404, 422]: # Documented PayWay errors in JSON # parse error message @@ -265,17 +272,17 @@ def _validate_response(self, response: requests.Response) -> list | None: errors = response.json() except json.JSONDecodeError: raise PaywayError( - code=response.status_code, + code=str(response.status_code), message="Internal server error", ) # Documented PayWay server errors in JSON payway_error = ServerError().from_dict(errors) message = payway_error.to_message() - raise PaywayError(code=response.status_code, message=message) + raise PaywayError(code=str(response.status_code), message=message) return None - def get_transaction(self, transaction_id: int) -> tuple[PayWayTransaction, list]: + def get_transaction(self, transaction_id: int) -> tuple[PayWayTransaction | None, list[PaymentError] | None]: """ Lookup and return a transaction if found in PayWay :param transaction_id: str A PayWay transaction ID @@ -288,7 +295,9 @@ def get_transaction(self, transaction_id: int) -> tuple[PayWayTransaction, list] transaction = PayWayTransaction.from_dict(response.json()) return transaction, errors - def void_transaction(self, transaction_id: int, idempotency_key: str | None = None) -> tuple[PayWayTransaction, list]: + def void_transaction( + self, transaction_id: int, idempotency_key: str | None = None + ) -> tuple[PayWayTransaction | None, list[PaymentError] | None]: """ Void a transaction in PayWay :param transaction_id: str A PayWay transaction ID @@ -309,7 +318,7 @@ def refund_transaction( order_id: str | None = None, ip_address: str | None = None, idempotency_key: str | None = None, - ) -> tuple[PayWayTransaction, list]: + ) -> tuple[PayWayTransaction | None, list[PaymentError] | None]: """ Refund a transaction in PayWay :param transaction_id: str A PayWay transaction ID @@ -334,7 +343,7 @@ def refund_transaction( transaction = PayWayTransaction.from_dict(response.json()) return transaction, errors - def get_customer(self, customer_id: str) -> tuple[PayWayCustomer, list]: + def get_customer(self, customer_id: str) -> tuple[PayWayCustomer | None, list[PaymentError] | None]: """ Returns a PayWay Customer's Payment Setup, [Payment] Schedule, Contact Details, Custom Fields and Notes :param customer_id str PayWay customer ID in PayWay system @@ -347,7 +356,7 @@ def get_customer(self, customer_id: str) -> tuple[PayWayCustomer, list]: customer = PayWayCustomer.from_dict(response.json()) return customer, errors - def update_payment_setup(self, token: str, customer_id: str) -> tuple[PaymentSetup, str]: + def update_payment_setup(self, token: str, customer_id: str) -> tuple[PaymentSetup | None, list[PaymentError] | None]: """ Updates the Customer's Payment Setup with a new Credit Card or Bank Account. :param token: PayWay credit card or bank account token diff --git a/payway/customers.py b/payway/customers.py index d36a584..f5f823f 100644 --- a/payway/customers.py +++ b/payway/customers.py @@ -1,5 +1,7 @@ from __future__ import annotations +from typing import Any + import requests from payway.conf import CUSTOMER_URL @@ -79,7 +81,7 @@ def start_all_payments(self, customer_number: int) -> requests.Response: @json_list("update_contact_details") def update_contact_details( - self, customer_number: int, customer: PayWayCustomer | None = None, **options: dict + self, customer_number: int, customer: PayWayCustomer | None = None, **options: dict[str, Any] ) -> requests.Response: """ param: customer_number: PayWay customer number diff --git a/payway/exceptions.py b/payway/exceptions.py index 440b2a2..fbb92b4 100644 --- a/payway/exceptions.py +++ b/payway/exceptions.py @@ -1,13 +1,13 @@ from __future__ import annotations -from typing import NoReturn +from typing import Any class PaywayError(Exception): - _code: str = None - _message: str = None + _code: str = "" + _message: str = "" - def __init__(self, code: str, message: str, *args: dict, **kwargs: dict) -> NoReturn: + def __init__(self, code: str, message: str, *args: dict[str, Any], **kwargs: dict[str, Any]) -> None: """ code : str = PayWay API response/error code message : str = appropriate message @@ -16,7 +16,7 @@ def __init__(self, code: str, message: str, *args: dict, **kwargs: dict) -> NoRe super().__init__(*args, **kwargs) self._code = code - self._message = f"{code}: {message}".encode() + self._message = f"{code}: {message}" def __str__(self) -> str: - return self.message + return self._message diff --git a/payway/model.py b/payway/model.py index c619cc0..06bc67e 100644 --- a/payway/model.py +++ b/payway/model.py @@ -48,7 +48,7 @@ def to_dict(self) -> dict[str, Any]: } @staticmethod - def from_dict(payway_card: dict) -> PayWayCard: + def from_dict(payway_card: dict[str, Any]) -> PayWayCard: card = PayWayCard() if payway_card.get("maskedCardNumber"): card.card_number = payway_card.get("maskedCardNumber") @@ -124,14 +124,14 @@ def to_dict(self) -> dict[str, Any]: return customer @staticmethod - def from_dict(response: dict) -> PayWayCustomer: + def from_dict(response: dict[str, Any]) -> PayWayCustomer: """ Parse PayWay Customer response data :param response: dict PayWay response dictionary :return: """ customer = PayWayCustomer() - contact = response.get("contact") + contact = response.get("contact", {}) customer.customer_name = contact.get("customerName") customer.email_address = contact.get("emailAddress") customer.send_email_receipts = contact.get("sendEmailReceipts") @@ -146,11 +146,11 @@ def from_dict(response: dict) -> PayWayCustomer: if response.get("paymentSetup") is not None: customer.payment_setup = PaymentSetup().from_dict( - response.get("paymentSetup"), + response.get("paymentSetup", {}), ) if response.get("customFields") is not None: - custom_fields = response.get("customFields") + custom_fields = response.get("customFields", {}) for k, v in custom_fields.items(): setattr(customer, k, v) @@ -161,17 +161,17 @@ def from_dict(response: dict) -> PayWayCustomer: class PaymentError: - field_name: str = None - message: str = None - field_value: str = None + field_name: str | None = None + message: str | None = None + field_value: str | None = None @staticmethod - def from_dict(payway_response: dict) -> list: + def from_dict(payway_response: dict[str, Any]) -> list[PaymentError]: """ Returns a list of errors from PayWay :param: payway_response: dict PayWay response dictionary """ - errors = payway_response.get("data") + errors = payway_response.get("data", []) payment_errors = [] for error in errors: payway_error = PaymentError() @@ -200,17 +200,17 @@ def list_to_message(payway_errors: list[PaymentError]) -> str: class ServerError: - error_number: int = None - trace_code: str = None + error_number: int | None = None + trace_code: str | None = None @staticmethod - def from_dict(response: dict) -> ServerError: + def from_dict(response: dict[str, Any]) -> ServerError: """ :param: response: dict PayWay response dictionary """ payway_error = ServerError() - payway_error.error_number = response.get("errorNumber") - payway_error.trace_code = response.get("traceCode") + payway_error.error_number = response.get("errorNumber", {}) + payway_error.trace_code = response.get("traceCode", {}) return payway_error def to_message(self) -> str: @@ -233,8 +233,8 @@ class PayWayPayment: def __init__( self, transaction_type: str, - customer_number: int | None = None, - amount: int | None = None, + customer_number: str | None = None, + amount: float | None = None, currency: str | None = None, order_number: str | None = None, ip_address: str | None = None, @@ -272,42 +272,42 @@ def to_dict(self) -> dict[str, Any]: class PayWayTransaction: - transaction_id: int = None - receipt_number: str = None - status: str = None - response_code: str = None - response_text: str = None - transaction_type: str = None - customer_number: str = None - customer_name: str = None - customer_email: str = None - bpay_ref: str = None - order_number: str = None - currency: str = None - principal_amount: float = None - surcharge_amount: float = None - payment_amount: float = None - payment_method: str = None - declined_date: str = None - card: PayWayCard = None - merchant: Merchant = None - virtual_account: dict = None - australia_post: dict = None - bpay: dict = None - your_bank_account: dict = None - customer_paypal_account: dict = None - your_paypal_account: dict = None - transaction_date_time: str = None - user: dict = None - settlement_date: str = None - parent_transaction: dict = None - ip_address: str = None - fraud_result: str = None - ip_country: str = None - card_country: str = None - custom_fields: dict = None - is_voidable: bool = None - is_refundable: bool = None + transaction_id: int | None = None + receipt_number: str | None = None + status: str | None = None + response_code: str | None = None + response_text: str | None = None + transaction_type: str | None = None + customer_number: str | None = None + customer_name: str | None = None + customer_email: str | None = None + bpay_ref: str | None = None + order_number: str | None = None + currency: str | None = None + principal_amount: float | None = None + surcharge_amount: float | None = None + payment_amount: float | None = None + payment_method: str | None = None + declined_date: str | None = None + card: PayWayCard | None = None + merchant: Merchant | None = None + virtual_account: dict[str, Any] | None = None + australia_post: dict[str, Any] | None = None + bpay: dict[str, Any] | None = None + your_bank_account: dict[str, Any] | None = None + customer_paypal_account: dict[str, Any] | None = None + your_paypal_account: dict[str, Any] | None = None + transaction_date_time: str | None = None + user: dict[str, Any] | None = None + settlement_date: str | None = None + parent_transaction: dict[str, Any] | None = None + ip_address: str | None = None + fraud_result: str | None = None + ip_country: str | None = None + card_country: str | None = None + custom_fields: dict[str, Any] | None = None + is_voidable: bool | None = None + is_refundable: bool | None = None def to_dict(self) -> dict[str, Any]: return { @@ -325,8 +325,8 @@ def to_dict(self) -> dict[str, Any]: "surchargeAmount": self.surcharge_amount, "paymentAmount": self.payment_amount, "paymentMethod": self.payment_method, - "creditCard": self.card.to_dict(), - "merchant": self.merchant.to_dict(), + "creditCard": self.card.to_dict() if self.card is not None else {}, + "merchant": self.merchant.to_dict() if self.merchant is not None else {}, "virtualAccount": self.virtual_account, "bpaustraliaPostay": self.australia_post, "bpay": self.bpay, @@ -348,7 +348,7 @@ def to_dict(self) -> dict[str, Any]: } @staticmethod - def from_dict(response: dict) -> PayWayTransaction: + def from_dict(response: dict[str, Any]) -> PayWayTransaction: """ :param: response: dict PayWay response dictionary """ @@ -369,10 +369,10 @@ def from_dict(response: dict) -> PayWayTransaction: transaction.payment_method = response.get("paymentMethod") if response.get("creditCard") is not None: - transaction.card = PayWayCard.from_dict(response.get("creditCard")) + transaction.card = PayWayCard.from_dict(response.get("creditCard", {})) if response.get("merchant") is not None: - transaction.merchant = Merchant.from_dict(response.get("merchant")) + transaction.merchant = Merchant.from_dict(response.get("merchant", {})) transaction.virtual_account = response.get("virtualAccount") transaction.australia_post = response.get("bpaustraliaPostay") @@ -406,14 +406,14 @@ class Merchant: account """ - merchant_id: str = None - merchant_name: str = None - settlement_bsb: str = None - settlement_account_number: str = None - surcharge_bsb: str = None - surcharge_account_number: str = None + merchant_id: str | None = None + merchant_name: str | None = None + settlement_bsb: str | None = None + settlement_account_number: str | None = None + surcharge_bsb: str | None = None + surcharge_account_number: str | None = None - def to_dict(self) -> dict[str, None]: + def to_dict(self) -> dict[str, Any]: return { "merchantId": self.merchant_id, "merchantName": self.merchant_name, @@ -424,7 +424,7 @@ def to_dict(self) -> dict[str, None]: } @staticmethod - def from_dict(payway_obj: dict) -> Merchant: + def from_dict(payway_obj: dict[str, Any]) -> Merchant: """ :param: payway_obj: dict PayWay response dictionary """ @@ -439,13 +439,13 @@ def from_dict(payway_obj: dict) -> Merchant: class PaymentSetup: - payment_method: str = None - stopped: bool = None - credit_card: PayWayCard = None - merchant: Merchant = None + payment_method: str | None = None + stopped: bool | None = None + credit_card: PayWayCard | None = None + merchant: Merchant | None = None @staticmethod - def from_dict(response: dict) -> PaymentSetup: + def from_dict(response: dict[str, Any]) -> PaymentSetup: """ :param: response: dict PayWay response dictionary """ @@ -453,20 +453,20 @@ def from_dict(response: dict) -> PaymentSetup: ps.payment_method = response.get("paymentMethod") ps.stopped = response.get("stopped") if response.get("creditCard") is not None: - ps.credit_card = PayWayCard().from_dict(response.get("creditCard")) + ps.credit_card = PayWayCard().from_dict(response.get("creditCard", {})) if response.get("merchant") is not None: - ps.merchant = Merchant().from_dict(response.get("merchant")) + ps.merchant = Merchant().from_dict(response.get("merchant", {})) return ps class TokenResponse: - token: str = None - payment_method: str = None - card: PayWayCard = None - bank_account: dict = None + token: str | None = None + payment_method: str | None = None + card: PayWayCard | None = None + bank_account: dict[str, Any] | None = None @staticmethod - def from_dict(response: dict) -> TokenResponse: + def from_dict(response: dict[str, Any]) -> TokenResponse: """ :param: response: dict PayWay response dictionary """ diff --git a/payway/test_utils.py b/payway/test_utils.py index e1bd562..d1a7f5b 100644 --- a/payway/test_utils.py +++ b/payway/test_utils.py @@ -2,9 +2,10 @@ import json import pathlib +from typing import Any -def load_json_file(file_path: str) -> dict: +def load_json_file(file_path: str) -> dict[str, Any]: file = pathlib.Path(file_path) with open(file) as f: return json.load(f) diff --git a/pyproject.toml b/pyproject.toml index 5850806..3ca4a15 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -114,7 +114,7 @@ exclude = ["__init__.py"] required-imports = ["from __future__ import annotations"] [tool.pyright] -typeCheckingMode = "off" +typeCheckingMode = "strict" [tool.pytest.ini_options] log_cli = true diff --git a/tests/test_client.py b/tests/test_client.py index 64cf0c9..1e08049 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -2,7 +2,6 @@ import copy import unittest -from typing import NoReturn from unittest.mock import patch from payway.client import Client @@ -18,7 +17,7 @@ class TestClient(unittest.TestCase): @classmethod - def setUpClass(cls) -> NoReturn: + def setUpClass(cls) -> None: merchant_id = "TEST" bank_account_id = "0000000A" publishable_api_key = "TPUBLISHABLE-API-KEY" @@ -72,7 +71,7 @@ def setUpClass(cls) -> NoReturn: cls.payment = PayWayPayment( customer_number="", transaction_type="payment", - amount="10", + amount=10, currency="aud", order_number="5100", ip_address="127.0.0.1", @@ -80,7 +79,7 @@ def setUpClass(cls) -> NoReturn: cls.pre_auth_payment = PayWayPayment( customer_number="", transaction_type="preAuth", - amount="2.15", + amount=2.15, currency="aud", order_number="5110", ip_address="127.0.0.1", @@ -88,7 +87,7 @@ def setUpClass(cls) -> NoReturn: cls.pre_auth_capture_payment = PayWayPayment( transaction_type="capture", parent_transaction_id="", - amount="2.15", + amount=2.15, order_number="5111", ip_address="127.0.0.1", ) @@ -104,7 +103,7 @@ def setUpClass(cls) -> NoReturn: ) @patch("requests.post") - def test_create_token(self, mock_post) -> NoReturn: + def test_create_token(self, mock_post) -> None: mock_post.return_value.status_code = 200 mock_post.return_value.json.return_value = { "singleUseTokenId": "2bcec36f-7b02-43db-b3ec-bfb65acfe272", @@ -118,7 +117,7 @@ def test_create_token(self, mock_post) -> NoReturn: self.assertEqual(token_response.token, "2bcec36f-7b02-43db-b3ec-bfb65acfe272") @patch("requests.post") - def test_create_bank_account_token(self, mock_post) -> NoReturn: + def test_create_bank_account_token(self, mock_post) -> None: mock_post.return_value.status_code = 200 mock_post.return_value.json.return_value = { "singleUseTokenId": "3bcec36f-7b02-43db-b3ec-bfb65acfe272", @@ -131,7 +130,7 @@ def test_create_bank_account_token(self, mock_post) -> NoReturn: self.assertEqual(token, "3bcec36f-7b02-43db-b3ec-bfb65acfe272") @patch("requests.post") - def test_create_customer(self, mock_post) -> NoReturn: + def test_create_customer(self, mock_post) -> None: mock_post.return_value.status_code = 200 mock_post.return_value.json.return_value = load_json_file("tests/data/customer.json") card = self.card @@ -146,7 +145,7 @@ def test_create_customer(self, mock_post) -> NoReturn: self.assertEqual(payway_customer.email_address, "bect@example.net") @patch("requests.put") - def test_create_customer_with_custom_id(self, mock_post) -> NoReturn: + def test_create_customer_with_custom_id(self, mock_post) -> None: mock_post.return_value.status_code = 200 mock_post.return_value.json.return_value = load_json_file("tests/data/customer.json") customer = copy.deepcopy(self.customer) @@ -158,7 +157,7 @@ def test_create_customer_with_custom_id(self, mock_post) -> NoReturn: self.assertEqual(payway_customer_number, "98") @patch("requests.post") - def test_process_payment(self, mock_post) -> NoReturn: + def test_process_payment(self, mock_post) -> None: # Take payment (using a credit card token) mock_post.return_value.status_code = 200 mock_post.return_value.json.return_value = load_json_file("tests/data/transaction.json") @@ -177,7 +176,7 @@ def test_process_payment(self, mock_post) -> NoReturn: self.assertEqual(transaction.response_code, "11") @patch("requests.post") - def test_process_payment_with_idempotency_key(self, mock_post) -> NoReturn: + def test_process_payment_with_idempotency_key(self, mock_post) -> None: """ Send a payment using a unique idempotency key to try and avoid duplicate POSTs https://www.payway.com.au/docs/rest.html#avoiding-duplicate-posts @@ -204,7 +203,7 @@ def test_process_payment_with_idempotency_key(self, mock_post) -> NoReturn: self.assertEqual(transaction.response_code, "11") @patch("requests.get") - def test_get_transaction_card(self, mock_get) -> NoReturn: + def test_get_transaction_card(self, mock_get) -> None: mock_get.return_value.status_code = 200 mock_get.return_value.json.return_value = load_json_file("tests/data/card_transaction.json") transaction, errors = self.client.get_transaction( @@ -215,7 +214,7 @@ def test_get_transaction_card(self, mock_get) -> NoReturn: self.assertEqual(transaction.transaction_id, 1179985404) @patch("requests.post") - def test_void(self, mock_post) -> NoReturn: + def test_void(self, mock_post) -> None: mock_post.return_value.status_code = 200 mock_post.return_value.json.return_value = load_json_file("tests/data/void_transaction.json") void_transaction, void_errors = self.client.void_transaction(1179985404) @@ -224,7 +223,7 @@ def test_void(self, mock_post) -> NoReturn: self.assertEqual(void_transaction.status, "voided") @patch("requests.post") - def test_refund(self, mock_post) -> NoReturn: + def test_refund(self, mock_post) -> None: mock_post.return_value.status_code = 200 mock_post.return_value.json.return_value = load_json_file("tests/data/refund_transaction.json") transaction, errors = self.client.refund_transaction( @@ -235,7 +234,7 @@ def test_refund(self, mock_post) -> NoReturn: self.assertEqual(transaction.status, "refunded") @patch("requests.get") - def test_get_customer(self, mock_get) -> NoReturn: + def test_get_customer(self, mock_get) -> None: mock_get.return_value.status_code = 200 mock_get.return_value.json.return_value = load_json_file("tests/data/customer.json") customer, customer_errors = self.client.get_customer("98") @@ -245,7 +244,7 @@ def test_get_customer(self, mock_get) -> NoReturn: self.assertEqual(customer.customer_name, "Rebecca Turing") @patch("requests.put") - def test_update_payment_setup_card(self, mock_post) -> NoReturn: + def test_update_payment_setup_card(self, mock_post) -> None: # update card or bank account in PayWay from token mock_post.return_value.status_code = 200 mock_post.return_value.json.return_value = { diff --git a/tests/test_customers.py b/tests/test_customers.py index d25a587..9651709 100644 --- a/tests/test_customers.py +++ b/tests/test_customers.py @@ -2,7 +2,6 @@ import datetime import unittest -from typing import NoReturn from unittest.mock import patch from payway.client import Client @@ -12,7 +11,7 @@ class TestCustomerRequest(unittest.TestCase): @classmethod - def setUpClass(cls) -> NoReturn: + def setUpClass(cls) -> None: """ You will need to create a sandbox PayWay account and add your sandbox API keys into your environment """ @@ -102,7 +101,7 @@ def setUpClass(cls) -> NoReturn: ) @patch("requests.Session.patch") - def test_stop_all_payments(self, mock_post) -> NoReturn: + def test_stop_all_payments(self, mock_post) -> None: # stop payments for customer mock_post.return_value.status_code = 200 mock_post.return_value.json.return_value = { @@ -113,7 +112,7 @@ def test_stop_all_payments(self, mock_post) -> NoReturn: self.assertEqual(stopped, True) @patch("requests.Session.patch") - def test_start_all_payments(self, mock_post) -> NoReturn: + def test_start_all_payments(self, mock_post) -> None: # start payments for customer mock_post.return_value.status_code = 200 mock_post.return_value.json.return_value = { @@ -124,14 +123,14 @@ def test_start_all_payments(self, mock_post) -> NoReturn: self.assertEqual(stopped, False) @patch("requests.Session.delete") - def test_delete_customer(self, mock_post) -> NoReturn: + def test_delete_customer(self, mock_post) -> None: mock_post.return_value.status_code = 204 # delete customer record in PayWay response = self.client.delete_customer("1") self.assertEqual(response.status_code, 204) @patch("requests.Session.put") - def test_schedule_payments(self, mock_post) -> NoReturn: + def test_schedule_payments(self, mock_post) -> None: next_week = datetime.datetime.now() + datetime.timedelta(weeks=1) next_payment_date = next_week.strftime("%d %b %Y") mock_post.return_value.status_code = 200 @@ -156,14 +155,14 @@ def test_schedule_payments(self, mock_post) -> NoReturn: self.assertEqual(response["regularPaymentAmount"], 10.50) @patch("requests.Session.delete") - def test_stop_schedule(self, mock_post) -> NoReturn: + def test_stop_schedule(self, mock_post) -> None: # stop schedule mock_post.return_value.status_code = 204 response = self.client.stop_schedule("1") self.assertEqual(response.status_code, 204) @patch("requests.Session.put") - def test_update_contact_details(self, mock_post) -> NoReturn: + def test_update_contact_details(self, mock_post) -> None: # update contact details mock_post.return_value.status_code = 200 mock_post.return_value.json.return_value = { @@ -208,7 +207,7 @@ def test_update_contact_details(self, mock_post) -> NoReturn: self.assertEqual(address["postalCode"], new_postcode) @patch("requests.Session.get") - def test_list_customers(self, mock_get) -> NoReturn: + def test_list_customers(self, mock_get) -> None: mock_get.return_value.status_code = 200 mock_get.return_value.json.return_value = load_json_file("tests/data/customers.json") response = self.client.list_customers() diff --git a/tests/test_transactions.py b/tests/test_transactions.py index 13d4137..a829d26 100644 --- a/tests/test_transactions.py +++ b/tests/test_transactions.py @@ -1,7 +1,6 @@ from __future__ import annotations import unittest -from typing import NoReturn from unittest.mock import patch from payway.client import Client @@ -10,7 +9,7 @@ class TestTransactionRequest(unittest.TestCase): @classmethod - def setUpClass(cls) -> NoReturn: + def setUpClass(cls) -> None: merchant_id = "TEST" bank_account_id = "0000000A" publishable_api_key = "TPUBLISHABLE-API-KEY" @@ -24,7 +23,7 @@ def setUpClass(cls) -> NoReturn: ) @patch("requests.Session.get") - def test_search_transactions(self, mock_get) -> NoReturn: + def test_search_transactions(self, mock_get) -> None: mock_get.return_value.status_code = 200 mock_get.return_value.json.return_value = load_json_file("tests/data/transactions.json") query = "/search-customer?customerNumber=1"