@@ -202,6 +202,7 @@ def test_fetch_default_limit(self):
202202
203203 def test_fetch_explicit_limit (self ):
204204 from gcloud .datastore .datastore_v1_pb2 import Entity
205+ _CURSOR = 'CURSOR'
205206 _DATASET = 'DATASET'
206207 _KIND = 'KIND'
207208 _ID = 123
@@ -213,10 +214,12 @@ def test_fetch_explicit_limit(self):
213214 prop .name = 'foo'
214215 prop .value .string_value = 'Foo'
215216 connection = _Connection (entity_pb )
217+ connection ._cursor = _CURSOR
216218 dataset = _Dataset (_DATASET , connection )
217219 query = self ._makeOne (_KIND , dataset )
218220 limited = query .limit (13 )
219221 entities = query .fetch (13 )
222+ self .assertEqual (query ._cursor , _CURSOR )
220223 self .assertEqual (len (entities ), 1 )
221224 self .assertEqual (entities [0 ].key ().path (),
222225 [{'kind' : _KIND , 'id' : _ID }])
@@ -225,6 +228,80 @@ def test_fetch_explicit_limit(self):
225228 'query_pb' : limited .to_protobuf (),
226229 })
227230
231+ def test_cursor_not_fetched (self ):
232+ _DATASET = 'DATASET'
233+ _KIND = 'KIND'
234+ connection = _Connection ()
235+ dataset = _Dataset (_DATASET , connection )
236+ query = self ._makeOne (_KIND , dataset )
237+ self .assertRaises (RuntimeError , query .cursor )
238+
239+ def test_cursor_fetched (self ):
240+ import base64
241+ _CURSOR = 'CURSOR'
242+ _DATASET = 'DATASET'
243+ _KIND = 'KIND'
244+ connection = _Connection ()
245+ dataset = _Dataset (_DATASET , connection )
246+ query = self ._makeOne (_KIND , dataset )
247+ query ._cursor = _CURSOR
248+ self .assertEqual (query .cursor (), base64 .b64encode (_CURSOR ))
249+
250+ def test_with_cursor_neither (self ):
251+ _DATASET = 'DATASET'
252+ _KIND = 'KIND'
253+ connection = _Connection ()
254+ dataset = _Dataset (_DATASET , connection )
255+ query = self ._makeOne (_KIND , dataset )
256+ self .assertTrue (query .with_cursor (None ) is query )
257+
258+ def test_with_cursor_w_start (self ):
259+ import base64
260+ _CURSOR = 'CURSOR'
261+ _CURSOR_B64 = base64 .b64encode (_CURSOR )
262+ _DATASET = 'DATASET'
263+ _KIND = 'KIND'
264+ connection = _Connection ()
265+ dataset = _Dataset (_DATASET , connection )
266+ query = self ._makeOne (_KIND , dataset )
267+ after = query .with_cursor (_CURSOR_B64 )
268+ self .assertFalse (after is query )
269+ q_pb = after .to_protobuf ()
270+ self .assertEqual (q_pb .start_cursor , _CURSOR )
271+ self .assertEqual (q_pb .end_cursor , '' )
272+
273+ def test_with_cursor_w_end (self ):
274+ import base64
275+ _CURSOR = 'CURSOR'
276+ _CURSOR_B64 = base64 .b64encode (_CURSOR )
277+ _DATASET = 'DATASET'
278+ _KIND = 'KIND'
279+ connection = _Connection ()
280+ dataset = _Dataset (_DATASET , connection )
281+ query = self ._makeOne (_KIND , dataset )
282+ after = query .with_cursor (None , _CURSOR_B64 )
283+ self .assertFalse (after is query )
284+ q_pb = after .to_protobuf ()
285+ self .assertEqual (q_pb .start_cursor , '' )
286+ self .assertEqual (q_pb .end_cursor , _CURSOR )
287+
288+ def test_with_cursor_w_both (self ):
289+ import base64
290+ _START = 'START'
291+ _START_B64 = base64 .b64encode (_START )
292+ _END = 'CURSOR'
293+ _END_B64 = base64 .b64encode (_END )
294+ _DATASET = 'DATASET'
295+ _KIND = 'KIND'
296+ connection = _Connection ()
297+ dataset = _Dataset (_DATASET , connection )
298+ query = self ._makeOne (_KIND , dataset )
299+ after = query .with_cursor (_START_B64 , _END_B64 )
300+ self .assertFalse (after is query )
301+ q_pb = after .to_protobuf ()
302+ self .assertEqual (q_pb .start_cursor , _START )
303+ self .assertEqual (q_pb .end_cursor , _END )
304+
228305 def test_order_empty (self ):
229306 _KIND = 'KIND'
230307 before = self ._makeOne (_KIND )
@@ -285,10 +362,13 @@ def connection(self):
285362
286363class _Connection (object ):
287364 _called_with = None
365+ _cursor = ''
366+ _more = True
367+ _skipped = 0
288368
289369 def __init__ (self , * result ):
290370 self ._result = list (result )
291371
292372 def run_query (self , ** kw ):
293373 self ._called_with = kw
294- return self ._result
374+ return self ._result , self . _cursor , self . _more , self . _skipped
0 commit comments