def test_lookup_multiple_keys_w_deferred_from_backend_but_not_passed(self): from gcloud.datastore.connection import datastore_pb from gcloud.datastore.key import Key DATASET_ID = 'DATASET' key_pb1 = Key(path=[{'kind': 'Kind', 'id': 1234}]).to_protobuf() key_pb2 = Key(path=[{'kind': 'Kind', 'id': 2345}]).to_protobuf() rsp_pb1 = datastore_pb.LookupResponse() entity1 = datastore_pb.Entity() entity1.key.CopyFrom(key_pb1) rsp_pb1.found.add(entity=entity1) rsp_pb1.deferred.add().CopyFrom(key_pb2) rsp_pb2 = datastore_pb.LookupResponse() entity2 = datastore_pb.Entity() entity2.key.CopyFrom(key_pb2) rsp_pb2.found.add(entity=entity2) conn = self._makeOne() URI = '/'.join([ conn.API_BASE_URL, 'datastore', conn.API_VERSION, 'datasets', DATASET_ID, 'lookup', ]) http = conn._http = HttpMultiple( ({ 'status': '200' }, rsp_pb1.SerializeToString()), ({ 'status': '200' }, rsp_pb2.SerializeToString()), ) found = conn.lookup(DATASET_ID, [key_pb1, key_pb2]) self.assertEqual(len(found), 2) self.assertEqual(found[0].key.path_element[0].kind, 'Kind') self.assertEqual(found[0].key.path_element[0].id, 1234) self.assertEqual(found[1].key.path_element[0].kind, 'Kind') self.assertEqual(found[1].key.path_element[0].id, 2345) cw = http._called_with rq_class = datastore_pb.LookupRequest request = rq_class() self.assertEqual(len(cw), 2) self._verifyProtobufCall(cw[0], URI, conn) request.ParseFromString(cw[0]['body']) keys = list(request.key) self.assertEqual(len(keys), 2) self.assertEqual(keys[0], key_pb1) self.assertEqual(keys[1], key_pb2) self._verifyProtobufCall(cw[1], URI, conn) request.ParseFromString(cw[1]['body']) keys = list(request.key) self.assertEqual(len(keys), 1) self.assertEqual(keys[0], key_pb2)
def test_lookup_multiple_keys_empty_response(self): from gcloud.datastore.connection import datastore_pb from gcloud.datastore.key import Key DATASET_ID = 'DATASET' key_pb1 = Key(path=[{'kind': 'Kind', 'id': 1234}]).to_protobuf() key_pb2 = Key(path=[{'kind': 'Kind', 'id': 2345}]).to_protobuf() rsp_pb = datastore_pb.LookupResponse() conn = self._makeOne() URI = '/'.join([ conn.API_BASE_URL, 'datastore', conn.API_VERSION, 'datasets', DATASET_ID, 'lookup', ]) http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString()) self.assertEqual(conn.lookup(DATASET_ID, [key_pb1, key_pb2]), []) cw = http._called_with self._verifyProtobufCall(cw, URI, conn) rq_class = datastore_pb.LookupRequest request = rq_class() request.ParseFromString(cw['body']) keys = list(request.key) self.assertEqual(len(keys), 2) self.assertEqual(keys[0], key_pb1) self.assertEqual(keys[1], key_pb2)
def test_lookup_multiple_keys_w_missing(self): from gcloud.datastore import _datastore_v1_pb2 as datastore_pb DATASET_ID = 'DATASET' key_pb1 = self._make_key_pb(DATASET_ID) key_pb2 = self._make_key_pb(DATASET_ID, id=2345) rsp_pb = datastore_pb.LookupResponse() er_1 = rsp_pb.missing.add() er_1.entity.key.CopyFrom(key_pb1) er_2 = rsp_pb.missing.add() er_2.entity.key.CopyFrom(key_pb2) conn = self._makeOne() URI = '/'.join([ conn.API_BASE_URL, 'datastore', conn.API_VERSION, 'datasets', DATASET_ID, 'lookup', ]) http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString()) missing = [] result = conn.lookup(DATASET_ID, [key_pb1, key_pb2], missing=missing) self.assertEqual(result, []) self.assertEqual([missed.key for missed in missing], [key_pb1, key_pb2]) cw = http._called_with self._verifyProtobufCall(cw, URI, conn) rq_class = datastore_pb.LookupRequest request = rq_class() request.ParseFromString(cw['body']) keys = list(request.key) self.assertEqual(len(keys), 2) _compare_key_pb_after_request(self, key_pb1, keys[0]) _compare_key_pb_after_request(self, key_pb2, keys[1])
def test_lookup_single_key_nonempty_response(self): from gcloud.datastore.connection import datastore_pb from gcloud.datastore.key import Key DATASET_ID = 'DATASET' key_pb = Key(path=[{'kind': 'Kind', 'id': 1234}]).to_protobuf() rsp_pb = datastore_pb.LookupResponse() entity = datastore_pb.Entity() entity.key.CopyFrom(key_pb) rsp_pb.found.add(entity=entity) conn = self._makeOne() URI = '/'.join([ conn.API_BASE_URL, 'datastore', conn.API_VERSION, 'datasets', DATASET_ID, 'lookup', ]) http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString()) found = conn.lookup(DATASET_ID, key_pb) self.assertEqual(found.key.path_element[0].kind, 'Kind') self.assertEqual(found.key.path_element[0].id, 1234) cw = http._called_with self._verifyProtobufCall(cw, URI, conn) rq_class = datastore_pb.LookupRequest request = rq_class() request.ParseFromString(cw['body']) keys = list(request.key) self.assertEqual(len(keys), 1) self.assertEqual(keys[0], key_pb)
def test_lookup_multiple_keys_empty_response(self): from gcloud.datastore import _datastore_v1_pb2 as datastore_pb DATASET_ID = 'DATASET' key_pb1 = self._make_key_pb(DATASET_ID) key_pb2 = self._make_key_pb(DATASET_ID, id=2345) rsp_pb = datastore_pb.LookupResponse() conn = self._makeOne() URI = '/'.join([ conn.api_base_url, 'datastore', conn.API_VERSION, 'datasets', DATASET_ID, 'lookup', ]) http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString()) found, missing, deferred = conn.lookup(DATASET_ID, [key_pb1, key_pb2]) self.assertEqual(len(found), 0) self.assertEqual(len(missing), 0) self.assertEqual(len(deferred), 0) cw = http._called_with self._verifyProtobufCall(cw, URI, conn) rq_class = datastore_pb.LookupRequest request = rq_class() request.ParseFromString(cw['body']) keys = list(request.key) self.assertEqual(len(keys), 2) _compare_key_pb_after_request(self, key_pb1, keys[0]) _compare_key_pb_after_request(self, key_pb2, keys[1])
def test_lookup_single_key_empty_response_w_transaction(self): from gcloud.datastore import _datastore_v1_pb2 as datastore_pb DATASET_ID = 'DATASET' TRANSACTION = 'TRANSACTION' key_pb = self._make_key_pb(DATASET_ID) rsp_pb = datastore_pb.LookupResponse() conn = self._makeOne() URI = '/'.join([ conn.API_BASE_URL, 'datastore', conn.API_VERSION, 'datasets', DATASET_ID, 'lookup', ]) http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString()) found = conn.lookup(DATASET_ID, key_pb, transaction_id=TRANSACTION) self.assertEqual(found, None) cw = http._called_with self._verifyProtobufCall(cw, URI, conn) rq_class = datastore_pb.LookupRequest request = rq_class() request.ParseFromString(cw['body']) keys = list(request.key) self.assertEqual(len(keys), 1) _compare_key_pb_after_request(self, key_pb, keys[0]) self.assertEqual(request.read_options.transaction, TRANSACTION)
def test_lookup_single_key_nonempty_response(self): from gcloud.datastore import _datastore_v1_pb2 as datastore_pb DATASET_ID = 'DATASET' key_pb = self._make_key_pb(DATASET_ID) rsp_pb = datastore_pb.LookupResponse() entity = datastore_pb.Entity() entity.key.CopyFrom(key_pb) rsp_pb.found.add(entity=entity) conn = self._makeOne() URI = '/'.join([ conn.api_base_url, 'datastore', conn.API_VERSION, 'datasets', DATASET_ID, 'lookup', ]) http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString()) (found, ), missing, deferred = conn.lookup(DATASET_ID, [key_pb]) self.assertEqual(len(missing), 0) self.assertEqual(len(deferred), 0) self.assertEqual(found.key.path_element[0].kind, 'Kind') self.assertEqual(found.key.path_element[0].id, 1234) cw = http._called_with self._verifyProtobufCall(cw, URI, conn) rq_class = datastore_pb.LookupRequest request = rq_class() request.ParseFromString(cw['body']) keys = list(request.key) self.assertEqual(len(keys), 1) _compare_key_pb_after_request(self, key_pb, keys[0])
def test_lookup_single_key_empty_response_w_eventual(self): from gcloud.datastore import _datastore_v1_pb2 as datastore_pb DATASET_ID = 'DATASET' key_pb = self._make_key_pb(DATASET_ID) rsp_pb = datastore_pb.LookupResponse() conn = self._makeOne() URI = '/'.join([ conn.api_base_url, 'datastore', conn.API_VERSION, 'datasets', DATASET_ID, 'lookup', ]) http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString()) found, missing, deferred = conn.lookup(DATASET_ID, [key_pb], eventual=True) self.assertEqual(len(found), 0) self.assertEqual(len(missing), 0) self.assertEqual(len(deferred), 0) cw = http._called_with self._verifyProtobufCall(cw, URI, conn) rq_class = datastore_pb.LookupRequest request = rq_class() request.ParseFromString(cw['body']) keys = list(request.key) self.assertEqual(len(keys), 1) _compare_key_pb_after_request(self, key_pb, keys[0]) self.assertEqual(request.read_options.read_consistency, datastore_pb.ReadOptions.EVENTUAL) self.assertEqual(request.read_options.transaction, b'')
def test_lookup_single_key_empty_response_w_transaction(self): from gcloud.datastore.connection import datastore_pb from gcloud.datastore.key import Key DATASET_ID = 'DATASET' TRANSACTION = 'TRANSACTION' key_pb = Key(path=[{'kind': 'Kind', 'id': 1234}]).to_protobuf() rsp_pb = datastore_pb.LookupResponse() conn = self._makeOne() conn.transaction(Transaction(TRANSACTION)) URI = '/'.join([ conn.API_BASE_URL, 'datastore', conn.API_VERSION, 'datasets', DATASET_ID, 'lookup', ]) http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString()) self.assertEqual(conn.lookup(DATASET_ID, key_pb), None) cw = http._called_with self._verifyProtobufCall(cw, URI, conn) rq_class = datastore_pb.LookupRequest request = rq_class() request.ParseFromString(cw['body']) keys = list(request.key) self.assertEqual(len(keys), 1) self.assertEqual(keys[0], key_pb) self.assertEqual(request.read_options.transaction, TRANSACTION)
def test_lookup_single_key_empty_response(self): from gcloud.datastore.connection import datastore_pb from gcloud.datastore.key import Key DATASET_ID = 'DATASET' key_pb = Key(path=[{'kind': 'Kind', 'id': 1234}]).to_protobuf() rsp_pb = datastore_pb.LookupResponse() conn = self._makeOne() URI = '/'.join([ conn.API_BASE_URL, 'datastore', conn.API_VERSION, 'datasets', DATASET_ID, 'lookup', ]) http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString()) self.assertEqual(conn.lookup(DATASET_ID, key_pb), None) cw = http._called_with self.assertEqual(cw['uri'], URI) self.assertEqual(cw['method'], 'POST') self.assertEqual(cw['headers']['Content-Type'], 'application/x-protobuf') self.assertEqual(cw['headers']['User-Agent'], conn.USER_AGENT) rq_class = datastore_pb.LookupRequest request = rq_class() request.ParseFromString(cw['body']) keys = list(request.key) self.assertEqual(len(keys), 1) self.assertEqual(keys[0], key_pb)
def test_lookup_multiple_keys_w_deferred(self): from gcloud.datastore.connection import datastore_pb from gcloud.datastore.key import Key DATASET_ID = 'DATASET' key_pb1 = Key(path=[{'kind': 'Kind', 'id': 1234}]).to_protobuf() key_pb2 = Key(path=[{'kind': 'Kind', 'id': 2345}]).to_protobuf() rsp_pb = datastore_pb.LookupResponse() rsp_pb.deferred.add().CopyFrom(key_pb1) rsp_pb.deferred.add().CopyFrom(key_pb2) conn = self._makeOne() URI = '/'.join([ conn.API_BASE_URL, 'datastore', conn.API_VERSION, 'datasets', DATASET_ID, 'lookup', ]) http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString()) deferred = [] result = conn.lookup(DATASET_ID, [key_pb1, key_pb2], deferred=deferred) self.assertEqual(result, []) self.assertEqual([def_key for def_key in deferred], [key_pb1, key_pb2]) cw = http._called_with self._verifyProtobufCall(cw, URI, conn) self.assertEqual(cw['uri'], URI) self.assertEqual(cw['method'], 'POST') self.assertEqual(cw['headers']['Content-Type'], 'application/x-protobuf') self.assertEqual(cw['headers']['User-Agent'], conn.USER_AGENT) rq_class = datastore_pb.LookupRequest request = rq_class() request.ParseFromString(cw['body']) keys = list(request.key) self.assertEqual(len(keys), 2) self.assertEqual(keys[0], key_pb1) self.assertEqual(keys[1], key_pb2)
def test_lookup_multiple_keys_w_deferred(self): from gcloud.datastore import _datastore_v1_pb2 as datastore_pb DATASET_ID = 'DATASET' key_pb1 = self._make_key_pb(DATASET_ID) key_pb2 = self._make_key_pb(DATASET_ID, id=2345) rsp_pb = datastore_pb.LookupResponse() rsp_pb.deferred.add().CopyFrom(key_pb1) rsp_pb.deferred.add().CopyFrom(key_pb2) conn = self._makeOne() URI = '/'.join([ conn.api_base_url, 'datastore', conn.API_VERSION, 'datasets', DATASET_ID, 'lookup', ]) http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString()) result, missing, deferred = conn.lookup(DATASET_ID, [key_pb1, key_pb2]) self.assertEqual(result, []) self.assertEqual(len(missing), 0) self.assertEqual([def_key for def_key in deferred], [key_pb1, key_pb2]) cw = http._called_with self._verifyProtobufCall(cw, URI, conn) self.assertEqual(cw['uri'], URI) self.assertEqual(cw['method'], 'POST') self.assertEqual(cw['headers']['Content-Type'], 'application/x-protobuf') self.assertEqual(cw['headers']['User-Agent'], conn.USER_AGENT) rq_class = datastore_pb.LookupRequest request = rq_class() request.ParseFromString(cw['body']) keys = list(request.key) self.assertEqual(len(keys), 2) _compare_key_pb_after_request(self, key_pb1, keys[0]) _compare_key_pb_after_request(self, key_pb2, keys[1])