예제 #1
0
    def test_required_fields(self) -> None:
        with self.assertRaises(TypeError):
            # None is not acceptable as a string
            hard(  # type: ignore
                val=1, val_list=[1, 2], name=None, an_int=Integers(small=1)
            )

        with self.assertRaises(TypeError):
            hard(val=1, val_list=[1, 2])  # type: ignore
예제 #2
0
 def test_pickle_hard_struct(self) -> None:
     control = hard(
         val=0,
         val_list=[1, 2, 3, 4],
         name='foo',
         an_int=Integers(tiny=1),
     )
     self.pickle_round_robin(control)
예제 #3
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_robin(control)
예제 #4
0
 def test_call_replace_required(self) -> None:
     x = hard(
         val=5,
         val_list=[1, 2],
         name="something",
         an_int=Integers(small=1),
         other='non default',
     )
     y = x(other=None)
     self.assertEqual(y.other, "some default")
     with self.assertRaises(TypeError):
         x(name=None)  # type: ignore
예제 #5
0
 def test_call_replace_required(self) -> None:
     x = hard(
         val=5,
         val_list=[1, 2],
         name="something",
         an_int=Integers(small=1),
         other="non default",
     )
     y = x(other=None)
     self.assertEqual(y.other, "some default")
     with self.assertRaises(TypeError):
         x(name=None)  # type: ignore
예제 #6
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)
     )
     fixtures: Mapping[Protocol, bytes] = {
         Protocol.COMPACT: b"\x15\x00\x19E\x02\x04\x06\x08\x18\x03foo\x1c\x13\x01"
         b"\x00\x18\x0csome default\x00",
         Protocol.BINARY: b"\x08\x00\x01\x00\x00\x00\x00\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\x0b\x00\x03\x00\x00\x00\x03foo\x0c\x00\x04"
         b"\x03\x00\x01\x01\x00\x0b\x00\x05\x00\x00\x00\x0csome def"
         b"ault\x00",
         Protocol.JSON: b'{"val":0,"val_list":[1,2,3,4],"name":"foo","an_int":{"tiny'
         b'":1},"other":"some default"}',
         Protocol.COMPACT_JSON: b'{"1":{"i32":0},"2":{"lst":["i32",4,1,2,3,4]},"3":'
         b'{"str":"foo"},"4":{"rec":{"1":{"i8":1}}},"5":{"str":"some default"}}',
     }
     self.thrift_serialization_round_robin(control, fixtures)
예제 #7
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)
     )
     fixtures: Mapping[Protocol, bytes] = {
         Protocol.COMPACT: b"\x15\x00\x19E\x02\x04\x06\x08\x18\x03foo\x1c\x13\x01"
         b"\x00\x18\x0csome default\x00",
         Protocol.BINARY: b"\x08\x00\x01\x00\x00\x00\x00\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\x0b\x00\x03\x00\x00\x00\x03foo\x0c\x00\x04"
         b"\x03\x00\x01\x01\x00\x0b\x00\x05\x00\x00\x00\x0csome def"
         b"ault\x00",
         Protocol.JSON: b'{"val":0,"val_list":[1,2,3,4],"name":"foo","an_int":{"tiny'
         b'":1},"other":"some default"}',
         Protocol.COMPACT_JSON: b'{"1":{"i32":0},"2":{"lst":["i32",4,1,2,3,4]},"3":'
         b'{"str":"foo"},"4":{"rec":{"1":{"i8":1}}},"5":{"str":"some default"}}',
     }
     self.thrift_serialization_round_robin(control, fixtures)
예제 #8
0
    def test_metadata_structs(self) -> None:
        meta = gen_metadata(testing.metadata)
        structName = "testing.hard"
        self.assertIsNotNone(meta)
        hardStruct = meta.structs[structName]
        hardStructClass = gen_metadata(hard)
        hardStructInstance = gen_metadata(
            hard(val=1, val_list=[1, 2], name="name"))

        self.assertFalse(hardStruct.is_union)
        self.assertEqual(hardStruct.name, structName)
        self.assertEqual(len(hardStruct.fields), 5)
        self.assertEqual(len(list(hardStructClass.fields)), 5)
        self.assertEqual(len(list(hardStructInstance.fields)), 5)

        field = hardStruct.fields[2]
        _, typedef, fieldClass, *rest = hardStructClass.fields
        _, _, fieldInstance, *rest = hardStructClass.fields
        self.assertEqual(field.name, "name")
        self.assertEqual(fieldClass.name, field.name)
        self.assertEqual(fieldInstance.name, field.name)
        self.assertEqual(field.is_optional, False)
        self.assertEqual(fieldClass.is_optional, False)
        self.assertEqual(fieldInstance.is_optional, False)
        self.assertEqual(field.type.value,
                         ThriftPrimitiveType.THRIFT_STRING_TYPE)
        self.assertEqual(fieldClass.type.as_primitive(),
                         ThriftPrimitiveType.THRIFT_STRING_TYPE)
        self.assertEqual(
            fieldInstance.type.as_primitive(),
            ThriftPrimitiveType.THRIFT_STRING_TYPE,
        )
        self.assertEqual(typedef.type.kind, ThriftKind.TYPEDEF)
        self.assertEqual(typedef.type.as_typedef().underlyingType.kind,
                         ThriftKind.LIST)

        self.assertEqual(meta.structs["testing.EmptyUnion"].is_union, True)

        mixedStruct = gen_metadata(mixed)
        _, _, _, _, _, _, field, *rest = mixedStruct.fields
        self.assertEqual(field.name, "some_field")
        self.assertEqual(field.pyname, "some_field_")
예제 #9
0
    def test_required_fields_not_enforced(self) -> None:
        # None is not acceptable as a string
        hard(val=1, val_list=[1, 2], name=None, an_int=Integers(small=1))

        hard(val=1, val_list=[1, 2])
예제 #10
0
 def test_pickle_hard_struct(self) -> None:
     control = hard(
         val=0, val_list=[1, 2, 3, 4], name="foo", an_int=Integers(tiny=1)
     )
     self.pickle_round_robin(control)