@@ -161,13 +161,6 @@ def _get_target_class():
161161
162162 return Connection
163163
164- def _make_query_pb (self , kind ):
165- from google .cloud .proto .datastore .v1 import query_pb2
166-
167- pb = query_pb2 .Query ()
168- pb .kind .add ().name = kind
169- return pb
170-
171164 def _make_one (self , client , use_grpc = False ):
172165 with mock .patch ('google.cloud.datastore._http._USE_GRPC' ,
173166 new = use_grpc ):
@@ -279,14 +272,14 @@ def test_lookup_single_key_empty_response_w_eventual(self):
279272 self .assertEqual (request .read_options .transaction , b'' )
280273
281274 def test_lookup_single_key_empty_response_w_eventual_and_transaction (self ):
282- PROJECT = 'PROJECT'
283- TRANSACTION = b'TRANSACTION'
284- key_pb = _make_key_pb (PROJECT )
275+ project = 'PROJECT'
276+ transaction = b'TRANSACTION'
277+ key_pb = _make_key_pb (project )
285278
286279 client = mock .Mock (spec = ['_base_url' ])
287280 conn = self ._make_one (client )
288- self .assertRaises (ValueError , conn .lookup , PROJECT , key_pb ,
289- eventual = True , transaction_id = TRANSACTION )
281+ self .assertRaises (ValueError , conn .lookup , project , [ key_pb ] ,
282+ eventual = True , transaction_id = transaction )
290283
291284 def test_lookup_single_key_empty_response_w_transaction (self ):
292285 from google .cloud .proto .datastore .v1 import datastore_pb2
@@ -472,133 +465,152 @@ def test_lookup_multiple_keys_w_deferred(self):
472465 self .assertEqual (key_pb1 , keys [0 ])
473466 self .assertEqual (key_pb2 , keys [1 ])
474467
468+
469+ class TestHTTPDatastoreAPI (unittest .TestCase ):
470+
471+ @staticmethod
472+ def _get_target_class ():
473+ from google .cloud .datastore ._http import HTTPDatastoreAPI
474+
475+ return HTTPDatastoreAPI
476+
477+ def _make_one (self , * args , ** kwargs ):
478+ return self ._get_target_class ()(* args , ** kwargs )
479+
480+ @staticmethod
481+ def _make_query_pb (kind ):
482+ from google .cloud .proto .datastore .v1 import query_pb2
483+
484+ return query_pb2 .Query (
485+ kind = [query_pb2 .KindExpression (name = kind )],
486+ )
487+
488+ def test_constructor (self ):
489+ client = object ()
490+ ds_api = self ._make_one (client )
491+ self .assertIs (ds_api .client , client )
492+
475493 def test_run_query_w_eventual_no_transaction (self ):
476494 from google .cloud .proto .datastore .v1 import datastore_pb2
495+ from google .cloud .proto .datastore .v1 import entity_pb2
477496 from google .cloud .proto .datastore .v1 import query_pb2
478497
479498 project = 'PROJECT'
480499 kind = 'Nonesuch'
481500 cursor = b'\x00 '
482- q_pb = self ._make_query_pb (kind )
483- rsp_pb = datastore_pb2 .RunQueryResponse ()
484- rsp_pb .batch .end_cursor = cursor
485- no_more = query_pb2 .QueryResultBatch .NO_MORE_RESULTS
486- rsp_pb .batch .more_results = no_more
487- rsp_pb .batch .entity_result_type = query_pb2 .EntityResult .FULL
501+ query_pb = self ._make_query_pb (kind )
502+ partition_id = entity_pb2 .PartitionId (project_id = project )
503+ read_options = datastore_pb2 .ReadOptions (
504+ read_consistency = datastore_pb2 .ReadOptions .EVENTUAL )
505+ rsp_pb = datastore_pb2 .RunQueryResponse (
506+ batch = query_pb2 .QueryResultBatch (
507+ entity_result_type = query_pb2 .EntityResult .FULL ,
508+ end_cursor = cursor ,
509+ more_results = query_pb2 .QueryResultBatch .NO_MORE_RESULTS ,
510+ )
511+ )
488512
489513 # Create mock HTTP and client with response.
490514 http = Http ({'status' : '200' }, rsp_pb .SerializeToString ())
491515 client = mock .Mock (
492516 _http = http , _base_url = 'test.invalid' , spec = ['_http' , '_base_url' ])
493517
494518 # Make request.
495- conn = self ._make_one (client )
496- response = conn .run_query (project , q_pb , eventual = True )
519+ ds_api = self ._make_one (client )
520+ response = ds_api .run_query (
521+ project , partition_id , read_options , query = query_pb )
497522
498523 # Check the result and verify the callers.
499524 self .assertEqual (response , rsp_pb )
500- uri = _build_expected_url (conn . api_base_url , project , 'runQuery' )
525+ uri = _build_expected_url (client . _base_url , project , 'runQuery' )
501526 cw = http ._called_with
502527 _verify_protobuf_call (self , cw , uri )
503528 request = datastore_pb2 .RunQueryRequest ()
504529 request .ParseFromString (cw ['body' ])
505- self .assertEqual (request .partition_id .namespace_id , '' )
506- self .assertEqual (request .query , q_pb )
507- self .assertEqual (request .read_options .read_consistency ,
508- datastore_pb2 .ReadOptions .EVENTUAL )
509- self .assertEqual (request .read_options .transaction , b'' )
530+ self .assertEqual (request .partition_id , partition_id )
531+ self .assertEqual (request .query , query_pb )
532+ self .assertEqual (request .read_options , read_options )
510533
511534 def test_run_query_wo_eventual_w_transaction (self ):
512535 from google .cloud .proto .datastore .v1 import datastore_pb2
536+ from google .cloud .proto .datastore .v1 import entity_pb2
513537 from google .cloud .proto .datastore .v1 import query_pb2
514538
515539 project = 'PROJECT'
516540 kind = 'Nonesuch'
517541 cursor = b'\x00 '
518542 transaction = b'TRANSACTION'
519- q_pb = self ._make_query_pb (kind )
520- rsp_pb = datastore_pb2 .RunQueryResponse ()
521- rsp_pb .batch .end_cursor = cursor
522- no_more = query_pb2 .QueryResultBatch .NO_MORE_RESULTS
523- rsp_pb .batch .more_results = no_more
524- rsp_pb .batch .entity_result_type = query_pb2 .EntityResult .FULL
543+ query_pb = self ._make_query_pb (kind )
544+ partition_id = entity_pb2 .PartitionId (project_id = project )
545+ read_options = datastore_pb2 .ReadOptions (transaction = transaction )
546+ rsp_pb = datastore_pb2 .RunQueryResponse (
547+ batch = query_pb2 .QueryResultBatch (
548+ entity_result_type = query_pb2 .EntityResult .FULL ,
549+ end_cursor = cursor ,
550+ more_results = query_pb2 .QueryResultBatch .NO_MORE_RESULTS ,
551+ )
552+ )
525553
526554 # Create mock HTTP and client with response.
527555 http = Http ({'status' : '200' }, rsp_pb .SerializeToString ())
528556 client = mock .Mock (
529557 _http = http , _base_url = 'test.invalid' , spec = ['_http' , '_base_url' ])
530558
531559 # Make request.
532- conn = self ._make_one (client )
533- response = conn .run_query (
534- project , q_pb , transaction_id = transaction )
560+ ds_api = self ._make_one (client )
561+ response = ds_api .run_query (
562+ project , partition_id , read_options , query = query_pb )
535563
536564 # Check the result and verify the callers.
537565 self .assertEqual (response , rsp_pb )
538- uri = _build_expected_url (conn . api_base_url , project , 'runQuery' )
566+ uri = _build_expected_url (client . _base_url , project , 'runQuery' )
539567 cw = http ._called_with
540568 _verify_protobuf_call (self , cw , uri )
541569 request = datastore_pb2 .RunQueryRequest ()
542570 request .ParseFromString (cw ['body' ])
543- self .assertEqual (request .partition_id .namespace_id , '' )
544- self .assertEqual (request .query , q_pb )
545- self .assertEqual (
546- request .read_options .read_consistency ,
547- datastore_pb2 .ReadOptions .READ_CONSISTENCY_UNSPECIFIED )
548- self .assertEqual (request .read_options .transaction , transaction )
549-
550- def test_run_query_w_eventual_and_transaction (self ):
551- from google .cloud .proto .datastore .v1 import datastore_pb2
552- from google .cloud .proto .datastore .v1 import query_pb2
553-
554- PROJECT = 'PROJECT'
555- KIND = 'Nonesuch'
556- CURSOR = b'\x00 '
557- TRANSACTION = b'TRANSACTION'
558- q_pb = self ._make_query_pb (KIND )
559- rsp_pb = datastore_pb2 .RunQueryResponse ()
560- rsp_pb .batch .end_cursor = CURSOR
561- no_more = query_pb2 .QueryResultBatch .NO_MORE_RESULTS
562- rsp_pb .batch .more_results = no_more
563- rsp_pb .batch .entity_result_type = query_pb2 .EntityResult .FULL
564-
565- client = mock .Mock (spec = ['_base_url' ])
566- conn = self ._make_one (client )
567- self .assertRaises (ValueError , conn .run_query , PROJECT , q_pb ,
568- eventual = True , transaction_id = TRANSACTION )
571+ self .assertEqual (request .partition_id , partition_id )
572+ self .assertEqual (request .query , query_pb )
573+ self .assertEqual (request .read_options , read_options )
569574
570575 def test_run_query_wo_namespace_empty_result (self ):
571576 from google .cloud .proto .datastore .v1 import datastore_pb2
577+ from google .cloud .proto .datastore .v1 import entity_pb2
572578 from google .cloud .proto .datastore .v1 import query_pb2
573579
574580 project = 'PROJECT'
575581 kind = 'Nonesuch'
576582 cursor = b'\x00 '
577- q_pb = self ._make_query_pb (kind )
578- rsp_pb = datastore_pb2 .RunQueryResponse ()
579- rsp_pb .batch .end_cursor = cursor
580- no_more = query_pb2 .QueryResultBatch .NO_MORE_RESULTS
581- rsp_pb .batch .more_results = no_more
582- rsp_pb .batch .entity_result_type = query_pb2 .EntityResult .FULL
583+ query_pb = self ._make_query_pb (kind )
584+ partition_id = entity_pb2 .PartitionId (project_id = project )
585+ read_options = datastore_pb2 .ReadOptions ()
586+ rsp_pb = datastore_pb2 .RunQueryResponse (
587+ batch = query_pb2 .QueryResultBatch (
588+ entity_result_type = query_pb2 .EntityResult .FULL ,
589+ end_cursor = cursor ,
590+ more_results = query_pb2 .QueryResultBatch .NO_MORE_RESULTS ,
591+ )
592+ )
583593
584594 # Create mock HTTP and client with response.
585595 http = Http ({'status' : '200' }, rsp_pb .SerializeToString ())
586596 client = mock .Mock (
587597 _http = http , _base_url = 'test.invalid' , spec = ['_http' , '_base_url' ])
588598
589599 # Make request.
590- conn = self ._make_one (client )
591- response = conn .run_query (project , q_pb )
600+ ds_api = self ._make_one (client )
601+ response = ds_api .run_query (
602+ project , partition_id , read_options , query = query_pb )
592603
593604 # Check the result and verify the callers.
594605 self .assertEqual (response , rsp_pb )
595- uri = _build_expected_url (conn . api_base_url , project , 'runQuery' )
606+ uri = _build_expected_url (client . _base_url , project , 'runQuery' )
596607 cw = http ._called_with
597608 _verify_protobuf_call (self , cw , uri )
598609 request = datastore_pb2 .RunQueryRequest ()
599610 request .ParseFromString (cw ['body' ])
600- self .assertEqual (request .partition_id .namespace_id , '' )
601- self .assertEqual (request .query , q_pb )
611+ self .assertEqual (request .partition_id , partition_id )
612+ self .assertEqual (request .query , query_pb )
613+ self .assertEqual (request .read_options , read_options )
602614
603615 def test_run_query_w_namespace_nonempty_result (self ):
604616 from google .cloud .proto .datastore .v1 import datastore_pb2
@@ -607,49 +619,40 @@ def test_run_query_w_namespace_nonempty_result(self):
607619
608620 project = 'PROJECT'
609621 kind = 'Kind'
610- entity = entity_pb2 .Entity ()
611- q_pb = self ._make_query_pb (kind )
612- rsp_pb = datastore_pb2 .RunQueryResponse ()
613- rsp_pb .batch .entity_results .add (entity = entity )
614- rsp_pb .batch .entity_result_type = query_pb2 .EntityResult .FULL
615- rsp_pb .batch .more_results = query_pb2 .QueryResultBatch .NO_MORE_RESULTS
622+ namespace = 'NS'
623+ query_pb = self ._make_query_pb (kind )
624+ partition_id = entity_pb2 .PartitionId (
625+ project_id = project , namespace_id = namespace )
626+ read_options = datastore_pb2 .ReadOptions ()
627+ rsp_pb = datastore_pb2 .RunQueryResponse (
628+ batch = query_pb2 .QueryResultBatch (
629+ entity_result_type = query_pb2 .EntityResult .FULL ,
630+ entity_results = [
631+ query_pb2 .EntityResult (entity = entity_pb2 .Entity ()),
632+ ],
633+ more_results = query_pb2 .QueryResultBatch .NO_MORE_RESULTS ,
634+ )
635+ )
616636
617637 # Create mock HTTP and client with response.
618638 http = Http ({'status' : '200' }, rsp_pb .SerializeToString ())
619639 client = mock .Mock (
620640 _http = http , _base_url = 'test.invalid' , spec = ['_http' , '_base_url' ])
621641
622642 # Make request.
623- conn = self ._make_one (client )
624- namespace = 'NS'
625- response = conn . run_query ( project , q_pb , namespace = namespace )
643+ ds_api = self ._make_one (client )
644+ response = ds_api . run_query (
645+ project , partition_id , read_options , query = query_pb )
626646
627647 # Check the result and verify the callers.
628648 self .assertEqual (response , rsp_pb )
629649 cw = http ._called_with
630- uri = _build_expected_url (conn . api_base_url , project , 'runQuery' )
650+ uri = _build_expected_url (client . _base_url , project , 'runQuery' )
631651 _verify_protobuf_call (self , cw , uri )
632652 request = datastore_pb2 .RunQueryRequest ()
633653 request .ParseFromString (cw ['body' ])
634- self .assertEqual (request .partition_id .namespace_id , namespace )
635- self .assertEqual (request .query , q_pb )
636-
637-
638- class TestHTTPDatastoreAPI (unittest .TestCase ):
639-
640- @staticmethod
641- def _get_target_class ():
642- from google .cloud .datastore ._http import HTTPDatastoreAPI
643-
644- return HTTPDatastoreAPI
645-
646- def _make_one (self , * args , ** kwargs ):
647- return self ._get_target_class ()(* args , ** kwargs )
648-
649- def test_constructor (self ):
650- client = object ()
651- ds_api = self ._make_one (client )
652- self .assertIs (ds_api .client , client )
654+ self .assertEqual (request .partition_id , partition_id )
655+ self .assertEqual (request .query , query_pb )
653656
654657 def test_begin_transaction (self ):
655658 from google .cloud .proto .datastore .v1 import datastore_pb2
0 commit comments