def test_load_protocol_specification_too_many_parts(): """Test 'load_protocol_specification' with more than three parts.""" with pytest.raises( ValueError, match="Incorrect number of Yaml documents in the protocol specification.", ): with mock.patch.object( yaml, "safe_load_all", return_value=[{}] * 4 ), mock.patch("builtins.open"): load_protocol_specification("foo")
def test_load_protocol_specification_only_first_part(): """Test 'load_protocol_specification' with only the first part.""" valid_protocol_specification = dict( name="name", author="author", version="0.1.0", license="", aea_version="0.1.0", speech_acts={"example": {}}, ) with mock.patch.object( yaml, "safe_load_all", return_value=[valid_protocol_specification] ), mock.patch("builtins.open"), mock.patch("jsonschema.Draft4Validator.validate"): load_protocol_specification("foo")
def test_extract_negative_invalid_specification(self, mocked_validate): """Negative test the 'extract' method: invalid protocol specification""" protocol_specification = load_protocol_specification( PATH_TO_T_PROTOCOL_SPECIFICATION) with self.assertRaises(ProtocolSpecificationParseError) as cm: extract(protocol_specification) expected_msg = "Some error!" assert str(cm.exception) == expected_msg
def test_load_protocol_specification_only_first_part(): """Test 'load_protocol_specification' with only the first part.""" valid_protocol_specification = dict( name="name", author="author", version="0.1.0", license="", aea_version="0.1.0", speech_acts={"example": {}}, protocol_specification_id="test/test:0.1.0", description="some", ) with mock.patch.object( yaml, "safe_load_all", return_value=[valid_protocol_specification]), mock.patch( "aea.protocols.generator.common.open_file"), mock.patch.object( OwnDraft4Validator, "validate"): load_protocol_specification("foo")
def test_load_protocol_specification(self, ): """Test the 'load_protocol_specification' method""" spec = load_protocol_specification(PATH_TO_T_PROTOCOL_SPECIFICATION) assert spec.name == T_PROTOCOL_NAME assert spec.version == "0.1.0" assert spec.author == "fetchai" assert spec.license == "Apache-2.0" assert spec.aea_version == ">=0.10.0, <0.11.0" assert spec.description == "A protocol for testing purposes." assert spec.speech_acts is not None assert spec.protobuf_snippets is not None and spec.protobuf_snippets != ""
def test_extract_positive(self): """Positive test the 'extract' method.""" protocol_specification = load_protocol_specification( PATH_TO_T_PROTOCOL_SPECIFICATION) spec = extract(protocol_specification) assert spec.speech_acts == { "performative_ct": { "content_ct": "DataModel" }, "performative_pt": { "content_bytes": "bytes", "content_int": "int", "content_float": "float", "content_bool": "bool", "content_str": "str", }, "performative_pct": { "content_set_bytes": "FrozenSet[bytes]", "content_set_int": "FrozenSet[int]", "content_set_float": "FrozenSet[float]", "content_set_bool": "FrozenSet[bool]", "content_set_str": "FrozenSet[str]", "content_list_bytes": "Tuple[bytes, ...]", "content_list_int": "Tuple[int, ...]", "content_list_float": "Tuple[float, ...]", "content_list_bool": "Tuple[bool, ...]", "content_list_str": "Tuple[str, ...]", }, "performative_pmt": { "content_dict_int_bytes": "Dict[int, bytes]", "content_dict_int_int": "Dict[int, int]", "content_dict_int_float": "Dict[int, float]", "content_dict_int_bool": "Dict[int, bool]", "content_dict_int_str": "Dict[int, str]", "content_dict_bool_bytes": "Dict[bool, bytes]", "content_dict_bool_int": "Dict[bool, int]", "content_dict_bool_float": "Dict[bool, float]", "content_dict_bool_bool": "Dict[bool, bool]", "content_dict_bool_str": "Dict[bool, str]", "content_dict_str_bytes": "Dict[str, bytes]", "content_dict_str_int": "Dict[str, int]", "content_dict_str_float": "Dict[str, float]", "content_dict_str_bool": "Dict[str, bool]", "content_dict_str_str": "Dict[str, str]", }, "performative_mt": { "content_union_1": "Union[DataModel, bytes, int, float, bool, str, FrozenSet[int], Tuple[bool, ...], Dict[str, int]]", "content_union_2": "Union[FrozenSet[bytes], FrozenSet[int], FrozenSet[str], Tuple[float, ...], Tuple[bool, ...], Tuple[bytes, ...], Dict[str, int], Dict[int, float], Dict[bool, bytes]]", }, "performative_o": { "content_o_ct": "Optional[DataModel]", "content_o_bool": "Optional[bool]", "content_o_set_int": "Optional[FrozenSet[int]]", "content_o_list_bytes": "Optional[Tuple[bytes, ...]]", "content_o_dict_str_int": "Optional[Dict[str, int]]", }, "performative_empty_contents": {}, } assert spec.all_performatives == [ "performative_ct", "performative_empty_contents", "performative_mt", "performative_o", "performative_pct", "performative_pmt", "performative_pt", ] assert spec.all_unique_contents == { "content_ct": "DataModel", "content_bytes": "bytes", "content_int": "int", "content_float": "float", "content_bool": "bool", "content_str": "str", "content_set_bytes": "FrozenSet[bytes]", "content_set_int": "FrozenSet[int]", "content_set_float": "FrozenSet[float]", "content_set_bool": "FrozenSet[bool]", "content_set_str": "FrozenSet[str]", "content_list_bytes": "Tuple[bytes, ...]", "content_list_int": "Tuple[int, ...]", "content_list_float": "Tuple[float, ...]", "content_list_bool": "Tuple[bool, ...]", "content_list_str": "Tuple[str, ...]", "content_dict_int_bytes": "Dict[int, bytes]", "content_dict_int_int": "Dict[int, int]", "content_dict_int_float": "Dict[int, float]", "content_dict_int_bool": "Dict[int, bool]", "content_dict_int_str": "Dict[int, str]", "content_dict_bool_bytes": "Dict[bool, bytes]", "content_dict_bool_int": "Dict[bool, int]", "content_dict_bool_float": "Dict[bool, float]", "content_dict_bool_bool": "Dict[bool, bool]", "content_dict_bool_str": "Dict[bool, str]", "content_dict_str_bytes": "Dict[str, bytes]", "content_dict_str_int": "Dict[str, int]", "content_dict_str_float": "Dict[str, float]", "content_dict_str_bool": "Dict[str, bool]", "content_dict_str_str": "Dict[str, str]", "content_union_1": "Union[DataModel, bytes, int, float, bool, str, FrozenSet[int], Tuple[bool, ...], Dict[str, int]]", "content_union_2": "Union[FrozenSet[bytes], FrozenSet[int], FrozenSet[str], Tuple[float, ...], Tuple[bool, ...], Tuple[bytes, ...], Dict[str, int], Dict[int, float], Dict[bool, bytes]]", "content_o_ct": "Optional[DataModel]", "content_o_bool": "Optional[bool]", "content_o_set_int": "Optional[FrozenSet[int]]", "content_o_list_bytes": "Optional[Tuple[bytes, ...]]", "content_o_dict_str_int": "Optional[Dict[str, int]]", } assert spec.all_custom_types == ["DataModel"] assert spec.custom_custom_types == {"DataModel": "CustomDataModel"} assert spec.initial_performatives == [ "PERFORMATIVE_CT", "PERFORMATIVE_PT" ] assert spec.reply == { "performative_ct": ["performative_pct"], "performative_pt": ["performative_pmt"], "performative_pct": ["performative_mt", "performative_o"], "performative_pmt": ["performative_mt", "performative_o"], "performative_mt": [], "performative_o": [], "performative_empty_contents": ["performative_empty_contents"], } assert spec.terminal_performatives == [ "PERFORMATIVE_MT", "PERFORMATIVE_O", "PERFORMATIVE_EMPTY_CONTENTS", ] assert spec.roles == ["role_1", "role_2"] assert spec.end_states == ["end_state_1", "end_state_2", "end_state_3"] assert spec.typing_imports == { "Set": True, "Tuple": True, "cast": True, "FrozenSet": True, "Dict": True, "Union": True, "Optional": True, }
def test_load_protocol_specification(spec_file_path): """Test for the utility function 'load_protocol_specification'""" result = load_protocol_specification(spec_file_path) assert type(result) == ProtocolSpecification
def _generate_item(ctx: Context, item_type: str, specification_path: str): """Generate an item based on a specification and add it to the configuration file and agent.""" # Get existing items existing_id_list = getattr(ctx.agent_config, "{}s".format(item_type)) existing_item_list = [public_id.name for public_id in existing_id_list] item_type_plural = item_type + "s" # Load item specification yaml file try: protocol_spec = load_protocol_specification(specification_path) except Exception as e: raise click.ClickException(str(e)) protocol_directory_path = os.path.join( ctx.cwd, item_type_plural, protocol_spec.name ) # Check if we already have an item with the same name in the agent config logger.debug( "{} already supported by the agent: {}".format( item_type_plural, existing_item_list ) ) if protocol_spec.name in existing_item_list: raise click.ClickException( "A {} with name '{}' already exists. Aborting...".format( item_type, protocol_spec.name ) ) # Check if we already have a directory with the same name in the resource directory (e.g. protocols) of the agent's directory if os.path.exists(protocol_directory_path): raise click.ClickException( "A directory with name '{}' already exists. Aborting...".format( protocol_spec.name ) ) ctx.clean_paths.append(protocol_directory_path) try: agent_name = ctx.agent_config.agent_name click.echo( "Generating {} '{}' and adding it to the agent '{}'...".format( item_type, protocol_spec.name, agent_name ) ) output_path = os.path.join(ctx.cwd, item_type_plural) protocol_generator = ProtocolGenerator(specification_path, output_path) warning_message = protocol_generator.generate() if warning_message is not None: click.echo(warning_message) # Add the item to the configurations logger.debug( "Registering the {} into {}".format(item_type, DEFAULT_AEA_CONFIG_FILE) ) existing_id_list.add( PublicId(protocol_spec.author, protocol_spec.name, protocol_spec.version) ) ctx.agent_loader.dump( ctx.agent_config, open(os.path.join(ctx.cwd, DEFAULT_AEA_CONFIG_FILE), "w") ) except FileExistsError: raise click.ClickException( # pragma: no cover "A {} with this name already exists. Please choose a different name and try again.".format( item_type ) ) except ProtocolSpecificationParseError as e: raise click.ClickException( # pragma: no cover "The following error happened while parsing the protocol specification: " + str(e) ) except Exception as e: raise click.ClickException( "Protocol is NOT generated. The following error happened while generating the protocol:\n" + str(e) ) fingerprint_item(ctx, PROTOCOL, protocol_spec.public_id)