Skip to content

Commit 25e3afc

Browse files
committed
Merge pull request #1461 from dhermes/commit-request-v1beta3-changes
Updating CommitRequest, Mutation and helpers for v1beta3.
2 parents 8f3fe6d + 97d6e87 commit 25e3afc

File tree

9 files changed

+63
-50
lines changed

9 files changed

+63
-50
lines changed

gcloud/datastore/batch.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ def _add_partial_key_entity_pb(self):
119119
:returns: The newly created entity protobuf that will be
120120
updated and sent with a commit.
121121
"""
122-
return self.mutations.insert_auto_id.add()
122+
new_mutation = self.mutations.add()
123+
return new_mutation.insert
123124

124125
def _add_complete_key_entity_pb(self):
125126
"""Adds a new mutation for an entity with a completed key.
@@ -131,7 +132,8 @@ def _add_complete_key_entity_pb(self):
131132
# We use ``upsert`` for entities with completed keys, rather than
132133
# ``insert`` or ``update``, in order not to create race conditions
133134
# based on prior existence / removal of the entity.
134-
return self.mutations.upsert.add()
135+
new_mutation = self.mutations.add()
136+
return new_mutation.upsert
135137

136138
def _add_delete_key_pb(self):
137139
"""Adds a new mutation for a key to be deleted.
@@ -140,7 +142,8 @@ def _add_delete_key_pb(self):
140142
:returns: The newly created key protobuf that will be
141143
deleted when sent with a commit.
142144
"""
143-
return self.mutations.delete.add()
145+
new_mutation = self.mutations.add()
146+
return new_mutation.delete
144147

145148
@property
146149
def mutations(self):
@@ -152,10 +155,11 @@ def mutations(self):
152155
adding a new mutation. This getter returns the protobuf that has been
153156
built-up so far.
154157
155-
:rtype: :class:`gcloud.datastore._generated.datastore_pb2.Mutation`
156-
:returns: The Mutation protobuf to be sent in the commit request.
158+
:rtype: iterable
159+
:returns: The list of :class:`._generated.datastore_pb2.Mutation`
160+
protobufs to be sent in the commit request.
157161
"""
158-
return self._commit_request.mutation
162+
return self._commit_request.mutations
159163

160164
def put(self, entity):
161165
"""Remember an entity's state to be saved during :meth:`commit`.
@@ -172,7 +176,7 @@ def put(self, entity):
172176
"bytes" ('str' in Python2, 'bytes' in Python3) map to 'blob_value'.
173177
174178
When an entity has a partial key, calling :meth:`commit` sends it as
175-
an ``insert_auto_id`` mutation and the key is completed. On return,
179+
an ``insert`` mutation and the key is completed. On return,
176180
the key for the ``entity`` passed in is updated to match the key ID
177181
assigned by the server.
178182

