Reimplement tests for stdlib unittest

This commit is contained in:
lash 2023-03-26 06:45:20 +01:00
parent 5a81927fa1
commit faa9e71bac
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
4 changed files with 68 additions and 52 deletions

View File

@ -50,7 +50,7 @@ class Registry(TxFactory):
enc = ABIContractEncoder() enc = ABIContractEncoder()
enc.method('identifier') enc.method('identifier')
enc.typ(ABIContractType.UINT256) enc.typ(ABIContractType.UINT256)
enc.bytes32(idx) enc.uint256(idx)
data = add_0x(enc.encode()) data = add_0x(enc.encode())
tx = self.template(sender_address, contract_address) tx = self.template(sender_address, contract_address)
tx = self.set_code(tx, data) tx = self.set_code(tx, data)
@ -60,7 +60,7 @@ class Registry(TxFactory):
def identifier_count(self, contract_address, sender_address=ZERO_ADDRESS, id_generator=None): def identifier_count(self, contract_address, sender_address=ZERO_ADDRESS, id_generator=None):
return self.call_noarg('identifierCount', contract_address, sender_address=ZERO_ADDRESS, id_generator=None) return self.call_noarg('identifierCount', contract_address, sender_address=sender_address, id_generator=id_generator)
@classmethod @classmethod

View File

@ -94,6 +94,8 @@ class ContractRegistry(Registry):
def set(self, contract_address, sender_address, identifier_string, address): def set(self, contract_address, sender_address, identifier_string, address):
if len(identifier_string) > 32:
raise ValueError('String too long')
enc = ABIContractEncoder() enc = ABIContractEncoder()
enc.method('set') enc.method('set')
enc.typ(ABIContractType.BYTES32) enc.typ(ABIContractType.BYTES32)

View File

@ -1,3 +1,4 @@
pytest==6.0.1 pytest==6.0.1
eth-tester==0.5.0b3 eth-tester==0.5.0b3
py-evm==0.3.0a20 py-evm==0.3.0a20
eth-erc20~=0.7.2

View File

@ -1,73 +1,86 @@
# standard imports # standard imports
import os import os
import unittest
import json
import logging
import hashlib
# external imports # external imports
import logging from chainlib.eth.unittest.ethtester import EthTesterCase
import pytest
from chainlib.eth.tx import (
receipt,
transaction,
)
from chainlib.connection import RPCConnection
from chainlib.eth.contract import ( from chainlib.eth.contract import (
ABIContractEncoder, ABIContractEncoder,
ABIContractType, ABIContractType,
abi_decode_single,
) )
from chainlib.eth.nonce import RPCNonceOracle from chainlib.eth.nonce import RPCNonceOracle
from chainlib.eth.address import to_checksum_address from chainlib.eth.tx import receipt
from giftable_erc20_token import GiftableToken
from hexathon import ( from hexathon import (
add_0x, add_0x,
strip_0x, strip_0x,
) same as same_hex,
)
# local imports # local imports
from eth_contract_registry import Registry
from eth_contract_registry.registry import ContractRegistry from eth_contract_registry.registry import ContractRegistry
from eth_contract_registry.encoding import from_identifier_hex from eth_contract_registry import Registry
from eth_contract_registry.pytest.fixtures_registry import valid_identifiers
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger() logg = logging.getLogger()
valid_identifiers += [
'FooContract',
]
def test_set( class TestContractRegistry(EthTesterCase):
default_chain_spec,
registry,
eth_accounts,
eth_rpc,
eth_signer,
roles,
):
addr_registry = to_checksum_address(os.urandom(20).hex()) def setUp(self):
addr_foo = to_checksum_address(os.urandom(20).hex()) super(TestContractRegistry, self).setUp()
bogus_hash = add_0x(os.urandom(32).hex()) self.registry_ids = ['FOo', 'Bar', 'baz', 'XYZZY']
nonce_oracle = RPCNonceOracle(roles['CONTRACT_DEPLOYER'], eth_rpc) nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
builder = ContractRegistry(default_chain_spec, signer=eth_signer, nonce_oracle=nonce_oracle) c = ContractRegistry(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
(tx_hash_hex, o) = c.constructor(self.accounts[0], self.registry_ids)
self.rpc.do(o)
o = receipt(tx_hash_hex)
rcpt = self.rpc.do(o)
self.assertEqual(rcpt['status'], 1)
self.address = rcpt['contract_address']
logg.info('registry published to ' + self.address)
o = builder.address_of(registry, 'ContractRegistry', sender_address=eth_accounts[0])
r = eth_rpc.do(o)
r = abi_decode_single(ABIContractType.ADDRESS, r)
assert r == strip_0x(registry)
(tx_hash_hex, o) = builder.set(registry, roles['CONTRACT_DEPLOYER'], 'ContractRegistry', addr_registry) def test_retrieve(self):
r = eth_rpc.do(o) nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
o = receipt(r) c = ContractRegistry(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
rcpt = eth_rpc.do(o) (tx_hash_hex, o) = c.set(self.address, self.accounts[0], 'FOO', self.address)
assert rcpt['status'] == 0 r = self.rpc.do(o)
o = receipt(tx_hash_hex)
r = self.rpc.do(o)
self.assertEqual(r['status'], 0)
(tx_hash_hex, o) = builder.set(registry, roles['CONTRACT_DEPLOYER'], 'FooContract', addr_foo) (tx_hash_hex, o) = c.set(self.address, self.accounts[0], 'FOo', self.address)
r = eth_rpc.do(o) r = self.rpc.do(o)
o = receipt(r) o = receipt(tx_hash_hex)
rcpt = eth_rpc.do(o) r = self.rpc.do(o)
assert rcpt['status'] == 1 self.assertEqual(r['status'], 1)
builder = Registry(default_chain_spec) c = ContractRegistry(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
o = builder.address_of(registry, 'FooContract', sender_address=eth_accounts[0]) o = c.address_of(self.address, 'FOo', sender_address=self.accounts[0])
r = eth_rpc.do(o) r = self.rpc.do(o)
r = abi_decode_single(ABIContractType.ADDRESS, r) self.assertTrue(same_hex(strip_0x(r)[24:], self.address))
def test_identifiers(self):
c = Registry(self.chain_spec)
o = c.identifier_count(self.address, sender_address=self.accounts[0])
r = self.rpc.do(o)
self.assertEqual(int(r, 16), 4)
for i in range(4):
o = c.identifier(self.address, i, sender_address=self.accounts[0])
r = self.rpc.do(o)
r = bytes.fromhex(strip_0x(r))
r = r.strip(b'\x00')
s = r.decode('utf-8')
self.assertEqual(s, self.registry_ids[i])
if __name__ == '__main__':
unittest.main()