• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Protocol Buffers - Google's data interchange format
2# Copyright 2008 Google Inc.  All rights reserved.
3#
4# Use of this source code is governed by a BSD-style
5# license that can be found in the LICENSE file or at
6# https://developers.google.com/open-source/licenses/bsd
7
8"""Tests for google.protobuf.symbol_database."""
9
10import unittest
11
12from google.protobuf import descriptor
13from google.protobuf import descriptor_pool
14from google.protobuf import symbol_database
15from google.protobuf import unittest_pb2
16
17
18class SymbolDatabaseTest(unittest.TestCase):
19
20  def _Database(self):
21    if descriptor._USE_C_DESCRIPTORS:
22      # The C++ implementation does not allow mixing descriptors from
23      # different pools.
24      db = symbol_database.SymbolDatabase(pool=descriptor_pool.Default())
25    else:
26      db = symbol_database.SymbolDatabase()
27    # Register representative types from unittest_pb2.
28    db.RegisterFileDescriptor(unittest_pb2.DESCRIPTOR)
29    db.RegisterMessage(unittest_pb2.TestAllTypes)
30    db.RegisterMessage(unittest_pb2.TestAllTypes.NestedMessage)
31    db.RegisterMessage(unittest_pb2.TestAllTypes.OptionalGroup)
32    db.RegisterMessage(unittest_pb2.TestAllTypes.RepeatedGroup)
33    db.RegisterEnumDescriptor(unittest_pb2.ForeignEnum.DESCRIPTOR)
34    db.RegisterEnumDescriptor(unittest_pb2.TestAllTypes.NestedEnum.DESCRIPTOR)
35    db.RegisterServiceDescriptor(unittest_pb2._TESTSERVICE)
36    return db
37
38  def testGetMessages(self):
39    messages = self._Database().GetMessages(
40        ['google/protobuf/unittest.proto'])
41    self.assertTrue(
42        unittest_pb2.TestAllTypes is
43        messages['protobuf_unittest.TestAllTypes'])
44
45  def testGetSymbol(self):
46    self.assertEqual(
47        unittest_pb2.TestAllTypes, self._Database().GetSymbol(
48            'protobuf_unittest.TestAllTypes'))
49    self.assertEqual(
50        unittest_pb2.TestAllTypes.NestedMessage, self._Database().GetSymbol(
51            'protobuf_unittest.TestAllTypes.NestedMessage'))
52    self.assertEqual(
53        unittest_pb2.TestAllTypes.OptionalGroup, self._Database().GetSymbol(
54            'protobuf_unittest.TestAllTypes.OptionalGroup'))
55    self.assertEqual(
56        unittest_pb2.TestAllTypes.RepeatedGroup, self._Database().GetSymbol(
57            'protobuf_unittest.TestAllTypes.RepeatedGroup'))
58
59  def testEnums(self):
60    # Check registration of types in the pool.
61    self.assertEqual(
62        'protobuf_unittest.ForeignEnum',
63        self._Database().pool.FindEnumTypeByName(
64            'protobuf_unittest.ForeignEnum').full_name)
65    self.assertEqual(
66        'protobuf_unittest.TestAllTypes.NestedEnum',
67        self._Database().pool.FindEnumTypeByName(
68            'protobuf_unittest.TestAllTypes.NestedEnum').full_name)
69
70  def testFindMessageTypeByName(self):
71    self.assertEqual(
72        'protobuf_unittest.TestAllTypes',
73        self._Database().pool.FindMessageTypeByName(
74            'protobuf_unittest.TestAllTypes').full_name)
75    self.assertEqual(
76        'protobuf_unittest.TestAllTypes.NestedMessage',
77        self._Database().pool.FindMessageTypeByName(
78            'protobuf_unittest.TestAllTypes.NestedMessage').full_name)
79
80  def testFindServiceByName(self):
81    self.assertEqual(
82        'protobuf_unittest.TestService',
83        self._Database().pool.FindServiceByName(
84            'protobuf_unittest.TestService').full_name)
85
86  def testFindFileContainingSymbol(self):
87    # Lookup based on either enum or message.
88    self.assertEqual(
89        'google/protobuf/unittest.proto',
90        self._Database().pool.FindFileContainingSymbol(
91            'protobuf_unittest.TestAllTypes.NestedEnum').name)
92    self.assertEqual(
93        'google/protobuf/unittest.proto',
94        self._Database().pool.FindFileContainingSymbol(
95            'protobuf_unittest.TestAllTypes').name)
96
97  def testFindFileByName(self):
98    self.assertEqual(
99        'google/protobuf/unittest.proto',
100        self._Database().pool.FindFileByName(
101            'google/protobuf/unittest.proto').name)
102
103
104if __name__ == '__main__':
105  unittest.main()
106