예제 #1
0
def pack_unpack_test(
    obj: TypePlaceholder,
    comparator: Callable[[TypePlaceholder, TypePlaceholder],
                         bool] = lambda a, b: a == b,
    expect: bool = True,
    use_pickle: bool = False,
    serial_option: Optional[int] = None,
    deserial_option: Optional[int] = None,
    **kwargs: Any,
) -> None:
    r"""
    Tests packing and unpacking of an object

    :param obj: the object to pack/unpack
    :param comparator: function comparing two objects, returning True
        if they are equal
    :param expect: expected result of comparison
    :param serial_option: ormsgpack option for serialization
    :param deserial_option: ormsgpack option for deserialization
    :param \**kwargs: optional extra keyword arguments
    """
    msg_id = "test_id"
    obj_copy = copy.deepcopy(obj)
    msg_id_prime, obj_prime = Serialization.unpack(
        Serialization.pack(obj,
                           msg_id,
                           use_pickle=use_pickle,
                           option=serial_option,
                           **kwargs),
        use_pickle=use_pickle,
        option=deserial_option,
        **kwargs,
    )
    obj = obj_copy
    assert comparator(obj, obj_prime) == expect and msg_id == msg_id_prime
예제 #2
0
def test_custom_serialization_no_annotation() -> None:
    """
    Tests whether an AnnotationError exception is raised when serialization
    functions are not annotated
    """
    Serialization.clear_new_serialization_logic()
    with pytest.raises(AnnotationError):
        Serialization.set_serialization_logic(ClassNoAnnotation)
예제 #3
0
def test_custom_serialization_mismatch_type() -> None:
    """
    Tests whether an AnnotationError exception is raised when deserialization
    functions 'obj' and serialization return type mismatch
    """
    Serialization.clear_new_serialization_logic()
    with pytest.raises(AnnotationError):
        Serialization.set_serialization_logic(ClassMismatchType)
예제 #4
0
def test_custom_serialization_wrong_signature() -> None:
    """
    Tests whether a TypeError exception is raised when deserialization
    functions have wrong signature
    """
    Serialization.clear_new_serialization_logic()
    with pytest.raises(TypeError):
        Serialization.set_serialization_logic(ClassWrongSignature)
예제 #5
0
def test_custom_serialization_no_kwargs() -> None:
    """
    Tests whether a TypeError exception is raised when serialization functions do not include the
    **kwargs keyword.
    """
    Serialization.clear_new_serialization_logic()
    with pytest.raises(TypeError):
        Serialization.set_serialization_logic(
            ClassNoKwargs)  # type: ignore[arg-type]
예제 #6
0
def test_custom_serialization_no_functions() -> None:
    """
    Tests whether a TypeError exception is raised when custom  serialization
    functions are missing
    """
    Serialization.clear_new_serialization_logic()
    with pytest.raises(TypeError):
        Serialization.set_serialization_logic(
            ClassNoFunctions)  # type: ignore[arg-type]
예제 #7
0
def test_custom_serialization_no_logic() -> None:
    """
    Tests whether an AttributeError exception is raised when custom
    serialization logic is missing
    """
    Serialization.clear_new_serialization_logic()
    with pytest.raises(AttributeError):
        Serialization.set_serialization_logic(
            ClassNoLogic)  # type: ignore[arg-type]
예제 #8
0
def test_custom_serialization_correct(
        correct_class: Type[Union[ClassCorrect, ClassCorrect2]]) -> None:
    """
    Tests correctly implemented serialization logic

    :param correct_class: a correctly implemented serialization class
    """
    Serialization.clear_new_serialization_logic()
    Serialization.set_serialization_logic(correct_class)
    obj = correct_class(1)
    pack_unpack_test(obj, lambda a, b: a.value == b.value)
