Skip to content

Commit e838157

Browse files
fix: Handle optional receiverCounterparty field on AccountLowBalanceClosureTransaction resource (#290)
* fix: Handle accountLowBalanceClosureTransaction's optional receiverCounterparty field * tests: Add accountLowBalanceClosureTransaction decoding tests --------- Co-authored-by: Henrique Salvaro <[email protected]>
1 parent 0abc696 commit e838157

File tree

2 files changed

+83
-5
lines changed

2 files changed

+83
-5
lines changed

e2e_tests/transaction_test.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import os
2+
3+
import pytest
4+
25
from unit import Unit
36
from unit.models.transaction import *
47
from unit.models.codecs import DtoDecoder, mappings
@@ -690,3 +693,75 @@ def test_wire_transaction():
690693
assert transaction.attributes["balance"] == wire_transaction_api_response.get("attributes").get("balance")
691694
assert transaction.attributes["summary"] == wire_transaction_api_response.get("attributes").get("summary")
692695

696+
697+
@pytest.mark.parametrize("include_counterparty", [True, False])
698+
def test_account_low_balance_closure_transaction(include_counterparty):
699+
account_low_balance_closure_transaction_response = {
700+
"type": "accountLowBalanceClosureTransaction",
701+
"id": "53",
702+
"attributes": {
703+
"createdAt": "2022-04-06T10:46:34.371Z",
704+
"amount": 800,
705+
"direction": "Credit",
706+
"balance": 113000,
707+
"summary": "Account Low Balance Closure",
708+
"tags": {
709+
"customer_type": "vip"
710+
}
711+
},
712+
"relationships": {
713+
"account": {
714+
"data": {
715+
"type": "account",
716+
"id": "10001"
717+
}
718+
},
719+
"customer": {
720+
"data": {
721+
"type": "customer",
722+
"id": "10000"
723+
}
724+
},
725+
"customers": {
726+
"data": [
727+
{
728+
"type": "customer",
729+
"id": "10000"
730+
}
731+
]
732+
},
733+
"org": {
734+
"data": {
735+
"type": "org",
736+
"id": "1"
737+
}
738+
},
739+
"receiverAccount": {
740+
"data": {
741+
"type": "account",
742+
"id": "10000"
743+
}
744+
}
745+
}
746+
}
747+
if include_counterparty:
748+
account_low_balance_closure_transaction_response["attributes"]["receiverCounterparty"] = {
749+
"name": "Unit Finance Inc.",
750+
"routingNumber": "091311229",
751+
"accountNumber": "864800000000",
752+
"accountType": "Checking"
753+
}
754+
755+
transaction = DtoDecoder.decode(account_low_balance_closure_transaction_response)
756+
757+
assert transaction.id == "53"
758+
assert transaction.type == "accountLowBalanceClosureTransaction"
759+
assert transaction.attributes["amount"] == 800
760+
assert transaction.attributes["direction"] == "Credit"
761+
assert transaction.attributes["balance"] == 113000
762+
assert transaction.attributes["summary"] == "Account Low Balance Closure"
763+
764+
if include_counterparty:
765+
assert transaction.attributes["receiverCounterparty"].routing_number == "091311229"
766+
else:
767+
assert "receiverCounterparty" not in transaction.attributes

unit/models/transaction.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -457,19 +457,22 @@ def from_json_api(_id, _type, attributes, relationships):
457457

458458
class AccountLowBalanceClosureTransactionDTO(BaseTransactionDTO):
459459
def __init__(self, id: str, created_at: datetime, direction: str, amount: int, balance: int, summary: str,
460-
receiver_counterparty: Counterparty, tags: Optional[Dict[str, str]],
460+
receiver_counterparty: Optional[Counterparty], tags: Optional[Dict[str, str]],
461461
relationships: Optional[Dict[str, Relationship]]):
462462
BaseTransactionDTO.__init__(self, id, created_at, direction, amount, balance, summary, tags, relationships)
463463
self.type = 'accountLowBalanceClosureTransaction'
464-
self.attributes["receiverCounterparty"] = receiver_counterparty
464+
if receiver_counterparty:
465+
self.attributes["receiverCounterparty"] = receiver_counterparty
465466

466467
@staticmethod
467468
def from_json_api(_id, _type, attributes, relationships):
469+
counterparty = None
470+
if attributes.get("receiverCounterparty"):
471+
counterparty = Counterparty.from_json_api(attributes.get("receiverCounterparty"))
468472
return AccountLowBalanceClosureTransactionDTO(_id, date_utils.to_datetime(attributes["createdAt"]),
469473
attributes["direction"], attributes["amount"], attributes["balance"],
470-
attributes["summary"],
471-
Counterparty.from_json_api(attributes.get("receiverCounterparty")),
472-
attributes.get("tags"), relationships)
474+
attributes["summary"], counterparty, attributes.get("tags"),
475+
relationships)
473476

474477

475478
class NegativeBalanceCoverageTransactionDTO(BaseTransactionDTO):

0 commit comments

Comments
 (0)