Skip to content

Commit 19b9454

Browse files
committed
Fix Account pydantic model to properly handle serialization and deserialization
1 parent a8ee1a0 commit 19b9454

File tree

3 files changed

+30
-50
lines changed

3 files changed

+30
-50
lines changed

alpaca/broker/models/accounts.py

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -262,56 +262,6 @@ class Account(ModelWithID):
262262
documents: Optional[List[AccountDocument]] = None
263263
trusted_contact: Optional[TrustedContact] = None
264264

265-
def __init__(self, **response):
266-
super().__init__(
267-
id=(UUID(response["id"])),
268-
account_number=(response["account_number"]),
269-
status=(response["status"]),
270-
crypto_status=(
271-
response["crypto_status"] if "crypto_status" in response else None
272-
),
273-
kyc_results=(
274-
TypeAdapter(KycResults).validate_python(response["kyc_results"])
275-
if "kyc_results" in response and response["kyc_results"] is not None
276-
else None
277-
),
278-
currency=(response["currency"]),
279-
last_equity=(response["last_equity"]),
280-
created_at=(response["created_at"]),
281-
contact=(
282-
TypeAdapter(Contact).validate_python(response["contact"])
283-
if "contact" in response
284-
else None
285-
),
286-
identity=(
287-
TypeAdapter(Identity).validate_python(response["identity"])
288-
if "identity" in response
289-
else None
290-
),
291-
disclosures=(
292-
TypeAdapter(Disclosures).validate_python(response["disclosures"])
293-
if "disclosures" in response
294-
else None
295-
),
296-
agreements=(
297-
TypeAdapter(List[Agreement]).validate_python(response["agreements"])
298-
if "agreements" in response
299-
else None
300-
),
301-
documents=(
302-
TypeAdapter(List[AccountDocument]).validate_python(
303-
response["documents"]
304-
)
305-
if "documents" in response
306-
else None
307-
),
308-
trusted_contact=(
309-
TypeAdapter(TrustedContact).validate_python(response["trusted_contact"])
310-
if "trusted_contact" in response
311-
else None
312-
),
313-
)
314-
315265

316266
class TradeAccount(BaseTradeAccount):
317267
"""

tests/broker/factories/accounts.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from typing import List
2+
3+
from alpaca.broker import Account
24
from alpaca.trading.enums import DTBPCheck, PDTCheck
35
from alpaca.trading.models import AccountConfiguration as TradeAccountConfiguration
46

@@ -143,3 +145,21 @@ def create_dummy_trade_account_configuration() -> TradeAccountConfiguration:
143145
trade_confirm_email="all",
144146
ptp_no_exception_entry=False,
145147
)
148+
149+
150+
def create_dummy_account() -> Account:
151+
"""
152+
Create a basic account instance with prefilled data
153+
154+
Returns:
155+
Account: a prefilled Account instance for testing
156+
"""
157+
158+
return Account(
159+
id="2d6cab28-c5d1-4ff8-91c6-b6404a9ee114",
160+
account_number="551081356",
161+
status="ACTIVE",
162+
currency="USD",
163+
last_equity="0",
164+
created_at="2024-07-10T18:35:52.244506Z",
165+
)

tests/broker/test_models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from datetime import datetime
22

3+
from alpaca.broker import Account
4+
from tests.broker.factories import accounts as factory
5+
36
import pytest
47

58
from alpaca.broker.requests import (
@@ -466,3 +469,10 @@ def test_journal_with_amount_and_qty():
466469
)
467470

468471
assert "Cash journals must contain an amount to transfer." in str(e.value)
472+
473+
474+
def test_account_serialization_deserialization():
475+
account = factory.create_dummy_account()
476+
json_str = account.model_dump_json()
477+
account2 = Account.model_validate_json(json_str)
478+
assert account2 == account

0 commit comments

Comments
 (0)