1#! /usr/bin/python 2# 3# Protocol Buffers - Google's data interchange format 4# Copyright 2008 Google Inc. All rights reserved. 5# https://developers.google.com/protocol-buffers/ 6# 7# Redistribution and use in source and binary forms, with or without 8# modification, are permitted provided that the following conditions are 9# met: 10# 11# * Redistributions of source code must retain the above copyright 12# notice, this list of conditions and the following disclaimer. 13# * Redistributions in binary form must reproduce the above 14# copyright notice, this list of conditions and the following disclaimer 15# in the documentation and/or other materials provided with the 16# distribution. 17# * Neither the name of Google Inc. nor the names of its 18# contributors may be used to endorse or promote products derived from 19# this software without specific prior written permission. 20# 21# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 33"""Tests for google.protobuf.message_factory.""" 34 35__author__ = 'matthewtoia@google.com (Matt Toia)' 36 37from google.apputils import basetest 38from google.protobuf import descriptor_pb2 39from google.protobuf.internal import factory_test1_pb2 40from google.protobuf.internal import factory_test2_pb2 41from google.protobuf import descriptor_database 42from google.protobuf import descriptor_pool 43from google.protobuf import message_factory 44 45 46class MessageFactoryTest(basetest.TestCase): 47 48 def setUp(self): 49 self.factory_test1_fd = descriptor_pb2.FileDescriptorProto.FromString( 50 factory_test1_pb2.DESCRIPTOR.serialized_pb) 51 self.factory_test2_fd = descriptor_pb2.FileDescriptorProto.FromString( 52 factory_test2_pb2.DESCRIPTOR.serialized_pb) 53 54 def _ExerciseDynamicClass(self, cls): 55 msg = cls() 56 msg.mandatory = 42 57 msg.nested_factory_2_enum = 0 58 msg.nested_factory_2_message.value = 'nested message value' 59 msg.factory_1_message.factory_1_enum = 1 60 msg.factory_1_message.nested_factory_1_enum = 0 61 msg.factory_1_message.nested_factory_1_message.value = ( 62 'nested message value') 63 msg.factory_1_message.scalar_value = 22 64 msg.factory_1_message.list_value.extend([u'one', u'two', u'three']) 65 msg.factory_1_message.list_value.append(u'four') 66 msg.factory_1_enum = 1 67 msg.nested_factory_1_enum = 0 68 msg.nested_factory_1_message.value = 'nested message value' 69 msg.circular_message.mandatory = 1 70 msg.circular_message.circular_message.mandatory = 2 71 msg.circular_message.scalar_value = 'one deep' 72 msg.scalar_value = 'zero deep' 73 msg.list_value.extend([u'four', u'three', u'two']) 74 msg.list_value.append(u'one') 75 msg.grouped.add() 76 msg.grouped[0].part_1 = 'hello' 77 msg.grouped[0].part_2 = 'world' 78 msg.grouped.add(part_1='testing', part_2='123') 79 msg.loop.loop.mandatory = 2 80 msg.loop.loop.loop.loop.mandatory = 4 81 serialized = msg.SerializeToString() 82 converted = factory_test2_pb2.Factory2Message.FromString(serialized) 83 reserialized = converted.SerializeToString() 84 self.assertEquals(serialized, reserialized) 85 result = cls.FromString(reserialized) 86 self.assertEquals(msg, result) 87 88 def testGetPrototype(self): 89 db = descriptor_database.DescriptorDatabase() 90 pool = descriptor_pool.DescriptorPool(db) 91 db.Add(self.factory_test1_fd) 92 db.Add(self.factory_test2_fd) 93 factory = message_factory.MessageFactory() 94 cls = factory.GetPrototype(pool.FindMessageTypeByName( 95 'google.protobuf.python.internal.Factory2Message')) 96 self.assertIsNot(cls, factory_test2_pb2.Factory2Message) 97 self._ExerciseDynamicClass(cls) 98 cls2 = factory.GetPrototype(pool.FindMessageTypeByName( 99 'google.protobuf.python.internal.Factory2Message')) 100 self.assertIs(cls, cls2) 101 102 def testGetMessages(self): 103 # performed twice because multiple calls with the same input must be allowed 104 for _ in range(2): 105 messages = message_factory.GetMessages([self.factory_test2_fd, 106 self.factory_test1_fd]) 107 self.assertContainsSubset( 108 ['google.protobuf.python.internal.Factory2Message', 109 'google.protobuf.python.internal.Factory1Message'], 110 messages.keys()) 111 self._ExerciseDynamicClass( 112 messages['google.protobuf.python.internal.Factory2Message']) 113 self.assertContainsSubset( 114 ['google.protobuf.python.internal.Factory2Message.one_more_field', 115 'google.protobuf.python.internal.another_field'], 116 (messages['google.protobuf.python.internal.Factory1Message'] 117 ._extensions_by_name.keys())) 118 factory_msg1 = messages['google.protobuf.python.internal.Factory1Message'] 119 msg1 = messages['google.protobuf.python.internal.Factory1Message']() 120 ext1 = factory_msg1._extensions_by_name[ 121 'google.protobuf.python.internal.Factory2Message.one_more_field'] 122 ext2 = factory_msg1._extensions_by_name[ 123 'google.protobuf.python.internal.another_field'] 124 msg1.Extensions[ext1] = 'test1' 125 msg1.Extensions[ext2] = 'test2' 126 self.assertEquals('test1', msg1.Extensions[ext1]) 127 self.assertEquals('test2', msg1.Extensions[ext2]) 128 129 130if __name__ == '__main__': 131 basetest.main() 132