proto_builder_test.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.proto_builder."""
  33. try:
  34. from collections import OrderedDict
  35. except ImportError:
  36. from ordereddict import OrderedDict #PY26
  37. try:
  38. import unittest2 as unittest
  39. except ImportError:
  40. import unittest
  41. from google.protobuf import descriptor_pb2
  42. from google.protobuf import descriptor_pool
  43. from google.protobuf import proto_builder
  44. from google.protobuf import text_format
  45. class ProtoBuilderTest(unittest.TestCase):
  46. def setUp(self):
  47. self.ordered_fields = OrderedDict([
  48. ('foo', descriptor_pb2.FieldDescriptorProto.TYPE_INT64),
  49. ('bar', descriptor_pb2.FieldDescriptorProto.TYPE_STRING),
  50. ])
  51. self._fields = dict(self.ordered_fields)
  52. def testMakeSimpleProtoClass(self):
  53. """Test that we can create a proto class."""
  54. proto_cls = proto_builder.MakeSimpleProtoClass(
  55. self._fields,
  56. full_name='net.proto2.python.public.proto_builder_test.Test')
  57. proto = proto_cls()
  58. proto.foo = 12345
  59. proto.bar = 'asdf'
  60. self.assertMultiLineEqual(
  61. 'bar: "asdf"\nfoo: 12345\n', text_format.MessageToString(proto))
  62. def testOrderedFields(self):
  63. """Test that the field order is maintained when given an OrderedDict."""
  64. proto_cls = proto_builder.MakeSimpleProtoClass(
  65. self.ordered_fields,
  66. full_name='net.proto2.python.public.proto_builder_test.OrderedTest')
  67. proto = proto_cls()
  68. proto.foo = 12345
  69. proto.bar = 'asdf'
  70. self.assertMultiLineEqual(
  71. 'foo: 12345\nbar: "asdf"\n', text_format.MessageToString(proto))
  72. def testMakeSameProtoClassTwice(self):
  73. """Test that the DescriptorPool is used."""
  74. pool = descriptor_pool.DescriptorPool()
  75. proto_cls1 = proto_builder.MakeSimpleProtoClass(
  76. self._fields,
  77. full_name='net.proto2.python.public.proto_builder_test.Test',
  78. pool=pool)
  79. proto_cls2 = proto_builder.MakeSimpleProtoClass(
  80. self._fields,
  81. full_name='net.proto2.python.public.proto_builder_test.Test',
  82. pool=pool)
  83. self.assertIs(proto_cls1.DESCRIPTOR, proto_cls2.DESCRIPTOR)
  84. if __name__ == '__main__':
  85. unittest.main()