Exemple #1
0
    def _commit_helper(self, transaction=None, retry=None, timeout=None):
        from google.cloud.datastore_v1.types import datastore as datastore_pb2
        from google.cloud.datastore.helpers import _new_value_pb

        project = "PROJECT"
        key_pb = _make_key_pb(project)
        rsp_pb = datastore_pb2.CommitResponse()
        req_pb = datastore_pb2.CommitRequest()
        mutation = req_pb._pb.mutations.add()
        insert = mutation.upsert
        insert.key.CopyFrom(key_pb._pb)
        value_pb = _new_value_pb(insert, "foo")
        value_pb.string_value = u"Foo"

        http = _make_requests_session(
            [_make_response(content=rsp_pb._pb.SerializeToString())])
        client_info = _make_client_info()
        client = mock.Mock(
            _http=http,
            _base_url="test.invalid",
            _client_info=client_info,
            spec=["_http", "_base_url", "_client_info"],
        )

        rq_class = datastore_pb2.CommitRequest
        ds_api = self._make_one(client)

        request = {"project_id": project, "mutations": [mutation]}

        if transaction is not None:
            request["transaction"] = transaction
            mode = request["mode"] = rq_class.Mode.TRANSACTIONAL
        else:
            mode = request["mode"] = rq_class.Mode.NON_TRANSACTIONAL

        kwargs = _make_retry_timeout_kwargs(retry, timeout, http)

        result = ds_api.commit(request=request, **kwargs)

        self.assertEqual(result, rsp_pb._pb)

        uri = _build_expected_url(client._base_url, project, "commit")
        request = _verify_protobuf_call(
            http,
            uri,
            rq_class(),
            retry=retry,
            timeout=timeout,
        )
        self.assertEqual(list(request.mutations), [mutation])
        self.assertEqual(request.mode, mode)

        if transaction is not None:
            self.assertEqual(request.transaction, transaction)
        else:
            self.assertEqual(request.transaction, b"")
    def test_commit_w_transaction(self):
        from google.cloud.datastore_v1.types import datastore as datastore_pb2
        from google.cloud.datastore.helpers import _new_value_pb

        project = "PROJECT"
        key_pb = _make_key_pb(project)
        rsp_pb = datastore_pb2.CommitResponse()
        req_pb = datastore_pb2.CommitRequest()
        mutation = req_pb._pb.mutations.add()
        insert = mutation.upsert
        insert.key.CopyFrom(key_pb._pb)
        value_pb = _new_value_pb(insert, "foo")
        value_pb.string_value = u"Foo"

        # Create mock HTTP and client with response.
        http = _make_requests_session(
            [_make_response(content=rsp_pb._pb.SerializeToString())])
        client_info = _make_client_info()
        client = mock.Mock(
            _http=http,
            _base_url="test.invalid",
            _client_info=client_info,
            spec=["_http", "_base_url", "_client_info"],
        )

        # Make request.
        rq_class = datastore_pb2.CommitRequest
        ds_api = self._make_one(client)
        mode = rq_class.Mode.TRANSACTIONAL
        result = ds_api.commit(project, mode, [mutation], transaction=b"xact")

        # Check the result and verify the callers.
        self.assertEqual(result, rsp_pb._pb)

        uri = _build_expected_url(client._base_url, project, "commit")
        request = _verify_protobuf_call(http, uri, rq_class())
        self.assertEqual(request.transaction, b"xact")
        self.assertEqual(list(request.mutations), [mutation])
        self.assertEqual(request.mode, rq_class.Mode.TRANSACTIONAL)
