Tamir Duberstein | 9d9d0b7 | 2015-04-11 20:23:45 -0700 | [diff] [blame] | 1 | #! /usr/bin/env python |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 2 | # |
| 3 | # Protocol Buffers - Google's data interchange format |
| 4 | # Copyright 2008 Google Inc. All rights reserved. |
Feng Xiao | e428862 | 2014-10-01 16:26:23 -0700 | [diff] [blame] | 5 | # https://developers.google.com/protocol-buffers/ |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 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.symbol_database.""" |
| 34 | |
Jisi Liu | dbea00a | 2015-10-05 16:08:22 -0700 | [diff] [blame] | 35 | try: |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 36 | import unittest2 as unittest #PY26 |
Jisi Liu | dbea00a | 2015-10-05 16:08:22 -0700 | [diff] [blame] | 37 | except ImportError: |
| 38 | import unittest |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 39 | |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 40 | from google.protobuf import unittest_pb2 |
Jisi Liu | 46e8ff6 | 2015-10-05 11:59:43 -0700 | [diff] [blame] | 41 | from google.protobuf import descriptor |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 42 | from google.protobuf import symbol_database |
| 43 | |
Tres Seaver | 7ee2583 | 2015-01-13 14:47:32 -0500 | [diff] [blame] | 44 | class SymbolDatabaseTest(unittest.TestCase): |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 45 | |
| 46 | def _Database(self): |
Jisi Liu | 46e8ff6 | 2015-10-05 11:59:43 -0700 | [diff] [blame] | 47 | # TODO(b/17734095): Remove this difference when the C++ implementation |
| 48 | # supports multiple databases. |
| 49 | if descriptor._USE_C_DESCRIPTORS: |
| 50 | return symbol_database.Default() |
| 51 | else: |
| 52 | db = symbol_database.SymbolDatabase() |
| 53 | # Register representative types from unittest_pb2. |
| 54 | db.RegisterFileDescriptor(unittest_pb2.DESCRIPTOR) |
| 55 | db.RegisterMessage(unittest_pb2.TestAllTypes) |
| 56 | db.RegisterMessage(unittest_pb2.TestAllTypes.NestedMessage) |
| 57 | db.RegisterMessage(unittest_pb2.TestAllTypes.OptionalGroup) |
| 58 | db.RegisterMessage(unittest_pb2.TestAllTypes.RepeatedGroup) |
| 59 | db.RegisterEnumDescriptor(unittest_pb2.ForeignEnum.DESCRIPTOR) |
| 60 | db.RegisterEnumDescriptor(unittest_pb2.TestAllTypes.NestedEnum.DESCRIPTOR) |
| 61 | return db |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 62 | |
| 63 | def testGetPrototype(self): |
| 64 | instance = self._Database().GetPrototype( |
| 65 | unittest_pb2.TestAllTypes.DESCRIPTOR) |
| 66 | self.assertTrue(instance is unittest_pb2.TestAllTypes) |
| 67 | |
| 68 | def testGetMessages(self): |
| 69 | messages = self._Database().GetMessages( |
| 70 | ['google/protobuf/unittest.proto']) |
| 71 | self.assertTrue( |
| 72 | unittest_pb2.TestAllTypes is |
| 73 | messages['protobuf_unittest.TestAllTypes']) |
| 74 | |
| 75 | def testGetSymbol(self): |
Tres Seaver | a2abc94 | 2015-01-13 15:47:55 -0500 | [diff] [blame] | 76 | self.assertEqual( |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 77 | unittest_pb2.TestAllTypes, self._Database().GetSymbol( |
| 78 | 'protobuf_unittest.TestAllTypes')) |
Tres Seaver | a2abc94 | 2015-01-13 15:47:55 -0500 | [diff] [blame] | 79 | self.assertEqual( |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 80 | unittest_pb2.TestAllTypes.NestedMessage, self._Database().GetSymbol( |
| 81 | 'protobuf_unittest.TestAllTypes.NestedMessage')) |
Tres Seaver | a2abc94 | 2015-01-13 15:47:55 -0500 | [diff] [blame] | 82 | self.assertEqual( |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 83 | unittest_pb2.TestAllTypes.OptionalGroup, self._Database().GetSymbol( |
| 84 | 'protobuf_unittest.TestAllTypes.OptionalGroup')) |
Tres Seaver | a2abc94 | 2015-01-13 15:47:55 -0500 | [diff] [blame] | 85 | self.assertEqual( |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 86 | unittest_pb2.TestAllTypes.RepeatedGroup, self._Database().GetSymbol( |
| 87 | 'protobuf_unittest.TestAllTypes.RepeatedGroup')) |
| 88 | |
| 89 | def testEnums(self): |
| 90 | # Check registration of types in the pool. |
Tres Seaver | a2abc94 | 2015-01-13 15:47:55 -0500 | [diff] [blame] | 91 | self.assertEqual( |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 92 | 'protobuf_unittest.ForeignEnum', |
| 93 | self._Database().pool.FindEnumTypeByName( |
| 94 | 'protobuf_unittest.ForeignEnum').full_name) |
Tres Seaver | a2abc94 | 2015-01-13 15:47:55 -0500 | [diff] [blame] | 95 | self.assertEqual( |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 96 | 'protobuf_unittest.TestAllTypes.NestedEnum', |
| 97 | self._Database().pool.FindEnumTypeByName( |
| 98 | 'protobuf_unittest.TestAllTypes.NestedEnum').full_name) |
| 99 | |
| 100 | def testFindMessageTypeByName(self): |
Tres Seaver | a2abc94 | 2015-01-13 15:47:55 -0500 | [diff] [blame] | 101 | self.assertEqual( |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 102 | 'protobuf_unittest.TestAllTypes', |
| 103 | self._Database().pool.FindMessageTypeByName( |
| 104 | 'protobuf_unittest.TestAllTypes').full_name) |
Tres Seaver | a2abc94 | 2015-01-13 15:47:55 -0500 | [diff] [blame] | 105 | self.assertEqual( |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 106 | 'protobuf_unittest.TestAllTypes.NestedMessage', |
| 107 | self._Database().pool.FindMessageTypeByName( |
| 108 | 'protobuf_unittest.TestAllTypes.NestedMessage').full_name) |
| 109 | |
| 110 | def testFindFindContainingSymbol(self): |
| 111 | # Lookup based on either enum or message. |
Tres Seaver | a2abc94 | 2015-01-13 15:47:55 -0500 | [diff] [blame] | 112 | self.assertEqual( |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 113 | 'google/protobuf/unittest.proto', |
| 114 | self._Database().pool.FindFileContainingSymbol( |
| 115 | 'protobuf_unittest.TestAllTypes.NestedEnum').name) |
Tres Seaver | a2abc94 | 2015-01-13 15:47:55 -0500 | [diff] [blame] | 116 | self.assertEqual( |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 117 | 'google/protobuf/unittest.proto', |
| 118 | self._Database().pool.FindFileContainingSymbol( |
| 119 | 'protobuf_unittest.TestAllTypes').name) |
| 120 | |
| 121 | def testFindFileByName(self): |
Tres Seaver | a2abc94 | 2015-01-13 15:47:55 -0500 | [diff] [blame] | 122 | self.assertEqual( |
jieluo@google.com | bde4a32 | 2014-08-12 21:10:30 +0000 | [diff] [blame] | 123 | 'google/protobuf/unittest.proto', |
| 124 | self._Database().pool.FindFileByName( |
| 125 | 'google/protobuf/unittest.proto').name) |
| 126 | |
| 127 | |
| 128 | if __name__ == '__main__': |
Tres Seaver | 7ee2583 | 2015-01-13 14:47:32 -0500 | [diff] [blame] | 129 | unittest.main() |