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

Avoid copying a limit to query protobuf unless explicitly passed. #492

Merged
merged 1 commit into from
Jan 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions gcloud/datastore/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ class Iterator(object):
datastore_pb.QueryResultBatch.MORE_RESULTS_AFTER_LIMIT,
)

def __init__(self, query, limit=0, offset=0,
def __init__(self, query, limit=None, offset=0,
start_cursor=None, end_cursor=None):
self._query = query
self._limit = limit
Expand All @@ -361,7 +361,9 @@ def next_page(self):
if end_cursor is not None:
pb.end_cursor = base64.b64decode(end_cursor)

pb.limit = self._limit
if self._limit is not None:
pb.limit = self._limit

pb.offset = self._offset

query_results = self._query.dataset.connection().run_query(
Expand Down
39 changes: 33 additions & 6 deletions gcloud/datastore/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def test_ctor_defaults(self):
query = object()
iterator = self._makeOne(query)
self.assertTrue(iterator._query is query)
self.assertEqual(iterator._limit, 0)
self.assertEqual(iterator._limit, None)
self.assertEqual(iterator._offset, 0)

def test_ctor_explicit(self):
Expand Down Expand Up @@ -495,7 +495,34 @@ def test_next_page_no_cursors_no_more(self):
[{'kind': self._KIND, 'id': self._ID}])
self.assertEqual(entities[0]['foo'], u'Foo')
qpb = _pb_from_query(query)
qpb.limit = qpb.offset = 0
qpb.offset = 0
EXPECTED = {
'dataset_id': self._DATASET,
'query_pb': qpb,
'namespace': self._NAMESPACE,
}
self.assertEqual(connection._called_with, [EXPECTED])

def test_next_page_no_cursors_no_more_w_offset_and_limit(self):
from base64 import b64encode
from gcloud.datastore.query import _pb_from_query
self._KIND = 'KIND'
dataset, connection = self._makeDataset()
query = _Query(self._KIND, dataset, self._NAMESPACE)
self._addQueryResults(dataset)
iterator = self._makeOne(query, 13, 29)
entities, more_results, cursor = iterator.next_page()

self.assertEqual(cursor, b64encode(self._END))
self.assertFalse(more_results)
self.assertFalse(iterator._more_results)
self.assertEqual(len(entities), 1)
self.assertEqual(entities[0].key.path,
[{'kind': self._KIND, 'id': self._ID}])
self.assertEqual(entities[0]['foo'], u'Foo')
qpb = _pb_from_query(query)
qpb.limit = 13
qpb.offset = 29
EXPECTED = {
'dataset_id': self._DATASET,
'query_pb': qpb,
Expand Down Expand Up @@ -525,7 +552,7 @@ def test_next_page_w_cursors_w_more(self):
[{'kind': self._KIND, 'id': self._ID}])
self.assertEqual(entities[0]['foo'], u'Foo')
qpb = _pb_from_query(query)
qpb.limit = qpb.offset = 0
qpb.offset = 0
qpb.start_cursor = b64decode(self._START)
qpb.end_cursor = b64decode(self._END)
EXPECTED = {
Expand Down Expand Up @@ -559,7 +586,7 @@ def test___iter___no_more(self):
[{'kind': self._KIND, 'id': self._ID}])
self.assertEqual(entities[0]['foo'], u'Foo')
qpb = _pb_from_query(query)
qpb.limit = qpb.offset = 0
qpb.offset = 0
EXPECTED = {
'dataset_id': self._DATASET,
'query_pb': qpb,
Expand All @@ -584,9 +611,9 @@ 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.limit = qpb1.offset = 0
qpb1.offset = 0
qpb2 = _pb_from_query(query)
qpb2.limit = qpb2.offset = 0
qpb2.offset = 0
qpb2.start_cursor = self._END
EXPECTED1 = {
'dataset_id': self._DATASET,
Expand Down