Skip to content

Commit

Permalink
Increases code coverage to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreMiras committed Nov 6, 2019
1 parent d31cebd commit 1a7da2e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
7 changes: 4 additions & 3 deletions pyetheroll/transaction_debugger.py
Expand Up @@ -13,9 +13,10 @@ def decode_contract_call(contract_abi: list, call_data: str):
call_data = call_data.lower().replace("0x", "")
call_data_bin = decode_hex(call_data)
method_signature = call_data_bin[:4]
for description in contract_abi:
if description.get("type") != "function":
continue
function_descriptions = filter(
lambda x: x.get("type") == "function", contract_abi
)
for description in function_descriptions:
if function_abi_to_4byte_selector(description) == method_signature:
method_name = description["name"]
arg_types = [item["type"] for item in description["inputs"]]
Expand Down
53 changes: 49 additions & 4 deletions tests/test_etheroll.py
Expand Up @@ -8,6 +8,7 @@
import eth_account
import pytest
from eth_account._utils.transactions import assert_valid_fields
from etherscan.accounts import Account as EtherscanAccount
from hexbytes.main import HexBytes

from pyetheroll.constants import ChainID
Expand All @@ -16,7 +17,22 @@

def patch_get_abi(abi_str):
return mock.patch(
"etherscan.contracts.Contract.get_abi", return_value=abi_str)
"etherscan.contracts.Contract.get_abi", return_value=abi_str
)


def patch_get_transaction_page(transactions=None):
return mock.patch(
"etherscan.accounts.Account.get_transaction_page",
return_value=transactions,
)


def patch_chain_etherscan_account_factory_create(return_value):
return mock.patch(
"pyetheroll.etheroll.ChainEtherscanAccountFactory.create",
return_value=return_value,
)


class TestEtheroll:
Expand Down Expand Up @@ -448,10 +464,9 @@ def test_get_last_bets_transactions(self):
address = "0x46044beaa1e985c67767e04de58181de5daaa00f"
page = 1
offset = 3
with mock.patch(
"etherscan.accounts.Account.get_transaction_page"
with patch_get_transaction_page(
transactions
) as m_get_transaction_page:
m_get_transaction_page.return_value = transactions
bets = etheroll.get_last_bets_transactions(
address=address, page=page, offset=offset
)
Expand Down Expand Up @@ -1086,3 +1101,33 @@ def test_get_balance(self):
expected_calls = [expected_call]
assert m_get.call_args_list == expected_calls
assert balance == 365003.28

def test_get_transaction_page(self):
"""Should use the address passed in parameter or the contract one."""
contract_abi = []
address = "0x46044beAa1E985C67767E04dE58181de5DAAA00F"
contract_address = "0x048717Ea892F23Fb0126F00640e2b18072efd9D2"
expected_transactions = mock.sentinel
m_ChainEtherscanAccount = mock.Mock(spec=EtherscanAccount)
m_ChainEtherscanAccount.return_value.get_transaction_page = mock.Mock(
return_value=expected_transactions
)
with patch_get_abi(
json.dumps(contract_abi)
), patch_chain_etherscan_account_factory_create(
m_ChainEtherscanAccount
):
etheroll = Etheroll(contract_address=contract_address)
# pulls from address passed in parameters
transactions = etheroll.get_transaction_page(address=address)
assert m_ChainEtherscanAccount.call_args_list == [
mock.call(address=address, api_key="YourApiKeyToken")
]
assert transactions == expected_transactions
m_ChainEtherscanAccount.reset_mock()
# or defaults to contract address
transactions = etheroll.get_transaction_page()
assert m_ChainEtherscanAccount.call_args_list == [
mock.call(address=contract_address, api_key="YourApiKeyToken")
]
assert transactions == expected_transactions
6 changes: 5 additions & 1 deletion tests/test_transaction_debugger.py
Expand Up @@ -273,7 +273,11 @@ def test_decode_contract_call(self):
json_abi = (
'[{"constant":false,"inputs":[{"name":"_to","type":"address"},{"na'
'me":"_value","type":"uint256"}],"name":"transfer","outputs":[{"na'
'me":"success","type":"bool"}],"payable":false,"type":"function"}]'
'me":"success","type":"bool"}],"payable":false,"type":"function"},'
'{"anonymous":false,"inputs":[{"indexed":true,"name":"BetID","type'
'":"bytes32"},{"indexed":true,"name":"PlayerAddress","type":"addre'
'ss"},{"indexed":true,"name":"RefundValue","type":"uint256"}],"nam'
'e":"LogRefund","type":"event"}]'
)
contract_abi = json.loads(json_abi)
call_data = (
Expand Down

0 comments on commit 1a7da2e

Please sign in to comment.