Beispiel #1
0
 def test_optional_struct_creation(self) -> None:
     with self.assertRaises(TypeError):
         # pyre-ignore[19]: for test
         easy(1, [1, 1], "test", Integers(tiny=1))
     easy(val=1, an_int=Integers(small=500))
     with self.assertRaises(TypeError):
         # pyre-ignore[6]: for test
         easy(name=b"binary")
     # Only Required Fields don't accept None
     easy(val=5, an_int=None)
Beispiel #2
0
 def test_union_usage(self) -> None:
     value = hash("i64")
     x = Integers(large=value)
     self.assertIsInstance(x, Union)
     self.assertEqual(x.type, x.get_type())
     self.assertEqual(x.type, Integers.Type.large)
     self.assertEqual(x.value, value)
     # Hashing Works
     s = {x}
     self.assertIn(x, s)
     # Repr is useful
     rx = repr(x)
     self.assertIn(str(value), rx)
     self.assertIn(x.type.name, rx)
Beispiel #3
0
 def test_init_with_invalid_field(self) -> None:
     with self.assertRaises(TypeError):
         # pyre-ignore[28]: for test
         easy(val=1,
              an_int=Integers(small=300),
              name="foo",
              val_lists=[1, 2, 3, 4])
Beispiel #4
0
 def test_union_ordering(self) -> None:
     x = Integers(tiny=4)
     y = ComplexUnion(tiny=1)
     with self.assertRaises(TypeError):
         # flake8: noqa: B015 intentionally introduced for test
         x < y
     # same type, compare value
     y = Integers(tiny=2)
     self.assertGreater(x, y)
     self.assertLess(y, x)
     # different type
     y = Integers(small=2)
     self.assertLess(x, y)
     self.assertGreater(y, x)
     # equality
     y = Integers(tiny=4)
     self.assertEqual(x, y)
Beispiel #5
0
 def test_deepcopy(self) -> None:
     x = easy(val=1,
              an_int=Integers(small=300),
              name="bar",
              val_list=[1, 2, 3, 4])
     dif = copy.deepcopy(x)
     self.assertEqual(x.val, dif.val)
     self.assertEqual(x.val_list, dif.val_list)
     self.assertEqual(x.name, dif.name)
Beispiel #6
0
 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)
Beispiel #7
0
 def test_union_dir(self) -> None:
     expected = [
         "digits",
         "large",
         "medium",
         "name_",
         "small",
         "tiny",
         "type",
         "unbounded",
         "value",
     ]
     self.assertEqual(expected, dir(Integers()))
     self.assertEqual(expected, dir(Integers))
Beispiel #8
0
 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)
Beispiel #9
0
 def test_constructor(self) -> None:
     self.assertEqual(Integers(small=2).type, Integers.Type.small)
     self.assertEqual(Integers(unbounded="123").type, Integers.Type.unbounded)
     self.assertEqual(
         Integers(
             digits=Digits(
                 data=[Integers(tiny=1), Integers(small=2), Integers(large=3)]
             )
         ).type,
         Integers.Type.digits,
     )
Beispiel #10
0
 def test_integers_fromValue(self) -> None:
     tiny = 2**7 - 1
     small = 2**15 - 1
     medium = 2**31 - 1
     large = 2**63 - 1
     string = "123"
     union = Integers.fromValue(tiny)
     self.assertEqual(union.type, Integers.Type.tiny)
     union = Integers.fromValue(small)
     self.assertEqual(union.type, Integers.Type.small)
     union = Integers.fromValue(medium)
     self.assertEqual(union.type, Integers.Type.medium)
     union = Integers.fromValue(large)
     self.assertEqual(union.type, Integers.Type.large)
     union = Integers.fromValue(string)
     self.assertEqual(union.type, Integers.Type.unbounded)
     union = Integers.fromValue(
         Digits(data=[Integers(tiny=1), Integers(unbounded="123")])
     )
     self.assertEqual(union.type, Integers.Type.digits)
Beispiel #11
0
 def test_deserialize_nonempty(self) -> None:
     x = deserialize(Integers, serialize_iobuf(Integers(tiny=42)))
     self.assertEqual(x.type, Integers.Type.tiny)
Beispiel #12
0
 def test_deserialize_empty(self) -> None:
     x = deserialize(Integers, serialize_iobuf(Integers()))
     self.assertEqual(x.type, Integers.Type.EMPTY)
Beispiel #13
0
 def test_hashability(self) -> None:
     hash(Integers())
Beispiel #14
0
    def test_serialize_Integers_union(self) -> None:
        control = Integers(medium=1337)

        self.thrift_serialization_round_trip(control)
Beispiel #15
0
 def test_wrong_type(self) -> None:
     x = Integers(small=1)
     with self.assertRaises(AttributeError):
         x.large
     x.small
Beispiel #16
0
 def test_isset_Union(self) -> None:
     i = Integers(large=2)
     with self.assertRaises(TypeError):
         # pyre-ignore[6]: for test
         isset(i)["large"]
Beispiel #17
0
 def test_multiple_values(self) -> None:
     with self.assertRaises(TypeError):
         Integers(small=1, large=2)
Beispiel #18
0
 def test_serialize_hard_struct(self) -> None:
     control = hard(
         val=0, val_list=[1, 2, 3, 4], name="foo", an_int=Integers(tiny=1)
     )
     self.thrift_serialization_round_trip(control)