def _load_packages(agent_identity: Identity) -> None: """Load packages in the current interpreter.""" default_protocol_id = PublicId.from_str(DEFAULT_PROTOCOL) Protocol.from_dir( os.path.join(VENDOR, default_protocol_id.author, PROTOCOLS, default_protocol_id.name)) signing_protocol_id = PublicId.from_str(SIGNING_PROTOCOL) Protocol.from_dir( os.path.join(VENDOR, signing_protocol_id.author, PROTOCOLS, signing_protocol_id.name)) state_update_protocol_id = PublicId.from_str(STATE_UPDATE_PROTOCOL) Protocol.from_dir( os.path.join( VENDOR, state_update_protocol_id.author, PROTOCOLS, state_update_protocol_id.name, )) stub_connection_id = PublicId.from_str(STUB_CONNECTION) Connection.from_dir( os.path.join( VENDOR, stub_connection_id.author, CONNECTIONS, stub_connection_id.name, ), agent_identity, CryptoStore(), os.getcwd(), )
def test_ensure_valid_envelope_for_external_comms_negative_cases(): """Test the staticmethod '_ensure_valid_envelope_for_external_comms', negative cases.""" protocol_specification_id = PublicId("author", "name", "0.1.0") wrong_sender = wrong_to = "author/name:0.1.0" envelope_wrong_sender = Envelope( to="to", sender=wrong_sender, protocol_specification_id=protocol_specification_id, message=b"", ) with pytest.raises( AEAEnforceError, match= f"Sender field of envelope is public id, needs to be address. Found={wrong_to}", ): Connection._ensure_valid_envelope_for_external_comms( envelope_wrong_sender) envelope_wrong_sender = Envelope( to=wrong_to, sender="sender", protocol_specification_id=protocol_specification_id, message=b"", ) with pytest.raises( AEAEnforceError, match= f"To field of envelope is public id, needs to be address. Found={wrong_sender}", ): Connection._ensure_valid_envelope_for_external_comms( envelope_wrong_sender)
def __init__(self, envelope: Envelope, num: int, *args, **kwargs): """ Set fake connection with num of envelops to be generated. :param envelope: any envelope :param num: amount of envelopes to generate """ Connection.__init__(self, *args, **kwargs) self.num = num self.envelope = envelope self._state.set(ConnectionStates.connected)
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 test_add_remove_connection(self): """Test that the 'add connection' and 'remove connection' methods work correctly.""" a_connection = Connection.from_dir( Path(ROOT_DIR, "packages", "fetchai", "connections", "oef"), identity=MagicMock(), crypto_store=MagicMock(), ) self.resources.add_component(a_connection) assert self.resources.get_connection(a_connection.public_id) is not None # restore state self.resources.remove_connection(a_connection.public_id)
def test_from_dir(): """Test Connection.from_dir""" dummy_connection_dir = os.path.join(CUR_PATH, "data", "dummy_connection") identity = MagicMock() identity.name = "agent_name" crypto_store = MagicMock() connection = Connection.from_dir(dummy_connection_dir, identity, crypto_store) assert isinstance(connection, Connection) assert connection.component_id == ComponentId( ComponentType.CONNECTION, PublicId("fetchai", "dummy", "0.1.0") )
async def ledger_apis_connection(request): """Make a connection.""" identity = Identity("name", FetchAICrypto().address) crypto_store = CryptoStore() directory = Path(ROOT_DIR, "packages", "fetchai", "connections", "ledger") connection = Connection.from_dir(directory, identity=identity, crypto_store=crypto_store) connection = cast(Connection, connection) await connection.connect() yield connection await connection.disconnect()
def _load_packages(agent_identity: Identity): """Load packages in the current interpreter.""" Protocol.from_dir( os.path.join("vendor", DEFAULT_PROTOCOL.author, "protocols", DEFAULT_PROTOCOL.name)) Protocol.from_dir( os.path.join("vendor", SIGNING_PROTOCOL.author, "protocols", SIGNING_PROTOCOL.name)) Protocol.from_dir( os.path.join( "vendor", STATE_UPDATE_PROTOCOL.author, "protocols", STATE_UPDATE_PROTOCOL.name, )) Connection.from_dir( os.path.join("vendor", DEFAULT_CONNECTION.author, "connections", DEFAULT_CONNECTION.name), agent_identity, CryptoStore(), )
def test_get_all_connections(self): """Test get all connections.""" a_connection = Connection.from_dir( Path(ROOT_DIR, "packages", "fetchai", "connections", "oef"), identity=MagicMock(), crypto_store=MagicMock(), ) self.resources.add_component(a_connection) all_connections = self.resources.get_all_connections() assert len(all_connections) == 1 assert all_connections[0] == a_connection # restore state self.resources.remove_connection(a_connection.public_id)
async def ledger_apis_connection(request): """Create connection.""" crypto = make_crypto(DEFAULT_LEDGER) identity = Identity("name", crypto.address) crypto_store = CryptoStore() directory = Path(ROOT_DIR, "packages", "fetchai", "connections", "ledger") connection = Connection.from_dir(directory, identity=identity, crypto_store=crypto_store) connection = cast(Connection, connection) await connection.connect() yield connection await connection.disconnect()