def testMessageName(self):
   # Creates and sets message.
   submessage = any_test_pb2.TestAny()
   submessage.int_value = 12345
   msg = any_pb2.Any()
   msg.Pack(submessage)
   self.assertEqual(msg.TypeName(), 'dis_sdk_python.dependency.google.protobuf.internal.TestAny')
 def testPackDeterministic(self):
   submessage = any_test_pb2.TestAny()
   for i in range(10):
     submessage.map_value[str(i)] = i * 2
   msg = any_pb2.Any()
   msg.Pack(submessage, deterministic=True)
   serialized = msg.SerializeToString(deterministic=True)
   golden = (b'\n4type.googleapis.com/dis_sdk_python.dependency.google.protobuf.internal.TestAny\x12F'
             b'\x1a\x05\n\x010\x10\x00\x1a\x05\n\x011\x10\x02\x1a\x05\n\x01'
             b'2\x10\x04\x1a\x05\n\x013\x10\x06\x1a\x05\n\x014\x10\x08\x1a'
             b'\x05\n\x015\x10\n\x1a\x05\n\x016\x10\x0c\x1a\x05\n\x017\x10'
             b'\x0e\x1a\x05\n\x018\x10\x10\x1a\x05\n\x019\x10\x12')
   self.assertEqual(golden, serialized)
 def testPackWithCustomTypeUrl(self):
   submessage = any_test_pb2.TestAny()
   submessage.int_value = 12345
   msg = any_pb2.Any()
   # Pack with a custom type URL prefix.
   msg.Pack(submessage, 'type.myservice.com')
   self.assertEqual(msg.type_url,
                    'type.myservice.com/%s' % submessage.DESCRIPTOR.full_name)
   # Pack with a custom type URL prefix ending with '/'.
   msg.Pack(submessage, 'type.myservice.com/')
   self.assertEqual(msg.type_url,
                    'type.myservice.com/%s' % submessage.DESCRIPTOR.full_name)
   # Pack with an empty type URL prefix.
   msg.Pack(submessage, '')
   self.assertEqual(msg.type_url,
                    '/%s' % submessage.DESCRIPTOR.full_name)
   # Test unpacking the type.
   unpacked_message = any_test_pb2.TestAny()
   self.assertTrue(msg.Unpack(unpacked_message))
   self.assertEqual(submessage, unpacked_message)
 def testInvalidAny(self):
   message = any_pb2.Any()
   text = '{"@type": "type.googleapis.com/dis_sdk_python.dependency.google.protobuf.Int32Value"}'
   self.assertRaisesRegex(
       KeyError,
       'value',
       json_format.Parse, text, message)
   text = '{"value": 1234}'
   self.assertRaisesRegex(
       json_format.ParseError,
       '@type is missing when parsing any message.',
       json_format.Parse, text, message)
   text = '{"@type": "type.googleapis.com/MessageNotExist", "value": 1234}'
   self.assertRaisesRegex(
       TypeError,
       'Can not find message descriptor by type_url: '
       'type.googleapis.com/MessageNotExist.',
       json_format.Parse, text, message)
   # Only last part is to be used: b/25630112
   text = (r'{"@type": "incorrect.googleapis.com/dis_sdk_python.dependency.google.protobuf.Int32Value",'
           r'"value": 1234}')
   json_format.Parse(text, message)
  def testWellKnownInAnyMessage(self):
    message = any_pb2.Any()
    int32_value = wrappers_pb2.Int32Value()
    int32_value.value = 1234
    message.Pack(int32_value)
    self.assertEqual(
        json.loads(json_format.MessageToJson(message, True)),
        json.loads(
            '{\n'
            '  "@type": \"type.googleapis.com/dis_sdk_python.dependency.google.protobuf.Int32Value\",\n'
            '  "value": 1234\n'
            '}\n'))
    parsed_message = any_pb2.Any()
    self.CheckParseBack(message, parsed_message)

    timestamp = timestamp_pb2.Timestamp()
    message.Pack(timestamp)
    self.assertEqual(
        json.loads(json_format.MessageToJson(message, True)),
        json.loads(
            '{\n'
            '  "@type": "type.googleapis.com/dis_sdk_python.dependency.google.protobuf.Timestamp",\n'
            '  "value": "1970-01-01T00:00:00Z"\n'
            '}\n'))
    self.CheckParseBack(message, parsed_message)

    duration = duration_pb2.Duration()
    duration.seconds = 1
    message.Pack(duration)
    self.assertEqual(
        json.loads(json_format.MessageToJson(message, True)),
        json.loads(
            '{\n'
            '  "@type": "type.googleapis.com/dis_sdk_python.dependency.google.protobuf.Duration",\n'
            '  "value": "1s"\n'
            '}\n'))
    self.CheckParseBack(message, parsed_message)

    field_mask = field_mask_pb2.FieldMask()
    field_mask.paths.append('foo.bar')
    field_mask.paths.append('bar')
    message.Pack(field_mask)
    self.assertEqual(
        json.loads(json_format.MessageToJson(message, True)),
        json.loads(
            '{\n'
            '  "@type": "type.googleapis.com/dis_sdk_python.dependency.google.protobuf.FieldMask",\n'
            '  "value": "foo.bar,bar"\n'
            '}\n'))
    self.CheckParseBack(message, parsed_message)

    struct_message = struct_pb2.Struct()
    struct_message['name'] = 'Jim'
    message.Pack(struct_message)
    self.assertEqual(
        json.loads(json_format.MessageToJson(message, True)),
        json.loads(
            '{\n'
            '  "@type": "type.googleapis.com/dis_sdk_python.dependency.google.protobuf.Struct",\n'
            '  "value": {"name": "Jim"}\n'
            '}\n'))
    self.CheckParseBack(message, parsed_message)

    nested_any = any_pb2.Any()
    int32_value.value = 5678
    nested_any.Pack(int32_value)
    message.Pack(nested_any)
    self.assertEqual(
        json.loads(json_format.MessageToJson(message, True)),
        json.loads(
            '{\n'
            '  "@type": "type.googleapis.com/dis_sdk_python.dependency.google.protobuf.Any",\n'
            '  "value": {\n'
            '    "@type": "type.googleapis.com/dis_sdk_python.dependency.google.protobuf.Int32Value",\n'
            '    "value": 5678\n'
            '  }\n'
            '}\n'))
    self.CheckParseBack(message, parsed_message)