nucypher/tests/blockchain/eth/interfaces/test_registry.py

58 lines
2.1 KiB
Python
Raw Normal View History

import pytest
2018-06-20 20:20:25 +00:00
from nucypher.blockchain.eth.interfaces import EthereumContractRegistry
2018-06-25 19:21:01 +00:00
def test_contract_registry(tempfile_path):
with pytest.raises(EthereumContractRegistry.RegistryError):
2018-06-25 19:21:01 +00:00
bad_registry = EthereumContractRegistry(registry_filepath='/fake/file/path/registry.json')
bad_registry.search(contract_address='0xdeadbeef')
# Tests everything is as it should be when initially created
2018-06-25 19:21:01 +00:00
test_registry = EthereumContractRegistry(registry_filepath=tempfile_path)
with pytest.raises(EthereumContractRegistry.RegistryError) as e:
_should_be_empty = test_registry.read()
assert 'Empty' in e
# Test contract enrollment and dump_chain
test_name = 'TestContract'
test_addr = '0xDEADBEEF'
test_abi = ['fake', 'data']
2018-06-25 19:21:01 +00:00
test_registry.enroll(contract_name=test_name,
contract_address=test_addr,
contract_abi=test_abi)
# Search by name...
2018-06-25 19:21:01 +00:00
contract_records = test_registry.search(contract_name=test_name)
assert len(contract_records) == 1, 'More than one record for {}'.format(test_name)
assert len(contract_records[0]) == 3, 'Registry record is the wrong length'
name, address, abi = contract_records[0]
assert name == test_name
assert address == test_addr
assert abi == test_abi
# ...or by address
2018-06-25 19:21:01 +00:00
contract_record = test_registry.search(contract_address=test_addr)
name, address, abi = contract_record
assert name == test_name
assert address == test_addr
assert abi == test_abi
# Check that searching for an unknown contract raises
2018-06-20 20:20:25 +00:00
with pytest.raises(EthereumContractRegistry.UnknownContract):
2018-06-25 19:21:01 +00:00
test_registry.search(contract_name='this does not exist')
2018-06-25 19:21:01 +00:00
current_dataset = test_registry.read()
# Corrupt the registry with a duplicate address
current_dataset.append([test_name, test_addr, test_abi])
2018-06-25 19:21:01 +00:00
test_registry._EthereumContractRegistry__write(current_dataset)
# Check that searching for an unknown contract raises
with pytest.raises(EthereumContractRegistry.IllegalRegistry):
2018-06-25 19:21:01 +00:00
test_registry.search(contract_address=test_addr)