31 """Tests for google.protobuf.proto_builder."""
35 import unittest2
as unittest
46 class ProtoBuilderTest(unittest.TestCase):
50 (
'foo', descriptor_pb2.FieldDescriptorProto.TYPE_INT64),
51 (
'bar', descriptor_pb2.FieldDescriptorProto.TYPE_STRING),
56 """Test that we can create a proto class."""
57 proto_cls = proto_builder.MakeSimpleProtoClass(
59 full_name=
'net.proto2.python.public.proto_builder_test.Test')
63 self.assertMultiLineEqual(
64 'bar: "asdf"\nfoo: 12345\n', text_format.MessageToString(proto))
67 """Test that the field order is maintained when given an OrderedDict."""
68 proto_cls = proto_builder.MakeSimpleProtoClass(
70 full_name=
'net.proto2.python.public.proto_builder_test.OrderedTest')
74 self.assertMultiLineEqual(
75 'foo: 12345\nbar: "asdf"\n', text_format.MessageToString(proto))
78 """Test that the DescriptorPool is used."""
80 proto_cls1 = proto_builder.MakeSimpleProtoClass(
82 full_name=
'net.proto2.python.public.proto_builder_test.Test',
84 proto_cls2 = proto_builder.MakeSimpleProtoClass(
86 full_name=
'net.proto2.python.public.proto_builder_test.Test',
88 self.assertIs(proto_cls1.DESCRIPTOR, proto_cls2.DESCRIPTOR)
91 """Test that large created protos don't use reserved field numbers."""
94 'foo%d' % i: descriptor_pb2.FieldDescriptorProto.TYPE_INT64
95 for i
in range(num_fields)
97 proto_cls = proto_builder.MakeSimpleProtoClass(
99 full_name=
'net.proto2.python.public.proto_builder_test.LargeProtoTest')
101 reserved_field_numbers =
set(
102 range(descriptor.FieldDescriptor.FIRST_RESERVED_FIELD_NUMBER,
103 descriptor.FieldDescriptor.LAST_RESERVED_FIELD_NUMBER + 1))
104 proto_field_numbers =
set(proto_cls.DESCRIPTOR.fields_by_number)
105 self.assertFalse(reserved_field_numbers.intersection(proto_field_numbers))
108 if __name__ ==
'__main__':