Tamir Duberstein | 9d9d0b7 | 2015-04-11 20:23:45 -0700 | [diff] [blame] | 1 | #! /usr/bin/env python |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 2 | # |
| 3 | # Protocol Buffers - Google's data interchange format |
| 4 | # Copyright 2008 Google Inc. All rights reserved. |
Feng Xiao | e428862 | 2014-10-01 16:26:23 -0700 | [diff] [blame] | 5 | # https://developers.google.com/protocol-buffers/ |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 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 | |
Jisi Liu | dbea00a | 2015-10-05 16:08:22 -0700 | [diff] [blame] | 37 | try: |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 38 | import unittest2 as unittest #PY26 |
Jisi Liu | dbea00a | 2015-10-05 16:08:22 -0700 | [diff] [blame] | 39 | except ImportError: |
| 40 | import unittest |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 41 | |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 42 | from google.protobuf import descriptor_pb2 |
| 43 | from google.protobuf.internal import factory_test1_pb2 |
| 44 | from google.protobuf.internal import factory_test2_pb2 |
| 45 | from google.protobuf import descriptor_database |
| 46 | from google.protobuf import descriptor_pool |
| 47 | from google.protobuf import message_factory |
| 48 | |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 49 | |
Tres Seaver | 7ee2583 | 2015-01-13 14:47:32 -0500 | [diff] [blame] | 50 | class MessageFactoryTest(unittest.TestCase): |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 51 | |
| 52 | def setUp(self): |
| 53 | self.factory_test1_fd = descriptor_pb2.FileDescriptorProto.FromString( |
| 54 | factory_test1_pb2.DESCRIPTOR.serialized_pb) |
| 55 | self.factory_test2_fd = descriptor_pb2.FileDescriptorProto.FromString( |
| 56 | factory_test2_pb2.DESCRIPTOR.serialized_pb) |
| 57 | |
| 58 | def _ExerciseDynamicClass(self, cls): |
| 59 | msg = cls() |
| 60 | msg.mandatory = 42 |
| 61 | msg.nested_factory_2_enum = 0 |
| 62 | msg.nested_factory_2_message.value = 'nested message value' |
| 63 | msg.factory_1_message.factory_1_enum = 1 |
| 64 | msg.factory_1_message.nested_factory_1_enum = 0 |
| 65 | msg.factory_1_message.nested_factory_1_message.value = ( |
| 66 | 'nested message value') |
| 67 | msg.factory_1_message.scalar_value = 22 |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 68 | msg.factory_1_message.list_value.extend([u'one', u'two', u'three']) |
| 69 | msg.factory_1_message.list_value.append(u'four') |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 70 | msg.factory_1_enum = 1 |
| 71 | msg.nested_factory_1_enum = 0 |
| 72 | msg.nested_factory_1_message.value = 'nested message value' |
| 73 | msg.circular_message.mandatory = 1 |
| 74 | msg.circular_message.circular_message.mandatory = 2 |
| 75 | msg.circular_message.scalar_value = 'one deep' |
| 76 | msg.scalar_value = 'zero deep' |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 77 | msg.list_value.extend([u'four', u'three', u'two']) |
| 78 | msg.list_value.append(u'one') |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 79 | msg.grouped.add() |
| 80 | msg.grouped[0].part_1 = 'hello' |
| 81 | msg.grouped[0].part_2 = 'world' |
| 82 | msg.grouped.add(part_1='testing', part_2='123') |
| 83 | msg.loop.loop.mandatory = 2 |
| 84 | msg.loop.loop.loop.loop.mandatory = 4 |
| 85 | serialized = msg.SerializeToString() |
| 86 | converted = factory_test2_pb2.Factory2Message.FromString(serialized) |
| 87 | reserialized = converted.SerializeToString() |
Tres Seaver | a2abc94 | 2015-01-13 15:47:55 -0500 | [diff] [blame] | 88 | self.assertEqual(serialized, reserialized) |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 89 | result = cls.FromString(reserialized) |
Tres Seaver | a2abc94 | 2015-01-13 15:47:55 -0500 | [diff] [blame] | 90 | self.assertEqual(msg, result) |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 91 | |
| 92 | def testGetPrototype(self): |
| 93 | db = descriptor_database.DescriptorDatabase() |
| 94 | pool = descriptor_pool.DescriptorPool(db) |
| 95 | db.Add(self.factory_test1_fd) |
| 96 | db.Add(self.factory_test2_fd) |
| 97 | factory = message_factory.MessageFactory() |
| 98 | cls = factory.GetPrototype(pool.FindMessageTypeByName( |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 99 | 'google.protobuf.python.internal.Factory2Message')) |
Tres Seaver | 71edc31 | 2015-01-13 15:38:10 -0500 | [diff] [blame] | 100 | self.assertFalse(cls is factory_test2_pb2.Factory2Message) |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 101 | self._ExerciseDynamicClass(cls) |
| 102 | cls2 = factory.GetPrototype(pool.FindMessageTypeByName( |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 103 | 'google.protobuf.python.internal.Factory2Message')) |
Tres Seaver | 71edc31 | 2015-01-13 15:38:10 -0500 | [diff] [blame] | 104 | self.assertTrue(cls is cls2) |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 105 | |
| 106 | def testGetMessages(self): |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 107 | # performed twice because multiple calls with the same input must be allowed |
| 108 | for _ in range(2): |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 109 | messages = message_factory.GetMessages([self.factory_test1_fd, |
| 110 | self.factory_test2_fd]) |
Tres Seaver | 7ee2583 | 2015-01-13 14:47:32 -0500 | [diff] [blame] | 111 | self.assertTrue( |
| 112 | set(['google.protobuf.python.internal.Factory2Message', |
| 113 | 'google.protobuf.python.internal.Factory1Message'], |
| 114 | ).issubset(set(messages.keys()))) |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 115 | self._ExerciseDynamicClass( |
| 116 | messages['google.protobuf.python.internal.Factory2Message']) |
Tres Seaver | 7ee2583 | 2015-01-13 14:47:32 -0500 | [diff] [blame] | 117 | self.assertTrue( |
| 118 | set(['google.protobuf.python.internal.Factory2Message.one_more_field', |
| 119 | 'google.protobuf.python.internal.another_field'], |
| 120 | ).issubset( |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 121 | set(messages['google.protobuf.python.internal.Factory1Message'] |
Tres Seaver | 7ee2583 | 2015-01-13 14:47:32 -0500 | [diff] [blame] | 122 | ._extensions_by_name.keys()))) |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 123 | factory_msg1 = messages['google.protobuf.python.internal.Factory1Message'] |
| 124 | msg1 = messages['google.protobuf.python.internal.Factory1Message']() |
| 125 | ext1 = factory_msg1._extensions_by_name[ |
| 126 | 'google.protobuf.python.internal.Factory2Message.one_more_field'] |
| 127 | ext2 = factory_msg1._extensions_by_name[ |
| 128 | 'google.protobuf.python.internal.another_field'] |
| 129 | msg1.Extensions[ext1] = 'test1' |
| 130 | msg1.Extensions[ext2] = 'test2' |
Tres Seaver | a2abc94 | 2015-01-13 15:47:55 -0500 | [diff] [blame] | 131 | self.assertEqual('test1', msg1.Extensions[ext1]) |
| 132 | self.assertEqual('test2', msg1.Extensions[ext2]) |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 133 | |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 134 | |
| 135 | if __name__ == '__main__': |
Tres Seaver | 7ee2583 | 2015-01-13 14:47:32 -0500 | [diff] [blame] | 136 | unittest.main() |