1#! /usr/bin/python
2#
3# Protocol Buffers - Google's data interchange format
4# Copyright 2008 Google Inc.  All rights reserved.
5# http://code.google.com/p/protobuf/
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
37import unittest
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(unittest.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(['one', 'two', 'three'])
65    msg.factory_1_message.list_value.append('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(['four', 'three', 'two'])
74    msg.list_value.append('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        'net.proto2.python.internal.Factory2Message'))
96    self.assertIsNot(cls, factory_test2_pb2.Factory2Message)
97    self._ExerciseDynamicClass(cls)
98    cls2 = factory.GetPrototype(pool.FindMessageTypeByName(
99        'net.proto2.python.internal.Factory2Message'))
100    self.assertIs(cls, cls2)
101
102  def testGetMessages(self):
103    messages = message_factory.GetMessages([self.factory_test2_fd,
104                                            self.factory_test1_fd])
105    self.assertContainsSubset(
106        ['net.proto2.python.internal.Factory2Message',
107         'net.proto2.python.internal.Factory1Message'],
108        messages.keys())
109    self._ExerciseDynamicClass(
110        messages['net.proto2.python.internal.Factory2Message'])
111
112if __name__ == '__main__':
113  unittest.main()
114