def commit(self,
               project_id,
               mode,
               mutations,
               transaction=None,
               options=None):
        """
        Commits a transaction, optionally creating, deleting or modifying some
        entities.

        Example:
          >>> from google.cloud.gapic.datastore.v1 import datastore_client
          >>> from google.cloud.gapic.datastore.v1 import enums
          >>> from google.cloud.grpc.datastore.v1 import datastore_pb2
          >>> api = datastore_client.DatastoreClient()
          >>> project_id = ''
          >>> mode = enums.CommitRequest.Mode.MODE_UNSPECIFIED
          >>> mutations = []
          >>> response = api.commit(project_id, mode, mutations)

        Args:
          project_id (string): The ID of the project against which to make the request.
          mode (enum :class:`google.cloud.gapic.datastore.v1.enums.CommitRequest.Mode`): The type of commit to perform. Defaults to ``TRANSACTIONAL``.
          transaction (bytes): The identifier of the transaction associated with the commit. A
            transaction identifier is returned by a call to
            ``Datastore.BeginTransaction``.
          mutations (list[:class:`google.cloud.grpc.datastore.v1.datastore_pb2.Mutation`]): The mutations to perform.

            When mode is ``TRANSACTIONAL``, mutations affecting a single entity are
            applied in order. The following sequences of mutations affecting a single
            entity are not permitted in a single ``Commit`` request:

            - ``insert`` followed by ``insert``
            - ``update`` followed by ``insert``
            - ``upsert`` followed by ``insert``
            - ``delete`` followed by ``update``

            When mode is ``NON_TRANSACTIONAL``, no two mutations may affect a single
            entity.
          options (:class:`google.gax.CallOptions`): Overrides the default
            settings for this call, e.g, timeout, retries etc.

        Returns:
          A :class:`google.cloud.grpc.datastore.v1.datastore_pb2.CommitResponse` instance.

        Raises:
          :exc:`google.gax.errors.GaxError` if the RPC is aborted.
          :exc:`ValueError` if the parameters are invalid.
        """
        request = datastore_pb2.CommitRequest(project_id=project_id,
                                              mode=mode,
                                              mutations=mutations,
                                              transaction=transaction)
        return self._commit(request, options)
Esempio n. 2
0
    def test_commit_w_transaction(self):
        import mock
        from google.cloud.grpc.datastore.v1 import datastore_pb2
        from google.cloud.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.mutations.add()
        insert = mutation.upsert
        insert.key.CopyFrom(key_pb)
        value_pb = _new_value_pb(insert, 'foo')
        value_pb.string_value = u'Foo'
        conn = self._make_one()
        URI = '/'.join([
            conn.api_base_url,
            conn.API_VERSION,
            'projects',
            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

        patch = mock.patch(
            'google.cloud.datastore._http._parse_commit_response',
            new=mock_parse)
        with patch:
            result = conn.commit(PROJECT, req_pb, b'xact')

        self.assertIs(result, 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(list(request.mutations), [mutation])
        self.assertEqual(request.mode, rq_class.TRANSACTIONAL)
        self.assertEqual(_parsed, [rsp_pb])
Esempio n. 3
0
 def __init__(self, client):
     self._client = client
     self._commit_request = _datastore_pb2.CommitRequest()
     self._partial_key_entities = []
     self._status = self._INITIAL