def test_string_with_non_utf8_data(self) -> None: encoded = b"\x0b\x00\x01\x00\x00\x00\x03foo\x00" sb = deserialize(StringBucket, encoded, protocol=Protocol.BINARY) self.assertEqual("foo", sb.one) encoded = b"\x0b\x00\x01\x00\x00\x00\x03\xfa\xf0\xef\x00" sb = deserialize(StringBucket, encoded, protocol=Protocol.BINARY) with self.assertRaises(UnicodeDecodeError): # Accessing the property is when the string is decoded as UTF-8. sb.one
def test_serialize_deserialize(self) -> None: err = HardError(errortext="err", code=2) x = ValueOrError(error=err) serialized = serialize_iobuf(x) y = deserialize(ValueOrError, serialized) self.assertIsNot(x, y) self.assertEqual(x, y) serialized_direct = serialize_iobuf(err) deserialized_direct = deserialize(HardError, serialized_direct) self.assertIsNot(err, deserialized_direct) self.assertEqual(err, deserialized_direct)
def test_isset_Struct(self) -> None: to_serialize = OptionalFile(name="/dev/null", type=8) serialized = serialize_iobuf(to_serialize) file = deserialize(File, serialized) self.assertTrue(isset(file)["type"]) self.assertFalse(isset(file)["permissions"]) to_serialize = OptionalFile(name="/dev/null") serialized = serialize_iobuf(to_serialize) file = deserialize(File, serialized) self.assertEqual(file.type, Kind.REGULAR) self.assertFalse(isset(file)["type"])
def test_sanity(self) -> None: with self.assertRaises(TypeError): # pyre-ignore[6]: intentionally introduced for testing serialize(1, Protocol.COMPACT) with self.assertRaises(TypeError): # pyre-ignore[6]: intentionally introduced for testing serialize(easy(), None) with self.assertRaises(TypeError): # pyre-ignore[6]: intentionally introduced for testing deserialize(Protocol, b"")
def test_bad_enum_in_set_iter(self) -> None: x = deserialize( ColorGroups, serialize_iobuf(OptionalColorGroups(color_list=[1, 5, 0]))) for v in x.color_set: if v not in (Color.blue, Color.red): self.assertBadEnum(cast(BadEnum, v), Color, 5)
async def _fbthrift__handler_doRaise( self, args: _fbthrift_iobuf.IOBuf, protocol: Protocol) -> _fbthrift_iobuf.IOBuf: args_struct = deserialize( module.thrift_types._fbthrift_Raiser_doRaise_args, args, protocol) try: value = await self.doRaise() return_struct = module.thrift_types._fbthrift_Raiser_doRaise_result( ) except module.thrift_types.Banal as e: return_struct = module.thrift_types._fbthrift_Raiser_doRaise_result( b=e) buf = serialize_iobuf(return_struct, protocol) exp = PythonUserException('Banal', str(e), buf) raise exp except module.thrift_types.Fiery as e: return_struct = module.thrift_types._fbthrift_Raiser_doRaise_result( f=e) buf = serialize_iobuf(return_struct, protocol) exp = PythonUserException('Fiery', str(e), buf) raise exp except module.thrift_types.Serious as e: return_struct = module.thrift_types._fbthrift_Raiser_doRaise_result( s=e) buf = serialize_iobuf(return_struct, protocol) exp = PythonUserException('Serious', str(e), buf) raise exp return serialize_iobuf(return_struct, protocol)
def thrift_serialization_round_trip(self, control: StructOrUnion) -> None: for proto in Protocol: encoded = serialize(control, protocol=proto) self.assertIsInstance(encoded, bytes) decoded = deserialize(type(control), encoded, protocol=proto) self.assertIsInstance(decoded, type(control)) self.assertEqual(control, decoded)
def test_bad_enum_in_list_index(self) -> None: x = deserialize( ColorGroups, serialize_iobuf(OptionalColorGroups(color_list=[1, 5, 0]))) self.assertEqual(len(x.color_list), 3) self.assertEqual(x.color_list[0], Color.blue) self.assertBadEnum(cast(BadEnum, x.color_list[1]), Color, 5) self.assertEqual(x.color_list[2], Color.red)
def test_flag_enum_serialization_roundtrip(self) -> None: x = File(name="/dev/null", type=Kind.CHAR, permissions=Perm.read | Perm.write) y = deserialize(File, serialize_iobuf(x)) self.assertEqual(x, y) self.assertEqual(x.permissions, Perm.read | Perm.write) self.assertIsInstance(x.permissions, Perm)
async def _fbthrift__handler_doBland( self, args: _fbthrift_iobuf.IOBuf, protocol: Protocol) -> _fbthrift_iobuf.IOBuf: args_struct = deserialize( module.thrift_types._fbthrift_Raiser_doBland_args, args, protocol) value = await self.doBland() return_struct = module.thrift_types._fbthrift_Raiser_doBland_result() return serialize_iobuf(return_struct, protocol)
async def _fbthrift__handler_get200( self, args: _fbthrift_iobuf.IOBuf, protocol: Protocol) -> _fbthrift_iobuf.IOBuf: args_struct = deserialize( module.thrift_types._fbthrift_Raiser_get200_args, args, protocol) value = await self.get200() return_struct = module.thrift_types._fbthrift_Raiser_get200_result( success=value) return serialize_iobuf(return_struct, protocol)
def test_bad_enum_in_list_reverse(self) -> None: x = deserialize( ColorGroups, serialize_iobuf(OptionalColorGroups(color_list=[1, 5, 0]))) for idx, v in enumerate(reversed(x.color_list)): if idx == 0: self.assertEqual(v, Color.red) elif idx == 1: self.assertBadEnum(cast(BadEnum, v), Color, 5) else: self.assertEqual(v, Color.blue)
async def _fbthrift__handler_init( self, args: _fbthrift_iobuf.IOBuf, protocol: Protocol) -> _fbthrift_iobuf.IOBuf: args_struct = deserialize( test.namespace_from_package.module.thrift_types. _fbthrift_TestService_init_args, args, protocol) value = await self.init(args_struct.int1, ) return_struct = test.namespace_from_package.module.thrift_types._fbthrift_TestService_init_result( success=value) return serialize_iobuf(return_struct, protocol)
async def _fbthrift__handler_query( self, args: _fbthrift_iobuf.IOBuf, protocol: Protocol) -> _fbthrift_iobuf.IOBuf: args_struct = deserialize( test.fixtures.basic.module.thrift_types. _fbthrift_MyService_query_args, args, protocol) value = await self.query(args_struct.u, ) return_struct = test.fixtures.basic.module.thrift_types._fbthrift_MyService_query_result( success=value) return serialize_iobuf(return_struct, protocol)
async def _fbthrift__handler_init( self, args: _fbthrift_iobuf.IOBuf, protocol: Protocol) -> _fbthrift_iobuf.IOBuf: args_struct = deserialize( python_module_root.my.namespacing.test.hsmodule.thrift_types. _fbthrift_HsTestService_init_args, args, protocol) value = await self.init(args_struct.int1, ) return_struct = python_module_root.my.namespacing.test.hsmodule.thrift_types._fbthrift_HsTestService_init_result( success=value) return serialize_iobuf(return_struct, protocol)
async def _fbthrift__handler_init( self, args: _fbthrift_iobuf.IOBuf, protocol: Protocol) -> _fbthrift_iobuf.IOBuf: args_struct = deserialize( emptyns.thrift_types._fbthrift_TestService_init_args, args, protocol) value = await self.init(args_struct.int1, ) return_struct = emptyns.thrift_types._fbthrift_TestService_init_result( success=value) return serialize_iobuf(return_struct, protocol)
async def _fbthrift__handler_check( self, args: _fbthrift_iobuf.IOBuf, protocol: Protocol) -> _fbthrift_iobuf.IOBuf: args_struct = deserialize( python_module_root.my.namespacing.extend.test.extend.thrift_types. _fbthrift_ExtendTestService_check_args, args, protocol) value = await self.check(args_struct.struct1, ) return_struct = python_module_root.my.namespacing.extend.test.extend.thrift_types._fbthrift_ExtendTestService_check_result( success=value) return serialize_iobuf(return_struct, protocol)
def load(self, obj: Any) -> StructOrUnion: if obj.typeHashPrefixSha2_256: cls = find_by_universal_hash( self._alg_to_hash_to_type[UniversalHashAlgorithm.Sha2_256], obj.typeHashPrefixSha2_256, ) elif obj.type: cls = self._uri_to_type[obj.type] else: raise ValueError("No type information found") serializer_protocol = _to_serializer_protocol(obj.protocol) return deserialize(cls, obj.data, protocol=serializer_protocol)
def test_bad_deserialize(self) -> None: with self.assertRaises(Error): deserialize(easy, b"") with self.assertRaises(Error): deserialize(easy, b"\x05AAAAAAAA") with self.assertRaises(Error): deserialize(easy, b"\x02\xDE\xAD\xBE\xEF", protocol=Protocol.BINARY)
def test_bad_enum_in_map_lookup(self) -> None: x = deserialize( ColorGroups, serialize_iobuf( OptionalColorGroups(color_map={ 1: 2, 0: 5, 6: 1, 7: 8 })), ) val = x.color_map[Color.red] self.assertBadEnum(cast(BadEnum, val), Color, 5)
def test_bad_enum_in_map_values(self) -> None: x = deserialize( ColorGroups, serialize_iobuf( OptionalColorGroups(color_map={ 1: 2, 0: 5, 6: 1, 7: 8 })), ) s = set() for k in x.color_map.values(): s.add(k) self.assertEqual(len(s), 4) s.discard(Color.green) s.discard(Color.blue) lst = sorted(s, key=lambda e: cast(BadEnum, e).value) self.assertBadEnum(cast(BadEnum, lst[0]), Color, 5) self.assertBadEnum(cast(BadEnum, lst[1]), Color, 8)
def test_bad_enum_in_map_items(self) -> None: x = deserialize( ColorGroups, serialize_iobuf( OptionalColorGroups(color_map={ 1: 2, 0: 5, 6: 1, 7: 8 })), ) for k, v in x.color_map.items(): if k == Color.blue: self.assertEqual(v, Color.green) elif k == Color.red: self.assertBadEnum(cast(BadEnum, v), Color, 5) else: ck = cast(BadEnum, k) if ck.value == 6: self.assertEqual(v, Color.blue) else: self.assertBadEnum(cast(BadEnum, v), Color, 8)
def test_deserialize_nonempty(self) -> None: x = deserialize(Integers, serialize_iobuf(Integers(tiny=42))) self.assertEqual(x.type, Integers.Type.tiny)
async def _fbthrift__handler_simple_rpc(self, args: _fbthrift_iobuf.IOBuf, protocol: Protocol) -> _fbthrift_iobuf.IOBuf: args_struct = deserialize(test.fixtures.basic.module.thrift_types._fbthrift_FB303Service_simple_rpc_args, args, protocol) value = await self.simple_rpc(args_struct.int_parameter,) return_struct = test.fixtures.basic.module.thrift_types._fbthrift_FB303Service_simple_rpc_result(success=value) return serialize_iobuf(return_struct, protocol)
async def _fbthrift__handler_getDataByKey1(self, args: _fbthrift_iobuf.IOBuf, protocol: Protocol) -> _fbthrift_iobuf.IOBuf: args_struct = deserialize(test.fixtures.basic.module.thrift_types._fbthrift_DbMixedStackArguments_getDataByKey1_args, args, protocol) value = await self.getDataByKey1(args_struct.key,) return_struct = test.fixtures.basic.module.thrift_types._fbthrift_DbMixedStackArguments_getDataByKey1_result(success=value) return serialize_iobuf(return_struct, protocol)
async def _fbthrift__handler_rpc_skipped_codegen(self, args: _fbthrift_iobuf.IOBuf, protocol: Protocol) -> _fbthrift_iobuf.IOBuf: args_struct = deserialize(test.fixtures.basic.module.thrift_types._fbthrift_MyService_rpc_skipped_codegen_args, args, protocol) value = await self.rpc_skipped_codegen() return_struct = test.fixtures.basic.module.thrift_types._fbthrift_MyService_rpc_skipped_codegen_result() return serialize_iobuf(return_struct, protocol)
async def _fbthrift__handler_lobDataById(self, args: _fbthrift_iobuf.IOBuf, protocol: Protocol) -> None: args_struct = deserialize(test.fixtures.basic.module.thrift_types._fbthrift_MyService_lobDataById_args, args, protocol) value = await self.lobDataById(args_struct.id,args_struct.data,)
async def _fbthrift__handler_deleteDataById(self, args: _fbthrift_iobuf.IOBuf, protocol: Protocol) -> _fbthrift_iobuf.IOBuf: args_struct = deserialize(test.fixtures.basic.module.thrift_types._fbthrift_MyService_deleteDataById_args, args, protocol) value = await self.deleteDataById(args_struct.id,) return_struct = test.fixtures.basic.module.thrift_types._fbthrift_MyService_deleteDataById_result() return serialize_iobuf(return_struct, protocol)
def test_bad_enum_in_struct(self) -> None: to_serialize = OptionalFile(name="something", type=64) serialized = serialize_iobuf(to_serialize) x = deserialize(File, serialized) self.assertBadEnum(cast(BadEnum, x.type), Kind, 64)
def test_deserialize_empty(self) -> None: x = deserialize(Integers, serialize_iobuf(Integers())) self.assertEqual(x.type, Integers.Type.EMPTY)