gcloud/datastore/connection.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,8 @@ def _parse_commit_response(commit_response_pb):
431431
:class:`._generated.entity_pb2.Key` for each incomplete key
432432
that was completed in the commit.
433433
"""
434-
mut_result = commit_response_pb.mutation_result
435-
index_updates = mut_result.index_updates
436-
completed_keys = list(mut_result.insert_auto_id_key)
434+
mut_results = commit_response_pb.mutation_results
435+
index_updates = commit_response_pb.index_updates
436+
completed_keys = [mut_result.key for mut_result in mut_results
437+
if mut_result.HasField('key')] # Message field (Key)
437438
return index_updates, completed_keys

gcloud/datastore/helpers.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import six
2424

2525
from gcloud._helpers import _datetime_to_pb_timestamp
26-
from gcloud._helpers import _has_field
2726
from gcloud._helpers import _pb_timestamp_to_datetime
2827
from gcloud.datastore._generated import entity_pb2 as _entity_pb2
2928
from gcloud.datastore.entity import Entity

gcloud/datastore/test_batch.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ def test_ctor(self):
3838
self.assertEqual(batch.namespace, _NAMESPACE)
3939
self.assertTrue(batch._id is None)
4040
self.assertEqual(batch._status, batch._INITIAL)
41-
self.assertTrue(isinstance(batch.mutations, datastore_pb2.Mutation))
41+
self.assertTrue(isinstance(batch._commit_request,
42+
datastore_pb2.CommitRequest))
43+
self.assertTrue(batch.mutations is batch._commit_request.mutations)
4244
self.assertEqual(batch._partial_key_entities, [])
4345

4446
def test_current(self):
@@ -90,7 +92,7 @@ def test_put_entity_w_partial_key(self):
9092

9193
batch.put(entity)
9294

93-
mutated_entity = _mutated_pb(self, batch.mutations, 'insert_auto_id')
95+
mutated_entity = _mutated_pb(self, batch.mutations, 'insert')
9496
self.assertEqual(mutated_entity.key, key._key)
9597
self.assertEqual(batch._partial_key_entities, [entity])
9698

@@ -121,9 +123,10 @@ def test_put_entity_w_completed_key(self):
121123
self.assertFalse(prop_dict['foo'].exclude_from_indexes)
122124
self.assertTrue(prop_dict['baz'].exclude_from_indexes)
123125
self.assertFalse(prop_dict['spam'].exclude_from_indexes)
124-
self.assertTrue(prop_dict['spam'].list_value[0].exclude_from_indexes)
125-
self.assertTrue(prop_dict['spam'].list_value[1].exclude_from_indexes)
126-
self.assertTrue(prop_dict['spam'].list_value[2].exclude_from_indexes)
126+
spam_values = prop_dict['spam'].array_value.values
127+
self.assertTrue(spam_values[0].exclude_from_indexes)
128+
self.assertTrue(spam_values[1].exclude_from_indexes)
129+
self.assertTrue(spam_values[2].exclude_from_indexes)
127130
self.assertFalse('frotz' in prop_dict)
128131

129132
def test_put_entity_w_completed_key_prefixed_project(self):
@@ -153,9 +156,10 @@ def test_put_entity_w_completed_key_prefixed_project(self):
153156
self.assertFalse(prop_dict['foo'].exclude_from_indexes)
154157
self.assertTrue(prop_dict['baz'].exclude_from_indexes)
155158
self.assertFalse(prop_dict['spam'].exclude_from_indexes)
156-
self.assertTrue(prop_dict['spam'].list_value[0].exclude_from_indexes)
157-
self.assertTrue(prop_dict['spam'].list_value[1].exclude_from_indexes)
158-
self.assertTrue(prop_dict['spam'].list_value[2].exclude_from_indexes)
159+
spam_values = prop_dict['spam'].array_value.values
160+
self.assertTrue(spam_values[0].exclude_from_indexes)
161+
self.assertTrue(spam_values[1].exclude_from_indexes)
162+
self.assertTrue(spam_values[2].exclude_from_indexes)
159163
self.assertFalse('frotz' in prop_dict)
160164

161165
def test_delete_w_partial_key(self):
@@ -424,20 +428,18 @@ def current_batch(self):
424428
return self._batches[0]
425429

426430

427-
def _assert_num_mutations(test_case, mutation_pb, num_mutations):
428-
total_mutations = (len(mutation_pb.upsert) +
429-
len(mutation_pb.update) +
430-
len(mutation_pb.insert) +
431-
len(mutation_pb.insert_auto_id) +
432-
len(mutation_pb.delete))
433-
test_case.assertEqual(total_mutations, num_mutations)
431+
def _assert_num_mutations(test_case, mutation_pb_list, num_mutations):
432+
test_case.assertEqual(len(mutation_pb_list), num_mutations)
434433

435434

436-
def _mutated_pb(test_case, mutation_pb, mutation_type):
435+
def _mutated_pb(test_case, mutation_pb_list, mutation_type):
437436
# Make sure there is only one mutation.
438-
_assert_num_mutations(test_case, mutation_pb, 1)
437+
_assert_num_mutations(test_case, mutation_pb_list, 1)
439438

440-
mutated_pbs = getattr(mutation_pb, mutation_type, [])
441-
# Make sure we have exactly one protobuf.
442-
test_case.assertEqual(len(mutated_pbs), 1)
443-
return mutated_pbs[0]
439+
# We grab the only mutation.
440+
mutated_pb = mutation_pb_list[0]
441+
# Then check if it is the correct type.
442+
test_case.assertEqual(mutated_pb.WhichOneof('operation'),
443+
mutation_type)
444+
445+
return getattr(mutated_pb, mutation_type)

gcloud/datastore/test_client.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ def test_put_multi_no_batch_w_partial_key(self):
612612
from gcloud.datastore.test_batch import _Entity
613613
from gcloud.datastore.test_batch import _Key
614614
from gcloud.datastore.test_batch import _KeyPB
615+
from gcloud.datastore.test_batch import _mutated_pb
615616

616617
entity = _Entity(foo=u'bar')
617618
key = entity.key = _Key(self.PROJECT)
@@ -628,15 +629,16 @@ def test_put_multi_no_batch_w_partial_key(self):
628629
(project,
629630
commit_req, transaction_id) = client.connection._commit_cw[0]
630631
self.assertEqual(project, self.PROJECT)
631-
inserts = list(commit_req.mutation.insert_auto_id)
632-
self.assertEqual(len(inserts), 1)
633-
self.assertEqual(inserts[0].key, key.to_protobuf())
634632

635-
prop_list = list(_property_tuples(inserts[0]))
633+
mutated_entity = _mutated_pb(self, commit_req.mutations, 'insert')
634+
self.assertEqual(mutated_entity.key, key.to_protobuf())
635+
636+
prop_list = list(_property_tuples(mutated_entity))
636637
self.assertTrue(len(prop_list), 1)
637638
name, value_pb = prop_list[0]
638639
self.assertEqual(name, 'foo')
639640
self.assertEqual(value_pb.string_value, u'bar')
641+
640642
self.assertTrue(transaction_id is None)
641643

642644
def test_put_multi_existing_batch_w_completed_key(self):
@@ -688,6 +690,7 @@ def test_delete_multi_no_keys(self):
688690

689691
def test_delete_multi_no_batch(self):
690692
from gcloud.datastore.test_batch import _Key
693+
from gcloud.datastore.test_batch import _mutated_pb
691694

692695
key = _Key(self.PROJECT)
693696

@@ -701,7 +704,9 @@ def test_delete_multi_no_batch(self):
701704
(project,
702705
commit_req, transaction_id) = client.connection._commit_cw[0]
703706
self.assertEqual(project, self.PROJECT)
704-
self.assertEqual(list(commit_req.mutation.delete), [key.to_protobuf()])
707+
708+
mutated_key = _mutated_pb(self, commit_req.mutations, 'delete')
709+
self.assertEqual(mutated_key, key.to_protobuf())
705710
self.assertTrue(transaction_id is None)
706711

707712
def test_delete_multi_w_existing_batch(self):

gcloud/datastore/test_connection.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,8 @@ def test_commit_wo_transaction(self):
643643
key_pb = self._make_key_pb(PROJECT)
644644
rsp_pb = datastore_pb2.CommitResponse()
645645
req_pb = datastore_pb2.CommitRequest()
646-
mutation = req_pb.mutation
647-
insert = mutation.upsert.add()
646+
mutation = req_pb.mutations.add()
647+
insert = mutation.upsert
648648
insert.key.CopyFrom(key_pb)
649649
value_pb = _new_value_pb(insert, 'foo')
650650
value_pb.string_value = u'Foo'
@@ -675,7 +675,7 @@ def mock_parse(response):
675675
request = rq_class()
676676
request.ParseFromString(cw['body'])
677677
self.assertEqual(request.transaction, b'')
678-
self.assertEqual(request.mutation, mutation)
678+
self.assertEqual(list(request.mutations), [mutation])
679679
self.assertEqual(request.mode, rq_class.NON_TRANSACTIONAL)
680680
self.assertEqual(_parsed, [rsp_pb])
681681

@@ -689,8 +689,8 @@ def test_commit_w_transaction(self):
689689
key_pb = self._make_key_pb(PROJECT)
690690
rsp_pb = datastore_pb2.CommitResponse()
691691
req_pb = datastore_pb2.CommitRequest()
692-
mutation = req_pb.mutation
693-
insert = mutation.upsert.add()
692+
mutation = req_pb.mutations.add()
693+
insert = mutation.upsert
694694
insert.key.CopyFrom(key_pb)
695695
value_pb = _new_value_pb(insert, 'foo')
696696
value_pb.string_value = u'Foo'
@@ -721,7 +721,7 @@ def mock_parse(response):
721721
request = rq_class()
722722
request.ParseFromString(cw['body'])
723723
self.assertEqual(request.transaction, b'xact')
724-
self.assertEqual(request.mutation, mutation)
724+
self.assertEqual(list(request.mutations), [mutation])
725725
self.assertEqual(request.mode, rq_class.TRANSACTIONAL)
726726
self.assertEqual(_parsed, [rsp_pb])
727727

@@ -833,10 +833,10 @@ def test_it(self):
833833
),
834834
]
835835
response = datastore_pb2.CommitResponse(
836-
mutation_result=datastore_pb2.MutationResult(
837-
index_updates=index_updates,
838-
insert_auto_id_key=keys,
839-
),
836+
mutation_results=[
837+
datastore_pb2.MutationResult(key=key) for key in keys
838+
],
839+
index_updates=index_updates,
840840
)
841841
result = self._callFUT(response)
842842
self.assertEqual(result, (index_updates, keys))

gcloud/datastore/test_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def test_inverts_to_protobuf(self):
309309
array_val1.exclude_from_indexes = False
310310
array_val1.meaning = meaning = 22
311311
array_val1.blob_value = b'\xe2\x98\x83'
312-
array_val2 = val_pb4.array_value.add()
312+
array_val2 = val_pb4.array_value.values.add()
313313
array_val2.exclude_from_indexes = False
314314
array_val2.meaning = meaning
315315
array_val2.blob_value = b'\xe2\x98\x85'

gcloud/datastore/test_transaction.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ def test_ctor_defaults(self):
3535
self.assertEqual(xact.connection, connection)
3636
self.assertEqual(xact.id, None)
3737
self.assertEqual(xact._status, self._getTargetClass()._INITIAL)
38-
self.assertTrue(isinstance(xact.mutations, datastore_pb2.Mutation))
38+
self.assertTrue(isinstance(xact._commit_request,
39+
datastore_pb2.CommitRequest))
40+
self.assertTrue(xact.mutations is xact._commit_request.mutations)
3941
self.assertEqual(len(xact._partial_key_entities), 0)
4042

4143
def test_current(self):

gcloud/datastore/transaction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Transaction(Batch):
2424
or none succeed (transactionally).
2525
2626
For example, the following snippet of code will put the two ``save``
27-
operations (either ``insert_auto_id`` or ``upsert``) into the same
27+
operations (either ``insert`` or ``upsert``) into the same
2828
mutation, and execute those within a transaction::
2929
3030
>>> from gcloud import datastore

0 commit comments

Comments
 (0)