Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7e00169

Browse filesBrowse files
committed
Puting helpers in datastore Batch for getting new mutations.
Also adding test helpers to extract a single mutation (of a given type) from a mutation protobuf object. This is in advance of `v1beta3` where the structure of the `CommitRequest` is significantly different.
1 parent f71b32e commit 7e00169
Copy full SHA for 7e00169

3 files changed

+82-92Lines changed: 82 additions & 92 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎gcloud/datastore/batch.py‎

Copy file name to clipboardExpand all lines: gcloud/datastore/batch.py
+30-4Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,33 @@ def connection(self):
9898
"""
9999
return self._client.connection
100100

101+
def _add_partial_key_entity_pb(self):
102+
"""Adds a new mutation for an entity with a partial key.
103+
104+
:rtype: :class:`gcloud.datastore._entity_pb2.Entity`
105+
:returns: The newly created entity protobuf that will be
106+
updated and sent with a commit.
107+
"""
108+
return self.mutations.insert_auto_id.add()
109+
110+
def _add_complete_key_entity_pb(self):
111+
"""Adds a new mutation for an entity with a completed key.
112+
113+
:rtype: :class:`gcloud.datastore._entity_pb2.Entity`
114+
:returns: The newly created entity protobuf that will be
115+
updated and sent with a commit.
116+
"""
117+
return self.mutations.upsert.add()
118+
119+
def _add_delete_key_pb(self):
120+
"""Adds a new mutation for a key to be deleted.
121+
122+
:rtype: :class:`gcloud.datastore._entity_pb2.Key`
123+
:returns: The newly created key protobuf that will be
124+
deleted when sent with a commit.
125+
"""
126+
return self.mutations.delete.add()
127+
101128
@property
102129
def mutations(self):
103130
"""Getter for the changes accumulated by this batch.
@@ -146,10 +173,10 @@ def put(self, entity):
146173
raise ValueError("Key must be from same dataset as batch")
147174

148175
if entity.key.is_partial:
149-
entity_pb = self.mutations.insert_auto_id.add()
176+
entity_pb = self._add_partial_key_entity_pb()
150177
self._partial_key_entities.append(entity)
151178
else:
152-
entity_pb = self.mutations.upsert.add()
179+
entity_pb = self._add_complete_key_entity_pb()
153180

154181
_assign_entity_to_pb(entity_pb, entity)
155182

@@ -169,14 +196,13 @@ def delete(self, key):
169196
raise ValueError("Key must be from same dataset as batch")
170197

171198
key_pb = helpers._prepare_key_for_request(key.to_protobuf())
172-
self.mutations.delete.add().CopyFrom(key_pb)
199+
self._add_delete_key_pb().CopyFrom(key_pb)
173200

174201
def begin(self):
175202
"""No-op
176203
177204
Overridden by :class:`gcloud.datastore.transaction.Transaction`.
178205
"""
179-
pass
180206

