Example #1
0
    def test_import_documents(self):
        # Setup Expected Response
        name_2 = "name2-1052831874"
        done = True
        expected_response = {"name": name_2, "done": done}
        expected_response = operations_pb2.Operation(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = firestore_admin_v1.FirestoreAdminClient()

        # Setup Request
        name = client.database_path("[PROJECT]", "[DATABASE]")

        response = client.import_documents(name)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = firestore_admin_pb2.ImportDocumentsRequest(
            name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_import_documents(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = empty_pb2.Empty(**expected_response)
        operation = operations_pb2.Operation(
            name="operations/test_import_documents", done=True)
        operation.response.Pack(expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[operation])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = firestore_admin_v1.FirestoreAdminClient()

        # Setup Request
        name = client.database_path("[PROJECT]", "[DATABASE]")

        response = client.import_documents(name)
        result = response.result()
        assert expected_response == result

        assert len(channel.requests) == 1
        expected_request = firestore_admin_pb2.ImportDocumentsRequest(
            name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def import_documents(
        self,
        name,
        collection_ids=None,
        input_uri_prefix=None,
        retry=google.api_core.gapic_v1.method.DEFAULT,
        timeout=google.api_core.gapic_v1.method.DEFAULT,
        metadata=None,
    ):
        """
        Imports documents into Google Cloud Firestore. Existing documents with the
        same name are overwritten. The import occurs in the background and its
        progress can be monitored and managed via the Operation resource that is
        created. If an ImportDocuments operation is cancelled, it is possible
        that a subset of the data has already been imported to Cloud Firestore.

        Example:
            >>> from google.cloud import firestore_admin_v1
            >>>
            >>> client = firestore_admin_v1.FirestoreAdminClient()
            >>>
            >>> name = client.database_path('[PROJECT]', '[DATABASE]')
            >>>
            >>> response = client.import_documents(name)

        Args:
            name (str): Required. Database to import into. Should be of the form:
                ``projects/{project_id}/databases/{database_id}``.
            collection_ids (list[str]): Which collection ids to import. Unspecified means all collections included
                in the import.
            input_uri_prefix (str): Location of the exported files. This must match the output\_uri\_prefix
                of an ExportDocumentsResponse from an export that has completed
                successfully. See:
                ``google.firestore.admin.v1.ExportDocumentsResponse.output_uri_prefix``.
            retry (Optional[google.api_core.retry.Retry]):  A retry object used
                to retry requests. If ``None`` is specified, requests will
                be retried using a default configuration.
            timeout (Optional[float]): The amount of time, in seconds, to wait
                for the request to complete. Note that if ``retry`` is
                specified, the timeout applies to each individual attempt.
            metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata
                that is provided to the method.

        Returns:
            A :class:`~google.cloud.firestore_admin_v1.types.Operation` instance.

        Raises:
            google.api_core.exceptions.GoogleAPICallError: If the request
                    failed for any reason.
            google.api_core.exceptions.RetryError: If the request failed due
                    to a retryable error and retry attempts failed.
            ValueError: If the parameters are invalid.
        """
        # Wrap the transport method to add retry and timeout logic.
        if "import_documents" not in self._inner_api_calls:
            self._inner_api_calls[
                "import_documents"] = google.api_core.gapic_v1.method.wrap_method(
                    self.transport.import_documents,
                    default_retry=self._method_configs["ImportDocuments"].
                    retry,
                    default_timeout=self._method_configs["ImportDocuments"].
                    timeout,
                    client_info=self._client_info,
                )

        request = firestore_admin_pb2.ImportDocumentsRequest(
            name=name,
            collection_ids=collection_ids,
            input_uri_prefix=input_uri_prefix)
        if metadata is None:
            metadata = []
        metadata = list(metadata)
        try:
            routing_header = [("name", name)]
        except AttributeError:
            pass
        else:
            routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata(
                routing_header)
            metadata.append(routing_metadata)

        return self._inner_api_calls["import_documents"](request,
                                                         retry=retry,
                                                         timeout=timeout,
                                                         metadata=metadata)