Skip to content

Commit

Permalink
Set next_page_token to none if there are no more result (#4349)
Browse files Browse the repository at this point in the history
* Update _process_query_results_done Datastore test

Response ``end_cursor`` might be non-empty as reported in #4347.

* Set next_page_token to None using MoreResults enum (#4347)

Depending on empty value of ``response_pb.batch.end_cursor`` is not
reliable as the behaviour is not documented. It's better to check API
response field ``moreResults``, see
https://cloud.google.com/datastore/docs/reference/rpc/google.datastore.v1#google.datastore.v1.QueryResultBatch.
  • Loading branch information
Matej Čamaj authored and tseaver committed Nov 10, 2017
1 parent 2bf694b commit 69a9840
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions datastore/google/cloud/datastore/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@


_NOT_FINISHED = query_pb2.QueryResultBatch.NOT_FINISHED
_NO_MORE_RESULTS = query_pb2.QueryResultBatch.NO_MORE_RESULTS

_FINISHED = (
query_pb2.QueryResultBatch.NO_MORE_RESULTS,
_NO_MORE_RESULTS,
query_pb2.QueryResultBatch.MORE_RESULTS_AFTER_LIMIT,
query_pb2.QueryResultBatch.MORE_RESULTS_AFTER_CURSOR,
)
Expand Down Expand Up @@ -470,7 +471,7 @@ def _process_query_results(self, response_pb):
"""
self._skipped_results = response_pb.batch.skipped_results

if response_pb.batch.end_cursor == b'': # Empty-value for bytes.
if response_pb.batch.more_results == _NO_MORE_RESULTS:
self.next_page_token = None
else:
self.next_page_token = base64.urlsafe_b64encode(
Expand Down
2 changes: 1 addition & 1 deletion datastore/tests/unit/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def test__process_query_results_done(self):
entity_pbs = [
_make_entity('World', 1234, 'PROJECT'),
]
cursor_as_bytes = b''
cursor_as_bytes = b'\x9ai\xe7'
skipped_results = 44
more_results_enum = query_pb2.QueryResultBatch.NO_MORE_RESULTS
response_pb = _make_query_response(
Expand Down

0 comments on commit 69a9840

Please sign in to comment.