-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(test): test external protocols (#80)
- Loading branch information
1 parent
ff6c4a9
commit e5d781c
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
|
||
import pytest | ||
from unittest.mock import patch, AsyncMock, create_autospec | ||
|
||
from eth_portfolio.protocols._base import StakingPoolABC | ||
from eth_portfolio.typing import Balance, RemoteTokenBalances, TokenBalances | ||
from eth_portfolio.protocols import ExternalBalances | ||
|
||
|
||
SOME_ADDRESS = '0x0000000000000000000000000000000000000001' | ||
SOME_TOKEN = '0x0000000000000000000000000000000000000002' | ||
SOME_OTHER_TOKEN = '0x0000000000000000000000000000000000000003' | ||
|
||
|
||
class MockProtocolA(AsyncMock): | ||
... | ||
|
||
class MockProtocolB(AsyncMock): | ||
... | ||
|
||
@patch('a_sync.map') | ||
@pytest.mark.asyncio | ||
async def test_balances_no_protocols(mock_map): | ||
external_balances = ExternalBalances() | ||
external_balances.protocols = [] | ||
|
||
balances = await external_balances.balances(SOME_ADDRESS) | ||
|
||
assert balances == RemoteTokenBalances() | ||
mock_map.assert_not_called() | ||
|
||
@pytest.mark.asyncio | ||
async def test_balances_with_protocols(): | ||
mock_protocol_a = MockProtocolA() | ||
mock_protocol_a.balances.return_value = TokenBalances({SOME_TOKEN: Balance(100, 200)}) | ||
|
||
mock_protocol_b = MockProtocolB() | ||
mock_protocol_b.balances.return_value = TokenBalances({SOME_OTHER_TOKEN: Balance(200, 400)}) | ||
|
||
external_balances = ExternalBalances() | ||
external_balances.protocols = [mock_protocol_a, mock_protocol_b] | ||
|
||
balances = await external_balances.balances(SOME_ADDRESS) | ||
|
||
assert balances == RemoteTokenBalances({'MockProtocolA': TokenBalances({SOME_TOKEN: Balance(100, 200)}), | ||
'MockProtocolB': TokenBalances({SOME_OTHER_TOKEN: Balance(200, 400)})}) | ||
|
||
for protocol in external_balances.protocols: | ||
protocol.balances.assert_called_once_with(SOME_ADDRESS, None) | ||
|
||
@pytest.mark.asyncio | ||
async def test_balances_with_protocols_and_block(): | ||
block = 1234567 | ||
|
||
|
||
mock_protocol_a = MockProtocolA() | ||
mock_protocol_a.balances.return_value = TokenBalances({SOME_TOKEN: Balance(100, 200)}) | ||
|
||
mock_protocol_b = MockProtocolB() | ||
mock_protocol_b.balances.return_value = TokenBalances({SOME_OTHER_TOKEN: Balance(200, 400)}) | ||
|
||
external_balances = ExternalBalances() | ||
external_balances.protocols = [mock_protocol_a, mock_protocol_b] | ||
|
||
balances = await external_balances.balances(SOME_ADDRESS, block) | ||
|
||
assert balances == RemoteTokenBalances({'MockProtocolA': TokenBalances({SOME_TOKEN: Balance(100, 200)}), | ||
'MockProtocolB': TokenBalances({SOME_OTHER_TOKEN: Balance(200, 400)})}) | ||
|
||
for protocol in external_balances.protocols: | ||
protocol.balances.assert_called_once_with(SOME_ADDRESS, block) |