def test_serializer_helpers_errors():
    """
    Check some errors that might occur from serializer helpers.
    """

    short_datas = [b'',b'f',b'4g',b'132']
    # The following should raise an exception:
    for data in short_datas:
        with pytest.raises(DeserializeError):
            d_string(data)

        with pytest.raises(DeserializeError):
            d_blob(data)

        with pytest.raises(DeserializeError):
            d_uint32(data)

    # The length prefix here is too long. We expect an exception:
    bad_prefix = b'123444'
    with pytest.raises(DeserializeError):
        d_string(bad_prefix)
    with pytest.raises(DeserializeError):
        d_string(bad_prefix)

    # UTF-8 decoding error:
    # See https://docs.python.org/3/howto/unicode.html
    bad_utf8 = b'\x04\x00\x00\x00\x80abc'
    with pytest.raises(DeserializeError) as excinfo:
        d_string(bad_utf8)
    assert 'utf-8' in str(excinfo.value)
    def deserialize(self,msg_data:bytes):
        """
        Deserialize data bytes into a msg_inst.
        """
        # Read the amount of similars:
        nl,num_sims = d_uint32(msg_data)
        msg_data = msg_data[nl:]

        sims = []
        for _ in range(num_sims):
            nl,sim_name = d_string(msg_data)
            msg_data = msg_data[nl:]
            nl,sim_comment = d_string(msg_data)
            msg_data = msg_data[nl:]
            nl,sim_grade = d_uint32(msg_data)
            msg_data = msg_data[nl:]

            sims.append(FSimilar(\
                    name=sim_name,\
                    comment=sim_comment,\
                    sim_grade=sim_grade\
                    ))

        msg_inst = self.get_msg()
        msg_inst.set_field('similars',sims)
        return msg_inst
    def deserialize(self,msg_data:bytes):
        """
        Deserialize data bytes into a msg_inst.
        """
        nl,func_data = d_blob(msg_data)
        msg_data = msg_data[nl:]
        nl,num_similars = d_uint32(msg_data)

        msg_inst = self.get_msg()
        msg_inst.set_field('func_data',func_data)
        msg_inst.set_field('num_similars',num_similars)
        return msg_inst
def test_serializer_helpers():
    """
    Generally check the validity of the serializer helpers:
    """
    my_str = 'Hello world!'
    my_blob = b'This is a blob'
    my_uint32 = 345235


    data = s_string(my_str) + s_blob(my_blob) + s_uint32(my_uint32)

    nextl,my_str_2 = d_string(data)
    data = data[nextl:]
    nextl,my_blob_2 = d_blob(data)
    data = data[nextl:]
    nextl,my_uint32_2 = d_uint32(data)
    data = data[nextl:]
    assert len(data) == 0