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_serialisation_fipa(self): """Tests a Value Error flag for wrong CFP query.""" with pytest.raises(ValueError): msg = FIPAMessage( performative=FIPAMessage.Performative.CFP, message_id=0, dialogue_reference=(str(0), ""), target=1, query=None, ) 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(ValueError): msg.set("query", "Hello") # query type is not supported FIPASerializer().encode(msg) with pytest.raises(ValueError): cfp_msg = FIPAMessage( message_id=0, dialogue_reference=(str(0), ""), target=0, performative=FIPAMessage.Performative.CFP, query=b"hello", ) cfp_msg.set("query", "hello") fipa_msg = fipa_pb2.FIPAMessage() fipa_msg.message_id = cfp_msg.get("message_id") dialogue_reference = cast(Dict[str, str], cfp_msg.get("dialogue_reference")) fipa_msg.dialogue_starter_reference = dialogue_reference[0] fipa_msg.dialogue_responder_reference = dialogue_reference[1] fipa_msg.target = cfp_msg.get("target") performative = fipa_pb2.FIPAMessage.CFP() 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=0, dialogue_reference=(str(0), ""), target=0, performative=FIPAMessage.Performative.CFP, query=b"hello", ) 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.get("message_id") dialogue_reference = cast( Dict[str, str], cfp_msg.get("dialogue_reference") ) fipa_msg.dialogue_starter_reference = dialogue_reference[0] fipa_msg.dialogue_responder_reference = dialogue_reference[1] fipa_msg.target = cfp_msg.get("target") performative = fipa_pb2.FIPAMessage.CFP() fipa_msg.cfp.CopyFrom(performative) fipa_bytes = fipa_msg.SerializeToString() # The encoded message is not a FIPA message FIPASerializer().decode(fipa_bytes)