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_field_level_terse_write(self) -> None: obj = FieldLevelTerseStruct( bool_field=True, byte_field=1, short_field=2, int_field=3, long_field=4, float_field=5, double_field=6, string_field="7", binary_field=b"8", enum_field=MyEnum.ME1, list_field=[1], set_field={1}, map_field={1: 1}, struct_field=MyStruct(field1=1), ) empty = EmptyStruct() for proto in Protocol: encoded = serialize(obj, protocol=proto) decoded, length = deserialize_with_length( type(obj), encoded, protocol=proto ) self.assertIsInstance(decoded, type(obj)) self.assertEqual(decoded, obj) self.assertEqual(length, len(encoded)) # Set fields to their intrinsic default. obj = FieldLevelTerseStruct( bool_field=False, byte_field=0, short_field=0, int_field=0, long_field=0, float_field=0, double_field=0, string_field="", binary_field=b"", enum_field=MyEnum.ME0, list_field=[], set_field=set(), map_field={}, struct_field=MyStruct(field1=0), ) for proto in Protocol: encoded = serialize(obj, protocol=proto) encoded_empty = serialize(empty, protocol=proto) self.assertEqual(encoded, encoded_empty)
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_deserialize_with_length(self) -> None: control = easy(val=5, val_list=[1, 2, 3, 4, 5]) for proto in Protocol: encoded = serialize(control, protocol=proto) decoded, length = deserialize_with_length( type(control), encoded, protocol=proto ) self.assertIsInstance(decoded, type(control)) self.assertEqual(decoded, control) self.assertEqual(length, len(encoded))
def test_terse_struct_with_custom_default(self) -> None: empty = EmptyStruct() for proto in Protocol: encoded_empty = serialize(empty, protocol=proto) decoded, length = deserialize_with_length( TerseStructWithCustomDefault, encoded_empty, protocol=proto ) self.assertIsInstance(decoded, TerseStructWithCustomDefault) self.assertEqual(decoded.bool_field, False) self.assertEqual(decoded.byte_field, 0) self.assertEqual(decoded.short_field, 0) self.assertEqual(decoded.int_field, 0) self.assertEqual(decoded.long_field, 0) self.assertEqual(decoded.float_field, 0.0) self.assertEqual(decoded.double_field, 0.0) self.assertEqual(decoded.string_field, "") self.assertEqual(decoded.binary_field, b"") self.assertEqual(decoded.enum_field, MyEnum.ME0) self.assertEqual(decoded.list_field, []) self.assertEqual(decoded.set_field, set()) self.assertEqual(decoded.map_field, {}) self.assertEqual(decoded.struct_field, MyStructWithCustomDefault(field1=0))
def test_None(self) -> None: with self.assertRaises(TypeError): # pyre-ignore[6]: intentionally introduced for testing serialize(None)