def test_init_with_invalid_field(self) -> None: with self.assertRaises(TypeError): # pyre-ignore[28]: intentionally used a wrong field name "val_lists" for test easy(val=1, an_int=Integers(small=300), name="foo", val_lists=[1, 2, 3, 4])
def test_required_with_defaults(self) -> None: re = easy(val=10) x = mixed(req_easy_ref=re) self.assertEqual(x.opt_field, "optional") self.assertEqual(x.req_field, "required") self.assertEqual(x.unq_field, "unqualified") self.assertTrue(x) y = mixed(req_field="foo", opt_field="bar", unq_field="baz", req_easy_ref=re) self.assertTrue(x) z = y(req_field=None, opt_field=None, unq_field=None) self.assertEqual(x, z) self.assertTrue(x) self.assertIsNone(x.opt_easy_ref) self.assertIsNone(y.opt_easy_ref) self.assertIsNone(z.opt_easy_ref) e = easy(val=5) z = x(opt_easy_ref=e, req_easy_ref=re) self.assertEqual(z.opt_easy_ref, e) self.assertNotEqual(x, y) y = z(opt_easy_ref=None) self.assertIsNone(y.opt_easy_ref) self.assertEqual(y, x) with self.assertRaises(TypeError): z(req_easy_ref=None) # type: ignore
def test_update_conflicting_nested_fields(self) -> None: n = Nested1(a=Nested2(b=Nested3(c=easy(val=42, name="foo")))) with self.assertRaises(ValueError): n = Struct.update_nested_field( n, { "a.b.c": easy(val=128), "a.b.c.val": 256, }, )
def test_call_replace(self) -> None: x = easy(val=1, an_int=Integers(small=300), name="foo") y = x(name="bar") self.assertNotEqual(x.name, y.name) z = y(an_int=None, val=4) self.assertNotEqual(x.an_int, z.an_int) self.assertNotEqual(x.val, z.val) self.assertIsNone(z.an_int.value) self.assertEqual(y.val, x.val) self.assertEqual(y.an_int, x.an_int) x = easy() self.assertIsNotNone(x.val) self.assertIsNotNone(x.val_list) self.assertIsNone(x.name) self.assertIsNotNone(x.an_int)
def test_optional_struct_creation(self) -> None: with self.assertRaises(TypeError): easy(1, [1, 1], "test", Integers(tiny=1)) # type: ignore easy(val=1, an_int=Integers(small=500)) with self.assertRaises(TypeError): easy(name=b"binary") # type: ignore # Only Required Fields don't accept None easy(val=5, an_int=None)
def test_optional_struct_creation(self) -> None: with self.assertRaises(TypeError): easy(1, [1, 1], 'test', Integers(tiny=1)) # type: ignore easy(val=1, an_int=Integers(small=500)) with self.assertRaises(TypeError): easy(name=b'binary') # type: ignore # Only Required Fields don't accept None easy(val=5, an_int=None)
def test_complexunion_fromValue(self) -> None: tiny = 2 ** 7 - 1 large = 2 ** 63 - 1 afloat = 3.141592025756836 # Hand crafted to be representable as float adouble = 3.14159265358 union = ComplexUnion.fromValue(tiny) self.assertEqual(union.type, ComplexUnion.Type.tiny) union = ComplexUnion.fromValue(large) self.assertEqual(union.type, ComplexUnion.Type.large) union = ComplexUnion.fromValue(afloat) self.assertEqual(union.value, afloat) self.assertEqual(union.type, ComplexUnion.Type.float_val) union = ComplexUnion.fromValue(adouble) self.assertEqual(union.value, adouble) self.assertEqual(union.type, ComplexUnion.Type.double_val) union = ComplexUnion.fromValue(Color.red) self.assertEqual(union.type, ComplexUnion.Type.color) union = ComplexUnion.fromValue(easy()) self.assertEqual(union.type, ComplexUnion.Type.easy_struct) union = ComplexUnion.fromValue("foo") self.assertEqual(union.type, ComplexUnion.Type.text) union = ComplexUnion.fromValue(b"ar") self.assertEqual(union.type, ComplexUnion.Type.raw) union = ComplexUnion.fromValue(True) self.assertEqual(union.type, ComplexUnion.Type.truthy)
def test_with_header_binary(self) -> None: control = easy(val=6, val_list=[5, 4, 3, 2, 1]) iobuf = serialize_with_header( control, protocol=Protocol.BINARY, transform=Transform.ZLIB_TRANSFORM ) decoded = deserialize_from_header(easy, iobuf) self.assertEqual(control, decoded)
def test_with_header_iobuf_binary(self) -> None: control = easy(val=6, val_list=[5, 4, 3, 2, 1]) iobuf = serialize_with_header_iobuf( control, protocol=Protocol.BINARY, transform=Transform.ZLIB_TRANSFORM ) decoded = deserialize_from_header(easy, iobuf) self.assertEqual(control, decoded)
def test_none_arguments(self) -> None: client = TestingService() with self.assertRaises(TypeError): # missing argument # pyre-fixme[20]: Argument `what` expected. client.take_it_easy(9) with self.assertRaises(TypeError): # Should be an easy type # pyre-fixme[6]: Expected `easy` for 2nd param but got `None`. client.take_it_easy(9, None) with self.assertRaises(TypeError): # Should not be None # pyre-fixme[6]: Expected `Sequence[int]` for 1st param but got `None`. client.takes_a_list(None) with self.assertRaises(TypeError): # Should be a bool # pyre-fixme[6]: Expected `bool` for 1st param but got `None`. client.invert(None) with self.assertRaises(TypeError): # None is not a Color # pyre-fixme[6]: Expected `Color` for 1st param but got `None`. client.pick_a_color(None) with self.assertRaises(TypeError): # None is not an int # pyre-fixme[6]: Expected `int` for 1st param but got `None`. client.take_it_easy(None, easy())
def test_from_thread_pool(self) -> None: control = easy(val=5, val_list=[1, 2, 3, 4]) loop = asyncio.get_event_loop() coro = loop.run_in_executor(None, serialize, control) encoded = loop.run_until_complete(coro) coro = loop.run_in_executor(None, deserialize, type(control), encoded) decoded = loop.run_until_complete(coro) self.assertEqual(control, decoded)
def test_struct(self) -> None: x = easy(val=1, an_int=Integers(small=300), name="foo", val_list=[1, 2, 3, 4]) self.assertTrue(inspectable(x)) self.assertTrue(inspectable(easy)) r = inspect(x) self.assertEqual(r.name, "easy") self.assertEqual(r.kind, StructType.STRUCT) self.assertEqual(r.annotations, {"anno1": "foo", "bar": "1"})
def test_optional_struct_creation(self) -> None: with self.assertRaises(TypeError): # pyre-fixme[19]: Expected 0 positional arguments. easy(1, [1, 1], "test", Integers(tiny=1)) easy(val=1, an_int=Integers(small=500)) with self.assertRaises(TypeError): # pyre-fixme[6]: Expected `Optional[str]` for 1st param but got `bytes`. easy(name=b"binary") # Only Required Fields don't accept None easy(val=5, an_int=None)
def test_copy(self) -> None: x = easy(val=1, an_int=Integers(small=300), name="foo", val_list=[1, 2, 3, 4]) dif_list = copy.copy(x.val_list) self.assertEqual(x.val_list, dif_list) dif_int = copy.copy(x.an_int) self.assertEqual(x.an_int, dif_int)
def thrift_serialization_round_robin(self, control: Struct) -> None: control = easy(val=5, val_list=[1, 2, 3, 4]) 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_private_cpp_ref_field(self) -> None: x = PrivateCppRefField( field1=easy(val=1, name="11"), field2=easy(val=2, name="22"), field3=easy(val=3, name="33"), ) field1 = x.field1 field2 = x.field2 field3 = x.field3 if field1: self.assertEqual(field1.val, 1) self.assertEqual(field1.name, "11") if field2: self.assertEqual(field2.val, 2) self.assertEqual(field2.name, "22") if field3: self.assertEqual(field3.val, 3) self.assertEqual(field3.name, "33")
def test_thrift_serialization_round_robin(self): control = easy(val=5, val_list=[1, 2, 3, 4]) for proto in Protocol: encoded = serialize(control, protocol=proto) self.assertIsInstance(encoded, bytes) decoded = deserialize(easy, encoded, protocol=proto) self.assertIsInstance(decoded, easy) self.assertEqual(control, decoded)
def test_update_invalid_nested_fields(self) -> None: n = Nested1(a=Nested2(b=Nested3(c=easy(val=42, name="foo")))) with self.assertRaises(ValueError): Struct.update_nested_field(n, {"": 0}) with self.assertRaises(ValueError): Struct.update_nested_field(n, {"e": 0}) with self.assertRaises(ValueError): Struct.update_nested_field(n, {"a.b.e": 0}) with self.assertRaises(ValueError): Struct.update_nested_field(n, {"a.e.f": 0})
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_str(self) -> None: self.assertEqual( "easy(val=0, val_list=i[], name=None, an_int=Integers(type=EMPTY, value=None))", str(easy()), ) self.assertEqual( "easy(val=0, val_list=i[], name=None, an_int=Integers(type=EMPTY, value=None))", repr(easy()), ) x = easy(val=1, an_int=Integers(small=300), name="foo", val_list=[1, 2, 3, 4]) self.assertEqual( "easy(val=1, val_list=i[1, 2, 3, 4], name='foo', an_int=Integers(type=small, value=300))", str(x), ) self.assertEqual( "easy(val=1, val_list=i[1, 2, 3, 4], name='foo', an_int=Integers(type=small, value=300))", repr(x), )
def test_update_multiple_nested_fields(self) -> None: n = Nested1(a=Nested2(b=Nested3(c=easy(val=42, name="foo")))) n = Struct.update_nested_field( n, { "a.b.c.name": "bar", "a.b.c.val": 256, }, ) self.assertEqual(n.a.b.c.name, "bar") self.assertEqual(n.a.b.c.val, 256)
def test_serialize_easy_struct(self) -> None: control = easy(val=5, val_list=[1, 2, 3, 4]) fixtures: Mapping[Protocol, bytes] = { Protocol.COMPACT: b"\x15\n\x19E\x02\x04\x06\x08,\x00\x00", Protocol.BINARY: b"\x08\x00\x01\x00\x00\x00\x05\x0f\x00\x02\x08\x00\x00\x00" b"\x04\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00" b"\x00\x00\x04\x0c\x00\x04\x00\x00", Protocol.JSON: b'{"val":5,"val_list":[1,2,3,4],"an_int":{}}', Protocol.COMPACT_JSON: b'{"1":{"i32":5},"2":{"lst":["i32",4,1,2,3,4]},"4"' b':{"rec":{}}}', } self.thrift_serialization_round_robin(control, fixtures)
def test_sanity(self) -> None: with self.assertRaises(TypeError): serialize(1, Protocol.COMPACT) # type: ignore with self.assertRaises(TypeError): serialize(easy(), None) # type: ignore with self.assertRaises(TypeError): deserialize(Protocol, b"") # type: ignore with self.assertRaises(TypeError): deserialize(easy, Protocol) # type: ignore
def test_none_arguments(self): client = TestingService() with self.assertRaises(TypeError): client.take_it_easy(9) with self.assertRaises(TypeError): client.take_it_easy(9, None) # Should be an easy type with self.assertRaises(TypeError): client.takes_a_list(None) # Should not be None with self.assertRaises(TypeError): client.invert(None) # Should be a bool with self.assertRaises(TypeError): client.pick_a_color(None) # Should not be a None with self.assertRaises(TypeError): client.take_it_easy(None, easy())
def test_bad_deserialize(self) -> None: with self.assertRaises(Error): deserialize(easy, b"", protocol=Protocol.JSON) with self.assertRaises(Error): deserialize(easy, b"\x05AAAAAAAA") with self.assertRaises(Error): deserialize(easy, b"\x02\xDE\xAD\xBE\xEF", protocol=Protocol.BINARY) with self.assertRaises(BufferError): deserialize_from_header(easy, b"\x02\xDE\xAD\xBE\xEF") with self.assertRaises(Error): control = easy(val=5, val_list=[4, 3, 2, 1]) buf = serialize_with_header(control, transform=Transform.ZSTD_TRANSFORM) newBytes = bytearray(buf) newBytes[4] += 1 deserialize_from_header(easy, bytes(newBytes))
def test_sanity(self) -> None: with self.assertRaises(TypeError): # pyre-fixme[6]: Expected `sT` for 1st param but got `int`. serialize(1, Protocol.COMPACT) with self.assertRaises(TypeError): # pyre-fixme[6]: Expected `Protocol` for 2nd param but got `None`. serialize(easy(), None) with self.assertRaises(TypeError): # pyre-fixme[6]: Expected `Type[Variable[thrift.py3.serializer.sT (bound # to Struct)]]` for 1st param but got `Type[Protocol]`. deserialize(Protocol, b"") with self.assertRaises(TypeError): # pyre-fixme[6]: Expected `Union[bytearray, bytes, folly.iobuf.IOBuf, # memoryview]` for 2nd param but got `Type[Protocol]`. deserialize(easy, Protocol)
def test_none_arguments(self) -> None: client = TestingService() with self.assertRaises(TypeError): # missing argument client.take_it_easy(9) # type: ignore with self.assertRaises(TypeError): # Should be an easy type client.take_it_easy(9, None) # type: ignore with self.assertRaises(TypeError): # Should not be None client.takes_a_list(None) # type: ignore with self.assertRaises(TypeError): # Should be a bool client.invert(None) # type: ignore with self.assertRaises(TypeError): # None is not a Color client.pick_a_color(None) # type: ignore with self.assertRaises(TypeError): # None is not an int client.take_it_easy(None, easy()) # type: ignore
def test_hashability(self) -> None: hash(easy())
def test_pickle_easy_struct(self) -> None: control = easy(val=0, val_list=[5, 6, 7]) self.pickle_round_robin(control)
def test_hashability(self) -> None: hash(easy().val_list) hash(I32List(range(10)))
def test_serialize_iobuf(self) -> None: control = easy(val=5, val_list=[1, 2, 3, 4, 5]) iobuf = serialize_iobuf(control) decoded = deserialize(type(control), iobuf) self.assertEqual(control, decoded)
def test_with_header(self) -> None: control = easy(val=5, val_list=[4, 3, 2, 1]) iobuf = serialize_with_header(control, transform=Transform.ZSTD_TRANSFORM) decoded = deserialize_from_header(easy, iobuf) self.assertEqual(control, decoded)
def test_with_header_iobuf_json(self) -> None: control = easy(val=4, val_list=[3, 2, 1]) iobuf = serialize_with_header_iobuf(control, protocol=Protocol.JSON) decoded = deserialize_from_header(easy, iobuf) self.assertEqual(control, decoded)
def test_with_header_json(self) -> None: control = easy(val=4, val_list=[3, 2, 1]) iobuf = serialize_with_header(control, protocol=Protocol.JSON) decoded = deserialize_from_header(easy, iobuf) self.assertEqual(control, decoded)
def test_with_header_iobuf(self) -> None: control = easy(val=5, val_list=[4, 3, 2, 1]) iobuf = serialize_with_header_iobuf(control, transform=Transform.ZSTD_TRANSFORM) decoded = deserialize_from_header(easy, iobuf) self.assertEqual(control, decoded)
def test_copy(self) -> None: x = easy(val=1, an_int=Integers(small=300), name='foo', val_list=[1, 2, 3, 4]) dif_list = copy.copy(x.val_list) self.assertEqual(x.val_list, dif_list) dif_int = copy.copy(x.an_int) self.assertEqual(x.an_int, dif_int)