Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
xinyu-li-cb committed Oct 3, 2024
1 parent d6f7646 commit 08c3f8c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 30 deletions.
4 changes: 2 additions & 2 deletions cdp/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def balances(self):

return BalanceMap.from_models(response.data)

def historical_balances(self, asset_id) -> Iterator["HistoricalBalance"]:
def historical_balances(self, asset_id) -> Iterator[HistoricalBalance]:
"""List historical balances.
Args:
Expand All @@ -116,7 +116,7 @@ def historical_balances(self, asset_id) -> Iterator["HistoricalBalance"]:
"""
return HistoricalBalance.list(network_id=self.network_id, address_id=self.address_id, asset_id=asset_id)

def transactions(self) -> Iterator["Transaction"]:
def transactions(self) -> Iterator[Transaction]:
"""List transactions of the address.
Returns:
Expand Down
4 changes: 2 additions & 2 deletions cdp/historical_balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def from_model(cls, model: HistoricalBalanceModel) -> "HistoricalBalance":
"""
asset = Asset.from_model(model.asset)

return HistoricalBalance(
return cls(
amount=asset.from_atomic_amount(model.amount),
asset=asset,
block_height=model.block_height,
Expand Down Expand Up @@ -73,7 +73,7 @@ def list(cls, network_id: str, address_id: str, asset_id: str) -> Iterator["Hist
)

for model in response.data:
yield HistoricalBalance.from_model(model)
yield cls.from_model(model)

if not response.has_more:
break
Expand Down
26 changes: 0 additions & 26 deletions tests/test_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from cdp.client.exceptions import ApiException
from cdp.client.models.asset import Asset as AssetModel
from cdp.client.models.balance import Balance as BalanceModel
from cdp.client.models.historical_balance import HistoricalBalance as HistoricalBalanceModel
from cdp.client.models.transaction import Transaction as TransactionModel
from cdp.errors import ApiError
from cdp.faucet_transaction import FaucetTransaction
from cdp.historical_balance import HistoricalBalance
Expand All @@ -36,30 +34,6 @@ def balance_model(asset_model):
return BalanceModel(amount="1000000000000000000", asset=asset_model)


@pytest.fixture
def historical_balance_model(asset_model):
"""Create and return a fixture for a BalanceModel."""
return HistoricalBalanceModel(
amount="1000000000000000000", asset=asset_model, block_height="12345", block_hash="block_hash")


@pytest.fixture
def onchain_transaction_model():
"""Create and return a fixture for a TransactionModel."""
return TransactionModel(
network_id="base-sepolia",
transaction_hash="0xtransactionhash",
from_address_id="0xfromaddressid",
to_address_id="0xtoaddressid",
unsigned_payload="0xunsignedpayload",
signed_payload="0xsignedpayload",
status="complete",
block_hash="0xblockhash",
block_height="123456",
transaction_link="https://basescan.org/tx/0xtransactionlink",
)


def test_address_initialization(address_factory):
"""Test the initialization of an Address."""
address = address_factory()
Expand Down
17 changes: 17 additions & 0 deletions tests/test_historical_balance.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from decimal import Decimal
from unittest.mock import Mock, patch

import pytest

from cdp.asset import Asset
from cdp.client.exceptions import ApiException
from cdp.errors import ApiError
from cdp.historical_balance import HistoricalBalance


Expand Down Expand Up @@ -67,3 +71,16 @@ def test_list_historical_balances(mock_api_clients, historical_balance_model_fac
limit=100,
page=None
)


@patch("cdp.Cdp.api_clients")
def test_list_historical_balances_error(mock_api_clients):
"""Test the historical_balances method getting api error."""
mock_list_historical_balances = Mock()
err = ApiException(500, "boom")
mock_list_historical_balances.side_effect = ApiError(err, code="boom", message="boom")
mock_api_clients.balance_history.list_address_historical_balance = mock_list_historical_balances

with pytest.raises(ApiError):
historical_balances = HistoricalBalance.list(network_id="test-network-id", address_id="0xaddressid", asset_id="eth")
next(historical_balances)
15 changes: 15 additions & 0 deletions tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import pytest

from cdp.client.exceptions import ApiException
from cdp.client.models.transaction import Transaction as TransactionModel
from cdp.errors import ApiError
from cdp.transaction import Transaction


Expand Down Expand Up @@ -160,3 +162,16 @@ def test_list_transactions(mock_api_clients, transaction_model_factory):
mock_list_transactions.assert_called_once_with(
network_id="test-network-id", address_id="0xaddressid", limit=1, page=None
)


@patch("cdp.Cdp.api_clients")
def test_list_transactions_error(mock_api_clients):
"""Test the listing of transactions getting api error."""
mock_list_transactions = Mock()
err = ApiException(500, "boom")
mock_list_transactions.side_effect = ApiError(err, code="boom", message="boom")
mock_api_clients.transaction_history.list_address_transactions = mock_list_transactions

with pytest.raises(ApiError):
transactions = Transaction.list(network_id="test-network-id", address_id="0xaddressid")
next(transactions)

0 comments on commit 08c3f8c

Please sign in to comment.