Skip to content

Commit

Permalink
Test with mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
hweawer committed Aug 29, 2024
1 parent 55c4a55 commit bb4dd3e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 23 deletions.
7 changes: 5 additions & 2 deletions src/transport/msg_providers/data_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from web3 import Web3
from web3._utils.events import get_event_data
from web3.exceptions import BlockNotFound
from web3.types import FilterParams, LogReceipt
from web3.types import EventData, FilterParams, LogReceipt

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -57,8 +57,11 @@ def __init__(self, w3: Web3, schema: str):
def _create_message(self, parsed_data: dict, guardian: str) -> dict:
pass

def _decode_event(self, log: LogReceipt) -> EventData:
return get_event_data(self._w3.codec, self._message_abi, log)

def parse(self, log: LogReceipt) -> Optional[dict]:
e = get_event_data(self._w3.codec, self._message_abi, log)
e = self._decode_event(log)
unparsed_event = e['args']['data']
guardian = e['args']['sender']
decoded_data = self._w3.codec.decode([self._schema], unparsed_event)[0]
Expand Down
77 changes: 56 additions & 21 deletions tests/transport/test_data_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import pytest
import variables
from eth_typing import BlockNumber, ChecksumAddress, HexAddress, HexStr
from hexbytes import HexBytes
from schema import Or, Schema
from transport.msg_providers.data_bus import PING_V1_DATA_SCHEMA, DataBusProvider, DataBusSinks
from transport.msg_providers.data_bus import DEPOSIT_V1_DATA_SCHEMA, PING_V1_DATA_SCHEMA, DataBusProvider, DataBusSinks
from transport.msg_types.deposit import DepositMessageSchema
from transport.msg_types.ping import PingMessageDataBusSchema
from web3 import Web3
from web3.types import LogReceipt
from web3.types import EventData
from web3_multi_provider import FallbackProvider

_DEFAULT_GUARDIAN = '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266'
Expand Down Expand Up @@ -58,33 +56,70 @@ def test_data_bus_provider():
# print('stakingModuleId', stakingModuleId)


def foo(web3_lido_unit):
@pytest.mark.unit
def test_data_bus_mock_responses(web3_lido_unit):
receipts = mock_receipts(web3_lido_unit)
web3_lido_unit.eth.get_logs = Mock(return_value=receipts)
web3_lido_unit.eth.get_logs = Mock(side_effect=[receipts, None])
web3_lido_unit.is_connected = Mock(return_value=True)
web3_lido_unit.eth.get_block = Mock(return_value={'number': 123})
web3_lido_unit.eth.get_block_number = Mock(return_value=1)
provider = DataBusProvider(
w3=web3_lido_unit,
message_schema=Schema(Or(DepositMessageSchema, PingMessageDataBusSchema)),
sinks=[DataBusSinks.DEPOSIT_V1, DataBusSinks.PING_V1],
)

for parser in provider._parsers:
parser._decode_event = Mock(side_effect=receipts)

messages = provider.get_messages()
print(messages)
for mes in messages:
print(mes)
assert len(messages) == len(receipts)


def mock_receipts(w3: Web3) -> list[LogReceipt]:
def mock_receipts(w3: Web3) -> list[EventData]:
return [
LogReceipt(
address=ChecksumAddress(HexAddress(HexStr(_DEFAULT_GUARDIAN))),
blockHash=HexBytes(True), # dummy value
blockNumber=BlockNumber(1), # dummy value
logIndex=1, # dummy value
transactionHash=HexBytes(True), # dummy value
transactionIndex=1, # dummy value
removed=False, # dummy value
topics=[w3.keccak(text=DataBusSinks.PING_V1)],
data=w3.codec.encode(
types=[PING_V1_DATA_SCHEMA], args=[(1, ('0x0000000000000000000000000000000000000000000000000000000000000000',))]
),
EventData(
args={
'sender': _DEFAULT_GUARDIAN,
'data': w3.codec.encode(
types=[PING_V1_DATA_SCHEMA], args=[(1, ('0x0000000000000000000000000000000000000000000000000000000000000000',))]
),
},
),
EventData(
args={
'sender': _DEFAULT_GUARDIAN,
'data': w3.codec.encode(
types=[DEPOSIT_V1_DATA_SCHEMA],
args=[
(
'0x0000000000000000000000000000000000000000000000000000000000000000',
40,
2,
'0x42eef33d13c4440627c3fab6e3abee85af796ae6f77dcade628b183640b519d0',
'0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
3,
('0x0000000000000000000000000000000000000000000000000000000000000000',),
)
],
),
},
),
EventData(
args={
'sender': _DEFAULT_GUARDIAN,
'data': w3.codec.encode(
types=[PING_V1_DATA_SCHEMA], args=[(3, ('0x0000000000000000000000000000000000000000000000000000000000000000',))]
),
},
),
EventData(
args={
'sender': _DEFAULT_GUARDIAN,
'data': w3.codec.encode(
types=[PING_V1_DATA_SCHEMA], args=[(4, ('0x0000000000000000000000000000000000000000000000000000000000000000',))]
),
},
),
]

0 comments on commit bb4dd3e

Please sign in to comment.