Esempio n. 1
0
    def testMakePath(self):
        builder = to_url.URLEncodedRequestBuilder(SuperSuperMessage(), prefix='pre.')

        self.assertEqual(None, builder.make_path(''))
        self.assertEqual(None, builder.make_path('no_such_field'))
        self.assertEqual(None, builder.make_path('pre.no_such_field'))

        # Missing prefix.
        self.assertEqual(None, builder.make_path('sub_message'))

        # Valid parameters.
        self.assertEqual((('sub_message', None),),
                          builder.make_path('pre.sub_message'))
        self.assertEqual((('sub_message', None), ('sub_messages', 1)),
                          builder.make_path('pre.sub_message.sub_messages-1'))
        self.assertEqual(
            (('sub_message', None),
             ('sub_messages', 1),
             ('int64_value', None)),
            builder.make_path('pre.sub_message.sub_messages-1.int64_value'))

        # Missing index.
        self.assertEqual(
            None,
            builder.make_path('pre.sub_message.sub_messages.integer_field'))

        # Has unexpected index.
        self.assertEqual(
            None,
            builder.make_path('pre.sub_message.sub_message-1.integer_field'))
Esempio n. 2
0
    def testAddParameter_NestedMessages(self):
        message = SuperSuperMessage()
        builder = to_url.URLEncodedRequestBuilder(message, prefix='pre.')

        # Add a repeated empty message.
        self.assertTrue(builder.add_parameter(
            'pre.sub_message.sub_messages-0', ['']))
        sub_message = message.sub_message.sub_messages[0]
        self.assertTrue(1, len(message.sub_message.sub_messages))
        self.assertTrue(isinstance(sub_message,
                                   test_util.OptionalMessage))
        self.assertEqual(None, getattr(sub_message, 'int64_value'))
        self.assertEqual(None, getattr(sub_message, 'string_value'))
        self.assertEqual(None, getattr(sub_message, 'enum_value'))

        # Add a repeated message with value.
        self.assertTrue(builder.add_parameter(
            'pre.sub_message.sub_messages-1.int64_value', ['10']))
        self.assertTrue(2, len(message.sub_message.sub_messages))
        self.assertTrue(10, message.sub_message.sub_messages[1].int64_value)

        # Add another value to the same nested message.
        self.assertTrue(builder.add_parameter(
            'pre.sub_message.sub_messages-1.string_value', ['a string']))
        self.assertTrue(2, len(message.sub_message.sub_messages))
        self.assertEqual(10, message.sub_message.sub_messages[1].int64_value)
        self.assertEqual('a string',
                          message.sub_message.sub_messages[1].string_value)
Esempio n. 3
0
    def testAddParameter_UnexpectedNestedValue2(self):
        """Test getting a nested value on a non-message sub-field

        There is an odd corner case where if trying to insert a repeated value
        on an nested repeated message that would normally succeed in being created
        should fail.  This case can only be tested when the first message of the
        nested messages already exists.

        Another case is trying to access an indexed value nested within a
        non-message field.
        """

        class HasRepeated(messages.Message):
            values = messages.IntegerField(repeated=True)

        class HasNestedRepeated(messages.Message):
            nested = messages.MessageField(HasRepeated, repeated=True)


        message = HasNestedRepeated()
        builder = to_url.URLEncodedRequestBuilder(message, prefix='pre.')

        self.assertTrue(builder.add_parameter('pre.nested-0.values-0', ['1']))
        # Try to create an indexed value on a non-message field.
        self.assertFalse(builder.add_parameter('pre.nested-0.values-0.unknown-0',
                                               ['1']))
        # Try to create an out of range indexed field on an otherwise valid
        # repeated message field.
        self.assertFalse(builder.add_parameter('pre.nested-1.values-1', ['1']))
Esempio n. 4
0
    def testAddParameter_UnexpectedNestedValue(self):
        """Test getting a nested value on a non-message sub-field."""
        message = test_util.HasNestedMessage()
        builder = to_url.URLEncodedRequestBuilder(message, prefix='pre.')

        self.assertFalse(builder.add_parameter('pre.nested.a_value.whatever',
                                               ['1']))
