Skip to content

Commit

Permalink
Refactor fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
loredanacirstea committed Sep 19, 2018
1 parent ffd5071 commit a38c633
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 77 deletions.
69 changes: 27 additions & 42 deletions raiden_contracts/tests/fixtures/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,40 +41,12 @@ def fn(
return fn


@pytest.fixture
def deploy_contract(revert_chain, deploy_contract_txhash):
"""Returns a function that deploys a compiled contract"""
def fn(
web3,
deployer_address,
abi,
bytecode,
args,
bytecode_runtime,
):
txhash = deploy_contract_txhash(
web3,
deployer_address,
abi,
bytecode,
args,
bytecode_runtime,
)

contract = web3.eth.contract(abi=abi, bytecode=bytecode)

contract_address = web3.eth.getTransactionReceipt(txhash).contractAddress
web3.testing.mine(1)

return contract(contract_address), txhash
return fn


@pytest.fixture
def deploy_tester_contract(
web3,
contracts_manager,
deploy_contract,
revert_chain,
deploy_contract_txhash,
contract_deployer_address,
wait_for_transaction,
get_random_address,
Expand All @@ -83,19 +55,32 @@ def deploy_tester_contract(
using contract manager to compile the bytecode and get the ABI"""
def f(contract_name, libs=None, args=None):
json_contract = contracts_manager.get_contract(contract_name)
abi = json_contract['abi']
bytecode = json_contract['bin']

if isinstance(libs, dict):
json_contract['bin'] = link_code(json_contract['bin'], libs)
json_contract['bin-runtime'] = link_code(json_contract['bin-runtime'], libs)
if isinstance(libs, dict) and len(libs.keys()) > 0:
bytecode = link_code(bytecode, libs)
bytecode_runtime = link_code(json_contract['bin-runtime'], libs)
txhash = deploy_contract_txhash(
web3,
contract_deployer_address,
abi,
bytecode,
args,
bytecode_runtime,
)
else:
json_contract['bin-runtime'] = None
txhash = deploy_contract_txhash(
web3,
contract_deployer_address,
abi,
bytecode,
args,
)

return deploy_contract(
web3,
contract_deployer_address,
json_contract['abi'],
json_contract['bin'],
args,
bytecode_runtime=json_contract['bin-runtime'],
)
contract = web3.eth.contract(abi=abi, bytecode=bytecode)
contract_address = web3.eth.getTransactionReceipt(txhash).contractAddress
web3.testing.mine(1)

return contract(contract_address), txhash
return f
20 changes: 8 additions & 12 deletions raiden_contracts/tests/fixtures/test_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
@pytest.fixture()
def token_network_test_storage(
deploy_tester_contract,
token_network_utils_library,
token_network_libs,
web3,
custom_token,
secret_registry_contract,
):
return deploy_tester_contract(
'TokenNetworkInternalStorageTest',
# {'TokenNetworkUtils': token_network_utils_library.address},
{'/Users/loredana/ETH/raiden-contracts': token_network_utils_library.address},
token_network_libs,
[
custom_token.address,
secret_registry_contract.address,
Expand All @@ -31,15 +30,14 @@ def token_network_test_storage(
@pytest.fixture()
def token_network_test_signatures(
deploy_tester_contract,
token_network_utils_library,
token_network_libs,
web3,
custom_token,
secret_registry_contract,
):
return deploy_tester_contract(
'TokenNetworkSignatureTest',
# {'TokenNetworkUtils': token_network_utils_library.address},
{'/Users/loredana/ETH/raiden-contracts': token_network_utils_library.address},
token_network_libs,
[
custom_token.address,
secret_registry_contract.address,
Expand All @@ -53,15 +51,14 @@ def token_network_test_signatures(
@pytest.fixture()
def token_network_test_utils(
deploy_tester_contract,
token_network_utils_library,
token_network_libs,
web3,
custom_token,
secret_registry_contract,
):
return deploy_tester_contract(
'TokenNetworkUtilsTest',
# {'TokenNetworkUtils': token_network_utils_library.address},
{'/Users/loredana/ETH/raiden-contracts': token_network_utils_library.address},
token_network_libs,
[
custom_token.address,
secret_registry_contract.address,
Expand All @@ -73,10 +70,9 @@ def token_network_test_utils(


@pytest.fixture
def signature_test_contract(deploy_tester_contract, token_network_utils_library):
def signature_test_contract(deploy_tester_contract, token_network_libs):
return deploy_tester_contract(
'SignatureVerifyTest',
# {'TokenNetworkUtils': token_network_utils_library.address},
{'/Users/loredana/ETH/raiden-contracts': token_network_utils_library.address},
token_network_libs,
[],
)[0]
2 changes: 1 addition & 1 deletion raiden_contracts/tests/fixtures/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def custom_token_no_decimals(deploy_tester_contract, custom_token_no_decimals_pa
@pytest.fixture()
def human_standard_token(deploy_token_contract, custom_token_params):
"""Deploy HumanStandardToken contract"""
return deploy_token_contract(*custom_token_params)[0]
return deploy_token_contract(*custom_token_params)


@pytest.fixture
Expand Down
7 changes: 5 additions & 2 deletions raiden_contracts/tests/fixtures/token_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@


@pytest.fixture
def get_token_network(web3, deploy_tester_contract):
def get_token_network(web3, deploy_tester_contract, token_network_utils_library):
"""Deploy a token network as a separate contract (registry is not used)"""
def get(arguments):
return deploy_tester_contract(
CONTRACT_TOKEN_NETWORK,
{},
{
'/Users/loredana/ETH/raiden-contracts': token_network_utils_library.address,
'/home/travis/build/raiden-network/ra': token_network_utils_library.address,
},
arguments,
)[0]
return get
Expand Down
36 changes: 24 additions & 12 deletions raiden_contracts/tests/fixtures/token_network_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,50 @@
)
from web3.contract import get_event_data
from eth_utils import is_address
from raiden_contracts.contract_manager import CONTRACTS_SOURCE_DIRS


@pytest.fixture
def token_network_utils_library(deploy_tester_contract, web3):
"""Deployed TokenNetworkUtils library"""
return deploy_tester_contract(LIBRARY_TOKEN_NETWORK_UTILS)[0]


@pytest.fixture
def token_network_libs(token_network_utils_library):
"""Deployed TokenNetworkUtils library"""
# FIXME
# libraries should look lile: {'TokenNetworkUtils': token_network_utils_library.address}
# But solc assigns only 36 characters to the library name
# In our case, with remappings, the library name contains the full path to the library file
# Therefore, we get the same name for all libraries, containing path of the file path
libs = {}
libs[str(CONTRACTS_SOURCE_DIRS['raiden'])[:36]] = token_network_utils_library.address
return libs


@pytest.fixture()
def get_token_network_registry(deploy_tester_contract, token_network_utils_library):
def get_token_network_registry(deploy_tester_contract, token_network_libs):
def get(arguments, transaction=None):
return deploy_tester_contract(
CONTRACT_TOKEN_NETWORK_REGISTRY,
# {'TokenNetworkUtils': token_network_utils_library.address},
{'/Users/loredana/ETH/raiden-contracts': token_network_utils_library.address},
token_network_libs,
arguments,
)[0]
return get


@pytest.fixture
def token_network_utils_library(deploy_tester_contract, web3):
"""Deployed TokenNetworkUtils library"""
return deploy_tester_contract(LIBRARY_TOKEN_NETWORK_UTILS)[0]


@pytest.fixture
def token_network_registry_contract(
deploy_tester_contract,
token_network_utils_library,
token_network_libs,
secret_registry_contract,
web3,
):
"""Deployed TokenNetworkRegistry contract"""
return deploy_tester_contract(
CONTRACT_TOKEN_NETWORK_REGISTRY,
# {'TokenNetworkUtils': token_network_utils_library.address},
{'/Users/loredana/ETH/raiden-contracts': token_network_utils_library.address},
token_network_libs,
[
secret_registry_contract.address,
int(web3.version.network),
Expand Down
5 changes: 4 additions & 1 deletion raiden_contracts/tests/test_print_gas.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ def test_token_network_registry(
(token_network_utils_library, txn_hash) = deploy_tester_contract(LIBRARY_TOKEN_NETWORK_UTILS)
print_gas(txn_hash, LIBRARY_TOKEN_NETWORK_UTILS + ' DEPLOYMENT')

libs = {'/Users/loredana/ETH/raiden-contracts': token_network_utils_library.address}
libs = {
'/Users/loredana/ETH/raiden-contracts': token_network_utils_library.address,
'/home/travis/build/raiden-network/ra': token_network_utils_library.address,
}
(token_network_registry, txn_hash) = deploy_tester_contract(
CONTRACT_TOKEN_NETWORK_REGISTRY,
libs,
Expand Down
15 changes: 8 additions & 7 deletions raiden_contracts/tests/test_token_network_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ def test_constructor_call(
chain_id = int(web3.version.network)
settle_min = TEST_SETTLE_TIMEOUT_MIN
settle_max = TEST_SETTLE_TIMEOUT_MAX

get_token_network_registry([
secret_registry_contract.address,
chain_id,
settle_min,
settle_max,
])

with pytest.raises(TypeError):
get_token_network_registry([])
with pytest.raises(TypeError):
Expand Down Expand Up @@ -73,13 +81,6 @@ def test_constructor_call(
with pytest.raises(TransactionFailed):
get_token_network_registry([secret_registry_contract.address, 0, settle_max, settle_min])

get_token_network_registry([
secret_registry_contract.address,
chain_id,
settle_min,
settle_max,
])


def test_constructor_call_state(web3, get_token_network_registry, secret_registry_contract):
chain_id = int(web3.version.network)
Expand Down

0 comments on commit a38c633

Please sign in to comment.