• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# -*- coding: utf-8 -*-
2# Protocol Buffers - Google's data interchange format
3# Copyright 2008 Google Inc.  All rights reserved.
4#
5# Use of this source code is governed by a BSD-style
6# license that can be found in the LICENSE file or at
7# https://developers.google.com/open-source/licenses/bsd
8
9"""Tests proto Any APIs."""
10
11import unittest
12
13from google.protobuf import any
14
15from google.protobuf import any_pb2
16from google.protobuf import unittest_pb2
17
18
19class AnyTest(unittest.TestCase):
20
21  def test_pack_unpack(self):
22    all_types = unittest_pb2.TestAllTypes()
23    any_msg = any.pack(all_types)
24    all_descriptor = all_types.DESCRIPTOR
25    self.assertEqual(
26        any_msg.type_url, 'type.googleapis.com/%s' % all_descriptor.full_name
27    )
28    unpacked_message = unittest_pb2.TestAllTypes()
29    self.assertTrue(any.unpack(any_msg, unpacked_message))
30
31  def test_type_name(self):
32    all_types = unittest_pb2.TestAllTypes()
33    any_msg = any.pack(all_types)
34    self.assertEqual(any.type_name(any_msg), 'protobuf_unittest.TestAllTypes')
35
36  def test_is_type(self):
37    all_types = unittest_pb2.TestAllTypes()
38    any_msg = any.pack(all_types)
39    all_descriptor = all_types.DESCRIPTOR
40    self.assertTrue(any.is_type(any_msg, all_descriptor))
41
42    empty_any = any_pb2.Any()
43    self.assertFalse(any.is_type(empty_any, all_descriptor))
44
45
46if __name__ == '__main__':
47  unittest.main()
48