Skip to content

Commit 57e11b1

Browse files
committed
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().
1 parent d858ff0 commit 57e11b1

File tree

2 files changed

+12
-23
lines changed

2 files changed

+12
-23
lines changed

gcloud/datastore/connection.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,19 @@ def lookup(self, dataset_id, key_pbs,
136136
>>> from gcloud import datastore
137137
>>> datastore.set_defaults()
138138
>>> key = datastore.Key('MyKind', 1234, dataset_id='dataset-id')
139-
>>> datastore.get(key)
140-
<Entity object>
139+
>>> datastore.get([key])
140+
[<Entity object>]
141141
142142
Using the ``connection`` class directly:
143143
144-
>>> connection.lookup('dataset-id', key.to_protobuf())
145-
<Entity protobuf>
144+
>>> connection.lookup('dataset-id', [key.to_protobuf()])
145+
[<Entity protobuf>]
146146
147147
:type dataset_id: string
148148
:param dataset_id: The ID of the dataset to look up the keys.
149149
150150
:type key_pbs: list of :class:`gcloud.datastore._datastore_v1_pb2.Key`
151-
(or a single Key)
152-
:param key_pbs: The key (or keys) to retrieve from the datastore.
151+
:param key_pbs: The keys to retrieve from the datastore.
153152
154153
:type missing: an empty list or None.
155154
:param missing: If a list is passed, the key-only entity protobufs
@@ -188,12 +187,6 @@ def lookup(self, dataset_id, key_pbs,
188187

189188
lookup_request = datastore_pb.LookupRequest()
190189
_set_read_options(lookup_request, eventual, transaction_id)
191-
192-
single_key = isinstance(key_pbs, datastore_pb.Key)
193-
194-
if single_key:
195-
key_pbs = [key_pbs]
196-
197190
helpers._add_keys_to_request(lookup_request.key, key_pbs)
198191

199192
results, missing_found, deferred_found = self._lookup(
@@ -205,12 +198,6 @@ def lookup(self, dataset_id, key_pbs,
205198
if deferred is not None:
206199
deferred.extend(deferred_found)
207200

208-
if single_key:
209-
if results:
210-
return results[0]
211-
else:
212-
return None
213-
214201
return results
215202

216203
def run_query(self, dataset_id, query_pb, namespace=None,

gcloud/datastore/test_connection.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ def test_lookup_single_key_empty_response(self):
194194
'lookup',
195195
])
196196
http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString())
197-
self.assertEqual(conn.lookup(DATASET_ID, key_pb), None)
197+
found = conn.lookup(DATASET_ID, [key_pb])
198+
self.assertEqual(len(found), 0)
198199
cw = http._called_with
199200
self._verifyProtobufCall(cw, URI, conn)
200201
rq_class = datastore_pb.LookupRequest
@@ -220,7 +221,8 @@ def test_lookup_single_key_empty_response_w_eventual(self):
220221
'lookup',
221222
])
222223
http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString())
223-
self.assertEqual(conn.lookup(DATASET_ID, key_pb, eventual=True), None)
224+
found = conn.lookup(DATASET_ID, [key_pb], eventual=True)
225+
self.assertEqual(len(found), 0)
224226
cw = http._called_with
225227
self._verifyProtobufCall(cw, URI, conn)
226228
rq_class = datastore_pb.LookupRequest
@@ -258,8 +260,8 @@ def test_lookup_single_key_empty_response_w_transaction(self):
258260
'lookup',
259261
])
260262
http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString())
261-
found = conn.lookup(DATASET_ID, key_pb, transaction_id=TRANSACTION)
262-
self.assertEqual(found, None)
263+
found = conn.lookup(DATASET_ID, [key_pb], transaction_id=TRANSACTION)
264+
self.assertEqual(len(found), 0)
263265
cw = http._called_with
264266
self._verifyProtobufCall(cw, URI, conn)
265267
rq_class = datastore_pb.LookupRequest
@@ -289,7 +291,7 @@ def test_lookup_single_key_nonempty_response(self):
289291
'lookup',
290292
])
291293
http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString())
292-
found = conn.lookup(DATASET_ID, key_pb)
294+
found, = conn.lookup(DATASET_ID, [key_pb])
293295
self.assertEqual(found.key.path_element[0].kind, 'Kind')
294296
self.assertEqual(found.key.path_element[0].id, 1234)
295297
cw = http._called_with

0 commit comments

Comments
 (0)