Skip to content

Commit

Permalink
Merge pull request #582 from dhermes/connection-lookup-only-list
Browse files Browse the repository at this point in the history
Requiring Connection.lookup to take a list in datastore.
  • Loading branch information
dhermes committed Feb 3, 2015
2 parents d858ff0 + 57e11b1 commit f854cd1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
23 changes: 5 additions & 18 deletions gcloud/datastore/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
<Entity object>
>>> datastore.get([key])
[<Entity object>]
Using the ``connection`` class directly:
>>> connection.lookup('dataset-id', key.to_protobuf())
<Entity protobuf>
>>> connection.lookup('dataset-id', [key.to_protobuf()])
[<Entity 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
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down
12 changes: 7 additions & 5 deletions gcloud/datastore/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f854cd1

Please sign in to comment.