Skip to content

Commit

Permalink
add tests & refactor a little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
ape364 committed May 27, 2024
1 parent 394cd9b commit de9cbe6
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
15 changes: 14 additions & 1 deletion aioetherscan/modules/extra/generators/generator_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async def _parse_by_blocks(
if end_block is None:
end_block = await self._get_current_block()

blocks_parser = BlocksParser(
blocks_parser = self._get_blocks_parser(
api_method, request_params, start_block, end_block, blocks_limit, blocks_limit_divider
)
async for tx in blocks_parser.txs_generator():
Expand Down Expand Up @@ -120,3 +120,16 @@ def _get_request_params(self, api_method: Callable, params: dict[str, Any]) -> d
{k: v for k, v in params.items() if k in api_method_params},
('self', 'start_block', 'end_block'),
)

@staticmethod
def _get_blocks_parser(
api_method: Callable,
request_params: dict[str, Any],
start_block: int,
end_block: int,
blocks_limit: int,
blocks_limit_divider: int,
) -> BlocksParser:
return BlocksParser(
api_method, request_params, start_block, end_block, blocks_limit, blocks_limit_divider
)
72 changes: 71 additions & 1 deletion tests/extra/generators/test_generator_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from unittest.mock import Mock, patch, MagicMock
from unittest.mock import Mock, patch, MagicMock, AsyncMock

import pytest

from aioetherscan.modules.extra.generators.blocks_parser import BlocksParser
from aioetherscan.modules.extra.generators.generator_utils import GeneratorUtils


Expand All @@ -14,13 +15,34 @@ async def parse_mock(*args, **kwargs):
yield None


def test_transfers() -> list[dict[str, str]]:
return [{'result': 'transfer1'}, {'result': 'transfer2'}]


async def transfers_mock(*args, **kwargs):
for t in test_transfers():
yield t


def test_init_with_default_values():
client = Mock()
utils = GeneratorUtils(client)

assert utils._client == client


def test_get_blocks_parser(generator_utils):
blocks_parser = BlocksParser(
api_method=None,
request_params={'param': 'value'},
start_block=100,
end_block=200,
blocks_limit=1000,
blocks_limit_divider=2,
)
assert isinstance(blocks_parser, BlocksParser)


@pytest.mark.asyncio
async def test_token_transfers(generator_utils):
params_return_value = {'param': 'value'}
Expand Down Expand Up @@ -126,3 +148,51 @@ async def test_mined_blocks(generator_utils):
)

mock.assert_called_once_with(param='value')


@pytest.mark.asyncio
async def test_parse_by_blocks(generator_utils):
blocks_parser_mock = Mock()
blocks_parser_mock.return_value.txs_generator = MagicMock(side_effect=transfers_mock)
generator_utils._get_blocks_parser = blocks_parser_mock

transfers = []
async for transfer in generator_utils._parse_by_blocks(
api_method=None,
request_params={'param': 'value'},
start_block=100,
end_block=200,
blocks_limit=1000,
blocks_limit_divider=2,
):
transfers.append(transfer)
assert transfers == test_transfers()

blocks_parser_mock.assert_called_once_with(None, {'param': 'value'}, 100, 200, 1000, 2)


async def test_parse_by_blocks_end_block_is_none(generator_utils):
blocks_parser_mock = Mock()
blocks_parser_mock.return_value.txs_generator = MagicMock(side_effect=transfers_mock)
generator_utils._get_blocks_parser = blocks_parser_mock

current_block = 200
get_current_block_mock = AsyncMock(return_value=current_block)
generator_utils._get_current_block = get_current_block_mock

transfers = []
async for transfer in generator_utils._parse_by_blocks(
api_method=None,
request_params={'param': 'value'},
start_block=100,
end_block=None,
blocks_limit=1000,
blocks_limit_divider=2,
):
transfers.append(transfer)
assert transfers == test_transfers()

get_current_block_mock.assert_awaited_once()
blocks_parser_mock.assert_called_once_with(
None, {'param': 'value'}, 100, current_block, 1000, 2
)

0 comments on commit de9cbe6

Please sign in to comment.