def test_criterion_applied(): """Test sampling w/ criterion.""" # Prepare bridge = MagicMock() client1 = GrpcClientProxy(cid="train_client_1", bridge=bridge) client2 = GrpcClientProxy(cid="train_client_2", bridge=bridge) client3 = GrpcClientProxy(cid="test_client_1", bridge=bridge) client4 = GrpcClientProxy(cid="test_client_2", bridge=bridge) client_manager = SimpleClientManager() client_manager.register(client1) client_manager.register(client2) client_manager.register(client3) client_manager.register(client4) class TestCriterion(Criterion): """Criterion to select only test clients.""" def select(self, client: ClientProxy) -> bool: return client.cid.startswith("test_") # Execute sampled_clients = client_manager.sample(2, criterion=TestCriterion()) # Assert assert client3 in sampled_clients assert client4 in sampled_clients
def test_get_parameters(self): """This test is currently quite simple and should be improved""" # Prepare client = GrpcClientProxy(cid="1", bridge=self.bridge_mock) # Execute value: flwr.ParametersRes = client.get_parameters() # Assert assert value.parameters.tensors == []
def test_evaluate(self): """This test is currently quite simple and should be improved""" # Prepare client = GrpcClientProxy(cid="1", bridge=self.bridge_mock) parameters = flwr.Parameters(tensors=[], tensor_type="np") evaluate_ins: flwr.EvaluateIns = (parameters, {}) # Execute value = client.evaluate(evaluate_ins) # Assert assert (0, 0.0, 0.0) == value
def test_fit(self): """This test is currently quite simple and should be improved""" # Prepare client = GrpcClientProxy(cid="1", bridge=self.bridge_mock) parameters = flwr.weights_to_parameters([np.ones((2, 2))]) ins: flwr.FitIns = (parameters, {}) # Execute parameters_prime, num_examples, _, _ = client.fit(ins=ins) # Assert assert parameters_prime.tensor_type == "np" assert flwr.parameters_to_weights(parameters_prime) == [] assert num_examples == 10
def test_simple_client_manager_unregister(): """Tests if the unregister method works correctly""" # Prepare cid = "1" bridge = MagicMock() client = GrpcClientProxy(cid=cid, bridge=bridge) client_manager = SimpleClientManager() client_manager.register(client) # Execute client_manager.unregister(client) # Assert assert len(client_manager) == 0
def test_criterion_not_applied(): """Test sampling w/o criterion.""" # Prepare bridge = MagicMock() client1 = GrpcClientProxy(cid="train_client_1", bridge=bridge) client2 = GrpcClientProxy(cid="train_client_2", bridge=bridge) client3 = GrpcClientProxy(cid="test_client_1", bridge=bridge) client4 = GrpcClientProxy(cid="test_client_2", bridge=bridge) client_manager = SimpleClientManager() client_manager.register(client1) client_manager.register(client2) client_manager.register(client3) client_manager.register(client4) # Execute sampled_clients = client_manager.sample(4) # Assert assert client1 in sampled_clients assert client2 in sampled_clients assert client3 in sampled_clients assert client4 in sampled_clients
def default_grpc_client_factory(cid: str, bridge: GRPCBridge) -> GrpcClientProxy: """Return GrpcClientProxy instance.""" return GrpcClientProxy(cid=cid, bridge=bridge)