181207
def commit(self):
182208
"""Commits the batch.
Collapse file

‎gcloud/datastore/test_batch.py‎

Copy file name to clipboardExpand all lines: gcloud/datastore/test_batch.py
+42-72Lines changed: 42 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,8 @@ def test_put_entity_w_partial_key(self):
8989

9090
batch.put(entity)
9191

92-
insert_auto_ids = list(batch.mutations.insert_auto_id)
93-
self.assertEqual(len(insert_auto_ids), 1)
94-
self.assertEqual(insert_auto_ids[0].key, key._key)
95-
upserts = list(batch.mutations.upsert)
96-
self.assertEqual(len(upserts), 0)
97-
deletes = list(batch.mutations.delete)
98-
self.assertEqual(len(deletes), 0)
92+
mutated_entity = _mutated_pb(self, batch.mutations, 'insert_auto_id')
93+
self.assertEqual(mutated_entity.key, key._key)
9994
self.assertEqual(batch._partial_key_entities, [entity])
10095

10196
def test_put_entity_w_completed_key(self):
@@ -115,14 +110,10 @@ def test_put_entity_w_completed_key(self):
115110

116111
batch.put(entity)
117112

118-
insert_auto_ids = list(batch.mutations.insert_auto_id)
119-
self.assertEqual(len(insert_auto_ids), 0)
120-
upserts = list(batch.mutations.upsert)
121-
self.assertEqual(len(upserts), 1)
122-
123-
upsert = upserts[0]
124-
self.assertEqual(upsert.key, key._key)
125-
props = dict([(prop.name, prop.value) for prop in upsert.property])
113+
mutated_entity = _mutated_pb(self, batch.mutations, 'upsert')
114+
self.assertEqual(mutated_entity.key, key._key)
115+
props = dict([(prop.name, prop.value)
116+
for prop in mutated_entity.property])
126117
self.assertTrue(props['foo'].indexed)
127118
self.assertFalse(props['baz'].indexed)
128119
self.assertTrue(props['spam'].indexed)
@@ -131,9 +122,6 @@ def test_put_entity_w_completed_key(self):
131122
self.assertFalse(props['spam'].list_value[2].indexed)
132123
self.assertFalse('frotz' in props)
133124

134-
deletes = list(batch.mutations.delete)
135-
self.assertEqual(len(deletes), 0)
136-
137125
def test_put_entity_w_completed_key_prefixed_dataset_id(self):
138126
_DATASET = 'DATASET'
139127
_PROPERTIES = {
@@ -151,14 +139,10 @@ def test_put_entity_w_completed_key_prefixed_dataset_id(self):
151139

152140
batch.put(entity)
153141

154-
insert_auto_ids = list(batch.mutations.insert_auto_id)
155-
self.assertEqual(len(insert_auto_ids), 0)
156-
upserts = list(batch.mutations.upsert)
157-
self.assertEqual(len(upserts), 1)
158-
159-
upsert = upserts[0]
160-
self.assertEqual(upsert.key, key._key)
161-
props = dict([(prop.name, prop.value) for prop in upsert.property])
142+
mutated_entity = _mutated_pb(self, batch.mutations, 'upsert')
143+
self.assertEqual(mutated_entity.key, key._key)
144+
props = dict([(prop.name, prop.value)
145+
for prop in mutated_entity.property])
162146
self.assertTrue(props['foo'].indexed)
163147
self.assertFalse(props['baz'].indexed)
164148
self.assertTrue(props['spam'].indexed)
@@ -167,9 +151,6 @@ def test_put_entity_w_completed_key_prefixed_dataset_id(self):
167151
self.assertFalse(props['spam'].list_value[2].indexed)
168152
self.assertFalse('frotz' in props)
169153

170-
deletes = list(batch.mutations.delete)
171-
self.assertEqual(len(deletes), 0)
172-
173154
def test_delete_w_partial_key(self):
174155
_DATASET = 'DATASET'
175156
connection = _Connection()
@@ -198,13 +179,8 @@ def test_delete_w_completed_key(self):
198179

199180
batch.delete(key)
200181

201-
insert_auto_ids = list(batch.mutations.insert_auto_id)
202-
self.assertEqual(len(insert_auto_ids), 0)
203-
upserts = list(batch.mutations.upsert)
204-
self.assertEqual(len(upserts), 0)
205-
deletes = list(batch.mutations.delete)
206-
self.assertEqual(len(deletes), 1)
207-
self.assertEqual(deletes[0], key._key)
182+
mutated_key = _mutated_pb(self, batch.mutations, 'delete')
183+
self.assertEqual(mutated_key, key._key)
208184

209185
def test_delete_w_completed_key_w_prefixed_dataset_id(self):
210186
_DATASET = 'DATASET'
@@ -215,13 +191,8 @@ def test_delete_w_completed_key_w_prefixed_dataset_id(self):
215191

216192
batch.delete(key)
217193

218-
insert_auto_ids = list(batch.mutations.insert_auto_id)
219-
self.assertEqual(len(insert_auto_ids), 0)
220-
upserts = list(batch.mutations.upsert)
221-
self.assertEqual(len(upserts), 0)
222-
deletes = list(batch.mutations.delete)
223-
self.assertEqual(len(deletes), 1)
224-
self.assertEqual(deletes[0], key._key)
194+
mutated_key = _mutated_pb(self, batch.mutations, 'delete')
195+
self.assertEqual(mutated_key, key._key)
225196

226197
def test_commit(self):
227198
_DATASET = 'DATASET'
@@ -268,13 +239,8 @@ def test_as_context_mgr_wo_error(self):
268239

269240
self.assertEqual(list(client._batches), [])
270241

271-
insert_auto_ids = list(batch.mutations.insert_auto_id)
272-
self.assertEqual(len(insert_auto_ids), 0)
273-
upserts = list(batch.mutations.upsert)
274-
self.assertEqual(len(upserts), 1)
275-
self.assertEqual(upserts[0].key, key._key)
276-
deletes = list(batch.mutations.delete)
277-
self.assertEqual(len(deletes), 0)
242+
mutated_entity = _mutated_pb(self, batch.mutations, 'upsert')
243+
self.assertEqual(mutated_entity.key, key._key)
278244
self.assertEqual(connection._committed,
279245
[(_DATASET, batch.mutations, None)])
280246

@@ -301,21 +267,11 @@ def test_as_context_mgr_nested(self):
301267

302268
self.assertEqual(list(client._batches), [])
303269

304-
insert_auto_ids = list(batch1.mutations.insert_auto_id)
305-
self.assertEqual(len(insert_auto_ids), 0)
306-
upserts = list(batch1.mutations.upsert)
307-
self.assertEqual(len(upserts), 1)
308-
self.assertEqual(upserts[0].key, key1._key)
309-
deletes = list(batch1.mutations.delete)
310-
self.assertEqual(len(deletes), 0)
311-
312-
insert_auto_ids = list(batch2.mutations.insert_auto_id)
313-
self.assertEqual(len(insert_auto_ids), 0)
314-
upserts = list(batch2.mutations.upsert)
315-
self.assertEqual(len(upserts), 1)
316-
self.assertEqual(upserts[0].key, key2._key)
317-
deletes = list(batch2.mutations.delete)
318-
self.assertEqual(len(deletes), 0)
270+
mutated_entity1 = _mutated_pb(self, batch1.mutations, 'upsert')
271+
self.assertEqual(mutated_entity1.key, key1._key)
272+
273+
mutated_entity2 = _mutated_pb(self, batch2.mutations, 'upsert')
274+
self.assertEqual(mutated_entity2.key, key2._key)
319275

320276
self.assertEqual(connection._committed,
321277
[(_DATASET, batch2.mutations, None),
@@ -341,13 +297,8 @@ def test_as_context_mgr_w_error(self):
341297

342298
self.assertEqual(list(client._batches), [])
343299

344-
insert_auto_ids = list(batch.mutations.insert_auto_id)
345-
self.assertEqual(len(insert_auto_ids), 0)
346-
upserts = list(batch.mutations.upsert)
347-
self.assertEqual(len(upserts), 1)
348-
self.assertEqual(upserts[0].key, key._key)
349-
deletes = list(batch.mutations.delete)
350-
self.assertEqual(len(deletes), 0)
300+
mutated_entity = _mutated_pb(self, batch.mutations, 'upsert')
301+
self.assertEqual(mutated_entity.key, key._key)
351302
self.assertEqual(connection._committed, [])
352303

353304

@@ -436,3 +387,22 @@ def _pop_batch(self):
436387
def current_batch(self):
437388
if self._batches:
438389
return self._batches[0]
390+
391+
392+
def _assert_num_mutations(test_case, mutation_pb, num_mutations):
393+
total_mutations = (len(mutation_pb.upsert) +
394+
len(mutation_pb.update) +
395+
len(mutation_pb.insert) +
396+
len(mutation_pb.insert_auto_id) +
397+
len(mutation_pb.delete))
398+
test_case.assertEqual(total_mutations, num_mutations)
399+
400+
401+
def _mutated_pb(test_case, mutation_pb, mutation_type):
402+
# Make sure there is only one mutation.
403+
_assert_num_mutations(test_case, mutation_pb, 1)
404+
405+
mutated_pbs = getattr(mutation_pb, mutation_type, [])
406+
# Make sure we have exactly one protobuf.
407+
test_case.assertEqual(len(mutated_pbs), 1)
408+
return mutated_pbs[0]
Collapse file

‎gcloud/datastore/test_client.py‎

Copy file name to clipboardExpand all lines: gcloud/datastore/test_client.py
+10-16Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ def test_put_multi_no_batch_w_partial_key(self):
637637
def test_put_multi_existing_batch_w_completed_key(self):
638638
from gcloud.datastore.test_batch import _Entity
639639
from gcloud.datastore.test_batch import _Key
640+
from gcloud.datastore.test_batch import _mutated_pb
640641

641642
creds = object()
642643
client = self._makeOne(credentials=creds)
@@ -647,14 +648,11 @@ def test_put_multi_existing_batch_w_completed_key(self):
647648
result = client.put_multi([entity])
648649

649650
self.assertEqual(result, None)
650-
self.assertEqual(len(CURR_BATCH.mutations.insert_auto_id), 0)
651-
upserts = list(CURR_BATCH.mutations.upsert)
652-
self.assertEqual(len(upserts), 1)
653-
self.assertEqual(upserts[0].key, key.to_protobuf())
654-
properties = list(upserts[0].property)
651+
mutated_entity = _mutated_pb(self, CURR_BATCH.mutations, 'upsert')
652+
self.assertEqual(mutated_entity.key, key.to_protobuf())
653+
properties = list(mutated_entity.property)
655654
self.assertEqual(properties[0].name, 'foo')
656655
self.assertEqual(properties[0].value.string_value, u'bar')
657-
self.assertEqual(len(CURR_BATCH.mutations.delete), 0)
658656

659657
def test_delete(self):
660658
_called_with = []
@@ -698,6 +696,7 @@ def test_delete_multi_no_batch(self):
698696

699697
def test_delete_multi_w_existing_batch(self):
700698
from gcloud.datastore.test_batch import _Key
699+
from gcloud.datastore.test_batch import _mutated_pb
701700

702701
creds = object()
703702
client = self._makeOne(credentials=creds)
@@ -707,15 +706,13 @@ def test_delete_multi_w_existing_batch(self):
707706
result = client.delete_multi([key])
708707

709708
self.assertEqual(result, None)
710-
self.assertEqual(len(CURR_BATCH.mutations.insert_auto_id), 0)
711-
self.assertEqual(len(CURR_BATCH.mutations.upsert), 0)
712-
deletes = list(CURR_BATCH.mutations.delete)
713-
self.assertEqual(len(deletes), 1)
714-
self.assertEqual(deletes[0], key._key)
709+
mutated_key = _mutated_pb(self, CURR_BATCH.mutations, 'delete')
710+
self.assertEqual(mutated_key, key._key)
715711
self.assertEqual(len(client.connection._commit_cw), 0)
716712

717713
def test_delete_multi_w_existing_transaction(self):
718714
from gcloud.datastore.test_batch import _Key
715+
from gcloud.datastore.test_batch import _mutated_pb
719716

720717
creds = object()
721718
client = self._makeOne(credentials=creds)
@@ -725,11 +722,8 @@ def test_delete_multi_w_existing_transaction(self):
725722
result = client.delete_multi([key])
726723

727724
self.assertEqual(result, None)
728-
self.assertEqual(len(CURR_XACT.mutations.insert_auto_id), 0)
729-
self.assertEqual(len(CURR_XACT.mutations.upsert), 0)
730-
deletes = list(CURR_XACT.mutations.delete)
731-
self.assertEqual(len(deletes), 1)
732-
self.assertEqual(deletes[0], key._key)
725+
mutated_key = _mutated_pb(self, CURR_XACT.mutations, 'delete')
726+
self.assertEqual(mutated_key, key._key)
733727
self.assertEqual(len(client.connection._commit_cw), 0)
734728

735729
def test_allocate_ids_w_partial_key(self):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.