def test_pack_exceptions(self): for (name, obj, exception) in pack_exception_test_vectors: obj_repr = repr(obj) print("\tTesting %s: object %s" % (name, obj_repr if len(obj_repr) < 24 else obj_repr[0:24] + "...")) with self.assertRaises(exception): umsgpack.packb(obj)
def test_pack_composite(self): for (name, obj, data) in composite_test_vectors: obj_repr = repr(obj) print("\tTesting %s: object %s" % (name, obj_repr if len(obj_repr) < 24 else obj_repr[0:24] + "...")) self.assertEqual(umsgpack.packb(obj), data)
def test_pack_naive_timestamp(self): for (name, obj, data, _) in naive_timestamp_test_vectors: obj_repr = repr(obj) print("\t Testing %s: object %s" % (name, obj_repr if len(obj_repr) < 24 else obj_repr[0:24] + "...")) packed = umsgpack.packb(obj) self.assertEqual(packed, data)
def test_pack_ext_handler(self): for (name, obj, data) in ext_handlers_test_vectors: obj_repr = repr(obj) print("\tTesting %s: object %s" % (name, obj_repr if len(obj_repr) < 24 else obj_repr[0:24] + "...")) packed = umsgpack.packb(obj, ext_handlers=ext_handlers) self.assertEqual(packed, data)
def test_pack_ext_override(self): # Test overridden packing of datetime.datetime (name, obj, data) = override_ext_handlers_test_vectors[0] obj_repr = repr(obj) print( "\tTesting %s: object %s" % (name, obj_repr if len(obj_repr) < 24 else obj_repr[0:24] + "...")) packed = umsgpack.packb(obj, ext_handlers=override_ext_handlers) self.assertEqual(packed, data)
def test_pack_force_float_precision(self): for ((name, obj, data), precision) in zip(float_precision_test_vectors, ["single", "double"]): obj_repr = repr(obj) print("\tTesting %s: object %s" % (name, obj_repr if len(obj_repr) < 24 else obj_repr[0:24] + "...")) packed = umsgpack.packb(obj, force_float_precision=precision) self.assertEqual(packed, data)
def test_pack_compatibility(self): umsgpack.compatibility = True for (name, obj, data) in compatibility_test_vectors: obj_repr = repr(obj) print("\tTesting %s: object %s" % (name, obj_repr if len(obj_repr) < 24 else obj_repr[0:24] + "...")) self.assertEqual(umsgpack.packb(obj), data) umsgpack.compatibility = False
datetime.datetime(2200, 1, 1, 10, 5, 2, 1234, umsgpack._utc_tzinfo) ], [ "96-bit timestamp (naive)", datetime.datetime(3000, 1, 1, 10, 5, 2, 1234, umsgpack._utc_tzinfo), b"\xc7\x0c\xff\x00\x12\xd4\x50\x00\x00\x00\x07\x91\x5f\x59\xce", datetime.datetime(3000, 1, 1, 10, 5, 2, 1234, umsgpack._utc_tzinfo) ], ] CustomType = namedtuple('CustomType', ['x', 'y', 'z']) ext_handlers = { complex: lambda obj: umsgpack.Ext(0x20, struct.pack("<ff", obj.real, obj.imag)), CustomType: lambda obj: umsgpack.Ext(0x30, umsgpack.packb(list(obj))), 0x20: lambda ext: complex(*struct.unpack("<ff", ext.data)), 0x30: lambda ext: CustomType(*umsgpack.unpackb(ext.data)), } ext_handlers_test_vectors = [ ["complex", complex(1, 2), b"\xd7\x20\x00\x00\x80\x3f\x00\x00\x00\x40"], [ "custom type", CustomType(b"abc", 123, True), b"\xd7\x30\x93\xc4\x03\x61\x62\x63\x7b\xc3" ], ] override_ext_handlers = {
def pack(obj: Any) -> bytes: """ Serialize """ return umsgpack.packb(obj, ext_handlers=__pack_handlers)