예제 #9
0
def test_custom_serialization_correct_kwargs() -> None:
    """
    Tests correctly implemented serialization logic making use of optional keyword arguments
    """
    ClassCorrectKwargs.origin = []
    ClassCorrectKwargs.destination = []
    Serialization.clear_new_serialization_logic()
    Serialization.set_serialization_logic(ClassCorrectKwargs)
    obj = ClassCorrectKwargs([1, 2, 3, 4], "test")
    pack_unpack_test(
        obj,
        lambda a, b: a.values == b.values and a.name == b.name,
        origin="origin",
        destination="destination",
    )
    assert ClassCorrectKwargs.origin == ["origin"]
    assert ClassCorrectKwargs.destination == ["destination"]
    ClassCorrectKwargs.origin = []
    ClassCorrectKwargs.destination = []
예제 #10
0
    def deserialize(obj: "Union[bytes, Dict[str, bytes]]",
                    **_kwargs: "Any") -> "ClassCorrect3":
        r"""
        Deserialization method

        :param obj: object to deserialize
        :param \**_kwargs: optional extra keyword arguments
        :return: deserializes object
        """
        dict_obj = Serialization.deserialize(obj, use_pickle=True)
        return ClassCorrect3(dict_obj["values"], dict_obj["name"])
예제 #11
0
async def test_http_server_custom_kwargs2(
        pool_http_2p: Tuple[Pool, Pool]) -> None:
    """
    Tests asynchronous sending and receiving of a custom object making use of keyword arguments
    between two communication pools

    :param pool_http_2p: collection of two communication pools
    """
    ClassCorrectKwargs.origin = []
    ClassCorrectKwargs.destination = []
    Serialization.clear_new_serialization_logic()
    Serialization.set_serialization_logic(ClassCorrectKwargs)
    Serialization.set_serialization_logic(ClassCorrectKwargs2)
    obj = ClassCorrectKwargs([1, 2, 3, 4], "test")
    obj2 = ClassCorrectKwargs2([5, 6, 7, 8], obj)
    pool_http_2p[0].asend("local1", obj2)
    res = await pool_http_2p[1].recv("local0")
    assert isinstance(obj2, type(res))
    assert res.values == obj2.values
    assert res.other.name == obj2.other.name
    assert res.other.values == obj2.other.values
    assert ClassCorrectKwargs.destination[0] == pool_http_2p[0].pool_handlers[
        "local1"]
    assert ClassCorrectKwargs.origin[0] == pool_http_2p[1].pool_handlers[
        "local0"]
    ClassCorrectKwargs.origin = []
    ClassCorrectKwargs.destination = []
예제 #12
0
    def serialize(self, **_kwargs: "Any") -> "Union[bytes, Dict[str, bytes]]":
        r"""
        Serialization method

        :param \**_kwargs: optional extra keyword arguments
        :return: serialized object
        """
        return Serialization.serialize(
            {
                "values": self.values,
                "name": self.name,
            },
            use_pickle=True,
        )
예제 #13
0
def test_custom_serialization_correct_double() -> None:
    """
    Tests whether a RepetitionError exception is raised when serialization
    functions is set twice
    """
    Serialization.clear_new_serialization_logic()
    with pytest.raises(RepetitionError):
        Serialization.set_serialization_logic(ClassCorrect)
        # setting logic twice makes no sense. This should return a RepetitionError
        Serialization.set_serialization_logic(ClassCorrect)
        obj = ClassCorrect(1)
        pack_unpack_test(obj, lambda a, b: a.value == b.value)
예제 #14
0
def test_custom_serialization_correct2(
    correct_class: Type[Union[ClassCorrect, ClassCorrect2]],
    correct_class_2: Type[ClassCorrect3],
) -> None:
    """
    Tests correctly implemented serialization logic

    :param correct_class: a correctly implemented serialization class
    :param correct_class_2: a correctly implemented serialization class
    """
    Serialization.clear_new_serialization_logic()
    Serialization.set_serialization_logic(correct_class)
    Serialization.set_serialization_logic(correct_class_2)
    obj = correct_class(1)
    pack_unpack_test(obj, lambda a, b: a.value == b.value)
    obj2 = correct_class_2([1, 2, 3, 4], "test")
    pack_unpack_test(obj2,
                     lambda a, b: a.values == b.values and a.name == b.name)