Skip to content

Commit

Permalink
Update docstring example for request_mocker
Browse files Browse the repository at this point in the history
  • Loading branch information
fselmo committed Feb 1, 2024
1 parent 5af6781 commit b757af7
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions web3/_utils/module_testing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,21 @@ def test_my_w3(w3, request_mocker):
async def test_my_w3(async_w3, request_mocker):
def _iter_responses():
yield {"error": {"code": -32000, "message": "indexing in progress"}}
yield {"error": {"code": -32000, "message": "indexing in progress"}}
yield {"result": "0x1"}
while True:
yield {"error": {"message": "transaction indexing in progress"}}
yield {"error": {"message": "transaction indexing in progress"}}
yield {"result": {"status": "0x1"}}
iter_responses = _iter_responses()
async with request_mocker(
async_w3,
mock_responses={"eth_getTransactionReceipt": next(iter_responses)}
mock_responses={
"eth_getTransactionReceipt": lambda *_: next(iter_responses)
},
):
# assert that the first two error responses are handled and the result
# is eventually returned when present
assert await w3.eth.get_transaction_receipt("0x1") == "0x1"
Expand All @@ -85,9 +90,9 @@ def __init__(
self.mock_results = mock_results or {}
self.mock_errors = mock_errors or {}
self.mock_responses = mock_responses or {}
self._make_request: Union["AsyncMakeRequestFn", "MakeRequestFn"] = (
w3.provider.make_request
)
self._make_request: Union[
"AsyncMakeRequestFn", "MakeRequestFn"
] = w3.provider.make_request

def __enter__(self) -> "Self":
setattr(self.w3.provider, "make_request", self._mock_request_handler)
Expand Down

0 comments on commit b757af7

Please sign in to comment.