def test_component_configuration_load_file_not_found(): """Test Component.load when a file is not found.""" with mock.patch("aea.configurations.loader.open_file", side_effect=FileNotFoundError): with pytest.raises(FileNotFoundError): load_component_configuration(ComponentType.PROTOCOL, mock.MagicMock(spec=Path))
def _fingerprint_protocol(name: str): """Fingerprint the generated (and modified) protocol.""" log(f"Fingerprint the generated (and modified) protocol '{name}'") protocol_config = load_component_configuration(ComponentType.PROTOCOL, Path("protocols", name), skip_consistency_check=True) run_aea("fingerprint", "protocol", str(protocol_config.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 = 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 from_dir( cls, directory: str, identity: Identity, crypto_store: CryptoStore, data_dir: str, **kwargs: Any, ) -> "Connection": """ Load the connection from a directory. :param directory: the directory to the connection package. :param identity: the identity object. :param crypto_store: object to access the connection crypto objects. :param data_dir: the assets directory. :return: the connection object. """ configuration = cast( ConnectionConfig, load_component_configuration(ComponentType.CONNECTION, Path(directory)), ) configuration.directory = Path(directory) return Connection.from_config( configuration, identity, crypto_store, data_dir, **kwargs )
def test_load_component_failed_cause_package_not_found(self): """Test package not found in import.""" self.add_item("skill", "fetchai/echo:latest", local=True) with cd(self._get_cwd()): echo_dir = "./vendor/fetchai/skills/echo" handlers_file = Path(echo_dir) / "handlers.py" assert handlers_file.exists() file_data = handlers_file.read_text() file_data = file_data.replace( "from packages.fetchai.protocols.default", "from packages.fetchai.protocols.not_exist_protocol", ) handlers_file.write_text(file_data) with cd("./vendor/fetchai"): self.run_cli_command("fingerprint", "skill", "fetchai/echo:0.14.0") agent_context = Mock() agent_context.agent_name = self.agent_name configuration = cast( SkillConfig, load_component_configuration(ComponentType.SKILL, Path(echo_dir)), ) configuration.directory = Path(echo_dir) with pytest.raises( ClickException, match= r"Package loading error: An error occurred while loading skill fetchai/echo:.*\nTraceback", ) as e: self.run_cli_command("run", cwd=self._get_cwd()) assert "No AEA package found with author name" in str(e) assert "not_exist_protocol" in str(e)
def assert_dependency_updated(self, item_type: ComponentType, package_name: str, expected: Set[PublicId]): """Assert dependency is updated.""" package_path = Path(self._get_cwd(), item_type.to_plural(), package_name) component_config = load_component_configuration( item_type, package_path) assert component_config.protocols == expected # type: ignore
def _bump_protocol_specification_id_if_needed(package_path: Path) -> None: """ Check if protocol specification id needs to be bumped. Workflow: - extract protocol specification file from README - download latest protocol id and extract its protocol specification as above - if different, bump protocol specification version, else don't. :param package_path: path to the protocol package. :return: None """ # extract protocol specification file from README current_specification_content = _get_protocol_specification_from_readme( package_path) # download latest protocol id and extract its protocol specification as above configuration: ProtocolConfig = cast( ProtocolConfig, load_component_configuration(ComponentType.PROTOCOL, package_path), ) temp_directory = Path(tempfile.mkdtemp()) download_package(configuration.package_id, str(temp_directory)) downloaded_package_directory = temp_directory / configuration.name old_specification_content = _get_protocol_specification_from_readme( downloaded_package_directory) old_configuration: ProtocolConfig = cast( ProtocolConfig, load_component_configuration(ComponentType.PROTOCOL, downloaded_package_directory), ) # if different, bump protocol specification version, else don't. public_id_version_is_newer = ( old_configuration.public_id.package_version # type: ignore <= configuration.public_id.package_version) content_is_different = current_specification_content != old_specification_content if public_id_version_is_newer and content_is_different: log(f"Bumping protocol specification id from '{old_configuration.protocol_specification_id}' to '{configuration.protocol_specification_id}'" ) _bump_protocol_specification_id(package_path, configuration) return log("Protocol specification id not bumped - content is not different, or version is not newer." )
def test_username_is_correct(self): """Test that the author name in the ejected component configuration is updated correctly.""" package_path = Path(self.current_agent_context, "skills", ERROR_PUBLIC_ID.name) assert ( package_path.exists() ), f"Expected ejected package in '{package_path}', but not found." component_configuration = load_component_configuration( ComponentType.SKILL, package_path ) assert component_configuration.author == self.EXPECTED_AUTHOR
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 from_dir(cls, directory: str, **kwargs) -> "Protocol": """ Load the protocol from a directory. :param directory: the directory to the skill package. :return: the protocol object. """ configuration = cast( ProtocolConfig, load_component_configuration(ComponentType.PROTOCOL, Path(directory)), ) configuration.directory = Path(directory) return Protocol.from_config(configuration, **kwargs)
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_username_is_correct(self): """Test that the author name in the ejected component configuration is updated correctly.""" package_path = Path( self.current_agent_context, "protocols", DefaultMessage.protocol_id.name ) assert ( package_path.exists() ), f"Expected ejected package in '{package_path}', but not found." component_configuration = load_component_configuration( ComponentType.PROTOCOL, package_path ) assert component_configuration.author == self.EXPECTED_AUTHOR assert component_configuration.name == DefaultMessage.protocol_id.name assert component_configuration.version == DEFAULT_VERSION
def from_dir(cls, directory: str, **kwargs) -> "Contract": """ Load the protocol from a directory. :param directory: the directory to the skill package. :return: the contract object. """ configuration = cast( ContractConfig, load_component_configuration(ComponentType.CONTRACT, Path(directory)), ) configuration.directory = Path(directory) return Contract.from_config(configuration, **kwargs)
def from_dir(cls, directory: str, agent_context: AgentContext, **kwargs) -> "Skill": """ Load the skill from a directory. :param directory: the directory to the skill package. :param agent_context: the skill context :return: the skill object. """ configuration = cast( SkillConfig, load_component_configuration(ComponentType.SKILL, Path(directory)), ) configuration.directory = Path(directory) return Skill.from_config(configuration, agent_context, **kwargs)
def test_from_config_exception_class(): """Test Connection.from_config with exception""" dummy_connection_dir = os.path.join(CUR_PATH, "data", "dummy_connection") configuration = cast( ConnectionConfig, load_component_configuration(ComponentType.CONNECTION, Path(dummy_connection_dir)), ) configuration.directory = Path(dummy_connection_dir) configuration.class_name = "WrongName" identity = MagicMock() identity.name = "agent_name" crypto_store = MagicMock() data_dir = MagicMock() with pytest.raises(AEAComponentLoadException, match="Connection class"): Connection.from_config(configuration, identity, crypto_store, data_dir)
def assert_dependency_updated( self, item_type: ComponentType, package_name: str, package_type: str, expected: Set[PublicId], ): """Assert dependency is updated.""" package_path = Path(self._get_cwd(), item_type.to_plural(), package_name) component_config = load_component_configuration(item_type, package_path) assert hasattr(component_config, package_type), "Test is not well-written." assert getattr(component_config, package_type) == expected # type: ignore expected_version_range = compute_specifier_from_version( get_current_aea_version() ) assert component_config.aea_version == expected_version_range
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_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 load_component_configuration( self, component_id: ComponentId, skip_consistency_check: bool = True, ) -> ComponentConfiguration: """ Load component configuration from the project directory. :param component_id: Id of the component to load config for. :param skip_consistency_check: bool. :return: ComponentConfiguration """ path = find_component_directory_from_component_id( aea_project_directory=Path(self.aea_project_directory), component_id=component_id, ) return load_component_configuration( component_type=component_id.component_type, directory=path, skip_consistency_check=skip_consistency_check, )