def test_uid_default_deserialization() -> None: """Tests that default UID deserialization works as expected - from Protobuf""" uid = UID(value=uuid.UUID(int=333779996850170035686993356951732753684)) blob = _serialize(obj=uid) obj = sy.deserialize(blob=blob) assert obj == UID(value=uuid.UUID(int=333779996850170035686993356951732753684))
def test_dataset_get_element() -> None: id = UID() data = UID() description = "This is a dummy id" tags = ["dummy", "test"] obj1 = StorableObject(id=id, data=data, description=description, tags=tags) id = UID() data = th.Tensor([1, 2, 3, 4]) description = "This is a dummy tensor n1" tags = ["dummy", "test"] obj2 = StorableObject(id=id, data=data, description=description, tags=tags) id = UID() data = th.Tensor([10, 20, 30, 40]) description = "This is a dummy tensor n2" tags = ["dummy", "test"] obj3 = StorableObject(id=id, data=data, description=description, tags=tags) id = UID() data = [obj1, obj2, obj3] description = "This is a dataset" tags = ["dummy", "dataset"] dataset_obj = Dataset(id=id, data=data, description=description, tags=tags) result = dataset_obj.__getitem__(_id=obj1.id) assert len(result) == 1 assert result[0] == obj1
def test_dataset_search_id() -> None: id = UID() data = UID() description = "This is a dummy id" tags = ["dummy", "test"] obj1 = StorableObject(id=id, data=data, description=description, tags=tags) id = UID() data = th.Tensor([1, 2, 3, 4]) description = "This is a dummy tensor n1" tags = ["dummy", "test"] obj2 = StorableObject(id=id, data=data, description=description, tags=tags) id = UID() data = th.Tensor([10, 20, 30, 40]) description = "This is a dummy tensor n2" tags = ["dummy", "test"] obj3 = StorableObject(id=id, data=data, description=description, tags=tags) id = UID() data = [obj1, obj2, obj3] description = "This is a dataset" tags = ["dummy", "dataset"] dataset_obj = Dataset(id=id, data=data, description=description, tags=tags) assert dataset_obj.__contains__(_id=obj1.id)
def test_to_string() -> None: """Tests that UID generates an intuitive string.""" uid = UID(value=uuid.UUID(int=333779996850170035686993356951732753684)) assert str(uid) == "<UID: fb1bb0675bb74c49becee700ab0a1514>" assert uid.__repr__() == "<UID: fb1bb0675bb74c49becee700ab0a1514>"
def test_serde_storable_obj() -> None: id = UID() data = th.Tensor([-1, -2, -3, -4]) description = "This is a dummy id" tags = ["dummy", "test"] obj1 = StorableObject(id=id, data=data, description=description, tags=tags) id = UID() data = th.Tensor([1, 2, 3, 4]) description = "This is a dummy tensor n1" tags = ["dummy", "test"] obj2 = StorableObject(id=id, data=data, description=description, tags=tags) id = UID() data = th.Tensor([10, 20, 30, 40]) description = "This is a dummy tensor n2" tags = ["dummy", "test"] obj3 = StorableObject(id=id, data=data, description=description, tags=tags) id = UID() data = [obj1, obj2, obj3] description = "This is a dataset" tags = ["dummy", "dataset"] dataset_obj = Dataset(id=id, data=data, description=description, tags=tags) blob = sy.serialize(obj=dataset_obj) sy.deserialize(blob=blob)
def test_uid_proto_deserialization() -> None: """Tests that proto UID deserialization works as expected""" uid = UID(value=uuid.UUID(int=333779996850170035686993356951732753684)) blob = _serialize(obj=uid) obj = sy.deserialize(blob=blob, from_proto=True) assert obj == UID(value=uuid.UUID(int=333779996850170035686993356951732753684))
def test_object_with_id_default_deserialization() -> None: """Tests that default ObjectWithID deserialization works as expected - from Protobuf""" uid = UID(value=uuid.UUID(int=333779996850170035686993356951732753684)) obj = ObjectWithID(id=uid) blob = ObjectWithID.get_protobuf_schema()(id=uid.serialize()) obj2 = sy.deserialize(blob=blob) assert obj == obj2
def test_from_string() -> None: """Tests that UID can be deserialized by a human readable string.""" uid_str = "fb1bb067-5bb7-4c49-bece-e700ab0a1514" uid = UID.from_string(value=uid_str) uid_comp = UID(value=uuid.UUID(int=333779996850170035686993356951732753684)) assert str(uid) == "<UID: fb1bb0675bb74c49becee700ab0a1514>" assert uid.__repr__() == "<UID: fb1bb0675bb74c49becee700ab0a1514>" assert uid == uid_comp
def test_uid_proto_serialization() -> None: """Tests that proto UID serialization works as expected""" uid = UID(value=uuid.UUID(int=333779996850170035686993356951732753684)) blob = _serialize(obj=uid) assert uid.proto() == blob assert uid.to_proto() == blob assert uid.serialize(to_proto=True) == blob
def test_serde_storable_obj() -> None: id = UID() data = UID() description = "This is a dummy test" tags = ["dummy", "test"] obj = StorableObject(id=id, data=data, description=description, tags=tags) blob = sy.serialize(obj=obj) sy.deserialize(blob=blob)
def test_object_with_id_proto_serialization() -> None: """Tests that default ObjectWithID serialization works as expected - to Protobuf""" uid = UID(value=uuid.UUID(int=333779996850170035686993356951732753684)) obj = ObjectWithID(id=uid) blob = ObjectWithID.get_protobuf_schema()(id=uid.serialize()) assert obj.proto() == blob assert obj.to_proto() == blob assert obj.serialize(to_proto=True) == blob
def test_uid_binary_serialization() -> None: """Tests that binary UID serializes as expected""" uid = UID(value=uuid.UUID(int=333779996850170035686993356951732753684)) blob = ( b"\n\x18syft.core.common.uid.UID\x12\x12\n\x10\xfb\x1b\xb0" + b"g[\xb7LI\xbe\xce\xe7\x00\xab\n\x15\x14" ) assert uid.binary() == blob assert uid.to_bytes() == blob assert uid.serialize(to_bytes=True) == blob
def test_uid_comparison() -> None: """Tests that two UIDs can be compared and will correctly evaluate""" uid1 = UID() uid2 = UID() # first do some basic checks assert uid1 == uid1 assert uid1 != uid2 # since the comparison should be based on the underlying value # let's make sure it's comparing based on the value instead of # just based on some other attribute of the object. uid2.value = uid1.value assert uid1 == uid2
def generate_id_obj( data: th.Tensor, description: str, tags: List[str] ) -> Tuple[UID, StorableObject]: id = UID() obj = StorableObject(id=id, data=data, description=description, tags=tags) return id, obj
def test_object_with_id_default_serialization() -> None: """Tests that default ObjectWithID serialization works as expected - to Protobuf""" uid = UID(value=uuid.UUID(int=333779996850170035686993356951732753684)) obj = ObjectWithID(id=uid) blob = sy.serialize(obj, to_proto=True) assert sy.serialize(obj) == blob
def test_create_dataset_with_store_obj() -> None: id = UID() data = UID() description = "This is a dummy id" tags = ["dummy", "test"] obj1 = StorableObject(id=id, data=data, description=description, tags=tags) id = UID() data = th.Tensor([1, 2, 3, 4]) description = "This is a dummy tensor" tags = ["dummy", "test"] obj2 = StorableObject(id=id, data=data, description=description, tags=tags) id = UID() data = [obj1, obj2] description = "This is a dummy tensor" tags = ["dummy", "dataset"] Dataset(id=id, data=data, description=description, tags=tags)
def test_uid_binary_deserialization() -> None: """Test that binary deserialization works as expected""" blob = (b"\n\x18syft.core.common.uid.UID\x12\x12\n\x10\xfb\x1b\xb0" + b"g[\xb7LI\xbe\xce\xe7\x00\xab\n\x15\x14") obj = sy.deserialize(blob=blob, from_bytes=True) assert obj == UID(value=uuid.UUID( int=333779996850170035686993356951732753684))
def test_uid_hash() -> None: """Tests that a UID hashes correctly. If this test fails then it means that the uuid.UUID library changed or we tried to swap it out for something else. Are you sure you want to do this?""" uid = UID(value=uuid.UUID(int=333779996850170035686993356951732753684)) assert hash(uid) == 1705855162796767136 assert hash(uid.value.int) == 1705855162796767136 fake_dict = {} fake_dict[uid] = "Just testing we can use it as a key in a dictionary"
def test_fail_process_request_service() -> None: addr = Address() request = RequestMessage( object_id=UID(), address=addr, requester_verify_key=get_verify_key(), owner_address=addr, ) with raises(Exception, match="No way to dispatch Deny Message."): request.deny()
def test_fail_accept_request_message() -> None: addr = Address() request = RequestMessage( object_id=UID(), address=addr, requester_verify_key=get_verify_key(), owner_address=addr, ) with raises(Exception, match="No way to dispatch Accept Message."): request.accept()
def test_request_answer_message() -> None: addr = Address() msg = RequestAnswerMessage(request_id=UID(), address=addr, reply_to=addr) serialized = serialize(obj=msg) new_msg = deserialize(blob=serialized) assert msg.request_id == new_msg.request_id assert msg.address == new_msg.address assert msg.reply_to == new_msg.reply_to
def test_serde_storable_obj_2() -> None: id = UID() data = th.Tensor([1, 2, 3, 4]) description = "This is a dummy test" tags = ["dummy", "test"] obj = StorableObject(id=id, data=data, description=description, tags=tags) blob = serialize(obj) ds_obj = sy.deserialize(blob=blob) assert obj.id == ds_obj.id assert (obj.data == ds_obj.data).all() assert obj.description == ds_obj.description assert obj.tags == ds_obj.tags
def test_dataset_get_element_fail() -> None: id = UID() data = UID() description = "This is a dummy id" tags = ["dummy", "test"] obj1 = StorableObject(id=id, data=data, description=description, tags=tags) id = UID() data = th.Tensor([1, 2, 3, 4]) description = "This is a dummy tensor n1" tags = ["dummy", "test"] obj2 = StorableObject(id=id, data=data, description=description, tags=tags) id = UID() data = th.Tensor([10, 20, 30, 40]) description = "This is a dummy tensor n2" tags = ["dummy", "test"] obj3 = StorableObject(id=id, data=data, description=description, tags=tags) id = UID() data = [obj1, obj2, obj3] description = "This is a dataset" tags = ["dummy", "dataset"] dataset_obj = Dataset(id=id, data=data, description=description, tags=tags) assert dataset_obj.__getitem__(UID()) == []
def test_object_with_id_binary_serialization() -> None: """Tests that binary ObjectWithID serializes as expected""" uid = UID(value=uuid.UUID(int=333779996850170035686993356951732753684)) obj = ObjectWithID(id=uid) blob = ( b"\n$syft.core.common.object.ObjectWithID\x12\x14\n\x12\n\x10\xfb\x1b\xb0" + b"g[\xb7LI\xbe\xce\xe7\x00\xab\n\x15\x14") assert sy.serialize(obj, to_bytes=True) == blob assert sy.serialize(obj, to_bytes=True) == blob assert sy.serialize(obj, to_bytes=True) == blob
def test_serde_storable_obj_2() -> None: id = UID() data = th.Tensor([-1, -2, -3, -4]) description = "This is a dummy id" tags = ["dummy", "test"] obj1 = StorableObject(id=id, data=data, description=description, tags=tags) id = UID() data = th.Tensor([1, 2, 3, 4]) description = "This is a dummy tensor n1" tags = ["dummy", "test"] obj2 = StorableObject(id=id, data=data, description=description, tags=tags) id = UID() data = th.Tensor([10, 20, 30, 40]) description = "This is a dummy tensor n2" tags = ["dummy", "test"] obj3 = StorableObject(id=id, data=data, description=description, tags=tags) id = UID() data = [obj1, obj2, obj3] description = "This is a dataset" tags = ["dummy", "dataset"] obj = Dataset(id=id, data=data, description=description, tags=tags) blob = serialize(obj) ds_obj = sy.deserialize(blob=blob) assert obj.id == ds_obj.id assert obj.description == ds_obj.description assert obj.tags == ds_obj.tags assert len(obj.data) == len(ds_obj.data) for i in range(len(obj.data)): assert obj.data[i].id == ds_obj.data[i].id assert th.all(th.eq(obj.data[i].data, ds_obj.data[i].data)) assert obj.data[i].description == ds_obj.data[i].description assert obj.data[i].tags == ds_obj.data[i].tags
def test_request_answer_response() -> None: addr = Address() msg = RequestAnswerResponse( request_id=UID(), address=addr, status=RequestStatus.Pending ) serialized = serialize(obj=msg) new_msg = deserialize(blob=serialized) assert msg.request_id == new_msg.request_id assert msg.address == new_msg.address assert msg.status == new_msg.status
def test_request_message() -> None: addr = Address() msg = RequestMessage( object_id=UID(), address=addr, requester_verify_key=get_verify_key(), owner_address=addr, request_description="test description", ) deserialized_obj = serialize(obj=msg) new_obj = deserialize(blob=deserialized_obj) assert msg.request_description == new_obj.request_description assert msg.address == new_obj.address assert msg.owner_address == new_obj.owner_address assert msg.object_id == new_obj.object_id assert msg.requester_verify_key == get_verify_key()
def test_deny(method_name: str) -> None: node = Domain(name="remote domain") node_client = node.get_root_client() addr = Address() request = RequestMessage( object_id=UID(), address=addr, requester_verify_key=get_verify_key(), owner_address=addr, owner_client_if_available=node_client, ) with patch.object(request.owner_client_if_available, "send_immediate_msg_without_reply") as mock_send_msg: getattr(request, method_name)() assert mock_send_msg.call_args[1]["msg"].address == node_client.address assert mock_send_msg.call_args[1]["msg"].accept is False assert mock_send_msg.call_args[1]["msg"].request_id == request.id
def test_from_string_exception() -> None: """Tests that UID throws exception when invalid string is given.""" with pytest.raises(Exception): UID.from_string(value="Hello world")
def test_uid_creates_value_if_try_to_init_none() -> None: """Tests that the UID class will create an ID if you explicitly try to init with None""" uid = UID(value=None) assert uid.value is not None assert isinstance(uid.value, uuid_type)