1# Protocol Buffers - Google's data interchange format 2# Copyright 2008 Google Inc. All rights reserved. 3# https://developers.google.com/protocol-buffers/ 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions are 7# met: 8# 9# * Redistributions of source code must retain the above copyright 10# notice, this list of conditions and the following disclaimer. 11# * Redistributions in binary form must reproduce the above 12# copyright notice, this list of conditions and the following disclaimer 13# in the documentation and/or other materials provided with the 14# distribution. 15# * Neither the name of Google Inc. nor the names of its 16# contributors may be used to endorse or promote products derived from 17# this software without specific prior written permission. 18# 19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31"""Tests for google.protobuf.proto_builder.""" 32 33import collections 34import unittest 35 36from google.protobuf import descriptor_pb2 # pylint: disable=g-import-not-at-top 37from google.protobuf import descriptor 38from google.protobuf import descriptor_pool 39from google.protobuf import proto_builder 40from google.protobuf import text_format 41 42 43class ProtoBuilderTest(unittest.TestCase): 44 45 def setUp(self): 46 self.ordered_fields = collections.OrderedDict([ 47 ('foo', descriptor_pb2.FieldDescriptorProto.TYPE_INT64), 48 ('bar', descriptor_pb2.FieldDescriptorProto.TYPE_STRING), 49 ]) 50 self._fields = dict(self.ordered_fields) 51 52 def testMakeSimpleProtoClass(self): 53 """Test that we can create a proto class.""" 54 proto_cls = proto_builder.MakeSimpleProtoClass( 55 self._fields, 56 full_name='net.proto2.python.public.proto_builder_test.Test') 57 proto = proto_cls() 58 proto.foo = 12345 59 proto.bar = 'asdf' 60 self.assertMultiLineEqual( 61 'bar: "asdf"\nfoo: 12345\n', text_format.MessageToString(proto)) 62 63 def testOrderedFields(self): 64 """Test that the field order is maintained when given an OrderedDict.""" 65 proto_cls = proto_builder.MakeSimpleProtoClass( 66 self.ordered_fields, 67 full_name='net.proto2.python.public.proto_builder_test.OrderedTest') 68 proto = proto_cls() 69 proto.foo = 12345 70 proto.bar = 'asdf' 71 self.assertMultiLineEqual( 72 'foo: 12345\nbar: "asdf"\n', text_format.MessageToString(proto)) 73 74 def testMakeSameProtoClassTwice(self): 75 """Test that the DescriptorPool is used.""" 76 pool = descriptor_pool.DescriptorPool() 77 proto_cls1 = proto_builder.MakeSimpleProtoClass( 78 self._fields, 79 full_name='net.proto2.python.public.proto_builder_test.Test', 80 pool=pool) 81 proto_cls2 = proto_builder.MakeSimpleProtoClass( 82 self._fields, 83 full_name='net.proto2.python.public.proto_builder_test.Test', 84 pool=pool) 85 self.assertIs(proto_cls1.DESCRIPTOR, proto_cls2.DESCRIPTOR) 86 87 def testMakeLargeProtoClass(self): 88 """Test that large created protos don't use reserved field numbers.""" 89 num_fields = 123456 90 fields = { 91 'foo%d' % i: descriptor_pb2.FieldDescriptorProto.TYPE_INT64 92 for i in range(num_fields) 93 } 94 proto_cls = proto_builder.MakeSimpleProtoClass( 95 fields, 96 full_name='net.proto2.python.public.proto_builder_test.LargeProtoTest') 97 98 reserved_field_numbers = set( 99 range(descriptor.FieldDescriptor.FIRST_RESERVED_FIELD_NUMBER, 100 descriptor.FieldDescriptor.LAST_RESERVED_FIELD_NUMBER + 1)) 101 proto_field_numbers = set(proto_cls.DESCRIPTOR.fields_by_number) 102 self.assertFalse(reserved_field_numbers.intersection(proto_field_numbers)) 103 104 105if __name__ == '__main__': 106 unittest.main() 107