blob: 822ad895edbfc6edd7675da90350a389c171817b [file] [log] [blame]
Tamir Duberstein9d9d0b72015-04-11 20:23:45 -07001#! /usr/bin/env python
Feng Xiao6ef984a2014-11-10 17:34:54 -08002#
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'Reilly2621c8a2015-08-14 22:54:53 -040035try:
36 from collections import OrderedDict
Dan O'Reilly2621c8a2015-08-14 22:54:53 -040037except ImportError:
Jisi Liu46e8ff62015-10-05 11:59:43 -070038 from ordereddict import OrderedDict #PY26
Jisi Liudbea00a2015-10-05 16:08:22 -070039try:
40 import unittest2 as unittest
41except ImportError:
42 import unittest
Feng Xiao6ef984a2014-11-10 17:34:54 -080043from google.protobuf import descriptor_pb2
44from google.protobuf import descriptor_pool
45from google.protobuf import proto_builder
46from google.protobuf import text_format
47
48
Tamir Duberstein9f42f5f2015-01-13 14:47:32 -050049class ProtoBuilderTest(unittest.TestCase):
Feng Xiao6ef984a2014-11-10 17:34:54 -080050
51 def setUp(self):
Dan O'Reilly2621c8a2015-08-14 22:54:53 -040052 self.ordered_fields = OrderedDict([
Bo Yang5db21732015-05-21 14:28:59 -070053 ('foo', descriptor_pb2.FieldDescriptorProto.TYPE_INT64),
54 ('bar', descriptor_pb2.FieldDescriptorProto.TYPE_STRING),
55 ])
56 self._fields = dict(self.ordered_fields)
Feng Xiao6ef984a2014-11-10 17:34:54 -080057
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 Yang5db21732015-05-21 14:28:59 -070069 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 Xiao6ef984a2014-11-10 17:34:54 -080080 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
94if __name__ == '__main__':
Tamir Duberstein9f42f5f2015-01-13 14:47:32 -050095 unittest.main()