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_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 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 _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_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)