Exemple #3
0
    def commit(self, project_id, mode, mutations, transaction=None):
        """Perform a ``commit`` request.

        :type project_id: str
        :param project_id: The project to connect to. This is
                           usually your project name in the cloud console.

        :type mode: :class:`.gapic.datastore.v1.enums.CommitRequest.Mode`
        :param mode: The type of commit to perform. Expected to be one of
                     ``TRANSACTIONAL`` or ``NON_TRANSACTIONAL``.

        :type mutations: list
        :param mutations: List of :class:`.datastore_pb2.Mutation`, the
                          mutations to perform.

        :type transaction: bytes
        :param transaction: (Optional) The transaction ID returned from
                            :meth:`begin_transaction`.  Non-transactional
                            commits must pass :data:`None`.

        :rtype: :class:`.datastore_pb2.CommitResponse`
        :returns: The returned protobuf response object.
        """
        request_pb = _datastore_pb2.CommitRequest(
            project_id=project_id,
            mode=mode,
            transaction=transaction,
            mutations=mutations,
        )
        return _rpc(
            self.client._http,
            project_id,
            "commit",
            self.client._base_url,
            self.client._client_info,
            request_pb,
            _datastore_pb2.CommitResponse,
        )
Exemple #4
0
    async def commit(
        self,
        request: datastore.CommitRequest = None,
        *,
        project_id: str = None,
        mode: datastore.CommitRequest.Mode = None,
        transaction: bytes = None,
        mutations: Sequence[datastore.Mutation] = None,
        retry: retries.Retry = gapic_v1.method.DEFAULT,
        timeout: float = None,
        metadata: Sequence[Tuple[str, str]] = (),
    ) -> datastore.CommitResponse:
        r"""Commits a transaction, optionally creating, deleting
        or modifying some entities.

        Args:
            request (:class:`~.datastore.CommitRequest`):
                The request object. The request for
                [Datastore.Commit][google.datastore.v1.Datastore.Commit].
            project_id (:class:`str`):
                Required. The ID of the project
                against which to make the request.
                This corresponds to the ``project_id`` field
                on the ``request`` instance; if ``request`` is provided, this
                should not be set.
            mode (:class:`~.datastore.CommitRequest.Mode`):
                The type of commit to perform. Defaults to
                ``TRANSACTIONAL``.
                This corresponds to the ``mode`` field
                on the ``request`` instance; if ``request`` is provided, this
                should not be set.
            transaction (:class:`bytes`):
                The identifier of the transaction associated with the
                commit. A transaction identifier is returned by a call
                to
                [Datastore.BeginTransaction][google.datastore.v1.Datastore.BeginTransaction].
                This corresponds to the ``transaction`` field
                on the ``request`` instance; if ``request`` is provided, this
                should not be set.
            mutations (:class:`Sequence[~.datastore.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.
                This corresponds to the ``mutations`` field
                on the ``request`` instance; if ``request`` is provided, this
                should not be set.

            retry (google.api_core.retry.Retry): Designation of what errors, if any,
                should be retried.
            timeout (float): The timeout for this request.
            metadata (Sequence[Tuple[str, str]]): Strings which should be
                sent along with the request as metadata.

        Returns:
            ~.datastore.CommitResponse:
                The response for
                [Datastore.Commit][google.datastore.v1.Datastore.Commit].

        """
        # Create or coerce a protobuf request object.
        # Sanity check: If we got a request object, we should *not* have
        # gotten any keyword arguments that map to the request.
        has_flattened_params = any([project_id, mode, transaction, mutations])
        if request is not None and has_flattened_params:
            raise ValueError("If the `request` argument is set, then none of "
                             "the individual field arguments should be set.")

        request = datastore.CommitRequest(request)

        # If we have keyword arguments corresponding to fields on the
        # request, apply these.

        if project_id is not None:
            request.project_id = project_id
        if mode is not None:
            request.mode = mode
        if transaction is not None:
            request.transaction = transaction

        if mutations:
            request.mutations.extend(mutations)

        # Wrap the RPC method; this adds retry and timeout information,
        # and friendly error handling.
        rpc = gapic_v1.method_async.wrap_method(
            self._client._transport.commit,
            default_timeout=60.0,
            client_info=DEFAULT_CLIENT_INFO,
        )

        # Send the request.
        response = await rpc(
            request,
            retry=retry,
            timeout=timeout,
            metadata=metadata,
        )

        # Done; return the response.
        return response