@@ -231,6 +231,77 @@ def test_lookup_single_key_empty_response(self):
231
231
self .assertEqual (len (keys ), 1 )
232
232
self .assertEqual (keys [0 ], key_pb )
233
233
234
+ def test_lookup_single_key_empty_response_w_eventual (self ):
235
+ from gcloud .datastore .connection import datastore_pb
236
+ from gcloud .datastore .key import Key
237
+
238
+ DATASET_ID = 'DATASET'
239
+ key_pb = Key (path = [{'kind' : 'Kind' , 'id' : 1234 }]).to_protobuf ()
240
+ rsp_pb = datastore_pb .LookupResponse ()
241
+ conn = self ._makeOne ()
242
+ URI = '/' .join ([
243
+ conn .API_BASE_URL ,
244
+ 'datastore' ,
245
+ conn .API_VERSION ,
246
+ 'datasets' ,
247
+ DATASET_ID ,
248
+ 'lookup' ,
249
+ ])
250
+ http = conn ._http = Http ({'status' : '200' }, rsp_pb .SerializeToString ())
251
+ self .assertEqual (conn .lookup (DATASET_ID , key_pb , eventual = True ), None )
252
+ cw = http ._called_with
253
+ self ._verifyProtobufCall (cw , URI , conn )
254
+ rq_class = datastore_pb .LookupRequest
255
+ request = rq_class ()
256
+ request .ParseFromString (cw ['body' ])
257
+ keys = list (request .key )
258
+ self .assertEqual (len (keys ), 1 )
259
+ self .assertEqual (keys [0 ], key_pb )
260
+ self .assertEqual (request .read_options .read_consistency ,
261
+ datastore_pb .ReadOptions .EVENTUAL )
262
+ self .assertEqual (request .read_options .transaction , '' )
263
+
264
+ def test_lookup_single_key_empty_response_w_eventual_and_transaction (self ):
265
+ from gcloud .datastore .key import Key
266
+
267
+ DATASET_ID = 'DATASET'
268
+ TRANSACTION = 'TRANSACTION'
269
+ key_pb = Key (path = [{'kind' : 'Kind' , 'id' : 1234 }]).to_protobuf ()
270
+ conn = self ._makeOne ()
271
+ conn .transaction (TRANSACTION )
272
+ self .assertRaises (
273
+ ValueError , conn .lookup , DATASET_ID , key_pb , eventual = True )
274
+
275
+ def test_lookup_single_key_empty_response_w_transaction (self ):
276
+ from gcloud .datastore .connection import datastore_pb
277
+ from gcloud .datastore .key import Key
278
+
279
+ DATASET_ID = 'DATASET'
280
+ TRANSACTION = 'TRANSACTION'
281
+ key_pb = Key (path = [{'kind' : 'Kind' , 'id' : 1234 }]).to_protobuf ()
282
+ rsp_pb = datastore_pb .LookupResponse ()
283
+ conn = self ._makeOne ()
284
+ conn .transaction (TRANSACTION )
285
+ URI = '/' .join ([
286
+ conn .API_BASE_URL ,
287
+ 'datastore' ,
288
+ conn .API_VERSION ,
289
+ 'datasets' ,
290
+ DATASET_ID ,
291
+ 'lookup' ,
292
+ ])
293
+ http = conn ._http = Http ({'status' : '200' }, rsp_pb .SerializeToString ())
294
+ self .assertEqual (conn .lookup (DATASET_ID , key_pb ), None )
295
+ cw = http ._called_with
296
+ self ._verifyProtobufCall (cw , URI , conn )
297
+ rq_class = datastore_pb .LookupRequest
298
+ request = rq_class ()
299
+ request .ParseFromString (cw ['body' ])
300
+ keys = list (request .key )
301
+ self .assertEqual (len (keys ), 1 )
302
+ self .assertEqual (keys [0 ], key_pb )
303
+ self .assertEqual (request .read_options .transaction , TRANSACTION )
304
+
234
305
def test_lookup_single_key_nonempty_response (self ):
235
306
from gcloud .datastore .connection import datastore_pb
236
307
from gcloud .datastore .key import Key
@@ -443,6 +514,106 @@ def test_lookup_multiple_keys_w_deferred_from_backend_but_not_passed(self):
443
514
self .assertEqual (len (keys ), 1 )
444
515
self .assertEqual (keys [0 ], key_pb2 )
445
516
517
+ def test_run_query_w_eventual_no_transaction (self ):
518
+ from gcloud .datastore .connection import datastore_pb
519
+ from gcloud .datastore .query import Query
520
+
521
+ DATASET_ID = 'DATASET'
522
+ KIND = 'Nonesuch'
523
+ CURSOR = b'\x00 '
524
+ q_pb = Query (KIND , DATASET_ID ).to_protobuf ()
525
+ rsp_pb = datastore_pb .RunQueryResponse ()
526
+ rsp_pb .batch .end_cursor = CURSOR
527
+ no_more = datastore_pb .QueryResultBatch .NO_MORE_RESULTS
528
+ rsp_pb .batch .more_results = no_more
529
+ rsp_pb .batch .entity_result_type = datastore_pb .EntityResult .FULL
530
+ conn = self ._makeOne ()
531
+ URI = '/' .join ([
532
+ conn .API_BASE_URL ,
533
+ 'datastore' ,
534
+ conn .API_VERSION ,
535
+ 'datasets' ,
536
+ DATASET_ID ,
537
+ 'runQuery' ,
538
+ ])
539
+ http = conn ._http = Http ({'status' : '200' }, rsp_pb .SerializeToString ())
540
+ pbs , end , more , skipped = conn .run_query (DATASET_ID , q_pb ,
541
+ eventual = True )
542
+ self .assertEqual (pbs , [])
543
+ self .assertEqual (end , CURSOR )
544
+ self .assertTrue (more )
545
+ self .assertEqual (skipped , 0 )
546
+ cw = http ._called_with
547
+ self ._verifyProtobufCall (cw , URI , conn )
548
+ rq_class = datastore_pb .RunQueryRequest
549
+ request = rq_class ()
550
+ request .ParseFromString (cw ['body' ])
551
+ self .assertEqual (request .partition_id .namespace , '' )
552
+ self .assertEqual (request .query , q_pb )
553
+ self .assertEqual (request .read_options .read_consistency ,
554
+ datastore_pb .ReadOptions .EVENTUAL )
555
+ self .assertEqual (request .read_options .transaction , '' )
556
+
557
+ def test_run_query_wo_eventual_w_transaction (self ):
558
+ from gcloud .datastore .connection import datastore_pb
559
+ from gcloud .datastore .query import Query
560
+
561
+ DATASET_ID = 'DATASET'
562
+ KIND = 'Nonesuch'
563
+ CURSOR = b'\x00 '
564
+ TRANSACTION = 'TRANSACTION'
565
+ q_pb = Query (KIND , DATASET_ID ).to_protobuf ()
566
+ rsp_pb = datastore_pb .RunQueryResponse ()
567
+ rsp_pb .batch .end_cursor = CURSOR
568
+ no_more = datastore_pb .QueryResultBatch .NO_MORE_RESULTS
569
+ rsp_pb .batch .more_results = no_more
570
+ rsp_pb .batch .entity_result_type = datastore_pb .EntityResult .FULL
571
+ conn = self ._makeOne ()
572
+ conn .transaction (TRANSACTION )
573
+ URI = '/' .join ([
574
+ conn .API_BASE_URL ,
575
+ 'datastore' ,
576
+ conn .API_VERSION ,
577
+ 'datasets' ,
578
+ DATASET_ID ,
579
+ 'runQuery' ,
580
+ ])
581
+ http = conn ._http = Http ({'status' : '200' }, rsp_pb .SerializeToString ())
582
+ pbs , end , more , skipped = conn .run_query (DATASET_ID , q_pb )
583
+ self .assertEqual (pbs , [])
584
+ self .assertEqual (end , CURSOR )
585
+ self .assertTrue (more )
586
+ self .assertEqual (skipped , 0 )
587
+ cw = http ._called_with
588
+ self ._verifyProtobufCall (cw , URI , conn )
589
+ rq_class = datastore_pb .RunQueryRequest
590
+ request = rq_class ()
591
+ request .ParseFromString (cw ['body' ])
592
+ self .assertEqual (request .partition_id .namespace , '' )
593
+ self .assertEqual (request .query , q_pb )
594
+ self .assertEqual (request .read_options .read_consistency ,
595
+ datastore_pb .ReadOptions .DEFAULT )
596
+ self .assertEqual (request .read_options .transaction , TRANSACTION )
597
+
598
+ def test_run_query_w_eventual_and_transaction (self ):
599
+ from gcloud .datastore .connection import datastore_pb
600
+ from gcloud .datastore .query import Query
601
+
602
+ DATASET_ID = 'DATASET'
603
+ KIND = 'Nonesuch'
604
+ CURSOR = b'\x00 '
605
+ TRANSACTION = 'TRANSACTION'
606
+ q_pb = Query (KIND , DATASET_ID ).to_protobuf ()
607
+ rsp_pb = datastore_pb .RunQueryResponse ()
608
+ rsp_pb .batch .end_cursor = CURSOR
609
+ no_more = datastore_pb .QueryResultBatch .NO_MORE_RESULTS
610
+ rsp_pb .batch .more_results = no_more
611
+ rsp_pb .batch .entity_result_type = datastore_pb .EntityResult .FULL
612
+ conn = self ._makeOne ()
613
+ conn .transaction (TRANSACTION )
614
+ self .assertRaises (
615
+ ValueError , conn .run_query , DATASET_ID , q_pb , eventual = True )
616
+
446
617
def test_run_query_wo_namespace_empty_result (self ):
447
618
from gcloud .datastore .connection import datastore_pb
448
619
from gcloud .datastore .query import Query
0 commit comments