def test_serialisation_fipa(self): """Tests a Value Error flag for wrong CFP query.""" with pytest.raises(ValueError): msg = FipaMessage( performative=FipaMessage.Performative.CFP, message_id=1, dialogue_reference=(str(0), ""), target=0, query=Query([Constraint("something", ConstraintType(">", 1))]), ) with mock.patch( "packages.fetchai.protocols.fipa.message.FipaMessage.Performative" ) as mock_performative_enum: mock_performative_enum.CFP.value = "unknown" FipaSerializer().encode(msg), "Raises Value Error" with pytest.raises(EOFError): cfp_msg = FipaMessage( message_id=1, dialogue_reference=(str(0), ""), target=0, performative=FipaMessage.Performative.CFP, query=Query([Constraint("something", ConstraintType(">", 1))]), ) cfp_msg.set("query", "hello") fipa_msg = fipa_pb2.FipaMessage() fipa_msg.message_id = cfp_msg.message_id dialogue_reference = cast(Dict[str, str], cfp_msg.dialogue_reference) fipa_msg.dialogue_starter_reference = dialogue_reference[0] fipa_msg.dialogue_responder_reference = dialogue_reference[1] fipa_msg.target = cfp_msg.target performative = fipa_pb2.FipaMessage.Cfp_Performative() fipa_msg.cfp.CopyFrom(performative) fipa_bytes = fipa_msg.SerializeToString() # The encoded message is not a valid FIPA message. FipaSerializer().decode(fipa_bytes) with pytest.raises(ValueError): cfp_msg = FipaMessage( message_id=1, dialogue_reference=(str(0), ""), target=0, performative=FipaMessage.Performative.CFP, query=Query([Constraint("something", ConstraintType(">", 1))]), ) with mock.patch( "packages.fetchai.protocols.fipa.message.FipaMessage.Performative" ) as mock_performative_enum: mock_performative_enum.CFP.value = "unknown" fipa_msg = fipa_pb2.FipaMessage() fipa_msg.message_id = cfp_msg.message_id dialogue_reference = cast(Dict[str, str], cfp_msg.dialogue_reference) fipa_msg.dialogue_starter_reference = dialogue_reference[0] fipa_msg.dialogue_responder_reference = dialogue_reference[1] fipa_msg.target = cfp_msg.target performative = fipa_pb2.FipaMessage.Cfp_Performative() fipa_msg.cfp.CopyFrom(performative) fipa_bytes = fipa_msg.SerializeToString() # The encoded message is not a FIPA message FipaSerializer().decode(fipa_bytes)
def test_query_check(self): """Test that the query.check() method works.""" attribute_foo = Attribute("foo", int, True, "a foo attribute.") attribute_bar = Attribute("bar", str, True, "a bar attribute.") data_model_foobar = DataModel( "foobar", [attribute_foo, attribute_bar], "A foobar data model." ) description_foobar = Description( {"foo": 1, "bar": "baz"}, data_model=data_model_foobar ) query = Query( [ And( [ Or( [ Not(Constraint("foo", ConstraintType("==", 1))), Not(Constraint("bar", ConstraintType("==", "baz"))), ] ), Constraint("foo", ConstraintType("<", 2)), ] ) ], data_model_foobar, ) assert not query.check(description=description_foobar)
def test_is_matching_supply(self): """Test the is_matching_supply method of the GenericStrategy class.""" acceptable_constraint = Constraint( "seller_service", ConstraintType("==", "some_service")) matching_query = Query([acceptable_constraint]) is_matching_supply = self.strategy.is_matching_supply(matching_query) assert is_matching_supply unacceptable_constraint = Constraint( "seller_service", ConstraintType("==", "some_other_service")) unmatching_query = Query([unacceptable_constraint]) is_matching_supply = self.strategy.is_matching_supply(unmatching_query) assert not is_matching_supply
def test_not_empty_search_result(self): """Test that the search result contains one entry after a successful registration.""" request_id = 1 query = Query(constraints=[], model=self.data_model) # build and send the request search_services_request = OEFMessage( type=OEFMessage.Type.SEARCH_SERVICES, id=request_id, query=query ) msg_bytes = OEFSerializer().encode(search_services_request) envelope = Envelope( to=DEFAULT_OEF, sender=self.address_1, protocol_id=OEFMessage.protocol_id, message=msg_bytes, ) self.multiplexer.put(envelope) # check the result response_envelope = self.multiplexer.get(block=True, timeout=2.0) assert response_envelope.protocol_id == OEFMessage.protocol_id assert response_envelope.to == self.address_1 assert response_envelope.sender == DEFAULT_OEF search_result = OEFSerializer().decode(response_envelope.message) assert search_result.get("type") == OEFMessage.Type.SEARCH_RESULT assert search_result.get("agents") == [self.address_1]
async def test_bad_search_query(self, caplog): """Test fail on invalid query for search.""" await self.test_register_service() closeness_query = Query([], model=models.AGENT_LOCATION_MODEL) message = OefSearchMessage( performative=OefSearchMessage.Performative.SEARCH_SERVICES, dialogue_reference=self.oef_search_dialogues.new_self_initiated_dialogue_reference(), query=closeness_query, ) message.counterparty = SOEFConnection.connection_id.latest sending_dialogue = self.oef_search_dialogues.update(message) assert sending_dialogue is not None envelope = Envelope( to=message.counterparty, sender=self.crypto.address, protocol_id=message.protocol_id, message=message, ) with patch.object( self.connection.channel, "_request_text", make_async(self.search_empty_response), ): await self.connection.send(envelope) expected_envelope = await asyncio.wait_for(self.connection.receive(), timeout=1) assert expected_envelope message = expected_envelope.message assert message.performative == OefSearchMessage.Performative.OEF_ERROR message = copy.deepcopy(expected_envelope.message) message.is_incoming = True # TODO: fix message.counterparty = SOEFConnection.connection_id.latest # TODO; fix receiving_dialogue = self.oef_search_dialogues.update(message) assert sending_dialogue == receiving_dialogue
def test_search_no_filters(self): """Perform tests over real networ with no filters.""" agent_location = Location(*self.LOCATION) agent = Instance(agent_location) agent2 = Instance(agent_location) try: agent.start() agent2.start() agent.wait_registered() agent2.wait_registered() time.sleep(2) # find agents near me radius = 0.1 close_to_my_service = Constraint( "location", ConstraintType("distance", (agent_location, radius))) closeness_query = Query([close_to_my_service], model=models.AGENT_LOCATION_MODEL) # search for agents close to me message = agent.search(closeness_query) assert message.performative == OefSearchMessage.Performative.SEARCH_RESULT assert len(message.agents) >= 1 # second message in a raw to check we dont hit limit message = agent.search(closeness_query) assert message.performative == OefSearchMessage.Performative.SEARCH_RESULT assert len(message.agents) >= 1 assert agent2.address in message.agents finally: agent.stop() agent2.stop()
def test_filtered_search_result(self): """Test that the search result contains only the entries matching the query.""" query = Query(constraints=[], model=self.data_model_barfoo) # build and send the request search_services_request = OefSearchMessage( performative=OefSearchMessage.Performative.SEARCH_SERVICES, dialogue_reference=self.dialogues1.new_self_initiated_dialogue_reference(), query=query, ) search_services_request.counterparty = str(OEFLocalConnection.connection_id) sending_dialogue = cast( Optional[OefSearchDialogue], self.dialogues1.update(search_services_request) ) assert sending_dialogue is not None envelope = Envelope( to=search_services_request.counterparty, sender=search_services_request.sender, protocol_id=search_services_request.protocol_id, message=search_services_request, ) self.multiplexer1.put(envelope) # check the result response_envelope = InBox(self.multiplexer1).get(block=True, timeout=5.0) search_result_orig = cast(OefSearchMessage, response_envelope.message) search_result = copy.copy(search_result_orig) search_result.is_incoming = True search_result.counterparty = search_result_orig.sender response_dialogue = self.dialogues1.update(search_result) assert response_dialogue == sending_dialogue assert search_result.performative == OefSearchMessage.Performative.SEARCH_RESULT assert search_result.agents == (self.address_2,), self.node.services
def test_ml_messge_consistency(): """Test the consistency of the message.""" dm = DataModel("ml_datamodel", [Attribute("dataset_id", str, True)]) query = Query([Constraint("dataset_id", ConstraintType("==", "fmnist"))], model=dm) msg = MlTradeMessage(performative=MlTradeMessage.Performative.CFP, query=query) with mock.patch.object(MlTradeMessage.Performative, "__eq__", return_value=False): assert not msg._is_consistent()
def test_search_services_with_query_with_model(self): """Test that a search services request can be sent correctly. In this test, the query has a simple data model. """ data_model = DataModel("foobar", [Attribute("foo", str, True)]) search_query = Query( [Constraint("foo", ConstraintType("==", "bar"))], model=data_model) oef_search_request, sending_dialogue = self.oef_search_dialogues.create( counterparty=str(self.connection.connection_id), performative=OefSearchMessage.Performative.SEARCH_SERVICES, query=search_query, ) self.multiplexer.put( Envelope( to=oef_search_request.to, sender=oef_search_request.sender, protocol_id=oef_search_request.protocol_id, message=oef_search_request, )) envelope = self.multiplexer.get(block=True, timeout=5.0) oef_search_response = envelope.message oef_search_dialogue = self.oef_search_dialogues.update( oef_search_response) assert (oef_search_response.performative == OefSearchMessage.Performative.SEARCH_RESULT) assert oef_search_dialogue is not None assert oef_search_dialogue == sending_dialogue assert oef_search_response.agents == ()
def test_cfp_serialization(): """Test that the serialization for the 'fipa' protocol works.""" msg = FipaMessage( message_id=1, dialogue_reference=(str(0), ""), target=0, performative=FipaMessage.Performative.CFP, query=Query([Constraint("something", ConstraintType(">", 1))]), ) msg.to = "receiver" envelope = Envelope( to=msg.to, sender="sender", protocol_id=FipaMessage.protocol_id, message=msg, ) envelope_bytes = envelope.encode() actual_envelope = Envelope.decode(envelope_bytes) expected_envelope = envelope assert expected_envelope.to == actual_envelope.to assert expected_envelope.sender == actual_envelope.sender assert expected_envelope.protocol_id == actual_envelope.protocol_id assert expected_envelope.message != actual_envelope.message actual_msg = FipaMessage.serializer.decode(actual_envelope.message) actual_msg.to = actual_envelope.to actual_msg.sender = actual_envelope.sender expected_msg = msg assert expected_msg == actual_msg
def get_proposal_for_query(self, query: Query, is_seller: bool) -> Optional[Description]: """ Generate proposal (in the form of a description) which matches the query. :param query: the query for which to build the proposal :is_seller: whether the agent making the proposal is a seller or not :return: a description """ own_service_description = self.get_own_service_description( is_supply=is_seller) if not query.check(own_service_description): self.context.logger.debug( "[{}]: Current holdings do not satisfy CFP query.".format( self.context.agent_name)) return None else: proposal_description = self._get_proposal_for_query( query, is_seller=is_seller) if proposal_description is None: self.context.logger.debug( "[{}]: Current strategy does not generate proposal that satisfies CFP query." .format(self.context.agent_name)) return proposal_description
def test_search_services_with_query_with_model(self): """Test that a search services request can be sent correctly. In this test, the query has a simple data model. """ request_id = 2 data_model = DataModel("foobar", [Attribute("foo", str, True)]) search_query = Query( [Constraint("foo", ConstraintType("==", "bar"))], model=data_model ) search_request = OEFMessage( type=OEFMessage.Type.SEARCH_SERVICES, id=request_id, query=search_query ) self.multiplexer.put( Envelope( to=DEFAULT_OEF, sender=self.crypto1.address, protocol_id=OEFMessage.protocol_id, message=OEFSerializer().encode(search_request), ) ) envelope = self.multiplexer.get(block=True, timeout=5.0) search_result = OEFSerializer().decode(envelope.message) assert search_result.get("type") == OEFMessage.Type.SEARCH_RESULT assert search_result.get("id") == request_id assert search_result.get("agents") == []
def test_search_count_increases(self): """Test that the search count increases.""" request_id = 1 search_query_empty_model = Query( [Constraint("foo", ConstraintType("==", "bar"))], model=None ) search_request = OEFMessage( type=OEFMessage.Type.SEARCH_SERVICES, id=request_id, query=search_query_empty_model, ) self.multiplexer.put( Envelope( to=DEFAULT_OEF, sender=self.crypto1.address, protocol_id=OEFMessage.protocol_id, message=OEFSerializer().encode(search_request), ) ) envelope = self.multiplexer.get(block=True, timeout=5.0) search_result = OEFSerializer().decode(envelope.message) assert search_result.get("type") == OEFMessage.Type.SEARCH_RESULT assert search_result.get("id") assert request_id and search_result.get("agents") == []
def get_proposal_for_query(self, query: Query, role: Dialogue.Role) -> Optional[Description]: """ Generate proposal (in the form of a description) which matches the query. :param query: the query for which to build the proposal :param role: the role of the agent making the proposal (seller or buyer) :return: a description """ is_seller = role == Dialogue.Role.SELLER own_service_description = self.get_own_service_description( is_supply=is_seller, is_search_description=False) if not query.check(own_service_description): self.context.logger.debug( "[{}]: Current holdings do not satisfy CFP query.".format( self.context.agent_name)) return None else: proposal_description = self._get_proposal_for_query( query, is_seller=is_seller) if proposal_description is None: self.context.logger.debug( "[{}]: Current strategy does not generate proposal that satisfies CFP query." .format(self.context.agent_name)) return proposal_description
def test_on_oef_error(self): """Test the oef error.""" oef_connection = self.multiplexer1.connections[0] oef_channel = oef_connection.channel oef_channel.oef_msg_id += 1 dialogue_reference = ("1", "2") query = Query( constraints=[Constraint("foo", ConstraintType("==", "bar"))], model=None, ) dialogue = OefSearchDialogue( BaseDialogueLabel(dialogue_reference, "agent", "agent"), "agent", OefSearchDialogue.Role.OEF_NODE, ) oef_search_msg = OefSearchMessage( performative=OefSearchMessage.Performative.SEARCH_SERVICES, dialogue_reference=dialogue_reference, query=query, ) oef_search_msg.is_incoming = True oef_search_msg.counterparty = "agent" dialogue._incoming_messages = [oef_search_msg] oef_channel.oef_msg_id_to_dialogue[oef_channel.oef_msg_id] = dialogue oef_channel.on_oef_error( answer_id=oef_channel.oef_msg_id, operation=OEFErrorOperation.SEARCH_SERVICES, ) envelope = self.multiplexer1.get(block=True, timeout=5.0) dec_msg = envelope.message assert dec_msg.dialogue_reference == dialogue_reference assert ( dec_msg.performative is OefSearchMessage.Performative.OEF_ERROR ), "It should be an error message"
def test_search_services_with_query_with_model(self): """Test that a search services request can be sent correctly. In this test, the query has a simple data model. """ data_model = DataModel("foobar", [Attribute("foo", str, True)]) search_query = Query( [Constraint("foo", ConstraintType("==", "bar"))], model=data_model ) oef_search_request = OefSearchMessage( performative=OefSearchMessage.Performative.SEARCH_SERVICES, dialogue_reference=self.oef_search_dialogues.new_self_initiated_dialogue_reference(), query=search_query, ) oef_search_request.counterparty = DEFAULT_OEF sending_dialogue = self.oef_search_dialogues.update(oef_search_request) self.multiplexer.put( Envelope( to=DEFAULT_OEF, sender=FETCHAI_ADDRESS_ONE, protocol_id=OefSearchMessage.protocol_id, message=oef_search_request, ) ) envelope = self.multiplexer.get(block=True, timeout=5.0) oef_search_response = envelope.message oef_search_dialogue = self.oef_search_dialogues.update(oef_search_response) assert ( oef_search_response.performative == OefSearchMessage.Performative.SEARCH_RESULT ) assert oef_search_dialogue is not None assert oef_search_dialogue == sending_dialogue assert oef_search_response.agents == ()
def test_filtered_search_result(self): """Test that the search result contains only the entries matching the query.""" request_id = 1 query = Query(constraints=[], model=self.data_model_barfoo) # build and send the request search_services_request = OefSearchMessage( performative=OefSearchMessage.Performative.SEARCH_SERVICES, dialogue_reference=(str(request_id), ""), query=query, ) msg_bytes = OefSearchSerializer().encode(search_services_request) envelope = Envelope( to=DEFAULT_OEF, sender=self.address_1, protocol_id=OefSearchMessage.protocol_id, message=msg_bytes, ) self.multiplexer1.put(envelope) # check the result response_envelope = InBox(self.multiplexer1).get(block=True, timeout=5.0) assert response_envelope.protocol_id == OefSearchMessage.protocol_id assert response_envelope.to == self.address_1 assert response_envelope.sender == DEFAULT_OEF search_result = OefSearchSerializer().decode(response_envelope.message) assert search_result.performative == OefSearchMessage.Performative.SEARCH_RESULT assert search_result.agents == (self.address_2,)
def test_search_services_with_query_with_model(self): """Test that a search services request can be sent correctly. In this test, the query has a simple data model. """ request_id = 1 data_model = DataModel("foobar", [Attribute("foo", str, True)]) search_query = Query( [Constraint("foo", ConstraintType("==", "bar"))], model=data_model ) search_request = OefSearchMessage( performative=OefSearchMessage.Performative.SEARCH_SERVICES, dialogue_reference=(str(request_id), ""), query=search_query, ) self.multiplexer.put( Envelope( to=DEFAULT_OEF, sender=self.crypto1.address, protocol_id=OefSearchMessage.protocol_id, message=OefSearchSerializer().encode(search_request), ) ) envelope = self.multiplexer.get(block=True, timeout=5.0) search_result = OefSearchSerializer().decode(envelope.message) assert ( search_result.performative == OefSearchMessage.Performative.SEARCH_RESULT ) assert search_result.dialogue_reference[0] == str(request_id) assert search_result.agents == ()
def test_empty_search_result(self): """Test that at the beginning, the search request returns an empty search result.""" request_id = 1 query = Query(constraints=[], model=None) # build and send the request search_services_request = OefSearchMessage( performative=OefSearchMessage.Performative.SEARCH_SERVICES, dialogue_reference=(str(request_id), ""), query=query, ) msg_bytes = OefSearchSerializer().encode(search_services_request) envelope = Envelope( to=DEFAULT_OEF, sender=self.address_1, protocol_id=OefSearchMessage.protocol_id, message=msg_bytes, ) self.multiplexer.put(envelope) # check the result response_envelope = self.multiplexer.get(block=True, timeout=2.0) assert response_envelope.protocol_id == OefSearchMessage.protocol_id assert response_envelope.to == self.address_1 assert response_envelope.sender == DEFAULT_OEF search_result = OefSearchSerializer().decode(response_envelope.message) assert search_result.performative == OefSearchMessage.Performative.SEARCH_RESULT assert search_result.agents == ()
async def test_bad_search_query(self, caplog): """Test fail on invalid query for search.""" await self.test_register_service() closeness_query = Query([], model=models.AGENT_LOCATION_MODEL) message = OefSearchMessage( performative=OefSearchMessage.Performative.SEARCH_SERVICES, query=closeness_query, ) envelope = Envelope( to="soef", sender=self.crypto.address, protocol_id=message.protocol_id, message=message, ) with patch.object( self.connection.channel, "_request_text", make_async(self.search_empty_response), ): await self.connection.send(envelope) expected_envelope = await asyncio.wait_for(self.connection.receive(), timeout=1) assert expected_envelope message = expected_envelope.message assert message.performative == OefSearchMessage.Performative.OEF_ERROR
def test_fipa_cfp_serialization(): """Test that the serialization for the 'fipa' protocol works.""" query = Query([Constraint("something", ConstraintType(">", 1))]) msg = FIPAMessage( message_id=0, dialogue_reference=(str(0), ""), target=0, performative=FIPAMessage.Performative.CFP, query=query, ) msg_bytes = FIPASerializer().encode(msg) envelope = Envelope( to="receiver", sender="sender", protocol_id=FIPAMessage.protocol_id, message=msg_bytes, ) envelope_bytes = envelope.encode() actual_envelope = Envelope.decode(envelope_bytes) expected_envelope = envelope assert expected_envelope == actual_envelope actual_msg = FIPASerializer().decode(actual_envelope.message) expected_msg = msg assert expected_msg == actual_msg msg.set("query", "not_supported_query") with pytest.raises(ValueError, match="Query type not supported:"): FIPASerializer().encode(msg)
def test_search_services_with_query_without_model(self): """Test that a search services request can be sent correctly. In this test, the query has no data model. """ request_id = 1 search_query_empty_model = Query( [Constraint("foo", ConstraintType("==", "bar"))], model=None) search_request = OefSearchMessage( performative=OefSearchMessage.Performative.SEARCH_SERVICES, dialogue_reference=(str(request_id), ""), query=search_query_empty_model, ) self.multiplexer.put( Envelope( to=DEFAULT_OEF, sender=FETCHAI_ADDRESS_ONE, protocol_id=OefSearchMessage.protocol_id, message=OefSearchSerializer().encode(search_request), )) envelope = self.multiplexer.get(block=True, timeout=5.0) search_result = OefSearchSerializer().decode(envelope.message) assert (search_result.performative == OefSearchMessage.Performative.SEARCH_RESULT) assert search_result.dialogue_reference[0] == str(request_id) assert request_id and search_result.agents == ()
def test_search_count_increases(self): """Test that the search count increases.""" request_id = 1 search_query_empty_model = Query( [Constraint("foo", ConstraintType("==", "bar"))], model=None ) search_request = OefSearchMessage( performative=OefSearchMessage.Performative.SEARCH_SERVICES, dialogue_reference=(str(request_id), ""), query=search_query_empty_model, ) self.multiplexer.put( Envelope( to=DEFAULT_OEF, sender=self.crypto1.address, protocol_id=OefSearchMessage.protocol_id, message=OefSearchSerializer().encode(search_request), ) ) envelope = self.multiplexer.get(block=True, timeout=5.0) search_result = OefSearchSerializer().decode(envelope.message) assert ( search_result.performative == OefSearchMessage.Performative.SEARCH_RESULT ) assert search_result.dialogue_reference[0] == str(request_id) assert request_id and search_result.agents == ()
def test_fipa_cfp_serialization_bytes(): """Test that the serialization - deserialization for the 'fipa' protocol works.""" query = Query([Constraint("something", ConstraintType(">", 1))]) msg = FipaMessage( message_id=1, dialogue_reference=(str(0), ""), target=0, performative=FipaMessage.Performative.CFP, query=query, ) msg.counterparty = "sender" msg_bytes = FipaSerializer().encode(msg) envelope = Envelope( to="receiver", sender="sender", protocol_id=FipaMessage.protocol_id, message=msg_bytes, ) envelope_bytes = envelope.encode() actual_envelope = Envelope.decode(envelope_bytes) expected_envelope = envelope assert expected_envelope == actual_envelope actual_msg = FipaSerializer().decode(actual_envelope.message) actual_msg.counterparty = "sender" expected_msg = msg assert expected_msg == actual_msg deserialised_msg = FipaSerializer().decode(envelope.message) deserialised_msg.counterparty = "sender" assert msg.get("performative") == deserialised_msg.get("performative")
def test_search_services_with_distance_query(self): """Test that a search services request can be sent correctly. In this test, the query has a simple data model. """ tour_eiffel = Location(48.8581064, 2.29447) attribute = Attribute("latlon", Location, True) data_model = DataModel("geolocation", [attribute]) search_query = Query( [ Constraint(attribute.name, ConstraintType("distance", (tour_eiffel, 1.0))) ], model=data_model, ) oef_search_request, sending_dialogue = self.oef_search_dialogues.create( counterparty=str(self.connection.connection_id), performative=OefSearchMessage.Performative.SEARCH_SERVICES, query=search_query, ) self.multiplexer.put( Envelope( to=oef_search_request.to, sender=oef_search_request.sender, message=oef_search_request, )) envelope = self.multiplexer.get(block=True, timeout=5.0) oef_search_response = envelope.message oef_search_dialogue = self.oef_search_dialogues.update( oef_search_response) assert (oef_search_response.performative == OefSearchMessage.Performative.SEARCH_RESULT) assert oef_search_dialogue is not None assert oef_search_dialogue == sending_dialogue assert oef_search_response.agents == ()
def test_search_services_serialization(): """Test the serialization for 'search_services' speech-act works.""" msg = OefSearchMessage( performative=OefSearchMessage.Performative.SEARCH_SERVICES, query=Query([Constraint("something", ConstraintType(">", 1))]), ) msg.to = "receiver" envelope = Envelope( to=msg.to, sender="sender", protocol_id=OefSearchMessage.protocol_id, message=msg, ) envelope_bytes = envelope.encode() actual_envelope = Envelope.decode(envelope_bytes) expected_envelope = envelope assert expected_envelope.to == actual_envelope.to assert expected_envelope.sender == actual_envelope.sender assert expected_envelope.protocol_id == actual_envelope.protocol_id assert expected_envelope.message != actual_envelope.message actual_msg = OefSearchMessage.serializer.decode(actual_envelope.message) actual_msg.to = actual_envelope.to actual_msg.sender = actual_envelope.sender expected_msg = msg assert expected_msg == actual_msg
def test_on_oef_error(self): """Test the oef error.""" oef_connection = self.multiplexer1.connections[0] oef_channel = oef_connection.channel oef_channel.oef_msg_id += 1 dialogue_reference = ("1", "") query = Query( constraints=[Constraint("foo", ConstraintType("==", "bar"))], model=None, ) dialogues = oef_channel.oef_search_dialogues oef_search_msg = OefSearchMessage( performative=OefSearchMessage.Performative.SEARCH_SERVICES, dialogue_reference=dialogue_reference, query=query, ) oef_search_msg.to = str(oef_connection.connection_id) oef_search_msg.sender = "agent" dialogue = dialogues.update(oef_search_msg) assert dialogue is not None oef_channel.oef_msg_id_to_dialogue[oef_channel.oef_msg_id] = dialogue oef_channel.on_oef_error( answer_id=oef_channel.oef_msg_id, operation=OEFErrorOperation.SEARCH_SERVICES, ) envelope = self.multiplexer1.get(block=True, timeout=5.0) dec_msg = envelope.message assert dec_msg.dialogue_reference[0] == dialogue_reference[0] assert (dec_msg.performative is OefSearchMessage.Performative.OEF_ERROR ), "It should be an error message"
def test_cfp_serialization(): """Test the serialization for 'cfp' speech-act works.""" msg = MlTradeMessage( performative=MlTradeMessage.Performative.CFP, query=Query([Constraint("something", ConstraintType(">", 1))]), ) msg.to = "receiver" envelope = Envelope( to=msg.to, sender="sender", message=msg, ) envelope_bytes = envelope.encode() actual_envelope = Envelope.decode(envelope_bytes) expected_envelope = envelope assert expected_envelope.to == actual_envelope.to assert expected_envelope.sender == actual_envelope.sender assert (expected_envelope.protocol_specification_id == actual_envelope.protocol_specification_id) assert expected_envelope.message != actual_envelope.message actual_msg = MlTradeMessage.serializer.decode(actual_envelope.message) actual_msg.to = actual_envelope.to actual_msg.sender = actual_envelope.sender expected_msg = msg assert expected_msg == actual_msg
async def test_wrong_dialogue(self): """Test that at the beginning, the search request returns an empty search result.""" query = Query(constraints=[Constraint("foo", ConstraintType("==", 1))], model=None) # build and send the request search_services_request = OefSearchMessage( performative=OefSearchMessage.Performative.SEARCH_SERVICES, message_id=2, target=1, dialogue_reference=self.dialogues. new_self_initiated_dialogue_reference(), query=query, ) search_services_request.to = str(OEFLocalConnection.connection_id) # the incorrect message cannot be sent into a dialogue, so this is omitted. search_services_request.sender = self.address_1 envelope = Envelope( to=search_services_request.to, sender=search_services_request.sender, message=search_services_request, ) with unittest.mock.patch.object( self.node, "_handle_oef_message", side_effect=self.node._handle_oef_message) as mock_handle: with unittest.mock.patch.object(self.node.logger, "warning") as mock_logger: self.multiplexer.put(envelope) wait_for_condition(lambda: mock_handle.called, timeout=1.0) mock_logger.assert_any_call( AnyStringWith("Could not create dialogue for message="))
async def test_bad_search_query(self): """Test fail on invalid query for search.""" await self.test_register_service() closeness_query = Query([], model=models.AGENT_LOCATION_MODEL) message, sending_dialogue = self.oef_search_dialogues.create( counterparty=str(SOEFConnection.connection_id.to_any()), performative=OefSearchMessage.Performative.SEARCH_SERVICES, query=closeness_query, ) envelope = Envelope( to=message.to, sender=message.sender, protocol_id=message.protocol_id, message=message, ) with patch.object( self.connection.channel, "_request_text", make_async(self.search_empty_response), ): await self.connection.send(envelope) expected_envelope = await asyncio.wait_for(self.connection.receive(), timeout=1) assert expected_envelope message = expected_envelope.message assert message.performative == OefSearchMessage.Performative.OEF_ERROR message = expected_envelope.message receiving_dialogue = self.oef_search_dialogues.update(message) assert sending_dialogue == receiving_dialogue