Skip to content

Commit b6ccf75

Browse files
committed
Require that keys for 'put' / 'delete' match the 'dataset_id' of the batch.
Fixes #447.
1 parent 12ac983 commit b6ccf75

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

gcloud/datastore/batch.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,15 @@ def put(self, entity):
210210
:type entity: :class:`gcloud.datastore.entity.Entity`
211211
:param entity: the entity to be saved.
212212
213-
:raises: ValueError if entity has no key assigned.
213+
:raises: ValueError if entity has no key assigned, or if the key's
214+
``dataset_id`` does not match ours.
214215
"""
215216
if entity.key is None:
216217
raise ValueError("Entity must have a key")
217218

219+
if entity.key.dataset_id != self._dataset_id:
220+
raise ValueError("Key must be from same dataset as batch")
221+
218222
_assign_entity_to_mutation(
219223
self.mutation, entity, self._auto_id_entities)
220224

@@ -224,11 +228,15 @@ def delete(self, key):
224228
:type key: :class:`gcloud.datastore.key.Key`
225229
:param key: the key to be deleted.
226230
227-
:raises: ValueError if key is not complete.
231+
:raises: ValueError if key is not complete, or if the key's
232+
``dataset_id`` does not match ours.
228233
"""
229234
if key.is_partial:
230235
raise ValueError("Key must be complete")
231236

237+
if key.dataset_id != self._dataset_id:
238+
raise ValueError("Key must be from same dataset as batch")
239+
232240
key_pb = key.to_protobuf()
233241
helpers._add_keys_to_request(self.mutation.delete, [key_pb])
234242

gcloud/datastore/test_batch.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def test_add_auto_id_entity_w_partial_key(self):
116116
connection = _Connection()
117117
batch = self._makeOne(dataset_id=_DATASET, connection=connection)
118118
entity = _Entity()
119-
key = entity.key = _Key(_Entity)
119+
key = entity.key = _Key(_DATASET)
120120
key._id = None
121121

122122
batch.add_auto_id_entity(entity)
@@ -128,7 +128,7 @@ def test_add_auto_id_entity_w_completed_key(self):
128128
connection = _Connection()
129129
batch = self._makeOne(dataset_id=_DATASET, connection=connection)
130130
entity = _Entity()
131-
entity.key = _Key(_Entity)
131+
entity.key = _Key(_DATASET)
132132

133133
self.assertRaises(ValueError, batch.add_auto_id_entity, entity)
134134

@@ -139,6 +139,15 @@ def test_put_entity_wo_key(self):
139139

140140
self.assertRaises(ValueError, batch.put, _Entity())
141141

142+
def test_put_entity_w_key_wrong_dataset_id(self):
143+
_DATASET = 'DATASET'
144+
connection = _Connection()
145+
batch = self._makeOne(dataset_id=_DATASET, connection=connection)
146+
entity = _Entity()
147+
entity.key = _Key('OTHER')
148+
149+
self.assertRaises(ValueError, batch.put, entity)
150+
142151
def test_put_entity_w_partial_key(self):
143152
_DATASET = 'DATASET'
144153
_PROPERTIES = {'foo': 'bar'}
@@ -203,6 +212,14 @@ def test_delete_w_partial_key(self):
203212

204213
self.assertRaises(ValueError, batch.delete, key)
205214

215+
def test_delete_w_key_wrong_dataset_id(self):
216+
_DATASET = 'DATASET'
217+
connection = _Connection()
218+
batch = self._makeOne(dataset_id=_DATASET, connection=connection)
219+
key = _Key('OTHER')
220+
221+
self.assertRaises(ValueError, batch.delete, key)
222+
206223
def test_delete_w_completed_key(self):
207224
_DATASET = 'DATASET'
208225
connection = _Connection()

0 commit comments

Comments
 (0)