Skip to content

Commit

Permalink
add ability to pass limit param to collection queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Wainstock committed May 28, 2014
1 parent 5c4b707 commit dd35937
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 4 additions & 1 deletion sandman/sandman.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,19 @@ def retrieve_collection(collection, query_arguments=None):
if query_arguments:
filters = []
order = []
limit = None
for key, value in query_arguments.items():
if key == 'page':
continue
if value.startswith('%'):
filters.append(getattr(cls, key).like(str(value), escape='/'))
elif key == 'sort':
order.append(getattr(cls, value))
elif key == 'limit':
limit = value
elif key:
filters.append(getattr(cls, key) == value)
resources = session.query(cls).filter(*filters).order_by(*order)
resources = session.query(cls).filter(*filters).order_by(*order).limit(limit)
else:
resources = session.query(cls).all()
return resources
Expand Down
7 changes: 6 additions & 1 deletion tests/test_sandman.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def setup_method(self, _):
self.app = app.test_client()
#pylint: disable=unused-variable
from . import models

def teardown_method(self, _):
"""Remove the database file copied during setup."""
os.unlink(self.DB_LOCATION)
Expand Down Expand Up @@ -72,6 +72,11 @@ def test_get(self):
response = self.get_response('/artists', 200)
assert len(json.loads(response.get_data(as_text=True))[u'resources']) == 275

def test_get_with_limit(self):
"""Test simple HTTP GET"""
response = self.get_response('/artists', 200, params={'limit': 10})
assert len(json.loads(response.get_data(as_text=True))[u'resources']) == 10

def test_get_with_filter(self):
"""Test simple HTTP GET"""
response = self.get_response('/artists', 200, params={'Name': 'AC/DC'})
Expand Down

0 comments on commit dd35937

Please sign in to comment.