def test_map_type(self): s1 = MyStruct(intField=456, listOfIntField=[34, 59, 28], enumField=MyEnum.ONE) s2 = MyStruct(intField=123, listOfIntField=[12, 23], enumField=MyEnum.TWO) s3 = MapStruct( mapOfStrI32Field={ "a": 1, "b": 2, "c": 3, "d": 4 }, mapOfStrStructField={ "a": s1, "b": s2 }, mapOfStrEnumField={ "a": MyEnum.TWO, "c": MyEnum.ONE }, ) serialized = serialize_iobuf(s3) s4 = deserialize(MapStruct, serialized) self.assertEqual(s3, s4)
def test_default_construction(self): my_struct = MyStruct() self.assertEqual(my_struct.intField, 0) # int field defaults to 0 self.assertEqual(my_struct.listOfIntField, ()) # list field defaults to empty tuple self.assertEqual(my_struct.optionalField, None) # optional field defaults to None self.assertEqual(my_struct.enumField, BadEnum( MyEnum, 0)) # enum field defaults to 0 enum or BadEnum another_struct = AnotherStruct() self.assertEqual(another_struct.structField, IncludedStruct( )) # struct field defaults to default-constructed struct struct_with_union = StructWithAUnion() union_field = struct_with_union.unionField # union field defaults to empty union self.assertEqual(union_field.type, MyUnion.Type.EMPTY) self.assertIsNone(union_field.value) primitive_struct = PrimitiveStruct() self.assertEqual(primitive_struct.boolField, False) self.assertEqual(primitive_struct.floatField, 0.0) self.assertEqual(primitive_struct.doubleField, 0.0) self.assertEqual(primitive_struct.byteField, 0) self.assertEqual(primitive_struct.i16Field, 0) self.assertEqual(primitive_struct.i64Field, 0) string_struct = StringStruct() self.assertEqual(string_struct.stringField, "") self.assertEqual(string_struct.binaryField, b"") self.assertEqual(SetStruct().setOfI32Field, frozenset()) self.assertEqual(MapStruct().mapOfStrStructField, MappingProxyType({}))
def test_set_type(self): s1 = MyStruct(intField=456, listOfIntField=[34, 59, 28], enumField=MyEnum.ONE) s2 = MyStruct(intField=123, listOfIntField=[12, 23], enumField=MyEnum.TWO) s3 = SetStruct( setOfI32Field={1, 2, 3, 4}, setOfStructField={s1, s2}, setOfEnumField={MyEnum.TWO, MyEnum.ONE}, setOfStringField={"hello", "world"}, ) serialized = serialize_iobuf(s3) s4 = deserialize(SetStruct, serialized) self.assertEqual(s3, s4)
def test_simple_struct(self): s1 = MyStruct( intField=456, listOfIntField=[34, 59, 28], enumField=MyEnum.TWO, ) serialized = serialize_iobuf(s1) s2 = deserialize(MyStruct, serialized) self.assertIsNot(s1, s2) self.assertIs(s2.enumField, MyEnum.TWO) self.assertEqual(s1, s2) self.assertIsNone(s2.optionalField)
def test_iter(self): s1 = MyStruct( intField=456, listOfIntField=[34, 59, 28], enumField=MyEnum.TWO, ) self.assertEqual( list(s1), [ ("intField", 456), ("listOfIntField", (34, 59, 28)), ("optionalField", None), ("enumField", MyEnum.TWO), ], )
def test_complex_struct(self): included = IncludedStruct(intField=456, listOfIntField=[34, 59, 28]) s1 = MyStruct(intField=456, listOfIntField=[34, 59, 28], enumField=MyEnum.ONE) s2 = AnotherStruct(structField=included, listOfStructField=[s1], listOflistOfStructField=[[s1]]) serialized = serialize_iobuf(s2) s3 = deserialize(AnotherStruct, serialized) self.assertIsNot(s3, s2) self.assertEqual(s3, s2) self.assertIsNot(s3.structField, included) self.assertEqual(s3.structField, included) self.assertEqual(s3.listOfStructField, (s1, )) self.assertEqual(s3.listOflistOfStructField, ((s1, ), ))
def test_copy(self): s1 = MyStruct(intField=456, listOfIntField=[34, 59, 28], enumField=MyEnum.ONE) s2 = copy.copy(s1) self.assertEqual(s1, s2) self.assertIsNot(s1, s2) u1 = MyUnion(intField=42) u2 = copy.copy(u1) self.assertEqual(u1, u2) self.assertIsNot(u1, u2) e1 = MyException(error_message="error", internal_error_message="internal error") e2 = copy.copy(e1) self.assertEqual(e1, e2) self.assertIsNot(e1, e2)
def test_call_replace(self): s1 = MyStruct(intField=456, listOfIntField=[34, 59, 28], enumField=MyEnum.ONE) s2 = s1(intField=678, enumField=MyEnum.TWO) self.assertEqual(s2.intField, 678) self.assertEqual(s2.listOfIntField, (34, 59, 28)) self.assertIsNone(s2.optionalField) self.assertEqual(s2.enumField, MyEnum.TWO) s3 = s2(optionalField=(1, 2, 3)) self.assertEqual(s3.optionalField, (1, 2, 3)) s4 = s3(optionalField=None, listOfIntField=None, enumField=None) self.assertEqual(s3.intField, 678) self.assertIsNone(s4.optionalField) self.assertEqual(s4.listOfIntField, ()) self.assertEqual(s4.enumField, BadEnum(MyEnum, 0)) s5 = AnotherStruct() included = IncludedStruct(intField=678) s6 = s5(structField=included, listOfStructField=[s1, s2]) self.assertEqual(s6.structField, included) self.assertEqual(s6.listOfStructField, (s1, s2)) self.assertEqual(s6.listOflistOfStructField, ())