Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't bother with count() if a limit has already been set #330

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions flask_restless/helpers.py
Expand Up @@ -561,6 +561,8 @@ def count(session, query):
queries.

"""
if query._limit:
return query._limit
num_results = None
counts = query.selectable.with_only_columns([func.count()])
num_results = session.execute(counts.order_by(None)).scalar()
Expand Down
17 changes: 17 additions & 0 deletions tests/test_views.py
Expand Up @@ -1694,6 +1694,23 @@ def test_search2(self):
resp = self.app.search('/api/person', dumps(search))
assert resp.status_code == 200
assert 1 == len(loads(resp.data)['objects'])
assert loads(resp.data)['num_results'] == 1
assert loads(resp.data)['objects'][0]['name'] == u'Mary'

# Testing limit by itself
search = {'limit': 1}
resp = self.app.search('/api/person', dumps(search))
assert resp.status_code == 200
assert 1 == len(loads(resp.data)['objects'])
assert loads(resp.data)['num_results'] == 1
assert loads(resp.data)['objects'][0]['name'] == u'Lincoln'

# Testing offset by itself
search = {'offset': 1}
resp = self.app.search('/api/person', dumps(search))
assert resp.status_code == 200
assert 4 == len(loads(resp.data)['objects'])
assert loads(resp.data)['num_results'] == 4
assert loads(resp.data)['objects'][0]['name'] == u'Mary'

# Testing multiple results when calling .one()
Expand Down