| OLD | NEW |
| 1 #! /usr/bin/python | 1 #! /usr/bin/env python |
| 2 # | 2 # |
| 3 # Protocol Buffers - Google's data interchange format | 3 # Protocol Buffers - Google's data interchange format |
| 4 # Copyright 2008 Google Inc. All rights reserved. | 4 # Copyright 2008 Google Inc. All rights reserved. |
| 5 # http://code.google.com/p/protobuf/ | 5 # https://developers.google.com/protocol-buffers/ |
| 6 # | 6 # |
| 7 # Redistribution and use in source and binary forms, with or without | 7 # Redistribution and use in source and binary forms, with or without |
| 8 # modification, are permitted provided that the following conditions are | 8 # modification, are permitted provided that the following conditions are |
| 9 # met: | 9 # met: |
| 10 # | 10 # |
| 11 # * Redistributions of source code must retain the above copyright | 11 # * Redistributions of source code must retain the above copyright |
| 12 # notice, this list of conditions and the following disclaimer. | 12 # notice, this list of conditions and the following disclaimer. |
| 13 # * Redistributions in binary form must reproduce the above | 13 # * Redistributions in binary form must reproduce the above |
| 14 # copyright notice, this list of conditions and the following disclaimer | 14 # copyright notice, this list of conditions and the following disclaimer |
| 15 # in the documentation and/or other materials provided with the | 15 # in the documentation and/or other materials provided with the |
| (...skipping 11 matching lines...) Expand all Loading... |
| 27 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 27 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 28 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 29 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 31 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | 32 |
| 33 """Tests for google.protobuf.message_factory.""" | 33 """Tests for google.protobuf.message_factory.""" |
| 34 | 34 |
| 35 __author__ = 'matthewtoia@google.com (Matt Toia)' | 35 __author__ = 'matthewtoia@google.com (Matt Toia)' |
| 36 | 36 |
| 37 import unittest | 37 try: |
| 38 import unittest2 as unittest |
| 39 except ImportError: |
| 40 import unittest |
| 38 from google.protobuf import descriptor_pb2 | 41 from google.protobuf import descriptor_pb2 |
| 39 from google.protobuf.internal import factory_test1_pb2 | 42 from google.protobuf.internal import factory_test1_pb2 |
| 40 from google.protobuf.internal import factory_test2_pb2 | 43 from google.protobuf.internal import factory_test2_pb2 |
| 41 from google.protobuf import descriptor_database | 44 from google.protobuf import descriptor_database |
| 42 from google.protobuf import descriptor_pool | 45 from google.protobuf import descriptor_pool |
| 43 from google.protobuf import message_factory | 46 from google.protobuf import message_factory |
| 44 | 47 |
| 45 | 48 |
| 46 class MessageFactoryTest(unittest.TestCase): | 49 class MessageFactoryTest(unittest.TestCase): |
| 47 | 50 |
| 48 def setUp(self): | 51 def setUp(self): |
| 49 self.factory_test1_fd = descriptor_pb2.FileDescriptorProto.FromString( | 52 self.factory_test1_fd = descriptor_pb2.FileDescriptorProto.FromString( |
| 50 factory_test1_pb2.DESCRIPTOR.serialized_pb) | 53 factory_test1_pb2.DESCRIPTOR.serialized_pb) |
| 51 self.factory_test2_fd = descriptor_pb2.FileDescriptorProto.FromString( | 54 self.factory_test2_fd = descriptor_pb2.FileDescriptorProto.FromString( |
| 52 factory_test2_pb2.DESCRIPTOR.serialized_pb) | 55 factory_test2_pb2.DESCRIPTOR.serialized_pb) |
| 53 | 56 |
| 54 def _ExerciseDynamicClass(self, cls): | 57 def _ExerciseDynamicClass(self, cls): |
| 55 msg = cls() | 58 msg = cls() |
| 56 msg.mandatory = 42 | 59 msg.mandatory = 42 |
| 57 msg.nested_factory_2_enum = 0 | 60 msg.nested_factory_2_enum = 0 |
| 58 msg.nested_factory_2_message.value = 'nested message value' | 61 msg.nested_factory_2_message.value = 'nested message value' |
| 59 msg.factory_1_message.factory_1_enum = 1 | 62 msg.factory_1_message.factory_1_enum = 1 |
| 60 msg.factory_1_message.nested_factory_1_enum = 0 | 63 msg.factory_1_message.nested_factory_1_enum = 0 |
| 61 msg.factory_1_message.nested_factory_1_message.value = ( | 64 msg.factory_1_message.nested_factory_1_message.value = ( |
| 62 'nested message value') | 65 'nested message value') |
| 63 msg.factory_1_message.scalar_value = 22 | 66 msg.factory_1_message.scalar_value = 22 |
| 64 msg.factory_1_message.list_value.extend(['one', 'two', 'three']) | 67 msg.factory_1_message.list_value.extend([u'one', u'two', u'three']) |
| 65 msg.factory_1_message.list_value.append('four') | 68 msg.factory_1_message.list_value.append(u'four') |
| 66 msg.factory_1_enum = 1 | 69 msg.factory_1_enum = 1 |
| 67 msg.nested_factory_1_enum = 0 | 70 msg.nested_factory_1_enum = 0 |
| 68 msg.nested_factory_1_message.value = 'nested message value' | 71 msg.nested_factory_1_message.value = 'nested message value' |
| 69 msg.circular_message.mandatory = 1 | 72 msg.circular_message.mandatory = 1 |
| 70 msg.circular_message.circular_message.mandatory = 2 | 73 msg.circular_message.circular_message.mandatory = 2 |
| 71 msg.circular_message.scalar_value = 'one deep' | 74 msg.circular_message.scalar_value = 'one deep' |
| 72 msg.scalar_value = 'zero deep' | 75 msg.scalar_value = 'zero deep' |
| 73 msg.list_value.extend(['four', 'three', 'two']) | 76 msg.list_value.extend([u'four', u'three', u'two']) |
| 74 msg.list_value.append('one') | 77 msg.list_value.append(u'one') |
| 75 msg.grouped.add() | 78 msg.grouped.add() |
| 76 msg.grouped[0].part_1 = 'hello' | 79 msg.grouped[0].part_1 = 'hello' |
| 77 msg.grouped[0].part_2 = 'world' | 80 msg.grouped[0].part_2 = 'world' |
| 78 msg.grouped.add(part_1='testing', part_2='123') | 81 msg.grouped.add(part_1='testing', part_2='123') |
| 79 msg.loop.loop.mandatory = 2 | 82 msg.loop.loop.mandatory = 2 |
| 80 msg.loop.loop.loop.loop.mandatory = 4 | 83 msg.loop.loop.loop.loop.mandatory = 4 |
| 81 serialized = msg.SerializeToString() | 84 serialized = msg.SerializeToString() |
| 82 converted = factory_test2_pb2.Factory2Message.FromString(serialized) | 85 converted = factory_test2_pb2.Factory2Message.FromString(serialized) |
| 83 reserialized = converted.SerializeToString() | 86 reserialized = converted.SerializeToString() |
| 84 self.assertEquals(serialized, reserialized) | 87 self.assertEqual(serialized, reserialized) |
| 85 result = cls.FromString(reserialized) | 88 result = cls.FromString(reserialized) |
| 86 self.assertEquals(msg, result) | 89 self.assertEqual(msg, result) |
| 87 | 90 |
| 88 def testGetPrototype(self): | 91 def testGetPrototype(self): |
| 89 db = descriptor_database.DescriptorDatabase() | 92 db = descriptor_database.DescriptorDatabase() |
| 90 pool = descriptor_pool.DescriptorPool(db) | 93 pool = descriptor_pool.DescriptorPool(db) |
| 91 db.Add(self.factory_test1_fd) | 94 db.Add(self.factory_test1_fd) |
| 92 db.Add(self.factory_test2_fd) | 95 db.Add(self.factory_test2_fd) |
| 93 factory = message_factory.MessageFactory() | 96 factory = message_factory.MessageFactory() |
| 94 cls = factory.GetPrototype(pool.FindMessageTypeByName( | 97 cls = factory.GetPrototype(pool.FindMessageTypeByName( |
| 95 'net.proto2.python.internal.Factory2Message')) | 98 'google.protobuf.python.internal.Factory2Message')) |
| 96 self.assertIsNot(cls, factory_test2_pb2.Factory2Message) | 99 self.assertFalse(cls is factory_test2_pb2.Factory2Message) |
| 97 self._ExerciseDynamicClass(cls) | 100 self._ExerciseDynamicClass(cls) |
| 98 cls2 = factory.GetPrototype(pool.FindMessageTypeByName( | 101 cls2 = factory.GetPrototype(pool.FindMessageTypeByName( |
| 99 'net.proto2.python.internal.Factory2Message')) | 102 'google.protobuf.python.internal.Factory2Message')) |
| 100 self.assertIs(cls, cls2) | 103 self.assertTrue(cls is cls2) |
| 101 | 104 |
| 102 def testGetMessages(self): | 105 def testGetMessages(self): |
| 103 messages = message_factory.GetMessages([self.factory_test2_fd, | 106 # performed twice because multiple calls with the same input must be allowed |
| 104 self.factory_test1_fd]) | 107 for _ in range(2): |
| 105 self.assertContainsSubset( | 108 messages = message_factory.GetMessages([self.factory_test1_fd, |
| 106 ['net.proto2.python.internal.Factory2Message', | 109 self.factory_test2_fd]) |
| 107 'net.proto2.python.internal.Factory1Message'], | 110 self.assertTrue( |
| 108 messages.keys()) | 111 set(['google.protobuf.python.internal.Factory2Message', |
| 109 self._ExerciseDynamicClass( | 112 'google.protobuf.python.internal.Factory1Message'], |
| 110 messages['net.proto2.python.internal.Factory2Message']) | 113 ).issubset(set(messages.keys()))) |
| 114 self._ExerciseDynamicClass( |
| 115 messages['google.protobuf.python.internal.Factory2Message']) |
| 116 self.assertTrue( |
| 117 set(['google.protobuf.python.internal.Factory2Message.one_more_field', |
| 118 'google.protobuf.python.internal.another_field'], |
| 119 ).issubset( |
| 120 set(messages['google.protobuf.python.internal.Factory1Message'] |
| 121 ._extensions_by_name.keys()))) |
| 122 factory_msg1 = messages['google.protobuf.python.internal.Factory1Message'] |
| 123 msg1 = messages['google.protobuf.python.internal.Factory1Message']() |
| 124 ext1 = factory_msg1._extensions_by_name[ |
| 125 'google.protobuf.python.internal.Factory2Message.one_more_field'] |
| 126 ext2 = factory_msg1._extensions_by_name[ |
| 127 'google.protobuf.python.internal.another_field'] |
| 128 msg1.Extensions[ext1] = 'test1' |
| 129 msg1.Extensions[ext2] = 'test2' |
| 130 self.assertEqual('test1', msg1.Extensions[ext1]) |
| 131 self.assertEqual('test2', msg1.Extensions[ext2]) |
| 132 |
| 111 | 133 |
| 112 if __name__ == '__main__': | 134 if __name__ == '__main__': |
| 113 unittest.main() | 135 unittest.main() |
| OLD | NEW |