Esempio n. 5
0
    def testAddParameter_SimpleAttributes(self):
        message = test_util.OptionalMessage()
        builder = to_url.URLEncodedRequestBuilder(message, prefix='pre.')

        self.assertTrue(builder.add_parameter('pre.int64_value', ['10']))
        self.assertTrue(builder.add_parameter('pre.string_value', ['a string']))
        self.assertTrue(builder.add_parameter('pre.enum_value', ['VAL1']))
        self.assertEqual(10, message.int64_value)
        self.assertEqual('a string', message.string_value)
        self.assertEqual(test_util.OptionalMessage.SimpleEnum.VAL1,
                          message.enum_value)
Esempio n. 6
0
    def testAddParameter_RepeatedParameters(self):
        message = test_util.OptionalMessage()
        builder = to_url.URLEncodedRequestBuilder(message, prefix='pre.')

        self.assertRaises(messages.DecodeError,
                          builder.add_parameter,
                          'pre.int64_value',
                          [1, 2, 3])
        self.assertRaises(messages.DecodeError,
                          builder.add_parameter,
                          'pre.int64_value',
            [])
Esempio n. 7
0
    def testAddParameter_RepeatedValues(self):
        message = test_util.RepeatedMessage()
        builder = to_url.URLEncodedRequestBuilder(message, prefix='pre.')

        self.assertTrue(builder.add_parameter('pre.int64_value-0', ['20']))
        self.assertTrue(builder.add_parameter('pre.int64_value-1', ['30']))
        self.assertEqual([20, 30], message.int64_value)

        self.assertTrue(builder.add_parameter('pre.string_value-0', ['hi']))
        self.assertTrue(builder.add_parameter('pre.string_value-1', ['lo']))
        self.assertTrue(builder.add_parameter('pre.string_value-1', ['dups overwrite']))
        self.assertEqual(['hi', 'dups overwrite'], message.string_value)
Esempio n. 8
0
    def testAddParameter_InvalidAttributes(self):
        message = SuperSuperMessage()
        builder = to_url.URLEncodedRequestBuilder(message, prefix='pre.')

        def assert_empty():
            self.assertEqual(None, getattr(message, 'sub_message'))
            self.assertEqual([], getattr(message, 'sub_messages'))

        self.assertFalse(builder.add_parameter('pre.nothing', ['x']))
        assert_empty()

        self.assertFalse(builder.add_parameter('pre.sub_messages', ['x']))
        self.assertFalse(builder.add_parameter('pre.sub_messages-1.nothing', ['x']))
        assert_empty()
Esempio n. 9
0
    def testAddParameter_NestedAttributes(self):
        message = SuperSuperMessage()
        builder = to_url.URLEncodedRequestBuilder(message, prefix='pre.')

        # Set an empty message fields.
        self.assertTrue(builder.add_parameter('pre.sub_message', ['']))
        self.assertTrue(isinstance(message.sub_message, SuperMessage))

        # Add a basic attribute.
        self.assertTrue(builder.add_parameter(
            'pre.sub_message.sub_message.int64_value', ['10']))
        self.assertTrue(builder.add_parameter(
            'pre.sub_message.sub_message.string_value', ['hello']))

        self.assertTrue(10, message.sub_message.sub_message.int64_value)
        self.assertTrue('hello', message.sub_message.sub_message.string_value)
Esempio n. 10
0
    def testInvalidFieldFormat(self):
        message = test_util.OptionalMessage()
        builder = to_url.URLEncodedRequestBuilder(message, prefix='pre.')

        self.assertFalse(builder.add_parameter('pre.illegal%20', ['1']))
Esempio n. 11
0
    def testAddParameter_InvalidValuesMayRepeat(self):
        message = test_util.OptionalMessage()
        builder = to_url.URLEncodedRequestBuilder(message, prefix='pre.')

        self.assertFalse(builder.add_parameter('nothing', [1, 2, 3]))