message_factory_test.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #! /usr/bin/env 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. """Tests for google.protobuf.message_factory."""
  33. __author__ = 'matthewtoia@google.com (Matt Toia)'
  34. try:
  35. import unittest2 as unittest
  36. except ImportError:
  37. import unittest
  38. from google.protobuf import descriptor_pb2
  39. from google.protobuf.internal import factory_test1_pb2
  40. from google.protobuf.internal import factory_test2_pb2
  41. from google.protobuf import descriptor_database
  42. from google.protobuf import descriptor_pool
  43. from google.protobuf import message_factory
  44. class MessageFactoryTest(unittest.TestCase):
  45. def setUp(self):
  46. self.factory_test1_fd = descriptor_pb2.FileDescriptorProto.FromString(
  47. factory_test1_pb2.DESCRIPTOR.serialized_pb)
  48. self.factory_test2_fd = descriptor_pb2.FileDescriptorProto.FromString(
  49. factory_test2_pb2.DESCRIPTOR.serialized_pb)
  50. def _ExerciseDynamicClass(self, cls):
  51. msg = cls()
  52. msg.mandatory = 42
  53. msg.nested_factory_2_enum = 0
  54. msg.nested_factory_2_message.value = 'nested message value'
  55. msg.factory_1_message.factory_1_enum = 1
  56. msg.factory_1_message.nested_factory_1_enum = 0
  57. msg.factory_1_message.nested_factory_1_message.value = (
  58. 'nested message value')
  59. msg.factory_1_message.scalar_value = 22
  60. msg.factory_1_message.list_value.extend([u'one', u'two', u'three'])
  61. msg.factory_1_message.list_value.append(u'four')
  62. msg.factory_1_enum = 1
  63. msg.nested_factory_1_enum = 0
  64. msg.nested_factory_1_message.value = 'nested message value'
  65. msg.circular_message.mandatory = 1
  66. msg.circular_message.circular_message.mandatory = 2
  67. msg.circular_message.scalar_value = 'one deep'
  68. msg.scalar_value = 'zero deep'
  69. msg.list_value.extend([u'four', u'three', u'two'])
  70. msg.list_value.append(u'one')
  71. msg.grouped.add()
  72. msg.grouped[0].part_1 = 'hello'
  73. msg.grouped[0].part_2 = 'world'
  74. msg.grouped.add(part_1='testing', part_2='123')
  75. msg.loop.loop.mandatory = 2
  76. msg.loop.loop.loop.loop.mandatory = 4
  77. serialized = msg.SerializeToString()
  78. converted = factory_test2_pb2.Factory2Message.FromString(serialized)
  79. reserialized = converted.SerializeToString()
  80. self.assertEqual(serialized, reserialized)
  81. result = cls.FromString(reserialized)
  82. self.assertEqual(msg, result)
  83. def testGetPrototype(self):
  84. db = descriptor_database.DescriptorDatabase()
  85. pool = descriptor_pool.DescriptorPool(db)
  86. db.Add(self.factory_test1_fd)
  87. db.Add(self.factory_test2_fd)
  88. factory = message_factory.MessageFactory()
  89. cls = factory.GetPrototype(pool.FindMessageTypeByName(
  90. 'google.protobuf.python.internal.Factory2Message'))
  91. self.assertFalse(cls is factory_test2_pb2.Factory2Message)
  92. self._ExerciseDynamicClass(cls)
  93. cls2 = factory.GetPrototype(pool.FindMessageTypeByName(
  94. 'google.protobuf.python.internal.Factory2Message'))
  95. self.assertTrue(cls is cls2)
  96. def testGetMessages(self):
  97. # performed twice because multiple calls with the same input must be allowed
  98. for _ in range(2):
  99. messages = message_factory.GetMessages([self.factory_test2_fd,
  100. self.factory_test1_fd])
  101. self.assertTrue(
  102. set(['google.protobuf.python.internal.Factory2Message',
  103. 'google.protobuf.python.internal.Factory1Message'],
  104. ).issubset(set(messages.keys())))
  105. self._ExerciseDynamicClass(
  106. messages['google.protobuf.python.internal.Factory2Message'])
  107. self.assertTrue(
  108. set(['google.protobuf.python.internal.Factory2Message.one_more_field',
  109. 'google.protobuf.python.internal.another_field'],
  110. ).issubset(
  111. set(messages['google.protobuf.python.internal.Factory1Message']
  112. ._extensions_by_name.keys())))
  113. factory_msg1 = messages['google.protobuf.python.internal.Factory1Message']
  114. msg1 = messages['google.protobuf.python.internal.Factory1Message']()
  115. ext1 = factory_msg1._extensions_by_name[
  116. 'google.protobuf.python.internal.Factory2Message.one_more_field']
  117. ext2 = factory_msg1._extensions_by_name[
  118. 'google.protobuf.python.internal.another_field']
  119. msg1.Extensions[ext1] = 'test1'
  120. msg1.Extensions[ext2] = 'test2'
  121. self.assertEqual('test1', msg1.Extensions[ext1])
  122. self.assertEqual('test2', msg1.Extensions[ext2])
  123. if __name__ == '__main__':
  124. unittest.main()