예제 #1
0
    def allocate_ids(self, project_id, keys, options=None):
        """
        Allocates IDs for the given keys, which is useful for referencing an entity
        before it is inserted.

        Example:
          >>> from google.cloud.gapic.datastore.v1 import datastore_client
          >>> from google.cloud.proto.datastore.v1 import entity_pb2
          >>> api = datastore_client.DatastoreClient()
          >>> project_id = ''
          >>> keys = []
          >>> response = api.allocate_ids(project_id, keys)

        Args:
          project_id (string): The ID of the project against which to make the request.
          keys (list[:class:`google.cloud.proto.datastore.v1.entity_pb2.Key`]): A list of keys with incomplete key paths for which to allocate IDs.
            No key may be reserved/read-only.
          options (:class:`google.gax.CallOptions`): Overrides the default
            settings for this call, e.g, timeout, retries etc.

        Returns:
          A :class:`google.cloud.proto.datastore.v1.datastore_pb2.AllocateIdsResponse` instance.

        Raises:
          :exc:`google.gax.errors.GaxError` if the RPC is aborted.
          :exc:`ValueError` if the parameters are invalid.
        """
        # Create the request object.
        request = datastore_pb2.AllocateIdsRequest(project_id=project_id,
                                                   keys=keys)
        return self._allocate_ids(request, options)
예제 #2
0
    def test_allocate_ids_empty(self):
        from google.cloud.proto.datastore.v1 import datastore_pb2

        project = 'PROJECT'
        rsp_pb = datastore_pb2.AllocateIdsResponse()

        # Create mock HTTP and client with response.
        http = Http({'status': '200'}, rsp_pb.SerializeToString())
        client = mock.Mock(_http=http, spec=['_http'])

        # Make request.
        conn = self._make_one(client)
        response = conn.allocate_ids(project, [])

        # Check the result and verify the callers.
        self.assertEqual(list(response.keys), [])
        self.assertEqual(response, rsp_pb)
        uri = '/'.join([
            conn.api_base_url,
            conn.API_VERSION,
            'projects',
            project + ':allocateIds',
        ])
        cw = http._called_with
        self._verify_protobuf_call(cw, uri, conn)
        request = datastore_pb2.AllocateIdsRequest()
        request.ParseFromString(cw['body'])
        self.assertEqual(list(request.keys), [])
