def commit(self, dataset_id, commit_request, transaction_id): """Commit mutations in context of current transation (if any). Maps the ``DatastoreService.Commit`` protobuf RPC. :type dataset_id: string :param dataset_id: The ID dataset to which the transaction applies. :type commit_request: :class:`._generated.datastore_pb2.CommitRequest` :param commit_request: The protobuf with the mutations being committed. :type transaction_id: string or None :param transaction_id: The transaction ID returned from :meth:`begin_transaction`. Non-transactional batches must pass ``None``. :rtype: tuple :returns': The pair of the number of index updates and a list of :class:`._generated.entity_pb2.Key` for each incomplete key that was completed in the commit. """ request = _datastore_pb2.CommitRequest() request.CopyFrom(commit_request) if transaction_id: request.mode = _datastore_pb2.CommitRequest.TRANSACTIONAL request.transaction = transaction_id else: request.mode = _datastore_pb2.CommitRequest.NON_TRANSACTIONAL response = self._rpc(dataset_id, 'commit', request, _datastore_pb2.CommitResponse) return _parse_commit_response(response)
def test_commit_w_transaction(self): from gcloud._testing import _Monkey from gcloud.datastore._generated import datastore_pb2 from gcloud.datastore import connection as MUT from gcloud.datastore.helpers import _new_value_pb PROJECT = 'PROJECT' key_pb = self._make_key_pb(PROJECT) rsp_pb = datastore_pb2.CommitResponse() req_pb = datastore_pb2.CommitRequest() mutation = req_pb.mutation insert = mutation.upsert.add() insert.key.CopyFrom(key_pb) value_pb = _new_value_pb(insert, 'foo') value_pb.string_value = u'Foo' conn = self._makeOne() URI = '/'.join([ conn.api_base_url, 'datastore', conn.API_VERSION, 'datasets', PROJECT, 'commit', ]) http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString()) # Set up mock for parsing the response. expected_result = object() _parsed = [] def mock_parse(response): _parsed.append(response) return expected_result with _Monkey(MUT, _parse_commit_response=mock_parse): result = conn.commit(PROJECT, req_pb, b'xact') self.assertTrue(result is expected_result) cw = http._called_with self._verifyProtobufCall(cw, URI, conn) rq_class = datastore_pb2.CommitRequest request = rq_class() request.ParseFromString(cw['body']) self.assertEqual(request.transaction, b'xact') self.assertEqual(request.mutation, mutation) self.assertEqual(request.mode, rq_class.TRANSACTIONAL) self.assertEqual(_parsed, [rsp_pb])
def __init__(self, client): self._client = client self._commit_request = _datastore_pb2.CommitRequest() self._partial_key_entities = [] self._status = self._INITIAL