def test_run_query_w_namespace_nonempty_result(self): from google.cloud.grpc.datastore.v1 import datastore_pb2 from google.cloud.grpc.datastore.v1 import entity_pb2 PROJECT = 'PROJECT' KIND = 'Kind' entity = entity_pb2.Entity() q_pb = self._make_query_pb(KIND) rsp_pb = datastore_pb2.RunQueryResponse() rsp_pb.batch.entity_results.add(entity=entity) rsp_pb.batch.entity_result_type = 1 # FULL rsp_pb.batch.more_results = 3 # NO_MORE_RESULTS conn = self._make_one() URI = '/'.join([ conn.api_base_url, conn.API_VERSION, 'projects', PROJECT + ':runQuery', ]) http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString()) pbs = conn.run_query(PROJECT, q_pb, 'NS')[0] self.assertEqual(len(pbs), 1) cw = http._called_with self._verifyProtobufCall(cw, URI, conn) rq_class = datastore_pb2.RunQueryRequest request = rq_class() request.ParseFromString(cw['body']) self.assertEqual(request.partition_id.namespace_id, 'NS') self.assertEqual(request.query, q_pb)
def test_run_query_wo_namespace_empty_result(self): from google.cloud.grpc.datastore.v1 import datastore_pb2 from google.cloud.grpc.datastore.v1 import query_pb2 PROJECT = 'PROJECT' KIND = 'Nonesuch' CURSOR = b'\x00' q_pb = self._make_query_pb(KIND) rsp_pb = datastore_pb2.RunQueryResponse() rsp_pb.batch.end_cursor = CURSOR no_more = query_pb2.QueryResultBatch.NO_MORE_RESULTS rsp_pb.batch.more_results = no_more rsp_pb.batch.entity_result_type = query_pb2.EntityResult.FULL conn = self._make_one() URI = '/'.join([ conn.api_base_url, conn.API_VERSION, 'projects', PROJECT + ':runQuery', ]) http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString()) pbs, end, more, skipped = conn.run_query(PROJECT, q_pb) self.assertEqual(pbs, []) self.assertEqual(end, CURSOR) self.assertTrue(more) self.assertEqual(skipped, 0) cw = http._called_with self._verifyProtobufCall(cw, URI, conn) rq_class = datastore_pb2.RunQueryRequest request = rq_class() request.ParseFromString(cw['body']) self.assertEqual(request.partition_id.namespace_id, '') self.assertEqual(request.query, q_pb)
def test_run_query_w_eventual_and_transaction(self): from google.cloud.grpc.datastore.v1 import datastore_pb2 from google.cloud.grpc.datastore.v1 import query_pb2 PROJECT = 'PROJECT' KIND = 'Nonesuch' CURSOR = b'\x00' TRANSACTION = b'TRANSACTION' q_pb = self._make_query_pb(KIND) rsp_pb = datastore_pb2.RunQueryResponse() rsp_pb.batch.end_cursor = CURSOR no_more = query_pb2.QueryResultBatch.NO_MORE_RESULTS rsp_pb.batch.more_results = no_more rsp_pb.batch.entity_result_type = query_pb2.EntityResult.FULL conn = self._make_one() self.assertRaises(ValueError, conn.run_query, PROJECT, q_pb, eventual=True, transaction_id=TRANSACTION)
def test_run_query_wo_eventual_w_transaction(self): from google.cloud.grpc.datastore.v1 import datastore_pb2 from google.cloud.grpc.datastore.v1 import query_pb2 PROJECT = 'PROJECT' KIND = 'Nonesuch' CURSOR = b'\x00' TRANSACTION = b'TRANSACTION' q_pb = self._make_query_pb(KIND) rsp_pb = datastore_pb2.RunQueryResponse() rsp_pb.batch.end_cursor = CURSOR no_more = query_pb2.QueryResultBatch.NO_MORE_RESULTS rsp_pb.batch.more_results = no_more rsp_pb.batch.entity_result_type = query_pb2.EntityResult.FULL http = Http({'status': '200'}, rsp_pb.SerializeToString()) client = mock.Mock(_http=http, spec=['_http']) conn = self._make_one(client) URI = '/'.join([ conn.api_base_url, conn.API_VERSION, 'projects', PROJECT + ':runQuery', ]) pbs, end, more, skipped = conn.run_query(PROJECT, q_pb, transaction_id=TRANSACTION) self.assertEqual(pbs, []) self.assertEqual(end, CURSOR) self.assertTrue(more) self.assertEqual(skipped, 0) cw = http._called_with self._verifyProtobufCall(cw, URI, conn) rq_class = datastore_pb2.RunQueryRequest request = rq_class() request.ParseFromString(cw['body']) self.assertEqual(request.partition_id.namespace_id, '') self.assertEqual(request.query, q_pb) self.assertEqual( request.read_options.read_consistency, datastore_pb2.ReadOptions.READ_CONSISTENCY_UNSPECIFIED) self.assertEqual(request.read_options.transaction, TRANSACTION)