def dummy_contract(request): directory = Path(ROOT_DIR, "tests", "data", "dummy_contract") configuration = ComponentConfiguration.load(ComponentType.CONTRACT, directory) configuration._directory = directory configuration = cast(ContractConfig, configuration) if str(configuration.public_id) in contract_registry.specs: contract_registry.specs.pop(str(configuration.public_id)) # load into sys modules Contract.from_config(configuration) # load into registry path = Path(configuration.directory, configuration.path_to_contract_interface) with open(path, "r") as interface_file: contract_interface = json.load(interface_file) contract_registry.register( id_=str(configuration.public_id), entry_point= f"{configuration.prefix_import_path}.contract:{configuration.class_name}", class_kwargs={"contract_interface": contract_interface}, contract_config=configuration, ) contract = contract_registry.make(str(configuration.public_id)) yield contract contract_registry.specs.pop(str(configuration.public_id))
def set_contract(self): """Set contract.""" directory = Path(ROOT_DIR, "packages", "fetchai", "contracts", "erc1155") configuration = load_component_configuration(ComponentType.CONTRACT, directory) configuration._directory = directory configuration = cast(ContractConfig, configuration) if str(configuration.public_id) not in contract_registry.specs: # load contract into sys modules Contract.from_config(configuration) self.contract = contract_registry.make(str(configuration.public_id))
def test_from_config_and_registration(): """Tests the from config method and contract registry registration.""" directory = Path(ROOT_DIR, "tests", "data", "dummy_contract") configuration = ComponentConfiguration.load(ComponentType.CONTRACT, directory) configuration._directory = directory configuration = cast(ContractConfig, configuration) if str(configuration.public_id) in contract_registry.specs: contract_registry.specs.pop(str(configuration.public_id)) contract = Contract.from_config(configuration) assert contract is not None assert contract.contract_interface is None assert contract.configuration == configuration assert contract.id == configuration.public_id contract_registry.register( id_=str(configuration.public_id), entry_point=f"{configuration.prefix_import_path}.contract:{configuration.class_name}", class_kwargs={"contract_interface": configuration.contract_interfaces}, contract_config=configuration, ) contract = contract_registry.make(str(configuration.public_id)) assert contract is not None assert contract.configuration == configuration assert contract.contract_interface is not None
def test_from_dir(): """Tests the from dir and from config methods.""" contract = Contract.from_dir( os.path.join(ROOT_DIR, "tests", "data", "dummy_contract") ) assert contract is not None assert contract.contract_interface is None
def test_from_config_and_registration(): """Tests the from config method and contract registry registration.""" directory = Path(ROOT_DIR, "tests", "data", "dummy_contract") configuration = load_component_configuration(ComponentType.CONTRACT, directory) configuration._directory = directory configuration = cast(ContractConfig, configuration) if str(configuration.public_id) in contract_registry.specs: contract_registry.specs.pop(str(configuration.public_id)) contract = Contract.from_config(configuration) assert contract is not None assert contract.contract_interface is not None assert isinstance(contract.contract_interface, dict) assert contract.configuration == configuration assert contract.id == configuration.public_id # the contract is registered as side-effect assert str(contract.public_id) in contract_registry.specs try: contract_registry.specs.pop(str(configuration.public_id)) except Exception as e: logger.exception(e)
def test_get_all_contracts(self): """Test get all contracts.""" a_contract = Contract.from_dir( Path(ROOT_DIR, "packages", "fetchai", "contracts", "erc1155"), ) self.resources.add_component(a_contract) all_contracts = self.resources.get_all_contracts() assert len(all_contracts) == 1 # restore state self.resources.remove_contract(a_contract.public_id)
def test_add_remove_contract(self): """Test that the 'add contract' and 'remove contract' method work correctly.""" a_contract = Contract.from_dir( Path(ROOT_DIR, "packages", "fetchai", "contracts", "erc1155"), ) self.resources.add_component(a_contract) assert self.resources.get_contract(a_contract.public_id) == a_contract # restore state self.resources.remove_contract(a_contract.public_id) assert self.resources.get_contract(a_contract.public_id) is None
def erc1155_contract(): """ Instantiate an ERC1155 contract instance. As a side effect, register it to the registry, if not already registered. """ directory = Path(ROOT_DIR, "packages", "fetchai", "contracts", "erc1155") configuration = load_component_configuration(ComponentType.CONTRACT, directory) configuration._directory = directory configuration = cast(ContractConfig, configuration) if str(configuration.public_id) not in contract_registry.specs: # load contract into sys modules Contract.from_config(configuration) contract = contract_registry.make(str(configuration.public_id)) yield contract
def test_remove_contract(): """Test add/remove contract.""" builder = AEABuilder() builder.set_name("aea_1") builder.add_private_key("fetchai") contract = Contract.from_dir(contract_path) num_deps = len(builder._package_dependency_manager.all_dependencies) builder.add_component_instance(contract) assert len(builder._package_dependency_manager.all_dependencies) == num_deps + 1 builder.remove_contract(contract.public_id) assert len(builder._package_dependency_manager.all_dependencies) == num_deps
def _load_and_add_contracts(self) -> None: for configuration in self._package_dependency_manager.contracts.values(): configuration = cast(ContractConfig, configuration) try: contract = Contract.from_config(configuration) except Exception as e: raise Exception( "An error occurred while loading contract {}: {}".format( configuration.public_id, str(e) ) ) self._add_component_to_resources(contract)
def dummy_contract(request): """Dummy contract fixture.""" directory = Path(ROOT_DIR, "tests", "data", "dummy_contract") configuration = load_component_configuration(ComponentType.CONTRACT, directory) configuration._directory = directory configuration = cast(ContractConfig, configuration) if str(configuration.public_id) in contract_registry.specs: contract_registry.specs.pop(str(configuration.public_id)) # load into sys modules and register into contract registry contract = Contract.from_config(configuration) yield contract contract_registry.specs.pop(str(configuration.public_id))
def test_non_implemented_class_methods(): """Tests the non implemented class methods.""" with pytest.raises(NotImplementedError): Contract.get_raw_transaction("ledger_api", "contract_address") with pytest.raises(NotImplementedError): Contract.get_raw_message("ledger_api", "contract_address") with pytest.raises(NotImplementedError): Contract.get_state("ledger_api", "contract_address")
def setup_class(cls): """Set the tests up.""" cls.oldcwd = os.getcwd() cls.agent_name = "agent_dir_test" cls.t = tempfile.mkdtemp() cls.agent_folder = os.path.join(cls.t, cls.agent_name) shutil.copytree(os.path.join(CUR_PATH, "data", "dummy_aea"), cls.agent_folder) os.chdir(cls.agent_folder) contract = Contract.from_dir( str(Path(ROOT_DIR, "packages", "fetchai", "contracts", "erc1155")) ) cls.registry = AgentComponentRegistry() cls.patch = unittest.mock.patch.object(cls.registry.logger, "exception") cls.mocked_logger = cls.patch.start() cls.registry.register(contract.component_id, cast(Contract, contract)) cls.expected_contract_ids = {ERC1155_PUBLIC_ID}
def test_from_config_negative(): """Tests the from config method raises.""" directory = Path(ROOT_DIR, "tests", "data", "dummy_contract") configuration = load_component_configuration(ComponentType.CONTRACT, directory) configuration._directory = directory configuration = cast(ContractConfig, configuration) if str(configuration.public_id) in contract_registry.specs: contract_registry.specs.pop(str(configuration.public_id)) configuration.class_name = "WrongName" with pytest.raises(AEAComponentLoadException): _ = Contract.from_config(configuration) try: contract_registry.specs.pop(str(configuration.public_id)) except Exception as e: logger.exception(e)
def setup_class(cls): """Set the tests up.""" cls.patch = unittest.mock.patch.object(aea.registries.base.logger, "exception") cls.mocked_logger = cls.patch.__enter__() cls.oldcwd = os.getcwd() cls.agent_name = "agent_dir_test" cls.t = tempfile.mkdtemp() cls.agent_folder = os.path.join(cls.t, cls.agent_name) shutil.copytree(os.path.join(CUR_PATH, "data", "dummy_aea"), cls.agent_folder) os.chdir(cls.agent_folder) contract = Contract.from_dir( str(Path(ROOT_DIR, "packages", "fetchai", "contracts", "erc1155"))) cls.registry = ContractRegistry() cls.registry.register(contract.configuration.public_id, cast(Contract, contract)) cls.expected_contract_ids = { PublicId("fetchai", "erc1155", "0.1.0"), }