diff --git a/cdp/address.py b/cdp/address.py index f87a184..90dcdd0 100644 --- a/cdp/address.py +++ b/cdp/address.py @@ -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: @@ -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: diff --git a/cdp/historical_balance.py b/cdp/historical_balance.py index d70c643..f58c98f 100644 --- a/cdp/historical_balance.py +++ b/cdp/historical_balance.py @@ -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, @@ -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 diff --git a/tests/test_address.py b/tests/test_address.py index 8b8c647..b0fec8b 100644 --- a/tests/test_address.py +++ b/tests/test_address.py @@ -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 @@ -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() diff --git a/tests/test_historical_balance.py b/tests/test_historical_balance.py index f2a6d73..a50f126 100644 --- a/tests/test_historical_balance.py +++ b/tests/test_historical_balance.py @@ -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 @@ -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) diff --git a/tests/test_transaction.py b/tests/test_transaction.py index 87dcf84..ea8b58b 100644 --- a/tests/test_transaction.py +++ b/tests/test_transaction.py @@ -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 @@ -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)