Skip to content

Commit

Permalink
Merge pull request #689 from danielgtaylor/page-string
Browse files Browse the repository at this point in the history
Fix pagination with string params. Fixes #689.
  • Loading branch information
danielgtaylor committed Mar 5, 2014
2 parents 3fad698 + 05044cc commit bf5e9b3
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
16 changes: 15 additions & 1 deletion awscli/customizations/paginate.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,22 @@ def unify_paging_params(argument_table, operation, event_name, **kwargs):
STARTING_TOKEN_HELP,
operation,
parse_type='string')
# Try to get the pagination parameter type
limit_param = None
if 'limit_key' in operation.pagination:
for param in operation.params:
if param.name == operation.pagination['limit_key']:
limit_param = param
break

type_ = limit_param and limit_param.type or 'integer'
if limit_param and limit_param.type not in PageArgument.type_map:
raise TypeError(('Unsupported pagination type {0} for operation {1}'
' and parameter {2}').format(type_, operation.name,
limit_param.name))

argument_table['max-items'] = PageArgument('max-items', MAX_ITEMS_HELP,
operation, parse_type='integer')
operation, parse_type=type_)


def check_should_enable_pagination(input_tokens, parsed_args, parsed_globals, **kwargs):
Expand Down
57 changes: 56 additions & 1 deletion tests/unit/customizations/test_paginate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@
from awscli.customizations import paginate


class TestArgumentTableModifications(unittest.TestCase):
class TestPaginateBase(unittest.TestCase):

def setUp(self):
self.operation = mock.Mock()
self.operation.can_paginate = True
self.foo_param = mock.Mock()
self.foo_param.cli_name = 'foo'
self.foo_param.name = 'Foo'
self.foo_param.type = 'string'
self.bar_param = mock.Mock()
self.bar_param.cli_name = 'bar'
self.bar_param.type = 'string'
self.bar_param.name = 'Bar'
self.params = [self.foo_param, self.bar_param]
self.operation.pagination = {
Expand All @@ -35,6 +37,9 @@ def setUp(self):
}
self.operation.params = self.params


class TestArgumentTableModifications(TestPaginateBase):

def test_customize_arg_table(self):
argument_table = {
'foo': mock.Mock(),
Expand Down Expand Up @@ -66,3 +71,53 @@ def test_operation_with_no_paginate(self):
paginate.unify_paging_params(argument_table, self.operation,
'building-argument-table.foo.bar')
self.assertEqual(starting_table, argument_table)


class TestStringLimitKey(TestPaginateBase):

def setUp(self):
super(TestStringLimitKey, self).setUp()
self.bar_param.type = 'string'

def test_integer_limit_key(self):
argument_table = {
'foo': mock.Mock(),
'bar': mock.Mock(),
}
paginate.unify_paging_params(argument_table, self.operation,
'building-argument-table.foo.bar')
# Max items should be the same type as bar, which may not be an int
self.assertEqual('string', argument_table['max-items'].cli_type_name)


class TestIntegerLimitKey(TestPaginateBase):

def setUp(self):
super(TestIntegerLimitKey, self).setUp()
self.bar_param.type = 'integer'

def test_integer_limit_key(self):
argument_table = {
'foo': mock.Mock(),
'bar': mock.Mock(),
}
paginate.unify_paging_params(argument_table, self.operation,
'building-argument-table.foo.bar')
# Max items should be the same type as bar, which may not be an int
self.assertEqual('integer', argument_table['max-items'].cli_type_name)


class TestBadLimitKey(TestPaginateBase):

def setUp(self):
super(TestBadLimitKey, self).setUp()
self.bar_param.type = 'bad'

def test_integer_limit_key(self):
argument_table = {
'foo': mock.Mock(),
'bar': mock.Mock(),
}
with self.assertRaises(TypeError):
paginate.unify_paging_params(argument_table, self.operation,
'building-argument-table.foo.bar')
18 changes: 17 additions & 1 deletion tests/unit/route53/test_resource_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,20 @@ def test_short_resource_id(self):
self.assert_params_for_cmd(cmdline, result, expected_rc=0,
ignore_params=['payload'])[0]



class TestMaxItems(BaseAWSCommandParamsTest):

prefix = 'route53 list-resource-record-sets'

def test_full_resource_id(self):
args = ' --hosted-zone-id /hostedzone/ABCD --max-items 1'
cmdline = self.prefix + args
result = {
'uri_params': {
'HostedZoneId': 'ABCD',
'MaxItems': '1'
},
'headers': {}
}
self.assert_params_for_cmd(cmdline, result, expected_rc=0,
ignore_params=['payload'])[0]

0 comments on commit bf5e9b3

Please sign in to comment.