def test_msgpack_for_ipc_encode_deoode(self): expected_struct: list = [ 1, -1, b'123456', b'', "hello", "", create_address(), None ] data_list: list = [] for value in expected_struct: data_list.append(MsgPackForIpc.encode(value)) data: bytes = MsgPackForIpc.dumps(data_list) struct: list = MsgPackForIpc.loads(data) actual_struct: list = [ MsgPackForIpc.decode(TypeTag.INT, struct[0]), MsgPackForIpc.decode(TypeTag.INT, struct[1]), MsgPackForIpc.decode(TypeTag.BYTES, struct[2]), MsgPackForIpc.decode(TypeTag.BYTES, struct[3]), MsgPackForIpc.decode(TypeTag.STRING, struct[4]), MsgPackForIpc.decode(TypeTag.STRING, struct[5]), MsgPackForIpc.decode(TypeTag.ADDRESS, struct[6]), MsgPackForIpc.decode(TypeTag.NIL, struct[7]) ] self.assertEqual(expected_struct, actual_struct)
def test_iiss_data_converter_encode_decode_none_raise_exception(self): with self.assertRaises(InvalidParamsException) as cm: none_value = "wrong_data" data: bytes = MsgPackForIpc.encode(none_value) MsgPackForIpc.decode(TypeTag.NIL, data) self.assertEqual(cm.exception.message, f"Invalid tag type:{TypeTag.NIL} value: {data}")
def _encode_msg(self, obj: Any): if isinstance(obj, (int, bytes, str, Address, bool)): return MsgPackForIpc.encode(obj) elif isinstance(obj, list): tmp: list = [] for i in obj: tmp.append(self._encode_msg(i)) return tmp elif isinstance(obj, dict): tmp: dict = {} for k, v in obj.items(): tmp[self._encode_msg(k)] = self._encode_msg(v) return tmp
def test_iiss_data_converter_encode_decode_none(self): none_value = None data: bytes = MsgPackForIpc.encode(none_value) ret_none = MsgPackForIpc.decode(TypeTag.NIL, data) self.assertEqual(none_value, ret_none)
def test_iiss_data_converter_encode_decode_bool(self): bool_value: bool = True data: bytes = MsgPackForIpc.encode(bool_value) ret_bool: bool = bool(MsgPackForIpc.decode(TypeTag.INT, data)) self.assertEqual(bool_value, ret_bool)
def test_iiss_data_converter_encode_decode_bytes(self): bytes_value: bytes = b'byte_value' data: bytes = MsgPackForIpc.encode(bytes_value) ret_bytes: bytes = MsgPackForIpc.decode(TypeTag.BYTES, data) self.assertEqual(bytes_value, ret_bytes)
def test_iiss_data_converter_encode_decode_str(self): str_value: str = "str_value" data: bytes = MsgPackForIpc.encode(str_value) ret_str: str = MsgPackForIpc.decode(TypeTag.STRING, data) self.assertEqual(str_value, ret_str)
def test_iiss_data_converter_encode_decode_int(self): int_value: int = 10 data: bytes = MsgPackForIpc.encode(int_value) ret_int: int = MsgPackForIpc.decode(TypeTag.INT, data) self.assertEqual(int_value, ret_int)
def test_msg_pack_for_ipc_encode_decode_none(self): none_value = None data: bytes = MsgPackForIpc.encode(none_value) ret_none = MsgPackForIpc.decode(TypeTag.NIL, data) self.assertEqual(none_value, ret_none)
def test_msg_pack_for_ipc_encode_decode_bool(self): bool_value: bool = True data: bytes = MsgPackForIpc.encode(bool_value) ret_bool: bool = bool(MsgPackForIpc.decode(TypeTag.INT, data)) self.assertEqual(bool_value, ret_bool)
def test_msg_pack_for_ipc_encode_decode_bytes(self): bytes_value: bytes = b'byte_value' data: bytes = MsgPackForIpc.encode(bytes_value) ret_bytes: bytes = MsgPackForIpc.decode(TypeTag.BYTES, data) self.assertEqual(bytes_value, ret_bytes)
def test_msg_pack_for_ipc_encode_decode_str(self): str_value: str = "str_value" data: bytes = MsgPackForIpc.encode(str_value) ret_str: str = MsgPackForIpc.decode(TypeTag.STRING, data) self.assertEqual(str_value, ret_str)
def test_msg_pack_for_ipc_encode_decode_int(self): int_value: int = 10 data: bytes = MsgPackForIpc.encode(int_value) ret_int: int = MsgPackForIpc.decode(TypeTag.INT, data) self.assertEqual(int_value, ret_int)