Example #1
0
 def testParseDictAnyDescriptorPoolMissingType(self):
     # Confirm that ParseDict does not raise ParseError with default pool
     js_dict = {
         'any_value': {
             '@type': 'type.googleapis.com/proto3.MessageType',
             'value': 1234
         }
     }
     json_format.ParseDict(js_dict, any_test_pb2.TestAny())
     # Check ParseDict raises ParseError with empty pool
     js_dict = {
         'any_value': {
             '@type': 'type.googleapis.com/proto3.MessageType',
             'value': 1234
         }
     }
     with self.assertRaises(json_format.ParseError) as cm:
         empty_pool = descriptor_pool.DescriptorPool()
         json_format.ParseDict(js_dict,
                               any_test_pb2.TestAny(),
                               descriptor_pool=empty_pool)
     self.assertEqual(
         str(cm.exception),
         'Failed to parse any_value field: Can not find message descriptor by'
         ' type_url: type.googleapis.com/proto3.MessageType..')
 def testAnyMessageDescriptorPoolMissingType(self):
   packed_message = unittest_pb2.OneString()
   packed_message.data = 'string'
   message = any_test_pb2.TestAny()
   message.any_value.Pack(packed_message)
   empty_pool = descriptor_pool.DescriptorPool()
   with self.assertRaises(TypeError) as cm:
     json_format.MessageToJson(message, True, descriptor_pool=empty_pool)
   self.assertEqual(
       'Can not find message descriptor by type_url:'
       ' type.googleapis.com/protobuf_unittest.OneString.',
       str(cm.exception))