diff --git a/e2e_tests/api_token_test.py b/e2e_tests/api_token_test.py index 9b10bab2..b6e2d8f7 100644 --- a/e2e_tests/api_token_test.py +++ b/e2e_tests/api_token_test.py @@ -1,15 +1,17 @@ import os import unittest -from datetime import datetime +from datetime import datetime, timedelta from unit import Unit from unit.models.api_token import CreateAPITokenRequest +from unit.utils.date_utils import from_datetime token = os.environ.get('TOKEN') client = Unit("https://api.s.unit.sh", token) user_id = "252" +expiration_time = datetime.now() + timedelta(hours=1) def create_api_token(): - request = CreateAPITokenRequest(user_id, "Test token", "customers applications", datetime(2022, 7, 1, 13, 47, 17)) + request = CreateAPITokenRequest(user_id, "Test token", "customers applications", expiration_time) return client.api_tokens.create(request).data def test_list_api_tokens(): @@ -24,7 +26,7 @@ def test_create_api_token(): assert api_token.type == "apiToken" def test_create_api_token_prev_version( ): - request = CreateAPITokenRequest(user_id, "Test token", "customers applications", "2022-07-01T13:47:17.000Z") + request = CreateAPITokenRequest(user_id, "Test token", "customers applications", from_datetime(expiration_time)) api_token = client.api_tokens.create(request).data assert api_token.type == "apiToken" diff --git a/e2e_tests/transaction_test.py b/e2e_tests/transaction_test.py index bd9f8134..ccd7898f 100644 --- a/e2e_tests/transaction_test.py +++ b/e2e_tests/transaction_test.py @@ -335,6 +335,72 @@ def test_purchase_transaction(): assert transaction.attributes["digitalWallet"] == "Apple" assert transaction.attributes["paymentMethod"] == "Contactless" +def test_purchase_transaction_without_coordinates(): + purchase_transaction_api_response = { + "type": "purchaseTransaction", + "id": "51", + "attributes": { + "createdAt": "2020-09-08T12:41:43.360Z", + "direction": "Debit", + "amount": 2500, + "balance": 10523, + "summary": "Car rental", + "cardLast4Digits": "2282", + "merchant": { + "name": "Europcar Mobility Group", + "type": 3381, + "category": "EUROP CAR", + "location": "Cupertino, CA" + }, + "recurring": False, + "interchange": 2.43, + "ecommerce": False, + "cardPresent": True, + "paymentMethod": "Contactless", + "digitalWallet": "Apple", + "cardVerificationData": { + "verificationMethod": "CVV2" + }, + "cardNetwork": "Visa" + }, + "relationships": { + "account": { + "data": { + "type": "account", + "id": "10001" + } + }, + "customer": { + "data": { + "type": "customer", + "id": "3" + } + }, + "card": { + "data": { + "type": "card", + "id": "11" + } + }, + "authorization": { + "data": { + "type": "authorization", + "id": "40" + } + } + } + } + + id = purchase_transaction_api_response["id"] + attributes = purchase_transaction_api_response["attributes"] + relationships = purchase_transaction_api_response["relationships"] + _type = purchase_transaction_api_response["type"] + + transaction = PurchaseTransactionDTO.from_json_api(id, _type, attributes, relationships) + + assert transaction.id == id + assert transaction.attributes["interchange"] == 2.43 + def test_payment_canceled_transaction(): payment_canceled_transaction_api_response = { "type": "paymentCanceledTransaction", diff --git a/setup.py b/setup.py index c1fea1d2..91b8c01d 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='unit-python-sdk', packages=['unit', 'unit.api', 'unit.models', 'unit.utils'], - version='0.12.0', + version='0.12.1', license='Mozilla Public License 2.0', description='This library provides a python wrapper to http://unit.co API. See https://docs.unit.co/', author='unit.co', diff --git a/unit/models/__init__.py b/unit/models/__init__.py index 3b4feaca..6e049a45 100644 --- a/unit/models/__init__.py +++ b/unit/models/__init__.py @@ -260,6 +260,9 @@ def __init__(self, longitude: float, latitude: float): @staticmethod def from_json_api(data: Dict): + if not data: + return None + return Coordinates(data["longitude"], data["latitude"]) diff --git a/unit/models/transaction.py b/unit/models/transaction.py index 80779698..2700454c 100644 --- a/unit/models/transaction.py +++ b/unit/models/transaction.py @@ -129,7 +129,7 @@ def from_json_api(_id, _type, attributes, relationships): class PurchaseTransactionDTO(BaseTransactionDTO): def __init__(self, id: str, created_at: datetime, direction: str, amount: int, balance: int, - summary: str, card_last_4_digits: str, merchant: Merchant, coordinates: Coordinates, recurring: bool, + summary: str, card_last_4_digits: str, merchant: Merchant, coordinates: Optional[Coordinates], recurring: bool, interchange: Optional[int], ecommerce: bool, card_present: bool, payment_method: Optional[str], digital_wallet: Optional[str], card_verification_data, card_network: Optional[str], tags: Optional[Dict[str, str]], relationships: Optional[Dict[str, Relationship]]): @@ -152,7 +152,7 @@ def from_json_api(_id, _type, attributes, relationships): return PurchaseTransactionDTO( _id, date_utils.to_datetime(attributes["createdAt"]), attributes["direction"], attributes["amount"], attributes["balance"], attributes["summary"], attributes["cardLast4Digits"], - Merchant.from_json_api(attributes["merchant"]), Coordinates.from_json_api(attributes["coordinates"]), + Merchant.from_json_api(attributes["merchant"]), Coordinates.from_json_api(attributes.get("coordinates")), attributes["recurring"], attributes.get("interchange"), attributes.get("ecommerce"), attributes.get("cardPresent"), attributes.get("paymentMethod"), attributes.get("digitalWallet"), attributes.get("cardVerificationData"), attributes.get("cardNetwork"), attributes.get("tags"),