Update registry db api to properly handle pagination through sorted results.

This commit is contained in:
Brian Waldon 2011-08-02 18:38:20 +00:00 committed by Tarmac
commit 99b8a56259
2 changed files with 32 additions and 5 deletions

View File

@ -215,11 +215,18 @@ def image_get_all(context, filters=None, marker=None, limit=None,
if marker != None:
# images returned should be created before the image defined by marker
marker_created_at = image_get(context, marker).created_at
query = query.filter(
or_(models.Image.created_at < marker_created_at,
and_(models.Image.created_at == marker_created_at,
models.Image.id < marker)))
marker_image = image_get(context, marker)
marker_value = getattr(marker_image, sort_key)
if sort_dir == 'desc':
query = query.filter(
or_(sort_key_attr < marker_value,
and_(sort_key_attr == marker_value,
models.Image.id < marker)))
else:
query = query.filter(
or_(sort_key_attr > marker_value,
and_(sort_key_attr == marker_value,
models.Image.id > marker)))
if limit != None:
query = query.limit(limit)

View File

@ -1028,6 +1028,26 @@ class TestApiHttplib2(functional.FunctionalTest):
self.assertEqual(data['images'][1]['id'], 3)
self.assertEqual(data['images'][2]['id'], 2)
# 5. GET /images sorted by size desc with a marker
params = 'sort_key=size&sort_dir=desc&marker=1'
path = "http://%s:%d/v1/images?%s" % ("0.0.0.0", self.api_port, params)
http = httplib2.Http()
response, content = http.request(path, 'GET')
self.assertEqual(response.status, 200)
data = json.loads(content)
self.assertEqual(len(data['images']), 2)
self.assertEqual(data['images'][0]['id'], 3)
self.assertEqual(data['images'][1]['id'], 2)
# 6. GET /images sorted by name asc with a marker
params = 'sort_key=name&sort_dir=asc&marker=3'
path = "http://%s:%d/v1/images?%s" % ("0.0.0.0", self.api_port, params)
http = httplib2.Http()
response, content = http.request(path, 'GET')
self.assertEqual(response.status, 200)
data = json.loads(content)
self.assertEqual(len(data['images']), 0)
self.stop_servers()
def test_duplicate_image_upload(self):