@@ -306,6 +306,145 @@ def test_lookup_multiple_keys_empty_response(self):
306306 self .assertEqual (keys [0 ], key_pb1 )
307307 self .assertEqual (keys [1 ], key_pb2 )
308308
309+ def test_lookup_multiple_keys_w_missing (self ):
310+ from gcloud .datastore .connection import datastore_pb
311+ from gcloud .datastore .key import Key
312+
313+ DATASET_ID = 'DATASET'
314+ key_pb1 = Key (path = [{'kind' : 'Kind' , 'id' : 1234 }]).to_protobuf ()
315+ key_pb2 = Key (path = [{'kind' : 'Kind' , 'id' : 2345 }]).to_protobuf ()
316+ rsp_pb = datastore_pb .LookupResponse ()
317+ er_1 = rsp_pb .missing .add ()
318+ er_1 .entity .key .CopyFrom (key_pb1 )
319+ er_2 = rsp_pb .missing .add ()
320+ er_2 .entity .key .CopyFrom (key_pb2 )
321+ conn = self ._makeOne ()
322+ URI = '/' .join ([
323+ conn .API_BASE_URL ,
324+ 'datastore' ,
325+ conn .API_VERSION ,
326+ 'datasets' ,
327+ DATASET_ID ,
328+ 'lookup' ,
329+ ])
330+ http = conn ._http = Http ({'status' : '200' }, rsp_pb .SerializeToString ())
331+ missing = []
332+ result = conn .lookup (DATASET_ID , [key_pb1 , key_pb2 ], missing = missing )
333+ self .assertEqual (result , [])
334+ self .assertEqual ([missed .key for missed in missing ],
335+ [key_pb1 , key_pb2 ])
336+ cw = http ._called_with
337+ self .assertEqual (cw ['uri' ], URI )
338+ self .assertEqual (cw ['method' ], 'POST' )
339+ self .assertEqual (cw ['headers' ]['Content-Type' ],
340+ 'application/x-protobuf' )
341+ self .assertEqual (cw ['headers' ]['User-Agent' ], conn .USER_AGENT )
342+ rq_class = datastore_pb .LookupRequest
343+ request = rq_class ()
344+ request .ParseFromString (cw ['body' ])
345+ keys = list (request .key )
346+ self .assertEqual (len (keys ), 2 )
347+ self .assertEqual (keys [0 ], key_pb1 )
348+ self .assertEqual (keys [1 ], key_pb2 )
349+
350+ def test_lookup_multiple_keys_w_deferred (self ):
351+ from gcloud .datastore .connection import datastore_pb
352+ from gcloud .datastore .key import Key
353+
354+ DATASET_ID = 'DATASET'
355+ key_pb1 = Key (path = [{'kind' : 'Kind' , 'id' : 1234 }]).to_protobuf ()
356+ key_pb2 = Key (path = [{'kind' : 'Kind' , 'id' : 2345 }]).to_protobuf ()
357+ rsp_pb = datastore_pb .LookupResponse ()
358+ rsp_pb .deferred .add ().CopyFrom (key_pb1 )
359+ rsp_pb .deferred .add ().CopyFrom (key_pb2 )
360+ conn = self ._makeOne ()
361+ URI = '/' .join ([
362+ conn .API_BASE_URL ,
363+ 'datastore' ,
364+ conn .API_VERSION ,
365+ 'datasets' ,
366+ DATASET_ID ,
367+ 'lookup' ,
368+ ])
369+ http = conn ._http = Http ({'status' : '200' }, rsp_pb .SerializeToString ())
370+ deferred = []
371+ result = conn .lookup (DATASET_ID , [key_pb1 , key_pb2 ], deferred = deferred )
372+ self .assertEqual (result , [])
373+ self .assertEqual ([def_key for def_key in deferred ], [key_pb1 , key_pb2 ])
374+ cw = http ._called_with
375+ self .assertEqual (cw ['uri' ], URI )
376+ self .assertEqual (cw ['method' ], 'POST' )
377+ self .assertEqual (cw ['headers' ]['Content-Type' ],
378+ 'application/x-protobuf' )
379+ self .assertEqual (cw ['headers' ]['User-Agent' ], conn .USER_AGENT )
380+ rq_class = datastore_pb .LookupRequest
381+ request = rq_class ()
382+ request .ParseFromString (cw ['body' ])
383+ keys = list (request .key )
384+ self .assertEqual (len (keys ), 2 )
385+ self .assertEqual (keys [0 ], key_pb1 )
386+ self .assertEqual (keys [1 ], key_pb2 )
387+
388+ def test_lookup_multiple_keys_w_deferred_from_backend_but_not_passed (self ):
389+ from gcloud .datastore .connection import datastore_pb
390+ from gcloud .datastore .key import Key
391+
392+ DATASET_ID = 'DATASET'
393+ key_pb1 = Key (path = [{'kind' : 'Kind' , 'id' : 1234 }]).to_protobuf ()
394+ key_pb2 = Key (path = [{'kind' : 'Kind' , 'id' : 2345 }]).to_protobuf ()
395+ rsp_pb1 = datastore_pb .LookupResponse ()
396+ entity1 = datastore_pb .Entity ()
397+ entity1 .key .CopyFrom (key_pb1 )
398+ rsp_pb1 .found .add (entity = entity1 )
399+ rsp_pb1 .deferred .add ().CopyFrom (key_pb2 )
400+ rsp_pb2 = datastore_pb .LookupResponse ()
401+ entity2 = datastore_pb .Entity ()
402+ entity2 .key .CopyFrom (key_pb2 )
403+ rsp_pb2 .found .add (entity = entity2 )
404+ conn = self ._makeOne ()
405+ URI = '/' .join ([
406+ conn .API_BASE_URL ,
407+ 'datastore' ,
408+ conn .API_VERSION ,
409+ 'datasets' ,
410+ DATASET_ID ,
411+ 'lookup' ,
412+ ])
413+ http = conn ._http = HttpMultiple (
414+ ({'status' : '200' }, rsp_pb1 .SerializeToString ()),
415+ ({'status' : '200' }, rsp_pb2 .SerializeToString ()),
416+ )
417+ found = conn .lookup (DATASET_ID , [key_pb1 , key_pb2 ])
418+ self .assertEqual (len (found ), 2 )
419+ self .assertEqual (found [0 ].key .path_element [0 ].kind , 'Kind' )
420+ self .assertEqual (found [0 ].key .path_element [0 ].id , 1234 )
421+ self .assertEqual (found [1 ].key .path_element [0 ].kind , 'Kind' )
422+ self .assertEqual (found [1 ].key .path_element [0 ].id , 2345 )
423+ cw = http ._called_with
424+ rq_class = datastore_pb .LookupRequest
425+ request = rq_class ()
426+ self .assertEqual (len (cw ), 2 )
427+ self .assertEqual (cw [0 ]['uri' ], URI )
428+ self .assertEqual (cw [0 ]['method' ], 'POST' )
429+ self .assertEqual (cw [0 ]['headers' ]['Content-Type' ],
430+ 'application/x-protobuf' )
431+ self .assertEqual (cw [0 ]['headers' ]['User-Agent' ], conn .USER_AGENT )
432+ request .ParseFromString (cw [0 ]['body' ])
433+ keys = list (request .key )
434+ self .assertEqual (len (keys ), 2 )
435+ self .assertEqual (keys [0 ], key_pb1 )
436+ self .assertEqual (keys [1 ], key_pb2 )
437+
438+ self .assertEqual (cw [1 ]['uri' ], URI )
439+ self .assertEqual (cw [1 ]['method' ], 'POST' )
440+ self .assertEqual (cw [1 ]['headers' ]['Content-Type' ],
441+ 'application/x-protobuf' )
442+ self .assertEqual (cw [1 ]['headers' ]['User-Agent' ], conn .USER_AGENT )
443+ request .ParseFromString (cw [1 ]['body' ])
444+ keys = list (request .key )
445+ self .assertEqual (len (keys ), 1 )
446+ self .assertEqual (keys [0 ], key_pb2 )
447+
309448 def test_run_query_wo_namespace_empty_result (self ):
310449 from gcloud .datastore .connection import datastore_pb
311450 from gcloud .datastore .query import Query
@@ -901,3 +1040,15 @@ def __init__(self, headers, content):
9011040 def request (self , ** kw ):
9021041 self ._called_with = kw
9031042 return self ._headers , self ._content
1043+
1044+
1045+ class HttpMultiple (object ):
1046+
1047+ def __init__ (self , * responses ):
1048+ self ._called_with = []
1049+ self ._responses = list (responses )
1050+
1051+ def request (self , ** kw ):
1052+ self ._called_with .append (kw )
1053+ result , self ._responses = self ._responses [0 ], self ._responses [1 :]
1054+ return result
0 commit comments