From 6ff81aa41059d61105f33c94ae9186357a1201c5 Mon Sep 17 00:00:00 2001 From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Date: Tue, 21 Feb 2023 12:22:59 -0800 Subject: [PATCH] feat: Added Training and Evaluation functions, request, responses and metadata to document_processor_service.proto (#463) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: Added Training and Evaluation functions, request, responses and metadata to document_processor_service.proto feat: Added evaluation.proto feat: Added latest_evaluation to processor.proto chore: removed deprecated flag from REPLACE in OperationType in document.proto PiperOrigin-RevId: 511230520 Source-Link: https://github.com/googleapis/googleapis/commit/c53bf8d535e342737aa479cbe2cdead26507cdf4 Source-Link: https://github.com/googleapis/googleapis-gen/commit/5693cbc98b6412085ff6d3aba719b68e307c5357 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiNTY5M2NiYzk4YjY0MTIwODVmZjZkM2FiYTcxOWI2OGUzMDdjNTM1NyJ9 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot --- google/cloud/documentai/__init__.py | 21 + google/cloud/documentai_v1/__init__.py | 21 + .../cloud/documentai_v1/gapic_metadata.json | 60 + .../async_client.py | 484 ++ .../document_processor_service/client.py | 514 ++ .../document_processor_service/pagers.py | 133 + .../transports/base.py | 61 +- .../transports/grpc.py | 120 +- .../transports/grpc_asyncio.py | 121 +- .../transports/rest.py | 562 +- google/cloud/documentai_v1/types/__init__.py | 21 + google/cloud/documentai_v1/types/document.py | 31 +- .../types/document_processor_service.py | 300 + .../documentai_v1/types/document_schema.py | 20 +- .../cloud/documentai_v1/types/evaluation.py | 345 ++ google/cloud/documentai_v1/types/processor.py | 9 + ...ervice_evaluate_processor_version_async.py | 56 + ...service_evaluate_processor_version_sync.py | 56 + ..._processor_service_get_evaluation_async.py | 52 + ...t_processor_service_get_evaluation_sync.py | 52 + ...rocessor_service_list_evaluations_async.py | 53 + ...processor_service_list_evaluations_sync.py | 53 + ...r_service_train_processor_version_async.py | 56 + ...or_service_train_processor_version_sync.py | 56 + ...t_metadata_google.cloud.documentai.v1.json | 782 ++- ...adata_google.cloud.documentai.v1beta2.json | 2 +- ...adata_google.cloud.documentai.v1beta3.json | 2 +- scripts/fixup_documentai_v1_keywords.py | 4 + .../test_document_processor_service.py | 4984 ++++++++++++----- 29 files changed, 7650 insertions(+), 1381 deletions(-) create mode 100644 google/cloud/documentai_v1/types/evaluation.py create mode 100644 samples/generated_samples/documentai_v1_generated_document_processor_service_evaluate_processor_version_async.py create mode 100644 samples/generated_samples/documentai_v1_generated_document_processor_service_evaluate_processor_version_sync.py create mode 100644 samples/generated_samples/documentai_v1_generated_document_processor_service_get_evaluation_async.py create mode 100644 samples/generated_samples/documentai_v1_generated_document_processor_service_get_evaluation_sync.py create mode 100644 samples/generated_samples/documentai_v1_generated_document_processor_service_list_evaluations_async.py create mode 100644 samples/generated_samples/documentai_v1_generated_document_processor_service_list_evaluations_sync.py create mode 100644 samples/generated_samples/documentai_v1_generated_document_processor_service_train_processor_version_async.py create mode 100644 samples/generated_samples/documentai_v1_generated_document_processor_service_train_processor_version_sync.py diff --git a/google/cloud/documentai/__init__.py b/google/cloud/documentai/__init__.py index 29407c4c..fcc932dc 100644 --- a/google/cloud/documentai/__init__.py +++ b/google/cloud/documentai/__init__.py @@ -52,12 +52,18 @@ EnableProcessorMetadata, EnableProcessorRequest, EnableProcessorResponse, + EvaluateProcessorVersionMetadata, + EvaluateProcessorVersionRequest, + EvaluateProcessorVersionResponse, FetchProcessorTypesRequest, FetchProcessorTypesResponse, + GetEvaluationRequest, GetProcessorRequest, GetProcessorTypeRequest, GetProcessorVersionRequest, HumanReviewStatus, + ListEvaluationsRequest, + ListEvaluationsResponse, ListProcessorsRequest, ListProcessorsResponse, ListProcessorTypesRequest, @@ -72,11 +78,15 @@ SetDefaultProcessorVersionMetadata, SetDefaultProcessorVersionRequest, SetDefaultProcessorVersionResponse, + TrainProcessorVersionMetadata, + TrainProcessorVersionRequest, + TrainProcessorVersionResponse, UndeployProcessorVersionMetadata, UndeployProcessorVersionRequest, UndeployProcessorVersionResponse, ) from google.cloud.documentai_v1.types.document_schema import DocumentSchema +from google.cloud.documentai_v1.types.evaluation import Evaluation, EvaluationReference from google.cloud.documentai_v1.types.geometry import ( BoundingPoly, NormalizedVertex, @@ -114,12 +124,18 @@ "EnableProcessorMetadata", "EnableProcessorRequest", "EnableProcessorResponse", + "EvaluateProcessorVersionMetadata", + "EvaluateProcessorVersionRequest", + "EvaluateProcessorVersionResponse", "FetchProcessorTypesRequest", "FetchProcessorTypesResponse", + "GetEvaluationRequest", "GetProcessorRequest", "GetProcessorTypeRequest", "GetProcessorVersionRequest", "HumanReviewStatus", + "ListEvaluationsRequest", + "ListEvaluationsResponse", "ListProcessorsRequest", "ListProcessorsResponse", "ListProcessorTypesRequest", @@ -134,10 +150,15 @@ "SetDefaultProcessorVersionMetadata", "SetDefaultProcessorVersionRequest", "SetDefaultProcessorVersionResponse", + "TrainProcessorVersionMetadata", + "TrainProcessorVersionRequest", + "TrainProcessorVersionResponse", "UndeployProcessorVersionMetadata", "UndeployProcessorVersionRequest", "UndeployProcessorVersionResponse", "DocumentSchema", + "Evaluation", + "EvaluationReference", "BoundingPoly", "NormalizedVertex", "Vertex", diff --git a/google/cloud/documentai_v1/__init__.py b/google/cloud/documentai_v1/__init__.py index de139074..dc26842d 100644 --- a/google/cloud/documentai_v1/__init__.py +++ b/google/cloud/documentai_v1/__init__.py @@ -50,12 +50,18 @@ EnableProcessorMetadata, EnableProcessorRequest, EnableProcessorResponse, + EvaluateProcessorVersionMetadata, + EvaluateProcessorVersionRequest, + EvaluateProcessorVersionResponse, FetchProcessorTypesRequest, FetchProcessorTypesResponse, + GetEvaluationRequest, GetProcessorRequest, GetProcessorTypeRequest, GetProcessorVersionRequest, HumanReviewStatus, + ListEvaluationsRequest, + ListEvaluationsResponse, ListProcessorsRequest, ListProcessorsResponse, ListProcessorTypesRequest, @@ -70,11 +76,15 @@ SetDefaultProcessorVersionMetadata, SetDefaultProcessorVersionRequest, SetDefaultProcessorVersionResponse, + TrainProcessorVersionMetadata, + TrainProcessorVersionRequest, + TrainProcessorVersionResponse, UndeployProcessorVersionMetadata, UndeployProcessorVersionRequest, UndeployProcessorVersionResponse, ) from .types.document_schema import DocumentSchema +from .types.evaluation import Evaluation, EvaluationReference from .types.geometry import BoundingPoly, NormalizedVertex, Vertex from .types.operation_metadata import CommonOperationMetadata from .types.processor import Processor, ProcessorVersion @@ -107,15 +117,23 @@ "EnableProcessorMetadata", "EnableProcessorRequest", "EnableProcessorResponse", + "EvaluateProcessorVersionMetadata", + "EvaluateProcessorVersionRequest", + "EvaluateProcessorVersionResponse", + "Evaluation", + "EvaluationReference", "FetchProcessorTypesRequest", "FetchProcessorTypesResponse", "GcsDocument", "GcsDocuments", "GcsPrefix", + "GetEvaluationRequest", "GetProcessorRequest", "GetProcessorTypeRequest", "GetProcessorVersionRequest", "HumanReviewStatus", + "ListEvaluationsRequest", + "ListEvaluationsResponse", "ListProcessorTypesRequest", "ListProcessorTypesResponse", "ListProcessorVersionsRequest", @@ -135,6 +153,9 @@ "SetDefaultProcessorVersionMetadata", "SetDefaultProcessorVersionRequest", "SetDefaultProcessorVersionResponse", + "TrainProcessorVersionMetadata", + "TrainProcessorVersionRequest", + "TrainProcessorVersionResponse", "UndeployProcessorVersionMetadata", "UndeployProcessorVersionRequest", "UndeployProcessorVersionResponse", diff --git a/google/cloud/documentai_v1/gapic_metadata.json b/google/cloud/documentai_v1/gapic_metadata.json index 2b34dcff..863c39a3 100644 --- a/google/cloud/documentai_v1/gapic_metadata.json +++ b/google/cloud/documentai_v1/gapic_metadata.json @@ -45,11 +45,21 @@ "enable_processor" ] }, + "EvaluateProcessorVersion": { + "methods": [ + "evaluate_processor_version" + ] + }, "FetchProcessorTypes": { "methods": [ "fetch_processor_types" ] }, + "GetEvaluation": { + "methods": [ + "get_evaluation" + ] + }, "GetProcessor": { "methods": [ "get_processor" @@ -65,6 +75,11 @@ "get_processor_version" ] }, + "ListEvaluations": { + "methods": [ + "list_evaluations" + ] + }, "ListProcessorTypes": { "methods": [ "list_processor_types" @@ -95,6 +110,11 @@ "set_default_processor_version" ] }, + "TrainProcessorVersion": { + "methods": [ + "train_processor_version" + ] + }, "UndeployProcessorVersion": { "methods": [ "undeploy_processor_version" @@ -140,11 +160,21 @@ "enable_processor" ] }, + "EvaluateProcessorVersion": { + "methods": [ + "evaluate_processor_version" + ] + }, "FetchProcessorTypes": { "methods": [ "fetch_processor_types" ] }, + "GetEvaluation": { + "methods": [ + "get_evaluation" + ] + }, "GetProcessor": { "methods": [ "get_processor" @@ -160,6 +190,11 @@ "get_processor_version" ] }, + "ListEvaluations": { + "methods": [ + "list_evaluations" + ] + }, "ListProcessorTypes": { "methods": [ "list_processor_types" @@ -190,6 +225,11 @@ "set_default_processor_version" ] }, + "TrainProcessorVersion": { + "methods": [ + "train_processor_version" + ] + }, "UndeployProcessorVersion": { "methods": [ "undeploy_processor_version" @@ -235,11 +275,21 @@ "enable_processor" ] }, + "EvaluateProcessorVersion": { + "methods": [ + "evaluate_processor_version" + ] + }, "FetchProcessorTypes": { "methods": [ "fetch_processor_types" ] }, + "GetEvaluation": { + "methods": [ + "get_evaluation" + ] + }, "GetProcessor": { "methods": [ "get_processor" @@ -255,6 +305,11 @@ "get_processor_version" ] }, + "ListEvaluations": { + "methods": [ + "list_evaluations" + ] + }, "ListProcessorTypes": { "methods": [ "list_processor_types" @@ -285,6 +340,11 @@ "set_default_processor_version" ] }, + "TrainProcessorVersion": { + "methods": [ + "train_processor_version" + ] + }, "UndeployProcessorVersion": { "methods": [ "undeploy_processor_version" diff --git a/google/cloud/documentai_v1/services/document_processor_service/async_client.py b/google/cloud/documentai_v1/services/document_processor_service/async_client.py index 4a28256a..2360e046 100644 --- a/google/cloud/documentai_v1/services/document_processor_service/async_client.py +++ b/google/cloud/documentai_v1/services/document_processor_service/async_client.py @@ -55,6 +55,7 @@ document, document_processor_service, document_schema, + evaluation, ) from google.cloud.documentai_v1.types import processor from google.cloud.documentai_v1.types import processor as gcd_processor @@ -78,6 +79,10 @@ class DocumentProcessorServiceAsyncClient: DEFAULT_ENDPOINT = DocumentProcessorServiceClient.DEFAULT_ENDPOINT DEFAULT_MTLS_ENDPOINT = DocumentProcessorServiceClient.DEFAULT_MTLS_ENDPOINT + evaluation_path = staticmethod(DocumentProcessorServiceClient.evaluation_path) + parse_evaluation_path = staticmethod( + DocumentProcessorServiceClient.parse_evaluation_path + ) human_review_config_path = staticmethod( DocumentProcessorServiceClient.human_review_config_path ) @@ -1074,6 +1079,137 @@ async def sample_get_processor(): # Done; return the response. return response + async def train_processor_version( + self, + request: Optional[ + Union[document_processor_service.TrainProcessorVersionRequest, dict] + ] = None, + *, + parent: Optional[str] = None, + processor_version: Optional[processor.ProcessorVersion] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operation_async.AsyncOperation: + r"""Trains a new processor version. Operation metadata is returned + as cloud_documentai_core.TrainProcessorVersionMetadata. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import documentai_v1 + + async def sample_train_processor_version(): + # Create a client + client = documentai_v1.DocumentProcessorServiceAsyncClient() + + # Initialize request argument(s) + request = documentai_v1.TrainProcessorVersionRequest( + parent="parent_value", + ) + + # Make the request + operation = client.train_processor_version(request=request) + + print("Waiting for operation to complete...") + + response = (await operation).result() + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.documentai_v1.types.TrainProcessorVersionRequest, dict]]): + The request object. Request message for the create + processor version method. + parent (:class:`str`): + Required. The parent (project, location and processor) + to create the new version for. Format: + ``projects/{project}/locations/{location}/processors/{processor}``. + + This corresponds to the ``parent`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + processor_version (:class:`google.cloud.documentai_v1.types.ProcessorVersion`): + Required. The processor version to be + created. + + This corresponds to the ``processor_version`` 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: + google.api_core.operation_async.AsyncOperation: + An object representing a long-running operation. + + The result type for the operation will be + :class:`google.cloud.documentai_v1.types.TrainProcessorVersionResponse` + The response for the TrainProcessorVersion method. + + """ + # Create or coerce a protobuf request object. + # Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([parent, processor_version]) + 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 = document_processor_service.TrainProcessorVersionRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if parent is not None: + request.parent = parent + if processor_version is not None: + request.processor_version = processor_version + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = gapic_v1.method_async.wrap_method( + self._client._transport.train_processor_version, + default_timeout=None, + client_info=DEFAULT_CLIENT_INFO, + ) + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), + ) + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Wrap the response in an operation future. + response = operation_async.from_gapic( + response, + self._client._transport.operations_client, + document_processor_service.TrainProcessorVersionResponse, + metadata_type=document_processor_service.TrainProcessorVersionMetadata, + ) + + # Done; return the response. + return response + async def get_processor_version( self, request: Optional[ @@ -2356,6 +2492,354 @@ async def sample_review_document(): # Done; return the response. return response + async def evaluate_processor_version( + self, + request: Optional[ + Union[document_processor_service.EvaluateProcessorVersionRequest, dict] + ] = None, + *, + processor_version: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operation_async.AsyncOperation: + r"""Evaluates a ProcessorVersion against annotated + documents, producing an Evaluation. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import documentai_v1 + + async def sample_evaluate_processor_version(): + # Create a client + client = documentai_v1.DocumentProcessorServiceAsyncClient() + + # Initialize request argument(s) + request = documentai_v1.EvaluateProcessorVersionRequest( + processor_version="processor_version_value", + ) + + # Make the request + operation = client.evaluate_processor_version(request=request) + + print("Waiting for operation to complete...") + + response = (await operation).result() + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.documentai_v1.types.EvaluateProcessorVersionRequest, dict]]): + The request object. Evaluates the given ProcessorVersion + against the supplied documents. + processor_version (:class:`str`): + Required. The resource name of the + [ProcessorVersion][google.cloud.documentai.v1.ProcessorVersion] + to evaluate. + ``projects/{project}/locations/{location}/processors/{processor}/processorVersions/{processorVersion}`` + + This corresponds to the ``processor_version`` 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: + google.api_core.operation_async.AsyncOperation: + An object representing a long-running operation. + + The result type for the operation will be + :class:`google.cloud.documentai_v1.types.EvaluateProcessorVersionResponse` + Metadata of the EvaluateProcessorVersion method. + + """ + # Create or coerce a protobuf request object. + # Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([processor_version]) + 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 = document_processor_service.EvaluateProcessorVersionRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if processor_version is not None: + request.processor_version = processor_version + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = gapic_v1.method_async.wrap_method( + self._client._transport.evaluate_processor_version, + default_timeout=None, + client_info=DEFAULT_CLIENT_INFO, + ) + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata( + (("processor_version", request.processor_version),) + ), + ) + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Wrap the response in an operation future. + response = operation_async.from_gapic( + response, + self._client._transport.operations_client, + document_processor_service.EvaluateProcessorVersionResponse, + metadata_type=document_processor_service.EvaluateProcessorVersionMetadata, + ) + + # Done; return the response. + return response + + async def get_evaluation( + self, + request: Optional[ + Union[document_processor_service.GetEvaluationRequest, dict] + ] = None, + *, + name: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, str]] = (), + ) -> evaluation.Evaluation: + r"""Retrieves a specific evaluation. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import documentai_v1 + + async def sample_get_evaluation(): + # Create a client + client = documentai_v1.DocumentProcessorServiceAsyncClient() + + # Initialize request argument(s) + request = documentai_v1.GetEvaluationRequest( + name="name_value", + ) + + # Make the request + response = await client.get_evaluation(request=request) + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.documentai_v1.types.GetEvaluationRequest, dict]]): + The request object. Retrieves a specific Evaluation. + name (:class:`str`): + Required. The resource name of the + [Evaluation][google.cloud.documentai.v1.Evaluation] to + get. + ``projects/{project}/locations/{location}/processors/{processor}/processorVersions/{processorVersion}/evaluations/{evaluation}`` + + This corresponds to the ``name`` 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: + google.cloud.documentai_v1.types.Evaluation: + An evaluation of a ProcessorVersion's + performance. + + """ + # Create or coerce a protobuf request object. + # Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([name]) + 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 = document_processor_service.GetEvaluationRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if name is not None: + request.name = name + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = gapic_v1.method_async.wrap_method( + self._client._transport.get_evaluation, + default_timeout=None, + client_info=DEFAULT_CLIENT_INFO, + ) + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + async def list_evaluations( + self, + request: Optional[ + Union[document_processor_service.ListEvaluationsRequest, dict] + ] = None, + *, + parent: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, str]] = (), + ) -> pagers.ListEvaluationsAsyncPager: + r"""Retrieves a set of evaluations for a given processor + version. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import documentai_v1 + + async def sample_list_evaluations(): + # Create a client + client = documentai_v1.DocumentProcessorServiceAsyncClient() + + # Initialize request argument(s) + request = documentai_v1.ListEvaluationsRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_evaluations(request=request) + + # Handle the response + async for response in page_result: + print(response) + + Args: + request (Optional[Union[google.cloud.documentai_v1.types.ListEvaluationsRequest, dict]]): + The request object. Retrieves a list of evaluations for + a given ProcessorVersion. + parent (:class:`str`): + Required. The resource name of the + [ProcessorVersion][google.cloud.documentai.v1.ProcessorVersion] + to list evaluations for. + ``projects/{project}/locations/{location}/processors/{processor}/processorVersions/{processorVersion}`` + + This corresponds to the ``parent`` 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: + google.cloud.documentai_v1.services.document_processor_service.pagers.ListEvaluationsAsyncPager: + The response from ListEvaluations. + Iterating over this object will yield + results and resolve additional pages + automatically. + + """ + # Create or coerce a protobuf request object. + # Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([parent]) + 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 = document_processor_service.ListEvaluationsRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if parent is not None: + request.parent = parent + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = gapic_v1.method_async.wrap_method( + self._client._transport.list_evaluations, + default_timeout=None, + client_info=DEFAULT_CLIENT_INFO, + ) + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), + ) + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # This method is paged; wrap the response in a pager, which provides + # an `__aiter__` convenience method. + response = pagers.ListEvaluationsAsyncPager( + method=rpc, + request=request, + response=response, + metadata=metadata, + ) + + # Done; return the response. + return response + async def list_operations( self, request: Optional[operations_pb2.ListOperationsRequest] = None, diff --git a/google/cloud/documentai_v1/services/document_processor_service/client.py b/google/cloud/documentai_v1/services/document_processor_service/client.py index b541d7a8..e5c7e883 100644 --- a/google/cloud/documentai_v1/services/document_processor_service/client.py +++ b/google/cloud/documentai_v1/services/document_processor_service/client.py @@ -59,6 +59,7 @@ document, document_processor_service, document_schema, + evaluation, ) from google.cloud.documentai_v1.types import processor from google.cloud.documentai_v1.types import processor as gcd_processor @@ -197,6 +198,32 @@ def transport(self) -> DocumentProcessorServiceTransport: """ return self._transport + @staticmethod + def evaluation_path( + project: str, + location: str, + processor: str, + processor_version: str, + evaluation: str, + ) -> str: + """Returns a fully-qualified evaluation string.""" + return "projects/{project}/locations/{location}/processors/{processor}/processorVersions/{processor_version}/evaluations/{evaluation}".format( + project=project, + location=location, + processor=processor, + processor_version=processor_version, + evaluation=evaluation, + ) + + @staticmethod + def parse_evaluation_path(path: str) -> Dict[str, str]: + """Parses a evaluation path into its component segments.""" + m = re.match( + r"^projects/(?P.+?)/locations/(?P.+?)/processors/(?P.+?)/processorVersions/(?P.+?)/evaluations/(?P.+?)$", + path, + ) + return m.groupdict() if m else {} + @staticmethod def human_review_config_path( project: str, @@ -1330,6 +1357,139 @@ def sample_get_processor(): # Done; return the response. return response + def train_processor_version( + self, + request: Optional[ + Union[document_processor_service.TrainProcessorVersionRequest, dict] + ] = None, + *, + parent: Optional[str] = None, + processor_version: Optional[processor.ProcessorVersion] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operation.Operation: + r"""Trains a new processor version. Operation metadata is returned + as cloud_documentai_core.TrainProcessorVersionMetadata. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import documentai_v1 + + def sample_train_processor_version(): + # Create a client + client = documentai_v1.DocumentProcessorServiceClient() + + # Initialize request argument(s) + request = documentai_v1.TrainProcessorVersionRequest( + parent="parent_value", + ) + + # Make the request + operation = client.train_processor_version(request=request) + + print("Waiting for operation to complete...") + + response = operation.result() + + # Handle the response + print(response) + + Args: + request (Union[google.cloud.documentai_v1.types.TrainProcessorVersionRequest, dict]): + The request object. Request message for the create + processor version method. + parent (str): + Required. The parent (project, location and processor) + to create the new version for. Format: + ``projects/{project}/locations/{location}/processors/{processor}``. + + This corresponds to the ``parent`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + processor_version (google.cloud.documentai_v1.types.ProcessorVersion): + Required. The processor version to be + created. + + This corresponds to the ``processor_version`` 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: + google.api_core.operation.Operation: + An object representing a long-running operation. + + The result type for the operation will be + :class:`google.cloud.documentai_v1.types.TrainProcessorVersionResponse` + The response for the TrainProcessorVersion method. + + """ + # Create or coerce a protobuf request object. + # Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([parent, processor_version]) + 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." + ) + + # Minor optimization to avoid making a copy if the user passes + # in a document_processor_service.TrainProcessorVersionRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance( + request, document_processor_service.TrainProcessorVersionRequest + ): + request = document_processor_service.TrainProcessorVersionRequest(request) + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if parent is not None: + request.parent = parent + if processor_version is not None: + request.processor_version = processor_version + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[self._transport.train_processor_version] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), + ) + + # Send the request. + response = rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Wrap the response in an operation future. + response = operation.from_gapic( + response, + self._transport.operations_client, + document_processor_service.TrainProcessorVersionResponse, + metadata_type=document_processor_service.TrainProcessorVersionMetadata, + ) + + # Done; return the response. + return response + def get_processor_version( self, request: Optional[ @@ -2625,6 +2785,360 @@ def sample_review_document(): # Done; return the response. return response + def evaluate_processor_version( + self, + request: Optional[ + Union[document_processor_service.EvaluateProcessorVersionRequest, dict] + ] = None, + *, + processor_version: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operation.Operation: + r"""Evaluates a ProcessorVersion against annotated + documents, producing an Evaluation. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import documentai_v1 + + def sample_evaluate_processor_version(): + # Create a client + client = documentai_v1.DocumentProcessorServiceClient() + + # Initialize request argument(s) + request = documentai_v1.EvaluateProcessorVersionRequest( + processor_version="processor_version_value", + ) + + # Make the request + operation = client.evaluate_processor_version(request=request) + + print("Waiting for operation to complete...") + + response = operation.result() + + # Handle the response + print(response) + + Args: + request (Union[google.cloud.documentai_v1.types.EvaluateProcessorVersionRequest, dict]): + The request object. Evaluates the given ProcessorVersion + against the supplied documents. + processor_version (str): + Required. The resource name of the + [ProcessorVersion][google.cloud.documentai.v1.ProcessorVersion] + to evaluate. + ``projects/{project}/locations/{location}/processors/{processor}/processorVersions/{processorVersion}`` + + This corresponds to the ``processor_version`` 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: + google.api_core.operation.Operation: + An object representing a long-running operation. + + The result type for the operation will be + :class:`google.cloud.documentai_v1.types.EvaluateProcessorVersionResponse` + Metadata of the EvaluateProcessorVersion method. + + """ + # Create or coerce a protobuf request object. + # Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([processor_version]) + 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." + ) + + # Minor optimization to avoid making a copy if the user passes + # in a document_processor_service.EvaluateProcessorVersionRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance( + request, document_processor_service.EvaluateProcessorVersionRequest + ): + request = document_processor_service.EvaluateProcessorVersionRequest( + request + ) + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if processor_version is not None: + request.processor_version = processor_version + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[ + self._transport.evaluate_processor_version + ] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata( + (("processor_version", request.processor_version),) + ), + ) + + # Send the request. + response = rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Wrap the response in an operation future. + response = operation.from_gapic( + response, + self._transport.operations_client, + document_processor_service.EvaluateProcessorVersionResponse, + metadata_type=document_processor_service.EvaluateProcessorVersionMetadata, + ) + + # Done; return the response. + return response + + def get_evaluation( + self, + request: Optional[ + Union[document_processor_service.GetEvaluationRequest, dict] + ] = None, + *, + name: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, str]] = (), + ) -> evaluation.Evaluation: + r"""Retrieves a specific evaluation. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import documentai_v1 + + def sample_get_evaluation(): + # Create a client + client = documentai_v1.DocumentProcessorServiceClient() + + # Initialize request argument(s) + request = documentai_v1.GetEvaluationRequest( + name="name_value", + ) + + # Make the request + response = client.get_evaluation(request=request) + + # Handle the response + print(response) + + Args: + request (Union[google.cloud.documentai_v1.types.GetEvaluationRequest, dict]): + The request object. Retrieves a specific Evaluation. + name (str): + Required. The resource name of the + [Evaluation][google.cloud.documentai.v1.Evaluation] to + get. + ``projects/{project}/locations/{location}/processors/{processor}/processorVersions/{processorVersion}/evaluations/{evaluation}`` + + This corresponds to the ``name`` 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: + google.cloud.documentai_v1.types.Evaluation: + An evaluation of a ProcessorVersion's + performance. + + """ + # Create or coerce a protobuf request object. + # Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([name]) + 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." + ) + + # Minor optimization to avoid making a copy if the user passes + # in a document_processor_service.GetEvaluationRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, document_processor_service.GetEvaluationRequest): + request = document_processor_service.GetEvaluationRequest(request) + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if name is not None: + request.name = name + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[self._transport.get_evaluation] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)), + ) + + # Send the request. + response = rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + + def list_evaluations( + self, + request: Optional[ + Union[document_processor_service.ListEvaluationsRequest, dict] + ] = None, + *, + parent: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, str]] = (), + ) -> pagers.ListEvaluationsPager: + r"""Retrieves a set of evaluations for a given processor + version. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import documentai_v1 + + def sample_list_evaluations(): + # Create a client + client = documentai_v1.DocumentProcessorServiceClient() + + # Initialize request argument(s) + request = documentai_v1.ListEvaluationsRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_evaluations(request=request) + + # Handle the response + for response in page_result: + print(response) + + Args: + request (Union[google.cloud.documentai_v1.types.ListEvaluationsRequest, dict]): + The request object. Retrieves a list of evaluations for + a given ProcessorVersion. + parent (str): + Required. The resource name of the + [ProcessorVersion][google.cloud.documentai.v1.ProcessorVersion] + to list evaluations for. + ``projects/{project}/locations/{location}/processors/{processor}/processorVersions/{processorVersion}`` + + This corresponds to the ``parent`` 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: + google.cloud.documentai_v1.services.document_processor_service.pagers.ListEvaluationsPager: + The response from ListEvaluations. + Iterating over this object will yield + results and resolve additional pages + automatically. + + """ + # Create or coerce a protobuf request object. + # Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([parent]) + 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." + ) + + # Minor optimization to avoid making a copy if the user passes + # in a document_processor_service.ListEvaluationsRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, document_processor_service.ListEvaluationsRequest): + request = document_processor_service.ListEvaluationsRequest(request) + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if parent is not None: + request.parent = parent + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[self._transport.list_evaluations] + + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), + ) + + # Send the request. + response = rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListEvaluationsPager( + method=rpc, + request=request, + response=response, + metadata=metadata, + ) + + # Done; return the response. + return response + def __enter__(self) -> "DocumentProcessorServiceClient": return self diff --git a/google/cloud/documentai_v1/services/document_processor_service/pagers.py b/google/cloud/documentai_v1/services/document_processor_service/pagers.py index bbe91706..c38279ba 100644 --- a/google/cloud/documentai_v1/services/document_processor_service/pagers.py +++ b/google/cloud/documentai_v1/services/document_processor_service/pagers.py @@ -26,6 +26,7 @@ from google.cloud.documentai_v1.types import ( document_processor_service, + evaluation, processor, processor_type, ) @@ -427,3 +428,135 @@ async def async_generator(): def __repr__(self) -> str: return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListEvaluationsPager: + """A pager for iterating through ``list_evaluations`` requests. + + This class thinly wraps an initial + :class:`google.cloud.documentai_v1.types.ListEvaluationsResponse` object, and + provides an ``__iter__`` method to iterate through its + ``evaluations`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``ListEvaluations`` requests and continue to iterate + through the ``evaluations`` field on the + corresponding responses. + + All the usual :class:`google.cloud.documentai_v1.types.ListEvaluationsResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., document_processor_service.ListEvaluationsResponse], + request: document_processor_service.ListEvaluationsRequest, + response: document_processor_service.ListEvaluationsResponse, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.documentai_v1.types.ListEvaluationsRequest): + The initial request object. + response (google.cloud.documentai_v1.types.ListEvaluationsResponse): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = document_processor_service.ListEvaluationsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterator[document_processor_service.ListEvaluationsResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterator[evaluation.Evaluation]: + for page in self.pages: + yield from page.evaluations + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListEvaluationsAsyncPager: + """A pager for iterating through ``list_evaluations`` requests. + + This class thinly wraps an initial + :class:`google.cloud.documentai_v1.types.ListEvaluationsResponse` object, and + provides an ``__aiter__`` method to iterate through its + ``evaluations`` field. + + If there are more pages, the ``__aiter__`` method will make additional + ``ListEvaluations`` requests and continue to iterate + through the ``evaluations`` field on the + corresponding responses. + + All the usual :class:`google.cloud.documentai_v1.types.ListEvaluationsResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[ + ..., Awaitable[document_processor_service.ListEvaluationsResponse] + ], + request: document_processor_service.ListEvaluationsRequest, + response: document_processor_service.ListEvaluationsResponse, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiates the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.documentai_v1.types.ListEvaluationsRequest): + The initial request object. + response (google.cloud.documentai_v1.types.ListEvaluationsResponse): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = document_processor_service.ListEvaluationsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + async def pages( + self, + ) -> AsyncIterator[document_processor_service.ListEvaluationsResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = await self._method(self._request, metadata=self._metadata) + yield self._response + + def __aiter__(self) -> AsyncIterator[evaluation.Evaluation]: + async def async_generator(): + async for page in self.pages: + for response in page.evaluations: + yield response + + return async_generator() + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/google/cloud/documentai_v1/services/document_processor_service/transports/base.py b/google/cloud/documentai_v1/services/document_processor_service/transports/base.py index 00055f71..4e895680 100644 --- a/google/cloud/documentai_v1/services/document_processor_service/transports/base.py +++ b/google/cloud/documentai_v1/services/document_processor_service/transports/base.py @@ -27,7 +27,7 @@ from google.oauth2 import service_account # type: ignore from google.cloud.documentai_v1 import gapic_version as package_version -from google.cloud.documentai_v1.types import document_processor_service +from google.cloud.documentai_v1.types import document_processor_service, evaluation from google.cloud.documentai_v1.types import processor from google.cloud.documentai_v1.types import processor as gcd_processor from google.cloud.documentai_v1.types import processor_type @@ -182,6 +182,11 @@ def _prep_wrapped_messages(self, client_info): default_timeout=None, client_info=client_info, ), + self.train_processor_version: gapic_v1.method.wrap_method( + self.train_processor_version, + default_timeout=None, + client_info=client_info, + ), self.get_processor_version: gapic_v1.method.wrap_method( self.get_processor_version, default_timeout=None, @@ -247,6 +252,21 @@ def _prep_wrapped_messages(self, client_info): default_timeout=120.0, client_info=client_info, ), + self.evaluate_processor_version: gapic_v1.method.wrap_method( + self.evaluate_processor_version, + default_timeout=None, + client_info=client_info, + ), + self.get_evaluation: gapic_v1.method.wrap_method( + self.get_evaluation, + default_timeout=None, + client_info=client_info, + ), + self.list_evaluations: gapic_v1.method.wrap_method( + self.list_evaluations, + default_timeout=None, + client_info=client_info, + ), } def close(self): @@ -338,6 +358,15 @@ def get_processor( ]: raise NotImplementedError() + @property + def train_processor_version( + self, + ) -> Callable[ + [document_processor_service.TrainProcessorVersionRequest], + Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]], + ]: + raise NotImplementedError() + @property def get_processor_version( self, @@ -440,6 +469,36 @@ def review_document( ]: raise NotImplementedError() + @property + def evaluate_processor_version( + self, + ) -> Callable[ + [document_processor_service.EvaluateProcessorVersionRequest], + Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]], + ]: + raise NotImplementedError() + + @property + def get_evaluation( + self, + ) -> Callable[ + [document_processor_service.GetEvaluationRequest], + Union[evaluation.Evaluation, Awaitable[evaluation.Evaluation]], + ]: + raise NotImplementedError() + + @property + def list_evaluations( + self, + ) -> Callable[ + [document_processor_service.ListEvaluationsRequest], + Union[ + document_processor_service.ListEvaluationsResponse, + Awaitable[document_processor_service.ListEvaluationsResponse], + ], + ]: + raise NotImplementedError() + @property def list_operations( self, diff --git a/google/cloud/documentai_v1/services/document_processor_service/transports/grpc.py b/google/cloud/documentai_v1/services/document_processor_service/transports/grpc.py index 97eb8a7c..4ea00e32 100644 --- a/google/cloud/documentai_v1/services/document_processor_service/transports/grpc.py +++ b/google/cloud/documentai_v1/services/document_processor_service/transports/grpc.py @@ -24,7 +24,7 @@ from google.longrunning import operations_pb2 # type: ignore import grpc # type: ignore -from google.cloud.documentai_v1.types import document_processor_service +from google.cloud.documentai_v1.types import document_processor_service, evaluation from google.cloud.documentai_v1.types import processor from google.cloud.documentai_v1.types import processor as gcd_processor from google.cloud.documentai_v1.types import processor_type @@ -455,6 +455,36 @@ def get_processor( ) return self._stubs["get_processor"] + @property + def train_processor_version( + self, + ) -> Callable[ + [document_processor_service.TrainProcessorVersionRequest], + operations_pb2.Operation, + ]: + r"""Return a callable for the train processor version method over gRPC. + + Trains a new processor version. Operation metadata is returned + as cloud_documentai_core.TrainProcessorVersionMetadata. + + Returns: + Callable[[~.TrainProcessorVersionRequest], + ~.Operation]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "train_processor_version" not in self._stubs: + self._stubs["train_processor_version"] = self.grpc_channel.unary_unary( + "/google.cloud.documentai.v1.DocumentProcessorService/TrainProcessorVersion", + request_serializer=document_processor_service.TrainProcessorVersionRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["train_processor_version"] + @property def get_processor_version( self, @@ -782,6 +812,94 @@ def review_document( ) return self._stubs["review_document"] + @property + def evaluate_processor_version( + self, + ) -> Callable[ + [document_processor_service.EvaluateProcessorVersionRequest], + operations_pb2.Operation, + ]: + r"""Return a callable for the evaluate processor version method over gRPC. + + Evaluates a ProcessorVersion against annotated + documents, producing an Evaluation. + + Returns: + Callable[[~.EvaluateProcessorVersionRequest], + ~.Operation]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "evaluate_processor_version" not in self._stubs: + self._stubs["evaluate_processor_version"] = self.grpc_channel.unary_unary( + "/google.cloud.documentai.v1.DocumentProcessorService/EvaluateProcessorVersion", + request_serializer=document_processor_service.EvaluateProcessorVersionRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["evaluate_processor_version"] + + @property + def get_evaluation( + self, + ) -> Callable[ + [document_processor_service.GetEvaluationRequest], evaluation.Evaluation + ]: + r"""Return a callable for the get evaluation method over gRPC. + + Retrieves a specific evaluation. + + Returns: + Callable[[~.GetEvaluationRequest], + ~.Evaluation]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "get_evaluation" not in self._stubs: + self._stubs["get_evaluation"] = self.grpc_channel.unary_unary( + "/google.cloud.documentai.v1.DocumentProcessorService/GetEvaluation", + request_serializer=document_processor_service.GetEvaluationRequest.serialize, + response_deserializer=evaluation.Evaluation.deserialize, + ) + return self._stubs["get_evaluation"] + + @property + def list_evaluations( + self, + ) -> Callable[ + [document_processor_service.ListEvaluationsRequest], + document_processor_service.ListEvaluationsResponse, + ]: + r"""Return a callable for the list evaluations method over gRPC. + + Retrieves a set of evaluations for a given processor + version. + + Returns: + Callable[[~.ListEvaluationsRequest], + ~.ListEvaluationsResponse]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "list_evaluations" not in self._stubs: + self._stubs["list_evaluations"] = self.grpc_channel.unary_unary( + "/google.cloud.documentai.v1.DocumentProcessorService/ListEvaluations", + request_serializer=document_processor_service.ListEvaluationsRequest.serialize, + response_deserializer=document_processor_service.ListEvaluationsResponse.deserialize, + ) + return self._stubs["list_evaluations"] + def close(self): self.grpc_channel.close() diff --git a/google/cloud/documentai_v1/services/document_processor_service/transports/grpc_asyncio.py b/google/cloud/documentai_v1/services/document_processor_service/transports/grpc_asyncio.py index e4d2128c..32cc18cd 100644 --- a/google/cloud/documentai_v1/services/document_processor_service/transports/grpc_asyncio.py +++ b/google/cloud/documentai_v1/services/document_processor_service/transports/grpc_asyncio.py @@ -24,7 +24,7 @@ import grpc # type: ignore from grpc.experimental import aio # type: ignore -from google.cloud.documentai_v1.types import document_processor_service +from google.cloud.documentai_v1.types import document_processor_service, evaluation from google.cloud.documentai_v1.types import processor from google.cloud.documentai_v1.types import processor as gcd_processor from google.cloud.documentai_v1.types import processor_type @@ -461,6 +461,36 @@ def get_processor( ) return self._stubs["get_processor"] + @property + def train_processor_version( + self, + ) -> Callable[ + [document_processor_service.TrainProcessorVersionRequest], + Awaitable[operations_pb2.Operation], + ]: + r"""Return a callable for the train processor version method over gRPC. + + Trains a new processor version. Operation metadata is returned + as cloud_documentai_core.TrainProcessorVersionMetadata. + + Returns: + Callable[[~.TrainProcessorVersionRequest], + Awaitable[~.Operation]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "train_processor_version" not in self._stubs: + self._stubs["train_processor_version"] = self.grpc_channel.unary_unary( + "/google.cloud.documentai.v1.DocumentProcessorService/TrainProcessorVersion", + request_serializer=document_processor_service.TrainProcessorVersionRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["train_processor_version"] + @property def get_processor_version( self, @@ -793,6 +823,95 @@ def review_document( ) return self._stubs["review_document"] + @property + def evaluate_processor_version( + self, + ) -> Callable[ + [document_processor_service.EvaluateProcessorVersionRequest], + Awaitable[operations_pb2.Operation], + ]: + r"""Return a callable for the evaluate processor version method over gRPC. + + Evaluates a ProcessorVersion against annotated + documents, producing an Evaluation. + + Returns: + Callable[[~.EvaluateProcessorVersionRequest], + Awaitable[~.Operation]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "evaluate_processor_version" not in self._stubs: + self._stubs["evaluate_processor_version"] = self.grpc_channel.unary_unary( + "/google.cloud.documentai.v1.DocumentProcessorService/EvaluateProcessorVersion", + request_serializer=document_processor_service.EvaluateProcessorVersionRequest.serialize, + response_deserializer=operations_pb2.Operation.FromString, + ) + return self._stubs["evaluate_processor_version"] + + @property + def get_evaluation( + self, + ) -> Callable[ + [document_processor_service.GetEvaluationRequest], + Awaitable[evaluation.Evaluation], + ]: + r"""Return a callable for the get evaluation method over gRPC. + + Retrieves a specific evaluation. + + Returns: + Callable[[~.GetEvaluationRequest], + Awaitable[~.Evaluation]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "get_evaluation" not in self._stubs: + self._stubs["get_evaluation"] = self.grpc_channel.unary_unary( + "/google.cloud.documentai.v1.DocumentProcessorService/GetEvaluation", + request_serializer=document_processor_service.GetEvaluationRequest.serialize, + response_deserializer=evaluation.Evaluation.deserialize, + ) + return self._stubs["get_evaluation"] + + @property + def list_evaluations( + self, + ) -> Callable[ + [document_processor_service.ListEvaluationsRequest], + Awaitable[document_processor_service.ListEvaluationsResponse], + ]: + r"""Return a callable for the list evaluations method over gRPC. + + Retrieves a set of evaluations for a given processor + version. + + Returns: + Callable[[~.ListEvaluationsRequest], + Awaitable[~.ListEvaluationsResponse]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "list_evaluations" not in self._stubs: + self._stubs["list_evaluations"] = self.grpc_channel.unary_unary( + "/google.cloud.documentai.v1.DocumentProcessorService/ListEvaluations", + request_serializer=document_processor_service.ListEvaluationsRequest.serialize, + response_deserializer=document_processor_service.ListEvaluationsResponse.deserialize, + ) + return self._stubs["list_evaluations"] + def close(self): return self.grpc_channel.close() diff --git a/google/cloud/documentai_v1/services/document_processor_service/transports/rest.py b/google/cloud/documentai_v1/services/document_processor_service/transports/rest.py index f5569439..9a2faa01 100644 --- a/google/cloud/documentai_v1/services/document_processor_service/transports/rest.py +++ b/google/cloud/documentai_v1/services/document_processor_service/transports/rest.py @@ -46,7 +46,7 @@ from google.longrunning import operations_pb2 # type: ignore -from google.cloud.documentai_v1.types import document_processor_service +from google.cloud.documentai_v1.types import document_processor_service, evaluation from google.cloud.documentai_v1.types import processor from google.cloud.documentai_v1.types import processor as gcd_processor from google.cloud.documentai_v1.types import processor_type @@ -132,6 +132,14 @@ def post_enable_processor(self, response): logging.log(f"Received response: {response}") return response + def pre_evaluate_processor_version(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_evaluate_processor_version(self, response): + logging.log(f"Received response: {response}") + return response + def pre_fetch_processor_types(self, request, metadata): logging.log(f"Received request: {request}") return request, metadata @@ -140,6 +148,14 @@ def post_fetch_processor_types(self, response): logging.log(f"Received response: {response}") return response + def pre_get_evaluation(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_evaluation(self, response): + logging.log(f"Received response: {response}") + return response + def pre_get_processor(self, request, metadata): logging.log(f"Received request: {request}") return request, metadata @@ -164,6 +180,14 @@ def post_get_processor_version(self, response): logging.log(f"Received response: {response}") return response + def pre_list_evaluations(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_evaluations(self, response): + logging.log(f"Received response: {response}") + return response + def pre_list_processors(self, request, metadata): logging.log(f"Received request: {request}") return request, metadata @@ -212,6 +236,14 @@ def post_set_default_processor_version(self, response): logging.log(f"Received response: {response}") return response + def pre_train_processor_version(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_train_processor_version(self, response): + logging.log(f"Received response: {response}") + return response + def pre_undeploy_processor_version(self, request, metadata): logging.log(f"Received request: {request}") return request, metadata @@ -403,6 +435,32 @@ def post_enable_processor( """ return response + def pre_evaluate_processor_version( + self, + request: document_processor_service.EvaluateProcessorVersionRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[ + document_processor_service.EvaluateProcessorVersionRequest, + Sequence[Tuple[str, str]], + ]: + """Pre-rpc interceptor for evaluate_processor_version + + Override in a subclass to manipulate the request or metadata + before they are sent to the DocumentProcessorService server. + """ + return request, metadata + + def post_evaluate_processor_version( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for evaluate_processor_version + + Override in a subclass to manipulate the response + after it is returned by the DocumentProcessorService server but before + it is returned to user code. + """ + return response + def pre_fetch_processor_types( self, request: document_processor_service.FetchProcessorTypesRequest, @@ -428,6 +486,31 @@ def post_fetch_processor_types( """ return response + def pre_get_evaluation( + self, + request: document_processor_service.GetEvaluationRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[ + document_processor_service.GetEvaluationRequest, Sequence[Tuple[str, str]] + ]: + """Pre-rpc interceptor for get_evaluation + + Override in a subclass to manipulate the request or metadata + before they are sent to the DocumentProcessorService server. + """ + return request, metadata + + def post_get_evaluation( + self, response: evaluation.Evaluation + ) -> evaluation.Evaluation: + """Post-rpc interceptor for get_evaluation + + Override in a subclass to manipulate the response + after it is returned by the DocumentProcessorService server but before + it is returned to user code. + """ + return response + def pre_get_processor( self, request: document_processor_service.GetProcessorRequest, @@ -501,6 +584,31 @@ def post_get_processor_version( """ return response + def pre_list_evaluations( + self, + request: document_processor_service.ListEvaluationsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[ + document_processor_service.ListEvaluationsRequest, Sequence[Tuple[str, str]] + ]: + """Pre-rpc interceptor for list_evaluations + + Override in a subclass to manipulate the request or metadata + before they are sent to the DocumentProcessorService server. + """ + return request, metadata + + def post_list_evaluations( + self, response: document_processor_service.ListEvaluationsResponse + ) -> document_processor_service.ListEvaluationsResponse: + """Post-rpc interceptor for list_evaluations + + Override in a subclass to manipulate the response + after it is returned by the DocumentProcessorService server but before + it is returned to user code. + """ + return response + def pre_list_processors( self, request: document_processor_service.ListProcessorsRequest, @@ -651,6 +759,32 @@ def post_set_default_processor_version( """ return response + def pre_train_processor_version( + self, + request: document_processor_service.TrainProcessorVersionRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[ + document_processor_service.TrainProcessorVersionRequest, + Sequence[Tuple[str, str]], + ]: + """Pre-rpc interceptor for train_processor_version + + Override in a subclass to manipulate the request or metadata + before they are sent to the DocumentProcessorService server. + """ + return request, metadata + + def post_train_processor_version( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for train_processor_version + + Override in a subclass to manipulate the response + after it is returned by the DocumentProcessorService server but before + it is returned to user code. + """ + return response + def pre_undeploy_processor_version( self, request: document_processor_service.UndeployProcessorVersionRequest, @@ -1653,6 +1787,109 @@ def __call__( resp = self._interceptor.post_enable_processor(resp) return resp + class _EvaluateProcessorVersion(DocumentProcessorServiceRestStub): + def __hash__(self): + return hash("EvaluateProcessorVersion") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: document_processor_service.EvaluateProcessorVersionRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the evaluate processor + version method over HTTP. + + Args: + request (~.document_processor_service.EvaluateProcessorVersionRequest): + The request object. Evaluates the given ProcessorVersion + against the supplied documents. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{processor_version=projects/*/locations/*/processors/*/processorVersions/*}:evaluateProcessorVersion", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_evaluate_processor_version( + request, metadata + ) + pb_request = document_processor_service.EvaluateProcessorVersionRequest.pb( + request + ) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_evaluate_processor_version(resp) + return resp + class _FetchProcessorTypes(DocumentProcessorServiceRestStub): def __hash__(self): return hash("FetchProcessorTypes") @@ -1747,6 +1984,94 @@ def __call__( resp = self._interceptor.post_fetch_processor_types(resp) return resp + class _GetEvaluation(DocumentProcessorServiceRestStub): + def __hash__(self): + return hash("GetEvaluation") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: document_processor_service.GetEvaluationRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> evaluation.Evaluation: + r"""Call the get evaluation method over HTTP. + + Args: + request (~.document_processor_service.GetEvaluationRequest): + The request object. Retrieves a specific Evaluation. + 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: + ~.evaluation.Evaluation: + An evaluation of a ProcessorVersion's + performance. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/processors/*/processorVersions/*/evaluations/*}", + }, + ] + request, metadata = self._interceptor.pre_get_evaluation(request, metadata) + pb_request = document_processor_service.GetEvaluationRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = evaluation.Evaluation() + pb_resp = evaluation.Evaluation.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_evaluation(resp) + return resp + class _GetProcessor(DocumentProcessorServiceRestStub): def __hash__(self): return hash("GetProcessor") @@ -2030,6 +2355,96 @@ def __call__( resp = self._interceptor.post_get_processor_version(resp) return resp + class _ListEvaluations(DocumentProcessorServiceRestStub): + def __hash__(self): + return hash("ListEvaluations") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: document_processor_service.ListEvaluationsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> document_processor_service.ListEvaluationsResponse: + r"""Call the list evaluations method over HTTP. + + Args: + request (~.document_processor_service.ListEvaluationsRequest): + The request object. Retrieves a list of evaluations for a + given ProcessorVersion. + + 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: + ~.document_processor_service.ListEvaluationsResponse: + The response from ListEvaluations. + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{parent=projects/*/locations/*/processors/*/processorVersions/*}/evaluations", + }, + ] + request, metadata = self._interceptor.pre_list_evaluations( + request, metadata + ) + pb_request = document_processor_service.ListEvaluationsRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = document_processor_service.ListEvaluationsResponse() + pb_resp = document_processor_service.ListEvaluationsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_evaluations(resp) + return resp + class _ListProcessors(DocumentProcessorServiceRestStub): def __hash__(self): return hash("ListProcessors") @@ -2612,6 +3027,108 @@ def __call__( resp = self._interceptor.post_set_default_processor_version(resp) return resp + class _TrainProcessorVersion(DocumentProcessorServiceRestStub): + def __hash__(self): + return hash("TrainProcessorVersion") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: document_processor_service.TrainProcessorVersionRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the train processor version method over HTTP. + + Args: + request (~.document_processor_service.TrainProcessorVersionRequest): + The request object. Request message for the create + processor version method. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{parent=projects/*/locations/*/processors/*}/processorVersions:train", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_train_processor_version( + request, metadata + ) + pb_request = document_processor_service.TrainProcessorVersionRequest.pb( + request + ) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_train_processor_version(resp) + return resp + class _UndeployProcessorVersion(DocumentProcessorServiceRestStub): def __hash__(self): return hash("UndeployProcessorVersion") @@ -2787,6 +3304,17 @@ def enable_processor( # In C++ this would require a dynamic_cast return self._EnableProcessor(self._session, self._host, self._interceptor) # type: ignore + @property + def evaluate_processor_version( + self, + ) -> Callable[ + [document_processor_service.EvaluateProcessorVersionRequest], + operations_pb2.Operation, + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._EvaluateProcessorVersion(self._session, self._host, self._interceptor) # type: ignore + @property def fetch_processor_types( self, @@ -2798,6 +3326,16 @@ def fetch_processor_types( # In C++ this would require a dynamic_cast return self._FetchProcessorTypes(self._session, self._host, self._interceptor) # type: ignore + @property + def get_evaluation( + self, + ) -> Callable[ + [document_processor_service.GetEvaluationRequest], evaluation.Evaluation + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._GetEvaluation(self._session, self._host, self._interceptor) # type: ignore + @property def get_processor( self, @@ -2830,6 +3368,17 @@ def get_processor_version( # In C++ this would require a dynamic_cast return self._GetProcessorVersion(self._session, self._host, self._interceptor) # type: ignore + @property + def list_evaluations( + self, + ) -> Callable[ + [document_processor_service.ListEvaluationsRequest], + document_processor_service.ListEvaluationsResponse, + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ListEvaluations(self._session, self._host, self._interceptor) # type: ignore + @property def list_processors( self, @@ -2895,6 +3444,17 @@ def set_default_processor_version( # In C++ this would require a dynamic_cast return self._SetDefaultProcessorVersion(self._session, self._host, self._interceptor) # type: ignore + @property + def train_processor_version( + self, + ) -> Callable[ + [document_processor_service.TrainProcessorVersionRequest], + operations_pb2.Operation, + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._TrainProcessorVersion(self._session, self._host, self._interceptor) # type: ignore + @property def undeploy_processor_version( self, diff --git a/google/cloud/documentai_v1/types/__init__.py b/google/cloud/documentai_v1/types/__init__.py index 4d1bee7d..0525287c 100644 --- a/google/cloud/documentai_v1/types/__init__.py +++ b/google/cloud/documentai_v1/types/__init__.py @@ -41,12 +41,18 @@ EnableProcessorMetadata, EnableProcessorRequest, EnableProcessorResponse, + EvaluateProcessorVersionMetadata, + EvaluateProcessorVersionRequest, + EvaluateProcessorVersionResponse, FetchProcessorTypesRequest, FetchProcessorTypesResponse, + GetEvaluationRequest, GetProcessorRequest, GetProcessorTypeRequest, GetProcessorVersionRequest, HumanReviewStatus, + ListEvaluationsRequest, + ListEvaluationsResponse, ListProcessorsRequest, ListProcessorsResponse, ListProcessorTypesRequest, @@ -61,11 +67,15 @@ SetDefaultProcessorVersionMetadata, SetDefaultProcessorVersionRequest, SetDefaultProcessorVersionResponse, + TrainProcessorVersionMetadata, + TrainProcessorVersionRequest, + TrainProcessorVersionResponse, UndeployProcessorVersionMetadata, UndeployProcessorVersionRequest, UndeployProcessorVersionResponse, ) from .document_schema import DocumentSchema +from .evaluation import Evaluation, EvaluationReference from .geometry import BoundingPoly, NormalizedVertex, Vertex from .operation_metadata import CommonOperationMetadata from .processor import Processor, ProcessorVersion @@ -97,12 +107,18 @@ "EnableProcessorMetadata", "EnableProcessorRequest", "EnableProcessorResponse", + "EvaluateProcessorVersionMetadata", + "EvaluateProcessorVersionRequest", + "EvaluateProcessorVersionResponse", "FetchProcessorTypesRequest", "FetchProcessorTypesResponse", + "GetEvaluationRequest", "GetProcessorRequest", "GetProcessorTypeRequest", "GetProcessorVersionRequest", "HumanReviewStatus", + "ListEvaluationsRequest", + "ListEvaluationsResponse", "ListProcessorsRequest", "ListProcessorsResponse", "ListProcessorTypesRequest", @@ -117,10 +133,15 @@ "SetDefaultProcessorVersionMetadata", "SetDefaultProcessorVersionRequest", "SetDefaultProcessorVersionResponse", + "TrainProcessorVersionMetadata", + "TrainProcessorVersionRequest", + "TrainProcessorVersionResponse", "UndeployProcessorVersionMetadata", "UndeployProcessorVersionRequest", "UndeployProcessorVersionResponse", "DocumentSchema", + "Evaluation", + "EvaluationReference", "BoundingPoly", "NormalizedVertex", "Vertex", diff --git a/google/cloud/documentai_v1/types/document.py b/google/cloud/documentai_v1/types/document.py index 00ad692c..66342176 100644 --- a/google/cloud/documentai_v1/types/document.py +++ b/google/cloud/documentai_v1/types/document.py @@ -1477,20 +1477,30 @@ class OperationType(proto.Enum): Add an element. REMOVE (2): Remove an element identified by ``parent``. + UPDATE (7): + Updates any fields within the given + provenance scope of the message. It 'overwrites' + the fields rather than replacing them. This is + especially relevant when we just want to update + a field value of an entity without also + affecting all the child properties. REPLACE (3): - Replace an element identified by ``parent``. - EVAL_REQUESTED (4): - Request human review for the element identified by + Currently unused. Replace an element identified by ``parent``. + EVAL_REQUESTED (4): + Deprecated. Request human review for the element identified + by ``parent``. EVAL_APPROVED (5): - Element is reviewed and approved at human - review, confidence will be set to 1.0. + Deprecated. Element is reviewed and approved + at human review, confidence will be set to 1.0. EVAL_SKIPPED (6): - Element is skipped in the validation process. + Deprecated. Element is skipped in the + validation process. """ OPERATION_TYPE_UNSPECIFIED = 0 ADD = 1 REMOVE = 2 + UPDATE = 7 REPLACE = 3 EVAL_REQUESTED = 4 EVAL_APPROVED = 5 @@ -1567,8 +1577,9 @@ class Revision(proto.Message): This field is a member of `oneof`_ ``source``. id (str): - Id of the revision. Unique within the - context of the document. + Id of the revision, internally generated by + doc proto storage. Unique within the context of + the document. parent (MutableSequence[int]): The revisions that this revision is based on. This can include one or more parent (when documents are merged.) This @@ -1579,7 +1590,9 @@ class Revision(proto.Message): eg. there are ``provenance.parent.revision`` fields that index into this field. create_time (google.protobuf.timestamp_pb2.Timestamp): - The time that the revision was created. + The time that the revision was created, + internally generated by doc proto storage at the + time of create. human_review (google.cloud.documentai_v1.types.Document.Revision.HumanReview): Human Review information of this revision. """ diff --git a/google/cloud/documentai_v1/types/document_processor_service.py b/google/cloud/documentai_v1/types/document_processor_service.py index ec45701e..15649291 100644 --- a/google/cloud/documentai_v1/types/document_processor_service.py +++ b/google/cloud/documentai_v1/types/document_processor_service.py @@ -23,6 +23,7 @@ from google.cloud.documentai_v1.types import document_schema as gcd_document_schema from google.cloud.documentai_v1.types import document as gcd_document from google.cloud.documentai_v1.types import document_io +from google.cloud.documentai_v1.types import evaluation as gcd_evaluation from google.cloud.documentai_v1.types import operation_metadata from google.cloud.documentai_v1.types import processor as gcd_processor from google.cloud.documentai_v1.types import processor_type @@ -67,9 +68,18 @@ "SetDefaultProcessorVersionRequest", "SetDefaultProcessorVersionResponse", "SetDefaultProcessorVersionMetadata", + "TrainProcessorVersionRequest", + "TrainProcessorVersionResponse", + "TrainProcessorVersionMetadata", "ReviewDocumentRequest", "ReviewDocumentResponse", "ReviewDocumentOperationMetadata", + "EvaluateProcessorVersionRequest", + "EvaluateProcessorVersionMetadata", + "EvaluateProcessorVersionResponse", + "GetEvaluationRequest", + "ListEvaluationsRequest", + "ListEvaluationsResponse", }, ) @@ -932,6 +942,163 @@ class SetDefaultProcessorVersionMetadata(proto.Message): ) +class TrainProcessorVersionRequest(proto.Message): + r"""Request message for the create processor version method. + + Attributes: + parent (str): + Required. The parent (project, location and processor) to + create the new version for. Format: + ``projects/{project}/locations/{location}/processors/{processor}``. + processor_version (google.cloud.documentai_v1.types.ProcessorVersion): + Required. The processor version to be + created. + document_schema (google.cloud.documentai_v1.types.DocumentSchema): + Optional. The schema the processor version + will be trained with. + input_data (google.cloud.documentai_v1.types.TrainProcessorVersionRequest.InputData): + Optional. The input data used to train the + ``ProcessorVersion``. + base_processor_version (str): + Optional. The processor version to use as a base for + training. This processor version must be a child of + ``parent``. Format: + ``projects/{project}/locations/{location}/processors/{processor}/processorVersions/{processorVersion}``. + """ + + class InputData(proto.Message): + r"""The input data used to train a new ``ProcessorVersion``. + + Attributes: + training_documents (google.cloud.documentai_v1.types.BatchDocumentsInputConfig): + The documents used for training the new + version. + test_documents (google.cloud.documentai_v1.types.BatchDocumentsInputConfig): + The documents used for testing the trained + version. + """ + + training_documents: document_io.BatchDocumentsInputConfig = proto.Field( + proto.MESSAGE, + number=3, + message=document_io.BatchDocumentsInputConfig, + ) + test_documents: document_io.BatchDocumentsInputConfig = proto.Field( + proto.MESSAGE, + number=4, + message=document_io.BatchDocumentsInputConfig, + ) + + parent: str = proto.Field( + proto.STRING, + number=1, + ) + processor_version: gcd_processor.ProcessorVersion = proto.Field( + proto.MESSAGE, + number=2, + message=gcd_processor.ProcessorVersion, + ) + document_schema: gcd_document_schema.DocumentSchema = proto.Field( + proto.MESSAGE, + number=10, + message=gcd_document_schema.DocumentSchema, + ) + input_data: InputData = proto.Field( + proto.MESSAGE, + number=4, + message=InputData, + ) + base_processor_version: str = proto.Field( + proto.STRING, + number=8, + ) + + +class TrainProcessorVersionResponse(proto.Message): + r"""The response for the TrainProcessorVersion method. + + Attributes: + processor_version (str): + The resource name of the processor version + produced by training. + """ + + processor_version: str = proto.Field( + proto.STRING, + number=1, + ) + + +class TrainProcessorVersionMetadata(proto.Message): + r"""The metadata that represents a processor version being + created. + + Attributes: + common_metadata (google.cloud.documentai_v1.types.CommonOperationMetadata): + The basic metadata of the long running + operation. + training_dataset_validation (google.cloud.documentai_v1.types.TrainProcessorVersionMetadata.DatasetValidation): + The training dataset validation information. + test_dataset_validation (google.cloud.documentai_v1.types.TrainProcessorVersionMetadata.DatasetValidation): + The test dataset validation information. + """ + + class DatasetValidation(proto.Message): + r"""The dataset validation information. + This includes any and all errors with documents and the dataset. + + Attributes: + document_error_count (int): + The total number of document errors. + dataset_error_count (int): + The total number of dataset errors. + document_errors (MutableSequence[google.rpc.status_pb2.Status]): + Error information pertaining to specific + documents. A maximum of 10 document errors will + be returned. Any document with errors will not + be used throughout training. + dataset_errors (MutableSequence[google.rpc.status_pb2.Status]): + Error information for the dataset as a whole. + A maximum of 10 dataset errors will be returned. + A single dataset error is terminal for training. + """ + + document_error_count: int = proto.Field( + proto.INT32, + number=3, + ) + dataset_error_count: int = proto.Field( + proto.INT32, + number=4, + ) + document_errors: MutableSequence[status_pb2.Status] = proto.RepeatedField( + proto.MESSAGE, + number=1, + message=status_pb2.Status, + ) + dataset_errors: MutableSequence[status_pb2.Status] = proto.RepeatedField( + proto.MESSAGE, + number=2, + message=status_pb2.Status, + ) + + common_metadata: operation_metadata.CommonOperationMetadata = proto.Field( + proto.MESSAGE, + number=1, + message=operation_metadata.CommonOperationMetadata, + ) + training_dataset_validation: DatasetValidation = proto.Field( + proto.MESSAGE, + number=2, + message=DatasetValidation, + ) + test_dataset_validation: DatasetValidation = proto.Field( + proto.MESSAGE, + number=3, + message=DatasetValidation, + ) + + class ReviewDocumentRequest(proto.Message): r"""Request message for review document method. @@ -1065,4 +1232,137 @@ class ReviewDocumentOperationMetadata(proto.Message): ) +class EvaluateProcessorVersionRequest(proto.Message): + r"""Evaluates the given ProcessorVersion against the supplied + documents. + + Attributes: + processor_version (str): + Required. The resource name of the + [ProcessorVersion][google.cloud.documentai.v1.ProcessorVersion] + to evaluate. + ``projects/{project}/locations/{location}/processors/{processor}/processorVersions/{processorVersion}`` + evaluation_documents (google.cloud.documentai_v1.types.BatchDocumentsInputConfig): + Optional. The documents used in the + evaluation. If unspecified, use the processor's + dataset as evaluation input. + """ + + processor_version: str = proto.Field( + proto.STRING, + number=1, + ) + evaluation_documents: document_io.BatchDocumentsInputConfig = proto.Field( + proto.MESSAGE, + number=3, + message=document_io.BatchDocumentsInputConfig, + ) + + +class EvaluateProcessorVersionMetadata(proto.Message): + r"""Metadata of the EvaluateProcessorVersion method. + + Attributes: + common_metadata (google.cloud.documentai_v1.types.CommonOperationMetadata): + The basic metadata of the long running + operation. + """ + + common_metadata: operation_metadata.CommonOperationMetadata = proto.Field( + proto.MESSAGE, + number=1, + message=operation_metadata.CommonOperationMetadata, + ) + + +class EvaluateProcessorVersionResponse(proto.Message): + r"""Metadata of the EvaluateProcessorVersion method. + + Attributes: + evaluation (str): + The resource name of the created evaluation. + """ + + evaluation: str = proto.Field( + proto.STRING, + number=2, + ) + + +class GetEvaluationRequest(proto.Message): + r"""Retrieves a specific Evaluation. + + Attributes: + name (str): + Required. The resource name of the + [Evaluation][google.cloud.documentai.v1.Evaluation] to get. + ``projects/{project}/locations/{location}/processors/{processor}/processorVersions/{processorVersion}/evaluations/{evaluation}`` + """ + + name: str = proto.Field( + proto.STRING, + number=1, + ) + + +class ListEvaluationsRequest(proto.Message): + r"""Retrieves a list of evaluations for a given ProcessorVersion. + + Attributes: + parent (str): + Required. The resource name of the + [ProcessorVersion][google.cloud.documentai.v1.ProcessorVersion] + to list evaluations for. + ``projects/{project}/locations/{location}/processors/{processor}/processorVersions/{processorVersion}`` + page_size (int): + The standard list page size. + If unspecified, at most 5 evaluations will be + returned. The maximum value is 100; values above + 100 will be coerced to 100. + page_token (str): + A page token, received from a previous ``ListEvaluations`` + call. Provide this to retrieve the subsequent page. + """ + + parent: str = proto.Field( + proto.STRING, + number=1, + ) + page_size: int = proto.Field( + proto.INT32, + number=2, + ) + page_token: str = proto.Field( + proto.STRING, + number=3, + ) + + +class ListEvaluationsResponse(proto.Message): + r"""The response from ListEvaluations. + + Attributes: + evaluations (MutableSequence[google.cloud.documentai_v1.types.Evaluation]): + The evaluations requested. + next_page_token (str): + A token, which can be sent as ``page_token`` to retrieve the + next page. If this field is omitted, there are no subsequent + pages. + """ + + @property + def raw_page(self): + return self + + evaluations: MutableSequence[gcd_evaluation.Evaluation] = proto.RepeatedField( + proto.MESSAGE, + number=1, + message=gcd_evaluation.Evaluation, + ) + next_page_token: str = proto.Field( + proto.STRING, + number=2, + ) + + __all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/google/cloud/documentai_v1/types/document_schema.py b/google/cloud/documentai_v1/types/document_schema.py index 2a2bba61..79630a76 100644 --- a/google/cloud/documentai_v1/types/document_schema.py +++ b/google/cloud/documentai_v1/types/document_schema.py @@ -80,7 +80,7 @@ class EntityType(proto.Message): The entity type that this type is derived from. For now, one and only one should be set. properties (MutableSequence[google.cloud.documentai_v1.types.DocumentSchema.EntityType.Property]): - Describing the nested structure, or + Description the nested structure, or composition of an entity. """ @@ -116,22 +116,30 @@ class Property(proto.Message): """ class OccurrenceType(proto.Enum): - r"""Types of occurrences of the entity type in the document. - Note: this represents the number of instances of an entity - types, not number of mentions of a given entity instance. + r"""Types of occurrences of the entity type in the document. This + represents the number of instances of instances of an entity, not + number of mentions of an entity. For example, a bank statement may + only have one ``account_number``, but this account number may be + mentioned in several places on the document. In this case the + 'account_number' would be considered a ``REQUIRED_ONCE`` entity + type. If, on the other hand, we expect a bank statement to contain + the status of multiple different accounts for the customers, the + occurrence type will be set to ``REQUIRED_MULTIPLE``. Values: OCCURRENCE_TYPE_UNSPECIFIED (0): Unspecified occurrence type. OPTIONAL_ONCE (1): There will be zero or one instance of this - entity type. + entity type. The same entity instance may be + mentioned multiple times. OPTIONAL_MULTIPLE (2): The entity type will appear zero or multiple times. REQUIRED_ONCE (3): The entity type will only appear exactly - once. + once. The same entity instance may be mentioned + multiple times. REQUIRED_MULTIPLE (4): The entity type will appear once or more times. diff --git a/google/cloud/documentai_v1/types/evaluation.py b/google/cloud/documentai_v1/types/evaluation.py new file mode 100644 index 00000000..bbcef991 --- /dev/null +++ b/google/cloud/documentai_v1/types/evaluation.py @@ -0,0 +1,345 @@ +# -*- coding: utf-8 -*- +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +from typing import MutableMapping, MutableSequence + +from google.protobuf import timestamp_pb2 # type: ignore +import proto # type: ignore + +__protobuf__ = proto.module( + package="google.cloud.documentai.v1", + manifest={ + "EvaluationReference", + "Evaluation", + }, +) + + +class EvaluationReference(proto.Message): + r"""Gives a short summary of an evaluation, and links to the + evaluation itself. + + Attributes: + operation (str): + The resource name of the Long Running + Operation for the evaluation. + evaluation (str): + The resource name of the evaluation. + aggregate_metrics (google.cloud.documentai_v1.types.Evaluation.Metrics): + An aggregate of the statistics for the + evaluation with fuzzy matching on. + aggregate_metrics_exact (google.cloud.documentai_v1.types.Evaluation.Metrics): + An aggregate of the statistics for the + evaluation with fuzzy matching off. + """ + + operation: str = proto.Field( + proto.STRING, + number=1, + ) + evaluation: str = proto.Field( + proto.STRING, + number=2, + ) + aggregate_metrics: "Evaluation.Metrics" = proto.Field( + proto.MESSAGE, + number=4, + message="Evaluation.Metrics", + ) + aggregate_metrics_exact: "Evaluation.Metrics" = proto.Field( + proto.MESSAGE, + number=5, + message="Evaluation.Metrics", + ) + + +class Evaluation(proto.Message): + r"""An evaluation of a ProcessorVersion's performance. + + Attributes: + name (str): + The resource name of the evaluation. Format: + ``projects/{project}/locations/{location}/processors/{processor}/processorVersions/{processor_version}/evaluations/{evaluation}`` + create_time (google.protobuf.timestamp_pb2.Timestamp): + The time that the evaluation was created. + document_counters (google.cloud.documentai_v1.types.Evaluation.Counters): + Counters for the documents used in the + evaluation. + all_entities_metrics (google.cloud.documentai_v1.types.Evaluation.MultiConfidenceMetrics): + Metrics for all the entities in aggregate. + entity_metrics (MutableMapping[str, google.cloud.documentai_v1.types.Evaluation.MultiConfidenceMetrics]): + Metrics across confidence levels, for + different entities. + kms_key_name (str): + The KMS key name used for encryption. + kms_key_version_name (str): + The KMS key version with which data is + encrypted. + """ + + class Counters(proto.Message): + r"""Evaluation counters for the documents that were used. + + Attributes: + input_documents_count (int): + How many documents were sent for evaluation. + invalid_documents_count (int): + How many documents were not included in the + evaluation as they didn't pass validation. + failed_documents_count (int): + How many documents were not included in the + evaluation as Document AI failed to process + them. + evaluated_documents_count (int): + How many documents were used in the + evaluation. + """ + + input_documents_count: int = proto.Field( + proto.INT32, + number=1, + ) + invalid_documents_count: int = proto.Field( + proto.INT32, + number=2, + ) + failed_documents_count: int = proto.Field( + proto.INT32, + number=3, + ) + evaluated_documents_count: int = proto.Field( + proto.INT32, + number=4, + ) + + class Metrics(proto.Message): + r"""Evaluation metrics, either in aggregate or about a specific + entity. + + Attributes: + precision (float): + The calculated precision. + recall (float): + The calculated recall. + f1_score (float): + The calculated f1 score. + predicted_occurrences_count (int): + The amount of occurrences in predicted + documents. + ground_truth_occurrences_count (int): + The amount of occurrences in ground truth + documents. + predicted_document_count (int): + The amount of documents with a predicted + occurrence. + ground_truth_document_count (int): + The amount of documents with a ground truth + occurrence. + true_positives_count (int): + The amount of true positives. + false_positives_count (int): + The amount of false positives. + false_negatives_count (int): + The amount of false negatives. + total_documents_count (int): + The amount of documents that had an + occurrence of this label. + """ + + precision: float = proto.Field( + proto.FLOAT, + number=1, + ) + recall: float = proto.Field( + proto.FLOAT, + number=2, + ) + f1_score: float = proto.Field( + proto.FLOAT, + number=3, + ) + predicted_occurrences_count: int = proto.Field( + proto.INT32, + number=4, + ) + ground_truth_occurrences_count: int = proto.Field( + proto.INT32, + number=5, + ) + predicted_document_count: int = proto.Field( + proto.INT32, + number=10, + ) + ground_truth_document_count: int = proto.Field( + proto.INT32, + number=11, + ) + true_positives_count: int = proto.Field( + proto.INT32, + number=6, + ) + false_positives_count: int = proto.Field( + proto.INT32, + number=7, + ) + false_negatives_count: int = proto.Field( + proto.INT32, + number=8, + ) + total_documents_count: int = proto.Field( + proto.INT32, + number=9, + ) + + class ConfidenceLevelMetrics(proto.Message): + r"""Evaluations metrics, at a specific confidence level. + + Attributes: + confidence_level (float): + The confidence level. + metrics (google.cloud.documentai_v1.types.Evaluation.Metrics): + The metrics at the specific confidence level. + """ + + confidence_level: float = proto.Field( + proto.FLOAT, + number=1, + ) + metrics: "Evaluation.Metrics" = proto.Field( + proto.MESSAGE, + number=2, + message="Evaluation.Metrics", + ) + + class MultiConfidenceMetrics(proto.Message): + r"""Metrics across multiple confidence levels. + + Attributes: + confidence_level_metrics (MutableSequence[google.cloud.documentai_v1.types.Evaluation.ConfidenceLevelMetrics]): + Metrics across confidence levels with fuzzy + matching enabled. + confidence_level_metrics_exact (MutableSequence[google.cloud.documentai_v1.types.Evaluation.ConfidenceLevelMetrics]): + Metrics across confidence levels with only + exact matching. + auprc (float): + The calculated area under the precision + recall curve (AUPRC), computed by integrating + over all confidence thresholds. + estimated_calibration_error (float): + The Estimated Calibration Error (ECE) of the + confidence of the predicted entities. + auprc_exact (float): + The AUPRC for metrics with fuzzy matching + disabled, i.e., exact matching only. + estimated_calibration_error_exact (float): + The ECE for the predicted entities with fuzzy + matching disabled, i.e., exact matching only. + metrics_type (google.cloud.documentai_v1.types.Evaluation.MultiConfidenceMetrics.MetricsType): + The metrics type for the label. + """ + + class MetricsType(proto.Enum): + r"""A type that determines how metrics should be interpreted. + + Values: + METRICS_TYPE_UNSPECIFIED (0): + The metrics type is unspecified. By default, + metrics without a particular specification are + for leaf entity types (i.e., top-level entity + types without child types, or child types which + are not parent types themselves). + AGGREGATE (1): + Indicates whether metrics for this particular + label type represent an aggregate of metrics for + other types instead of being based on actual + TP/FP/FN values for the label type. Metrics for + parent (i.e., non-leaf) entity types are an + aggregate of metrics for their children. + """ + METRICS_TYPE_UNSPECIFIED = 0 + AGGREGATE = 1 + + confidence_level_metrics: MutableSequence[ + "Evaluation.ConfidenceLevelMetrics" + ] = proto.RepeatedField( + proto.MESSAGE, + number=1, + message="Evaluation.ConfidenceLevelMetrics", + ) + confidence_level_metrics_exact: MutableSequence[ + "Evaluation.ConfidenceLevelMetrics" + ] = proto.RepeatedField( + proto.MESSAGE, + number=4, + message="Evaluation.ConfidenceLevelMetrics", + ) + auprc: float = proto.Field( + proto.FLOAT, + number=2, + ) + estimated_calibration_error: float = proto.Field( + proto.FLOAT, + number=3, + ) + auprc_exact: float = proto.Field( + proto.FLOAT, + number=5, + ) + estimated_calibration_error_exact: float = proto.Field( + proto.FLOAT, + number=6, + ) + metrics_type: "Evaluation.MultiConfidenceMetrics.MetricsType" = proto.Field( + proto.ENUM, + number=7, + enum="Evaluation.MultiConfidenceMetrics.MetricsType", + ) + + name: str = proto.Field( + proto.STRING, + number=1, + ) + create_time: timestamp_pb2.Timestamp = proto.Field( + proto.MESSAGE, + number=2, + message=timestamp_pb2.Timestamp, + ) + document_counters: Counters = proto.Field( + proto.MESSAGE, + number=5, + message=Counters, + ) + all_entities_metrics: MultiConfidenceMetrics = proto.Field( + proto.MESSAGE, + number=3, + message=MultiConfidenceMetrics, + ) + entity_metrics: MutableMapping[str, MultiConfidenceMetrics] = proto.MapField( + proto.STRING, + proto.MESSAGE, + number=4, + message=MultiConfidenceMetrics, + ) + kms_key_name: str = proto.Field( + proto.STRING, + number=6, + ) + kms_key_version_name: str = proto.Field( + proto.STRING, + number=7, + ) + + +__all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/google/cloud/documentai_v1/types/processor.py b/google/cloud/documentai_v1/types/processor.py index b61d11f0..e50c4af8 100644 --- a/google/cloud/documentai_v1/types/processor.py +++ b/google/cloud/documentai_v1/types/processor.py @@ -19,6 +19,7 @@ import proto # type: ignore from google.cloud.documentai_v1.types import document_schema as gcd_document_schema +from google.cloud.documentai_v1.types import evaluation __protobuf__ = proto.module( package="google.cloud.documentai.v1", @@ -50,6 +51,9 @@ class ProcessorVersion(proto.Message): The state of the processor version. create_time (google.protobuf.timestamp_pb2.Timestamp): The time the processor version was created. + latest_evaluation (google.cloud.documentai_v1.types.EvaluationReference): + The most recently invoked evaluation for the + processor version. kms_key_name (str): The KMS key name used for encryption. kms_key_version_name (str): @@ -143,6 +147,11 @@ class DeprecationInfo(proto.Message): number=7, message=timestamp_pb2.Timestamp, ) + latest_evaluation: evaluation.EvaluationReference = proto.Field( + proto.MESSAGE, + number=8, + message=evaluation.EvaluationReference, + ) kms_key_name: str = proto.Field( proto.STRING, number=9, diff --git a/samples/generated_samples/documentai_v1_generated_document_processor_service_evaluate_processor_version_async.py b/samples/generated_samples/documentai_v1_generated_document_processor_service_evaluate_processor_version_async.py new file mode 100644 index 00000000..76e24c04 --- /dev/null +++ b/samples/generated_samples/documentai_v1_generated_document_processor_service_evaluate_processor_version_async.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generated code. DO NOT EDIT! +# +# Snippet for EvaluateProcessorVersion +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-documentai + + +# [START documentai_v1_generated_DocumentProcessorService_EvaluateProcessorVersion_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import documentai_v1 + + +async def sample_evaluate_processor_version(): + # Create a client + client = documentai_v1.DocumentProcessorServiceAsyncClient() + + # Initialize request argument(s) + request = documentai_v1.EvaluateProcessorVersionRequest( + processor_version="processor_version_value", + ) + + # Make the request + operation = client.evaluate_processor_version(request=request) + + print("Waiting for operation to complete...") + + response = (await operation).result() + + # Handle the response + print(response) + +# [END documentai_v1_generated_DocumentProcessorService_EvaluateProcessorVersion_async] diff --git a/samples/generated_samples/documentai_v1_generated_document_processor_service_evaluate_processor_version_sync.py b/samples/generated_samples/documentai_v1_generated_document_processor_service_evaluate_processor_version_sync.py new file mode 100644 index 00000000..33947155 --- /dev/null +++ b/samples/generated_samples/documentai_v1_generated_document_processor_service_evaluate_processor_version_sync.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generated code. DO NOT EDIT! +# +# Snippet for EvaluateProcessorVersion +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-documentai + + +# [START documentai_v1_generated_DocumentProcessorService_EvaluateProcessorVersion_sync] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import documentai_v1 + + +def sample_evaluate_processor_version(): + # Create a client + client = documentai_v1.DocumentProcessorServiceClient() + + # Initialize request argument(s) + request = documentai_v1.EvaluateProcessorVersionRequest( + processor_version="processor_version_value", + ) + + # Make the request + operation = client.evaluate_processor_version(request=request) + + print("Waiting for operation to complete...") + + response = operation.result() + + # Handle the response + print(response) + +# [END documentai_v1_generated_DocumentProcessorService_EvaluateProcessorVersion_sync] diff --git a/samples/generated_samples/documentai_v1_generated_document_processor_service_get_evaluation_async.py b/samples/generated_samples/documentai_v1_generated_document_processor_service_get_evaluation_async.py new file mode 100644 index 00000000..f1391f01 --- /dev/null +++ b/samples/generated_samples/documentai_v1_generated_document_processor_service_get_evaluation_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetEvaluation +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-documentai + + +# [START documentai_v1_generated_DocumentProcessorService_GetEvaluation_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import documentai_v1 + + +async def sample_get_evaluation(): + # Create a client + client = documentai_v1.DocumentProcessorServiceAsyncClient() + + # Initialize request argument(s) + request = documentai_v1.GetEvaluationRequest( + name="name_value", + ) + + # Make the request + response = await client.get_evaluation(request=request) + + # Handle the response + print(response) + +# [END documentai_v1_generated_DocumentProcessorService_GetEvaluation_async] diff --git a/samples/generated_samples/documentai_v1_generated_document_processor_service_get_evaluation_sync.py b/samples/generated_samples/documentai_v1_generated_document_processor_service_get_evaluation_sync.py new file mode 100644 index 00000000..ea4c1a33 --- /dev/null +++ b/samples/generated_samples/documentai_v1_generated_document_processor_service_get_evaluation_sync.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetEvaluation +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-documentai + + +# [START documentai_v1_generated_DocumentProcessorService_GetEvaluation_sync] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import documentai_v1 + + +def sample_get_evaluation(): + # Create a client + client = documentai_v1.DocumentProcessorServiceClient() + + # Initialize request argument(s) + request = documentai_v1.GetEvaluationRequest( + name="name_value", + ) + + # Make the request + response = client.get_evaluation(request=request) + + # Handle the response + print(response) + +# [END documentai_v1_generated_DocumentProcessorService_GetEvaluation_sync] diff --git a/samples/generated_samples/documentai_v1_generated_document_processor_service_list_evaluations_async.py b/samples/generated_samples/documentai_v1_generated_document_processor_service_list_evaluations_async.py new file mode 100644 index 00000000..a056d558 --- /dev/null +++ b/samples/generated_samples/documentai_v1_generated_document_processor_service_list_evaluations_async.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generated code. DO NOT EDIT! +# +# Snippet for ListEvaluations +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-documentai + + +# [START documentai_v1_generated_DocumentProcessorService_ListEvaluations_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import documentai_v1 + + +async def sample_list_evaluations(): + # Create a client + client = documentai_v1.DocumentProcessorServiceAsyncClient() + + # Initialize request argument(s) + request = documentai_v1.ListEvaluationsRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_evaluations(request=request) + + # Handle the response + async for response in page_result: + print(response) + +# [END documentai_v1_generated_DocumentProcessorService_ListEvaluations_async] diff --git a/samples/generated_samples/documentai_v1_generated_document_processor_service_list_evaluations_sync.py b/samples/generated_samples/documentai_v1_generated_document_processor_service_list_evaluations_sync.py new file mode 100644 index 00000000..e377f0b9 --- /dev/null +++ b/samples/generated_samples/documentai_v1_generated_document_processor_service_list_evaluations_sync.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generated code. DO NOT EDIT! +# +# Snippet for ListEvaluations +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-documentai + + +# [START documentai_v1_generated_DocumentProcessorService_ListEvaluations_sync] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import documentai_v1 + + +def sample_list_evaluations(): + # Create a client + client = documentai_v1.DocumentProcessorServiceClient() + + # Initialize request argument(s) + request = documentai_v1.ListEvaluationsRequest( + parent="parent_value", + ) + + # Make the request + page_result = client.list_evaluations(request=request) + + # Handle the response + for response in page_result: + print(response) + +# [END documentai_v1_generated_DocumentProcessorService_ListEvaluations_sync] diff --git a/samples/generated_samples/documentai_v1_generated_document_processor_service_train_processor_version_async.py b/samples/generated_samples/documentai_v1_generated_document_processor_service_train_processor_version_async.py new file mode 100644 index 00000000..2a0d5adf --- /dev/null +++ b/samples/generated_samples/documentai_v1_generated_document_processor_service_train_processor_version_async.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generated code. DO NOT EDIT! +# +# Snippet for TrainProcessorVersion +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-documentai + + +# [START documentai_v1_generated_DocumentProcessorService_TrainProcessorVersion_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import documentai_v1 + + +async def sample_train_processor_version(): + # Create a client + client = documentai_v1.DocumentProcessorServiceAsyncClient() + + # Initialize request argument(s) + request = documentai_v1.TrainProcessorVersionRequest( + parent="parent_value", + ) + + # Make the request + operation = client.train_processor_version(request=request) + + print("Waiting for operation to complete...") + + response = (await operation).result() + + # Handle the response + print(response) + +# [END documentai_v1_generated_DocumentProcessorService_TrainProcessorVersion_async] diff --git a/samples/generated_samples/documentai_v1_generated_document_processor_service_train_processor_version_sync.py b/samples/generated_samples/documentai_v1_generated_document_processor_service_train_processor_version_sync.py new file mode 100644 index 00000000..8f4661bd --- /dev/null +++ b/samples/generated_samples/documentai_v1_generated_document_processor_service_train_processor_version_sync.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generated code. DO NOT EDIT! +# +# Snippet for TrainProcessorVersion +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-documentai + + +# [START documentai_v1_generated_DocumentProcessorService_TrainProcessorVersion_sync] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import documentai_v1 + + +def sample_train_processor_version(): + # Create a client + client = documentai_v1.DocumentProcessorServiceClient() + + # Initialize request argument(s) + request = documentai_v1.TrainProcessorVersionRequest( + parent="parent_value", + ) + + # Make the request + operation = client.train_processor_version(request=request) + + print("Waiting for operation to complete...") + + response = operation.result() + + # Handle the response + print(response) + +# [END documentai_v1_generated_DocumentProcessorService_TrainProcessorVersion_sync] diff --git a/samples/generated_samples/snippet_metadata_google.cloud.documentai.v1.json b/samples/generated_samples/snippet_metadata_google.cloud.documentai.v1.json index fe28a6f0..96d60af2 100644 --- a/samples/generated_samples/snippet_metadata_google.cloud.documentai.v1.json +++ b/samples/generated_samples/snippet_metadata_google.cloud.documentai.v1.json @@ -8,7 +8,7 @@ ], "language": "PYTHON", "name": "google-cloud-documentai", - "version": "2.12.0" + "version": "0.1.0" }, "snippets": [ { @@ -1130,6 +1130,167 @@ ], "title": "documentai_v1_generated_document_processor_service_enable_processor_sync.py" }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceAsyncClient", + "shortName": "DocumentProcessorServiceAsyncClient" + }, + "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceAsyncClient.evaluate_processor_version", + "method": { + "fullName": "google.cloud.documentai.v1.DocumentProcessorService.EvaluateProcessorVersion", + "service": { + "fullName": "google.cloud.documentai.v1.DocumentProcessorService", + "shortName": "DocumentProcessorService" + }, + "shortName": "EvaluateProcessorVersion" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.documentai_v1.types.EvaluateProcessorVersionRequest" + }, + { + "name": "processor_version", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, str]" + } + ], + "resultType": "google.api_core.operation_async.AsyncOperation", + "shortName": "evaluate_processor_version" + }, + "description": "Sample for EvaluateProcessorVersion", + "file": "documentai_v1_generated_document_processor_service_evaluate_processor_version_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "documentai_v1_generated_DocumentProcessorService_EvaluateProcessorVersion_async", + "segments": [ + { + "end": 55, + "start": 27, + "type": "FULL" + }, + { + "end": 55, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 52, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 56, + "start": 53, + "type": "RESPONSE_HANDLING" + } + ], + "title": "documentai_v1_generated_document_processor_service_evaluate_processor_version_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceClient", + "shortName": "DocumentProcessorServiceClient" + }, + "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceClient.evaluate_processor_version", + "method": { + "fullName": "google.cloud.documentai.v1.DocumentProcessorService.EvaluateProcessorVersion", + "service": { + "fullName": "google.cloud.documentai.v1.DocumentProcessorService", + "shortName": "DocumentProcessorService" + }, + "shortName": "EvaluateProcessorVersion" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.documentai_v1.types.EvaluateProcessorVersionRequest" + }, + { + "name": "processor_version", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, str]" + } + ], + "resultType": "google.api_core.operation.Operation", + "shortName": "evaluate_processor_version" + }, + "description": "Sample for EvaluateProcessorVersion", + "file": "documentai_v1_generated_document_processor_service_evaluate_processor_version_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "documentai_v1_generated_DocumentProcessorService_EvaluateProcessorVersion_sync", + "segments": [ + { + "end": 55, + "start": 27, + "type": "FULL" + }, + { + "end": 55, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 52, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 56, + "start": 53, + "type": "RESPONSE_HANDLING" + } + ], + "title": "documentai_v1_generated_document_processor_service_evaluate_processor_version_sync.py" + }, { "canonical": true, "clientMethod": { @@ -1291,6 +1452,167 @@ ], "title": "documentai_v1_generated_document_processor_service_fetch_processor_types_sync.py" }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceAsyncClient", + "shortName": "DocumentProcessorServiceAsyncClient" + }, + "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceAsyncClient.get_evaluation", + "method": { + "fullName": "google.cloud.documentai.v1.DocumentProcessorService.GetEvaluation", + "service": { + "fullName": "google.cloud.documentai.v1.DocumentProcessorService", + "shortName": "DocumentProcessorService" + }, + "shortName": "GetEvaluation" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.documentai_v1.types.GetEvaluationRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, str]" + } + ], + "resultType": "google.cloud.documentai_v1.types.Evaluation", + "shortName": "get_evaluation" + }, + "description": "Sample for GetEvaluation", + "file": "documentai_v1_generated_document_processor_service_get_evaluation_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "documentai_v1_generated_DocumentProcessorService_GetEvaluation_async", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "documentai_v1_generated_document_processor_service_get_evaluation_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceClient", + "shortName": "DocumentProcessorServiceClient" + }, + "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceClient.get_evaluation", + "method": { + "fullName": "google.cloud.documentai.v1.DocumentProcessorService.GetEvaluation", + "service": { + "fullName": "google.cloud.documentai.v1.DocumentProcessorService", + "shortName": "DocumentProcessorService" + }, + "shortName": "GetEvaluation" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.documentai_v1.types.GetEvaluationRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, str]" + } + ], + "resultType": "google.cloud.documentai_v1.types.Evaluation", + "shortName": "get_evaluation" + }, + "description": "Sample for GetEvaluation", + "file": "documentai_v1_generated_document_processor_service_get_evaluation_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "documentai_v1_generated_DocumentProcessorService_GetEvaluation_sync", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "documentai_v1_generated_document_processor_service_get_evaluation_sync.py" + }, { "canonical": true, "clientMethod": { @@ -1330,14 +1652,175 @@ "type": "Sequence[Tuple[str, str]" } ], - "resultType": "google.cloud.documentai_v1.types.ProcessorType", - "shortName": "get_processor_type" + "resultType": "google.cloud.documentai_v1.types.ProcessorType", + "shortName": "get_processor_type" + }, + "description": "Sample for GetProcessorType", + "file": "documentai_v1_generated_document_processor_service_get_processor_type_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "documentai_v1_generated_DocumentProcessorService_GetProcessorType_async", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "documentai_v1_generated_document_processor_service_get_processor_type_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceClient", + "shortName": "DocumentProcessorServiceClient" + }, + "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceClient.get_processor_type", + "method": { + "fullName": "google.cloud.documentai.v1.DocumentProcessorService.GetProcessorType", + "service": { + "fullName": "google.cloud.documentai.v1.DocumentProcessorService", + "shortName": "DocumentProcessorService" + }, + "shortName": "GetProcessorType" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.documentai_v1.types.GetProcessorTypeRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, str]" + } + ], + "resultType": "google.cloud.documentai_v1.types.ProcessorType", + "shortName": "get_processor_type" + }, + "description": "Sample for GetProcessorType", + "file": "documentai_v1_generated_document_processor_service_get_processor_type_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "documentai_v1_generated_DocumentProcessorService_GetProcessorType_sync", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "documentai_v1_generated_document_processor_service_get_processor_type_sync.py" + }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceAsyncClient", + "shortName": "DocumentProcessorServiceAsyncClient" + }, + "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceAsyncClient.get_processor_version", + "method": { + "fullName": "google.cloud.documentai.v1.DocumentProcessorService.GetProcessorVersion", + "service": { + "fullName": "google.cloud.documentai.v1.DocumentProcessorService", + "shortName": "DocumentProcessorService" + }, + "shortName": "GetProcessorVersion" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.documentai_v1.types.GetProcessorVersionRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, str]" + } + ], + "resultType": "google.cloud.documentai_v1.types.ProcessorVersion", + "shortName": "get_processor_version" }, - "description": "Sample for GetProcessorType", - "file": "documentai_v1_generated_document_processor_service_get_processor_type_async.py", + "description": "Sample for GetProcessorVersion", + "file": "documentai_v1_generated_document_processor_service_get_processor_version_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "documentai_v1_generated_DocumentProcessorService_GetProcessorType_async", + "regionTag": "documentai_v1_generated_DocumentProcessorService_GetProcessorVersion_async", "segments": [ { "end": 51, @@ -1370,7 +1853,7 @@ "type": "RESPONSE_HANDLING" } ], - "title": "documentai_v1_generated_document_processor_service_get_processor_type_async.py" + "title": "documentai_v1_generated_document_processor_service_get_processor_version_async.py" }, { "canonical": true, @@ -1379,19 +1862,19 @@ "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceClient", "shortName": "DocumentProcessorServiceClient" }, - "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceClient.get_processor_type", + "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceClient.get_processor_version", "method": { - "fullName": "google.cloud.documentai.v1.DocumentProcessorService.GetProcessorType", + "fullName": "google.cloud.documentai.v1.DocumentProcessorService.GetProcessorVersion", "service": { "fullName": "google.cloud.documentai.v1.DocumentProcessorService", "shortName": "DocumentProcessorService" }, - "shortName": "GetProcessorType" + "shortName": "GetProcessorVersion" }, "parameters": [ { "name": "request", - "type": "google.cloud.documentai_v1.types.GetProcessorTypeRequest" + "type": "google.cloud.documentai_v1.types.GetProcessorVersionRequest" }, { "name": "name", @@ -1410,14 +1893,14 @@ "type": "Sequence[Tuple[str, str]" } ], - "resultType": "google.cloud.documentai_v1.types.ProcessorType", - "shortName": "get_processor_type" + "resultType": "google.cloud.documentai_v1.types.ProcessorVersion", + "shortName": "get_processor_version" }, - "description": "Sample for GetProcessorType", - "file": "documentai_v1_generated_document_processor_service_get_processor_type_sync.py", + "description": "Sample for GetProcessorVersion", + "file": "documentai_v1_generated_document_processor_service_get_processor_version_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "documentai_v1_generated_DocumentProcessorService_GetProcessorType_sync", + "regionTag": "documentai_v1_generated_DocumentProcessorService_GetProcessorVersion_sync", "segments": [ { "end": 51, @@ -1450,7 +1933,7 @@ "type": "RESPONSE_HANDLING" } ], - "title": "documentai_v1_generated_document_processor_service_get_processor_type_sync.py" + "title": "documentai_v1_generated_document_processor_service_get_processor_version_sync.py" }, { "canonical": true, @@ -1460,19 +1943,19 @@ "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceAsyncClient", "shortName": "DocumentProcessorServiceAsyncClient" }, - "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceAsyncClient.get_processor_version", + "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceAsyncClient.get_processor", "method": { - "fullName": "google.cloud.documentai.v1.DocumentProcessorService.GetProcessorVersion", + "fullName": "google.cloud.documentai.v1.DocumentProcessorService.GetProcessor", "service": { "fullName": "google.cloud.documentai.v1.DocumentProcessorService", "shortName": "DocumentProcessorService" }, - "shortName": "GetProcessorVersion" + "shortName": "GetProcessor" }, "parameters": [ { "name": "request", - "type": "google.cloud.documentai_v1.types.GetProcessorVersionRequest" + "type": "google.cloud.documentai_v1.types.GetProcessorRequest" }, { "name": "name", @@ -1491,14 +1974,14 @@ "type": "Sequence[Tuple[str, str]" } ], - "resultType": "google.cloud.documentai_v1.types.ProcessorVersion", - "shortName": "get_processor_version" + "resultType": "google.cloud.documentai_v1.types.Processor", + "shortName": "get_processor" }, - "description": "Sample for GetProcessorVersion", - "file": "documentai_v1_generated_document_processor_service_get_processor_version_async.py", + "description": "Sample for GetProcessor", + "file": "documentai_v1_generated_document_processor_service_get_processor_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "documentai_v1_generated_DocumentProcessorService_GetProcessorVersion_async", + "regionTag": "documentai_v1_generated_DocumentProcessorService_GetProcessor_async", "segments": [ { "end": 51, @@ -1531,7 +2014,7 @@ "type": "RESPONSE_HANDLING" } ], - "title": "documentai_v1_generated_document_processor_service_get_processor_version_async.py" + "title": "documentai_v1_generated_document_processor_service_get_processor_async.py" }, { "canonical": true, @@ -1540,19 +2023,19 @@ "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceClient", "shortName": "DocumentProcessorServiceClient" }, - "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceClient.get_processor_version", + "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceClient.get_processor", "method": { - "fullName": "google.cloud.documentai.v1.DocumentProcessorService.GetProcessorVersion", + "fullName": "google.cloud.documentai.v1.DocumentProcessorService.GetProcessor", "service": { "fullName": "google.cloud.documentai.v1.DocumentProcessorService", "shortName": "DocumentProcessorService" }, - "shortName": "GetProcessorVersion" + "shortName": "GetProcessor" }, "parameters": [ { "name": "request", - "type": "google.cloud.documentai_v1.types.GetProcessorVersionRequest" + "type": "google.cloud.documentai_v1.types.GetProcessorRequest" }, { "name": "name", @@ -1571,14 +2054,14 @@ "type": "Sequence[Tuple[str, str]" } ], - "resultType": "google.cloud.documentai_v1.types.ProcessorVersion", - "shortName": "get_processor_version" + "resultType": "google.cloud.documentai_v1.types.Processor", + "shortName": "get_processor" }, - "description": "Sample for GetProcessorVersion", - "file": "documentai_v1_generated_document_processor_service_get_processor_version_sync.py", + "description": "Sample for GetProcessor", + "file": "documentai_v1_generated_document_processor_service_get_processor_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "documentai_v1_generated_DocumentProcessorService_GetProcessorVersion_sync", + "regionTag": "documentai_v1_generated_DocumentProcessorService_GetProcessor_sync", "segments": [ { "end": 51, @@ -1611,7 +2094,7 @@ "type": "RESPONSE_HANDLING" } ], - "title": "documentai_v1_generated_document_processor_service_get_processor_version_sync.py" + "title": "documentai_v1_generated_document_processor_service_get_processor_sync.py" }, { "canonical": true, @@ -1621,22 +2104,22 @@ "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceAsyncClient", "shortName": "DocumentProcessorServiceAsyncClient" }, - "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceAsyncClient.get_processor", + "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceAsyncClient.list_evaluations", "method": { - "fullName": "google.cloud.documentai.v1.DocumentProcessorService.GetProcessor", + "fullName": "google.cloud.documentai.v1.DocumentProcessorService.ListEvaluations", "service": { "fullName": "google.cloud.documentai.v1.DocumentProcessorService", "shortName": "DocumentProcessorService" }, - "shortName": "GetProcessor" + "shortName": "ListEvaluations" }, "parameters": [ { "name": "request", - "type": "google.cloud.documentai_v1.types.GetProcessorRequest" + "type": "google.cloud.documentai_v1.types.ListEvaluationsRequest" }, { - "name": "name", + "name": "parent", "type": "str" }, { @@ -1652,22 +2135,22 @@ "type": "Sequence[Tuple[str, str]" } ], - "resultType": "google.cloud.documentai_v1.types.Processor", - "shortName": "get_processor" + "resultType": "google.cloud.documentai_v1.services.document_processor_service.pagers.ListEvaluationsAsyncPager", + "shortName": "list_evaluations" }, - "description": "Sample for GetProcessor", - "file": "documentai_v1_generated_document_processor_service_get_processor_async.py", + "description": "Sample for ListEvaluations", + "file": "documentai_v1_generated_document_processor_service_list_evaluations_async.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "documentai_v1_generated_DocumentProcessorService_GetProcessor_async", + "regionTag": "documentai_v1_generated_DocumentProcessorService_ListEvaluations_async", "segments": [ { - "end": 51, + "end": 52, "start": 27, "type": "FULL" }, { - "end": 51, + "end": 52, "start": 27, "type": "SHORT" }, @@ -1687,12 +2170,12 @@ "type": "REQUEST_EXECUTION" }, { - "end": 52, + "end": 53, "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "documentai_v1_generated_document_processor_service_get_processor_async.py" + "title": "documentai_v1_generated_document_processor_service_list_evaluations_async.py" }, { "canonical": true, @@ -1701,22 +2184,22 @@ "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceClient", "shortName": "DocumentProcessorServiceClient" }, - "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceClient.get_processor", + "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceClient.list_evaluations", "method": { - "fullName": "google.cloud.documentai.v1.DocumentProcessorService.GetProcessor", + "fullName": "google.cloud.documentai.v1.DocumentProcessorService.ListEvaluations", "service": { "fullName": "google.cloud.documentai.v1.DocumentProcessorService", "shortName": "DocumentProcessorService" }, - "shortName": "GetProcessor" + "shortName": "ListEvaluations" }, "parameters": [ { "name": "request", - "type": "google.cloud.documentai_v1.types.GetProcessorRequest" + "type": "google.cloud.documentai_v1.types.ListEvaluationsRequest" }, { - "name": "name", + "name": "parent", "type": "str" }, { @@ -1732,22 +2215,22 @@ "type": "Sequence[Tuple[str, str]" } ], - "resultType": "google.cloud.documentai_v1.types.Processor", - "shortName": "get_processor" + "resultType": "google.cloud.documentai_v1.services.document_processor_service.pagers.ListEvaluationsPager", + "shortName": "list_evaluations" }, - "description": "Sample for GetProcessor", - "file": "documentai_v1_generated_document_processor_service_get_processor_sync.py", + "description": "Sample for ListEvaluations", + "file": "documentai_v1_generated_document_processor_service_list_evaluations_sync.py", "language": "PYTHON", "origin": "API_DEFINITION", - "regionTag": "documentai_v1_generated_DocumentProcessorService_GetProcessor_sync", + "regionTag": "documentai_v1_generated_DocumentProcessorService_ListEvaluations_sync", "segments": [ { - "end": 51, + "end": 52, "start": 27, "type": "FULL" }, { - "end": 51, + "end": 52, "start": 27, "type": "SHORT" }, @@ -1767,12 +2250,12 @@ "type": "REQUEST_EXECUTION" }, { - "end": 52, + "end": 53, "start": 49, "type": "RESPONSE_HANDLING" } ], - "title": "documentai_v1_generated_document_processor_service_get_processor_sync.py" + "title": "documentai_v1_generated_document_processor_service_list_evaluations_sync.py" }, { "canonical": true, @@ -2732,6 +3215,175 @@ ], "title": "documentai_v1_generated_document_processor_service_set_default_processor_version_sync.py" }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceAsyncClient", + "shortName": "DocumentProcessorServiceAsyncClient" + }, + "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceAsyncClient.train_processor_version", + "method": { + "fullName": "google.cloud.documentai.v1.DocumentProcessorService.TrainProcessorVersion", + "service": { + "fullName": "google.cloud.documentai.v1.DocumentProcessorService", + "shortName": "DocumentProcessorService" + }, + "shortName": "TrainProcessorVersion" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.documentai_v1.types.TrainProcessorVersionRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "processor_version", + "type": "google.cloud.documentai_v1.types.ProcessorVersion" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, str]" + } + ], + "resultType": "google.api_core.operation_async.AsyncOperation", + "shortName": "train_processor_version" + }, + "description": "Sample for TrainProcessorVersion", + "file": "documentai_v1_generated_document_processor_service_train_processor_version_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "documentai_v1_generated_DocumentProcessorService_TrainProcessorVersion_async", + "segments": [ + { + "end": 55, + "start": 27, + "type": "FULL" + }, + { + "end": 55, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 52, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 56, + "start": 53, + "type": "RESPONSE_HANDLING" + } + ], + "title": "documentai_v1_generated_document_processor_service_train_processor_version_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceClient", + "shortName": "DocumentProcessorServiceClient" + }, + "fullName": "google.cloud.documentai_v1.DocumentProcessorServiceClient.train_processor_version", + "method": { + "fullName": "google.cloud.documentai.v1.DocumentProcessorService.TrainProcessorVersion", + "service": { + "fullName": "google.cloud.documentai.v1.DocumentProcessorService", + "shortName": "DocumentProcessorService" + }, + "shortName": "TrainProcessorVersion" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.documentai_v1.types.TrainProcessorVersionRequest" + }, + { + "name": "parent", + "type": "str" + }, + { + "name": "processor_version", + "type": "google.cloud.documentai_v1.types.ProcessorVersion" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, str]" + } + ], + "resultType": "google.api_core.operation.Operation", + "shortName": "train_processor_version" + }, + "description": "Sample for TrainProcessorVersion", + "file": "documentai_v1_generated_document_processor_service_train_processor_version_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "documentai_v1_generated_DocumentProcessorService_TrainProcessorVersion_sync", + "segments": [ + { + "end": 55, + "start": 27, + "type": "FULL" + }, + { + "end": 55, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 52, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 56, + "start": 53, + "type": "RESPONSE_HANDLING" + } + ], + "title": "documentai_v1_generated_document_processor_service_train_processor_version_sync.py" + }, { "canonical": true, "clientMethod": { diff --git a/samples/generated_samples/snippet_metadata_google.cloud.documentai.v1beta2.json b/samples/generated_samples/snippet_metadata_google.cloud.documentai.v1beta2.json index bd0b6d31..ef56bd7e 100644 --- a/samples/generated_samples/snippet_metadata_google.cloud.documentai.v1beta2.json +++ b/samples/generated_samples/snippet_metadata_google.cloud.documentai.v1beta2.json @@ -8,7 +8,7 @@ ], "language": "PYTHON", "name": "google-cloud-documentai", - "version": "2.12.0" + "version": "0.1.0" }, "snippets": [ { diff --git a/samples/generated_samples/snippet_metadata_google.cloud.documentai.v1beta3.json b/samples/generated_samples/snippet_metadata_google.cloud.documentai.v1beta3.json index 0fc04ebf..f02752f1 100644 --- a/samples/generated_samples/snippet_metadata_google.cloud.documentai.v1beta3.json +++ b/samples/generated_samples/snippet_metadata_google.cloud.documentai.v1beta3.json @@ -8,7 +8,7 @@ ], "language": "PYTHON", "name": "google-cloud-documentai", - "version": "2.12.0" + "version": "0.1.0" }, "snippets": [ { diff --git a/scripts/fixup_documentai_v1_keywords.py b/scripts/fixup_documentai_v1_keywords.py index 465e9dd8..090486ac 100644 --- a/scripts/fixup_documentai_v1_keywords.py +++ b/scripts/fixup_documentai_v1_keywords.py @@ -46,16 +46,20 @@ class documentaiCallTransformer(cst.CSTTransformer): 'deploy_processor_version': ('name', ), 'disable_processor': ('name', ), 'enable_processor': ('name', ), + 'evaluate_processor_version': ('processor_version', 'evaluation_documents', ), 'fetch_processor_types': ('parent', ), + 'get_evaluation': ('name', ), 'get_processor': ('name', ), 'get_processor_type': ('name', ), 'get_processor_version': ('name', ), + 'list_evaluations': ('parent', 'page_size', 'page_token', ), 'list_processors': ('parent', 'page_size', 'page_token', ), 'list_processor_types': ('parent', 'page_size', 'page_token', ), 'list_processor_versions': ('parent', 'page_size', 'page_token', ), 'process_document': ('name', 'inline_document', 'raw_document', 'skip_human_review', 'field_mask', ), 'review_document': ('human_review_config', 'inline_document', 'enable_schema_validation', 'priority', 'document_schema', ), 'set_default_processor_version': ('processor', 'default_processor_version', ), + 'train_processor_version': ('parent', 'processor_version', 'document_schema', 'input_data', 'base_processor_version', ), 'undeploy_processor_version': ('name', ), } diff --git a/tests/unit/gapic/documentai_v1/test_document_processor_service.py b/tests/unit/gapic/documentai_v1/test_document_processor_service.py index 3c1469a4..99def329 100644 --- a/tests/unit/gapic/documentai_v1/test_document_processor_service.py +++ b/tests/unit/gapic/documentai_v1/test_document_processor_service.py @@ -78,6 +78,7 @@ document_io, document_processor_service, document_schema, + evaluation, geometry, ) from google.cloud.documentai_v1.types import processor @@ -2882,6 +2883,257 @@ async def test_get_processor_flattened_error_async(): ) +@pytest.mark.parametrize( + "request_type", + [ + document_processor_service.TrainProcessorVersionRequest, + dict, + ], +) +def test_train_processor_version(request_type, transport: str = "grpc"): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.train_processor_version), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/spam") + response = client.train_processor_version(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.TrainProcessorVersionRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) + + +def test_train_processor_version_empty_call(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.train_processor_version), "__call__" + ) as call: + client.train_processor_version() + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.TrainProcessorVersionRequest() + + +@pytest.mark.asyncio +async def test_train_processor_version_async( + transport: str = "grpc_asyncio", + request_type=document_processor_service.TrainProcessorVersionRequest, +): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.train_processor_version), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + response = await client.train_processor_version(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.TrainProcessorVersionRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) + + +@pytest.mark.asyncio +async def test_train_processor_version_async_from_dict(): + await test_train_processor_version_async(request_type=dict) + + +def test_train_processor_version_field_headers(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = document_processor_service.TrainProcessorVersionRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.train_processor_version), "__call__" + ) as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.train_processor_version(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_train_processor_version_field_headers_async(): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = document_processor_service.TrainProcessorVersionRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.train_processor_version), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/op") + ) + await client.train_processor_version(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +def test_train_processor_version_flattened(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.train_processor_version), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.train_processor_version( + parent="parent_value", + processor_version=processor.ProcessorVersion(name="name_value"), + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].processor_version + mock_val = processor.ProcessorVersion(name="name_value") + assert arg == mock_val + + +def test_train_processor_version_flattened_error(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.train_processor_version( + document_processor_service.TrainProcessorVersionRequest(), + parent="parent_value", + processor_version=processor.ProcessorVersion(name="name_value"), + ) + + +@pytest.mark.asyncio +async def test_train_processor_version_flattened_async(): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.train_processor_version), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.train_processor_version( + parent="parent_value", + processor_version=processor.ProcessorVersion(name="name_value"), + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + arg = args[0].processor_version + mock_val = processor.ProcessorVersion(name="name_value") + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_train_processor_version_flattened_error_async(): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.train_processor_version( + document_processor_service.TrainProcessorVersionRequest(), + parent="parent_value", + processor_version=processor.ProcessorVersion(name="name_value"), + ) + + @pytest.mark.parametrize( "request_type", [ @@ -5494,50 +5746,2128 @@ async def test_review_document_flattened_error_async(): @pytest.mark.parametrize( "request_type", [ - document_processor_service.ProcessRequest, + document_processor_service.EvaluateProcessorVersionRequest, dict, ], ) -def test_process_document_rest(request_type): +def test_evaluate_processor_version(request_type, transport: str = "grpc"): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), - transport="rest", + transport=transport, ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} - request = request_type(**request_init) - - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = document_processor_service.ProcessResponse() + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - pb_return_value = document_processor_service.ProcessResponse.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.evaluate_processor_version), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/spam") + response = client.evaluate_processor_version(request) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - response = client.process_document(request) + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.EvaluateProcessorVersionRequest() # Establish that the response is the type that we expect. - assert isinstance(response, document_processor_service.ProcessResponse) + assert isinstance(response, future.Future) -def test_process_document_rest_required_fields( - request_type=document_processor_service.ProcessRequest, -): - transport_class = transports.DocumentProcessorServiceRestTransport +def test_evaluate_processor_version_empty_call(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) - request_init = {} - request_init["name"] = "" - request = request_type(**request_init) - pb_request = request_type.pb(request) - jsonified_request = json.loads( - json_format.MessageToJson( + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.evaluate_processor_version), "__call__" + ) as call: + client.evaluate_processor_version() + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.EvaluateProcessorVersionRequest() + + +@pytest.mark.asyncio +async def test_evaluate_processor_version_async( + transport: str = "grpc_asyncio", + request_type=document_processor_service.EvaluateProcessorVersionRequest, +): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.evaluate_processor_version), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + response = await client.evaluate_processor_version(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.EvaluateProcessorVersionRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) + + +@pytest.mark.asyncio +async def test_evaluate_processor_version_async_from_dict(): + await test_evaluate_processor_version_async(request_type=dict) + + +def test_evaluate_processor_version_field_headers(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = document_processor_service.EvaluateProcessorVersionRequest() + + request.processor_version = "processor_version_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.evaluate_processor_version), "__call__" + ) as call: + call.return_value = operations_pb2.Operation(name="operations/op") + client.evaluate_processor_version(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "processor_version=processor_version_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_evaluate_processor_version_field_headers_async(): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = document_processor_service.EvaluateProcessorVersionRequest() + + request.processor_version = "processor_version_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.evaluate_processor_version), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/op") + ) + await client.evaluate_processor_version(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "processor_version=processor_version_value", + ) in kw["metadata"] + + +def test_evaluate_processor_version_flattened(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.evaluate_processor_version), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.evaluate_processor_version( + processor_version="processor_version_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].processor_version + mock_val = "processor_version_value" + assert arg == mock_val + + +def test_evaluate_processor_version_flattened_error(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.evaluate_processor_version( + document_processor_service.EvaluateProcessorVersionRequest(), + processor_version="processor_version_value", + ) + + +@pytest.mark.asyncio +async def test_evaluate_processor_version_flattened_async(): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.evaluate_processor_version), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.evaluate_processor_version( + processor_version="processor_version_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].processor_version + mock_val = "processor_version_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_evaluate_processor_version_flattened_error_async(): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.evaluate_processor_version( + document_processor_service.EvaluateProcessorVersionRequest(), + processor_version="processor_version_value", + ) + + +@pytest.mark.parametrize( + "request_type", + [ + document_processor_service.GetEvaluationRequest, + dict, + ], +) +def test_get_evaluation(request_type, transport: str = "grpc"): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_evaluation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = evaluation.Evaluation( + name="name_value", + kms_key_name="kms_key_name_value", + kms_key_version_name="kms_key_version_name_value", + ) + response = client.get_evaluation(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.GetEvaluationRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, evaluation.Evaluation) + assert response.name == "name_value" + assert response.kms_key_name == "kms_key_name_value" + assert response.kms_key_version_name == "kms_key_version_name_value" + + +def test_get_evaluation_empty_call(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_evaluation), "__call__") as call: + client.get_evaluation() + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.GetEvaluationRequest() + + +@pytest.mark.asyncio +async def test_get_evaluation_async( + transport: str = "grpc_asyncio", + request_type=document_processor_service.GetEvaluationRequest, +): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_evaluation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + evaluation.Evaluation( + name="name_value", + kms_key_name="kms_key_name_value", + kms_key_version_name="kms_key_version_name_value", + ) + ) + response = await client.get_evaluation(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.GetEvaluationRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, evaluation.Evaluation) + assert response.name == "name_value" + assert response.kms_key_name == "kms_key_name_value" + assert response.kms_key_version_name == "kms_key_version_name_value" + + +@pytest.mark.asyncio +async def test_get_evaluation_async_from_dict(): + await test_get_evaluation_async(request_type=dict) + + +def test_get_evaluation_field_headers(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = document_processor_service.GetEvaluationRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_evaluation), "__call__") as call: + call.return_value = evaluation.Evaluation() + client.get_evaluation(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_get_evaluation_field_headers_async(): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = document_processor_service.GetEvaluationRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_evaluation), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + evaluation.Evaluation() + ) + await client.get_evaluation(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] + + +def test_get_evaluation_flattened(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_evaluation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = evaluation.Evaluation() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_evaluation( + name="name_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +def test_get_evaluation_flattened_error(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_evaluation( + document_processor_service.GetEvaluationRequest(), + name="name_value", + ) + + +@pytest.mark.asyncio +async def test_get_evaluation_flattened_async(): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_evaluation), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = evaluation.Evaluation() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + evaluation.Evaluation() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_evaluation( + name="name_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_get_evaluation_flattened_error_async(): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.get_evaluation( + document_processor_service.GetEvaluationRequest(), + name="name_value", + ) + + +@pytest.mark.parametrize( + "request_type", + [ + document_processor_service.ListEvaluationsRequest, + dict, + ], +) +def test_list_evaluations(request_type, transport: str = "grpc"): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_evaluations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = document_processor_service.ListEvaluationsResponse( + next_page_token="next_page_token_value", + ) + response = client.list_evaluations(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.ListEvaluationsRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListEvaluationsPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_evaluations_empty_call(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_evaluations), "__call__") as call: + client.list_evaluations() + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.ListEvaluationsRequest() + + +@pytest.mark.asyncio +async def test_list_evaluations_async( + transport: str = "grpc_asyncio", + request_type=document_processor_service.ListEvaluationsRequest, +): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_evaluations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + document_processor_service.ListEvaluationsResponse( + next_page_token="next_page_token_value", + ) + ) + response = await client.list_evaluations(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == document_processor_service.ListEvaluationsRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListEvaluationsAsyncPager) + assert response.next_page_token == "next_page_token_value" + + +@pytest.mark.asyncio +async def test_list_evaluations_async_from_dict(): + await test_list_evaluations_async(request_type=dict) + + +def test_list_evaluations_field_headers(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = document_processor_service.ListEvaluationsRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_evaluations), "__call__") as call: + call.return_value = document_processor_service.ListEvaluationsResponse() + client.list_evaluations(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_list_evaluations_field_headers_async(): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = document_processor_service.ListEvaluationsRequest() + + request.parent = "parent_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_evaluations), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + document_processor_service.ListEvaluationsResponse() + ) + await client.list_evaluations(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "parent=parent_value", + ) in kw["metadata"] + + +def test_list_evaluations_flattened(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_evaluations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = document_processor_service.ListEvaluationsResponse() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.list_evaluations( + parent="parent_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + + +def test_list_evaluations_flattened_error(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_evaluations( + document_processor_service.ListEvaluationsRequest(), + parent="parent_value", + ) + + +@pytest.mark.asyncio +async def test_list_evaluations_flattened_async(): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_evaluations), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = document_processor_service.ListEvaluationsResponse() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + document_processor_service.ListEvaluationsResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.list_evaluations( + parent="parent_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].parent + mock_val = "parent_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_list_evaluations_flattened_error_async(): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.list_evaluations( + document_processor_service.ListEvaluationsRequest(), + parent="parent_value", + ) + + +def test_list_evaluations_pager(transport_name: str = "grpc"): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials, + transport=transport_name, + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_evaluations), "__call__") as call: + # Set the response to a series of pages. + call.side_effect = ( + document_processor_service.ListEvaluationsResponse( + evaluations=[ + evaluation.Evaluation(), + evaluation.Evaluation(), + evaluation.Evaluation(), + ], + next_page_token="abc", + ), + document_processor_service.ListEvaluationsResponse( + evaluations=[], + next_page_token="def", + ), + document_processor_service.ListEvaluationsResponse( + evaluations=[ + evaluation.Evaluation(), + ], + next_page_token="ghi", + ), + document_processor_service.ListEvaluationsResponse( + evaluations=[ + evaluation.Evaluation(), + evaluation.Evaluation(), + ], + ), + RuntimeError, + ) + + metadata = () + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("parent", ""),)), + ) + pager = client.list_evaluations(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, evaluation.Evaluation) for i in results) + + +def test_list_evaluations_pages(transport_name: str = "grpc"): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials, + transport=transport_name, + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_evaluations), "__call__") as call: + # Set the response to a series of pages. + call.side_effect = ( + document_processor_service.ListEvaluationsResponse( + evaluations=[ + evaluation.Evaluation(), + evaluation.Evaluation(), + evaluation.Evaluation(), + ], + next_page_token="abc", + ), + document_processor_service.ListEvaluationsResponse( + evaluations=[], + next_page_token="def", + ), + document_processor_service.ListEvaluationsResponse( + evaluations=[ + evaluation.Evaluation(), + ], + next_page_token="ghi", + ), + document_processor_service.ListEvaluationsResponse( + evaluations=[ + evaluation.Evaluation(), + evaluation.Evaluation(), + ], + ), + RuntimeError, + ) + pages = list(client.list_evaluations(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.asyncio +async def test_list_evaluations_async_pager(): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials, + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_evaluations), "__call__", new_callable=mock.AsyncMock + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + document_processor_service.ListEvaluationsResponse( + evaluations=[ + evaluation.Evaluation(), + evaluation.Evaluation(), + evaluation.Evaluation(), + ], + next_page_token="abc", + ), + document_processor_service.ListEvaluationsResponse( + evaluations=[], + next_page_token="def", + ), + document_processor_service.ListEvaluationsResponse( + evaluations=[ + evaluation.Evaluation(), + ], + next_page_token="ghi", + ), + document_processor_service.ListEvaluationsResponse( + evaluations=[ + evaluation.Evaluation(), + evaluation.Evaluation(), + ], + ), + RuntimeError, + ) + async_pager = await client.list_evaluations( + request={}, + ) + assert async_pager.next_page_token == "abc" + responses = [] + async for response in async_pager: # pragma: no branch + responses.append(response) + + assert len(responses) == 6 + assert all(isinstance(i, evaluation.Evaluation) for i in responses) + + +@pytest.mark.asyncio +async def test_list_evaluations_async_pages(): + client = DocumentProcessorServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials, + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.list_evaluations), "__call__", new_callable=mock.AsyncMock + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + document_processor_service.ListEvaluationsResponse( + evaluations=[ + evaluation.Evaluation(), + evaluation.Evaluation(), + evaluation.Evaluation(), + ], + next_page_token="abc", + ), + document_processor_service.ListEvaluationsResponse( + evaluations=[], + next_page_token="def", + ), + document_processor_service.ListEvaluationsResponse( + evaluations=[ + evaluation.Evaluation(), + ], + next_page_token="ghi", + ), + document_processor_service.ListEvaluationsResponse( + evaluations=[ + evaluation.Evaluation(), + evaluation.Evaluation(), + ], + ), + RuntimeError, + ) + pages = [] + async for page_ in ( + await client.list_evaluations(request={}) + ).pages: # pragma: no branch + pages.append(page_) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + document_processor_service.ProcessRequest, + dict, + ], +) +def test_process_document_rest(request_type): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = document_processor_service.ProcessResponse() + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = document_processor_service.ProcessResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.process_document(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, document_processor_service.ProcessResponse) + + +def test_process_document_rest_required_fields( + request_type=document_processor_service.ProcessRequest, +): + transport_class = transports.DocumentProcessorServiceRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).process_document._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).process_document._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = document_processor_service.ProcessResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = document_processor_service.ProcessResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.process_document(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_process_document_rest_unset_required_fields(): + transport = transports.DocumentProcessorServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.process_document._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_process_document_rest_interceptors(null_interceptor): + transport = transports.DocumentProcessorServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.DocumentProcessorServiceRestInterceptor(), + ) + client = DocumentProcessorServiceClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.DocumentProcessorServiceRestInterceptor, "post_process_document" + ) as post, mock.patch.object( + transports.DocumentProcessorServiceRestInterceptor, "pre_process_document" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = document_processor_service.ProcessRequest.pb( + document_processor_service.ProcessRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = document_processor_service.ProcessResponse.to_json( + document_processor_service.ProcessResponse() + ) + + request = document_processor_service.ProcessRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = document_processor_service.ProcessResponse() + + client.process_document( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_process_document_rest_bad_request( + transport: str = "rest", request_type=document_processor_service.ProcessRequest +): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.process_document(request) + + +def test_process_document_rest_flattened(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = document_processor_service.ProcessResponse() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/processors/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = document_processor_service.ProcessResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.process_document(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/processors/*}:process" + % client.transport._host, + args[1], + ) + + +def test_process_document_rest_flattened_error(transport: str = "rest"): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.process_document( + document_processor_service.ProcessRequest(), + name="name_value", + ) + + +def test_process_document_rest_error(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + document_processor_service.BatchProcessRequest, + dict, + ], +) +def test_batch_process_documents_rest(request_type): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.batch_process_documents(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_batch_process_documents_rest_required_fields( + request_type=document_processor_service.BatchProcessRequest, +): + transport_class = transports.DocumentProcessorServiceRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).batch_process_documents._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).batch_process_documents._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.batch_process_documents(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_batch_process_documents_rest_unset_required_fields(): + transport = transports.DocumentProcessorServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.batch_process_documents._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_batch_process_documents_rest_interceptors(null_interceptor): + transport = transports.DocumentProcessorServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.DocumentProcessorServiceRestInterceptor(), + ) + client = DocumentProcessorServiceClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.DocumentProcessorServiceRestInterceptor, + "post_batch_process_documents", + ) as post, mock.patch.object( + transports.DocumentProcessorServiceRestInterceptor, + "pre_batch_process_documents", + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = document_processor_service.BatchProcessRequest.pb( + document_processor_service.BatchProcessRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = document_processor_service.BatchProcessRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.batch_process_documents( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_batch_process_documents_rest_bad_request( + transport: str = "rest", request_type=document_processor_service.BatchProcessRequest +): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.batch_process_documents(request) + + +def test_batch_process_documents_rest_flattened(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/processors/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.batch_process_documents(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/processors/*}:batchProcess" + % client.transport._host, + args[1], + ) + + +def test_batch_process_documents_rest_flattened_error(transport: str = "rest"): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.batch_process_documents( + document_processor_service.BatchProcessRequest(), + name="name_value", + ) + + +def test_batch_process_documents_rest_error(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + document_processor_service.FetchProcessorTypesRequest, + dict, + ], +) +def test_fetch_processor_types_rest(request_type): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = document_processor_service.FetchProcessorTypesResponse() + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = document_processor_service.FetchProcessorTypesResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.fetch_processor_types(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, document_processor_service.FetchProcessorTypesResponse) + + +def test_fetch_processor_types_rest_required_fields( + request_type=document_processor_service.FetchProcessorTypesRequest, +): + transport_class = transports.DocumentProcessorServiceRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).fetch_processor_types._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).fetch_processor_types._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = document_processor_service.FetchProcessorTypesResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = document_processor_service.FetchProcessorTypesResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.fetch_processor_types(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_fetch_processor_types_rest_unset_required_fields(): + transport = transports.DocumentProcessorServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.fetch_processor_types._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("parent",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_fetch_processor_types_rest_interceptors(null_interceptor): + transport = transports.DocumentProcessorServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.DocumentProcessorServiceRestInterceptor(), + ) + client = DocumentProcessorServiceClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.DocumentProcessorServiceRestInterceptor, "post_fetch_processor_types" + ) as post, mock.patch.object( + transports.DocumentProcessorServiceRestInterceptor, "pre_fetch_processor_types" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = document_processor_service.FetchProcessorTypesRequest.pb( + document_processor_service.FetchProcessorTypesRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = ( + document_processor_service.FetchProcessorTypesResponse.to_json( + document_processor_service.FetchProcessorTypesResponse() + ) + ) + + request = document_processor_service.FetchProcessorTypesRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = document_processor_service.FetchProcessorTypesResponse() + + client.fetch_processor_types( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_fetch_processor_types_rest_bad_request( + transport: str = "rest", + request_type=document_processor_service.FetchProcessorTypesRequest, +): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.fetch_processor_types(request) + + +def test_fetch_processor_types_rest_flattened(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = document_processor_service.FetchProcessorTypesResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = document_processor_service.FetchProcessorTypesResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.fetch_processor_types(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}:fetchProcessorTypes" + % client.transport._host, + args[1], + ) + + +def test_fetch_processor_types_rest_flattened_error(transport: str = "rest"): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.fetch_processor_types( + document_processor_service.FetchProcessorTypesRequest(), + parent="parent_value", + ) + + +def test_fetch_processor_types_rest_error(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + document_processor_service.ListProcessorTypesRequest, + dict, + ], +) +def test_list_processor_types_rest(request_type): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = document_processor_service.ListProcessorTypesResponse( + next_page_token="next_page_token_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = document_processor_service.ListProcessorTypesResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.list_processor_types(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListProcessorTypesPager) + assert response.next_page_token == "next_page_token_value" + + +def test_list_processor_types_rest_required_fields( + request_type=document_processor_service.ListProcessorTypesRequest, +): + transport_class = transports.DocumentProcessorServiceRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_processor_types._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_processor_types._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = document_processor_service.ListProcessorTypesResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = document_processor_service.ListProcessorTypesResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.list_processor_types(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_processor_types_rest_unset_required_fields(): + transport = transports.DocumentProcessorServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_processor_types._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_processor_types_rest_interceptors(null_interceptor): + transport = transports.DocumentProcessorServiceRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.DocumentProcessorServiceRestInterceptor(), + ) + client = DocumentProcessorServiceClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.DocumentProcessorServiceRestInterceptor, "post_list_processor_types" + ) as post, mock.patch.object( + transports.DocumentProcessorServiceRestInterceptor, "pre_list_processor_types" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = document_processor_service.ListProcessorTypesRequest.pb( + document_processor_service.ListProcessorTypesRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = ( + document_processor_service.ListProcessorTypesResponse.to_json( + document_processor_service.ListProcessorTypesResponse() + ) + ) + + request = document_processor_service.ListProcessorTypesRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = document_processor_service.ListProcessorTypesResponse() + + client.list_processor_types( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_processor_types_rest_bad_request( + transport: str = "rest", + request_type=document_processor_service.ListProcessorTypesRequest, +): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_processor_types(request) + + +def test_list_processor_types_rest_flattened(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = document_processor_service.ListProcessorTypesResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = document_processor_service.ListProcessorTypesResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.list_processor_types(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/processorTypes" + % client.transport._host, + args[1], + ) + + +def test_list_processor_types_rest_flattened_error(transport: str = "rest"): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_processor_types( + document_processor_service.ListProcessorTypesRequest(), + parent="parent_value", + ) + + +def test_list_processor_types_rest_pager(transport: str = "rest"): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + document_processor_service.ListProcessorTypesResponse( + processor_types=[ + processor_type.ProcessorType(), + processor_type.ProcessorType(), + processor_type.ProcessorType(), + ], + next_page_token="abc", + ), + document_processor_service.ListProcessorTypesResponse( + processor_types=[], + next_page_token="def", + ), + document_processor_service.ListProcessorTypesResponse( + processor_types=[ + processor_type.ProcessorType(), + ], + next_page_token="ghi", + ), + document_processor_service.ListProcessorTypesResponse( + processor_types=[ + processor_type.ProcessorType(), + processor_type.ProcessorType(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + document_processor_service.ListProcessorTypesResponse.to_json(x) + for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = {"parent": "projects/sample1/locations/sample2"} + + pager = client.list_processor_types(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, processor_type.ProcessorType) for i in results) + + pages = list(client.list_processor_types(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + document_processor_service.GetProcessorTypeRequest, + dict, + ], +) +def test_get_processor_type_rest(request_type): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/processorTypes/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = processor_type.ProcessorType( + name="name_value", + type_="type__value", + category="category_value", + allow_creation=True, + launch_stage=launch_stage_pb2.LaunchStage.UNIMPLEMENTED, + sample_document_uris=["sample_document_uris_value"], + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = processor_type.ProcessorType.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.get_processor_type(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, processor_type.ProcessorType) + assert response.name == "name_value" + assert response.type_ == "type__value" + assert response.category == "category_value" + assert response.allow_creation is True + assert response.launch_stage == launch_stage_pb2.LaunchStage.UNIMPLEMENTED + assert response.sample_document_uris == ["sample_document_uris_value"] + + +def test_get_processor_type_rest_required_fields( + request_type=document_processor_service.GetProcessorTypeRequest, +): + transport_class = transports.DocumentProcessorServiceRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( pb_request, including_default_value_fields=False, use_integers_for_enums=False, @@ -5548,7 +7878,7 @@ def test_process_document_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).process_document._get_unset_required_fields(jsonified_request) + ).get_processor_type._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present @@ -5557,7 +7887,7 @@ def test_process_document_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).process_document._get_unset_required_fields(jsonified_request) + ).get_processor_type._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone @@ -5571,7 +7901,7 @@ def test_process_document_rest_required_fields( request = request_type(**request_init) # Designate an appropriate value for the returned response. - return_value = document_processor_service.ProcessResponse() + return_value = processor_type.ProcessorType() # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # We need to mock transcode() because providing default values @@ -5583,41 +7913,38 @@ def test_process_document_rest_required_fields( pb_request = request_type.pb(request) transcode_result = { "uri": "v1/sample_method", - "method": "post", + "method": "get", "query_params": pb_request, } - transcode_result["body"] = pb_request transcode.return_value = transcode_result response_value = Response() response_value.status_code = 200 - pb_return_value = document_processor_service.ProcessResponse.pb( - return_value - ) + pb_return_value = processor_type.ProcessorType.pb(return_value) json_return_value = json_format.MessageToJson(pb_return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.process_document(request) + response = client.get_processor_type(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_process_document_rest_unset_required_fields(): +def test_get_processor_type_rest_unset_required_fields(): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.process_document._get_unset_required_fields({}) + unset_fields = transport.get_processor_type._get_unset_required_fields({}) assert set(unset_fields) == (set(()) & set(("name",))) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_process_document_rest_interceptors(null_interceptor): +def test_get_processor_type_rest_interceptors(null_interceptor): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -5630,14 +7957,14 @@ def test_process_document_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "post_process_document" + transports.DocumentProcessorServiceRestInterceptor, "post_get_processor_type" ) as post, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "pre_process_document" + transports.DocumentProcessorServiceRestInterceptor, "pre_get_processor_type" ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = document_processor_service.ProcessRequest.pb( - document_processor_service.ProcessRequest() + pb_message = document_processor_service.GetProcessorTypeRequest.pb( + document_processor_service.GetProcessorTypeRequest() ) transcode.return_value = { "method": "post", @@ -5649,19 +7976,19 @@ def test_process_document_rest_interceptors(null_interceptor): req.return_value = Response() req.return_value.status_code = 200 req.return_value.request = PreparedRequest() - req.return_value._content = document_processor_service.ProcessResponse.to_json( - document_processor_service.ProcessResponse() + req.return_value._content = processor_type.ProcessorType.to_json( + processor_type.ProcessorType() ) - request = document_processor_service.ProcessRequest() + request = document_processor_service.GetProcessorTypeRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = document_processor_service.ProcessResponse() + post.return_value = processor_type.ProcessorType() - client.process_document( + client.get_processor_type( request, metadata=[ ("key", "val"), @@ -5673,8 +8000,9 @@ def test_process_document_rest_interceptors(null_interceptor): post.assert_called_once() -def test_process_document_rest_bad_request( - transport: str = "rest", request_type=document_processor_service.ProcessRequest +def test_get_processor_type_rest_bad_request( + transport: str = "rest", + request_type=document_processor_service.GetProcessorTypeRequest, ): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -5682,7 +8010,7 @@ def test_process_document_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} + request_init = {"name": "projects/sample1/locations/sample2/processorTypes/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -5694,10 +8022,10 @@ def test_process_document_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.process_document(request) + client.get_processor_type(request) -def test_process_document_rest_flattened(): +def test_get_processor_type_rest_flattened(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -5706,11 +8034,11 @@ def test_process_document_rest_flattened(): # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = document_processor_service.ProcessResponse() + return_value = processor_type.ProcessorType() # get arguments that satisfy an http rule for this method sample_request = { - "name": "projects/sample1/locations/sample2/processors/sample3" + "name": "projects/sample1/locations/sample2/processorTypes/sample3" } # get truthy value for each flattened field @@ -5722,25 +8050,25 @@ def test_process_document_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = document_processor_service.ProcessResponse.pb(return_value) + pb_return_value = processor_type.ProcessorType.pb(return_value) json_return_value = json_format.MessageToJson(pb_return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - client.process_document(**mock_args) + client.get_processor_type(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/processors/*}:process" + "%s/v1/{name=projects/*/locations/*/processorTypes/*}" % client.transport._host, args[1], ) -def test_process_document_rest_flattened_error(transport: str = "rest"): +def test_get_processor_type_rest_flattened_error(transport: str = "rest"): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -5749,13 +8077,13 @@ def test_process_document_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.process_document( - document_processor_service.ProcessRequest(), + client.get_processor_type( + document_processor_service.GetProcessorTypeRequest(), name="name_value", ) -def test_process_document_rest_error(): +def test_get_processor_type_rest_error(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -5764,45 +8092,51 @@ def test_process_document_rest_error(): @pytest.mark.parametrize( "request_type", [ - document_processor_service.BatchProcessRequest, + document_processor_service.ListProcessorsRequest, dict, ], ) -def test_batch_process_documents_rest(request_type): +def test_list_processors_rest(request_type): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} + request_init = {"parent": "projects/sample1/locations/sample2"} request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + return_value = document_processor_service.ListProcessorsResponse( + next_page_token="next_page_token_value", + ) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) + pb_return_value = document_processor_service.ListProcessorsResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(pb_return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.batch_process_documents(request) + response = client.list_processors(request) # Establish that the response is the type that we expect. - assert response.operation.name == "operations/spam" + assert isinstance(response, pagers.ListProcessorsPager) + assert response.next_page_token == "next_page_token_value" -def test_batch_process_documents_rest_required_fields( - request_type=document_processor_service.BatchProcessRequest, +def test_list_processors_rest_required_fields( + request_type=document_processor_service.ListProcessorsRequest, ): transport_class = transports.DocumentProcessorServiceRestTransport request_init = {} - request_init["name"] = "" + request_init["parent"] = "" request = request_type(**request_init) pb_request = request_type.pb(request) jsonified_request = json.loads( @@ -5817,21 +8151,28 @@ def test_batch_process_documents_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).batch_process_documents._get_unset_required_fields(jsonified_request) + ).list_processors._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present - jsonified_request["name"] = "name_value" + jsonified_request["parent"] = "parent_value" unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).batch_process_documents._get_unset_required_fields(jsonified_request) + ).list_processors._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "page_size", + "page_token", + ) + ) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -5840,7 +8181,7 @@ def test_batch_process_documents_rest_required_fields( request = request_type(**request_init) # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + return_value = document_processor_service.ListProcessorsResponse() # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # We need to mock transcode() because providing default values @@ -5852,37 +8193,48 @@ def test_batch_process_documents_rest_required_fields( pb_request = request_type.pb(request) transcode_result = { "uri": "v1/sample_method", - "method": "post", + "method": "get", "query_params": pb_request, } - transcode_result["body"] = pb_request transcode.return_value = transcode_result response_value = Response() response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) + + pb_return_value = document_processor_service.ListProcessorsResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(pb_return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.batch_process_documents(request) + response = client.list_processors(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_batch_process_documents_rest_unset_required_fields(): +def test_list_processors_rest_unset_required_fields(): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.batch_process_documents._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + unset_fields = transport.list_processors._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_batch_process_documents_rest_interceptors(null_interceptor): +def test_list_processors_rest_interceptors(null_interceptor): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -5895,18 +8247,14 @@ def test_batch_process_documents_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - operation.Operation, "_set_result_from_operation" - ), mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, - "post_batch_process_documents", + transports.DocumentProcessorServiceRestInterceptor, "post_list_processors" ) as post, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, - "pre_batch_process_documents", + transports.DocumentProcessorServiceRestInterceptor, "pre_list_processors" ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = document_processor_service.BatchProcessRequest.pb( - document_processor_service.BatchProcessRequest() + pb_message = document_processor_service.ListProcessorsRequest.pb( + document_processor_service.ListProcessorsRequest() ) transcode.return_value = { "method": "post", @@ -5918,19 +8266,21 @@ def test_batch_process_documents_rest_interceptors(null_interceptor): req.return_value = Response() req.return_value.status_code = 200 req.return_value.request = PreparedRequest() - req.return_value._content = json_format.MessageToJson( - operations_pb2.Operation() + req.return_value._content = ( + document_processor_service.ListProcessorsResponse.to_json( + document_processor_service.ListProcessorsResponse() + ) ) - request = document_processor_service.BatchProcessRequest() + request = document_processor_service.ListProcessorsRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = operations_pb2.Operation() + post.return_value = document_processor_service.ListProcessorsResponse() - client.batch_process_documents( + client.list_processors( request, metadata=[ ("key", "val"), @@ -5942,8 +8292,9 @@ def test_batch_process_documents_rest_interceptors(null_interceptor): post.assert_called_once() -def test_batch_process_documents_rest_bad_request( - transport: str = "rest", request_type=document_processor_service.BatchProcessRequest +def test_list_processors_rest_bad_request( + transport: str = "rest", + request_type=document_processor_service.ListProcessorsRequest, ): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -5951,7 +8302,7 @@ def test_batch_process_documents_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} + request_init = {"parent": "projects/sample1/locations/sample2"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -5963,10 +8314,10 @@ def test_batch_process_documents_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.batch_process_documents(request) + client.list_processors(request) -def test_batch_process_documents_rest_flattened(): +def test_list_processors_rest_flattened(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -5975,40 +8326,40 @@ def test_batch_process_documents_rest_flattened(): # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + return_value = document_processor_service.ListProcessorsResponse() # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/processors/sample3" - } + sample_request = {"parent": "projects/sample1/locations/sample2"} # get truthy value for each flattened field mock_args = dict( - name="name_value", + parent="parent_value", ) mock_args.update(sample_request) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) + pb_return_value = document_processor_service.ListProcessorsResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(pb_return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - client.batch_process_documents(**mock_args) + client.list_processors(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/processors/*}:batchProcess" - % client.transport._host, + "%s/v1/{parent=projects/*/locations/*}/processors" % client.transport._host, args[1], ) -def test_batch_process_documents_rest_flattened_error(transport: str = "rest"): +def test_list_processors_rest_flattened_error(transport: str = "rest"): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -6017,63 +8368,134 @@ def test_batch_process_documents_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.batch_process_documents( - document_processor_service.BatchProcessRequest(), - name="name_value", + client.list_processors( + document_processor_service.ListProcessorsRequest(), + parent="parent_value", ) -def test_batch_process_documents_rest_error(): +def test_list_processors_rest_pager(transport: str = "rest"): client = DocumentProcessorServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + document_processor_service.ListProcessorsResponse( + processors=[ + processor.Processor(), + processor.Processor(), + processor.Processor(), + ], + next_page_token="abc", + ), + document_processor_service.ListProcessorsResponse( + processors=[], + next_page_token="def", + ), + document_processor_service.ListProcessorsResponse( + processors=[ + processor.Processor(), + ], + next_page_token="ghi", + ), + document_processor_service.ListProcessorsResponse( + processors=[ + processor.Processor(), + processor.Processor(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + document_processor_service.ListProcessorsResponse.to_json(x) + for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = {"parent": "projects/sample1/locations/sample2"} + + pager = client.list_processors(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, processor.Processor) for i in results) + + pages = list(client.list_processors(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + @pytest.mark.parametrize( "request_type", [ - document_processor_service.FetchProcessorTypesRequest, + document_processor_service.GetProcessorRequest, dict, ], ) -def test_fetch_processor_types_rest(request_type): +def test_get_processor_rest(request_type): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = document_processor_service.FetchProcessorTypesResponse() + return_value = processor.Processor( + name="name_value", + type_="type__value", + display_name="display_name_value", + state=processor.Processor.State.ENABLED, + default_processor_version="default_processor_version_value", + process_endpoint="process_endpoint_value", + kms_key_name="kms_key_name_value", + ) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = document_processor_service.FetchProcessorTypesResponse.pb( - return_value - ) + pb_return_value = processor.Processor.pb(return_value) json_return_value = json_format.MessageToJson(pb_return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.fetch_processor_types(request) + response = client.get_processor(request) # Establish that the response is the type that we expect. - assert isinstance(response, document_processor_service.FetchProcessorTypesResponse) - + assert isinstance(response, processor.Processor) + assert response.name == "name_value" + assert response.type_ == "type__value" + assert response.display_name == "display_name_value" + assert response.state == processor.Processor.State.ENABLED + assert response.default_processor_version == "default_processor_version_value" + assert response.process_endpoint == "process_endpoint_value" + assert response.kms_key_name == "kms_key_name_value" -def test_fetch_processor_types_rest_required_fields( - request_type=document_processor_service.FetchProcessorTypesRequest, + +def test_get_processor_rest_required_fields( + request_type=document_processor_service.GetProcessorRequest, ): transport_class = transports.DocumentProcessorServiceRestTransport request_init = {} - request_init["parent"] = "" + request_init["name"] = "" request = request_type(**request_init) pb_request = request_type.pb(request) jsonified_request = json.loads( @@ -6088,21 +8510,21 @@ def test_fetch_processor_types_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).fetch_processor_types._get_unset_required_fields(jsonified_request) + ).get_processor._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present - jsonified_request["parent"] = "parent_value" + jsonified_request["name"] = "name_value" unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).fetch_processor_types._get_unset_required_fields(jsonified_request) + ).get_processor._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -6111,7 +8533,7 @@ def test_fetch_processor_types_rest_required_fields( request = request_type(**request_init) # Designate an appropriate value for the returned response. - return_value = document_processor_service.FetchProcessorTypesResponse() + return_value = processor.Processor() # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # We need to mock transcode() because providing default values @@ -6131,32 +8553,30 @@ def test_fetch_processor_types_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = document_processor_service.FetchProcessorTypesResponse.pb( - return_value - ) + pb_return_value = processor.Processor.pb(return_value) json_return_value = json_format.MessageToJson(pb_return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.fetch_processor_types(request) + response = client.get_processor(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_fetch_processor_types_rest_unset_required_fields(): +def test_get_processor_rest_unset_required_fields(): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.fetch_processor_types._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("parent",))) + unset_fields = transport.get_processor._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_fetch_processor_types_rest_interceptors(null_interceptor): +def test_get_processor_rest_interceptors(null_interceptor): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -6169,14 +8589,14 @@ def test_fetch_processor_types_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "post_fetch_processor_types" + transports.DocumentProcessorServiceRestInterceptor, "post_get_processor" ) as post, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "pre_fetch_processor_types" + transports.DocumentProcessorServiceRestInterceptor, "pre_get_processor" ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = document_processor_service.FetchProcessorTypesRequest.pb( - document_processor_service.FetchProcessorTypesRequest() + pb_message = document_processor_service.GetProcessorRequest.pb( + document_processor_service.GetProcessorRequest() ) transcode.return_value = { "method": "post", @@ -6188,21 +8608,17 @@ def test_fetch_processor_types_rest_interceptors(null_interceptor): req.return_value = Response() req.return_value.status_code = 200 req.return_value.request = PreparedRequest() - req.return_value._content = ( - document_processor_service.FetchProcessorTypesResponse.to_json( - document_processor_service.FetchProcessorTypesResponse() - ) - ) + req.return_value._content = processor.Processor.to_json(processor.Processor()) - request = document_processor_service.FetchProcessorTypesRequest() + request = document_processor_service.GetProcessorRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = document_processor_service.FetchProcessorTypesResponse() + post.return_value = processor.Processor() - client.fetch_processor_types( + client.get_processor( request, metadata=[ ("key", "val"), @@ -6214,9 +8630,8 @@ def test_fetch_processor_types_rest_interceptors(null_interceptor): post.assert_called_once() -def test_fetch_processor_types_rest_bad_request( - transport: str = "rest", - request_type=document_processor_service.FetchProcessorTypesRequest, +def test_get_processor_rest_bad_request( + transport: str = "rest", request_type=document_processor_service.GetProcessorRequest ): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -6224,7 +8639,7 @@ def test_fetch_processor_types_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -6236,10 +8651,10 @@ def test_fetch_processor_types_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.fetch_processor_types(request) + client.get_processor(request) -def test_fetch_processor_types_rest_flattened(): +def test_get_processor_rest_flattened(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -6248,41 +8663,40 @@ def test_fetch_processor_types_rest_flattened(): # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = document_processor_service.FetchProcessorTypesResponse() + return_value = processor.Processor() # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + sample_request = { + "name": "projects/sample1/locations/sample2/processors/sample3" + } # get truthy value for each flattened field mock_args = dict( - parent="parent_value", + name="name_value", ) mock_args.update(sample_request) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = document_processor_service.FetchProcessorTypesResponse.pb( - return_value - ) + pb_return_value = processor.Processor.pb(return_value) json_return_value = json_format.MessageToJson(pb_return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - client.fetch_processor_types(**mock_args) + client.get_processor(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}:fetchProcessorTypes" - % client.transport._host, + "%s/v1/{name=projects/*/locations/*/processors/*}" % client.transport._host, args[1], ) -def test_fetch_processor_types_rest_flattened_error(transport: str = "rest"): +def test_get_processor_rest_flattened_error(transport: str = "rest"): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -6291,13 +8705,13 @@ def test_fetch_processor_types_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.fetch_processor_types( - document_processor_service.FetchProcessorTypesRequest(), - parent="parent_value", + client.get_processor( + document_processor_service.GetProcessorRequest(), + name="name_value", ) -def test_fetch_processor_types_rest_error(): +def test_get_processor_rest_error(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -6306,46 +8720,40 @@ def test_fetch_processor_types_rest_error(): @pytest.mark.parametrize( "request_type", [ - document_processor_service.ListProcessorTypesRequest, + document_processor_service.TrainProcessorVersionRequest, dict, ], ) -def test_list_processor_types_rest(request_type): +def test_train_processor_version_rest(request_type): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + request_init = {"parent": "projects/sample1/locations/sample2/processors/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = document_processor_service.ListProcessorTypesResponse( - next_page_token="next_page_token_value", - ) + return_value = operations_pb2.Operation(name="operations/spam") # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = document_processor_service.ListProcessorTypesResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.list_processor_types(request) + response = client.train_processor_version(request) # Establish that the response is the type that we expect. - assert isinstance(response, pagers.ListProcessorTypesPager) - assert response.next_page_token == "next_page_token_value" + assert response.operation.name == "operations/spam" -def test_list_processor_types_rest_required_fields( - request_type=document_processor_service.ListProcessorTypesRequest, +def test_train_processor_version_rest_required_fields( + request_type=document_processor_service.TrainProcessorVersionRequest, ): transport_class = transports.DocumentProcessorServiceRestTransport @@ -6365,7 +8773,7 @@ def test_list_processor_types_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).list_processor_types._get_unset_required_fields(jsonified_request) + ).train_processor_version._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present @@ -6374,14 +8782,7 @@ def test_list_processor_types_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).list_processor_types._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "page_size", - "page_token", - ) - ) + ).train_processor_version._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone @@ -6395,7 +8796,7 @@ def test_list_processor_types_rest_required_fields( request = request_type(**request_init) # Designate an appropriate value for the returned response. - return_value = document_processor_service.ListProcessorTypesResponse() + return_value = operations_pb2.Operation(name="operations/spam") # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # We need to mock transcode() because providing default values @@ -6407,48 +8808,45 @@ def test_list_processor_types_rest_required_fields( pb_request = request_type.pb(request) transcode_result = { "uri": "v1/sample_method", - "method": "get", + "method": "post", "query_params": pb_request, } + transcode_result["body"] = pb_request transcode.return_value = transcode_result response_value = Response() response_value.status_code = 200 - - pb_return_value = document_processor_service.ListProcessorTypesResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.list_processor_types(request) + response = client.train_processor_version(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_list_processor_types_rest_unset_required_fields(): +def test_train_processor_version_rest_unset_required_fields(): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.list_processor_types._get_unset_required_fields({}) + unset_fields = transport.train_processor_version._get_unset_required_fields({}) assert set(unset_fields) == ( - set( + set(()) + & set( ( - "pageSize", - "pageToken", + "parent", + "processorVersion", ) ) - & set(("parent",)) ) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_list_processor_types_rest_interceptors(null_interceptor): +def test_train_processor_version_rest_interceptors(null_interceptor): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -6461,14 +8859,18 @@ def test_list_processor_types_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "post_list_processor_types" + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.DocumentProcessorServiceRestInterceptor, + "post_train_processor_version", ) as post, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "pre_list_processor_types" + transports.DocumentProcessorServiceRestInterceptor, + "pre_train_processor_version", ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = document_processor_service.ListProcessorTypesRequest.pb( - document_processor_service.ListProcessorTypesRequest() + pb_message = document_processor_service.TrainProcessorVersionRequest.pb( + document_processor_service.TrainProcessorVersionRequest() ) transcode.return_value = { "method": "post", @@ -6480,21 +8882,19 @@ def test_list_processor_types_rest_interceptors(null_interceptor): req.return_value = Response() req.return_value.status_code = 200 req.return_value.request = PreparedRequest() - req.return_value._content = ( - document_processor_service.ListProcessorTypesResponse.to_json( - document_processor_service.ListProcessorTypesResponse() - ) + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() ) - request = document_processor_service.ListProcessorTypesRequest() + request = document_processor_service.TrainProcessorVersionRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = document_processor_service.ListProcessorTypesResponse() + post.return_value = operations_pb2.Operation() - client.list_processor_types( + client.train_processor_version( request, metadata=[ ("key", "val"), @@ -6506,9 +8906,9 @@ def test_list_processor_types_rest_interceptors(null_interceptor): post.assert_called_once() -def test_list_processor_types_rest_bad_request( +def test_train_processor_version_rest_bad_request( transport: str = "rest", - request_type=document_processor_service.ListProcessorTypesRequest, + request_type=document_processor_service.TrainProcessorVersionRequest, ): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -6516,7 +8916,7 @@ def test_list_processor_types_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + request_init = {"parent": "projects/sample1/locations/sample2/processors/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -6528,10 +8928,10 @@ def test_list_processor_types_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.list_processor_types(request) + client.train_processor_version(request) -def test_list_processor_types_rest_flattened(): +def test_train_processor_version_rest_flattened(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -6540,41 +8940,41 @@ def test_list_processor_types_rest_flattened(): # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = document_processor_service.ListProcessorTypesResponse() + return_value = operations_pb2.Operation(name="operations/spam") # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + sample_request = { + "parent": "projects/sample1/locations/sample2/processors/sample3" + } # get truthy value for each flattened field mock_args = dict( parent="parent_value", + processor_version=processor.ProcessorVersion(name="name_value"), ) mock_args.update(sample_request) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = document_processor_service.ListProcessorTypesResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - client.list_processor_types(**mock_args) + client.train_processor_version(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/processorTypes" + "%s/v1/{parent=projects/*/locations/*/processors/*}/processorVersions:train" % client.transport._host, args[1], ) -def test_list_processor_types_rest_flattened_error(transport: str = "rest"): +def test_train_processor_version_rest_flattened_error(transport: str = "rest"): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -6583,127 +8983,72 @@ def test_list_processor_types_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.list_processor_types( - document_processor_service.ListProcessorTypesRequest(), + client.train_processor_version( + document_processor_service.TrainProcessorVersionRequest(), parent="parent_value", + processor_version=processor.ProcessorVersion(name="name_value"), ) -def test_list_processor_types_rest_pager(transport: str = "rest"): +def test_train_processor_version_rest_error(): client = DocumentProcessorServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # TODO(kbandes): remove this mock unless there's a good reason for it. - # with mock.patch.object(path_template, 'transcode') as transcode: - # Set the response as a series of pages - response = ( - document_processor_service.ListProcessorTypesResponse( - processor_types=[ - processor_type.ProcessorType(), - processor_type.ProcessorType(), - processor_type.ProcessorType(), - ], - next_page_token="abc", - ), - document_processor_service.ListProcessorTypesResponse( - processor_types=[], - next_page_token="def", - ), - document_processor_service.ListProcessorTypesResponse( - processor_types=[ - processor_type.ProcessorType(), - ], - next_page_token="ghi", - ), - document_processor_service.ListProcessorTypesResponse( - processor_types=[ - processor_type.ProcessorType(), - processor_type.ProcessorType(), - ], - ), - ) - # Two responses for two calls - response = response + response - - # Wrap the values into proper Response objs - response = tuple( - document_processor_service.ListProcessorTypesResponse.to_json(x) - for x in response - ) - return_values = tuple(Response() for i in response) - for return_val, response_val in zip(return_values, response): - return_val._content = response_val.encode("UTF-8") - return_val.status_code = 200 - req.side_effect = return_values - - sample_request = {"parent": "projects/sample1/locations/sample2"} - - pager = client.list_processor_types(request=sample_request) - - results = list(pager) - assert len(results) == 6 - assert all(isinstance(i, processor_type.ProcessorType) for i in results) - - pages = list(client.list_processor_types(request=sample_request).pages) - for page_, token in zip(pages, ["abc", "def", "ghi", ""]): - assert page_.raw_page.next_page_token == token - @pytest.mark.parametrize( "request_type", [ - document_processor_service.GetProcessorTypeRequest, + document_processor_service.GetProcessorVersionRequest, dict, ], ) -def test_get_processor_type_rest(request_type): +def test_get_processor_version_rest(request_type): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/processorTypes/sample3"} + request_init = { + "name": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" + } request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = processor_type.ProcessorType( - name="name_value", - type_="type__value", - category="category_value", - allow_creation=True, - launch_stage=launch_stage_pb2.LaunchStage.UNIMPLEMENTED, - sample_document_uris=["sample_document_uris_value"], + # Designate an appropriate value for the returned response. + return_value = processor.ProcessorVersion( + name="name_value", + display_name="display_name_value", + state=processor.ProcessorVersion.State.DEPLOYED, + kms_key_name="kms_key_name_value", + kms_key_version_name="kms_key_version_name_value", + google_managed=True, ) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = processor_type.ProcessorType.pb(return_value) + pb_return_value = processor.ProcessorVersion.pb(return_value) json_return_value = json_format.MessageToJson(pb_return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.get_processor_type(request) + response = client.get_processor_version(request) # Establish that the response is the type that we expect. - assert isinstance(response, processor_type.ProcessorType) + assert isinstance(response, processor.ProcessorVersion) assert response.name == "name_value" - assert response.type_ == "type__value" - assert response.category == "category_value" - assert response.allow_creation is True - assert response.launch_stage == launch_stage_pb2.LaunchStage.UNIMPLEMENTED - assert response.sample_document_uris == ["sample_document_uris_value"] + assert response.display_name == "display_name_value" + assert response.state == processor.ProcessorVersion.State.DEPLOYED + assert response.kms_key_name == "kms_key_name_value" + assert response.kms_key_version_name == "kms_key_version_name_value" + assert response.google_managed is True -def test_get_processor_type_rest_required_fields( - request_type=document_processor_service.GetProcessorTypeRequest, +def test_get_processor_version_rest_required_fields( + request_type=document_processor_service.GetProcessorVersionRequest, ): transport_class = transports.DocumentProcessorServiceRestTransport @@ -6723,7 +9068,7 @@ def test_get_processor_type_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).get_processor_type._get_unset_required_fields(jsonified_request) + ).get_processor_version._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present @@ -6732,7 +9077,7 @@ def test_get_processor_type_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).get_processor_type._get_unset_required_fields(jsonified_request) + ).get_processor_version._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone @@ -6746,7 +9091,7 @@ def test_get_processor_type_rest_required_fields( request = request_type(**request_init) # Designate an appropriate value for the returned response. - return_value = processor_type.ProcessorType() + return_value = processor.ProcessorVersion() # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # We need to mock transcode() because providing default values @@ -6766,30 +9111,30 @@ def test_get_processor_type_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = processor_type.ProcessorType.pb(return_value) + pb_return_value = processor.ProcessorVersion.pb(return_value) json_return_value = json_format.MessageToJson(pb_return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.get_processor_type(request) + response = client.get_processor_version(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_get_processor_type_rest_unset_required_fields(): +def test_get_processor_version_rest_unset_required_fields(): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.get_processor_type._get_unset_required_fields({}) + unset_fields = transport.get_processor_version._get_unset_required_fields({}) assert set(unset_fields) == (set(()) & set(("name",))) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_processor_type_rest_interceptors(null_interceptor): +def test_get_processor_version_rest_interceptors(null_interceptor): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -6802,14 +9147,14 @@ def test_get_processor_type_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "post_get_processor_type" + transports.DocumentProcessorServiceRestInterceptor, "post_get_processor_version" ) as post, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "pre_get_processor_type" + transports.DocumentProcessorServiceRestInterceptor, "pre_get_processor_version" ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = document_processor_service.GetProcessorTypeRequest.pb( - document_processor_service.GetProcessorTypeRequest() + pb_message = document_processor_service.GetProcessorVersionRequest.pb( + document_processor_service.GetProcessorVersionRequest() ) transcode.return_value = { "method": "post", @@ -6821,19 +9166,19 @@ def test_get_processor_type_rest_interceptors(null_interceptor): req.return_value = Response() req.return_value.status_code = 200 req.return_value.request = PreparedRequest() - req.return_value._content = processor_type.ProcessorType.to_json( - processor_type.ProcessorType() + req.return_value._content = processor.ProcessorVersion.to_json( + processor.ProcessorVersion() ) - request = document_processor_service.GetProcessorTypeRequest() + request = document_processor_service.GetProcessorVersionRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = processor_type.ProcessorType() + post.return_value = processor.ProcessorVersion() - client.get_processor_type( + client.get_processor_version( request, metadata=[ ("key", "val"), @@ -6845,9 +9190,9 @@ def test_get_processor_type_rest_interceptors(null_interceptor): post.assert_called_once() -def test_get_processor_type_rest_bad_request( +def test_get_processor_version_rest_bad_request( transport: str = "rest", - request_type=document_processor_service.GetProcessorTypeRequest, + request_type=document_processor_service.GetProcessorVersionRequest, ): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -6855,7 +9200,9 @@ def test_get_processor_type_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/processorTypes/sample3"} + request_init = { + "name": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" + } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -6867,10 +9214,10 @@ def test_get_processor_type_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.get_processor_type(request) + client.get_processor_version(request) -def test_get_processor_type_rest_flattened(): +def test_get_processor_version_rest_flattened(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -6879,11 +9226,11 @@ def test_get_processor_type_rest_flattened(): # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = processor_type.ProcessorType() + return_value = processor.ProcessorVersion() # get arguments that satisfy an http rule for this method sample_request = { - "name": "projects/sample1/locations/sample2/processorTypes/sample3" + "name": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" } # get truthy value for each flattened field @@ -6895,25 +9242,25 @@ def test_get_processor_type_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = processor_type.ProcessorType.pb(return_value) + pb_return_value = processor.ProcessorVersion.pb(return_value) json_return_value = json_format.MessageToJson(pb_return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - client.get_processor_type(**mock_args) + client.get_processor_version(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/processorTypes/*}" + "%s/v1/{name=projects/*/locations/*/processors/*/processorVersions/*}" % client.transport._host, args[1], ) -def test_get_processor_type_rest_flattened_error(transport: str = "rest"): +def test_get_processor_version_rest_flattened_error(transport: str = "rest"): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -6922,13 +9269,13 @@ def test_get_processor_type_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.get_processor_type( - document_processor_service.GetProcessorTypeRequest(), + client.get_processor_version( + document_processor_service.GetProcessorVersionRequest(), name="name_value", ) -def test_get_processor_type_rest_error(): +def test_get_processor_version_rest_error(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -6937,46 +9284,46 @@ def test_get_processor_type_rest_error(): @pytest.mark.parametrize( "request_type", [ - document_processor_service.ListProcessorsRequest, + document_processor_service.ListProcessorVersionsRequest, dict, ], ) -def test_list_processors_rest(request_type): +def test_list_processor_versions_rest(request_type): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + request_init = {"parent": "projects/sample1/locations/sample2/processors/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = document_processor_service.ListProcessorsResponse( + return_value = document_processor_service.ListProcessorVersionsResponse( next_page_token="next_page_token_value", ) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = document_processor_service.ListProcessorsResponse.pb( + pb_return_value = document_processor_service.ListProcessorVersionsResponse.pb( return_value ) json_return_value = json_format.MessageToJson(pb_return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.list_processors(request) + response = client.list_processor_versions(request) # Establish that the response is the type that we expect. - assert isinstance(response, pagers.ListProcessorsPager) + assert isinstance(response, pagers.ListProcessorVersionsPager) assert response.next_page_token == "next_page_token_value" -def test_list_processors_rest_required_fields( - request_type=document_processor_service.ListProcessorsRequest, +def test_list_processor_versions_rest_required_fields( + request_type=document_processor_service.ListProcessorVersionsRequest, ): transport_class = transports.DocumentProcessorServiceRestTransport @@ -6996,7 +9343,7 @@ def test_list_processors_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).list_processors._get_unset_required_fields(jsonified_request) + ).list_processor_versions._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present @@ -7005,7 +9352,7 @@ def test_list_processors_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).list_processors._get_unset_required_fields(jsonified_request) + ).list_processor_versions._get_unset_required_fields(jsonified_request) # Check that path parameters and body parameters are not mixing in. assert not set(unset_fields) - set( ( @@ -7026,7 +9373,7 @@ def test_list_processors_rest_required_fields( request = request_type(**request_init) # Designate an appropriate value for the returned response. - return_value = document_processor_service.ListProcessorsResponse() + return_value = document_processor_service.ListProcessorVersionsResponse() # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # We need to mock transcode() because providing default values @@ -7046,27 +9393,29 @@ def test_list_processors_rest_required_fields( response_value = Response() response_value.status_code = 200 - pb_return_value = document_processor_service.ListProcessorsResponse.pb( - return_value + pb_return_value = ( + document_processor_service.ListProcessorVersionsResponse.pb( + return_value + ) ) json_return_value = json_format.MessageToJson(pb_return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.list_processors(request) + response = client.list_processor_versions(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_list_processors_rest_unset_required_fields(): +def test_list_processor_versions_rest_unset_required_fields(): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.list_processors._get_unset_required_fields({}) + unset_fields = transport.list_processor_versions._get_unset_required_fields({}) assert set(unset_fields) == ( set( ( @@ -7079,7 +9428,7 @@ def test_list_processors_rest_unset_required_fields(): @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_list_processors_rest_interceptors(null_interceptor): +def test_list_processor_versions_rest_interceptors(null_interceptor): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -7092,14 +9441,16 @@ def test_list_processors_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "post_list_processors" + transports.DocumentProcessorServiceRestInterceptor, + "post_list_processor_versions", ) as post, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "pre_list_processors" + transports.DocumentProcessorServiceRestInterceptor, + "pre_list_processor_versions", ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = document_processor_service.ListProcessorsRequest.pb( - document_processor_service.ListProcessorsRequest() + pb_message = document_processor_service.ListProcessorVersionsRequest.pb( + document_processor_service.ListProcessorVersionsRequest() ) transcode.return_value = { "method": "post", @@ -7112,20 +9463,20 @@ def test_list_processors_rest_interceptors(null_interceptor): req.return_value.status_code = 200 req.return_value.request = PreparedRequest() req.return_value._content = ( - document_processor_service.ListProcessorsResponse.to_json( - document_processor_service.ListProcessorsResponse() + document_processor_service.ListProcessorVersionsResponse.to_json( + document_processor_service.ListProcessorVersionsResponse() ) ) - request = document_processor_service.ListProcessorsRequest() + request = document_processor_service.ListProcessorVersionsRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = document_processor_service.ListProcessorsResponse() + post.return_value = document_processor_service.ListProcessorVersionsResponse() - client.list_processors( + client.list_processor_versions( request, metadata=[ ("key", "val"), @@ -7137,9 +9488,9 @@ def test_list_processors_rest_interceptors(null_interceptor): post.assert_called_once() -def test_list_processors_rest_bad_request( +def test_list_processor_versions_rest_bad_request( transport: str = "rest", - request_type=document_processor_service.ListProcessorsRequest, + request_type=document_processor_service.ListProcessorVersionsRequest, ): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -7147,7 +9498,7 @@ def test_list_processors_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + request_init = {"parent": "projects/sample1/locations/sample2/processors/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -7159,10 +9510,10 @@ def test_list_processors_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.list_processors(request) + client.list_processor_versions(request) -def test_list_processors_rest_flattened(): +def test_list_processor_versions_rest_flattened(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -7171,10 +9522,12 @@ def test_list_processors_rest_flattened(): # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = document_processor_service.ListProcessorsResponse() + return_value = document_processor_service.ListProcessorVersionsResponse() # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + sample_request = { + "parent": "projects/sample1/locations/sample2/processors/sample3" + } # get truthy value for each flattened field mock_args = dict( @@ -7185,26 +9538,27 @@ def test_list_processors_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = document_processor_service.ListProcessorsResponse.pb( + pb_return_value = document_processor_service.ListProcessorVersionsResponse.pb( return_value ) json_return_value = json_format.MessageToJson(pb_return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - client.list_processors(**mock_args) + client.list_processor_versions(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/processors" % client.transport._host, + "%s/v1/{parent=projects/*/locations/*/processors/*}/processorVersions" + % client.transport._host, args[1], ) -def test_list_processors_rest_flattened_error(transport: str = "rest"): +def test_list_processor_versions_rest_flattened_error(transport: str = "rest"): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -7213,13 +9567,13 @@ def test_list_processors_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.list_processors( - document_processor_service.ListProcessorsRequest(), + client.list_processor_versions( + document_processor_service.ListProcessorVersionsRequest(), parent="parent_value", ) -def test_list_processors_rest_pager(transport: str = "rest"): +def test_list_processor_versions_rest_pager(transport: str = "rest"): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -7231,28 +9585,28 @@ def test_list_processors_rest_pager(transport: str = "rest"): # with mock.patch.object(path_template, 'transcode') as transcode: # Set the response as a series of pages response = ( - document_processor_service.ListProcessorsResponse( - processors=[ - processor.Processor(), - processor.Processor(), - processor.Processor(), + document_processor_service.ListProcessorVersionsResponse( + processor_versions=[ + processor.ProcessorVersion(), + processor.ProcessorVersion(), + processor.ProcessorVersion(), ], next_page_token="abc", ), - document_processor_service.ListProcessorsResponse( - processors=[], + document_processor_service.ListProcessorVersionsResponse( + processor_versions=[], next_page_token="def", ), - document_processor_service.ListProcessorsResponse( - processors=[ - processor.Processor(), + document_processor_service.ListProcessorVersionsResponse( + processor_versions=[ + processor.ProcessorVersion(), ], next_page_token="ghi", ), - document_processor_service.ListProcessorsResponse( - processors=[ - processor.Processor(), - processor.Processor(), + document_processor_service.ListProcessorVersionsResponse( + processor_versions=[ + processor.ProcessorVersion(), + processor.ProcessorVersion(), ], ), ) @@ -7261,7 +9615,7 @@ def test_list_processors_rest_pager(transport: str = "rest"): # Wrap the values into proper Response objs response = tuple( - document_processor_service.ListProcessorsResponse.to_json(x) + document_processor_service.ListProcessorVersionsResponse.to_json(x) for x in response ) return_values = tuple(Response() for i in response) @@ -7270,15 +9624,17 @@ def test_list_processors_rest_pager(transport: str = "rest"): return_val.status_code = 200 req.side_effect = return_values - sample_request = {"parent": "projects/sample1/locations/sample2"} + sample_request = { + "parent": "projects/sample1/locations/sample2/processors/sample3" + } - pager = client.list_processors(request=sample_request) + pager = client.list_processor_versions(request=sample_request) results = list(pager) assert len(results) == 6 - assert all(isinstance(i, processor.Processor) for i in results) + assert all(isinstance(i, processor.ProcessorVersion) for i in results) - pages = list(client.list_processors(request=sample_request).pages) + pages = list(client.list_processor_versions(request=sample_request).pages) for page_, token in zip(pages, ["abc", "def", "ghi", ""]): assert page_.raw_page.next_page_token == token @@ -7286,56 +9642,42 @@ def test_list_processors_rest_pager(transport: str = "rest"): @pytest.mark.parametrize( "request_type", [ - document_processor_service.GetProcessorRequest, + document_processor_service.DeleteProcessorVersionRequest, dict, ], ) -def test_get_processor_rest(request_type): +def test_delete_processor_version_rest(request_type): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} + request_init = { + "name": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" + } request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = processor.Processor( - name="name_value", - type_="type__value", - display_name="display_name_value", - state=processor.Processor.State.ENABLED, - default_processor_version="default_processor_version_value", - process_endpoint="process_endpoint_value", - kms_key_name="kms_key_name_value", - ) + return_value = operations_pb2.Operation(name="operations/spam") # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = processor.Processor.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.get_processor(request) + response = client.delete_processor_version(request) # Establish that the response is the type that we expect. - assert isinstance(response, processor.Processor) - assert response.name == "name_value" - assert response.type_ == "type__value" - assert response.display_name == "display_name_value" - assert response.state == processor.Processor.State.ENABLED - assert response.default_processor_version == "default_processor_version_value" - assert response.process_endpoint == "process_endpoint_value" - assert response.kms_key_name == "kms_key_name_value" + assert response.operation.name == "operations/spam" -def test_get_processor_rest_required_fields( - request_type=document_processor_service.GetProcessorRequest, +def test_delete_processor_version_rest_required_fields( + request_type=document_processor_service.DeleteProcessorVersionRequest, ): transport_class = transports.DocumentProcessorServiceRestTransport @@ -7355,7 +9697,7 @@ def test_get_processor_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).get_processor._get_unset_required_fields(jsonified_request) + ).delete_processor_version._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present @@ -7364,7 +9706,7 @@ def test_get_processor_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).get_processor._get_unset_required_fields(jsonified_request) + ).delete_processor_version._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone @@ -7378,7 +9720,7 @@ def test_get_processor_rest_required_fields( request = request_type(**request_init) # Designate an appropriate value for the returned response. - return_value = processor.Processor() + return_value = operations_pb2.Operation(name="operations/spam") # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # We need to mock transcode() because providing default values @@ -7390,38 +9732,36 @@ def test_get_processor_rest_required_fields( pb_request = request_type.pb(request) transcode_result = { "uri": "v1/sample_method", - "method": "get", + "method": "delete", "query_params": pb_request, } transcode.return_value = transcode_result response_value = Response() response_value.status_code = 200 - - pb_return_value = processor.Processor.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.get_processor(request) + response = client.delete_processor_version(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_get_processor_rest_unset_required_fields(): +def test_delete_processor_version_rest_unset_required_fields(): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.get_processor._get_unset_required_fields({}) + unset_fields = transport.delete_processor_version._get_unset_required_fields({}) assert set(unset_fields) == (set(()) & set(("name",))) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_processor_rest_interceptors(null_interceptor): +def test_delete_processor_version_rest_interceptors(null_interceptor): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -7434,14 +9774,18 @@ def test_get_processor_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "post_get_processor" + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.DocumentProcessorServiceRestInterceptor, + "post_delete_processor_version", ) as post, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "pre_get_processor" + transports.DocumentProcessorServiceRestInterceptor, + "pre_delete_processor_version", ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = document_processor_service.GetProcessorRequest.pb( - document_processor_service.GetProcessorRequest() + pb_message = document_processor_service.DeleteProcessorVersionRequest.pb( + document_processor_service.DeleteProcessorVersionRequest() ) transcode.return_value = { "method": "post", @@ -7453,17 +9797,19 @@ def test_get_processor_rest_interceptors(null_interceptor): req.return_value = Response() req.return_value.status_code = 200 req.return_value.request = PreparedRequest() - req.return_value._content = processor.Processor.to_json(processor.Processor()) + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) - request = document_processor_service.GetProcessorRequest() + request = document_processor_service.DeleteProcessorVersionRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = processor.Processor() + post.return_value = operations_pb2.Operation() - client.get_processor( + client.delete_processor_version( request, metadata=[ ("key", "val"), @@ -7475,8 +9821,9 @@ def test_get_processor_rest_interceptors(null_interceptor): post.assert_called_once() -def test_get_processor_rest_bad_request( - transport: str = "rest", request_type=document_processor_service.GetProcessorRequest +def test_delete_processor_version_rest_bad_request( + transport: str = "rest", + request_type=document_processor_service.DeleteProcessorVersionRequest, ): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -7484,7 +9831,9 @@ def test_get_processor_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} + request_init = { + "name": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" + } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -7496,10 +9845,10 @@ def test_get_processor_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.get_processor(request) + client.delete_processor_version(request) -def test_get_processor_rest_flattened(): +def test_delete_processor_version_rest_flattened(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -7508,11 +9857,11 @@ def test_get_processor_rest_flattened(): # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = processor.Processor() + return_value = operations_pb2.Operation(name="operations/spam") # get arguments that satisfy an http rule for this method sample_request = { - "name": "projects/sample1/locations/sample2/processors/sample3" + "name": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" } # get truthy value for each flattened field @@ -7524,24 +9873,24 @@ def test_get_processor_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = processor.Processor.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - client.get_processor(**mock_args) + client.delete_processor_version(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/processors/*}" % client.transport._host, + "%s/v1/{name=projects/*/locations/*/processors/*/processorVersions/*}" + % client.transport._host, args[1], ) -def test_get_processor_rest_flattened_error(transport: str = "rest"): +def test_delete_processor_version_rest_flattened_error(transport: str = "rest"): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -7550,13 +9899,13 @@ def test_get_processor_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.get_processor( - document_processor_service.GetProcessorRequest(), + client.delete_processor_version( + document_processor_service.DeleteProcessorVersionRequest(), name="name_value", ) -def test_get_processor_rest_error(): +def test_delete_processor_version_rest_error(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -7565,11 +9914,11 @@ def test_get_processor_rest_error(): @pytest.mark.parametrize( "request_type", [ - document_processor_service.GetProcessorVersionRequest, + document_processor_service.DeployProcessorVersionRequest, dict, ], ) -def test_get_processor_version_rest(request_type): +def test_deploy_processor_version_rest(request_type): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -7584,37 +9933,23 @@ def test_get_processor_version_rest(request_type): # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = processor.ProcessorVersion( - name="name_value", - display_name="display_name_value", - state=processor.ProcessorVersion.State.DEPLOYED, - kms_key_name="kms_key_name_value", - kms_key_version_name="kms_key_version_name_value", - google_managed=True, - ) + return_value = operations_pb2.Operation(name="operations/spam") # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = processor.ProcessorVersion.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.get_processor_version(request) + response = client.deploy_processor_version(request) # Establish that the response is the type that we expect. - assert isinstance(response, processor.ProcessorVersion) - assert response.name == "name_value" - assert response.display_name == "display_name_value" - assert response.state == processor.ProcessorVersion.State.DEPLOYED - assert response.kms_key_name == "kms_key_name_value" - assert response.kms_key_version_name == "kms_key_version_name_value" - assert response.google_managed is True + assert response.operation.name == "operations/spam" -def test_get_processor_version_rest_required_fields( - request_type=document_processor_service.GetProcessorVersionRequest, +def test_deploy_processor_version_rest_required_fields( + request_type=document_processor_service.DeployProcessorVersionRequest, ): transport_class = transports.DocumentProcessorServiceRestTransport @@ -7634,7 +9969,7 @@ def test_get_processor_version_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).get_processor_version._get_unset_required_fields(jsonified_request) + ).deploy_processor_version._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present @@ -7643,7 +9978,7 @@ def test_get_processor_version_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).get_processor_version._get_unset_required_fields(jsonified_request) + ).deploy_processor_version._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone @@ -7657,7 +9992,7 @@ def test_get_processor_version_rest_required_fields( request = request_type(**request_init) # Designate an appropriate value for the returned response. - return_value = processor.ProcessorVersion() + return_value = operations_pb2.Operation(name="operations/spam") # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # We need to mock transcode() because providing default values @@ -7669,38 +10004,37 @@ def test_get_processor_version_rest_required_fields( pb_request = request_type.pb(request) transcode_result = { "uri": "v1/sample_method", - "method": "get", + "method": "post", "query_params": pb_request, } + transcode_result["body"] = pb_request transcode.return_value = transcode_result response_value = Response() response_value.status_code = 200 - - pb_return_value = processor.ProcessorVersion.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.get_processor_version(request) + response = client.deploy_processor_version(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_get_processor_version_rest_unset_required_fields(): +def test_deploy_processor_version_rest_unset_required_fields(): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.get_processor_version._get_unset_required_fields({}) + unset_fields = transport.deploy_processor_version._get_unset_required_fields({}) assert set(unset_fields) == (set(()) & set(("name",))) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_processor_version_rest_interceptors(null_interceptor): +def test_deploy_processor_version_rest_interceptors(null_interceptor): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -7713,14 +10047,18 @@ def test_get_processor_version_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "post_get_processor_version" + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.DocumentProcessorServiceRestInterceptor, + "post_deploy_processor_version", ) as post, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "pre_get_processor_version" + transports.DocumentProcessorServiceRestInterceptor, + "pre_deploy_processor_version", ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = document_processor_service.GetProcessorVersionRequest.pb( - document_processor_service.GetProcessorVersionRequest() + pb_message = document_processor_service.DeployProcessorVersionRequest.pb( + document_processor_service.DeployProcessorVersionRequest() ) transcode.return_value = { "method": "post", @@ -7732,19 +10070,19 @@ def test_get_processor_version_rest_interceptors(null_interceptor): req.return_value = Response() req.return_value.status_code = 200 req.return_value.request = PreparedRequest() - req.return_value._content = processor.ProcessorVersion.to_json( - processor.ProcessorVersion() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() ) - request = document_processor_service.GetProcessorVersionRequest() + request = document_processor_service.DeployProcessorVersionRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = processor.ProcessorVersion() + post.return_value = operations_pb2.Operation() - client.get_processor_version( + client.deploy_processor_version( request, metadata=[ ("key", "val"), @@ -7756,9 +10094,9 @@ def test_get_processor_version_rest_interceptors(null_interceptor): post.assert_called_once() -def test_get_processor_version_rest_bad_request( +def test_deploy_processor_version_rest_bad_request( transport: str = "rest", - request_type=document_processor_service.GetProcessorVersionRequest, + request_type=document_processor_service.DeployProcessorVersionRequest, ): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -7780,10 +10118,10 @@ def test_get_processor_version_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.get_processor_version(request) + client.deploy_processor_version(request) -def test_get_processor_version_rest_flattened(): +def test_deploy_processor_version_rest_flattened(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -7792,7 +10130,7 @@ def test_get_processor_version_rest_flattened(): # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = processor.ProcessorVersion() + return_value = operations_pb2.Operation(name="operations/spam") # get arguments that satisfy an http rule for this method sample_request = { @@ -7808,25 +10146,24 @@ def test_get_processor_version_rest_flattened(): # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = processor.ProcessorVersion.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - client.get_processor_version(**mock_args) + client.deploy_processor_version(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/processors/*/processorVersions/*}" + "%s/v1/{name=projects/*/locations/*/processors/*/processorVersions/*}:deploy" % client.transport._host, args[1], ) -def test_get_processor_version_rest_flattened_error(transport: str = "rest"): +def test_deploy_processor_version_rest_flattened_error(transport: str = "rest"): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -7835,13 +10172,13 @@ def test_get_processor_version_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.get_processor_version( - document_processor_service.GetProcessorVersionRequest(), + client.deploy_processor_version( + document_processor_service.DeployProcessorVersionRequest(), name="name_value", ) -def test_get_processor_version_rest_error(): +def test_deploy_processor_version_rest_error(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -7850,51 +10187,47 @@ def test_get_processor_version_rest_error(): @pytest.mark.parametrize( "request_type", [ - document_processor_service.ListProcessorVersionsRequest, + document_processor_service.UndeployProcessorVersionRequest, dict, ], ) -def test_list_processor_versions_rest(request_type): +def test_undeploy_processor_version_rest(request_type): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2/processors/sample3"} + request_init = { + "name": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" + } request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = document_processor_service.ListProcessorVersionsResponse( - next_page_token="next_page_token_value", - ) + return_value = operations_pb2.Operation(name="operations/spam") # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = document_processor_service.ListProcessorVersionsResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.list_processor_versions(request) + response = client.undeploy_processor_version(request) # Establish that the response is the type that we expect. - assert isinstance(response, pagers.ListProcessorVersionsPager) - assert response.next_page_token == "next_page_token_value" + assert response.operation.name == "operations/spam" -def test_list_processor_versions_rest_required_fields( - request_type=document_processor_service.ListProcessorVersionsRequest, +def test_undeploy_processor_version_rest_required_fields( + request_type=document_processor_service.UndeployProcessorVersionRequest, ): transport_class = transports.DocumentProcessorServiceRestTransport request_init = {} - request_init["parent"] = "" + request_init["name"] = "" request = request_type(**request_init) pb_request = request_type.pb(request) jsonified_request = json.loads( @@ -7909,28 +10242,21 @@ def test_list_processor_versions_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).list_processor_versions._get_unset_required_fields(jsonified_request) + ).undeploy_processor_version._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present - jsonified_request["parent"] = "parent_value" + jsonified_request["name"] = "name_value" unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).list_processor_versions._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "page_size", - "page_token", - ) - ) + ).undeploy_processor_version._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -7939,7 +10265,7 @@ def test_list_processor_versions_rest_required_fields( request = request_type(**request_init) # Designate an appropriate value for the returned response. - return_value = document_processor_service.ListProcessorVersionsResponse() + return_value = operations_pb2.Operation(name="operations/spam") # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # We need to mock transcode() because providing default values @@ -7951,50 +10277,37 @@ def test_list_processor_versions_rest_required_fields( pb_request = request_type.pb(request) transcode_result = { "uri": "v1/sample_method", - "method": "get", + "method": "post", "query_params": pb_request, } + transcode_result["body"] = pb_request transcode.return_value = transcode_result response_value = Response() response_value.status_code = 200 - - pb_return_value = ( - document_processor_service.ListProcessorVersionsResponse.pb( - return_value - ) - ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.list_processor_versions(request) + response = client.undeploy_processor_version(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_list_processor_versions_rest_unset_required_fields(): +def test_undeploy_processor_version_rest_unset_required_fields(): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.list_processor_versions._get_unset_required_fields({}) - assert set(unset_fields) == ( - set( - ( - "pageSize", - "pageToken", - ) - ) - & set(("parent",)) - ) + unset_fields = transport.undeploy_processor_version._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_list_processor_versions_rest_interceptors(null_interceptor): +def test_undeploy_processor_version_rest_interceptors(null_interceptor): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -8007,16 +10320,18 @@ def test_list_processor_versions_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( transports.DocumentProcessorServiceRestInterceptor, - "post_list_processor_versions", + "post_undeploy_processor_version", ) as post, mock.patch.object( transports.DocumentProcessorServiceRestInterceptor, - "pre_list_processor_versions", + "pre_undeploy_processor_version", ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = document_processor_service.ListProcessorVersionsRequest.pb( - document_processor_service.ListProcessorVersionsRequest() + pb_message = document_processor_service.UndeployProcessorVersionRequest.pb( + document_processor_service.UndeployProcessorVersionRequest() ) transcode.return_value = { "method": "post", @@ -8028,21 +10343,19 @@ def test_list_processor_versions_rest_interceptors(null_interceptor): req.return_value = Response() req.return_value.status_code = 200 req.return_value.request = PreparedRequest() - req.return_value._content = ( - document_processor_service.ListProcessorVersionsResponse.to_json( - document_processor_service.ListProcessorVersionsResponse() - ) + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() ) - request = document_processor_service.ListProcessorVersionsRequest() + request = document_processor_service.UndeployProcessorVersionRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = document_processor_service.ListProcessorVersionsResponse() + post.return_value = operations_pb2.Operation() - client.list_processor_versions( + client.undeploy_processor_version( request, metadata=[ ("key", "val"), @@ -8054,9 +10367,9 @@ def test_list_processor_versions_rest_interceptors(null_interceptor): post.assert_called_once() -def test_list_processor_versions_rest_bad_request( +def test_undeploy_processor_version_rest_bad_request( transport: str = "rest", - request_type=document_processor_service.ListProcessorVersionsRequest, + request_type=document_processor_service.UndeployProcessorVersionRequest, ): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -8064,7 +10377,9 @@ def test_list_processor_versions_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2/processors/sample3"} + request_init = { + "name": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" + } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -8076,10 +10391,10 @@ def test_list_processor_versions_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.list_processor_versions(request) + client.undeploy_processor_version(request) -def test_list_processor_versions_rest_flattened(): +def test_undeploy_processor_version_rest_flattened(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -8088,43 +10403,40 @@ def test_list_processor_versions_rest_flattened(): # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = document_processor_service.ListProcessorVersionsResponse() + return_value = operations_pb2.Operation(name="operations/spam") # get arguments that satisfy an http rule for this method sample_request = { - "parent": "projects/sample1/locations/sample2/processors/sample3" + "name": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" } # get truthy value for each flattened field mock_args = dict( - parent="parent_value", + name="name_value", ) mock_args.update(sample_request) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = document_processor_service.ListProcessorVersionsResponse.pb( - return_value - ) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - client.list_processor_versions(**mock_args) + client.undeploy_processor_version(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*/processors/*}/processorVersions" + "%s/v1/{name=projects/*/locations/*/processors/*/processorVersions/*}:undeploy" % client.transport._host, args[1], ) -def test_list_processor_versions_rest_flattened_error(transport: str = "rest"): +def test_undeploy_processor_version_rest_flattened_error(transport: str = "rest"): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -8133,122 +10445,86 @@ def test_list_processor_versions_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.list_processor_versions( - document_processor_service.ListProcessorVersionsRequest(), - parent="parent_value", + client.undeploy_processor_version( + document_processor_service.UndeployProcessorVersionRequest(), + name="name_value", ) -def test_list_processor_versions_rest_pager(transport: str = "rest"): +def test_undeploy_processor_version_rest_error(): client = DocumentProcessorServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # TODO(kbandes): remove this mock unless there's a good reason for it. - # with mock.patch.object(path_template, 'transcode') as transcode: - # Set the response as a series of pages - response = ( - document_processor_service.ListProcessorVersionsResponse( - processor_versions=[ - processor.ProcessorVersion(), - processor.ProcessorVersion(), - processor.ProcessorVersion(), - ], - next_page_token="abc", - ), - document_processor_service.ListProcessorVersionsResponse( - processor_versions=[], - next_page_token="def", - ), - document_processor_service.ListProcessorVersionsResponse( - processor_versions=[ - processor.ProcessorVersion(), - ], - next_page_token="ghi", - ), - document_processor_service.ListProcessorVersionsResponse( - processor_versions=[ - processor.ProcessorVersion(), - processor.ProcessorVersion(), - ], - ), - ) - # Two responses for two calls - response = response + response - - # Wrap the values into proper Response objs - response = tuple( - document_processor_service.ListProcessorVersionsResponse.to_json(x) - for x in response - ) - return_values = tuple(Response() for i in response) - for return_val, response_val in zip(return_values, response): - return_val._content = response_val.encode("UTF-8") - return_val.status_code = 200 - req.side_effect = return_values - - sample_request = { - "parent": "projects/sample1/locations/sample2/processors/sample3" - } - - pager = client.list_processor_versions(request=sample_request) - - results = list(pager) - assert len(results) == 6 - assert all(isinstance(i, processor.ProcessorVersion) for i in results) - - pages = list(client.list_processor_versions(request=sample_request).pages) - for page_, token in zip(pages, ["abc", "def", "ghi", ""]): - assert page_.raw_page.next_page_token == token - @pytest.mark.parametrize( "request_type", [ - document_processor_service.DeleteProcessorVersionRequest, + document_processor_service.CreateProcessorRequest, dict, ], ) -def test_delete_processor_version_rest(request_type): +def test_create_processor_rest(request_type): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["processor"] = { + "name": "name_value", + "type_": "type__value", + "display_name": "display_name_value", + "state": 1, + "default_processor_version": "default_processor_version_value", + "process_endpoint": "process_endpoint_value", + "create_time": {"seconds": 751, "nanos": 543}, + "kms_key_name": "kms_key_name_value", } request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + return_value = gcd_processor.Processor( + name="name_value", + type_="type__value", + display_name="display_name_value", + state=gcd_processor.Processor.State.ENABLED, + default_processor_version="default_processor_version_value", + process_endpoint="process_endpoint_value", + kms_key_name="kms_key_name_value", + ) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) + pb_return_value = gcd_processor.Processor.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.delete_processor_version(request) + response = client.create_processor(request) # Establish that the response is the type that we expect. - assert response.operation.name == "operations/spam" + assert isinstance(response, gcd_processor.Processor) + assert response.name == "name_value" + assert response.type_ == "type__value" + assert response.display_name == "display_name_value" + assert response.state == gcd_processor.Processor.State.ENABLED + assert response.default_processor_version == "default_processor_version_value" + assert response.process_endpoint == "process_endpoint_value" + assert response.kms_key_name == "kms_key_name_value" -def test_delete_processor_version_rest_required_fields( - request_type=document_processor_service.DeleteProcessorVersionRequest, +def test_create_processor_rest_required_fields( + request_type=document_processor_service.CreateProcessorRequest, ): transport_class = transports.DocumentProcessorServiceRestTransport request_init = {} - request_init["name"] = "" + request_init["parent"] = "" request = request_type(**request_init) pb_request = request_type.pb(request) jsonified_request = json.loads( @@ -8263,21 +10539,21 @@ def test_delete_processor_version_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).delete_processor_version._get_unset_required_fields(jsonified_request) + ).create_processor._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present - jsonified_request["name"] = "name_value" + jsonified_request["parent"] = "parent_value" unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).delete_processor_version._get_unset_required_fields(jsonified_request) + ).create_processor._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -8286,7 +10562,7 @@ def test_delete_processor_version_rest_required_fields( request = request_type(**request_init) # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + return_value = gcd_processor.Processor() # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # We need to mock transcode() because providing default values @@ -8298,36 +10574,47 @@ def test_delete_processor_version_rest_required_fields( pb_request = request_type.pb(request) transcode_result = { "uri": "v1/sample_method", - "method": "delete", + "method": "post", "query_params": pb_request, } + transcode_result["body"] = pb_request transcode.return_value = transcode_result response_value = Response() response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) + + pb_return_value = gcd_processor.Processor.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.delete_processor_version(request) + response = client.create_processor(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_delete_processor_version_rest_unset_required_fields(): +def test_create_processor_rest_unset_required_fields(): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.delete_processor_version._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + unset_fields = transport.create_processor._get_unset_required_fields({}) + assert set(unset_fields) == ( + set(()) + & set( + ( + "parent", + "processor", + ) + ) + ) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_delete_processor_version_rest_interceptors(null_interceptor): +def test_create_processor_rest_interceptors(null_interceptor): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -8340,18 +10627,14 @@ def test_delete_processor_version_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - operation.Operation, "_set_result_from_operation" - ), mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, - "post_delete_processor_version", + transports.DocumentProcessorServiceRestInterceptor, "post_create_processor" ) as post, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, - "pre_delete_processor_version", + transports.DocumentProcessorServiceRestInterceptor, "pre_create_processor" ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = document_processor_service.DeleteProcessorVersionRequest.pb( - document_processor_service.DeleteProcessorVersionRequest() + pb_message = document_processor_service.CreateProcessorRequest.pb( + document_processor_service.CreateProcessorRequest() ) transcode.return_value = { "method": "post", @@ -8363,19 +10646,19 @@ def test_delete_processor_version_rest_interceptors(null_interceptor): req.return_value = Response() req.return_value.status_code = 200 req.return_value.request = PreparedRequest() - req.return_value._content = json_format.MessageToJson( - operations_pb2.Operation() + req.return_value._content = gcd_processor.Processor.to_json( + gcd_processor.Processor() ) - request = document_processor_service.DeleteProcessorVersionRequest() + request = document_processor_service.CreateProcessorRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = operations_pb2.Operation() + post.return_value = gcd_processor.Processor() - client.delete_processor_version( + client.create_processor( request, metadata=[ ("key", "val"), @@ -8387,9 +10670,9 @@ def test_delete_processor_version_rest_interceptors(null_interceptor): post.assert_called_once() -def test_delete_processor_version_rest_bad_request( +def test_create_processor_rest_bad_request( transport: str = "rest", - request_type=document_processor_service.DeleteProcessorVersionRequest, + request_type=document_processor_service.CreateProcessorRequest, ): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -8397,8 +10680,16 @@ def test_delete_processor_version_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["processor"] = { + "name": "name_value", + "type_": "type__value", + "display_name": "display_name_value", + "state": 1, + "default_processor_version": "default_processor_version_value", + "process_endpoint": "process_endpoint_value", + "create_time": {"seconds": 751, "nanos": 543}, + "kms_key_name": "kms_key_name_value", } request = request_type(**request_init) @@ -8411,10 +10702,10 @@ def test_delete_processor_version_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.delete_processor_version(request) + client.create_processor(request) -def test_delete_processor_version_rest_flattened(): +def test_create_processor_rest_flattened(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -8423,40 +10714,39 @@ def test_delete_processor_version_rest_flattened(): # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + return_value = gcd_processor.Processor() # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" - } + sample_request = {"parent": "projects/sample1/locations/sample2"} # get truthy value for each flattened field mock_args = dict( - name="name_value", + parent="parent_value", + processor=gcd_processor.Processor(name="name_value"), ) mock_args.update(sample_request) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) + pb_return_value = gcd_processor.Processor.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - client.delete_processor_version(**mock_args) + client.create_processor(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/processors/*/processorVersions/*}" - % client.transport._host, + "%s/v1/{parent=projects/*/locations/*}/processors" % client.transport._host, args[1], ) -def test_delete_processor_version_rest_flattened_error(transport: str = "rest"): +def test_create_processor_rest_flattened_error(transport: str = "rest"): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -8465,13 +10755,14 @@ def test_delete_processor_version_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.delete_processor_version( - document_processor_service.DeleteProcessorVersionRequest(), - name="name_value", + client.create_processor( + document_processor_service.CreateProcessorRequest(), + parent="parent_value", + processor=gcd_processor.Processor(name="name_value"), ) -def test_delete_processor_version_rest_error(): +def test_create_processor_rest_error(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -8480,20 +10771,18 @@ def test_delete_processor_version_rest_error(): @pytest.mark.parametrize( "request_type", [ - document_processor_service.DeployProcessorVersionRequest, + document_processor_service.DeleteProcessorRequest, dict, ], ) -def test_deploy_processor_version_rest(request_type): +def test_delete_processor_rest(request_type): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" - } + request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -8508,14 +10797,14 @@ def test_deploy_processor_version_rest(request_type): response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.deploy_processor_version(request) + response = client.delete_processor(request) # Establish that the response is the type that we expect. assert response.operation.name == "operations/spam" -def test_deploy_processor_version_rest_required_fields( - request_type=document_processor_service.DeployProcessorVersionRequest, +def test_delete_processor_rest_required_fields( + request_type=document_processor_service.DeleteProcessorRequest, ): transport_class = transports.DocumentProcessorServiceRestTransport @@ -8535,7 +10824,7 @@ def test_deploy_processor_version_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).deploy_processor_version._get_unset_required_fields(jsonified_request) + ).delete_processor._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present @@ -8544,7 +10833,7 @@ def test_deploy_processor_version_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).deploy_processor_version._get_unset_required_fields(jsonified_request) + ).delete_processor._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone @@ -8570,10 +10859,9 @@ def test_deploy_processor_version_rest_required_fields( pb_request = request_type.pb(request) transcode_result = { "uri": "v1/sample_method", - "method": "post", + "method": "delete", "query_params": pb_request, } - transcode_result["body"] = pb_request transcode.return_value = transcode_result response_value = Response() @@ -8583,24 +10871,24 @@ def test_deploy_processor_version_rest_required_fields( response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.deploy_processor_version(request) + response = client.delete_processor(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_deploy_processor_version_rest_unset_required_fields(): +def test_delete_processor_rest_unset_required_fields(): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.deploy_processor_version._get_unset_required_fields({}) + unset_fields = transport.delete_processor._get_unset_required_fields({}) assert set(unset_fields) == (set(()) & set(("name",))) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_deploy_processor_version_rest_interceptors(null_interceptor): +def test_delete_processor_rest_interceptors(null_interceptor): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -8615,16 +10903,14 @@ def test_deploy_processor_version_rest_interceptors(null_interceptor): ) as transcode, mock.patch.object( operation.Operation, "_set_result_from_operation" ), mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, - "post_deploy_processor_version", + transports.DocumentProcessorServiceRestInterceptor, "post_delete_processor" ) as post, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, - "pre_deploy_processor_version", + transports.DocumentProcessorServiceRestInterceptor, "pre_delete_processor" ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = document_processor_service.DeployProcessorVersionRequest.pb( - document_processor_service.DeployProcessorVersionRequest() + pb_message = document_processor_service.DeleteProcessorRequest.pb( + document_processor_service.DeleteProcessorRequest() ) transcode.return_value = { "method": "post", @@ -8640,7 +10926,7 @@ def test_deploy_processor_version_rest_interceptors(null_interceptor): operations_pb2.Operation() ) - request = document_processor_service.DeployProcessorVersionRequest() + request = document_processor_service.DeleteProcessorRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), @@ -8648,7 +10934,7 @@ def test_deploy_processor_version_rest_interceptors(null_interceptor): pre.return_value = request, metadata post.return_value = operations_pb2.Operation() - client.deploy_processor_version( + client.delete_processor( request, metadata=[ ("key", "val"), @@ -8660,9 +10946,9 @@ def test_deploy_processor_version_rest_interceptors(null_interceptor): post.assert_called_once() -def test_deploy_processor_version_rest_bad_request( +def test_delete_processor_rest_bad_request( transport: str = "rest", - request_type=document_processor_service.DeployProcessorVersionRequest, + request_type=document_processor_service.DeleteProcessorRequest, ): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -8670,9 +10956,7 @@ def test_deploy_processor_version_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" - } + request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -8684,10 +10968,10 @@ def test_deploy_processor_version_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.deploy_processor_version(request) + client.delete_processor(request) -def test_deploy_processor_version_rest_flattened(): +def test_delete_processor_rest_flattened(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -8700,7 +10984,7 @@ def test_deploy_processor_version_rest_flattened(): # get arguments that satisfy an http rule for this method sample_request = { - "name": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" + "name": "projects/sample1/locations/sample2/processors/sample3" } # get truthy value for each flattened field @@ -8716,20 +11000,19 @@ def test_deploy_processor_version_rest_flattened(): response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - client.deploy_processor_version(**mock_args) + client.delete_processor(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/processors/*/processorVersions/*}:deploy" - % client.transport._host, + "%s/v1/{name=projects/*/locations/*/processors/*}" % client.transport._host, args[1], ) -def test_deploy_processor_version_rest_flattened_error(transport: str = "rest"): +def test_delete_processor_rest_flattened_error(transport: str = "rest"): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -8738,13 +11021,13 @@ def test_deploy_processor_version_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.deploy_processor_version( - document_processor_service.DeployProcessorVersionRequest(), + client.delete_processor( + document_processor_service.DeleteProcessorRequest(), name="name_value", ) -def test_deploy_processor_version_rest_error(): +def test_delete_processor_rest_error(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -8753,20 +11036,18 @@ def test_deploy_processor_version_rest_error(): @pytest.mark.parametrize( "request_type", [ - document_processor_service.UndeployProcessorVersionRequest, + document_processor_service.EnableProcessorRequest, dict, ], ) -def test_undeploy_processor_version_rest(request_type): +def test_enable_processor_rest(request_type): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" - } + request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -8781,14 +11062,14 @@ def test_undeploy_processor_version_rest(request_type): response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.undeploy_processor_version(request) + response = client.enable_processor(request) # Establish that the response is the type that we expect. assert response.operation.name == "operations/spam" -def test_undeploy_processor_version_rest_required_fields( - request_type=document_processor_service.UndeployProcessorVersionRequest, +def test_enable_processor_rest_required_fields( + request_type=document_processor_service.EnableProcessorRequest, ): transport_class = transports.DocumentProcessorServiceRestTransport @@ -8808,7 +11089,7 @@ def test_undeploy_processor_version_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).undeploy_processor_version._get_unset_required_fields(jsonified_request) + ).enable_processor._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present @@ -8817,7 +11098,7 @@ def test_undeploy_processor_version_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).undeploy_processor_version._get_unset_required_fields(jsonified_request) + ).enable_processor._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone @@ -8856,24 +11137,24 @@ def test_undeploy_processor_version_rest_required_fields( response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.undeploy_processor_version(request) + response = client.enable_processor(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_undeploy_processor_version_rest_unset_required_fields(): +def test_enable_processor_rest_unset_required_fields(): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.undeploy_processor_version._get_unset_required_fields({}) + unset_fields = transport.enable_processor._get_unset_required_fields({}) assert set(unset_fields) == (set(()) & set(("name",))) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_undeploy_processor_version_rest_interceptors(null_interceptor): +def test_enable_processor_rest_interceptors(null_interceptor): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -8888,16 +11169,14 @@ def test_undeploy_processor_version_rest_interceptors(null_interceptor): ) as transcode, mock.patch.object( operation.Operation, "_set_result_from_operation" ), mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, - "post_undeploy_processor_version", + transports.DocumentProcessorServiceRestInterceptor, "post_enable_processor" ) as post, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, - "pre_undeploy_processor_version", + transports.DocumentProcessorServiceRestInterceptor, "pre_enable_processor" ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = document_processor_service.UndeployProcessorVersionRequest.pb( - document_processor_service.UndeployProcessorVersionRequest() + pb_message = document_processor_service.EnableProcessorRequest.pb( + document_processor_service.EnableProcessorRequest() ) transcode.return_value = { "method": "post", @@ -8913,7 +11192,7 @@ def test_undeploy_processor_version_rest_interceptors(null_interceptor): operations_pb2.Operation() ) - request = document_processor_service.UndeployProcessorVersionRequest() + request = document_processor_service.EnableProcessorRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), @@ -8921,7 +11200,7 @@ def test_undeploy_processor_version_rest_interceptors(null_interceptor): pre.return_value = request, metadata post.return_value = operations_pb2.Operation() - client.undeploy_processor_version( + client.enable_processor( request, metadata=[ ("key", "val"), @@ -8933,9 +11212,9 @@ def test_undeploy_processor_version_rest_interceptors(null_interceptor): post.assert_called_once() -def test_undeploy_processor_version_rest_bad_request( +def test_enable_processor_rest_bad_request( transport: str = "rest", - request_type=document_processor_service.UndeployProcessorVersionRequest, + request_type=document_processor_service.EnableProcessorRequest, ): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -8943,9 +11222,7 @@ def test_undeploy_processor_version_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = { - "name": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" - } + request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -8957,67 +11234,10 @@ def test_undeploy_processor_version_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.undeploy_processor_version(request) - - -def test_undeploy_processor_version_rest_flattened(): - client = DocumentProcessorServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") - - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" - } - - # get truthy value for each flattened field - mock_args = dict( - name="name_value", - ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - - client.undeploy_processor_version(**mock_args) - - # Establish that the underlying call was made with the expected - # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/processors/*/processorVersions/*}:undeploy" - % client.transport._host, - args[1], - ) - - -def test_undeploy_processor_version_rest_flattened_error(transport: str = "rest"): - client = DocumentProcessorServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.undeploy_processor_version( - document_processor_service.UndeployProcessorVersionRequest(), - name="name_value", - ) + client.enable_processor(request) -def test_undeploy_processor_version_rest_error(): +def test_enable_processor_rest_error(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -9026,71 +11246,45 @@ def test_undeploy_processor_version_rest_error(): @pytest.mark.parametrize( "request_type", [ - document_processor_service.CreateProcessorRequest, + document_processor_service.DisableProcessorRequest, dict, ], ) -def test_create_processor_rest(request_type): +def test_disable_processor_rest(request_type): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request_init["processor"] = { - "name": "name_value", - "type_": "type__value", - "display_name": "display_name_value", - "state": 1, - "default_processor_version": "default_processor_version_value", - "process_endpoint": "process_endpoint_value", - "create_time": {"seconds": 751, "nanos": 543}, - "kms_key_name": "kms_key_name_value", - } + request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = gcd_processor.Processor( - name="name_value", - type_="type__value", - display_name="display_name_value", - state=gcd_processor.Processor.State.ENABLED, - default_processor_version="default_processor_version_value", - process_endpoint="process_endpoint_value", - kms_key_name="kms_key_name_value", - ) + return_value = operations_pb2.Operation(name="operations/spam") # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - pb_return_value = gcd_processor.Processor.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.create_processor(request) + response = client.disable_processor(request) # Establish that the response is the type that we expect. - assert isinstance(response, gcd_processor.Processor) - assert response.name == "name_value" - assert response.type_ == "type__value" - assert response.display_name == "display_name_value" - assert response.state == gcd_processor.Processor.State.ENABLED - assert response.default_processor_version == "default_processor_version_value" - assert response.process_endpoint == "process_endpoint_value" - assert response.kms_key_name == "kms_key_name_value" + assert response.operation.name == "operations/spam" -def test_create_processor_rest_required_fields( - request_type=document_processor_service.CreateProcessorRequest, +def test_disable_processor_rest_required_fields( + request_type=document_processor_service.DisableProcessorRequest, ): transport_class = transports.DocumentProcessorServiceRestTransport request_init = {} - request_init["parent"] = "" + request_init["name"] = "" request = request_type(**request_init) pb_request = request_type.pb(request) jsonified_request = json.loads( @@ -9105,21 +11299,21 @@ def test_create_processor_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).create_processor._get_unset_required_fields(jsonified_request) + ).disable_processor._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present - jsonified_request["parent"] = "parent_value" + jsonified_request["name"] = "name_value" unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).create_processor._get_unset_required_fields(jsonified_request) + ).disable_processor._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -9128,7 +11322,7 @@ def test_create_processor_rest_required_fields( request = request_type(**request_init) # Designate an appropriate value for the returned response. - return_value = gcd_processor.Processor() + return_value = operations_pb2.Operation(name="operations/spam") # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # We need to mock transcode() because providing default values @@ -9148,39 +11342,29 @@ def test_create_processor_rest_required_fields( response_value = Response() response_value.status_code = 200 - - pb_return_value = gcd_processor.Processor.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) + json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.create_processor(request) + response = client.disable_processor(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_create_processor_rest_unset_required_fields(): +def test_disable_processor_rest_unset_required_fields(): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.create_processor._get_unset_required_fields({}) - assert set(unset_fields) == ( - set(()) - & set( - ( - "parent", - "processor", - ) - ) - ) + unset_fields = transport.disable_processor._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_create_processor_rest_interceptors(null_interceptor): +def test_disable_processor_rest_interceptors(null_interceptor): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -9193,14 +11377,16 @@ def test_create_processor_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "post_create_processor" + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.DocumentProcessorServiceRestInterceptor, "post_disable_processor" ) as post, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "pre_create_processor" + transports.DocumentProcessorServiceRestInterceptor, "pre_disable_processor" ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = document_processor_service.CreateProcessorRequest.pb( - document_processor_service.CreateProcessorRequest() + pb_message = document_processor_service.DisableProcessorRequest.pb( + document_processor_service.DisableProcessorRequest() ) transcode.return_value = { "method": "post", @@ -9212,19 +11398,19 @@ def test_create_processor_rest_interceptors(null_interceptor): req.return_value = Response() req.return_value.status_code = 200 req.return_value.request = PreparedRequest() - req.return_value._content = gcd_processor.Processor.to_json( - gcd_processor.Processor() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() ) - request = document_processor_service.CreateProcessorRequest() + request = document_processor_service.DisableProcessorRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = gcd_processor.Processor() + post.return_value = operations_pb2.Operation() - client.create_processor( + client.disable_processor( request, metadata=[ ("key", "val"), @@ -9236,9 +11422,9 @@ def test_create_processor_rest_interceptors(null_interceptor): post.assert_called_once() -def test_create_processor_rest_bad_request( +def test_disable_processor_rest_bad_request( transport: str = "rest", - request_type=document_processor_service.CreateProcessorRequest, + request_type=document_processor_service.DisableProcessorRequest, ): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -9246,17 +11432,7 @@ def test_create_processor_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request_init["processor"] = { - "name": "name_value", - "type_": "type__value", - "display_name": "display_name_value", - "state": 1, - "default_processor_version": "default_processor_version_value", - "process_endpoint": "process_endpoint_value", - "create_time": {"seconds": 751, "nanos": 543}, - "kms_key_name": "kms_key_name_value", - } + request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -9268,67 +11444,10 @@ def test_create_processor_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.create_processor(request) - - -def test_create_processor_rest_flattened(): - client = DocumentProcessorServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = gcd_processor.Processor() - - # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} - - # get truthy value for each flattened field - mock_args = dict( - parent="parent_value", - processor=gcd_processor.Processor(name="name_value"), - ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - pb_return_value = gcd_processor.Processor.pb(return_value) - json_return_value = json_format.MessageToJson(pb_return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - - client.create_processor(**mock_args) - - # Establish that the underlying call was made with the expected - # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/processors" % client.transport._host, - args[1], - ) - - -def test_create_processor_rest_flattened_error(transport: str = "rest"): - client = DocumentProcessorServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.create_processor( - document_processor_service.CreateProcessorRequest(), - parent="parent_value", - processor=gcd_processor.Processor(name="name_value"), - ) + client.disable_processor(request) -def test_create_processor_rest_error(): +def test_disable_processor_rest_error(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -9337,18 +11456,20 @@ def test_create_processor_rest_error(): @pytest.mark.parametrize( "request_type", [ - document_processor_service.DeleteProcessorRequest, + document_processor_service.SetDefaultProcessorVersionRequest, dict, ], ) -def test_delete_processor_rest(request_type): +def test_set_default_processor_version_rest(request_type): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} + request_init = { + "processor": "projects/sample1/locations/sample2/processors/sample3" + } request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -9363,19 +11484,20 @@ def test_delete_processor_rest(request_type): response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.delete_processor(request) + response = client.set_default_processor_version(request) # Establish that the response is the type that we expect. assert response.operation.name == "operations/spam" -def test_delete_processor_rest_required_fields( - request_type=document_processor_service.DeleteProcessorRequest, +def test_set_default_processor_version_rest_required_fields( + request_type=document_processor_service.SetDefaultProcessorVersionRequest, ): transport_class = transports.DocumentProcessorServiceRestTransport request_init = {} - request_init["name"] = "" + request_init["processor"] = "" + request_init["default_processor_version"] = "" request = request_type(**request_init) pb_request = request_type.pb(request) jsonified_request = json.loads( @@ -9390,21 +11512,27 @@ def test_delete_processor_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).delete_processor._get_unset_required_fields(jsonified_request) + ).set_default_processor_version._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present - jsonified_request["name"] = "name_value" + jsonified_request["processor"] = "processor_value" + jsonified_request["defaultProcessorVersion"] = "default_processor_version_value" unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).delete_processor._get_unset_required_fields(jsonified_request) + ).set_default_processor_version._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + assert "processor" in jsonified_request + assert jsonified_request["processor"] == "processor_value" + assert "defaultProcessorVersion" in jsonified_request + assert ( + jsonified_request["defaultProcessorVersion"] + == "default_processor_version_value" + ) client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -9425,9 +11553,10 @@ def test_delete_processor_rest_required_fields( pb_request = request_type.pb(request) transcode_result = { "uri": "v1/sample_method", - "method": "delete", + "method": "post", "query_params": pb_request, } + transcode_result["body"] = pb_request transcode.return_value = transcode_result response_value = Response() @@ -9437,24 +11566,34 @@ def test_delete_processor_rest_required_fields( response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.delete_processor(request) + response = client.set_default_processor_version(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_delete_processor_rest_unset_required_fields(): +def test_set_default_processor_version_rest_unset_required_fields(): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.delete_processor._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + unset_fields = transport.set_default_processor_version._get_unset_required_fields( + {} + ) + assert set(unset_fields) == ( + set(()) + & set( + ( + "processor", + "defaultProcessorVersion", + ) + ) + ) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_delete_processor_rest_interceptors(null_interceptor): +def test_set_default_processor_version_rest_interceptors(null_interceptor): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -9469,14 +11608,16 @@ def test_delete_processor_rest_interceptors(null_interceptor): ) as transcode, mock.patch.object( operation.Operation, "_set_result_from_operation" ), mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "post_delete_processor" + transports.DocumentProcessorServiceRestInterceptor, + "post_set_default_processor_version", ) as post, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "pre_delete_processor" + transports.DocumentProcessorServiceRestInterceptor, + "pre_set_default_processor_version", ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = document_processor_service.DeleteProcessorRequest.pb( - document_processor_service.DeleteProcessorRequest() + pb_message = document_processor_service.SetDefaultProcessorVersionRequest.pb( + document_processor_service.SetDefaultProcessorVersionRequest() ) transcode.return_value = { "method": "post", @@ -9492,7 +11633,7 @@ def test_delete_processor_rest_interceptors(null_interceptor): operations_pb2.Operation() ) - request = document_processor_service.DeleteProcessorRequest() + request = document_processor_service.SetDefaultProcessorVersionRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), @@ -9500,7 +11641,7 @@ def test_delete_processor_rest_interceptors(null_interceptor): pre.return_value = request, metadata post.return_value = operations_pb2.Operation() - client.delete_processor( + client.set_default_processor_version( request, metadata=[ ("key", "val"), @@ -9512,88 +11653,34 @@ def test_delete_processor_rest_interceptors(null_interceptor): post.assert_called_once() -def test_delete_processor_rest_bad_request( +def test_set_default_processor_version_rest_bad_request( transport: str = "rest", - request_type=document_processor_service.DeleteProcessorRequest, + request_type=document_processor_service.SetDefaultProcessorVersionRequest, ): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, ) - # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} - request = request_type(**request_init) - - # Mock the http request call within the method and fake a BadRequest error. - with mock.patch.object(Session, "request") as req, pytest.raises( - core_exceptions.BadRequest - ): - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 400 - response_value.request = Request() - req.return_value = response_value - client.delete_processor(request) - - -def test_delete_processor_rest_flattened(): - client = DocumentProcessorServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport="rest", - ) - - # Mock the http request call within the method and fake a response. - with mock.patch.object(type(client.transport._session), "request") as req: - # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") - - # get arguments that satisfy an http rule for this method - sample_request = { - "name": "projects/sample1/locations/sample2/processors/sample3" - } - - # get truthy value for each flattened field - mock_args = dict( - name="name_value", - ) - mock_args.update(sample_request) - - # Wrap the value into a proper Response obj - response_value = Response() - response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) - response_value._content = json_return_value.encode("UTF-8") - req.return_value = response_value - - client.delete_processor(**mock_args) - - # Establish that the underlying call was made with the expected - # request object values. - assert len(req.mock_calls) == 1 - _, args, _ = req.mock_calls[0] - assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/processors/*}" % client.transport._host, - args[1], - ) - - -def test_delete_processor_rest_flattened_error(transport: str = "rest"): - client = DocumentProcessorServiceClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, - ) - - # Attempting to call a method with both a request object and flattened - # fields is an error. - with pytest.raises(ValueError): - client.delete_processor( - document_processor_service.DeleteProcessorRequest(), - name="name_value", - ) + # send a request that will satisfy transcoding + request_init = { + "processor": "projects/sample1/locations/sample2/processors/sample3" + } + request = request_type(**request_init) + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.set_default_processor_version(request) -def test_delete_processor_rest_error(): + +def test_set_default_processor_version_rest_error(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -9602,18 +11689,20 @@ def test_delete_processor_rest_error(): @pytest.mark.parametrize( "request_type", [ - document_processor_service.EnableProcessorRequest, + document_processor_service.ReviewDocumentRequest, dict, ], ) -def test_enable_processor_rest(request_type): +def test_review_document_rest(request_type): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} + request_init = { + "human_review_config": "projects/sample1/locations/sample2/processors/sample3/humanReviewConfig" + } request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -9628,19 +11717,19 @@ def test_enable_processor_rest(request_type): response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.enable_processor(request) + response = client.review_document(request) # Establish that the response is the type that we expect. assert response.operation.name == "operations/spam" -def test_enable_processor_rest_required_fields( - request_type=document_processor_service.EnableProcessorRequest, +def test_review_document_rest_required_fields( + request_type=document_processor_service.ReviewDocumentRequest, ): transport_class = transports.DocumentProcessorServiceRestTransport request_init = {} - request_init["name"] = "" + request_init["human_review_config"] = "" request = request_type(**request_init) pb_request = request_type.pb(request) jsonified_request = json.loads( @@ -9655,21 +11744,21 @@ def test_enable_processor_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).enable_processor._get_unset_required_fields(jsonified_request) + ).review_document._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present - jsonified_request["name"] = "name_value" + jsonified_request["humanReviewConfig"] = "human_review_config_value" unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).enable_processor._get_unset_required_fields(jsonified_request) + ).review_document._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + assert "humanReviewConfig" in jsonified_request + assert jsonified_request["humanReviewConfig"] == "human_review_config_value" client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -9703,24 +11792,24 @@ def test_enable_processor_rest_required_fields( response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.enable_processor(request) + response = client.review_document(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_enable_processor_rest_unset_required_fields(): +def test_review_document_rest_unset_required_fields(): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.enable_processor._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + unset_fields = transport.review_document._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("humanReviewConfig",))) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_enable_processor_rest_interceptors(null_interceptor): +def test_review_document_rest_interceptors(null_interceptor): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -9735,14 +11824,14 @@ def test_enable_processor_rest_interceptors(null_interceptor): ) as transcode, mock.patch.object( operation.Operation, "_set_result_from_operation" ), mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "post_enable_processor" + transports.DocumentProcessorServiceRestInterceptor, "post_review_document" ) as post, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "pre_enable_processor" + transports.DocumentProcessorServiceRestInterceptor, "pre_review_document" ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = document_processor_service.EnableProcessorRequest.pb( - document_processor_service.EnableProcessorRequest() + pb_message = document_processor_service.ReviewDocumentRequest.pb( + document_processor_service.ReviewDocumentRequest() ) transcode.return_value = { "method": "post", @@ -9758,7 +11847,7 @@ def test_enable_processor_rest_interceptors(null_interceptor): operations_pb2.Operation() ) - request = document_processor_service.EnableProcessorRequest() + request = document_processor_service.ReviewDocumentRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), @@ -9766,7 +11855,7 @@ def test_enable_processor_rest_interceptors(null_interceptor): pre.return_value = request, metadata post.return_value = operations_pb2.Operation() - client.enable_processor( + client.review_document( request, metadata=[ ("key", "val"), @@ -9778,9 +11867,9 @@ def test_enable_processor_rest_interceptors(null_interceptor): post.assert_called_once() -def test_enable_processor_rest_bad_request( +def test_review_document_rest_bad_request( transport: str = "rest", - request_type=document_processor_service.EnableProcessorRequest, + request_type=document_processor_service.ReviewDocumentRequest, ): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -9788,7 +11877,9 @@ def test_enable_processor_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} + request_init = { + "human_review_config": "projects/sample1/locations/sample2/processors/sample3/humanReviewConfig" + } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -9800,10 +11891,67 @@ def test_enable_processor_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.enable_processor(request) + client.review_document(request) -def test_enable_processor_rest_error(): +def test_review_document_rest_flattened(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "human_review_config": "projects/sample1/locations/sample2/processors/sample3/humanReviewConfig" + } + + # get truthy value for each flattened field + mock_args = dict( + human_review_config="human_review_config_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.review_document(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{human_review_config=projects/*/locations/*/processors/*/humanReviewConfig}:reviewDocument" + % client.transport._host, + args[1], + ) + + +def test_review_document_rest_flattened_error(transport: str = "rest"): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.review_document( + document_processor_service.ReviewDocumentRequest(), + human_review_config="human_review_config_value", + ) + + +def test_review_document_rest_error(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -9812,18 +11960,20 @@ def test_enable_processor_rest_error(): @pytest.mark.parametrize( "request_type", [ - document_processor_service.DisableProcessorRequest, + document_processor_service.EvaluateProcessorVersionRequest, dict, ], ) -def test_disable_processor_rest(request_type): +def test_evaluate_processor_version_rest(request_type): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} + request_init = { + "processor_version": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" + } request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -9838,19 +11988,19 @@ def test_disable_processor_rest(request_type): response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.disable_processor(request) + response = client.evaluate_processor_version(request) # Establish that the response is the type that we expect. assert response.operation.name == "operations/spam" -def test_disable_processor_rest_required_fields( - request_type=document_processor_service.DisableProcessorRequest, +def test_evaluate_processor_version_rest_required_fields( + request_type=document_processor_service.EvaluateProcessorVersionRequest, ): transport_class = transports.DocumentProcessorServiceRestTransport request_init = {} - request_init["name"] = "" + request_init["processor_version"] = "" request = request_type(**request_init) pb_request = request_type.pb(request) jsonified_request = json.loads( @@ -9865,21 +12015,21 @@ def test_disable_processor_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).disable_processor._get_unset_required_fields(jsonified_request) + ).evaluate_processor_version._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present - jsonified_request["name"] = "name_value" + jsonified_request["processorVersion"] = "processor_version_value" unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).disable_processor._get_unset_required_fields(jsonified_request) + ).evaluate_processor_version._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + assert "processorVersion" in jsonified_request + assert jsonified_request["processorVersion"] == "processor_version_value" client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -9913,24 +12063,24 @@ def test_disable_processor_rest_required_fields( response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.disable_processor(request) + response = client.evaluate_processor_version(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_disable_processor_rest_unset_required_fields(): +def test_evaluate_processor_version_rest_unset_required_fields(): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.disable_processor._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + unset_fields = transport.evaluate_processor_version._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("processorVersion",))) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_disable_processor_rest_interceptors(null_interceptor): +def test_evaluate_processor_version_rest_interceptors(null_interceptor): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -9945,14 +12095,16 @@ def test_disable_processor_rest_interceptors(null_interceptor): ) as transcode, mock.patch.object( operation.Operation, "_set_result_from_operation" ), mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "post_disable_processor" + transports.DocumentProcessorServiceRestInterceptor, + "post_evaluate_processor_version", ) as post, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "pre_disable_processor" + transports.DocumentProcessorServiceRestInterceptor, + "pre_evaluate_processor_version", ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = document_processor_service.DisableProcessorRequest.pb( - document_processor_service.DisableProcessorRequest() + pb_message = document_processor_service.EvaluateProcessorVersionRequest.pb( + document_processor_service.EvaluateProcessorVersionRequest() ) transcode.return_value = { "method": "post", @@ -9968,7 +12120,7 @@ def test_disable_processor_rest_interceptors(null_interceptor): operations_pb2.Operation() ) - request = document_processor_service.DisableProcessorRequest() + request = document_processor_service.EvaluateProcessorVersionRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), @@ -9976,7 +12128,7 @@ def test_disable_processor_rest_interceptors(null_interceptor): pre.return_value = request, metadata post.return_value = operations_pb2.Operation() - client.disable_processor( + client.evaluate_processor_version( request, metadata=[ ("key", "val"), @@ -9988,9 +12140,9 @@ def test_disable_processor_rest_interceptors(null_interceptor): post.assert_called_once() -def test_disable_processor_rest_bad_request( +def test_evaluate_processor_version_rest_bad_request( transport: str = "rest", - request_type=document_processor_service.DisableProcessorRequest, + request_type=document_processor_service.EvaluateProcessorVersionRequest, ): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -9998,7 +12150,9 @@ def test_disable_processor_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/processors/sample3"} + request_init = { + "processor_version": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" + } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -10010,10 +12164,67 @@ def test_disable_processor_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.disable_processor(request) + client.evaluate_processor_version(request) -def test_disable_processor_rest_error(): +def test_evaluate_processor_version_rest_flattened(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "processor_version": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + processor_version="processor_version_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.evaluate_processor_version(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{processor_version=projects/*/locations/*/processors/*/processorVersions/*}:evaluateProcessorVersion" + % client.transport._host, + args[1], + ) + + +def test_evaluate_processor_version_rest_flattened_error(transport: str = "rest"): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.evaluate_processor_version( + document_processor_service.EvaluateProcessorVersionRequest(), + processor_version="processor_version_value", + ) + + +def test_evaluate_processor_version_rest_error(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -10022,11 +12233,11 @@ def test_disable_processor_rest_error(): @pytest.mark.parametrize( "request_type", [ - document_processor_service.SetDefaultProcessorVersionRequest, + document_processor_service.GetEvaluationRequest, dict, ], ) -def test_set_default_processor_version_rest(request_type): +def test_get_evaluation_rest(request_type): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -10034,36 +12245,43 @@ def test_set_default_processor_version_rest(request_type): # send a request that will satisfy transcoding request_init = { - "processor": "projects/sample1/locations/sample2/processors/sample3" + "name": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4/evaluations/sample5" } request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + return_value = evaluation.Evaluation( + name="name_value", + kms_key_name="kms_key_name_value", + kms_key_version_name="kms_key_version_name_value", + ) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) + pb_return_value = evaluation.Evaluation.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.set_default_processor_version(request) + response = client.get_evaluation(request) # Establish that the response is the type that we expect. - assert response.operation.name == "operations/spam" + assert isinstance(response, evaluation.Evaluation) + assert response.name == "name_value" + assert response.kms_key_name == "kms_key_name_value" + assert response.kms_key_version_name == "kms_key_version_name_value" -def test_set_default_processor_version_rest_required_fields( - request_type=document_processor_service.SetDefaultProcessorVersionRequest, +def test_get_evaluation_rest_required_fields( + request_type=document_processor_service.GetEvaluationRequest, ): transport_class = transports.DocumentProcessorServiceRestTransport request_init = {} - request_init["processor"] = "" - request_init["default_processor_version"] = "" + request_init["name"] = "" request = request_type(**request_init) pb_request = request_type.pb(request) jsonified_request = json.loads( @@ -10078,27 +12296,21 @@ def test_set_default_processor_version_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).set_default_processor_version._get_unset_required_fields(jsonified_request) + ).get_evaluation._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present - jsonified_request["processor"] = "processor_value" - jsonified_request["defaultProcessorVersion"] = "default_processor_version_value" + jsonified_request["name"] = "name_value" unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).set_default_processor_version._get_unset_required_fields(jsonified_request) + ).get_evaluation._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone - assert "processor" in jsonified_request - assert jsonified_request["processor"] == "processor_value" - assert "defaultProcessorVersion" in jsonified_request - assert ( - jsonified_request["defaultProcessorVersion"] - == "default_processor_version_value" - ) + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -10107,7 +12319,7 @@ def test_set_default_processor_version_rest_required_fields( request = request_type(**request_init) # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + return_value = evaluation.Evaluation() # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # We need to mock transcode() because providing default values @@ -10119,47 +12331,38 @@ def test_set_default_processor_version_rest_required_fields( pb_request = request_type.pb(request) transcode_result = { "uri": "v1/sample_method", - "method": "post", + "method": "get", "query_params": pb_request, } - transcode_result["body"] = pb_request transcode.return_value = transcode_result response_value = Response() response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) + + pb_return_value = evaluation.Evaluation.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.set_default_processor_version(request) + response = client.get_evaluation(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_set_default_processor_version_rest_unset_required_fields(): +def test_get_evaluation_rest_unset_required_fields(): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.set_default_processor_version._get_unset_required_fields( - {} - ) - assert set(unset_fields) == ( - set(()) - & set( - ( - "processor", - "defaultProcessorVersion", - ) - ) - ) + unset_fields = transport.get_evaluation._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_set_default_processor_version_rest_interceptors(null_interceptor): +def test_get_evaluation_rest_interceptors(null_interceptor): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -10172,18 +12375,14 @@ def test_set_default_processor_version_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - operation.Operation, "_set_result_from_operation" - ), mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, - "post_set_default_processor_version", + transports.DocumentProcessorServiceRestInterceptor, "post_get_evaluation" ) as post, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, - "pre_set_default_processor_version", + transports.DocumentProcessorServiceRestInterceptor, "pre_get_evaluation" ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = document_processor_service.SetDefaultProcessorVersionRequest.pb( - document_processor_service.SetDefaultProcessorVersionRequest() + pb_message = document_processor_service.GetEvaluationRequest.pb( + document_processor_service.GetEvaluationRequest() ) transcode.return_value = { "method": "post", @@ -10195,19 +12394,19 @@ def test_set_default_processor_version_rest_interceptors(null_interceptor): req.return_value = Response() req.return_value.status_code = 200 req.return_value.request = PreparedRequest() - req.return_value._content = json_format.MessageToJson( - operations_pb2.Operation() + req.return_value._content = evaluation.Evaluation.to_json( + evaluation.Evaluation() ) - request = document_processor_service.SetDefaultProcessorVersionRequest() + request = document_processor_service.GetEvaluationRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = operations_pb2.Operation() + post.return_value = evaluation.Evaluation() - client.set_default_processor_version( + client.get_evaluation( request, metadata=[ ("key", "val"), @@ -10219,9 +12418,9 @@ def test_set_default_processor_version_rest_interceptors(null_interceptor): post.assert_called_once() -def test_set_default_processor_version_rest_bad_request( +def test_get_evaluation_rest_bad_request( transport: str = "rest", - request_type=document_processor_service.SetDefaultProcessorVersionRequest, + request_type=document_processor_service.GetEvaluationRequest, ): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -10230,7 +12429,7 @@ def test_set_default_processor_version_rest_bad_request( # send a request that will satisfy transcoding request_init = { - "processor": "projects/sample1/locations/sample2/processors/sample3" + "name": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4/evaluations/sample5" } request = request_type(**request_init) @@ -10243,10 +12442,68 @@ def test_set_default_processor_version_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.set_default_processor_version(request) + client.get_evaluation(request) -def test_set_default_processor_version_rest_error(): +def test_get_evaluation_rest_flattened(): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = evaluation.Evaluation() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4/evaluations/sample5" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = evaluation.Evaluation.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.get_evaluation(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/processors/*/processorVersions/*/evaluations/*}" + % client.transport._host, + args[1], + ) + + +def test_get_evaluation_rest_flattened_error(transport: str = "rest"): + client = DocumentProcessorServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_evaluation( + document_processor_service.GetEvaluationRequest(), + name="name_value", + ) + + +def test_get_evaluation_rest_error(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -10255,11 +12512,11 @@ def test_set_default_processor_version_rest_error(): @pytest.mark.parametrize( "request_type", [ - document_processor_service.ReviewDocumentRequest, + document_processor_service.ListEvaluationsRequest, dict, ], ) -def test_review_document_rest(request_type): +def test_list_evaluations_rest(request_type): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -10267,35 +12524,41 @@ def test_review_document_rest(request_type): # send a request that will satisfy transcoding request_init = { - "human_review_config": "projects/sample1/locations/sample2/processors/sample3/humanReviewConfig" + "parent": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" } request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + return_value = document_processor_service.ListEvaluationsResponse( + next_page_token="next_page_token_value", + ) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) + pb_return_value = document_processor_service.ListEvaluationsResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(pb_return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.review_document(request) + response = client.list_evaluations(request) # Establish that the response is the type that we expect. - assert response.operation.name == "operations/spam" + assert isinstance(response, pagers.ListEvaluationsPager) + assert response.next_page_token == "next_page_token_value" -def test_review_document_rest_required_fields( - request_type=document_processor_service.ReviewDocumentRequest, +def test_list_evaluations_rest_required_fields( + request_type=document_processor_service.ListEvaluationsRequest, ): transport_class = transports.DocumentProcessorServiceRestTransport request_init = {} - request_init["human_review_config"] = "" + request_init["parent"] = "" request = request_type(**request_init) pb_request = request_type.pb(request) jsonified_request = json.loads( @@ -10310,21 +12573,28 @@ def test_review_document_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).review_document._get_unset_required_fields(jsonified_request) + ).list_evaluations._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present - jsonified_request["humanReviewConfig"] = "human_review_config_value" + jsonified_request["parent"] = "parent_value" unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).review_document._get_unset_required_fields(jsonified_request) + ).list_evaluations._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "page_size", + "page_token", + ) + ) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone - assert "humanReviewConfig" in jsonified_request - assert jsonified_request["humanReviewConfig"] == "human_review_config_value" + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -10333,7 +12603,7 @@ def test_review_document_rest_required_fields( request = request_type(**request_init) # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + return_value = document_processor_service.ListEvaluationsResponse() # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # We need to mock transcode() because providing default values @@ -10345,37 +12615,48 @@ def test_review_document_rest_required_fields( pb_request = request_type.pb(request) transcode_result = { "uri": "v1/sample_method", - "method": "post", + "method": "get", "query_params": pb_request, } - transcode_result["body"] = pb_request transcode.return_value = transcode_result response_value = Response() response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) + + pb_return_value = document_processor_service.ListEvaluationsResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(pb_return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.review_document(request) + response = client.list_evaluations(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_review_document_rest_unset_required_fields(): +def test_list_evaluations_rest_unset_required_fields(): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.review_document._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("humanReviewConfig",))) + unset_fields = transport.list_evaluations._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_review_document_rest_interceptors(null_interceptor): +def test_list_evaluations_rest_interceptors(null_interceptor): transport = transports.DocumentProcessorServiceRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -10388,16 +12669,14 @@ def test_review_document_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - operation.Operation, "_set_result_from_operation" - ), mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "post_review_document" + transports.DocumentProcessorServiceRestInterceptor, "post_list_evaluations" ) as post, mock.patch.object( - transports.DocumentProcessorServiceRestInterceptor, "pre_review_document" + transports.DocumentProcessorServiceRestInterceptor, "pre_list_evaluations" ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = document_processor_service.ReviewDocumentRequest.pb( - document_processor_service.ReviewDocumentRequest() + pb_message = document_processor_service.ListEvaluationsRequest.pb( + document_processor_service.ListEvaluationsRequest() ) transcode.return_value = { "method": "post", @@ -10409,19 +12688,21 @@ def test_review_document_rest_interceptors(null_interceptor): req.return_value = Response() req.return_value.status_code = 200 req.return_value.request = PreparedRequest() - req.return_value._content = json_format.MessageToJson( - operations_pb2.Operation() + req.return_value._content = ( + document_processor_service.ListEvaluationsResponse.to_json( + document_processor_service.ListEvaluationsResponse() + ) ) - request = document_processor_service.ReviewDocumentRequest() + request = document_processor_service.ListEvaluationsRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = operations_pb2.Operation() + post.return_value = document_processor_service.ListEvaluationsResponse() - client.review_document( + client.list_evaluations( request, metadata=[ ("key", "val"), @@ -10433,9 +12714,9 @@ def test_review_document_rest_interceptors(null_interceptor): post.assert_called_once() -def test_review_document_rest_bad_request( +def test_list_evaluations_rest_bad_request( transport: str = "rest", - request_type=document_processor_service.ReviewDocumentRequest, + request_type=document_processor_service.ListEvaluationsRequest, ): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -10444,7 +12725,7 @@ def test_review_document_rest_bad_request( # send a request that will satisfy transcoding request_init = { - "human_review_config": "projects/sample1/locations/sample2/processors/sample3/humanReviewConfig" + "parent": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" } request = request_type(**request_init) @@ -10457,10 +12738,10 @@ def test_review_document_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.review_document(request) + client.list_evaluations(request) -def test_review_document_rest_flattened(): +def test_list_evaluations_rest_flattened(): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -10469,40 +12750,43 @@ def test_review_document_rest_flattened(): # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + return_value = document_processor_service.ListEvaluationsResponse() # get arguments that satisfy an http rule for this method sample_request = { - "human_review_config": "projects/sample1/locations/sample2/processors/sample3/humanReviewConfig" + "parent": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" } # get truthy value for each flattened field mock_args = dict( - human_review_config="human_review_config_value", + parent="parent_value", ) mock_args.update(sample_request) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - json_return_value = json_format.MessageToJson(return_value) + pb_return_value = document_processor_service.ListEvaluationsResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(pb_return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - client.review_document(**mock_args) + client.list_evaluations(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1/{human_review_config=projects/*/locations/*/processors/*/humanReviewConfig}:reviewDocument" + "%s/v1/{parent=projects/*/locations/*/processors/*/processorVersions/*}/evaluations" % client.transport._host, args[1], ) -def test_review_document_rest_flattened_error(transport: str = "rest"): +def test_list_evaluations_rest_flattened_error(transport: str = "rest"): client = DocumentProcessorServiceClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -10511,17 +12795,77 @@ def test_review_document_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.review_document( - document_processor_service.ReviewDocumentRequest(), - human_review_config="human_review_config_value", + client.list_evaluations( + document_processor_service.ListEvaluationsRequest(), + parent="parent_value", ) -def test_review_document_rest_error(): +def test_list_evaluations_rest_pager(transport: str = "rest"): client = DocumentProcessorServiceClient( - credentials=ga_credentials.AnonymousCredentials(), transport="rest" + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + document_processor_service.ListEvaluationsResponse( + evaluations=[ + evaluation.Evaluation(), + evaluation.Evaluation(), + evaluation.Evaluation(), + ], + next_page_token="abc", + ), + document_processor_service.ListEvaluationsResponse( + evaluations=[], + next_page_token="def", + ), + document_processor_service.ListEvaluationsResponse( + evaluations=[ + evaluation.Evaluation(), + ], + next_page_token="ghi", + ), + document_processor_service.ListEvaluationsResponse( + evaluations=[ + evaluation.Evaluation(), + evaluation.Evaluation(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + document_processor_service.ListEvaluationsResponse.to_json(x) + for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = { + "parent": "projects/sample1/locations/sample2/processors/sample3/processorVersions/sample4" + } + + pager = client.list_evaluations(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, evaluation.Evaluation) for i in results) + + pages = list(client.list_evaluations(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. @@ -10669,6 +13013,7 @@ def test_document_processor_service_base_transport(): "get_processor_type", "list_processors", "get_processor", + "train_processor_version", "get_processor_version", "list_processor_versions", "delete_processor_version", @@ -10680,6 +13025,9 @@ def test_document_processor_service_base_transport(): "disable_processor", "set_default_processor_version", "review_document", + "evaluate_processor_version", + "get_evaluation", + "list_evaluations", "get_location", "list_locations", "get_operation", @@ -10990,6 +13338,9 @@ def test_document_processor_service_client_transport_session_collision(transport session1 = client1.transport.get_processor._session session2 = client2.transport.get_processor._session assert session1 != session2 + session1 = client1.transport.train_processor_version._session + session2 = client2.transport.train_processor_version._session + assert session1 != session2 session1 = client1.transport.get_processor_version._session session2 = client2.transport.get_processor_version._session assert session1 != session2 @@ -11023,6 +13374,15 @@ def test_document_processor_service_client_transport_session_collision(transport session1 = client1.transport.review_document._session session2 = client2.transport.review_document._session assert session1 != session2 + session1 = client1.transport.evaluate_processor_version._session + session2 = client2.transport.evaluate_processor_version._session + assert session1 != session2 + session1 = client1.transport.get_evaluation._session + session2 = client2.transport.get_evaluation._session + assert session1 != session2 + session1 = client1.transport.list_evaluations._session + session2 = client2.transport.list_evaluations._session + assert session1 != session2 def test_document_processor_service_grpc_transport_channel(): @@ -11185,10 +13545,44 @@ def test_document_processor_service_grpc_lro_async_client(): assert transport.operations_client is transport.operations_client -def test_human_review_config_path(): +def test_evaluation_path(): project = "squid" location = "clam" processor = "whelk" + processor_version = "octopus" + evaluation = "oyster" + expected = "projects/{project}/locations/{location}/processors/{processor}/processorVersions/{processor_version}/evaluations/{evaluation}".format( + project=project, + location=location, + processor=processor, + processor_version=processor_version, + evaluation=evaluation, + ) + actual = DocumentProcessorServiceClient.evaluation_path( + project, location, processor, processor_version, evaluation + ) + assert expected == actual + + +def test_parse_evaluation_path(): + expected = { + "project": "nudibranch", + "location": "cuttlefish", + "processor": "mussel", + "processor_version": "winkle", + "evaluation": "nautilus", + } + path = DocumentProcessorServiceClient.evaluation_path(**expected) + + # Check that the path construction is reversible. + actual = DocumentProcessorServiceClient.parse_evaluation_path(path) + assert expected == actual + + +def test_human_review_config_path(): + project = "scallop" + location = "abalone" + processor = "squid" expected = "projects/{project}/locations/{location}/processors/{processor}/humanReviewConfig".format( project=project, location=location, @@ -11202,9 +13596,9 @@ def test_human_review_config_path(): def test_parse_human_review_config_path(): expected = { - "project": "octopus", - "location": "oyster", - "processor": "nudibranch", + "project": "clam", + "location": "whelk", + "processor": "octopus", } path = DocumentProcessorServiceClient.human_review_config_path(**expected) @@ -11214,9 +13608,9 @@ def test_parse_human_review_config_path(): def test_processor_path(): - project = "cuttlefish" - location = "mussel" - processor = "winkle" + project = "oyster" + location = "nudibranch" + processor = "cuttlefish" expected = "projects/{project}/locations/{location}/processors/{processor}".format( project=project, location=location, @@ -11228,9 +13622,9 @@ def test_processor_path(): def test_parse_processor_path(): expected = { - "project": "nautilus", - "location": "scallop", - "processor": "abalone", + "project": "mussel", + "location": "winkle", + "processor": "nautilus", } path = DocumentProcessorServiceClient.processor_path(**expected) @@ -11240,9 +13634,9 @@ def test_parse_processor_path(): def test_processor_type_path(): - project = "squid" - location = "clam" - processor_type = "whelk" + project = "scallop" + location = "abalone" + processor_type = "squid" expected = "projects/{project}/locations/{location}/processorTypes/{processor_type}".format( project=project, location=location, @@ -11256,9 +13650,9 @@ def test_processor_type_path(): def test_parse_processor_type_path(): expected = { - "project": "octopus", - "location": "oyster", - "processor_type": "nudibranch", + "project": "clam", + "location": "whelk", + "processor_type": "octopus", } path = DocumentProcessorServiceClient.processor_type_path(**expected) @@ -11268,10 +13662,10 @@ def test_parse_processor_type_path(): def test_processor_version_path(): - project = "cuttlefish" - location = "mussel" - processor = "winkle" - processor_version = "nautilus" + project = "oyster" + location = "nudibranch" + processor = "cuttlefish" + processor_version = "mussel" expected = "projects/{project}/locations/{location}/processors/{processor}/processorVersions/{processor_version}".format( project=project, location=location, @@ -11286,10 +13680,10 @@ def test_processor_version_path(): def test_parse_processor_version_path(): expected = { - "project": "scallop", - "location": "abalone", - "processor": "squid", - "processor_version": "clam", + "project": "winkle", + "location": "nautilus", + "processor": "scallop", + "processor_version": "abalone", } path = DocumentProcessorServiceClient.processor_version_path(**expected) @@ -11299,7 +13693,7 @@ def test_parse_processor_version_path(): def test_common_billing_account_path(): - billing_account = "whelk" + billing_account = "squid" expected = "billingAccounts/{billing_account}".format( billing_account=billing_account, ) @@ -11309,7 +13703,7 @@ def test_common_billing_account_path(): def test_parse_common_billing_account_path(): expected = { - "billing_account": "octopus", + "billing_account": "clam", } path = DocumentProcessorServiceClient.common_billing_account_path(**expected) @@ -11319,7 +13713,7 @@ def test_parse_common_billing_account_path(): def test_common_folder_path(): - folder = "oyster" + folder = "whelk" expected = "folders/{folder}".format( folder=folder, ) @@ -11329,7 +13723,7 @@ def test_common_folder_path(): def test_parse_common_folder_path(): expected = { - "folder": "nudibranch", + "folder": "octopus", } path = DocumentProcessorServiceClient.common_folder_path(**expected) @@ -11339,7 +13733,7 @@ def test_parse_common_folder_path(): def test_common_organization_path(): - organization = "cuttlefish" + organization = "oyster" expected = "organizations/{organization}".format( organization=organization, ) @@ -11349,7 +13743,7 @@ def test_common_organization_path(): def test_parse_common_organization_path(): expected = { - "organization": "mussel", + "organization": "nudibranch", } path = DocumentProcessorServiceClient.common_organization_path(**expected) @@ -11359,7 +13753,7 @@ def test_parse_common_organization_path(): def test_common_project_path(): - project = "winkle" + project = "cuttlefish" expected = "projects/{project}".format( project=project, ) @@ -11369,7 +13763,7 @@ def test_common_project_path(): def test_parse_common_project_path(): expected = { - "project": "nautilus", + "project": "mussel", } path = DocumentProcessorServiceClient.common_project_path(**expected) @@ -11379,8 +13773,8 @@ def test_parse_common_project_path(): def test_common_location_path(): - project = "scallop" - location = "abalone" + project = "winkle" + location = "nautilus" expected = "projects/{project}/locations/{location}".format( project=project, location=location, @@ -11391,8 +13785,8 @@ def test_common_location_path(): def test_parse_common_location_path(): expected = { - "project": "squid", - "location": "clam", + "project": "scallop", + "location": "abalone", } path = DocumentProcessorServiceClient.common_location_path(**expected)