Skip to content

Commit

Permalink
Test error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
trizin committed Jan 17, 2024
1 parent b3feb59 commit db30b3a
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions df_py/util/test/test_blocktime_eth_mainnet.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import random
from datetime import datetime

from enforce_typing import enforce_types
Expand Down Expand Up @@ -28,6 +29,32 @@ def test_eth_timestamp_to_block(monkeypatch):
assert guess == approx(block, 10)


@enforce_types
def test_eth_timestamp_to_block_error_handling(monkeypatch):
monkeypatch.setenv("WEB3_INFURA_PROJECT_ID", "9aa3d95b3bc440fa88ea12eaa4456161")
monkeypatch.setenv("MAINNET_RPC_URL", "https://mainnet.infura.io/v3/")
monkeypatch.setenv("INFURA_NETWORKS", "mainnet")
web3 = get_web3(get_rpc_url("mainnet"))
n = web3.eth.get_block

def random_error(*args, **kwargs):
if random.randint(1, 6) == 1:
raise Exception("Random error occurred!")
return n(*args, **kwargs)

web3.eth.get_block = random_error

current_block = web3.eth.get_block("latest").number
blocks_ago = web3.eth.get_block(current_block - 5000)

ts = blocks_ago.timestamp
block = blocks_ago.number

guess = eth_timestamp_to_block(web3, ts)

assert guess == approx(block, 10)


def test_timestr_to_block_eth_1(monkeypatch):
monkeypatch.setenv("WEB3_INFURA_PROJECT_ID", "9aa3d95b3bc440fa88ea12eaa4456161")
monkeypatch.setenv("MAINNET_RPC_URL", "https://mainnet.infura.io/v3/")
Expand Down

0 comments on commit db30b3a

Please sign in to comment.