예제 #3
0
    def test_allocate_ids_empty(self):
        from google.cloud.proto.datastore.v1 import datastore_pb2

        project = 'PROJECT'
        rsp_pb = datastore_pb2.AllocateIdsResponse()

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

        # Make request.
        ds_api = self._make_one(client)
        response = ds_api.allocate_ids(project, [])

        # Check the result and verify the callers.
        self.assertEqual(response, rsp_pb)
        self.assertEqual(list(response.keys), [])

        uri = _build_expected_url(client._base_url, project, 'allocateIds')
        request = _verify_protobuf_call(http, uri,
                                        datastore_pb2.AllocateIdsRequest())
        self.assertEqual(list(request.keys), [])
    def test_allocate_ids(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = datastore_client.DatastoreClient()

        # Mock request
        project_id = 'projectId-1969970175'
        keys = []

        # Mock response
        expected_response = datastore_pb2.AllocateIdsResponse()
        grpc_stub.AllocateIds.return_value = expected_response

        response = client.allocate_ids(project_id, keys)
        self.assertEqual(expected_response, response)

        grpc_stub.AllocateIds.assert_called_once()
        args, kwargs = grpc_stub.AllocateIds.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = datastore_pb2.AllocateIdsRequest(
            project_id=project_id, keys=keys)
        self.assertEqual(expected_request, actual_request)
예제 #5
0
    def test_allocate_ids_empty(self):
        from google.cloud.proto.datastore.v1 import datastore_pb2

        project = 'PROJECT'
        rsp_pb = datastore_pb2.AllocateIdsResponse()

        # Create mock HTTP and client with response.
        http = Http({'status': '200'}, rsp_pb.SerializeToString())
        client = mock.Mock(_http=http,
                           _base_url='test.invalid',
                           spec=['_http', '_base_url'])

        # Make request.
        ds_api = self._make_one(client)
        response = ds_api.allocate_ids(project, [])

        # Check the result and verify the callers.
        self.assertEqual(response, rsp_pb)
        self.assertEqual(list(response.keys), [])
        uri = _build_expected_url(client._base_url, project, 'allocateIds')
        cw = http._called_with
        _verify_protobuf_call(self, cw, uri)
        request = datastore_pb2.AllocateIdsRequest()
        request.ParseFromString(cw['body'])
        self.assertEqual(list(request.keys), [])
예제 #6
0
    def allocate_ids(self, project, key_pbs):
        """Perform an ``allocateIds`` request.

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

        :type key_pbs: list of :class:`.entity_pb2.Key`
        :param key_pbs: The keys for which the backend should allocate IDs.

        :rtype: :class:`.datastore_pb2.AllocateIdsResponse`
        :returns: The returned protobuf response object.
        """
        request_pb = _datastore_pb2.AllocateIdsRequest(keys=key_pbs)
        return _rpc(self.client._http, project, 'allocateIds',
                    self.client._base_url,
                    request_pb, _datastore_pb2.AllocateIdsResponse)
예제 #7
0
    def allocate_ids(self, project, key_pbs):
        """Obtain backend-generated IDs for a set of keys.

        Maps the ``DatastoreService.AllocateIds`` protobuf RPC.

        :type project: str
        :param project: The project to which the transaction belongs.

        :type key_pbs: list of
                       :class:`.entity_pb2.Key`
        :param key_pbs: The keys for which the backend should allocate IDs.

        :rtype: :class:`.datastore_pb2.AllocateIdsResponse`
        :returns: The protobuf response from an allocate IDs request.
        """
        request = _datastore_pb2.AllocateIdsRequest()
        _add_keys_to_request(request.keys, key_pbs)
        return self._datastore_api.allocate_ids(project, request)
예제 #8
0
    def allocate_ids(self, project, key_pbs):
        """Obtain backend-generated IDs for a set of keys.

        Maps the ``DatastoreService.AllocateIds`` protobuf RPC.

        :type project: str
        :param project: The project to which the transaction belongs.

        :type key_pbs: list of
                       :class:`.entity_pb2.Key`
        :param key_pbs: The keys for which the backend should allocate IDs.

        :rtype: list of :class:`.entity_pb2.Key`
        :returns: An equal number of keys,  with IDs filled in by the backend.
        """
        request = _datastore_pb2.AllocateIdsRequest()
        _add_keys_to_request(request.keys, key_pbs)
        # Nothing to do with this response, so just execute the method.
        response = self._datastore_api.allocate_ids(project, request)
        return list(response.keys)
예제 #9
0
    def test_allocate_ids_non_empty(self):
        from google.cloud.proto.datastore.v1 import datastore_pb2

        project = 'PROJECT'
        before_key_pbs = [
            self._make_key_pb(project, id_=None),
            self._make_key_pb(project, id_=None),
        ]
        after_key_pbs = [
            self._make_key_pb(project),
            self._make_key_pb(project, id_=2345),
        ]
        rsp_pb = datastore_pb2.AllocateIdsResponse()
        rsp_pb.keys.add().CopyFrom(after_key_pbs[0])
        rsp_pb.keys.add().CopyFrom(after_key_pbs[1])

        # Create mock HTTP and client with response.
        http = Http({'status': '200'}, rsp_pb.SerializeToString())
        client = mock.Mock(_http=http, spec=['_http'])

        # Make request.
        conn = self._make_one(client)
        response = conn.allocate_ids(project, before_key_pbs)

        # Check the result and verify the callers.
        self.assertEqual(list(response.keys), after_key_pbs)
        self.assertEqual(response, rsp_pb)
        uri = '/'.join([
            conn.api_base_url,
            conn.API_VERSION,
            'projects',
            project + ':allocateIds',
        ])
        cw = http._called_with
        self._verify_protobuf_call(cw, uri, conn)
        request = datastore_pb2.AllocateIdsRequest()
        request.ParseFromString(cw['body'])
        self.assertEqual(len(request.keys), len(before_key_pbs))
        for key_before, key_after in zip(before_key_pbs, request.keys):
            self.assertEqual(key_before, key_after)
예제 #10
0
    def test_allocate_ids_non_empty(self):
        from google.cloud.proto.datastore.v1 import datastore_pb2

        project = 'PROJECT'
        before_key_pbs = [
            _make_key_pb(project, id_=None),
            _make_key_pb(project, id_=None),
        ]
        after_key_pbs = [
            _make_key_pb(project),
            _make_key_pb(project, id_=2345),
        ]
        rsp_pb = datastore_pb2.AllocateIdsResponse()
        rsp_pb.keys.add().CopyFrom(after_key_pbs[0])
        rsp_pb.keys.add().CopyFrom(after_key_pbs[1])

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

        # Make request.
        ds_api = self._make_one(client)
        response = ds_api.allocate_ids(project, before_key_pbs)

        # Check the result and verify the callers.
        self.assertEqual(list(response.keys), after_key_pbs)
        self.assertEqual(response, rsp_pb)

        uri = _build_expected_url(client._base_url, project, 'allocateIds')
        request = _verify_protobuf_call(http, uri,
                                        datastore_pb2.AllocateIdsRequest())
        self.assertEqual(len(request.keys), len(before_key_pbs))
        for key_before, key_after in zip(before_key_pbs, request.keys):
            self.assertEqual(key_before, key_after)