def test_component_loading_instantiation_exception(component_configuration): """Test 'load_component_from_config' method when a generic "Exception" occurs.""" with mock.patch.object( Protocol, "from_config", side_effect=AEAInstantiationException("Generic exception"), ): with pytest.raises(AEAInstantiationException): load_component_from_config(component_configuration)
def test_component_loading_generic_exception(component_configuration): """Test 'load_component_from_config' method when a generic "Exception" occurs.""" with mock.patch.object(Protocol, "from_config", side_effect=Exception("Generic exception")): with pytest.raises( Exception, match="An error occurred while loading .*: Generic exception"): load_component_from_config(component_configuration)
def test_component_loading_module_not_found_error_framework_package( component_configuration, ): """Test 'load_component_from_config' method when a "ModuleNotFoundError" occurs for a framework-related import (starts with 'packages') but for some reason it doesn't contain the author name.""" with mock.patch.object( Protocol, "from_config", side_effect=ModuleNotFoundError("No module named 'packages'"), ): with pytest.raises(ModuleNotFoundError, match="No module named 'packages'"): load_component_from_config(component_configuration)
def test_component_loading_module_not_found_error_non_framework_package( component_configuration, ): """Test 'load_component_from_config' method when a "ModuleNotFoundError" occurs for a generic import path (non framework related.""" with mock.patch.object( Protocol, "from_config", side_effect=ModuleNotFoundError( "No module named 'generic.package'"), ): with pytest.raises(ModuleNotFoundError): load_component_from_config(component_configuration)
def test_component_loading_generic_module_not_found_error( component_configuration): """Test 'load_component_from_config' method when a generic "ModuleNotFoundError" occurs.""" with mock.patch.object( Protocol, "from_config", side_effect=ModuleNotFoundError( "An error occurred while loading .*: Generic error"), ): with pytest.raises(ModuleNotFoundError, match="Generic error"): load_component_from_config(component_configuration)
def test_component_loading_component_exception(component_configuration): """Test 'load_component_from_config' method when a generic "Exception" occurs.""" with mock.patch.object( Protocol, "from_config", side_effect=AEAComponentLoadException("Generic exception"), ): with pytest.raises( AEAPackageLoadingError, match= "Package loading error: An error occurred while loading protocol an_author/a_protocol:0.1.0: Generic exception", ): load_component_from_config(component_configuration)
def test_component_loading_module_not_found_error_framework_package_with_wrong_author( component_configuration, ): """Test 'load_component_from_config' method when a "ModuleNotFoundError" occurs for a framework-related import (starts with 'packages') with wrong author.""" with mock.patch.object( Protocol, "from_config", side_effect=ModuleNotFoundError( "No module named 'packages.some_author'"), ): with pytest.raises( AEAPackageLoadingError, match= "No module named packages.some_author; No AEA package found with author name 'some_author'", ): load_component_from_config(component_configuration)
def test_component_loading_module_not_found_error_framework_package_with_wrong_type( component_configuration, ): """Test 'load_component_from_config' method when a "ModuleNotFoundError" occurs for a framework-related import (starts with 'packages') with correct author but wrong type.""" with mock.patch.object( Protocol, "from_config", side_effect=ModuleNotFoundError( "No module named 'packages.some_author.some_type'"), ): with pytest.raises( AEAPackageLoadingError, match= r"No module named packages.some_author.some_type; 'some_type' is not a valid type name, choose one of \['protocols', 'connections', 'skills', 'contracts'\]", ): load_component_from_config(component_configuration)
def test_component_loading_module_not_found_error_framework_package_with_wrong_suffix( component_configuration, ): """Test 'load_component_from_config' method when a "ModuleNotFoundError" occurs for a framework-related import (starts with 'packages') with correct author and type but wrong suffix.""" with mock.patch.object( Protocol, "from_config", side_effect=ModuleNotFoundError( "No module named 'packages.some_author.protocols.some_name.some_subpackage'" ), ): with pytest.raises( AEAPackageLoadingError, match= "No module named packages.some_author.protocols.some_name.some_subpackage; The package 'packages/some_author' of type 'protocols' exists, but cannot find module 'some_subpackage'", ): load_component_from_config(component_configuration)
def test_component_loading_module_not_found_error_framework_package_with_wrong_author( component_configuration, ): """Test 'load_component_from_config' method when a "ModuleNotFoundError" occurs for a framework-related import (starts with 'packages') with wrong author.""" with mock.patch.object( Protocol, "from_config", side_effect=ModuleNotFoundError( "No module named 'packages.some_author'"), ): with pytest.raises( AEAPackageLoadingError, match= "An error occurred while loading protocol an_author/a_protocol:0.1.0:", ) as e: load_component_from_config(component_configuration) assert "some_type" in str(e)
def test_component_loading_module_not_found_error_framework_package_with_wrong_name( component_configuration, ): """Test 'load_component_from_config' method when a "ModuleNotFoundError" occurs for a framework-related import (starts with 'packages') with correct author and type but wrong name.""" with mock.patch.object( Protocol, "from_config", side_effect=ModuleNotFoundError( "No module named 'packages.some_author.protocols.some_name'"), ): with pytest.raises( AEAPackageLoadingError, match= "An error occurred while loading protocol an_author/a_protocol:0.1.0:", ) as e: load_component_from_config(component_configuration) assert ( " No AEA package found with author name 'some_author', type 'protocols', name 'some_name'" in e.value.args[0])
def _load_and_add_components( self, component_type: ComponentType, resources: Resources, agent_name: str, **kwargs, ) -> None: """ Load and add components added to the builder to a Resources instance. :param component_type: the component type for which :param resources: the resources object to populate. :param agent_name: the AEA name for logging purposes. :param kwargs: keyword argument to forward to the component loader. :return: None """ for configuration in self._package_dependency_manager.get_components_by_type( component_type ).values(): if configuration.is_abstract_component: load_aea_package(configuration) continue if configuration in self._component_instances[component_type].keys(): component = self._component_instances[component_type][configuration] if configuration.component_type != ComponentType.SKILL: component.logger = cast( logging.Logger, make_logger(configuration, agent_name) ) else: configuration = deepcopy(configuration) _logger = make_logger(configuration, agent_name) component = load_component_from_config( configuration, logger=_logger, **kwargs ) resources.add_component(component)