Skip to content

Commit

Permalink
test: show we can use namedtuples and web3 namedtuples (ApeWorX#2171)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Jul 8, 2024
1 parent b6bdb8d commit c172e9d
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion tests/functional/test_contract_instance.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import re
from collections import namedtuple

import pytest
from eth_pydantic_types import HexBytes
from eth_utils import is_checksum_address, to_hex
from ethpm_types import BaseModel, ContractType
from web3._utils.abi import recursive_dict_to_namedtuple

from ape.api import TransactionAPI
from ape.contracts import ContractInstance
Expand Down Expand Up @@ -220,7 +222,7 @@ def test_repr(vyper_contract_instance):
)


def test_structs(contract_instance, owner, chain, mystruct_c):
def test_structs_output(contract_instance, owner, chain, mystruct_c):
actual = contract_instance.getStruct()
actual_sender, actual_prev_block, actual_c = actual
tx_hash = chain.blocks[-2].hash
Expand Down Expand Up @@ -716,6 +718,29 @@ def test_dict_as_struct_input(contract_instance, owner):
assert contract_instance.setStruct(data) is None


def test_tuple_as_struct_input(contract_instance, owner):
# NOTE: Also showing extra keys like "extra_key" don't matter and are ignored.
data = (owner, HexBytes(123), 999, "GES_IGNORED")
assert contract_instance.setStruct(data) is None


def test_named_tuple_as_struct_input(contract_instance, owner):
# NOTE: Also showing extra keys like "extra_key" don't matter and are ignored.
values = {"a": AddressType, "b": HexBytes, "c": int, "extra_key": int}
MyStruct = namedtuple("MyStruct", values) # type: ignore
data = MyStruct(owner, HexBytes(123), 999, 0) # type: ignore
assert contract_instance.setStruct(data) is None


def test_web3_named_tuple_as_struct_input(solidity_contract_instance, owner):
"""
Show we integrate nicely with web3 contracts notion of namedtuples.
"""
data = {"a": solidity_contract_instance.address, "b": HexBytes(123), "c": 321}
w3_named_tuple = recursive_dict_to_namedtuple(data)
assert solidity_contract_instance.setStruct(w3_named_tuple) is None


@pytest.mark.parametrize("sequence_type", (list, tuple))
def test_obj_list_as_struct_array_input(contract_instance, data_object, sequence_type):
parameter = sequence_type([data_object, data_object])
Expand Down

0 comments on commit c172e9d

Please sign in to comment.