Tamir Duberstein | 9d9d0b7 | 2015-04-11 20:23:45 -0700 | [diff] [blame] | 1 | #! /usr/bin/env python |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 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 | |
| 33 | """Tests for google.protobuf.proto_builder.""" |
| 34 | |
Dan O'Reilly | 2621c8a | 2015-08-14 22:54:53 -0400 | [diff] [blame] | 35 | try: |
| 36 | from collections import OrderedDict |
Dan O'Reilly | 2621c8a | 2015-08-14 22:54:53 -0400 | [diff] [blame] | 37 | except ImportError: |
Jisi Liu | 46e8ff6 | 2015-10-05 11:59:43 -0700 | [diff] [blame] | 38 | from ordereddict import OrderedDict #PY26 |
Jisi Liu | dbea00a | 2015-10-05 16:08:22 -0700 | [diff] [blame] | 39 | try: |
| 40 | import unittest2 as unittest |
| 41 | except ImportError: |
| 42 | import unittest |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 43 | from google.protobuf import descriptor_pb2 |
| 44 | from google.protobuf import descriptor_pool |
| 45 | from google.protobuf import proto_builder |
| 46 | from google.protobuf import text_format |
| 47 | |
| 48 | |
Tamir Duberstein | 9f42f5f | 2015-01-13 14:47:32 -0500 | [diff] [blame] | 49 | class ProtoBuilderTest(unittest.TestCase): |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 50 | |
| 51 | def setUp(self): |
Dan O'Reilly | 2621c8a | 2015-08-14 22:54:53 -0400 | [diff] [blame] | 52 | self.ordered_fields = OrderedDict([ |
Bo Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 53 | ('foo', descriptor_pb2.FieldDescriptorProto.TYPE_INT64), |
| 54 | ('bar', descriptor_pb2.FieldDescriptorProto.TYPE_STRING), |
| 55 | ]) |
| 56 | self._fields = dict(self.ordered_fields) |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 57 | |
| 58 | def testMakeSimpleProtoClass(self): |
| 59 | """Test that we can create a proto class.""" |
| 60 | proto_cls = proto_builder.MakeSimpleProtoClass( |
| 61 | self._fields, |
| 62 | full_name='net.proto2.python.public.proto_builder_test.Test') |
| 63 | proto = proto_cls() |
| 64 | proto.foo = 12345 |
| 65 | proto.bar = 'asdf' |
| 66 | self.assertMultiLineEqual( |
| 67 | 'bar: "asdf"\nfoo: 12345\n', text_format.MessageToString(proto)) |
| 68 | |
Bo Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 69 | def testOrderedFields(self): |
| 70 | """Test that the field order is maintained when given an OrderedDict.""" |
| 71 | proto_cls = proto_builder.MakeSimpleProtoClass( |
| 72 | self.ordered_fields, |
| 73 | full_name='net.proto2.python.public.proto_builder_test.OrderedTest') |
| 74 | proto = proto_cls() |
| 75 | proto.foo = 12345 |
| 76 | proto.bar = 'asdf' |
| 77 | self.assertMultiLineEqual( |
| 78 | 'foo: 12345\nbar: "asdf"\n', text_format.MessageToString(proto)) |
| 79 | |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 80 | def testMakeSameProtoClassTwice(self): |
| 81 | """Test that the DescriptorPool is used.""" |
| 82 | pool = descriptor_pool.DescriptorPool() |
| 83 | proto_cls1 = proto_builder.MakeSimpleProtoClass( |
| 84 | self._fields, |
| 85 | full_name='net.proto2.python.public.proto_builder_test.Test', |
| 86 | pool=pool) |
| 87 | proto_cls2 = proto_builder.MakeSimpleProtoClass( |
| 88 | self._fields, |
| 89 | full_name='net.proto2.python.public.proto_builder_test.Test', |
| 90 | pool=pool) |
| 91 | self.assertIs(proto_cls1.DESCRIPTOR, proto_cls2.DESCRIPTOR) |
| 92 | |
| 93 | |
| 94 | if __name__ == '__main__': |
Tamir Duberstein | 9f42f5f | 2015-01-13 14:47:32 -0500 | [diff] [blame] | 95 | unittest.main() |