Skip to content

Commit

Permalink
Reducing limit or the life of a datastore query iterator.
Browse files Browse the repository at this point in the history
Fixes #1763.
  • Loading branch information
dhermes committed May 12, 2016
1 parent 6064270 commit 2a72031
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
2 changes: 2 additions & 0 deletions gcloud/datastore/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,8 @@ def __iter__(self):
yield entity
if not self._more_results:
break
if self._limit is not None:
self._limit -= len(self._page)
self.next_page()


Expand Down
41 changes: 39 additions & 2 deletions gcloud/datastore/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,7 @@ def test___iter___w_more(self):
[{'kind': self._KIND, 'id': self._ID}])
self.assertEqual(entities[1]['foo'], u'Foo')
qpb1 = _pb_from_query(query)
qpb1.offset = 0
qpb2 = _pb_from_query(query)
qpb2.offset = 0
qpb2.start_cursor = self._END
EXPECTED1 = {
'project': self._PROJECT,
Expand All @@ -543,6 +541,45 @@ def test___iter___w_more(self):
self.assertEqual(connection._called_with[0], EXPECTED1)
self.assertEqual(connection._called_with[1], EXPECTED2)

def test___iter___w_limit(self):
from gcloud.datastore.query import _pb_from_query

connection = _Connection()
client = self._makeClient(connection)
query = _Query(client, self._KIND, self._PROJECT, self._NAMESPACE)
self._addQueryResults(connection, more=True)
self._addQueryResults(connection)
iterator = self._makeOne(query, client, limit=2)
entities = list(iterator)

self.assertFalse(iterator._more_results)
self.assertEqual(len(entities), 2)
for entity in entities:
self.assertEqual(
entity.key.path,
[{'kind': self._KIND, 'id': self._ID}])
self.assertEqual(entities[1]['foo'], u'Foo')
qpb1 = _pb_from_query(query)
qpb1.limit.value = 2
qpb2 = _pb_from_query(query)
qpb2.start_cursor = self._END
qpb2.limit.value = 1
EXPECTED1 = {
'project': self._PROJECT,
'query_pb': qpb1,
'namespace': self._NAMESPACE,
'transaction_id': None,
}
EXPECTED2 = {
'project': self._PROJECT,
'query_pb': qpb2,
'namespace': self._NAMESPACE,
'transaction_id': None,
}
self.assertEqual(len(connection._called_with), 2)
self.assertEqual(connection._called_with[0], EXPECTED1)
self.assertEqual(connection._called_with[1], EXPECTED2)


class Test__pb_from_query(unittest2.TestCase):

Expand Down

0 comments on commit 2a72031

Please sign in to comment.