def testProtourlencodeUnrecognizedField(self):
        """Test that unrecognized fields are saved and can be accessed."""
        class MyMessage(messages.Message):
            number = messages.IntegerField(1)

        decoded = protourlencode.decode_message(MyMessage,
                                                self.unexpected_tag_message)
        self.assertEquals(1, len(decoded.all_unrecognized_fields()))
        self.assertEquals('unexpected', decoded.all_unrecognized_fields()[0])
        # Unknown values set to a list of however many values had that name.
        self.assertEquals((['whatever'], messages.Variant.STRING),
                          decoded.get_unrecognized_field_info('unexpected'))

        repeated_unknown = urllib.urlencode([('repeated', 400),
                                             ('repeated', 'test'),
                                             ('repeated', '123.456')])
        decoded2 = protourlencode.decode_message(MyMessage, repeated_unknown)
        self.assertEquals(
            (['400', 'test', '123.456'], messages.Variant.STRING),
            decoded2.get_unrecognized_field_info('repeated'))
  def testProtourlencodeUnrecognizedField(self):
    """Test that unrecognized fields are saved and can be accessed."""

    class MyMessage(messages.Message):
      number = messages.IntegerField(1)

    decoded = protourlencode.decode_message(MyMessage,
                                            self.unexpected_tag_message)
    self.assertEquals(1, len(decoded.all_unrecognized_fields()))
    self.assertEquals('unexpected', decoded.all_unrecognized_fields()[0])
    # Unknown values set to a list of however many values had that name.
    self.assertEquals((['whatever'], messages.Variant.STRING),
                      decoded.get_unrecognized_field_info('unexpected'))

    repeated_unknown = urllib.urlencode([('repeated', 400),
                                         ('repeated', 'test'),
                                         ('repeated', '123.456')])
    decoded2 = protourlencode.decode_message(MyMessage, repeated_unknown)
    self.assertEquals((['400', 'test', '123.456'], messages.Variant.STRING),
                      decoded2.get_unrecognized_field_info('repeated'))
  def testParameterPrefix(self):
    """Test using the 'prefix' parameter to encode_message."""
    class MyMessage(messages.Message):
      number = messages.IntegerField(1)
      names = messages.StringField(2, repeated=True)

    message = MyMessage()
    message.number = 10
    message.names = [u'Fred', u'Lisa']

    encoded_message = protourlencode.encode_message(message, prefix='prefix-')
    self.assertEquals({'prefix-number': ['10'],
                       'prefix-names-0': ['Fred'],
                       'prefix-names-1': ['Lisa'],
                      },
                      cgi.parse_qs(encoded_message))

    self.assertEquals(message, protourlencode.decode_message(MyMessage,
                                                             encoded_message,
                                                             prefix='prefix-'))
예제 #4
0
    def testParameterPrefix(self):
        """Test using the 'prefix' parameter to encode_message."""
        class MyMessage(messages.Message):
            number = messages.IntegerField(1)
            names = messages.StringField(2, repeated=True)

        message = MyMessage()
        message.number = 10
        message.names = [u'Fred', u'Lisa']

        encoded_message = protourlencode.encode_message(message,
                                                        prefix='prefix-')
        self.assertEquals(
            {
                'prefix-number': ['10'],
                'prefix-names-0': ['Fred'],
                'prefix-names-1': ['Lisa'],
            }, cgi.parse_qs(encoded_message))

        self.assertEquals(
            message,
            protourlencode.decode_message(MyMessage,
                                          encoded_message,
                                          prefix='prefix-'))