Ensure v1 'limit' query parameter works correctly.
The tests were present but were not asserting list results. page_size was overriding the absolute limit so limits were not working if they were less than the page_size. Fixes bug 1037233 Change-Id: If102824212e3846bc65d3f7928cf7aa2e48aaa63
This commit is contained in:
@@ -123,18 +123,20 @@ class ImageManager(base.Manager):
|
|||||||
structure of an image object
|
structure of an image object
|
||||||
:rtype: list of :class:`Image`
|
:rtype: list of :class:`Image`
|
||||||
"""
|
"""
|
||||||
limit = kwargs.get('limit')
|
absolute_limit = kwargs.get('limit')
|
||||||
|
|
||||||
def paginate(qp, seen=0):
|
def paginate(qp, seen=0):
|
||||||
url = '/v1/images/detail?%s' % urllib.urlencode(qp)
|
url = '/v1/images/detail?%s' % urllib.urlencode(qp)
|
||||||
images = self._list(url, "images")
|
images = self._list(url, "images")
|
||||||
for image in images:
|
for image in images:
|
||||||
seen += 1
|
seen += 1
|
||||||
|
if absolute_limit is not None and seen > absolute_limit:
|
||||||
|
return
|
||||||
yield image
|
yield image
|
||||||
|
|
||||||
page_size = qp.get('limit')
|
page_size = qp.get('limit')
|
||||||
if (page_size and len(images) == page_size and
|
if (page_size and len(images) == page_size and
|
||||||
(limit is None or 0 < seen < limit)):
|
(absolute_limit is None or 0 < seen < absolute_limit)):
|
||||||
qp['marker'] = image.id
|
qp['marker'] = image.id
|
||||||
for image in paginate(qp, seen):
|
for image in paginate(qp, seen):
|
||||||
yield image
|
yield image
|
||||||
|
@@ -223,8 +223,9 @@ class ImageManagerTest(unittest.TestCase):
|
|||||||
self.assertEqual(images[2].id, 'c')
|
self.assertEqual(images[2].id, 'c')
|
||||||
|
|
||||||
def test_list_with_limit_less_than_page_size(self):
|
def test_list_with_limit_less_than_page_size(self):
|
||||||
list(self.mgr.list(page_size=20, limit=10))
|
results = list(self.mgr.list(page_size=2, limit=1))
|
||||||
expect = [('GET', '/v1/images/detail?limit=20', {}, None)]
|
expect = [('GET', '/v1/images/detail?limit=2', {}, None)]
|
||||||
|
self.assertEqual(1, len(results))
|
||||||
self.assertEqual(self.api.calls, expect)
|
self.assertEqual(self.api.calls, expect)
|
||||||
|
|
||||||
def test_list_with_limit_greater_than_page_size(self):
|
def test_list_with_limit_greater_than_page_size(self):
|
||||||
|
Reference in New Issue
Block a user