Skip to content

Commit

Permalink
Merge pull request #257 from tseaver/168_169-connection_delete_entities
Browse files Browse the repository at this point in the history
Fix #168 and #169:  Rationalize 'Connection.delete_entities'
  • Loading branch information
tseaver committed Oct 19, 2014
2 parents 20ac9ef + e8dda0f commit 714cd5d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 101 deletions.
31 changes: 4 additions & 27 deletions gcloud/datastore/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,38 +405,15 @@ def delete_entities(self, dataset_id, key_pbs):
:rtype: boolean (if in a transaction) or else
:class:`gcloud.datastore.datastore_v1_pb2.MutationResult`.
:returns: True (if in a transaction) or else a mutation result
protobuf.
:returns: True
"""
mutation = self.mutation()

for key_pb in key_pbs:
delete = mutation.delete.add()
delete.CopyFrom(key_pb)

if self.transaction():
return True
else:
return self.commit(dataset_id, mutation)

def delete_entity(self, dataset_id, key_pb):
"""Delete a single key from a dataset in the Cloud Datastore.
This method deals only with
:class:`gcloud.datastore.datastore_v1_pb2.Key` protobufs
and not with any of the other abstractions.
For example, it's used under the hood in the
:func:`gcloud.datastore.entity.Entity.delete` method.
:type dataset_id: string
:param dataset_id: The dataset from which to delete the key.
:type key_pb: :class:`gcloud.datastore.datastore_v1_pb2.Key`
:param key_pb: The key to delete from the datastore.
if not self.transaction():
self.commit(dataset_id, mutation)

:rtype: boolean (if in a transaction) or else
:class:`gcloud.datastore.datastore_v1_pb2.MutationResult`.
:returns: True (if in a transaction) or else a mutation result
protobuf.
"""
return self.delete_entities(dataset_id, [key_pb])
return True
6 changes: 4 additions & 2 deletions gcloud/datastore/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,10 @@ def delete(self):
"""
key = self._must_key
dataset = key.dataset()
dataset.connection().delete_entity(
dataset_id=dataset.id(), key_pb=key.to_protobuf())
dataset.connection().delete_entities(
dataset_id=dataset.id(),
key_pbs=[key.to_protobuf()],
)

def __repr__(self):
if self._key:
Expand Down
70 changes: 1 addition & 69 deletions gcloud/datastore/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,8 +778,7 @@ def test_delete_entities_wo_transaction(self):
])
http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString())
result = conn.delete_entities(DATASET_ID, [key_pb])
self.assertEqual(result.index_updates, 0)
self.assertEqual(list(result.insert_auto_id_key), [])
self.assertEqual(result, True)
cw = http._called_with
self.assertEqual(cw['uri'], URI)
self.assertEqual(cw['method'], 'POST')
Expand Down Expand Up @@ -825,73 +824,6 @@ def mutation(self):
mutation = conn.mutation()
self.assertEqual(len(mutation.delete), 1)

def test_delete_entity_wo_transaction(self):
from gcloud.datastore.connection import datastore_pb
from gcloud.datastore.dataset import Dataset
from gcloud.datastore.key import Key

DATASET_ID = 'DATASET'
key_pb = Key(dataset=Dataset(DATASET_ID),
path=[{'kind': 'Kind', 'id': 1234}]).to_protobuf()
rsp_pb = datastore_pb.CommitResponse()
conn = self._makeOne()
URI = '/'.join([
conn.API_BASE_URL,
'datastore',
conn.API_VERSION,
'datasets',
DATASET_ID,
'commit',
])
http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString())
result = conn.delete_entity(DATASET_ID, key_pb)
self.assertEqual(result.index_updates, 0)
self.assertEqual(list(result.insert_auto_id_key), [])
cw = http._called_with
self.assertEqual(cw['uri'], URI)
self.assertEqual(cw['method'], 'POST')
expected_headers = {
'Content-Type': 'application/x-protobuf',
'Content-Length': '30',
'User-Agent': conn.USER_AGENT,
}
self.assertEqual(cw['headers'], expected_headers)
rq_class = datastore_pb.CommitRequest
request = rq_class()
request.ParseFromString(cw['body'])
self.assertEqual(request.transaction, '')
mutation = request.mutation
self.assertEqual(len(mutation.insert_auto_id), 0)
self.assertEqual(len(mutation.upsert), 0)
deletes = list(mutation.delete)
self.assertEqual(len(deletes), 1)
delete = deletes[0]
self.assertEqual(delete, key_pb)
self.assertEqual(request.mode, rq_class.NON_TRANSACTIONAL)

def test_delete_entity_w_transaction(self):
from gcloud.datastore.connection import datastore_pb
from gcloud.datastore.dataset import Dataset
from gcloud.datastore.key import Key

mutation = datastore_pb.Mutation()

class Xact(object):
def mutation(self):
return mutation
DATASET_ID = 'DATASET'
key_pb = Key(dataset=Dataset(DATASET_ID),
path=[{'kind': 'Kind', 'id': 1234}]).to_protobuf()
rsp_pb = datastore_pb.CommitResponse()
conn = self._makeOne()
conn.transaction(Xact())
http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString())
result = conn.delete_entity(DATASET_ID, key_pb)
self.assertEqual(result, True)
self.assertEqual(http._called_with, None)
mutation = conn.mutation()
self.assertEqual(len(mutation.delete), 1)


class Http(object):

Expand Down
6 changes: 3 additions & 3 deletions gcloud/datastore/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def test_delete(self):
entity.key(key)
entity['foo'] = 'Foo'
self.assertTrue(entity.delete() is None)
self.assertEqual(connection._deleted, (_DATASET_ID, 'KEY'))
self.assertEqual(connection._deleted, (_DATASET_ID, ['KEY']))

def test___repr___no_key_empty(self):
entity = self._makeOne(None, None)
Expand Down Expand Up @@ -264,8 +264,8 @@ def save_entity(self, dataset_id, key_pb, properties):
self._saved = (dataset_id, key_pb, properties)
return self._save_result

def delete_entity(self, dataset_id, key_pb):
self._deleted = (dataset_id, key_pb)
def delete_entities(self, dataset_id, key_pbs):
self._deleted = (dataset_id, key_pbs)


class _Transaction(object):
Expand Down

0 comments on commit 714cd5d

Please sign in to comment.