def test_account_state_event_to_from_dict_and_str_repr(self): # Arrange uuid = UUID4() balance = AccountBalance( currency=USD, total=Money(1525000, USD), locked=Money(0, USD), free=Money(1525000, USD), ) event = AccountState( account_id=AccountId("SIM", "000"), account_type=AccountType.MARGIN, base_currency=USD, reported=True, balances=[balance], info={}, event_id=uuid, ts_event=0, ts_init=0, ) # Act, Assert assert AccountState.from_dict(AccountState.to_dict(event)) == event assert ( str(event) == f"AccountState(account_id=SIM-000, account_type=MARGIN, base_currency=USD, is_reported=True, balances=[AccountBalance(total=1_525_000.00 USD, locked=0.00 USD, free=1_525_000.00 USD)], event_id={uuid})" # noqa ) assert ( repr(event) == f"AccountState(account_id=SIM-000, account_type=MARGIN, base_currency=USD, is_reported=True, balances=[AccountBalance(total=1_525_000.00 USD, locked=0.00 USD, free=1_525_000.00 USD)], event_id={uuid})" # noqa )
def _deserialize(values): balances = [] for v in values: balances.append( dict( currency=v["balance_currency"], total=v["balance_total"], locked=v["balance_locked"], free=v["balance_free"], )) state = { k: v for k, v in values[0].items() if not k.startswith("balance_") } state["balances"] = orjson.dumps(balances) return AccountState.from_dict(state)
def _deserialize(values): balances = [] for v in values: total = v.get("balance_total") if total is None: continue balances.append( dict( total=total, locked=v["balance_locked"], free=v["balance_free"], currency=v["balance_currency"], )) margins = [] for v in values: initial = v.get("margin_initial") if initial is None: continue margins.append( dict( initial=initial, maintenance=v["margin_maintenance"], currency=v["margin_currency"], instrument_id=v["margin_instrument_id"], )) state = { k: v for k, v in values[0].items() if not k.startswith("balance_") and not k.startswith("margin_") } state["balances"] = orjson.dumps(balances) state["margins"] = orjson.dumps(margins) return AccountState.from_dict(state)