Skip to content

Commit 6914e59

Browse files
author
pacrob
committed
factor out assert_contains_log function
1 parent d46fa48 commit 6914e59

File tree

1 file changed

+64
-44
lines changed

1 file changed

+64
-44
lines changed

web3/_utils/module_testing/eth_module.py

+64-44
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ def mine_pending_block(web3: "Web3") -> None:
8383
web3.geth.miner.stop() # type: ignore
8484

8585

86+
def _assert_contains_log(
87+
result: Sequence[LogReceipt],
88+
block_with_txn_with_log: BlockData,
89+
emitter_contract_address: ChecksumAddress,
90+
txn_hash_with_log: HexStr,
91+
) -> None:
92+
assert len(result) == 1
93+
log_entry = result[0]
94+
assert log_entry['blockNumber'] == block_with_txn_with_log['number']
95+
assert log_entry['blockHash'] == block_with_txn_with_log['hash']
96+
assert log_entry['logIndex'] == 0
97+
assert is_same_address(log_entry['address'], emitter_contract_address)
98+
assert log_entry['transactionIndex'] == 0
99+
assert log_entry['transactionHash'] == HexBytes(txn_hash_with_log)
100+
101+
86102
class AsyncEthModuleTest:
87103
@pytest.mark.asyncio
88104
async def test_eth_gas_price(self, async_w3: "Web3") -> None:
@@ -897,15 +913,6 @@ async def test_async_eth_get_logs_with_logs(
897913
emitter_contract_address: ChecksumAddress,
898914
txn_hash_with_log: HexStr,
899915
) -> None:
900-
def assert_contains_log(result: Sequence[LogReceipt]) -> None:
901-
assert len(result) == 1
902-
log_entry = result[0]
903-
assert log_entry['blockNumber'] == block_with_txn_with_log['number']
904-
assert log_entry['blockHash'] == block_with_txn_with_log['hash']
905-
assert log_entry['logIndex'] == 0
906-
assert is_same_address(log_entry['address'], emitter_contract_address)
907-
assert log_entry['transactionIndex'] == 0
908-
assert log_entry['transactionHash'] == HexBytes(txn_hash_with_log)
909916

910917
# Test with block range
911918

@@ -915,14 +922,24 @@ def assert_contains_log(result: Sequence[LogReceipt]) -> None:
915922
"toBlock": block_with_txn_with_log['number'],
916923
}
917924
result = await async_w3.eth.get_logs(filter_params) # type: ignore
918-
assert_contains_log(result)
925+
_assert_contains_log(
926+
result,
927+
block_with_txn_with_log,
928+
emitter_contract_address,
929+
txn_hash_with_log
930+
)
919931

920932
# specify only `from_block`. by default `to_block` should be 'latest'
921933
filter_params = {
922934
"fromBlock": BlockNumber(0),
923935
}
924936
result = await async_w3.eth.get_logs(filter_params) # type: ignore
925-
assert_contains_log(result)
937+
_assert_contains_log(
938+
result,
939+
block_with_txn_with_log,
940+
emitter_contract_address,
941+
txn_hash_with_log
942+
)
926943

927944
# Test with `address`
928945

@@ -940,15 +957,6 @@ async def test_async_eth_get_logs_with_logs_topic_args(
940957
emitter_contract_address: ChecksumAddress,
941958
txn_hash_with_log: HexStr,
942959
) -> None:
943-
def assert_contains_log(result: Sequence[LogReceipt]) -> None:
944-
assert len(result) == 1
945-
log_entry = result[0]
946-
assert log_entry['blockNumber'] == block_with_txn_with_log['number']
947-
assert log_entry['blockHash'] == block_with_txn_with_log['hash']
948-
assert log_entry['logIndex'] == 0
949-
assert is_same_address(log_entry['address'], emitter_contract_address)
950-
assert log_entry['transactionIndex'] == 0
951-
assert log_entry['transactionHash'] == HexBytes(txn_hash_with_log)
952960

953961
# Test with None event sig
954962

@@ -960,7 +968,12 @@ def assert_contains_log(result: Sequence[LogReceipt]) -> None:
960968
}
961969

962970
result = await async_w3.eth.get_logs(filter_params) # type: ignore
963-
assert_contains_log(result)
971+
_assert_contains_log(
972+
result,
973+
block_with_txn_with_log,
974+
emitter_contract_address,
975+
txn_hash_with_log
976+
)
964977

