message_factory_test.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. """Tests for google.protobuf.message_factory."""
  33. __author__ = 'matthewtoia@google.com (Matt Toia)'
  34. import unittest
  35. from google.protobuf import descriptor_pb2
  36. from google.protobuf.internal import factory_test1_pb2
  37. from google.protobuf.internal import factory_test2_pb2
  38. from google.protobuf import descriptor_database
  39. from google.protobuf import descriptor_pool
  40. from google.protobuf import message_factory
  41. class MessageFactoryTest(unittest.TestCase):
  42. def setUp(self):
  43. self.factory_test1_fd = descriptor_pb2.FileDescriptorProto.FromString(
  44. factory_test1_pb2.DESCRIPTOR.serialized_pb)
  45. self.factory_test2_fd = descriptor_pb2.FileDescriptorProto.FromString(
  46. factory_test2_pb2.DESCRIPTOR.serialized_pb)
  47. def _ExerciseDynamicClass(self, cls):
  48. msg = cls()
  49. msg.mandatory = 42
  50. msg.nested_factory_2_enum = 0
  51. msg.nested_factory_2_message.value = 'nested message value'
  52. msg.factory_1_message.factory_1_enum = 1
  53. msg.factory_1_message.nested_factory_1_enum = 0
  54. msg.factory_1_message.nested_factory_1_message.value = (
  55. 'nested message value')
  56. msg.factory_1_message.scalar_value = 22
  57. msg.factory_1_message.list_value.extend(['one', 'two', 'three'])
  58. msg.factory_1_message.list_value.append('four')
  59. msg.factory_1_enum = 1
  60. msg.nested_factory_1_enum = 0
  61. msg.nested_factory_1_message.value = 'nested message value'
  62. msg.circular_message.mandatory = 1
  63. msg.circular_message.circular_message.mandatory = 2
  64. msg.circular_message.scalar_value = 'one deep'
  65. msg.scalar_value = 'zero deep'
  66. msg.list_value.extend(['four', 'three', 'two'])
  67. msg.list_value.append('one')
  68. msg.grouped.add()
  69. msg.grouped[0].part_1 = 'hello'
  70. msg.grouped[0].part_2 = 'world'
  71. msg.grouped.add(part_1='testing', part_2='123')
  72. msg.loop.loop.mandatory = 2
  73. msg.loop.loop.loop.loop.mandatory = 4
  74. serialized = msg.SerializeToString()
  75. converted = factory_test2_pb2.Factory2Message.FromString(serialized)
  76. reserialized = converted.SerializeToString()
  77. self.assertEquals(serialized, reserialized)
  78. result = cls.FromString(reserialized)
  79. self.assertEquals(msg, result)
  80. def testGetPrototype(self):
  81. db = descriptor_database.DescriptorDatabase()
  82. pool = descriptor_pool.DescriptorPool(db)
  83. db.Add(self.factory_test1_fd)
  84. db.Add(self.factory_test2_fd)
  85. factory = message_factory.MessageFactory()
  86. cls = factory.GetPrototype(pool.FindMessageTypeByName(
  87. 'net.proto2.python.internal.Factory2Message'))
  88. self.assertIsNot(cls, factory_test2_pb2.Factory2Message)
  89. self._ExerciseDynamicClass(cls)
  90. cls2 = factory.GetPrototype(pool.FindMessageTypeByName(
  91. 'net.proto2.python.internal.Factory2Message'))
  92. self.assertIs(cls, cls2)
  93. def testGetMessages(self):
  94. messages = message_factory.GetMessages([self.factory_test2_fd,
  95. self.factory_test1_fd])
  96. self.assertContainsSubset(
  97. ['net.proto2.python.internal.Factory2Message',
  98. 'net.proto2.python.internal.Factory1Message'],
  99. messages.keys())
  100. self._ExerciseDynamicClass(
  101. messages['net.proto2.python.internal.Factory2Message'])
  102. if __name__ == '__main__':
  103. unittest.main()