From 57e11b1e89ad38340bae6fbf89ff040c9b69bbb9 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Fri, 30 Jan 2015 15:55:14 -0800 Subject: [PATCH] Requiring Connection.lookup to take a list in datastore. This is in line with our decision in api.get / api.put / api.delete to only take a list and not over-use isinstance(). --- gcloud/datastore/connection.py | 23 +++++------------------ gcloud/datastore/test_connection.py | 12 +++++++----- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/gcloud/datastore/connection.py b/gcloud/datastore/connection.py index d52f794b2f6f..673e9374fe53 100644 --- a/gcloud/datastore/connection.py +++ b/gcloud/datastore/connection.py @@ -136,20 +136,19 @@ def lookup(self, dataset_id, key_pbs, >>> from gcloud import datastore >>> datastore.set_defaults() >>> key = datastore.Key('MyKind', 1234, dataset_id='dataset-id') - >>> datastore.get(key) - + >>> datastore.get([key]) + [] Using the ``connection`` class directly: - >>> connection.lookup('dataset-id', key.to_protobuf()) - + >>> connection.lookup('dataset-id', [key.to_protobuf()]) + [] :type dataset_id: string :param dataset_id: The ID of the dataset to look up the keys. :type key_pbs: list of :class:`gcloud.datastore._datastore_v1_pb2.Key` - (or a single Key) - :param key_pbs: The key (or keys) to retrieve from the datastore. + :param key_pbs: The keys to retrieve from the datastore. :type missing: an empty list or None. :param missing: If a list is passed, the key-only entity protobufs @@ -188,12 +187,6 @@ def lookup(self, dataset_id, key_pbs, lookup_request = datastore_pb.LookupRequest() _set_read_options(lookup_request, eventual, transaction_id) - - single_key = isinstance(key_pbs, datastore_pb.Key) - - if single_key: - key_pbs = [key_pbs] - helpers._add_keys_to_request(lookup_request.key, key_pbs) results, missing_found, deferred_found = self._lookup( @@ -205,12 +198,6 @@ def lookup(self, dataset_id, key_pbs, if deferred is not None: deferred.extend(deferred_found) - if single_key: - if results: - return results[0] - else: - return None - return results def run_query(self, dataset_id, query_pb, namespace=None, diff --git a/gcloud/datastore/test_connection.py b/gcloud/datastore/test_connection.py index 278b2e9af1ea..0523599f6ce8 100644 --- a/gcloud/datastore/test_connection.py +++ b/gcloud/datastore/test_connection.py @@ -194,7 +194,8 @@ def test_lookup_single_key_empty_response(self): 'lookup', ]) http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString()) - self.assertEqual(conn.lookup(DATASET_ID, key_pb), None) + found = conn.lookup(DATASET_ID, [key_pb]) + self.assertEqual(len(found), 0) cw = http._called_with self._verifyProtobufCall(cw, URI, conn) rq_class = datastore_pb.LookupRequest @@ -220,7 +221,8 @@ def test_lookup_single_key_empty_response_w_eventual(self): 'lookup', ]) http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString()) - self.assertEqual(conn.lookup(DATASET_ID, key_pb, eventual=True), None) + found = conn.lookup(DATASET_ID, [key_pb], eventual=True) + self.assertEqual(len(found), 0) cw = http._called_with self._verifyProtobufCall(cw, URI, conn) rq_class = datastore_pb.LookupRequest @@ -258,8 +260,8 @@ def test_lookup_single_key_empty_response_w_transaction(self): 'lookup', ]) http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString()) - found = conn.lookup(DATASET_ID, key_pb, transaction_id=TRANSACTION) - self.assertEqual(found, None) + found = conn.lookup(DATASET_ID, [key_pb], transaction_id=TRANSACTION) + self.assertEqual(len(found), 0) cw = http._called_with self._verifyProtobufCall(cw, URI, conn) rq_class = datastore_pb.LookupRequest @@ -289,7 +291,7 @@ def test_lookup_single_key_nonempty_response(self): 'lookup', ]) http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString()) - found = conn.lookup(DATASET_ID, key_pb) + 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