blob: 54b1f6881164633035c5c64cdbcfada39f92a87d [file] [log] [blame]
Tamir Duberstein9d9d0b72015-04-11 20:23:45 -07001#! /usr/bin/env python
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00002#
3# Protocol Buffers - Google's data interchange format
4# Copyright 2008 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07005# https://developers.google.com/protocol-buffers/
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00006#
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 Liudbea00a2015-10-05 16:08:22 -070037try:
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070038 import unittest2 as unittest #PY26
Jisi Liudbea00a2015-10-05 16:08:22 -070039except ImportError:
40 import unittest
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070041
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000042from google.protobuf import descriptor_pb2
43from google.protobuf.internal import factory_test1_pb2
44from google.protobuf.internal import factory_test2_pb2
45from google.protobuf import descriptor_database
46from google.protobuf import descriptor_pool
47from google.protobuf import message_factory
48
Feng Xiaoe841bac2015-12-11 17:09:20 -080049
Tres Seaver7ee25832015-01-13 14:47:32 -050050class MessageFactoryTest(unittest.TestCase):
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000051
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.combde4a322014-08-12 21:10:30 +000068 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.comb55a20f2012-09-22 02:40:50 +000070 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.combde4a322014-08-12 21:10:30 +000077 msg.list_value.extend([u'four', u'three', u'two'])
78 msg.list_value.append(u'one')
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000079 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 Seavera2abc942015-01-13 15:47:55 -050088 self.assertEqual(serialized, reserialized)
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000089 result = cls.FromString(reserialized)
Tres Seavera2abc942015-01-13 15:47:55 -050090 self.assertEqual(msg, result)
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000091
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.combde4a322014-08-12 21:10:30 +000099 'google.protobuf.python.internal.Factory2Message'))
Tres Seaver71edc312015-01-13 15:38:10 -0500100 self.assertFalse(cls is factory_test2_pb2.Factory2Message)
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000101 self._ExerciseDynamicClass(cls)
102 cls2 = factory.GetPrototype(pool.FindMessageTypeByName(
jieluo@google.combde4a322014-08-12 21:10:30 +0000103 'google.protobuf.python.internal.Factory2Message'))
Tres Seaver71edc312015-01-13 15:38:10 -0500104 self.assertTrue(cls is cls2)
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000105
106 def testGetMessages(self):
jieluo@google.combde4a322014-08-12 21:10:30 +0000107 # performed twice because multiple calls with the same input must be allowed
108 for _ in range(2):
Feng Xiaoe841bac2015-12-11 17:09:20 -0800109 messages = message_factory.GetMessages([self.factory_test1_fd,
110 self.factory_test2_fd])
Tres Seaver7ee25832015-01-13 14:47:32 -0500111 self.assertTrue(
112 set(['google.protobuf.python.internal.Factory2Message',
113 'google.protobuf.python.internal.Factory1Message'],
114 ).issubset(set(messages.keys())))
jieluo@google.combde4a322014-08-12 21:10:30 +0000115 self._ExerciseDynamicClass(
116 messages['google.protobuf.python.internal.Factory2Message'])
Tres Seaver7ee25832015-01-13 14:47:32 -0500117 self.assertTrue(
118 set(['google.protobuf.python.internal.Factory2Message.one_more_field',
119 'google.protobuf.python.internal.another_field'],
120 ).issubset(
Feng Xiaoe841bac2015-12-11 17:09:20 -0800121 set(messages['google.protobuf.python.internal.Factory1Message']
Tres Seaver7ee25832015-01-13 14:47:32 -0500122 ._extensions_by_name.keys())))
jieluo@google.combde4a322014-08-12 21:10:30 +0000123 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 Seavera2abc942015-01-13 15:47:55 -0500131 self.assertEqual('test1', msg1.Extensions[ext1])
132 self.assertEqual('test2', msg1.Extensions[ext2])
jieluo@google.combde4a322014-08-12 21:10:30 +0000133
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000134
135if __name__ == '__main__':
Tres Seaver7ee25832015-01-13 14:47:32 -0500136 unittest.main()