965978
# Test with None indexed arg
966979
filter_params = {
@@ -970,7 +983,12 @@ def assert_contains_log(result: Sequence[LogReceipt]) -> None:
970983
None],
971984
}
972985
result = await async_w3.eth.get_logs(filter_params) # type: ignore
973-
assert_contains_log(result)
986+
_assert_contains_log(
987+
result,
988+
block_with_txn_with_log,
989+
emitter_contract_address,
990+
txn_hash_with_log
991+
)
974992

975993
@pytest.mark.asyncio
976994
async def test_async_eth_get_logs_with_logs_none_topic_args(self, async_w3: "Web3") -> None:
@@ -2853,15 +2871,6 @@ def test_eth_get_logs_with_logs(
28532871
emitter_contract_address: ChecksumAddress,
28542872
txn_hash_with_log: HexStr,
28552873
) -> None:
2856-
def assert_contains_log(result: Sequence[LogReceipt]) -> None:
2857-
assert len(result) == 1
2858-
log_entry = result[0]
2859-
assert log_entry['blockNumber'] == block_with_txn_with_log['number']
2860-
assert log_entry['blockHash'] == block_with_txn_with_log['hash']
2861-
assert log_entry['logIndex'] == 0
2862-
assert is_same_address(log_entry['address'], emitter_contract_address)
2863-
assert log_entry['transactionIndex'] == 0
2864-
assert log_entry['transactionHash'] == HexBytes(txn_hash_with_log)
28652874

28662875
# Test with block range
28672876

@@ -2871,14 +2880,24 @@ def assert_contains_log(result: Sequence[LogReceipt]) -> None:
28712880
"toBlock": block_with_txn_with_log['number'],
28722881
}
28732882
result = web3.eth.get_logs(filter_params)
2874-
assert_contains_log(result)
2883+
_assert_contains_log(
2884+
result,
2885+
block_with_txn_with_log,
2886+
emitter_contract_address,
2887+
txn_hash_with_log
2888+
)
28752889

28762890
# specify only `from_block`. by default `to_block` should be 'latest'
28772891
filter_params = {
28782892
"fromBlock": BlockNumber(0),
28792893
}
28802894
result = web3.eth.get_logs(filter_params)
2881-
assert_contains_log(result)
2895+
_assert_contains_log(
2896+
result,
2897+
block_with_txn_with_log,
2898+
emitter_contract_address,
2899+
txn_hash_with_log
2900+
)
28822901

28832902
# Test with `address`
28842903

@@ -2895,15 +2914,6 @@ def test_eth_get_logs_with_logs_topic_args(
28952914
emitter_contract_address: ChecksumAddress,
28962915
txn_hash_with_log: HexStr,
28972916
) -> None:
2898-
def assert_contains_log(result: Sequence[LogReceipt]) -> None:
2899-
assert len(result) == 1
2900-
log_entry = result[0]
2901-
assert log_entry['blockNumber'] == block_with_txn_with_log['number']
2902-
assert log_entry['blockHash'] == block_with_txn_with_log['hash']
2903-
assert log_entry['logIndex'] == 0
2904-
assert is_same_address(log_entry['address'], emitter_contract_address)
2905-
assert log_entry['transactionIndex'] == 0
2906-
assert log_entry['transactionHash'] == HexBytes(txn_hash_with_log)
29072917

29082918
# Test with None event sig
29092919

@@ -2915,7 +2925,12 @@ def assert_contains_log(result: Sequence[LogReceipt]) -> None:
29152925
}
29162926

29172927
result = web3.eth.get_logs(filter_params)
2918-
assert_contains_log(result)
2928+
_assert_contains_log(
2929+
result,
2930+
block_with_txn_with_log,
2931+
emitter_contract_address,
2932+
txn_hash_with_log
2933+
)
29192934

29202935
# Test with None indexed arg
29212936
filter_params = {
@@ -2925,7 +2940,12 @@ def assert_contains_log(result: Sequence[LogReceipt]) -> None:
29252940
None],
29262941
}
29272942
result = web3.eth.get_logs(filter_params)
2928-
assert_contains_log(result)
2943+
_assert_contains_log(
2944+
result,
2945+
block_with_txn_with_log,
2946+
emitter_contract_address,
2947+
txn_hash_with_log
2948+
)
29292949

29302950
def test_eth_get_logs_with_logs_none_topic_args(self, web3: "Web3") -> None:
29312951
# Test with None overflowing

0 commit comments

Comments
 (0)