From 010bc938120b1b3804bdade43292947be6c80d64 Mon Sep 17 00:00:00 2001 From: Krista Pratico Date: Mon, 15 Jun 2020 12:31:11 -0700 Subject: [PATCH] [formrecognizer] reduce time for recorded tests runs (#11970) * wip * wip * add transport wrapper for get clients * fix mypy * fix * refactor * pass through kwargs * add polling interval tests * feedback --- .../formrecognizer/_form_recognizer_client.py | 14 +- .../formrecognizer/_form_training_client.py | 20 +- .../azure/ai/formrecognizer/_helpers.py | 25 ++ .../aio/_form_recognizer_client_async.py | 14 +- .../aio/_form_training_client_async.py | 19 +- .../ai/formrecognizer/aio/_helpers_async.py | 31 ++ ...eceipt_from_url.test_polling_interval.yaml | 232 ++++++++++++++ ..._from_url_async.test_polling_interval.yaml | 184 +++++++++++ .../test_training.test_polling_interval.yaml | 287 ++++++++++++++++++ ..._training_async.test_polling_interval.yaml | 203 +++++++++++++ .../tests/test_content.py | 89 +++--- .../tests/test_content_async.py | 90 +++--- .../tests/test_content_from_url.py | 65 ++-- .../tests/test_content_from_url_async.py | 65 ++-- .../tests/test_copy_model.py | 19 +- .../tests/test_copy_model_async.py | 19 +- .../tests/test_custom_forms.py | 30 +- .../tests/test_custom_forms_async.py | 28 +- .../tests/test_custom_forms_from_url.py | 28 +- .../tests/test_custom_forms_from_url_async.py | 29 +- .../tests/test_mgmt.py | 30 +- .../tests/test_mgmt_async.py | 30 +- .../tests/test_receipt.py | 93 +++--- .../tests/test_receipt_async.py | 78 ++--- .../tests/test_receipt_from_url.py | 63 ++-- .../tests/test_receipt_from_url_async.py | 64 ++-- .../tests/test_training.py | 40 ++- .../tests/test_training_async.py | 41 ++- .../azure-ai-formrecognizer/tests/testcase.py | 117 ++++--- 29 files changed, 1551 insertions(+), 496 deletions(-) create mode 100644 sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/aio/_helpers_async.py create mode 100644 sdk/formrecognizer/azure-ai-formrecognizer/tests/recordings/test_receipt_from_url.test_polling_interval.yaml create mode 100644 sdk/formrecognizer/azure-ai-formrecognizer/tests/recordings/test_receipt_from_url_async.test_polling_interval.yaml create mode 100644 sdk/formrecognizer/azure-ai-formrecognizer/tests/recordings/test_training.test_polling_interval.yaml create mode 100644 sdk/formrecognizer/azure-ai-formrecognizer/tests/recordings/test_training_async.test_polling_interval.yaml diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_form_recognizer_client.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_form_recognizer_client.py index 315b3d328924..34889afbc6fa 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_form_recognizer_client.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_form_recognizer_client.py @@ -66,11 +66,13 @@ def __init__(self, endpoint, credential, **kwargs): # type: (str, Union[AzureKeyCredential, TokenCredential], Any) -> None authentication_policy = get_authentication_policy(credential) + polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL) self._client = FormRecognizer( endpoint=endpoint, credential=credential, # type: ignore sdk_moniker=USER_AGENT, authentication_policy=authentication_policy, + polling_interval=polling_interval, **kwargs ) @@ -111,7 +113,7 @@ def begin_recognize_receipts(self, receipt, **kwargs): :caption: Recognize US sales receipt fields. """ - polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL) + polling_interval = kwargs.pop("polling_interval", self._client._config.polling_interval) continuation_token = kwargs.pop("continuation_token", None) content_type = kwargs.pop("content_type", None) if content_type == "application/json": @@ -162,7 +164,7 @@ def begin_recognize_receipts_from_url(self, receipt_url, **kwargs): :caption: Recognize US sales receipt fields from a URL. """ - polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL) + polling_interval = kwargs.pop("polling_interval", self._client._config.polling_interval) continuation_token = kwargs.pop("continuation_token", None) include_text_content = kwargs.pop("include_text_content", False) @@ -210,7 +212,7 @@ def begin_recognize_content(self, form, **kwargs): :caption: Recognize text and content/layout information from a form. """ - polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL) + polling_interval = kwargs.pop("polling_interval", self._client._config.polling_interval) continuation_token = kwargs.pop("continuation_token", None) content_type = kwargs.pop("content_type", None) if content_type == "application/json": @@ -246,7 +248,7 @@ def begin_recognize_content_from_url(self, form_url, **kwargs): :raises ~azure.core.exceptions.HttpResponseError: """ - polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL) + polling_interval = kwargs.pop("polling_interval", self._client._config.polling_interval) continuation_token = kwargs.pop("continuation_token", None) return self._client.begin_analyze_layout_async( @@ -296,7 +298,7 @@ def begin_recognize_custom_forms(self, model_id, form, **kwargs): raise ValueError("model_id cannot be None or empty.") cls = kwargs.pop("cls", None) - polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL) + polling_interval = kwargs.pop("polling_interval", self._client._config.polling_interval) continuation_token = kwargs.pop("continuation_token", None) content_type = kwargs.pop("content_type", None) if content_type == "application/json": @@ -348,7 +350,7 @@ def begin_recognize_custom_forms_from_url(self, model_id, form_url, **kwargs): raise ValueError("model_id cannot be None or empty.") cls = kwargs.pop("cls", None) - polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL) + polling_interval = kwargs.pop("polling_interval", self._client._config.polling_interval) continuation_token = kwargs.pop("continuation_token", None) include_text_content = kwargs.pop("include_text_content", False) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_form_training_client.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_form_training_client.py index e83ed347d56c..acb99b178a21 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_form_training_client.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_form_training_client.py @@ -17,6 +17,7 @@ from azure.core.tracing.decorator import distributed_trace from azure.core.polling import LROPoller from azure.core.polling.base_polling import LROBasePolling +from azure.core.pipeline import Pipeline from ._generated._form_recognizer_client import FormRecognizerClient as FormRecognizer from ._generated.models import ( TrainRequest, @@ -26,7 +27,7 @@ CopyOperationResult, CopyAuthorizationResult ) -from ._helpers import error_map, get_authentication_policy, POLLING_INTERVAL +from ._helpers import error_map, get_authentication_policy, POLLING_INTERVAL, TransportWrapper from ._models import ( CustomFormModelInfo, AccountProperties, @@ -78,11 +79,13 @@ def __init__(self, endpoint, credential, **kwargs): self._endpoint = endpoint self._credential = credential authentication_policy = get_authentication_policy(credential) + polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL) self._client = FormRecognizer( endpoint=self._endpoint, credential=self._credential, # type: ignore sdk_moniker=USER_AGENT, authentication_policy=authentication_policy, + polling_interval=polling_interval, **kwargs ) @@ -129,7 +132,7 @@ def callback(raw_response): cls = kwargs.pop("cls", None) continuation_token = kwargs.pop("continuation_token", None) - polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL) + polling_interval = kwargs.pop("polling_interval", self._client._config.polling_interval) deserialization_callback = cls if cls else callback if continuation_token: @@ -339,7 +342,7 @@ def begin_copy_model( if not model_id: raise ValueError("model_id cannot be None or empty.") - polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL) + polling_interval = kwargs.pop("polling_interval", self._client._config.polling_interval) continuation_token = kwargs.pop("continuation_token", None) def _copy_callback(raw_response, _, headers): # pylint: disable=unused-argument @@ -371,11 +374,20 @@ def get_form_recognizer_client(self, **kwargs): :rtype: ~azure.ai.formrecognizer.FormRecognizerClient :return: A FormRecognizerClient """ - return FormRecognizerClient( + + _pipeline = Pipeline( + transport=TransportWrapper(self._client._client._pipeline._transport), + policies=self._client._client._pipeline._impl_policies + ) # type: Pipeline + client = FormRecognizerClient( endpoint=self._endpoint, credential=self._credential, + pipeline=_pipeline, **kwargs ) + # need to share config, but can't pass as a keyword into client + client._client._config = self._client._client._config + return client def close(self): # type: () -> None diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_helpers.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_helpers.py index 38e5fd0ae13b..f474f894918c 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_helpers.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_helpers.py @@ -7,6 +7,7 @@ import six from azure.core.credentials import AzureKeyCredential from azure.core.pipeline.policies import AzureKeyCredentialPolicy +from azure.core.pipeline.transport import HttpTransport from azure.core.exceptions import ( ResourceNotFoundError, ResourceExistsError, @@ -24,6 +25,30 @@ } +class TransportWrapper(HttpTransport): + """Wrapper class that ensures that an inner client created + by a `get_client` method does not close the outer transport for the parent + when used in a context manager. + """ + def __init__(self, transport): + self._transport = transport + + def send(self, request, **kwargs): + return self._transport.send(request, **kwargs) + + def open(self): + pass + + def close(self): + pass + + def __enter__(self): + pass + + def __exit__(self, *args): # pylint: disable=arguments-differ + pass + + def get_authentication_policy(credential): authentication_policy = None if credential is None: diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/aio/_form_recognizer_client_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/aio/_form_recognizer_client_async.py index 1f5c0d19d43b..c6f75d43d5db 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/aio/_form_recognizer_client_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/aio/_form_recognizer_client_async.py @@ -71,11 +71,13 @@ def __init__( ) -> None: authentication_policy = get_authentication_policy(credential) + polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL) self._client = FormRecognizer( endpoint=endpoint, credential=credential, # type: ignore sdk_moniker=USER_AGENT, authentication_policy=authentication_policy, + polling_interval=polling_interval, **kwargs ) @@ -119,7 +121,7 @@ async def begin_recognize_receipts( :caption: Recognize US sales receipt fields. """ - polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL) + polling_interval = kwargs.pop("polling_interval", self._client._config.polling_interval) continuation_token = kwargs.pop("continuation_token", None) content_type = kwargs.pop("content_type", None) if content_type == "application/json": @@ -176,7 +178,7 @@ async def begin_recognize_receipts_from_url( :caption: Recognize US sales receipt fields from a URL. """ - polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL) + polling_interval = kwargs.pop("polling_interval", self._client._config.polling_interval) continuation_token = kwargs.pop("continuation_token", None) include_text_content = kwargs.pop("include_text_content", False) @@ -230,7 +232,7 @@ async def begin_recognize_content( :caption: Recognize text and content/layout information from a form. """ - polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL) + polling_interval = kwargs.pop("polling_interval", self._client._config.polling_interval) continuation_token = kwargs.pop("continuation_token", None) content_type = kwargs.pop("content_type", None) if content_type == "application/json": @@ -268,7 +270,7 @@ async def begin_recognize_content_from_url(self, form_url: str, **kwargs: Any) - :raises ~azure.core.exceptions.HttpResponseError: """ - polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL) + polling_interval = kwargs.pop("polling_interval", self._client._config.polling_interval) continuation_token = kwargs.pop("continuation_token", None) return await self._client.begin_analyze_layout_async( # type: ignore file_stream={"source": form_url}, @@ -324,7 +326,7 @@ async def begin_recognize_custom_forms( raise ValueError("model_id cannot be None or empty.") cls = kwargs.pop("cls", None) - polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL) + polling_interval = kwargs.pop("polling_interval", self._client._config.polling_interval) continuation_token = kwargs.pop("continuation_token", None) content_type = kwargs.pop("content_type", None) if content_type == "application/json": @@ -385,7 +387,7 @@ async def begin_recognize_custom_forms_from_url( raise ValueError("model_id cannot be None or empty.") cls = kwargs.pop("cls", None) - polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL) + polling_interval = kwargs.pop("polling_interval", self._client._config.polling_interval) continuation_token = kwargs.pop("continuation_token", None) include_text_content = kwargs.pop("include_text_content", False) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/aio/_form_training_client_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/aio/_form_training_client_async.py index e4ebde191e69..8dd09b6db226 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/aio/_form_training_client_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/aio/_form_training_client_async.py @@ -15,10 +15,12 @@ TYPE_CHECKING, ) from azure.core.polling import AsyncLROPoller +from azure.core.pipeline import AsyncPipeline from azure.core.polling.async_base_polling import AsyncLROBasePolling from azure.core.tracing.decorator import distributed_trace from azure.core.tracing.decorator_async import distributed_trace_async from ._form_recognizer_client_async import FormRecognizerClient +from ._helpers_async import AsyncTransportWrapper from .._generated.aio._form_recognizer_client_async import FormRecognizerClient as FormRecognizer from .._generated.models import ( TrainRequest, @@ -81,13 +83,14 @@ def __init__( ) -> None: self._endpoint = endpoint self._credential = credential - authentication_policy = get_authentication_policy(credential) + polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL) self._client = FormRecognizer( endpoint=self._endpoint, credential=self._credential, # type: ignore sdk_moniker=USER_AGENT, authentication_policy=authentication_policy, + polling_interval=polling_interval, **kwargs ) @@ -138,7 +141,7 @@ def callback(raw_response): cls = kwargs.pop("cls", None) continuation_token = kwargs.pop("continuation_token", None) - polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL) + polling_interval = kwargs.pop("polling_interval", self._client._config.polling_interval) deserialization_callback = cls if cls else callback if continuation_token: @@ -361,7 +364,7 @@ async def begin_copy_model( raise ValueError("model_id cannot be None or empty.") continuation_token = kwargs.pop("continuation_token", None) - polling_interval = kwargs.pop("polling_interval", POLLING_INTERVAL) + polling_interval = kwargs.pop("polling_interval", self._client._config.polling_interval) def _copy_callback(raw_response, _, headers): # pylint: disable=unused-argument copy_result = self._client._deserialize(CopyOperationResult, raw_response) @@ -395,11 +398,19 @@ def get_form_recognizer_client(self, **kwargs: Any) -> FormRecognizerClient: :rtype: ~azure.ai.formrecognizer.aio.FormRecognizerClient :return: A FormRecognizerClient """ - return FormRecognizerClient( + _pipeline = AsyncPipeline( + transport=AsyncTransportWrapper(self._client._client._pipeline._transport), + policies=self._client._client._pipeline._impl_policies + ) # type: AsyncPipeline + client = FormRecognizerClient( endpoint=self._endpoint, credential=self._credential, + pipeline=_pipeline, **kwargs ) + # need to share config, but can't pass as a keyword into client + client._client._config = self._client._client._config + return client async def __aenter__(self) -> "FormTrainingClient": await self._client.__aenter__() diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/aio/_helpers_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/aio/_helpers_async.py new file mode 100644 index 000000000000..f95037f25144 --- /dev/null +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/aio/_helpers_async.py @@ -0,0 +1,31 @@ +# coding=utf-8 +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + +from azure.core.pipeline.transport import AsyncHttpTransport + + +class AsyncTransportWrapper(AsyncHttpTransport): + """Wrapper class that ensures that an inner client created + by a `get_client` method does not close the outer transport for the parent + when used in a context manager. + """ + def __init__(self, async_transport): + self._transport = async_transport + + async def send(self, request, **kwargs): + return await self._transport.send(request, **kwargs) + + async def open(self): + pass + + async def close(self): + pass + + async def __aenter__(self): + pass + + async def __aexit__(self, *args): # pylint: disable=arguments-differ + pass diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/recordings/test_receipt_from_url.test_polling_interval.yaml b/sdk/formrecognizer/azure-ai-formrecognizer/tests/recordings/test_receipt_from_url.test_polling_interval.yaml new file mode 100644 index 000000000000..65fdfcc1302f --- /dev/null +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/recordings/test_receipt_from_url.test_polling_interval.yaml @@ -0,0 +1,232 @@ +interactions: +- request: + body: 'b''{"source": "https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/formrecognizer/azure-ai-formrecognizer/tests/sample_forms/receipt/contoso-allinone.jpg"}''' + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '172' + Content-Type: + - application/json + User-Agent: + - azsdk-python-ai-formrecognizer/1.0.0b4 Python/3.7.3 (Windows-10-10.0.18362-SP0) + Python/3.7.3 (Windows-10-10.0.18362-SP0) + method: POST + uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/prebuilt/receipt/analyze?includeTextDetails=false + response: + body: + string: '' + headers: + apim-request-id: + - a710a6a5-4a54-46a1-9e88-f2a0b555765b + content-length: + - '0' + date: + - Fri, 12 Jun 2020 16:50:56 GMT + operation-location: + - https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/prebuilt/receipt/analyzeResults/a710a6a5-4a54-46a1-9e88-f2a0b555765b + strict-transport-security: + - max-age=31536000; includeSubDomains; preload + x-content-type-options: + - nosniff + x-envoy-upstream-service-time: + - '1025' + status: + code: 202 + message: Accepted +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + User-Agent: + - azsdk-python-ai-formrecognizer/1.0.0b4 Python/3.7.3 (Windows-10-10.0.18362-SP0) + Python/3.7.3 (Windows-10-10.0.18362-SP0) + method: GET + uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/prebuilt/receipt/analyzeResults/a710a6a5-4a54-46a1-9e88-f2a0b555765b + response: + body: + string: '{"status": "succeeded", "createdDateTime": "2020-06-12T16:50:56Z", + "lastUpdatedDateTime": "2020-06-12T16:50:58Z", "analyzeResult": {"version": + "2.0.0", "readResults": [{"page": 1, "angle": 0.6893, "width": 1688, "height": + 3000, "unit": "pixel", "language": "en"}], "documentResults": [{"docType": + "prebuilt:receipt", "pageRange": [1, 1], "fields": {"ReceiptType": {"type": + "string", "valueString": "Itemized", "confidence": 0.692}, "MerchantName": + {"type": "string", "valueString": "Contoso Contoso", "text": "Contoso Contoso", + "boundingBox": [378.2, 292.4, 1117.7, 468.3, 1035.7, 812.7, 296.3, 636.8], + "page": 1, "confidence": 0.613}, "MerchantAddress": {"type": "string", "valueString": + "123 Main Street Redmond, WA 98052", "text": "123 Main Street Redmond, WA + 98052", "boundingBox": [302, 675.8, 848.1, 793.7, 809.9, 970.4, 263.9, 852.5], + "page": 1, "confidence": 0.99}, "MerchantPhoneNumber": {"type": "phoneNumber", + "valuePhoneNumber": "+19876543210", "text": "987-654-3210", "boundingBox": + [278, 1004, 656.3, 1054.7, 646.8, 1125.3, 268.5, 1074.7], "page": 1, "confidence": + 0.99}, "TransactionDate": {"type": "date", "valueDate": "2019-06-10", "text": + "6/10/2019", "boundingBox": [265.1, 1228.4, 525, 1247, 518.9, 1332.1, 259, + 1313.5], "page": 1, "confidence": 0.99}, "TransactionTime": {"type": "time", + "valueTime": "13:59:00", "text": "13:59", "boundingBox": [541, 1248, 677.3, + 1261.5, 668.9, 1346.5, 532.6, 1333], "page": 1, "confidence": 0.977}, "Items": + {"type": "array", "valueArray": [{"type": "object", "valueObject": {"Quantity": + {"type": "number", "text": "1", "boundingBox": [245.1, 1581.5, 300.9, 1585.1, + 295, 1676, 239.2, 1672.4], "page": 1, "confidence": 0.92}, "Name": {"type": + "string", "valueString": "Cappuccino", "text": "Cappuccino", "boundingBox": + [322, 1586, 654.2, 1601.1, 650, 1693, 317.8, 1678], "page": 1, "confidence": + 0.923}, "TotalPrice": {"type": "number", "valueNumber": 2.2, "text": "$2.20", + "boundingBox": [1107.7, 1584, 1263, 1574, 1268.3, 1656, 1113, 1666], "page": + 1, "confidence": 0.918}}}, {"type": "object", "valueObject": {"Quantity": + {"type": "number", "text": "1", "boundingBox": [232, 1834, 286.6, 1835, 285, + 1921, 230.4, 1920], "page": 1, "confidence": 0.858}, "Name": {"type": "string", + "valueString": "BACON & EGGS", "text": "BACON & EGGS", "boundingBox": [308, + 1836, 746, 1841.4, 745, 1925.4, 307, 1920], "page": 1, "confidence": 0.916}, + "TotalPrice": {"type": "number", "text": "$9.5", "boundingBox": [1133.9, 1955, + 1257, 1952, 1259.1, 2036, 1136, 2039], "page": 1, "confidence": 0.916}}}]}, + "Subtotal": {"type": "number", "valueNumber": 11.7, "text": "11.70", "boundingBox": + [1146, 2221, 1297.3, 2223, 1296, 2319, 1144.7, 2317], "page": 1, "confidence": + 0.955}, "Tax": {"type": "number", "valueNumber": 1.17, "text": "1.17", "boundingBox": + [1190, 2359, 1304, 2359, 1304, 2456, 1190, 2456], "page": 1, "confidence": + 0.979}, "Tip": {"type": "number", "valueNumber": 1.63, "text": "1.63", "boundingBox": + [1094, 2479, 1267.7, 2485, 1264, 2591, 1090.3, 2585], "page": 1, "confidence": + 0.941}, "Total": {"type": "number", "valueNumber": 14.5, "text": "$14.50", + "boundingBox": [1034.2, 2617, 1387.5, 2638.2, 1380, 2763, 1026.7, 2741.8], + "page": 1, "confidence": 0.985}}}]}}' + headers: + apim-request-id: + - 8d2cb919-ee61-49dd-92d5-899ad45eb4f0 + content-type: + - application/json; charset=utf-8 + date: + - Fri, 12 Jun 2020 16:51:02 GMT + strict-transport-security: + - max-age=31536000; includeSubDomains; preload + transfer-encoding: + - chunked + x-content-type-options: + - nosniff + x-envoy-upstream-service-time: + - '71' + status: + code: 200 + message: OK +- request: + body: 'b''{"source": "https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/formrecognizer/azure-ai-formrecognizer/tests/sample_forms/receipt/contoso-allinone.jpg"}''' + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '172' + Content-Type: + - application/json + User-Agent: + - azsdk-python-ai-formrecognizer/1.0.0b4 Python/3.7.3 (Windows-10-10.0.18362-SP0) + Python/3.7.3 (Windows-10-10.0.18362-SP0) + method: POST + uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/prebuilt/receipt/analyze?includeTextDetails=false + response: + body: + string: '' + headers: + apim-request-id: + - c824e710-1289-403a-bcc9-1ecc5970fd69 + content-length: + - '0' + date: + - Fri, 12 Jun 2020 16:51:03 GMT + operation-location: + - https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/prebuilt/receipt/analyzeResults/c824e710-1289-403a-bcc9-1ecc5970fd69 + strict-transport-security: + - max-age=31536000; includeSubDomains; preload + x-content-type-options: + - nosniff + x-envoy-upstream-service-time: + - '274' + status: + code: 202 + message: Accepted +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + User-Agent: + - azsdk-python-ai-formrecognizer/1.0.0b4 Python/3.7.3 (Windows-10-10.0.18362-SP0) + Python/3.7.3 (Windows-10-10.0.18362-SP0) + method: GET + uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/prebuilt/receipt/analyzeResults/c824e710-1289-403a-bcc9-1ecc5970fd69 + response: + body: + string: '{"status": "succeeded", "createdDateTime": "2020-06-12T16:51:03Z", + "lastUpdatedDateTime": "2020-06-12T16:51:04Z", "analyzeResult": {"version": + "2.0.0", "readResults": [{"page": 1, "angle": 0.6893, "width": 1688, "height": + 3000, "unit": "pixel", "language": "en"}], "documentResults": [{"docType": + "prebuilt:receipt", "pageRange": [1, 1], "fields": {"ReceiptType": {"type": + "string", "valueString": "Itemized", "confidence": 0.692}, "MerchantName": + {"type": "string", "valueString": "Contoso Contoso", "text": "Contoso Contoso", + "boundingBox": [378.2, 292.4, 1117.7, 468.3, 1035.7, 812.7, 296.3, 636.8], + "page": 1, "confidence": 0.613}, "MerchantAddress": {"type": "string", "valueString": + "123 Main Street Redmond, WA 98052", "text": "123 Main Street Redmond, WA + 98052", "boundingBox": [302, 675.8, 848.1, 793.7, 809.9, 970.4, 263.9, 852.5], + "page": 1, "confidence": 0.99}, "MerchantPhoneNumber": {"type": "phoneNumber", + "valuePhoneNumber": "+19876543210", "text": "987-654-3210", "boundingBox": + [278, 1004, 656.3, 1054.7, 646.8, 1125.3, 268.5, 1074.7], "page": 1, "confidence": + 0.99}, "TransactionDate": {"type": "date", "valueDate": "2019-06-10", "text": + "6/10/2019", "boundingBox": [265.1, 1228.4, 525, 1247, 518.9, 1332.1, 259, + 1313.5], "page": 1, "confidence": 0.99}, "TransactionTime": {"type": "time", + "valueTime": "13:59:00", "text": "13:59", "boundingBox": [541, 1248, 677.3, + 1261.5, 668.9, 1346.5, 532.6, 1333], "page": 1, "confidence": 0.977}, "Items": + {"type": "array", "valueArray": [{"type": "object", "valueObject": {"Quantity": + {"type": "number", "text": "1", "boundingBox": [245.1, 1581.5, 300.9, 1585.1, + 295, 1676, 239.2, 1672.4], "page": 1, "confidence": 0.92}, "Name": {"type": + "string", "valueString": "Cappuccino", "text": "Cappuccino", "boundingBox": + [322, 1586, 654.2, 1601.1, 650, 1693, 317.8, 1678], "page": 1, "confidence": + 0.923}, "TotalPrice": {"type": "number", "valueNumber": 2.2, "text": "$2.20", + "boundingBox": [1107.7, 1584, 1263, 1574, 1268.3, 1656, 1113, 1666], "page": + 1, "confidence": 0.918}}}, {"type": "object", "valueObject": {"Quantity": + {"type": "number", "text": "1", "boundingBox": [232, 1834, 286.6, 1835, 285, + 1921, 230.4, 1920], "page": 1, "confidence": 0.858}, "Name": {"type": "string", + "valueString": "BACON & EGGS", "text": "BACON & EGGS", "boundingBox": [308, + 1836, 746, 1841.4, 745, 1925.4, 307, 1920], "page": 1, "confidence": 0.916}, + "TotalPrice": {"type": "number", "text": "$9.5", "boundingBox": [1133.9, 1955, + 1257, 1952, 1259.1, 2036, 1136, 2039], "page": 1, "confidence": 0.916}}}]}, + "Subtotal": {"type": "number", "valueNumber": 11.7, "text": "11.70", "boundingBox": + [1146, 2221, 1297.3, 2223, 1296, 2319, 1144.7, 2317], "page": 1, "confidence": + 0.955}, "Tax": {"type": "number", "valueNumber": 1.17, "text": "1.17", "boundingBox": + [1190, 2359, 1304, 2359, 1304, 2456, 1190, 2456], "page": 1, "confidence": + 0.979}, "Tip": {"type": "number", "valueNumber": 1.63, "text": "1.63", "boundingBox": + [1094, 2479, 1267.7, 2485, 1264, 2591, 1090.3, 2585], "page": 1, "confidence": + 0.941}, "Total": {"type": "number", "valueNumber": 14.5, "text": "$14.50", + "boundingBox": [1034.2, 2617, 1387.5, 2638.2, 1380, 2763, 1026.7, 2741.8], + "page": 1, "confidence": 0.985}}}]}}' + headers: + apim-request-id: + - 9c817312-1988-4d3d-82ea-cf8a7a90f683 + content-type: + - application/json; charset=utf-8 + date: + - Fri, 12 Jun 2020 16:51:09 GMT + strict-transport-security: + - max-age=31536000; includeSubDomains; preload + transfer-encoding: + - chunked + x-content-type-options: + - nosniff + x-envoy-upstream-service-time: + - '23' + status: + code: 200 + message: OK +version: 1 diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/recordings/test_receipt_from_url_async.test_polling_interval.yaml b/sdk/formrecognizer/azure-ai-formrecognizer/tests/recordings/test_receipt_from_url_async.test_polling_interval.yaml new file mode 100644 index 000000000000..dc830a4c8ab7 --- /dev/null +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/recordings/test_receipt_from_url_async.test_polling_interval.yaml @@ -0,0 +1,184 @@ +interactions: +- request: + body: 'b''{"source": "https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/formrecognizer/azure-ai-formrecognizer/tests/sample_forms/receipt/contoso-allinone.jpg"}''' + headers: + Content-Length: + - '172' + Content-Type: + - application/json + User-Agent: + - azsdk-python-ai-formrecognizer/1.0.0b4 Python/3.7.3 (Windows-10-10.0.18362-SP0) + Python/3.7.3 (Windows-10-10.0.18362-SP0) + method: POST + uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/prebuilt/receipt/analyze?includeTextDetails=false + response: + body: + string: '' + headers: + apim-request-id: daea113a-952a-44c6-b9ff-22690d2f8c46 + content-length: '0' + date: Fri, 12 Jun 2020 17:00:04 GMT + operation-location: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/prebuilt/receipt/analyzeResults/daea113a-952a-44c6-b9ff-22690d2f8c46 + strict-transport-security: max-age=31536000; includeSubDomains; preload + x-content-type-options: nosniff + x-envoy-upstream-service-time: '488' + status: + code: 202 + message: Accepted + url: https://centraluseuap.api.cognitive.microsoft.com//formrecognizer/v2.0-preview/prebuilt/receipt/analyze?includeTextDetails=false +- request: + body: null + headers: + User-Agent: + - azsdk-python-ai-formrecognizer/1.0.0b4 Python/3.7.3 (Windows-10-10.0.18362-SP0) + Python/3.7.3 (Windows-10-10.0.18362-SP0) + method: GET + uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/prebuilt/receipt/analyzeResults/daea113a-952a-44c6-b9ff-22690d2f8c46 + response: + body: + string: '{"status": "succeeded", "createdDateTime": "2020-06-12T17:00:04Z", + "lastUpdatedDateTime": "2020-06-12T17:00:06Z", "analyzeResult": {"version": + "2.0.0", "readResults": [{"page": 1, "angle": 0.6893, "width": 1688, "height": + 3000, "unit": "pixel", "language": "en"}], "documentResults": [{"docType": + "prebuilt:receipt", "pageRange": [1, 1], "fields": {"ReceiptType": {"type": + "string", "valueString": "Itemized", "confidence": 0.692}, "MerchantName": + {"type": "string", "valueString": "Contoso Contoso", "text": "Contoso Contoso", + "boundingBox": [378.2, 292.4, 1117.7, 468.3, 1035.7, 812.7, 296.3, 636.8], + "page": 1, "confidence": 0.613}, "MerchantAddress": {"type": "string", "valueString": + "123 Main Street Redmond, WA 98052", "text": "123 Main Street Redmond, WA + 98052", "boundingBox": [302, 675.8, 848.1, 793.7, 809.9, 970.4, 263.9, 852.5], + "page": 1, "confidence": 0.99}, "MerchantPhoneNumber": {"type": "phoneNumber", + "valuePhoneNumber": "+19876543210", "text": "987-654-3210", "boundingBox": + [278, 1004, 656.3, 1054.7, 646.8, 1125.3, 268.5, 1074.7], "page": 1, "confidence": + 0.99}, "TransactionDate": {"type": "date", "valueDate": "2019-06-10", "text": + "6/10/2019", "boundingBox": [265.1, 1228.4, 525, 1247, 518.9, 1332.1, 259, + 1313.5], "page": 1, "confidence": 0.99}, "TransactionTime": {"type": "time", + "valueTime": "13:59:00", "text": "13:59", "boundingBox": [541, 1248, 677.3, + 1261.5, 668.9, 1346.5, 532.6, 1333], "page": 1, "confidence": 0.977}, "Items": + {"type": "array", "valueArray": [{"type": "object", "valueObject": {"Quantity": + {"type": "number", "text": "1", "boundingBox": [245.1, 1581.5, 300.9, 1585.1, + 295, 1676, 239.2, 1672.4], "page": 1, "confidence": 0.92}, "Name": {"type": + "string", "valueString": "Cappuccino", "text": "Cappuccino", "boundingBox": + [322, 1586, 654.2, 1601.1, 650, 1693, 317.8, 1678], "page": 1, "confidence": + 0.923}, "TotalPrice": {"type": "number", "valueNumber": 2.2, "text": "$2.20", + "boundingBox": [1107.7, 1584, 1263, 1574, 1268.3, 1656, 1113, 1666], "page": + 1, "confidence": 0.918}}}, {"type": "object", "valueObject": {"Quantity": + {"type": "number", "text": "1", "boundingBox": [232, 1834, 286.6, 1835, 285, + 1921, 230.4, 1920], "page": 1, "confidence": 0.858}, "Name": {"type": "string", + "valueString": "BACON & EGGS", "text": "BACON & EGGS", "boundingBox": [308, + 1836, 746, 1841.4, 745, 1925.4, 307, 1920], "page": 1, "confidence": 0.916}, + "TotalPrice": {"type": "number", "text": "$9.5", "boundingBox": [1133.9, 1955, + 1257, 1952, 1259.1, 2036, 1136, 2039], "page": 1, "confidence": 0.916}}}]}, + "Subtotal": {"type": "number", "valueNumber": 11.7, "text": "11.70", "boundingBox": + [1146, 2221, 1297.3, 2223, 1296, 2319, 1144.7, 2317], "page": 1, "confidence": + 0.955}, "Tax": {"type": "number", "valueNumber": 1.17, "text": "1.17", "boundingBox": + [1190, 2359, 1304, 2359, 1304, 2456, 1190, 2456], "page": 1, "confidence": + 0.979}, "Tip": {"type": "number", "valueNumber": 1.63, "text": "1.63", "boundingBox": + [1094, 2479, 1267.7, 2485, 1264, 2591, 1090.3, 2585], "page": 1, "confidence": + 0.941}, "Total": {"type": "number", "valueNumber": 14.5, "text": "$14.50", + "boundingBox": [1034.2, 2617, 1387.5, 2638.2, 1380, 2763, 1026.7, 2741.8], + "page": 1, "confidence": 0.985}}}]}}' + headers: + apim-request-id: d2b56931-1870-4c76-ae23-89ebe5c56143 + content-type: application/json; charset=utf-8 + date: Fri, 12 Jun 2020 17:00:09 GMT + strict-transport-security: max-age=31536000; includeSubDomains; preload + transfer-encoding: chunked + x-content-type-options: nosniff + x-envoy-upstream-service-time: '38' + status: + code: 200 + message: OK + url: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/prebuilt/receipt/analyzeResults/daea113a-952a-44c6-b9ff-22690d2f8c46 +- request: + body: 'b''{"source": "https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/formrecognizer/azure-ai-formrecognizer/tests/sample_forms/receipt/contoso-allinone.jpg"}''' + headers: + Content-Length: + - '172' + Content-Type: + - application/json + User-Agent: + - azsdk-python-ai-formrecognizer/1.0.0b4 Python/3.7.3 (Windows-10-10.0.18362-SP0) + Python/3.7.3 (Windows-10-10.0.18362-SP0) + method: POST + uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/prebuilt/receipt/analyze?includeTextDetails=false + response: + body: + string: '' + headers: + apim-request-id: aefd083e-3ba3-4f84-beb5-719d583936d0 + content-length: '0' + date: Fri, 12 Jun 2020 17:00:10 GMT + operation-location: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/prebuilt/receipt/analyzeResults/aefd083e-3ba3-4f84-beb5-719d583936d0 + strict-transport-security: max-age=31536000; includeSubDomains; preload + x-content-type-options: nosniff + x-envoy-upstream-service-time: '336' + status: + code: 202 + message: Accepted + url: https://centraluseuap.api.cognitive.microsoft.com//formrecognizer/v2.0-preview/prebuilt/receipt/analyze?includeTextDetails=false +- request: + body: null + headers: + User-Agent: + - azsdk-python-ai-formrecognizer/1.0.0b4 Python/3.7.3 (Windows-10-10.0.18362-SP0) + Python/3.7.3 (Windows-10-10.0.18362-SP0) + method: GET + uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/prebuilt/receipt/analyzeResults/aefd083e-3ba3-4f84-beb5-719d583936d0 + response: + body: + string: '{"status": "succeeded", "createdDateTime": "2020-06-12T17:00:10Z", + "lastUpdatedDateTime": "2020-06-12T17:00:13Z", "analyzeResult": {"version": + "2.0.0", "readResults": [{"page": 1, "angle": 0.6893, "width": 1688, "height": + 3000, "unit": "pixel", "language": "en"}], "documentResults": [{"docType": + "prebuilt:receipt", "pageRange": [1, 1], "fields": {"ReceiptType": {"type": + "string", "valueString": "Itemized", "confidence": 0.692}, "MerchantName": + {"type": "string", "valueString": "Contoso Contoso", "text": "Contoso Contoso", + "boundingBox": [378.2, 292.4, 1117.7, 468.3, 1035.7, 812.7, 296.3, 636.8], + "page": 1, "confidence": 0.613}, "MerchantAddress": {"type": "string", "valueString": + "123 Main Street Redmond, WA 98052", "text": "123 Main Street Redmond, WA + 98052", "boundingBox": [302, 675.8, 848.1, 793.7, 809.9, 970.4, 263.9, 852.5], + "page": 1, "confidence": 0.99}, "MerchantPhoneNumber": {"type": "phoneNumber", + "valuePhoneNumber": "+19876543210", "text": "987-654-3210", "boundingBox": + [278, 1004, 656.3, 1054.7, 646.8, 1125.3, 268.5, 1074.7], "page": 1, "confidence": + 0.99}, "TransactionDate": {"type": "date", "valueDate": "2019-06-10", "text": + "6/10/2019", "boundingBox": [265.1, 1228.4, 525, 1247, 518.9, 1332.1, 259, + 1313.5], "page": 1, "confidence": 0.99}, "TransactionTime": {"type": "time", + "valueTime": "13:59:00", "text": "13:59", "boundingBox": [541, 1248, 677.3, + 1261.5, 668.9, 1346.5, 532.6, 1333], "page": 1, "confidence": 0.977}, "Items": + {"type": "array", "valueArray": [{"type": "object", "valueObject": {"Quantity": + {"type": "number", "text": "1", "boundingBox": [245.1, 1581.5, 300.9, 1585.1, + 295, 1676, 239.2, 1672.4], "page": 1, "confidence": 0.92}, "Name": {"type": + "string", "valueString": "Cappuccino", "text": "Cappuccino", "boundingBox": + [322, 1586, 654.2, 1601.1, 650, 1693, 317.8, 1678], "page": 1, "confidence": + 0.923}, "TotalPrice": {"type": "number", "valueNumber": 2.2, "text": "$2.20", + "boundingBox": [1107.7, 1584, 1263, 1574, 1268.3, 1656, 1113, 1666], "page": + 1, "confidence": 0.918}}}, {"type": "object", "valueObject": {"Quantity": + {"type": "number", "text": "1", "boundingBox": [232, 1834, 286.6, 1835, 285, + 1921, 230.4, 1920], "page": 1, "confidence": 0.858}, "Name": {"type": "string", + "valueString": "BACON & EGGS", "text": "BACON & EGGS", "boundingBox": [308, + 1836, 746, 1841.4, 745, 1925.4, 307, 1920], "page": 1, "confidence": 0.916}, + "TotalPrice": {"type": "number", "text": "$9.5", "boundingBox": [1133.9, 1955, + 1257, 1952, 1259.1, 2036, 1136, 2039], "page": 1, "confidence": 0.916}}}]}, + "Subtotal": {"type": "number", "valueNumber": 11.7, "text": "11.70", "boundingBox": + [1146, 2221, 1297.3, 2223, 1296, 2319, 1144.7, 2317], "page": 1, "confidence": + 0.955}, "Tax": {"type": "number", "valueNumber": 1.17, "text": "1.17", "boundingBox": + [1190, 2359, 1304, 2359, 1304, 2456, 1190, 2456], "page": 1, "confidence": + 0.979}, "Tip": {"type": "number", "valueNumber": 1.63, "text": "1.63", "boundingBox": + [1094, 2479, 1267.7, 2485, 1264, 2591, 1090.3, 2585], "page": 1, "confidence": + 0.941}, "Total": {"type": "number", "valueNumber": 14.5, "text": "$14.50", + "boundingBox": [1034.2, 2617, 1387.5, 2638.2, 1380, 2763, 1026.7, 2741.8], + "page": 1, "confidence": 0.985}}}]}}' + headers: + apim-request-id: 0f0d31ca-86d6-4fd4-9407-fdaa170bc62b + content-type: application/json; charset=utf-8 + date: Fri, 12 Jun 2020 17:00:17 GMT + strict-transport-security: max-age=31536000; includeSubDomains; preload + transfer-encoding: chunked + x-content-type-options: nosniff + x-envoy-upstream-service-time: '212' + status: + code: 200 + message: OK + url: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/prebuilt/receipt/analyzeResults/aefd083e-3ba3-4f84-beb5-719d583936d0 +version: 1 diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/recordings/test_training.test_polling_interval.yaml b/sdk/formrecognizer/azure-ai-formrecognizer/tests/recordings/test_training.test_polling_interval.yaml new file mode 100644 index 000000000000..51579c749a4d --- /dev/null +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/recordings/test_training.test_polling_interval.yaml @@ -0,0 +1,287 @@ +interactions: +- request: + body: 'b''b\''{"source": "containersasurl", "sourceFilter": {"prefix": "", "includeSubFolders": + false}, "useLabelFile": false}\''''' + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '288' + Content-Type: + - application/json + User-Agent: + - azsdk-python-ai-formrecognizer/1.0.0b4 Python/3.7.3 (Windows-10-10.0.18362-SP0) + Python/3.7.3 (Windows-10-10.0.18362-SP0) + method: POST + uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models + response: + body: + string: '' + headers: + apim-request-id: + - d39ac0fa-ba19-4498-9b41-2e4fe0a9db5e + content-length: + - '0' + date: + - Fri, 12 Jun 2020 17:14:17 GMT + location: + - https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models/bb293dcb-3705-4a5d-8453-5139686c2721 + strict-transport-security: + - max-age=31536000; includeSubDomains; preload + x-content-type-options: + - nosniff + x-envoy-upstream-service-time: + - '1221' + status: + code: 201 + message: Created +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + User-Agent: + - azsdk-python-ai-formrecognizer/1.0.0b4 Python/3.7.3 (Windows-10-10.0.18362-SP0) + Python/3.7.3 (Windows-10-10.0.18362-SP0) + method: GET + uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models/bb293dcb-3705-4a5d-8453-5139686c2721?includeKeys=true + response: + body: + string: '{"modelInfo": {"modelId": "bb293dcb-3705-4a5d-8453-5139686c2721", "status": + "creating", "createdDateTime": "2020-06-12T17:14:16Z", "lastUpdatedDateTime": + "2020-06-12T17:14:16Z"}}' + headers: + apim-request-id: + - 1c623540-200a-4ff9-b93f-cabbe6768db0 + content-type: + - application/json; charset=utf-8 + date: + - Fri, 12 Jun 2020 17:14:23 GMT + strict-transport-security: + - max-age=31536000; includeSubDomains; preload + transfer-encoding: + - chunked + x-content-type-options: + - nosniff + x-envoy-upstream-service-time: + - '148' + status: + code: 200 + message: OK +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + User-Agent: + - azsdk-python-ai-formrecognizer/1.0.0b4 Python/3.7.3 (Windows-10-10.0.18362-SP0) + Python/3.7.3 (Windows-10-10.0.18362-SP0) + method: GET + uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models/bb293dcb-3705-4a5d-8453-5139686c2721?includeKeys=true + response: + body: + string: '{"modelInfo": {"modelId": "bb293dcb-3705-4a5d-8453-5139686c2721", "status": + "ready", "createdDateTime": "2020-06-12T17:14:16Z", "lastUpdatedDateTime": + "2020-06-12T17:14:28Z"}, "keys": {"clusters": {"0": ["Additional Notes:", + "Address:", "Company Name:", "Company Phone:", "Dated As:", "Details", "Email:", + "Hero Limited", "Name:", "Phone:", "Purchase Order", "Purchase Order #:", + "Quantity", "SUBTOTAL", "Seattle, WA 93849 Phone:", "Shipped From", "Shipped + To", "TAX", "TOTAL", "Total", "Unit Price", "Vendor Name:", "Website:"]}}, + "trainResult": {"trainingDocuments": [{"documentName": "Form_1.jpg", "pages": + 1, "errors": [], "status": "succeeded"}, {"documentName": "Form_2.jpg", "pages": + 1, "errors": [], "status": "succeeded"}, {"documentName": "Form_3.jpg", "pages": + 1, "errors": [], "status": "succeeded"}, {"documentName": "Form_4.jpg", "pages": + 1, "errors": [], "status": "succeeded"}, {"documentName": "Form_5.jpg", "pages": + 1, "errors": [], "status": "succeeded"}], "errors": []}}' + headers: + apim-request-id: + - d15e05cb-2953-45ca-8b70-2517f1a7f9e7 + content-type: + - application/json; charset=utf-8 + date: + - Fri, 12 Jun 2020 17:14:29 GMT + strict-transport-security: + - max-age=31536000; includeSubDomains; preload + transfer-encoding: + - chunked + x-content-type-options: + - nosniff + x-envoy-upstream-service-time: + - '149' + status: + code: 200 + message: OK +- request: + body: 'b''b\''{"source": "containersasurl", "sourceFilter": {"prefix": "", "includeSubFolders": + false}, "useLabelFile": false}\''''' + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '288' + Content-Type: + - application/json + User-Agent: + - azsdk-python-ai-formrecognizer/1.0.0b4 Python/3.7.3 (Windows-10-10.0.18362-SP0) + Python/3.7.3 (Windows-10-10.0.18362-SP0) + method: POST + uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models + response: + body: + string: '' + headers: + apim-request-id: + - 30022b63-b2d4-4772-9926-50c933b943c2 + content-length: + - '0' + date: + - Fri, 12 Jun 2020 17:14:30 GMT + location: + - https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models/b428e552-b46a-4a2e-a8cd-f8f5359656d5 + strict-transport-security: + - max-age=31536000; includeSubDomains; preload + x-content-type-options: + - nosniff + x-envoy-upstream-service-time: + - '212' + status: + code: 201 + message: Created +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + User-Agent: + - azsdk-python-ai-formrecognizer/1.0.0b4 Python/3.7.3 (Windows-10-10.0.18362-SP0) + Python/3.7.3 (Windows-10-10.0.18362-SP0) + method: GET + uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models/b428e552-b46a-4a2e-a8cd-f8f5359656d5?includeKeys=true + response: + body: + string: '{"modelInfo": {"modelId": "b428e552-b46a-4a2e-a8cd-f8f5359656d5", "status": + "creating", "createdDateTime": "2020-06-12T17:14:30Z", "lastUpdatedDateTime": + "2020-06-12T17:14:30Z"}}' + headers: + apim-request-id: + - 17cf4951-a81f-4ee1-81b1-4b1f037ec32b + content-type: + - application/json; charset=utf-8 + date: + - Fri, 12 Jun 2020 17:14:35 GMT + strict-transport-security: + - max-age=31536000; includeSubDomains; preload + transfer-encoding: + - chunked + x-content-type-options: + - nosniff + x-envoy-upstream-service-time: + - '163' + status: + code: 200 + message: OK +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + User-Agent: + - azsdk-python-ai-formrecognizer/1.0.0b4 Python/3.7.3 (Windows-10-10.0.18362-SP0) + Python/3.7.3 (Windows-10-10.0.18362-SP0) + method: GET + uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models/b428e552-b46a-4a2e-a8cd-f8f5359656d5?includeKeys=true + response: + body: + string: '{"modelInfo": {"modelId": "b428e552-b46a-4a2e-a8cd-f8f5359656d5", "status": + "creating", "createdDateTime": "2020-06-12T17:14:30Z", "lastUpdatedDateTime": + "2020-06-12T17:14:30Z"}}' + headers: + apim-request-id: + - bf02c76e-cfe7-4dfc-9c9f-9dfa8404619f + content-type: + - application/json; charset=utf-8 + date: + - Fri, 12 Jun 2020 17:14:40 GMT + strict-transport-security: + - max-age=31536000; includeSubDomains; preload + transfer-encoding: + - chunked + x-content-type-options: + - nosniff + x-envoy-upstream-service-time: + - '160' + status: + code: 200 + message: OK +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + User-Agent: + - azsdk-python-ai-formrecognizer/1.0.0b4 Python/3.7.3 (Windows-10-10.0.18362-SP0) + Python/3.7.3 (Windows-10-10.0.18362-SP0) + method: GET + uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models/b428e552-b46a-4a2e-a8cd-f8f5359656d5?includeKeys=true + response: + body: + string: '{"modelInfo": {"modelId": "b428e552-b46a-4a2e-a8cd-f8f5359656d5", "status": + "ready", "createdDateTime": "2020-06-12T17:14:30Z", "lastUpdatedDateTime": + "2020-06-12T17:14:43Z"}, "keys": {"clusters": {"0": ["Additional Notes:", + "Address:", "Company Name:", "Company Phone:", "Dated As:", "Details", "Email:", + "Hero Limited", "Name:", "Phone:", "Purchase Order", "Purchase Order #:", + "Quantity", "SUBTOTAL", "Seattle, WA 93849 Phone:", "Shipped From", "Shipped + To", "TAX", "TOTAL", "Total", "Unit Price", "Vendor Name:", "Website:"]}}, + "trainResult": {"trainingDocuments": [{"documentName": "Form_1.jpg", "pages": + 1, "errors": [], "status": "succeeded"}, {"documentName": "Form_2.jpg", "pages": + 1, "errors": [], "status": "succeeded"}, {"documentName": "Form_3.jpg", "pages": + 1, "errors": [], "status": "succeeded"}, {"documentName": "Form_4.jpg", "pages": + 1, "errors": [], "status": "succeeded"}, {"documentName": "Form_5.jpg", "pages": + 1, "errors": [], "status": "succeeded"}], "errors": []}}' + headers: + apim-request-id: + - a1205f89-6b1b-41f5-b6f4-d09b7dc4cb0f + content-type: + - application/json; charset=utf-8 + date: + - Fri, 12 Jun 2020 17:14:45 GMT + strict-transport-security: + - max-age=31536000; includeSubDomains; preload + transfer-encoding: + - chunked + x-content-type-options: + - nosniff + x-envoy-upstream-service-time: + - '195' + status: + code: 200 + message: OK +version: 1 diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/recordings/test_training_async.test_polling_interval.yaml b/sdk/formrecognizer/azure-ai-formrecognizer/tests/recordings/test_training_async.test_polling_interval.yaml new file mode 100644 index 000000000000..3ca99f843569 --- /dev/null +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/recordings/test_training_async.test_polling_interval.yaml @@ -0,0 +1,203 @@ +interactions: +- request: + body: 'b''b\''{"source": "containersasurl", "sourceFilter": {"prefix": "", "includeSubFolders": + false}, "useLabelFile": false}\''''' + headers: + Content-Length: + - '288' + Content-Type: + - application/json + User-Agent: + - azsdk-python-ai-formrecognizer/1.0.0b4 Python/3.7.3 (Windows-10-10.0.18362-SP0) + Python/3.7.3 (Windows-10-10.0.18362-SP0) + method: POST + uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models + response: + body: + string: '' + headers: + apim-request-id: 266ae45e-1e1a-4c42-a21a-f73ca5bbefb8 + content-length: '0' + date: Fri, 12 Jun 2020 17:17:20 GMT + location: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models/280b1966-c58c-400b-aa00-967fbc90cbf6 + strict-transport-security: max-age=31536000; includeSubDomains; preload + x-content-type-options: nosniff + x-envoy-upstream-service-time: '523' + status: + code: 201 + message: Created + url: https://centraluseuap.api.cognitive.microsoft.com//formrecognizer/v2.0-preview/custom/models +- request: + body: null + headers: + User-Agent: + - azsdk-python-ai-formrecognizer/1.0.0b4 Python/3.7.3 (Windows-10-10.0.18362-SP0) + Python/3.7.3 (Windows-10-10.0.18362-SP0) + method: GET + uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models/280b1966-c58c-400b-aa00-967fbc90cbf6?includeKeys=true + response: + body: + string: '{"modelInfo": {"modelId": "280b1966-c58c-400b-aa00-967fbc90cbf6", "status": + "creating", "createdDateTime": "2020-06-12T17:17:20Z", "lastUpdatedDateTime": + "2020-06-12T17:17:20Z"}}' + headers: + apim-request-id: 7f1bf53e-0d76-4bec-84fe-ac6d448fba4e + content-type: application/json; charset=utf-8 + date: Fri, 12 Jun 2020 17:17:26 GMT + strict-transport-security: max-age=31536000; includeSubDomains; preload + transfer-encoding: chunked + x-content-type-options: nosniff + x-envoy-upstream-service-time: '67' + status: + code: 200 + message: OK + url: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models/280b1966-c58c-400b-aa00-967fbc90cbf6?includeKeys=true +- request: + body: null + headers: + User-Agent: + - azsdk-python-ai-formrecognizer/1.0.0b4 Python/3.7.3 (Windows-10-10.0.18362-SP0) + Python/3.7.3 (Windows-10-10.0.18362-SP0) + method: GET + uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models/280b1966-c58c-400b-aa00-967fbc90cbf6?includeKeys=true + response: + body: + string: '{"modelInfo": {"modelId": "280b1966-c58c-400b-aa00-967fbc90cbf6", "status": + "ready", "createdDateTime": "2020-06-12T17:17:20Z", "lastUpdatedDateTime": + "2020-06-12T17:17:30Z"}, "keys": {"clusters": {"0": ["Additional Notes:", + "Address:", "Company Name:", "Company Phone:", "Dated As:", "Details", "Email:", + "Hero Limited", "Name:", "Phone:", "Purchase Order", "Purchase Order #:", + "Quantity", "SUBTOTAL", "Seattle, WA 93849 Phone:", "Shipped From", "Shipped + To", "TAX", "TOTAL", "Total", "Unit Price", "Vendor Name:", "Website:"]}}, + "trainResult": {"trainingDocuments": [{"documentName": "Form_1.jpg", "pages": + 1, "errors": [], "status": "succeeded"}, {"documentName": "Form_2.jpg", "pages": + 1, "errors": [], "status": "succeeded"}, {"documentName": "Form_3.jpg", "pages": + 1, "errors": [], "status": "succeeded"}, {"documentName": "Form_4.jpg", "pages": + 1, "errors": [], "status": "succeeded"}, {"documentName": "Form_5.jpg", "pages": + 1, "errors": [], "status": "succeeded"}], "errors": []}}' + headers: + apim-request-id: e5ce8d1a-e6ce-4637-ad2d-c81861343bd2 + content-type: application/json; charset=utf-8 + date: Fri, 12 Jun 2020 17:17:33 GMT + strict-transport-security: max-age=31536000; includeSubDomains; preload + transfer-encoding: chunked + x-content-type-options: nosniff + x-envoy-upstream-service-time: '157' + status: + code: 200 + message: OK + url: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models/280b1966-c58c-400b-aa00-967fbc90cbf6?includeKeys=true +- request: + body: 'b''b\''{"source": "containersasurl", "sourceFilter": {"prefix": "", "includeSubFolders": + false}, "useLabelFile": false}\''''' + headers: + Content-Length: + - '288' + Content-Type: + - application/json + User-Agent: + - azsdk-python-ai-formrecognizer/1.0.0b4 Python/3.7.3 (Windows-10-10.0.18362-SP0) + Python/3.7.3 (Windows-10-10.0.18362-SP0) + method: POST + uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models + response: + body: + string: '' + headers: + apim-request-id: fb75dd82-85ff-4a90-a429-74c065ad7df4 + content-length: '0' + date: Fri, 12 Jun 2020 17:17:33 GMT + location: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models/6f6b1513-1eca-4d9a-9f79-cdc500551d01 + strict-transport-security: max-age=31536000; includeSubDomains; preload + x-content-type-options: nosniff + x-envoy-upstream-service-time: '188' + status: + code: 201 + message: Created + url: https://centraluseuap.api.cognitive.microsoft.com//formrecognizer/v2.0-preview/custom/models +- request: + body: null + headers: + User-Agent: + - azsdk-python-ai-formrecognizer/1.0.0b4 Python/3.7.3 (Windows-10-10.0.18362-SP0) + Python/3.7.3 (Windows-10-10.0.18362-SP0) + method: GET + uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models/6f6b1513-1eca-4d9a-9f79-cdc500551d01?includeKeys=true + response: + body: + string: '{"modelInfo": {"modelId": "6f6b1513-1eca-4d9a-9f79-cdc500551d01", "status": + "creating", "createdDateTime": "2020-06-12T17:17:33Z", "lastUpdatedDateTime": + "2020-06-12T17:17:33Z"}}' + headers: + apim-request-id: a26cf20e-3646-41c7-a707-22719fceaf6a + content-type: application/json; charset=utf-8 + date: Fri, 12 Jun 2020 17:17:44 GMT + strict-transport-security: max-age=31536000; includeSubDomains; preload + transfer-encoding: chunked + x-content-type-options: nosniff + x-envoy-upstream-service-time: '5227' + status: + code: 200 + message: OK + url: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models/6f6b1513-1eca-4d9a-9f79-cdc500551d01?includeKeys=true +- request: + body: null + headers: + User-Agent: + - azsdk-python-ai-formrecognizer/1.0.0b4 Python/3.7.3 (Windows-10-10.0.18362-SP0) + Python/3.7.3 (Windows-10-10.0.18362-SP0) + method: GET + uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models/6f6b1513-1eca-4d9a-9f79-cdc500551d01?includeKeys=true + response: + body: + string: '{"modelInfo": {"modelId": "6f6b1513-1eca-4d9a-9f79-cdc500551d01", "status": + "creating", "createdDateTime": "2020-06-12T17:17:33Z", "lastUpdatedDateTime": + "2020-06-12T17:17:33Z"}}' + headers: + apim-request-id: fb1c69cf-7ef4-4140-bff4-a40efc38492d + content-type: application/json; charset=utf-8 + date: Fri, 12 Jun 2020 17:17:49 GMT + strict-transport-security: max-age=31536000; includeSubDomains; preload + transfer-encoding: chunked + x-content-type-options: nosniff + x-envoy-upstream-service-time: '203' + status: + code: 200 + message: OK + url: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models/6f6b1513-1eca-4d9a-9f79-cdc500551d01?includeKeys=true +- request: + body: null + headers: + User-Agent: + - azsdk-python-ai-formrecognizer/1.0.0b4 Python/3.7.3 (Windows-10-10.0.18362-SP0) + Python/3.7.3 (Windows-10-10.0.18362-SP0) + method: GET + uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models/6f6b1513-1eca-4d9a-9f79-cdc500551d01?includeKeys=true + response: + body: + string: '{"modelInfo": {"modelId": "6f6b1513-1eca-4d9a-9f79-cdc500551d01", "status": + "ready", "createdDateTime": "2020-06-12T17:17:33Z", "lastUpdatedDateTime": + "2020-06-12T17:17:53Z"}, "keys": {"clusters": {"0": ["Additional Notes:", + "Address:", "Company Name:", "Company Phone:", "Dated As:", "Details", "Email:", + "Hero Limited", "Name:", "Phone:", "Purchase Order", "Purchase Order #:", + "Quantity", "SUBTOTAL", "Seattle, WA 93849 Phone:", "Shipped From", "Shipped + To", "TAX", "TOTAL", "Total", "Unit Price", "Vendor Name:", "Website:"]}}, + "trainResult": {"trainingDocuments": [{"documentName": "Form_1.jpg", "pages": + 1, "errors": [], "status": "succeeded"}, {"documentName": "Form_2.jpg", "pages": + 1, "errors": [], "status": "succeeded"}, {"documentName": "Form_3.jpg", "pages": + 1, "errors": [], "status": "succeeded"}, {"documentName": "Form_4.jpg", "pages": + 1, "errors": [], "status": "succeeded"}, {"documentName": "Form_5.jpg", "pages": + 1, "errors": [], "status": "succeeded"}], "errors": []}}' + headers: + apim-request-id: b4e7cf16-37d8-4853-b0b3-ae8ec59768f5 + content-type: application/json; charset=utf-8 + date: Fri, 12 Jun 2020 17:17:54 GMT + strict-transport-security: max-age=31536000; includeSubDomains; preload + transfer-encoding: chunked + x-content-type-options: nosniff + x-envoy-upstream-service-time: '17' + status: + code: 200 + message: OK + url: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0-preview/custom/models/6f6b1513-1eca-4d9a-9f79-cdc500551d01?includeKeys=true +version: 1 diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_content.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_content.py index 1f8bb1c8d568..1c3f693af9e7 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_content.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_content.py @@ -5,6 +5,7 @@ # ------------------------------------ import pytest +import functools from io import BytesIO from azure.core.exceptions import ServiceRequestError, ClientAuthenticationError, HttpResponseError from azure.core.credentials import AzureKeyCredential @@ -12,6 +13,10 @@ from azure.ai.formrecognizer._response_handlers import prepare_content_result from azure.ai.formrecognizer import FormRecognizerClient, FormContentType from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer +from testcase import GlobalClientPreparer as _GlobalClientPreparer + + +GlobalClientPreparer = functools.partial(_GlobalClientPreparer, FormRecognizerClient) class TestContentFromStream(FormRecognizerTest): @@ -25,8 +30,8 @@ def test_content_bad_endpoint(self, resource_group, location, form_recognizer_ac poller = client.begin_recognize_content(myfile) @GlobalFormRecognizerAccountPreparer() - def test_content_authentication_successful_key(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_content_authentication_successful_key(self, client): with open(self.invoice_pdf, "rb") as fd: myfile = fd.read() poller = client.begin_recognize_content(myfile) @@ -39,8 +44,8 @@ def test_content_authentication_bad_key(self, resource_group, location, form_rec poller = client.begin_recognize_content(b"xx", content_type="application/pdf") @GlobalFormRecognizerAccountPreparer() - def test_passing_enum_content_type(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_passing_enum_content_type(self, client): with open(self.invoice_pdf, "rb") as fd: myfile = fd.read() poller = client.begin_recognize_content( @@ -51,8 +56,8 @@ def test_passing_enum_content_type(self, resource_group, location, form_recogniz self.assertIsNotNone(result) @GlobalFormRecognizerAccountPreparer() - def test_damaged_file_passed_as_bytes(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_damaged_file_passed_as_bytes(self, client): damaged_pdf = b"\x25\x50\x44\x46\x55\x55\x55" # still has correct bytes to be recognized as PDF with self.assertRaises(HttpResponseError): poller = client.begin_recognize_content( @@ -60,8 +65,8 @@ def test_damaged_file_passed_as_bytes(self, resource_group, location, form_recog ) @GlobalFormRecognizerAccountPreparer() - def test_damaged_file_bytes_fails_autodetect_content_type(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_damaged_file_bytes_fails_autodetect_content_type(self, client): damaged_pdf = b"\x50\x44\x46\x55\x55\x55" # doesn't match any magic file numbers with self.assertRaises(ValueError): poller = client.begin_recognize_content( @@ -69,8 +74,8 @@ def test_damaged_file_bytes_fails_autodetect_content_type(self, resource_group, ) @GlobalFormRecognizerAccountPreparer() - def test_damaged_file_passed_as_bytes_io(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_damaged_file_passed_as_bytes_io(self, client): damaged_pdf = BytesIO(b"\x25\x50\x44\x46\x55\x55\x55") # still has correct bytes to be recognized as PDF with self.assertRaises(HttpResponseError): poller = client.begin_recognize_content( @@ -78,8 +83,8 @@ def test_damaged_file_passed_as_bytes_io(self, resource_group, location, form_re ) @GlobalFormRecognizerAccountPreparer() - def test_damaged_file_bytes_io_fails_autodetect(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_damaged_file_bytes_io_fails_autodetect(self, client): damaged_pdf = BytesIO(b"\x50\x44\x46\x55\x55\x55") # doesn't match any magic file numbers with self.assertRaises(ValueError): poller = client.begin_recognize_content( @@ -87,9 +92,8 @@ def test_damaged_file_bytes_io_fails_autodetect(self, resource_group, location, ) @GlobalFormRecognizerAccountPreparer() - def test_blank_page(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + def test_blank_page(self, client): with open(self.blank_pdf, "rb") as stream: poller = client.begin_recognize_content( stream, @@ -98,8 +102,8 @@ def test_blank_page(self, resource_group, location, form_recognizer_account, for self.assertIsNotNone(result) @GlobalFormRecognizerAccountPreparer() - def test_passing_bad_content_type_param_passed(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_passing_bad_content_type_param_passed(self, client): with open(self.invoice_pdf, "rb") as fd: myfile = fd.read() with self.assertRaises(ValueError): @@ -109,16 +113,14 @@ def test_passing_bad_content_type_param_passed(self, resource_group, location, f ) @GlobalFormRecognizerAccountPreparer() - def test_content_stream_passing_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + def test_content_stream_passing_url(self, client): with self.assertRaises(TypeError): poller = client.begin_recognize_content("https://badurl.jpg", content_type="application/json") @GlobalFormRecognizerAccountPreparer() - def test_auto_detect_unsupported_stream_content(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + def test_auto_detect_unsupported_stream_content(self, client): with open(self.unsupported_content_py, "rb") as fd: myfile = fd.read() @@ -128,8 +130,8 @@ def test_auto_detect_unsupported_stream_content(self, resource_group, location, ) @GlobalFormRecognizerAccountPreparer() - def test_content_stream_transform_pdf(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_content_stream_transform_pdf(self, client): with open(self.invoice_pdf, "rb") as fd: myform = fd.read() @@ -152,9 +154,8 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(layout, read_results, page_results) @GlobalFormRecognizerAccountPreparer() - def test_content_stream_pdf(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, - AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_content_stream_pdf(self, client): with open(self.invoice_pdf, "rb") as fd: myform = fd.read() @@ -169,8 +170,8 @@ def test_content_stream_pdf(self, resource_group, location, form_recognizer_acco self.assertEqual(layout.tables[0].page_number, 1) @GlobalFormRecognizerAccountPreparer() - def test_content_stream_transform_jpg(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_content_stream_transform_jpg(self, client): with open(self.form_jpg, "rb") as fd: myform = fd.read() @@ -193,9 +194,8 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(layout, read_results, page_results) @GlobalFormRecognizerAccountPreparer() - def test_content_stream_jpg(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, - AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_content_stream_jpg(self, client): with open(self.form_jpg, "rb") as stream: poller = client.begin_recognize_content(stream) result = poller.result() @@ -211,8 +211,8 @@ def test_content_stream_jpg(self, resource_group, location, form_recognizer_acco self.assertEqual(layout.tables[1].page_number, 1) @GlobalFormRecognizerAccountPreparer() - def test_content_multipage(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_content_multipage(self, client): with open(self.multipage_invoice_pdf, "rb") as fd: invoice = fd.read() poller = client.begin_recognize_content(invoice) @@ -222,8 +222,8 @@ def test_content_multipage(self, resource_group, location, form_recognizer_accou self.assertFormPagesHasValues(result) @GlobalFormRecognizerAccountPreparer() - def test_content_multipage_transform(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_content_multipage_transform(self, client): with open(self.multipage_invoice_pdf, "rb") as fd: myform = fd.read() @@ -246,10 +246,9 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(layout, read_results, page_results) @GlobalFormRecognizerAccountPreparer() + @GlobalClientPreparer() @pytest.mark.live_test_only - def test_content_continuation_token(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, - AzureKeyCredential(form_recognizer_account_key)) + def test_content_continuation_token(self, client): with open(self.form_jpg, "rb") as fd: myfile = fd.read() initial_poller = client.begin_recognize_content(myfile) @@ -261,9 +260,8 @@ def test_content_continuation_token(self, resource_group, location, form_recogni initial_poller.wait() # necessary so azure-devtools doesn't throw assertion error @GlobalFormRecognizerAccountPreparer() - def test_content_multipage_table_span_pdf(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, - AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_content_multipage_table_span_pdf(self, client): with open(self.multipage_table_pdf, "rb") as stream: poller = client.begin_recognize_content(stream) @@ -287,9 +285,8 @@ def test_content_multipage_table_span_pdf(self, resource_group, location, form_r self.assertFormPagesHasValues(result) @GlobalFormRecognizerAccountPreparer() - def test_content_multipage_table_span_transform(self, resource_group, location, form_recognizer_account, - form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_content_multipage_table_span_transform(self, client): with open(self.multipage_table_pdf, "rb") as fd: myform = fd.read() diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_content_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_content_async.py index 59dccb633efd..9c13de807c60 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_content_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_content_async.py @@ -5,6 +5,7 @@ # ------------------------------------ import pytest +import functools from io import BytesIO from azure.core.exceptions import ServiceRequestError, ClientAuthenticationError, HttpResponseError from azure.core.credentials import AzureKeyCredential @@ -14,6 +15,10 @@ from azure.ai.formrecognizer import FormContentType from testcase import GlobalFormRecognizerAccountPreparer from asynctestcase import AsyncFormRecognizerTest +from testcase import GlobalClientPreparer as _GlobalClientPreparer + + +GlobalClientPreparer = functools.partial(_GlobalClientPreparer, FormRecognizerClient) class TestContentFromStreamAsync(AsyncFormRecognizerTest): @@ -28,8 +33,8 @@ async def test_content_bad_endpoint(self, resource_group, location, form_recogni result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_content_authentication_successful_key(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_content_authentication_successful_key(self, client): with open(self.invoice_pdf, "rb") as fd: myfile = fd.read() poller = await client.begin_recognize_content(myfile) @@ -43,8 +48,8 @@ async def test_content_authentication_bad_key(self, resource_group, location, fo result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_passing_enum_content_type(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_passing_enum_content_type(self, client): with open(self.invoice_pdf, "rb") as fd: myfile = fd.read() poller = await client.begin_recognize_content( @@ -55,8 +60,8 @@ async def test_passing_enum_content_type(self, resource_group, location, form_re self.assertIsNotNone(result) @GlobalFormRecognizerAccountPreparer() - async def test_damaged_file_passed_as_bytes(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_damaged_file_passed_as_bytes(self, client): damaged_pdf = b"\x25\x50\x44\x46\x55\x55\x55" # still has correct bytes to be recognized as PDF with self.assertRaises(HttpResponseError): poller = await client.begin_recognize_content( @@ -65,8 +70,8 @@ async def test_damaged_file_passed_as_bytes(self, resource_group, location, form result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_damaged_file_bytes_fails_autodetect_content_type(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_damaged_file_bytes_fails_autodetect_content_type(self, client): damaged_pdf = b"\x50\x44\x46\x55\x55\x55" # doesn't match any magic file numbers with self.assertRaises(ValueError): poller = await client.begin_recognize_content( @@ -75,8 +80,8 @@ async def test_damaged_file_bytes_fails_autodetect_content_type(self, resource_g result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_damaged_file_passed_as_bytes_io(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_damaged_file_passed_as_bytes_io(self, client): damaged_pdf = BytesIO(b"\x25\x50\x44\x46\x55\x55\x55") # still has correct bytes to be recognized as PDF with self.assertRaises(HttpResponseError): poller = await client.begin_recognize_content( @@ -85,8 +90,8 @@ async def test_damaged_file_passed_as_bytes_io(self, resource_group, location, f result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_damaged_file_bytes_io_fails_autodetect(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_damaged_file_bytes_io_fails_autodetect(self, client): damaged_pdf = BytesIO(b"\x50\x44\x46\x55\x55\x55") # doesn't match any magic file numbers with self.assertRaises(ValueError): poller = await client.begin_recognize_content( @@ -95,9 +100,8 @@ async def test_damaged_file_bytes_io_fails_autodetect(self, resource_group, loca result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_blank_page(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + async def test_blank_page(self, client): with open(self.blank_pdf, "rb") as fd: blank = fd.read() poller = await client.begin_recognize_content( @@ -107,8 +111,8 @@ async def test_blank_page(self, resource_group, location, form_recognizer_accoun self.assertIsNotNone(result) @GlobalFormRecognizerAccountPreparer() - async def test_passing_bad_content_type_param_passed(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_passing_bad_content_type_param_passed(self, client): with open(self.invoice_pdf, "rb") as fd: myfile = fd.read() with self.assertRaises(ValueError): @@ -119,17 +123,15 @@ async def test_passing_bad_content_type_param_passed(self, resource_group, locat result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_content_stream_passing_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + async def test_content_stream_passing_url(self, client): with self.assertRaises(TypeError): poller = await client.begin_recognize_content("https://badurl.jpg", content_type="application/json") result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_auto_detect_unsupported_stream_content(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + async def test_auto_detect_unsupported_stream_content(self, client): with open(self.unsupported_content_py, "rb") as fd: myfile = fd.read() @@ -140,8 +142,8 @@ async def test_auto_detect_unsupported_stream_content(self, resource_group, loca result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_content_stream_transform_pdf(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_content_stream_transform_pdf(self, client): with open(self.invoice_pdf, "rb") as fd: myform = fd.read() @@ -164,9 +166,8 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(layout, read_results, page_results) @GlobalFormRecognizerAccountPreparer() - async def test_content_stream_pdf(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, - AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_content_stream_pdf(self, client): with open(self.invoice_pdf, "rb") as fd: myform = fd.read() @@ -181,8 +182,8 @@ async def test_content_stream_pdf(self, resource_group, location, form_recognize self.assertEqual(layout.tables[0].page_number, 1) @GlobalFormRecognizerAccountPreparer() - async def test_content_stream_transform_jpg(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_content_stream_transform_jpg(self, client): with open(self.form_jpg, "rb") as fd: myform = fd.read() @@ -205,9 +206,8 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(layout, read_results, page_results) @GlobalFormRecognizerAccountPreparer() - async def test_content_stream_jpg(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, - AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_content_stream_jpg(self, client): with open(self.form_jpg, "rb") as fd: myform = fd.read() @@ -225,8 +225,8 @@ async def test_content_stream_jpg(self, resource_group, location, form_recognize self.assertEqual(layout.tables[1].page_number, 1) @GlobalFormRecognizerAccountPreparer() - async def test_content_multipage(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_content_multipage(self, client): with open(self.multipage_invoice_pdf, "rb") as fd: invoice = fd.read() poller = await client.begin_recognize_content(invoice) @@ -236,8 +236,8 @@ async def test_content_multipage(self, resource_group, location, form_recognizer self.assertFormPagesHasValues(result) @GlobalFormRecognizerAccountPreparer() - async def test_content_multipage_transform(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_content_multipage_transform(self, client): with open(self.multipage_invoice_pdf, "rb") as fd: myform = fd.read() @@ -260,10 +260,9 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(layout, read_results, page_results) @GlobalFormRecognizerAccountPreparer() + @GlobalClientPreparer() @pytest.mark.live_test_only - async def test_content_continuation_token(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, - AzureKeyCredential(form_recognizer_account_key)) + async def test_content_continuation_token(self, client): with open(self.form_jpg, "rb") as fd: myfile = fd.read() initial_poller = await client.begin_recognize_content(myfile) @@ -276,10 +275,8 @@ async def test_content_continuation_token(self, resource_group, location, form_r @GlobalFormRecognizerAccountPreparer() - async def test_content_multipage_table_span_pdf(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, - AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + async def test_content_multipage_table_span_pdf(self, client): with open(self.multipage_table_pdf, "rb") as fd: myfile = fd.read() poller = await client.begin_recognize_content(myfile) @@ -303,9 +300,8 @@ async def test_content_multipage_table_span_pdf(self, resource_group, location, self.assertFormPagesHasValues(result) @GlobalFormRecognizerAccountPreparer() - async def test_content_multipage_table_span_transform(self, resource_group, location, form_recognizer_account, - form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_content_multipage_table_span_transform(self, client): with open(self.multipage_table_pdf, "rb") as fd: myform = fd.read() diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_content_from_url.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_content_from_url.py index 8c45cbe9758a..caeef8752c99 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_content_from_url.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_content_from_url.py @@ -5,12 +5,17 @@ # ------------------------------------ import pytest +import functools from azure.core.exceptions import HttpResponseError, ServiceRequestError, ClientAuthenticationError from azure.core.credentials import AzureKeyCredential from azure.ai.formrecognizer._generated.models import AnalyzeOperationResult from azure.ai.formrecognizer._response_handlers import prepare_content_result from azure.ai.formrecognizer import FormRecognizerClient from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer +from testcase import GlobalClientPreparer as _GlobalClientPreparer + + +GlobalClientPreparer = functools.partial(_GlobalClientPreparer, FormRecognizerClient) class TestContentFromUrl(FormRecognizerTest): @@ -22,8 +27,8 @@ def test_content_url_bad_endpoint(self, resource_group, location, form_recognize poller = client.begin_recognize_content_from_url(self.invoice_url_pdf) @GlobalFormRecognizerAccountPreparer() - def test_content_url_auth_successful_key(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_content_url_auth_successful_key(self, client): poller = client.begin_recognize_content_from_url(self.invoice_url_pdf) result = poller.result() @@ -34,23 +39,21 @@ def test_content_url_auth_bad_key(self, resource_group, location, form_recognize poller = client.begin_recognize_content_from_url(self.invoice_url_pdf) @GlobalFormRecognizerAccountPreparer() - def test_content_bad_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + def test_content_bad_url(self, client): with self.assertRaises(HttpResponseError): poller = client.begin_recognize_content_from_url("https://badurl.jpg") @GlobalFormRecognizerAccountPreparer() - def test_content_url_pass_stream(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_content_url_pass_stream(self, client): with open(self.receipt_jpg, "rb") as receipt: with self.assertRaises(HttpResponseError): poller = client.begin_recognize_content_from_url(receipt) @GlobalFormRecognizerAccountPreparer() - def test_content_url_transform_pdf(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + def test_content_url_transform_pdf(self, client): responses = [] def callback(raw_response, _, headers): @@ -70,10 +73,8 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(layout, read_results, page_results) @GlobalFormRecognizerAccountPreparer() - def test_content_url_pdf(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, - AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + def test_content_url_pdf(self, client): poller = client.begin_recognize_content_from_url(self.invoice_url_pdf) result = poller.result() self.assertEqual(len(result), 1) @@ -85,9 +86,8 @@ def test_content_url_pdf(self, resource_group, location, form_recognizer_account self.assertEqual(layout.tables[0].page_number, 1) @GlobalFormRecognizerAccountPreparer() - def test_content_url_transform_jpg(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + def test_content_url_transform_jpg(self, client): responses = [] def callback(raw_response, _, headers): @@ -107,10 +107,8 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(layout, read_results, page_results) @GlobalFormRecognizerAccountPreparer() - def test_content_url_jpg(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, - AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + def test_content_url_jpg(self, client): poller = client.begin_recognize_content_from_url(self.form_url_jpg) result = poller.result() self.assertEqual(len(result), 1) @@ -125,8 +123,8 @@ def test_content_url_jpg(self, resource_group, location, form_recognizer_account self.assertEqual(layout.tables[1].page_number, 1) @GlobalFormRecognizerAccountPreparer() - def test_content_multipage_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_content_multipage_url(self, client): poller = client.begin_recognize_content_from_url(self.multipage_url_pdf) result = poller.result() @@ -134,8 +132,8 @@ def test_content_multipage_url(self, resource_group, location, form_recognizer_a self.assertFormPagesHasValues(result) @GlobalFormRecognizerAccountPreparer() - def test_content_multipage_transform_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_content_multipage_transform_url(self, client): responses = [] def callback(raw_response, _, headers): @@ -155,10 +153,9 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(layout, read_results, page_results) @GlobalFormRecognizerAccountPreparer() + @GlobalClientPreparer() @pytest.mark.live_test_only - def test_content_continuation_token(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, - AzureKeyCredential(form_recognizer_account_key)) + def test_content_continuation_token(self, client): initial_poller = client.begin_recognize_content_from_url(self.form_url_jpg) cont_token = initial_poller.continuation_token() @@ -168,10 +165,8 @@ def test_content_continuation_token(self, resource_group, location, form_recogni initial_poller.wait() # necessary so azure-devtools doesn't throw assertion error @GlobalFormRecognizerAccountPreparer() - def test_content_multipage_table_span_pdf(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, - AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + def test_content_multipage_table_span_pdf(self, client): poller = client.begin_recognize_content_from_url(self.multipage_table_url_pdf) result = poller.result() self.assertEqual(len(result), 2) @@ -193,10 +188,8 @@ def test_content_multipage_table_span_pdf(self, resource_group, location, form_r self.assertFormPagesHasValues(result) @GlobalFormRecognizerAccountPreparer() - def test_content_multipage_table_span_transform(self, resource_group, location, form_recognizer_account, - form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + def test_content_multipage_table_span_transform(self, client): responses = [] def callback(raw_response, _, headers): diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_content_from_url_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_content_from_url_async.py index 48536e0072f7..c40afea312f3 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_content_from_url_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_content_from_url_async.py @@ -5,6 +5,7 @@ # ------------------------------------ import pytest +import functools from azure.core.exceptions import HttpResponseError, ServiceRequestError, ClientAuthenticationError from azure.core.credentials import AzureKeyCredential from azure.ai.formrecognizer._generated.models import AnalyzeOperationResult @@ -12,6 +13,10 @@ from azure.ai.formrecognizer.aio import FormRecognizerClient from testcase import GlobalFormRecognizerAccountPreparer from asynctestcase import AsyncFormRecognizerTest +from testcase import GlobalClientPreparer as _GlobalClientPreparer + + +GlobalClientPreparer = functools.partial(_GlobalClientPreparer, FormRecognizerClient) class TestContentFromUrlAsync(AsyncFormRecognizerTest): @@ -24,8 +29,8 @@ async def test_content_url_bad_endpoint(self, resource_group, location, form_rec result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_content_url_auth_successful_key(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_content_url_auth_successful_key(self, client): poller = await client.begin_recognize_content_from_url(self.invoice_url_pdf) result = await poller.result() @@ -37,16 +42,15 @@ async def test_content_url_auth_bad_key(self, resource_group, location, form_rec result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_content_bad_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + async def test_content_bad_url(self, client): with self.assertRaises(HttpResponseError): poller = await client.begin_recognize_content_from_url("https://badurl.jpg") result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_content_url_pass_stream(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_content_url_pass_stream(self, client): with open(self.receipt_jpg, "rb") as fd: receipt = fd.read(4) # makes the recording smaller @@ -55,9 +59,8 @@ async def test_content_url_pass_stream(self, resource_group, location, form_reco result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_content_url_transform_pdf(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + async def test_content_url_transform_pdf(self, client): responses = [] def callback(raw_response, _, headers): @@ -77,10 +80,8 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(layout, read_results, page_results) @GlobalFormRecognizerAccountPreparer() - async def test_content_url_pdf(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, - AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + async def test_content_url_pdf(self, client): poller = await client.begin_recognize_content_from_url(self.invoice_url_pdf) result = await poller.result() self.assertEqual(len(result), 1) @@ -92,9 +93,8 @@ async def test_content_url_pdf(self, resource_group, location, form_recognizer_a self.assertEqual(layout.tables[0].page_number, 1) @GlobalFormRecognizerAccountPreparer() - async def test_content_url_transform_jpg(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + async def test_content_url_transform_jpg(self, client): responses = [] def callback(raw_response, _, headers): @@ -114,10 +114,8 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(layout, read_results, page_results) @GlobalFormRecognizerAccountPreparer() - async def test_content_url_jpg(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, - AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + async def test_content_url_jpg(self, client): poller = await client.begin_recognize_content_from_url(self.form_url_jpg) result = await poller.result() self.assertEqual(len(result), 1) @@ -132,16 +130,16 @@ async def test_content_url_jpg(self, resource_group, location, form_recognizer_a self.assertEqual(layout.tables[1].page_number, 1) @GlobalFormRecognizerAccountPreparer() - async def test_content_multipage_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_content_multipage_url(self, client): poller = await client.begin_recognize_content_from_url(self.multipage_url_pdf) result = await poller.result() self.assertEqual(len(result), 3) self.assertFormPagesHasValues(result) @GlobalFormRecognizerAccountPreparer() - async def test_content_multipage_transform_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_content_multipage_transform_url(self, client): responses = [] def callback(raw_response, _, headers): @@ -161,10 +159,9 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(layout, read_results, page_results) @GlobalFormRecognizerAccountPreparer() + @GlobalClientPreparer() @pytest.mark.live_test_only - async def test_content_continuation_token(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, - AzureKeyCredential(form_recognizer_account_key)) + async def test_content_continuation_token(self, client): initial_poller = await client.begin_recognize_content_from_url(self.form_url_jpg) cont_token = initial_poller.continuation_token() @@ -174,10 +171,8 @@ async def test_content_continuation_token(self, resource_group, location, form_r await initial_poller.wait() # necessary so azure-devtools doesn't throw assertion error @GlobalFormRecognizerAccountPreparer() - async def test_content_multipage_table_span_pdf(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, - AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + async def test_content_multipage_table_span_pdf(self, client): poller = await client.begin_recognize_content_from_url(self.multipage_table_url_pdf) result = await poller.result() self.assertEqual(len(result), 2) @@ -199,10 +194,8 @@ async def test_content_multipage_table_span_pdf(self, resource_group, location, self.assertFormPagesHasValues(result) @GlobalFormRecognizerAccountPreparer() - async def test_content_multipage_table_span_transform(self, resource_group, location, form_recognizer_account, - form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + async def test_content_multipage_table_span_transform(self, client): responses = [] def callback(raw_response, _, headers): diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_copy_model.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_copy_model.py index b5e2cedf9e38..8904bbe29745 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_copy_model.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_copy_model.py @@ -11,27 +11,28 @@ from azure.ai.formrecognizer import CustomFormModelInfo from azure.ai.formrecognizer import FormTrainingClient from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer -from testcase import GlobalTrainingAccountPreparer as _GlobalTrainingAccountPreparer +from testcase import GlobalClientPreparer as _GlobalClientPreparer -GlobalTrainingAccountPreparer = functools.partial(_GlobalTrainingAccountPreparer, FormTrainingClient) + +GlobalClientPreparer = functools.partial(_GlobalClientPreparer, FormTrainingClient) class TestCopyModel(FormRecognizerTest): @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) def test_copy_model_none_model_id(self, client, container_sas_url): with self.assertRaises(ValueError): client.begin_copy_model(model_id=None, target={}) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) def test_copy_model_empty_model_id(self, client, container_sas_url): with self.assertRaises(ValueError): client.begin_copy_model(model_id="", target={}) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(copy=True) + @GlobalClientPreparer(training=True, copy=True) def test_copy_model_successful(self, client, container_sas_url, location, resource_id): poller = client.begin_training(container_sas_url, use_training_labels=False) @@ -52,7 +53,7 @@ def test_copy_model_successful(self, client, container_sas_url, location, resour self.assertIsNotNone(copied_model) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(copy=True) + @GlobalClientPreparer(training=True, copy=True) def test_copy_model_fail(self, client, container_sas_url, location, resource_id): poller = client.begin_training(container_sas_url, use_training_labels=False) @@ -66,7 +67,7 @@ def test_copy_model_fail(self, client, container_sas_url, location, resource_id) copy = poller.result() @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(copy=True) + @GlobalClientPreparer(training=True, copy=True) def test_copy_model_transform(self, client, container_sas_url, location, resource_id): poller = client.begin_training(container_sas_url, use_training_labels=False) @@ -93,7 +94,7 @@ def callback(response, _, headers): self.assertEqual(copy.model_id, target["modelId"]) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(copy=True) + @GlobalClientPreparer(training=True, copy=True) def test_copy_authorization(self, client, container_sas_url, location, resource_id): target = client.get_copy_authorization(resource_region="eastus", resource_id=resource_id) @@ -105,7 +106,7 @@ def test_copy_authorization(self, client, container_sas_url, location, resource_ self.assertEqual(target["resourceId"], resource_id) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(copy=True) + @GlobalClientPreparer(training=True, copy=True) @pytest.mark.live_test_only def test_copy_continuation_token(self, client, container_sas_url, location, resource_id): diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_copy_model_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_copy_model_async.py index e9c12e069677..18ff025e80de 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_copy_model_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_copy_model_async.py @@ -11,28 +11,29 @@ from azure.ai.formrecognizer import CustomFormModelInfo from azure.ai.formrecognizer.aio import FormTrainingClient from testcase import GlobalFormRecognizerAccountPreparer -from testcase import GlobalTrainingAccountPreparer as _GlobalTrainingAccountPreparer from asynctestcase import AsyncFormRecognizerTest +from testcase import GlobalClientPreparer as _GlobalClientPreparer -GlobalTrainingAccountPreparer = functools.partial(_GlobalTrainingAccountPreparer, FormTrainingClient) + +GlobalClientPreparer = functools.partial(_GlobalClientPreparer, FormTrainingClient) class TestCopyModelAsync(AsyncFormRecognizerTest): @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) async def test_copy_model_none_model_id(self, client, container_sas_url): with self.assertRaises(ValueError): await client.begin_copy_model(model_id=None, target={}) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) async def test_copy_model_empty_model_id(self, client, container_sas_url): with self.assertRaises(ValueError): await client.begin_copy_model(model_id="", target={}) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(copy=True) + @GlobalClientPreparer(training=True, copy=True) async def test_copy_model_successful(self, client, container_sas_url, location, resource_id): training_poller = await client.begin_training(container_sas_url, use_training_labels=False) @@ -53,7 +54,7 @@ async def test_copy_model_successful(self, client, container_sas_url, location, self.assertIsNotNone(copied_model) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(copy=True) + @GlobalClientPreparer(training=True, copy=True) async def test_copy_model_fail(self, client, container_sas_url, location, resource_id): training_poller = await client.begin_training(container_sas_url, use_training_labels=False) @@ -67,7 +68,7 @@ async def test_copy_model_fail(self, client, container_sas_url, location, resour copy = await poller.result() @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(copy=True) + @GlobalClientPreparer(training=True, copy=True) async def test_copy_model_transform(self, client, container_sas_url, location, resource_id): training_poller = await client.begin_training(container_sas_url, use_training_labels=False) @@ -94,7 +95,7 @@ def callback(response, _, headers): self.assertEqual(copy.model_id, target["modelId"]) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(copy=True) + @GlobalClientPreparer(training=True, copy=True) async def test_copy_authorization(self, client, container_sas_url, location, resource_id): target = await client.get_copy_authorization(resource_region="eastus", resource_id=resource_id) @@ -106,7 +107,7 @@ async def test_copy_authorization(self, client, container_sas_url, location, res self.assertEqual(target["resourceId"], resource_id) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(copy=True) + @GlobalClientPreparer(training=True, copy=True) @pytest.mark.live_test_only async def test_copy_continuation_token(self, client, container_sas_url, location, resource_id): diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms.py index 9a5d6aab553e..752d24666cf1 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms.py @@ -12,10 +12,10 @@ from azure.ai.formrecognizer._generated.models import AnalyzeOperationResult from azure.ai.formrecognizer._response_handlers import prepare_form_result from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer -from testcase import GlobalTrainingAccountPreparer as _GlobalTrainingAccountPreparer +from testcase import GlobalClientPreparer as _GlobalClientPreparer -GlobalTrainingAccountPreparer = functools.partial(_GlobalTrainingAccountPreparer, FormTrainingClient) +GlobalClientPreparer = functools.partial(_GlobalClientPreparer, FormTrainingClient) class TestCustomForms(FormRecognizerTest): @@ -49,14 +49,12 @@ def test_authentication_bad_key(self, resource_group, location, form_recognizer_ @GlobalFormRecognizerAccountPreparer() def test_passing_unsupported_url_content_type(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - with self.assertRaises(TypeError): poller = client.begin_recognize_custom_forms(model_id="xx", form="https://badurl.jpg", content_type="application/json") @GlobalFormRecognizerAccountPreparer() def test_auto_detect_unsupported_stream_content(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - with open(self.unsupported_content_py, "rb") as fd: myfile = fd.read() @@ -67,7 +65,7 @@ def test_auto_detect_unsupported_stream_content(self, resource_group, location, ) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) def test_custom_form_damaged_file(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -82,7 +80,7 @@ def test_custom_form_damaged_file(self, client, container_sas_url): form = poller.result() @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) def test_custom_form_unlabeled(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -107,7 +105,7 @@ def test_custom_form_unlabeled(self, client, container_sas_url): self.assertIsNotNone(field.label_data.text) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True) + @GlobalClientPreparer(training=True, multipage=True) def test_custom_form_multipage_unlabeled(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -135,7 +133,7 @@ def test_custom_form_multipage_unlabeled(self, client, container_sas_url): self.assertIsNotNone(field.label_data.text) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) def test_custom_form_labeled(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -160,7 +158,7 @@ def test_custom_form_labeled(self, client, container_sas_url): self.assertIsNotNone(field.value_data.bounding_box) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True) + @GlobalClientPreparer(training=True, multipage=True) def test_custom_form_multipage_labeled(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -190,7 +188,7 @@ def test_custom_form_multipage_labeled(self, client, container_sas_url): self.assertIsNotNone(field.value_data.bounding_box) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) def test_custom_form_unlabeled_transform(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -227,7 +225,7 @@ def callback(raw_response, _, headers): self.assertUnlabeledFormFieldDictTransformCorrect(recognized_form[0].fields, actual_fields, read_results) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True) + @GlobalClientPreparer(training=True, multipage=True) def test_custom_form_multipage_unlabeled_transform(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -265,7 +263,7 @@ def callback(raw_response, _, headers): self.assertUnlabeledFormFieldDictTransformCorrect(form.fields, actual.key_value_pairs, read_results) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) def test_custom_form_labeled_transform(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -302,7 +300,7 @@ def callback(raw_response, _, headers): self.assertLabeledFormFieldDictTransformCorrect(recognized_form[0].fields, actual_fields, read_results) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True) + @GlobalClientPreparer(training=True, multipage=True) def test_custom_form_multipage_labeled_transform(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -341,7 +339,7 @@ def callback(raw_response, _, headers): self.assertLabeledFormFieldDictTransformCorrect(form.fields, actual.fields, read_results) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) @pytest.mark.live_test_only def test_custom_form_continuation_token(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -366,7 +364,7 @@ def test_custom_form_continuation_token(self, client, container_sas_url): initial_poller.wait() # necessary so azure-devtools doesn't throw assertion error @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage2=True) + @GlobalClientPreparer(training=True, multipage2=True) def test_custom_form_multipage_vendor_set_unlabeled_transform(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -405,7 +403,7 @@ def callback(raw_response, _, headers): self.assertLabeledFormFieldDictTransformCorrect(form.fields, actual.fields, read_results) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage2=True) + @GlobalClientPreparer(training=True, multipage2=True) def test_custom_form_multipage_vendor_set_labeled_transform(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms_async.py index 903e35b6c4df..7b08ea902f42 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms_async.py @@ -13,11 +13,11 @@ from azure.ai.formrecognizer._generated.models import AnalyzeOperationResult from azure.ai.formrecognizer._response_handlers import prepare_form_result from testcase import GlobalFormRecognizerAccountPreparer -from testcase import GlobalTrainingAccountPreparer as _GlobalTrainingAccountPreparer from asynctestcase import AsyncFormRecognizerTest +from testcase import GlobalClientPreparer as _GlobalClientPreparer -GlobalTrainingAccountPreparer = functools.partial(_GlobalTrainingAccountPreparer, FormTrainingClient) +GlobalClientPreparer = functools.partial(_GlobalClientPreparer, FormTrainingClient) class TestCustomFormsAsync(AsyncFormRecognizerTest): @@ -73,7 +73,7 @@ async def test_auto_detect_unsupported_stream_content(self, resource_group, loca result = await poller.result() @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) async def test_custom_form_damaged_file(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -88,7 +88,7 @@ async def test_custom_form_damaged_file(self, client, container_sas_url): result = await poller.result() @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) async def test_custom_form_unlabeled(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -110,7 +110,7 @@ async def test_custom_form_unlabeled(self, client, container_sas_url): self.assertIsNotNone(field.label_data.text) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True) + @GlobalClientPreparer(training=True, multipage=True) async def test_custom_form_multipage_unlabeled(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -140,7 +140,7 @@ async def test_custom_form_multipage_unlabeled(self, client, container_sas_url): self.assertIsNotNone(field.label_data.text) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) async def test_custom_form_labeled(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -162,7 +162,7 @@ async def test_custom_form_labeled(self, client, container_sas_url): self.assertIsNotNone(field.value_data.bounding_box) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True) + @GlobalClientPreparer(training=True, multipage=True) async def test_custom_form_multipage_labeled(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -193,7 +193,7 @@ async def test_custom_form_multipage_labeled(self, client, container_sas_url): @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) async def test_form_unlabeled_transform(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -231,7 +231,7 @@ def callback(raw_response, _, headers): self.assertUnlabeledFormFieldDictTransformCorrect(recognized_form[0].fields, actual_fields, read_results) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True) + @GlobalClientPreparer(training=True, multipage=True) async def test_custom_forms_multipage_unlabeled_transform(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -270,7 +270,7 @@ def callback(raw_response, _, headers): @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) async def test_form_labeled_transform(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -308,7 +308,7 @@ def callback(raw_response, _, headers): self.assertLabeledFormFieldDictTransformCorrect(recognized_form[0].fields, actual_fields, read_results) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True) + @GlobalClientPreparer(training=True, multipage=True) async def test_custom_forms_multipage_labeled_transform(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -348,7 +348,7 @@ def callback(raw_response, _, headers): self.assertLabeledFormFieldDictTransformCorrect(form.fields, actual.fields, read_results) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) @pytest.mark.live_test_only async def test_custom_form_continuation_token(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -374,7 +374,7 @@ async def test_custom_form_continuation_token(self, client, container_sas_url): await initial_poller.wait() # necessary so azure-devtools doesn't throw assertion error @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage2=True) + @GlobalClientPreparer(training=True, multipage2=True) async def test_custom_form_multipage_vendor_set_unlabeled_transform(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -413,7 +413,7 @@ def callback(raw_response, _, headers): self.assertLabeledFormFieldDictTransformCorrect(form.fields, actual.fields, read_results) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage2=True) + @GlobalClientPreparer(training=True, multipage2=True) async def test_custom_form_multipage_vendor_set_labeled_transform(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms_from_url.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms_from_url.py index f20b84dd71a3..de974ac79afd 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms_from_url.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms_from_url.py @@ -12,10 +12,10 @@ from azure.ai.formrecognizer._generated.models import AnalyzeOperationResult from azure.ai.formrecognizer._response_handlers import prepare_form_result from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer -from testcase import GlobalTrainingAccountPreparer as _GlobalTrainingAccountPreparer +from testcase import GlobalClientPreparer as _GlobalClientPreparer -GlobalTrainingAccountPreparer = functools.partial(_GlobalTrainingAccountPreparer, FormTrainingClient) +GlobalClientPreparer = functools.partial(_GlobalClientPreparer, FormTrainingClient) class TestCustomFormsFromUrl(FormRecognizerTest): @@ -63,7 +63,7 @@ def test_pass_stream_into_url(self, resource_group, location, form_recognizer_ac ) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) def test_custom_form_bad_url(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -78,7 +78,7 @@ def test_custom_form_bad_url(self, client, container_sas_url): form = poller.result() @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) def test_custom_form_unlabeled(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -98,7 +98,7 @@ def test_custom_form_unlabeled(self, client, container_sas_url): self.assertIsNotNone(field.label_data.text) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True, blob_sas_url=True) + @GlobalClientPreparer(training=True, multipage=True, blob_sas_url=True) def test_form_multipage_unlabeled(self, client, container_sas_url, blob_sas_url): fr_client = client.get_form_recognizer_client() @@ -124,7 +124,7 @@ def test_form_multipage_unlabeled(self, client, container_sas_url, blob_sas_url) self.assertIsNotNone(field.label_data.text) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) def test_custom_form_labeled(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -143,7 +143,7 @@ def test_custom_form_labeled(self, client, container_sas_url): self.assertIsNotNone(field.value_data.bounding_box) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True, blob_sas_url=True) + @GlobalClientPreparer(training=True, multipage=True, blob_sas_url=True) def test_form_multipage_labeled(self, client, container_sas_url, blob_sas_url): fr_client = client.get_form_recognizer_client() @@ -170,7 +170,7 @@ def test_form_multipage_labeled(self, client, container_sas_url, blob_sas_url): @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) def test_custom_form_unlabeled_transform(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -204,7 +204,7 @@ def callback(raw_response, _, headers): self.assertUnlabeledFormFieldDictTransformCorrect(recognized_form[0].fields, actual_fields, read_results) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True, blob_sas_url=True) + @GlobalClientPreparer(training=True, multipage=True, blob_sas_url=True) def test_custom_form_multipage_unlabeled_transform(self, client, container_sas_url, blob_sas_url): fr_client = client.get_form_recognizer_client() @@ -240,7 +240,7 @@ def callback(raw_response, _, headers): @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) def test_form_labeled_transform(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -274,7 +274,7 @@ def callback(raw_response, _, headers): self.assertLabeledFormFieldDictTransformCorrect(recognized_form[0].fields, actual_fields, read_results) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True, blob_sas_url=True) + @GlobalClientPreparer(training=True, multipage=True, blob_sas_url=True) def test_custom_form_multipage_labeled_transform(self, client, container_sas_url, blob_sas_url): fr_client = client.get_form_recognizer_client() @@ -310,7 +310,7 @@ def callback(raw_response, _, headers): self.assertLabeledFormFieldDictTransformCorrect(form.fields, actual.fields, read_results) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) @pytest.mark.live_test_only def test_custom_form_continuation_token(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -334,7 +334,7 @@ def test_custom_form_continuation_token(self, client, container_sas_url): initial_poller.wait() # necessary so azure-devtools doesn't throw assertion error @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage2=True, blob_sas_url=True) + @GlobalClientPreparer(training=True, multipage2=True, blob_sas_url=True) def test_custom_form_multipage_vendor_set_unlabeled_transform(self, client, container_sas_url, blob_sas_url): fr_client = client.get_form_recognizer_client() @@ -370,7 +370,7 @@ def callback(raw_response, _, headers): self.assertLabeledFormFieldDictTransformCorrect(form.fields, actual.fields, read_results) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage2=True, blob_sas_url=True) + @GlobalClientPreparer(training=True, multipage2=True, blob_sas_url=True) def test_custom_form_multipage_vendor_set_labeled_transform(self, client, container_sas_url, blob_sas_url): fr_client = client.get_form_recognizer_client() diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms_from_url_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms_from_url_async.py index 34fc8660cf32..da5a03505a28 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms_from_url_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_custom_forms_from_url_async.py @@ -12,10 +12,11 @@ from azure.ai.formrecognizer._generated.models import AnalyzeOperationResult from azure.ai.formrecognizer._response_handlers import prepare_form_result from testcase import GlobalFormRecognizerAccountPreparer -from testcase import GlobalTrainingAccountPreparer as _GlobalTrainingAccountPreparer from asynctestcase import AsyncFormRecognizerTest +from testcase import GlobalClientPreparer as _GlobalClientPreparer -GlobalTrainingAccountPreparer = functools.partial(_GlobalTrainingAccountPreparer, FormTrainingClient) + +GlobalClientPreparer = functools.partial(_GlobalClientPreparer, FormTrainingClient) class TestCustomFormsFromUrlAsync(AsyncFormRecognizerTest): @@ -67,7 +68,7 @@ async def test_pass_stream_into_url(self, resource_group, location, form_recogni result = await poller.result() @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) async def test_form_bad_url(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -82,7 +83,7 @@ async def test_form_bad_url(self, client, container_sas_url): result = await poller.result() @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) async def test_form_unlabeled(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -102,7 +103,7 @@ async def test_form_unlabeled(self, client, container_sas_url): self.assertIsNotNone(field.label_data.text) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True, blob_sas_url=True) + @GlobalClientPreparer(training=True, multipage=True, blob_sas_url=True) async def test_custom_form_multipage_unlabeled(self, client, container_sas_url, blob_sas_url): fr_client = client.get_form_recognizer_client() @@ -128,7 +129,7 @@ async def test_custom_form_multipage_unlabeled(self, client, container_sas_url, self.assertIsNotNone(field.label_data.text) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) async def test_form_labeled(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -147,7 +148,7 @@ async def test_form_labeled(self, client, container_sas_url): self.assertIsNotNone(field.value_data.bounding_box) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True, blob_sas_url=True) + @GlobalClientPreparer(training=True, multipage=True, blob_sas_url=True) async def test_form_multipage_labeled(self, client, container_sas_url, blob_sas_url): fr_client = client.get_form_recognizer_client() @@ -173,7 +174,7 @@ async def test_form_multipage_labeled(self, client, container_sas_url, blob_sas_ self.assertIsNotNone(field.value_data.bounding_box) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) async def test_form_unlabeled_transform(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -208,7 +209,7 @@ def callback(raw_response, _, headers): self.assertUnlabeledFormFieldDictTransformCorrect(recognized_form[0].fields, actual_fields, read_results) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True, blob_sas_url=True) + @GlobalClientPreparer(training=True, multipage=True, blob_sas_url=True) async def test_multipage_unlabeled_transform(self, client, container_sas_url, blob_sas_url): fr_client = client.get_form_recognizer_client() @@ -244,7 +245,7 @@ def callback(raw_response, _, headers): self.assertUnlabeledFormFieldDictTransformCorrect(form.fields, actual.key_value_pairs, read_results) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) async def test_form_labeled_transform(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -279,7 +280,7 @@ def callback(raw_response, _, headers): self.assertLabeledFormFieldDictTransformCorrect(recognized_form[0].fields, actual_fields, read_results) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True, blob_sas_url=True) + @GlobalClientPreparer(training=True, multipage=True, blob_sas_url=True) async def test_multipage_labeled_transform(self, client, container_sas_url, blob_sas_url): fr_client = client.get_form_recognizer_client() @@ -316,7 +317,7 @@ def callback(raw_response, _, headers): self.assertLabeledFormFieldDictTransformCorrect(form.fields, actual.fields, read_results) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) @pytest.mark.live_test_only async def test_custom_form_continuation_token(self, client, container_sas_url): fr_client = client.get_form_recognizer_client() @@ -339,7 +340,7 @@ async def test_custom_form_continuation_token(self, client, container_sas_url): await initial_poller.wait() # necessary so azure-devtools doesn't throw assertion error @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage2=True, blob_sas_url=True) + @GlobalClientPreparer(training=True, multipage2=True, blob_sas_url=True) async def test_custom_form_multipage_vendor_set_unlabeled_transform(self, client, container_sas_url, blob_sas_url): fr_client = client.get_form_recognizer_client() @@ -375,7 +376,7 @@ def callback(raw_response, _, headers): self.assertLabeledFormFieldDictTransformCorrect(form.fields, actual.fields, read_results) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage2=True, blob_sas_url=True) + @GlobalClientPreparer(training=True, multipage2=True, blob_sas_url=True) async def test_custom_form_multipage_vendor_set_labeled_transform(self, client, container_sas_url, blob_sas_url): fr_client = client.get_form_recognizer_client() diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_mgmt.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_mgmt.py index 0de73c057d2d..668f8080e82f 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_mgmt.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_mgmt.py @@ -8,12 +8,12 @@ from azure.core.credentials import AzureKeyCredential from azure.core.exceptions import ResourceNotFoundError, ClientAuthenticationError from azure.core.pipeline.transport import RequestsTransport -from azure.ai.formrecognizer import FormTrainingClient, FormRecognizerClient +from azure.ai.formrecognizer import FormTrainingClient from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer -from testcase import GlobalTrainingAccountPreparer as _GlobalTrainingAccountPreparer +from testcase import GlobalClientPreparer as _GlobalClientPreparer -GlobalTrainingAccountPreparer = functools.partial(_GlobalTrainingAccountPreparer, FormTrainingClient) +GlobalClientPreparer = functools.partial(_GlobalClientPreparer, FormTrainingClient) class TestManagement(FormRecognizerTest): @@ -31,14 +31,14 @@ def test_get_model_auth_bad_key(self, resource_group, location, form_recognizer_ result = client.get_custom_model("xx") @GlobalFormRecognizerAccountPreparer() - def test_get_model_empty_model_id(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_get_model_empty_model_id(self, client): with self.assertRaises(ValueError): result = client.get_custom_model("") @GlobalFormRecognizerAccountPreparer() - def test_get_model_none_model_id(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_get_model_none_model_id(self, client): with self.assertRaises(ValueError): result = client.get_custom_model(None) @@ -57,27 +57,27 @@ def test_delete_model_auth_bad_key(self, resource_group, location, form_recogniz client.delete_model("xx") @GlobalFormRecognizerAccountPreparer() - def test_delete_model_none_model_id(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_delete_model_none_model_id(self, client): with self.assertRaises(ValueError): result = client.delete_model(None) @GlobalFormRecognizerAccountPreparer() - def test_delete_model_empty_model_id(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_delete_model_empty_model_id(self, client): with self.assertRaises(ValueError): result = client.delete_model("") @GlobalFormRecognizerAccountPreparer() - def test_account_properties(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_account_properties(self, client): properties = client.get_account_properties() self.assertIsNotNone(properties.custom_model_limit) self.assertIsNotNone(properties.custom_model_count) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) def test_mgmt_model_labeled(self, client, container_sas_url): poller = client.begin_training(container_sas_url, use_training_labels=True) @@ -113,7 +113,7 @@ def test_mgmt_model_labeled(self, client, container_sas_url): client.get_custom_model(labeled_model_from_train.model_id) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) def test_mgmt_model_unlabeled(self, client, container_sas_url): poller = client.begin_training(container_sas_url, use_training_labels=False) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_mgmt_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_mgmt_async.py index e2d03690e520..013552bc98d4 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_mgmt_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_mgmt_async.py @@ -9,13 +9,13 @@ from azure.core.pipeline.transport import AioHttpTransport from azure.core.credentials import AzureKeyCredential from azure.core.exceptions import ResourceNotFoundError, ClientAuthenticationError -from azure.ai.formrecognizer.aio import FormTrainingClient, FormRecognizerClient +from azure.ai.formrecognizer.aio import FormTrainingClient from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer -from testcase import GlobalTrainingAccountPreparer as _GlobalTrainingAccountPreparer from asynctestcase import AsyncFormRecognizerTest +from testcase import GlobalClientPreparer as _GlobalClientPreparer -GlobalTrainingAccountPreparer = functools.partial(_GlobalTrainingAccountPreparer, FormTrainingClient) +GlobalClientPreparer = functools.partial(_GlobalClientPreparer, FormTrainingClient) class TestManagementAsync(AsyncFormRecognizerTest): @@ -42,14 +42,14 @@ async def test_get_model_auth_bad_key(self, resource_group, location, form_recog result = await client.get_custom_model("xx") @GlobalFormRecognizerAccountPreparer() - async def test_get_model_empty_model_id(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_get_model_empty_model_id(self, client): with self.assertRaises(ValueError): result = await client.get_custom_model("") @GlobalFormRecognizerAccountPreparer() - async def test_get_model_none_model_id(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_get_model_none_model_id(self, client): with self.assertRaises(ValueError): result = await client.get_custom_model(None) @@ -68,27 +68,27 @@ async def test_delete_model_auth_bad_key(self, resource_group, location, form_re result = await client.delete_model("xx") @GlobalFormRecognizerAccountPreparer() - async def test_delete_model_none_model_id(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_delete_model_none_model_id(self, client): with self.assertRaises(ValueError): result = await client.delete_model(None) @GlobalFormRecognizerAccountPreparer() - async def test_delete_model_empty_model_id(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_delete_model_empty_model_id(self, client): with self.assertRaises(ValueError): result = await client.delete_model("") @GlobalFormRecognizerAccountPreparer() - async def test_account_properties(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_account_properties(self, client): properties = await client.get_account_properties() self.assertIsNotNone(properties.custom_model_limit) self.assertIsNotNone(properties.custom_model_count) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) async def test_mgmt_model_labeled(self, client, container_sas_url): poller = await client.begin_training(container_sas_url, use_training_labels=True) @@ -123,7 +123,7 @@ async def test_mgmt_model_labeled(self, client, container_sas_url): await client.get_custom_model(labeled_model_from_train.model_id) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) async def test_mgmt_model_unlabeled(self, client, container_sas_url): poller = await client.begin_training(container_sas_url, use_training_labels=False) unlabeled_model_from_train = await poller.result() diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt.py index 4e5c0d2f6579..8f3f56f1d6b5 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt.py @@ -5,6 +5,7 @@ # ------------------------------------ import pytest +import functools from io import BytesIO from datetime import date, time from azure.core.exceptions import ClientAuthenticationError, ServiceRequestError, HttpResponseError @@ -13,6 +14,10 @@ from azure.ai.formrecognizer._response_handlers import prepare_receipt from azure.ai.formrecognizer import FormRecognizerClient, FormContentType from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer +from testcase import GlobalClientPreparer as _GlobalClientPreparer + + +GlobalClientPreparer = functools.partial(_GlobalClientPreparer, FormRecognizerClient) class TestReceiptFromStream(FormRecognizerTest): @@ -26,8 +31,8 @@ def test_receipt_bad_endpoint(self, resource_group, location, form_recognizer_ac poller = client.begin_recognize_receipts(myfile) @GlobalFormRecognizerAccountPreparer() - def test_authentication_successful_key(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_authentication_successful_key(self, client): with open(self.receipt_jpg, "rb") as fd: myfile = fd.read() poller = client.begin_recognize_receipts(myfile) @@ -40,8 +45,8 @@ def test_authentication_bad_key(self, resource_group, location, form_recognizer_ poller = client.begin_recognize_receipts(b"xx", content_type="image/jpeg") @GlobalFormRecognizerAccountPreparer() - def test_passing_enum_content_type(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_passing_enum_content_type(self, client): with open(self.receipt_png, "rb") as fd: myfile = fd.read() poller = client.begin_recognize_receipts( @@ -52,56 +57,56 @@ def test_passing_enum_content_type(self, resource_group, location, form_recogniz self.assertIsNotNone(result) @GlobalFormRecognizerAccountPreparer() - def test_damaged_file_passed_as_bytes(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_damaged_file_passed_as_bytes(self, client): damaged_pdf = b"\x25\x50\x44\x46\x55\x55\x55" # still has correct bytes to be recognized as PDF with self.assertRaises(HttpResponseError): poller = client.begin_recognize_receipts( - damaged_pdf, + damaged_pdf ) @GlobalFormRecognizerAccountPreparer() - def test_damaged_file_bytes_fails_autodetect_content_type(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_damaged_file_bytes_fails_autodetect_content_type(self, client): damaged_pdf = b"\x50\x44\x46\x55\x55\x55" # doesn't match any magic file numbers with self.assertRaises(ValueError): poller = client.begin_recognize_receipts( - damaged_pdf, + damaged_pdf ) @GlobalFormRecognizerAccountPreparer() - def test_damaged_file_passed_as_bytes_io(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_damaged_file_passed_as_bytes_io(self, client): damaged_pdf = BytesIO(b"\x25\x50\x44\x46\x55\x55\x55") # still has correct bytes to be recognized as PDF with self.assertRaises(HttpResponseError): poller = client.begin_recognize_receipts( - damaged_pdf, + damaged_pdf ) @GlobalFormRecognizerAccountPreparer() - def test_damaged_file_bytes_io_fails_autodetect(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_damaged_file_bytes_io_fails_autodetect(self, client): damaged_pdf = BytesIO(b"\x50\x44\x46\x55\x55\x55") # doesn't match any magic file numbers with self.assertRaises(ValueError): poller = client.begin_recognize_receipts( - damaged_pdf, + damaged_pdf ) @GlobalFormRecognizerAccountPreparer() - def test_blank_page(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_blank_page(self, client): with open(self.blank_pdf, "rb") as fd: blank = fd.read() poller = client.begin_recognize_receipts( - blank, + blank ) result = poller.result() self.assertIsNotNone(result) @GlobalFormRecognizerAccountPreparer() - def test_passing_bad_content_type_param_passed(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_passing_bad_content_type_param_passed(self, client): with open(self.receipt_jpg, "rb") as fd: myfile = fd.read() with self.assertRaises(ValueError): @@ -111,28 +116,26 @@ def test_passing_bad_content_type_param_passed(self, resource_group, location, f ) @GlobalFormRecognizerAccountPreparer() - def test_passing_unsupported_url_content_type(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + def test_passing_unsupported_url_content_type(self, client): with self.assertRaises(TypeError): poller = client.begin_recognize_receipts("https://badurl.jpg", content_type="application/json") @GlobalFormRecognizerAccountPreparer() - def test_auto_detect_unsupported_stream_content(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_auto_detect_unsupported_stream_content(self, client): with open(self.unsupported_content_py, "rb") as fd: myfile = fd.read() with self.assertRaises(ValueError): poller = client.begin_recognize_receipts( - myfile, + myfile ) @GlobalFormRecognizerAccountPreparer() - def test_receipt_stream_transform_png(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + def test_receipt_stream_transform_png(self, client): responses = [] def callback(raw_response, _, headers): @@ -185,9 +188,8 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(receipt.pages, read_results) @GlobalFormRecognizerAccountPreparer() - def test_receipt_stream_transform_jpg(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + def test_receipt_stream_transform_jpg(self, client): responses = [] def callback(raw_response, _, headers): @@ -241,8 +243,8 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(receipt.pages, read_results) @GlobalFormRecognizerAccountPreparer() - def test_receipt_jpg(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_receipt_jpg(self, client): with open(self.receipt_jpg, "rb") as fd: receipt = fd.read() @@ -270,8 +272,8 @@ def test_receipt_jpg(self, resource_group, location, form_recognizer_account, fo self.assertReceiptItemsHasValues(receipt.fields['Items'].value, receipt.page_range.first_page_number, False) @GlobalFormRecognizerAccountPreparer() - def test_receipt_png(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_receipt_png(self, client): with open(self.receipt_png, "rb") as stream: poller = client.begin_recognize_receipts(stream) @@ -294,8 +296,8 @@ def test_receipt_png(self, resource_group, location, form_recognizer_account, fo self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() - def test_receipt_jpg_include_text_content(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_receipt_jpg_include_text_content(self, client): with open(self.receipt_jpg, "rb") as fd: receipt = fd.read() poller = client.begin_recognize_receipts(receipt, include_text_content=True) @@ -314,8 +316,9 @@ def test_receipt_jpg_include_text_content(self, resource_group, location, form_r self.assertTextContentHasValues(value.value_data.text_content, receipt.page_range.first_page_number) @GlobalFormRecognizerAccountPreparer() - def test_receipt_multipage(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_receipt_multipage(self, client): + with open(self.multipage_invoice_pdf, "rb") as fd: receipt = fd.read() poller = client.begin_recognize_receipts(receipt, include_text_content=True) @@ -348,8 +351,8 @@ def test_receipt_multipage(self, resource_group, location, form_recognizer_accou self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() - def test_receipt_multipage_transform(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_receipt_multipage_transform(self, client): responses = [] @@ -408,9 +411,9 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(returned_model, read_results) @GlobalFormRecognizerAccountPreparer() + @GlobalClientPreparer() @pytest.mark.live_test_only - def test_receipt_continuation_token(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + def test_receipt_continuation_token(self, client): with open(self.receipt_jpg, "rb") as fd: receipt = fd.read() diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_async.py index b30744a32ab2..07c9c099b473 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_async.py @@ -5,6 +5,7 @@ # ------------------------------------ import pytest +import functools from io import BytesIO from datetime import date, time from azure.core.exceptions import ServiceRequestError, ClientAuthenticationError, HttpResponseError @@ -15,6 +16,10 @@ from azure.ai.formrecognizer import FormContentType from testcase import GlobalFormRecognizerAccountPreparer from asynctestcase import AsyncFormRecognizerTest +from testcase import GlobalClientPreparer as _GlobalClientPreparer + + +GlobalClientPreparer = functools.partial(_GlobalClientPreparer, FormRecognizerClient) class TestReceiptFromStreamAsync(AsyncFormRecognizerTest): @@ -29,8 +34,8 @@ async def test_receipt_bad_endpoint(self, resource_group, location, form_recogni result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_authentication_successful_key(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_authentication_successful_key(self, client): with open(self.receipt_jpg, "rb") as fd: myfile = fd.read() poller = await client.begin_recognize_receipts(myfile) @@ -44,8 +49,8 @@ async def test_authentication_bad_key(self, resource_group, location, form_recog result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_passing_enum_content_type(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_passing_enum_content_type(self, client): with open(self.receipt_png, "rb") as fd: myfile = fd.read() poller = await client.begin_recognize_receipts( @@ -56,8 +61,8 @@ async def test_passing_enum_content_type(self, resource_group, location, form_re self.assertIsNotNone(result) @GlobalFormRecognizerAccountPreparer() - async def test_damaged_file_passed_as_bytes(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_damaged_file_passed_as_bytes(self, client): damaged_pdf = b"\x25\x50\x44\x46\x55\x55\x55" # still has correct bytes to be recognized as PDF with self.assertRaises(HttpResponseError): poller = await client.begin_recognize_receipts( @@ -66,8 +71,8 @@ async def test_damaged_file_passed_as_bytes(self, resource_group, location, form result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_damaged_file_bytes_fails_autodetect_content_type(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_damaged_file_bytes_fails_autodetect_content_type(self, client): damaged_pdf = b"\x50\x44\x46\x55\x55\x55" # doesn't match any magic file numbers with self.assertRaises(ValueError): poller = await client.begin_recognize_receipts( @@ -76,8 +81,8 @@ async def test_damaged_file_bytes_fails_autodetect_content_type(self, resource_g result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_damaged_file_passed_as_bytes_io(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_damaged_file_passed_as_bytes_io(self, client): damaged_pdf = BytesIO(b"\x25\x50\x44\x46\x55\x55\x55") # still has correct bytes to be recognized as PDF with self.assertRaises(HttpResponseError): poller = await client.begin_recognize_receipts( @@ -96,8 +101,8 @@ async def test_damaged_file_bytes_io_fails_autodetect(self, resource_group, loca result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_blank_page(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_blank_page(self, client): with open(self.blank_pdf, "rb") as fd: blank = fd.read() @@ -108,8 +113,8 @@ async def test_blank_page(self, resource_group, location, form_recognizer_accoun self.assertIsNotNone(result) @GlobalFormRecognizerAccountPreparer() - async def test_passing_bad_content_type_param_passed(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_passing_bad_content_type_param_passed(self, client): with open(self.receipt_jpg, "rb") as fd: myfile = fd.read() with self.assertRaises(ValueError): @@ -120,17 +125,15 @@ async def test_passing_bad_content_type_param_passed(self, resource_group, locat result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_passing_unsupported_url_content_type(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + async def test_passing_unsupported_url_content_type(self, client): with self.assertRaises(TypeError): poller = await client.begin_recognize_receipts("https://badurl.jpg", content_type="application/json") result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_auto_detect_unsupported_stream_content(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + async def test_auto_detect_unsupported_stream_content(self, client): with open(self.unsupported_content_py, "rb") as fd: myfile = fd.read() @@ -141,8 +144,8 @@ async def test_auto_detect_unsupported_stream_content(self, resource_group, loca result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_receipt_stream_transform_png(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_receipt_stream_transform_png(self, client): responses = [] @@ -196,9 +199,8 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(receipt.pages, read_results) @GlobalFormRecognizerAccountPreparer() - async def test_receipt_stream_transform_jpg(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + async def test_receipt_stream_transform_jpg(self, client): responses = [] def callback(raw_response, _, headers): @@ -252,8 +254,8 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(receipt.pages, read_results) @GlobalFormRecognizerAccountPreparer() - async def test_receipt_jpg(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_receipt_jpg(self, client): with open(self.receipt_jpg, "rb") as fd: receipt = fd.read() @@ -281,9 +283,8 @@ async def test_receipt_jpg(self, resource_group, location, form_recognizer_accou self.assertReceiptItemsHasValues(receipt.fields["Items"].value, receipt.page_range.first_page_number, False) @GlobalFormRecognizerAccountPreparer() - async def test_receipt_png(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + async def test_receipt_png(self, client): with open(self.receipt_png, "rb") as fd: receipt = fd.read() @@ -306,8 +307,8 @@ async def test_receipt_png(self, resource_group, location, form_recognizer_accou self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() - async def test_receipt_jpg_include_text_content(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_receipt_jpg_include_text_content(self, client): with open(self.receipt_jpg, "rb") as fd: receipt = fd.read() poller = await client.begin_recognize_receipts(receipt, include_text_content=True) @@ -326,8 +327,8 @@ async def test_receipt_jpg_include_text_content(self, resource_group, location, self.assertTextContentHasValues(value.value_data.text_content, receipt.page_range.first_page_number) @GlobalFormRecognizerAccountPreparer() - async def test_receipt_multipage(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_receipt_multipage(self, client): with open(self.multipage_invoice_pdf, "rb") as fd: receipt = fd.read() poller = await client.begin_recognize_receipts(receipt, include_text_content=True) @@ -360,9 +361,8 @@ async def test_receipt_multipage(self, resource_group, location, form_recognizer self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() - async def test_receipt_multipage_transform(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + async def test_receipt_multipage_transform(self, client): responses = [] def callback(raw_response, _, headers): @@ -420,9 +420,9 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(returned_model, read_results) @GlobalFormRecognizerAccountPreparer() + @GlobalClientPreparer() @pytest.mark.live_test_only - async def test_receipt_continuation_token(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + async def test_receipt_continuation_token(self, client): with open(self.receipt_jpg, "rb") as fd: receipt = fd.read() diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url.py index 31c4924fc5ad..c3f6945d9190 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url.py @@ -5,6 +5,7 @@ # ------------------------------------ import pytest +import functools from datetime import date, time from azure.core.exceptions import HttpResponseError, ServiceRequestError, ClientAuthenticationError from azure.core.credentials import AzureKeyCredential @@ -12,10 +13,26 @@ from azure.ai.formrecognizer._response_handlers import prepare_receipt from azure.ai.formrecognizer import FormRecognizerClient from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer +from testcase import GlobalClientPreparer as _GlobalClientPreparer + + +GlobalClientPreparer = functools.partial(_GlobalClientPreparer, FormRecognizerClient) class TestReceiptFromUrl(FormRecognizerTest): + @GlobalFormRecognizerAccountPreparer() + def test_polling_interval(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): + client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key), polling_interval=7) + self.assertEqual(client._client._config.polling_interval, 7) + + poller = client.begin_recognize_receipts_from_url(self.receipt_url_jpg, polling_interval=6) + poller.wait() + self.assertEqual(poller._polling_method._timeout, 6) + poller2 = client.begin_recognize_receipts_from_url(self.receipt_url_jpg) + poller2.wait() + self.assertEqual(poller2._polling_method._timeout, 7) # goes back to client default + @pytest.mark.live_test_only def test_active_directory_auth(self): token = self.generate_oauth_token() @@ -32,8 +49,8 @@ def test_receipt_url_bad_endpoint(self, resource_group, location, form_recognize poller = client.begin_recognize_receipts_from_url(self.receipt_url_jpg) @GlobalFormRecognizerAccountPreparer() - def test_receipt_url_auth_successful_key(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_receipt_url_auth_successful_key(self, client): poller = client.begin_recognize_receipts_from_url(self.receipt_url_jpg) result = poller.result() @@ -44,23 +61,21 @@ def test_receipt_url_auth_bad_key(self, resource_group, location, form_recognize poller = client.begin_recognize_receipts_from_url(self.receipt_url_jpg) @GlobalFormRecognizerAccountPreparer() - def test_receipt_bad_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + def test_receipt_bad_url(self, client): with self.assertRaises(HttpResponseError): poller = client.begin_recognize_receipts_from_url("https://badurl.jpg") @GlobalFormRecognizerAccountPreparer() - def test_receipt_url_pass_stream(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_receipt_url_pass_stream(self, client): with open(self.receipt_png, "rb") as receipt: with self.assertRaises(HttpResponseError): poller = client.begin_recognize_receipts_from_url(receipt) @GlobalFormRecognizerAccountPreparer() - def test_receipt_url_transform_jpg(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + def test_receipt_url_transform_jpg(self, client): responses = [] def callback(raw_response, _, headers): @@ -110,8 +125,8 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(receipt.pages, read_results) @GlobalFormRecognizerAccountPreparer() - def test_receipt_url_transform_png(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_receipt_url_transform_png(self, client): responses = [] @@ -162,8 +177,8 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(receipt.pages, read_results) @GlobalFormRecognizerAccountPreparer() - def test_receipt_url_include_text_content(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_receipt_url_include_text_content(self, client): poller = client.begin_recognize_receipts_from_url( self.receipt_url_jpg, @@ -184,8 +199,8 @@ def test_receipt_url_include_text_content(self, resource_group, location, form_r self.assertTextContentHasValues(value.value_data.text_content, receipt.page_range.first_page_number) @GlobalFormRecognizerAccountPreparer() - def test_receipt_url_jpg(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_receipt_url_jpg(self, client): poller = client.begin_recognize_receipts_from_url(self.receipt_url_jpg) @@ -210,8 +225,8 @@ def test_receipt_url_jpg(self, resource_group, location, form_recognizer_account self.assertReceiptItemsHasValues(receipt.fields["Items"].value, receipt.page_range.first_page_number, False) @GlobalFormRecognizerAccountPreparer() - def test_receipt_url_png(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_receipt_url_png(self, client): poller = client.begin_recognize_receipts_from_url(self.receipt_url_png) @@ -233,8 +248,8 @@ def test_receipt_url_png(self, resource_group, location, form_recognizer_account self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() - def test_receipt_multipage_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_receipt_multipage_url(self, client): poller = client.begin_recognize_receipts_from_url(self.multipage_url_pdf, include_text_content=True) result = poller.result() @@ -266,8 +281,8 @@ def test_receipt_multipage_url(self, resource_group, location, form_recognizer_a self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() - def test_receipt_multipage_transform_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + def test_receipt_multipage_transform_url(self, client): responses = [] @@ -323,9 +338,9 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(returned_model, read_results) @GlobalFormRecognizerAccountPreparer() + @GlobalClientPreparer() @pytest.mark.live_test_only - def test_receipt_continuation_token(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + def test_receipt_continuation_token(self, client): initial_poller = client.begin_recognize_receipts_from_url(self.receipt_url_jpg) cont_token = initial_poller.continuation_token() diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url_async.py index 21558906aed4..6770833364f7 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url_async.py @@ -5,6 +5,7 @@ # ------------------------------------ import pytest +import functools from datetime import date, time from azure.core.exceptions import HttpResponseError, ServiceRequestError, ClientAuthenticationError from azure.core.credentials import AzureKeyCredential @@ -13,10 +14,26 @@ from azure.ai.formrecognizer.aio import FormRecognizerClient from testcase import GlobalFormRecognizerAccountPreparer from asynctestcase import AsyncFormRecognizerTest +from testcase import GlobalClientPreparer as _GlobalClientPreparer + + +GlobalClientPreparer = functools.partial(_GlobalClientPreparer, FormRecognizerClient) class TestReceiptFromUrlAsync(AsyncFormRecognizerTest): + @GlobalFormRecognizerAccountPreparer() + async def test_polling_interval(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): + client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key), polling_interval=7) + self.assertEqual(client._client._config.polling_interval, 7) + + poller = await client.begin_recognize_receipts_from_url(self.receipt_url_jpg, polling_interval=6) + await poller.wait() + self.assertEqual(poller._polling_method._timeout, 6) + poller2 = await client.begin_recognize_receipts_from_url(self.receipt_url_jpg) + await poller2.wait() + self.assertEqual(poller2._polling_method._timeout, 7) # goes back to client default + @pytest.mark.live_test_only @GlobalFormRecognizerAccountPreparer() async def test_active_directory_auth_async(self): @@ -39,8 +56,8 @@ async def test_receipt_url_bad_endpoint(self, resource_group, location, form_rec result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_receipt_url_auth_successful_key(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_receipt_url_auth_successful_key(self, client): poller = await client.begin_recognize_receipts_from_url( self.receipt_url_jpg ) @@ -56,16 +73,16 @@ async def test_receipt_url_auth_bad_key(self, resource_group, location, form_rec result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_receipt_bad_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + async def test_receipt_bad_url(self, client): with self.assertRaises(HttpResponseError): poller = await client.begin_recognize_receipts_from_url("https://badurl.jpg") result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_receipt_url_pass_stream(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_receipt_url_pass_stream(self, client): + with open(self.receipt_png, "rb") as fd: receipt = fd.read(4) # makes the recording smaller @@ -74,8 +91,8 @@ async def test_receipt_url_pass_stream(self, resource_group, location, form_reco result = await poller.result() @GlobalFormRecognizerAccountPreparer() - async def test_receipt_url_transform_jpg(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_receipt_url_transform_jpg(self, client): responses = [] @@ -126,9 +143,8 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(receipt.pages, read_results) @GlobalFormRecognizerAccountPreparer() - async def test_receipt_url_transform_png(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) - + @GlobalClientPreparer() + async def test_receipt_url_transform_png(self, client): responses = [] def callback(raw_response, _, headers): @@ -178,8 +194,8 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(receipt.pages, read_results) @GlobalFormRecognizerAccountPreparer() - async def test_receipt_url_include_text_content(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_receipt_url_include_text_content(self, client): poller = await client.begin_recognize_receipts_from_url( self.receipt_url_jpg, @@ -200,8 +216,8 @@ async def test_receipt_url_include_text_content(self, resource_group, location, self.assertTextContentHasValues(value.value_data.text_content, receipt.page_range.first_page_number) @GlobalFormRecognizerAccountPreparer() - async def test_receipt_url_jpg(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_receipt_url_jpg(self, client): poller = await client.begin_recognize_receipts_from_url( self.receipt_url_jpg @@ -228,8 +244,8 @@ async def test_receipt_url_jpg(self, resource_group, location, form_recognizer_a self.assertReceiptItemsHasValues(receipt.fields["Items"].value, receipt.page_range.first_page_number, False) @GlobalFormRecognizerAccountPreparer() - async def test_receipt_url_png(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_receipt_url_png(self, client): poller = await client.begin_recognize_receipts_from_url(self.receipt_url_png) result = await poller.result() @@ -251,8 +267,8 @@ async def test_receipt_url_png(self, resource_group, location, form_recognizer_a self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() - async def test_receipt_multipage_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_receipt_multipage_url(self, client): poller = await client.begin_recognize_receipts_from_url(self.multipage_url_pdf, include_text_content=True) result = await poller.result() @@ -284,8 +300,8 @@ async def test_receipt_multipage_url(self, resource_group, location, form_recogn self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() - async def test_receipt_multipage_transform_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + @GlobalClientPreparer() + async def test_receipt_multipage_transform_url(self, client): responses = [] @@ -341,9 +357,9 @@ def callback(raw_response, _, headers): self.assertFormPagesTransformCorrect(returned_model, read_results) @GlobalFormRecognizerAccountPreparer() + @GlobalClientPreparer() @pytest.mark.live_test_only - async def test_receipt_continuation_token(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) + async def test_receipt_continuation_token(self, client): initial_poller = await client.begin_recognize_receipts_from_url(self.receipt_url_jpg) cont_token = initial_poller.continuation_token() diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_training.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_training.py index 92f73759df85..5810557853da 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_training.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_training.py @@ -12,14 +12,30 @@ from azure.ai.formrecognizer._models import CustomFormModel from azure.ai.formrecognizer import FormTrainingClient from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer -from testcase import GlobalTrainingAccountPreparer as _GlobalTrainingAccountPreparer +from testcase import GlobalClientPreparer as _GlobalClientPreparer -GlobalTrainingAccountPreparer = functools.partial(_GlobalTrainingAccountPreparer, FormTrainingClient) +GlobalClientPreparer = functools.partial(_GlobalClientPreparer, FormTrainingClient) class TestTraining(FormRecognizerTest): + @GlobalFormRecognizerAccountPreparer() + @GlobalClientPreparer(training=True) + def test_polling_interval(self, client, container_sas_url): + def check_poll_value(poll): + if self.is_live: + self.assertEqual(poll, 5) + else: + self.assertEqual(poll, 0) + check_poll_value(client._client._config.polling_interval) + poller = client.begin_training(training_files_url=container_sas_url, use_training_labels=False, polling_interval=6) + poller.wait() + self.assertEqual(poller._polling_method._timeout, 6) + poller2 = client.begin_training(training_files_url=container_sas_url, use_training_labels=False) + poller2.wait() + check_poll_value(poller2._polling_method._timeout) # goes back to client default + @GlobalFormRecognizerAccountPreparer() def test_training_auth_bad_key(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): client = FormTrainingClient(form_recognizer_account, AzureKeyCredential("xxxx")) @@ -27,7 +43,7 @@ def test_training_auth_bad_key(self, resource_group, location, form_recognizer_a poller = client.begin_training("xx", use_training_labels=False) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) def test_training(self, client, container_sas_url): poller = client.begin_training(training_files_url=container_sas_url, use_training_labels=False) @@ -50,7 +66,7 @@ def test_training(self, client, container_sas_url): self.assertIsNotNone(field.name) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True) + @GlobalClientPreparer(training=True, multipage=True) def test_training_multipage(self, client, container_sas_url): poller = client.begin_training(container_sas_url, use_training_labels=False) @@ -73,7 +89,7 @@ def test_training_multipage(self, client, container_sas_url): self.assertIsNotNone(field.name) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) def test_training_transform(self, client, container_sas_url): raw_response = [] @@ -92,7 +108,7 @@ def callback(response): self.assertModelTransformCorrect(custom_model, raw_model, unlabeled=True) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True) + @GlobalClientPreparer(training=True, multipage=True) def test_training_multipage_transform(self, client, container_sas_url): raw_response = [] @@ -111,7 +127,7 @@ def callback(response): self.assertModelTransformCorrect(custom_model, raw_model, unlabeled=True) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) def test_training_with_labels(self, client, container_sas_url): poller = client.begin_training(training_files_url=container_sas_url, use_training_labels=True) @@ -135,7 +151,7 @@ def test_training_with_labels(self, client, container_sas_url): self.assertIsNotNone(field.name) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True) + @GlobalClientPreparer(training=True, multipage=True) def test_training_multipage_with_labels(self, client, container_sas_url): poller = client.begin_training(container_sas_url, use_training_labels=True) @@ -159,7 +175,7 @@ def test_training_multipage_with_labels(self, client, container_sas_url): self.assertIsNotNone(field.name) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) def test_training_with_labels_transform(self, client, container_sas_url): raw_response = [] @@ -178,7 +194,7 @@ def callback(response): self.assertModelTransformCorrect(custom_model, raw_model) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True) + @GlobalClientPreparer(training=True, multipage=True) def test_train_multipage_w_labels_transform(self, client, container_sas_url): raw_response = [] @@ -197,7 +213,7 @@ def callback(response): self.assertModelTransformCorrect(custom_model, raw_model) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) def test_training_with_files_filter(self, client, container_sas_url): poller = client.begin_training(training_files_url=container_sas_url, use_training_labels=False, include_sub_folders=True) @@ -215,7 +231,7 @@ def test_training_with_files_filter(self, client, container_sas_url): model = poller.result() @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) @pytest.mark.live_test_only def test_training_continuation_token(self, client, container_sas_url): diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_training_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_training_async.py index 67eb33d4398f..87ba872fd296 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_training_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_training_async.py @@ -12,14 +12,31 @@ from azure.ai.formrecognizer._models import CustomFormModel from azure.ai.formrecognizer.aio import FormTrainingClient from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer -from testcase import GlobalTrainingAccountPreparer as _GlobalTrainingAccountPreparer from asynctestcase import AsyncFormRecognizerTest +from testcase import GlobalClientPreparer as _GlobalClientPreparer -GlobalTrainingAccountPreparer = functools.partial(_GlobalTrainingAccountPreparer, FormTrainingClient) + +GlobalClientPreparer = functools.partial(_GlobalClientPreparer, FormTrainingClient) class TestTrainingAsync(AsyncFormRecognizerTest): + @GlobalFormRecognizerAccountPreparer() + @GlobalClientPreparer(training=True) + async def test_polling_interval(self, client, container_sas_url): + def check_poll_value(poll): + if self.is_live: + self.assertEqual(poll, 5) + else: + self.assertEqual(poll, 0) + check_poll_value(client._client._config.polling_interval) + poller = await client.begin_training(training_files_url=container_sas_url, use_training_labels=False, polling_interval=6) + await poller.wait() + self.assertEqual(poller._polling_method._timeout, 6) + poller2 = await client.begin_training(training_files_url=container_sas_url, use_training_labels=False) + await poller2.wait() + check_poll_value(poller2._polling_method._timeout) # goes back to client default + @GlobalFormRecognizerAccountPreparer() async def test_training_auth_bad_key(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): client = FormTrainingClient(form_recognizer_account, AzureKeyCredential("xxxx")) @@ -28,7 +45,7 @@ async def test_training_auth_bad_key(self, resource_group, location, form_recogn result = await poller.result() @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) async def test_training(self, client, container_sas_url): poller = await client.begin_training( @@ -53,7 +70,7 @@ async def test_training(self, client, container_sas_url): self.assertIsNotNone(field.name) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True) + @GlobalClientPreparer(training=True, multipage=True) async def test_training_multipage(self, client, container_sas_url): poller = await client.begin_training(container_sas_url, use_training_labels=False) @@ -76,7 +93,7 @@ async def test_training_multipage(self, client, container_sas_url): self.assertIsNotNone(field.name) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) async def test_training_transform(self, client, container_sas_url): raw_response = [] @@ -98,7 +115,7 @@ def callback(response): self.assertModelTransformCorrect(custom_model, raw_model, unlabeled=True) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True) + @GlobalClientPreparer(training=True, multipage=True) async def test_training_multipage_transform(self, client, container_sas_url): raw_response = [] @@ -117,7 +134,7 @@ def callback(response): self.assertModelTransformCorrect(custom_model, raw_model, unlabeled=True) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) async def test_training_with_labels(self, client, container_sas_url): poller = await client.begin_training(training_files_url=container_sas_url, use_training_labels=True) @@ -140,7 +157,7 @@ async def test_training_with_labels(self, client, container_sas_url): self.assertIsNotNone(field.name) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True) + @GlobalClientPreparer(training=True, multipage=True) async def test_training_multipage_with_labels(self, client, container_sas_url): poller = await client.begin_training(container_sas_url, use_training_labels=True) @@ -164,7 +181,7 @@ async def test_training_multipage_with_labels(self, client, container_sas_url): self.assertIsNotNone(field.name) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) async def test_training_with_labels_transform(self, client, container_sas_url): raw_response = [] @@ -183,7 +200,7 @@ def callback(response): self.assertModelTransformCorrect(custom_model, raw_model) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer(multipage=True) + @GlobalClientPreparer(training=True, multipage=True) async def test_train_multipage_w_lbls_trnsfrm(self, client, container_sas_url): raw_response = [] @@ -202,7 +219,7 @@ def callback(response): self.assertModelTransformCorrect(custom_model, raw_model) @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) async def test_training_with_files_filter(self, client, container_sas_url): poller = await client.begin_training(training_files_url=container_sas_url, use_training_labels=False, include_sub_folders=True) @@ -220,7 +237,7 @@ async def test_training_with_files_filter(self, client, container_sas_url): model = await poller.result() @GlobalFormRecognizerAccountPreparer() - @GlobalTrainingAccountPreparer() + @GlobalClientPreparer(training=True) @pytest.mark.live_test_only async def test_training_continuation_token(self, client, container_sas_url): diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/testcase.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/testcase.py index c67bb0592dcf..c5bc014973fb 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/testcase.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/testcase.py @@ -32,6 +32,7 @@ class AccessTokenReplacer(RecordingProcessor): """Replace the access token in a request/response body.""" def __init__(self, replacement='redacted'): + self._replacement = replacement def process_request(self, request): @@ -387,7 +388,7 @@ def create_resource(self, name, **kwargs): ) return { - 'location': 'centraluseuap', + 'location': REGION, 'resource_group': rg, } @@ -402,21 +403,22 @@ def __init__(self): def create_resource(self, name, **kwargs): form_recognizer_account = FormRecognizerTest._FORM_RECOGNIZER_ACCOUNT return { - 'location': 'centraluseuap', + 'location': REGION, 'resource_group': FormRecognizerTest._RESOURCE_GROUP, 'form_recognizer_account': form_recognizer_account, 'form_recognizer_account_key': FormRecognizerTest._FORM_RECOGNIZER_KEY } -class GlobalTrainingAccountPreparer(AzureMgmtPreparer): +class GlobalClientPreparer(AzureMgmtPreparer): def __init__(self, client_cls, client_kwargs={}, **kwargs): - super(GlobalTrainingAccountPreparer, self).__init__( + super(GlobalClientPreparer, self).__init__( name_prefix='', random_name_length=42 ) self.client_kwargs = client_kwargs self.client_cls = client_cls + self.training = kwargs.get("training", False) self.multipage_test = kwargs.get("multipage", False) self.multipage_test_2 = kwargs.get("multipage2", False) self.need_blob_sas_url = kwargs.get("blob_sas_url", False) @@ -446,50 +448,7 @@ def get_settings_value(self, key): raise return key_value - def create_resource(self, name, **kwargs): - client, container_sas_url, blob_sas_url = self.create_form_client_and_container_sas_url(**kwargs) - - if self.need_blob_sas_url: - return {"client": client, - "container_sas_url": container_sas_url, - "blob_sas_url": blob_sas_url} - if self.copy: - if self.is_live: - resource_group = kwargs.get("resource_group") - subscription_id = self.get_settings_value("SUBSCRIPTION_ID") - form_recognizer_name = FormRecognizerTest._FORM_RECOGNIZER_NAME - - resource_id = "/subscriptions/" + subscription_id + "/resourceGroups/" + resource_group.name + \ - "/providers/Microsoft.CognitiveServices/accounts/" + form_recognizer_name - resource_location = REGION - self.test_class_instance.scrubber.register_name_pair( - resource_id, - "resource_id" - ) - else: - resource_location = REGION - resource_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rgname/providers/Microsoft.CognitiveServices/accounts/frname" - - return { - "client": client, - "container_sas_url": container_sas_url, - "location": resource_location, - "resource_id": resource_id - } - - else: - return {"client": client, - "container_sas_url": container_sas_url} - - def create_form_client_and_container_sas_url(self, **kwargs): - form_recognizer_account = self.client_kwargs.pop("form_recognizer_account", None) - if form_recognizer_account is None: - form_recognizer_account = kwargs.pop("form_recognizer_account") - - form_recognizer_account_key = self.client_kwargs.pop("form_recognizer_account_key", None) - if form_recognizer_account_key is None: - form_recognizer_account_key = kwargs.pop("form_recognizer_account_key") - + def get_training_parameters(self, client): if self.is_live: if self.multipage_test: container_sas_url = self.get_settings_value("FORM_RECOGNIZER_MULTIPAGE_STORAGE_CONTAINER_SAS_URL") @@ -521,11 +480,71 @@ def create_form_client_and_container_sas_url(self, **kwargs): container_sas_url = "containersasurl" blob_sas_url = "blob_sas_url" + if self.need_blob_sas_url: + return {"client": client, + "container_sas_url": container_sas_url, + "blob_sas_url": blob_sas_url} + else: + return {"client": client, + "container_sas_url": container_sas_url} + + def get_copy_parameters(self, training_params, client, **kwargs): + if self.is_live: + resource_group = kwargs.get("resource_group") + subscription_id = self.get_settings_value("SUBSCRIPTION_ID") + form_recognizer_name = FormRecognizerTest._FORM_RECOGNIZER_NAME + + resource_id = "/subscriptions/" + subscription_id + "/resourceGroups/" + resource_group.name + \ + "/providers/Microsoft.CognitiveServices/accounts/" + form_recognizer_name + resource_location = REGION + self.test_class_instance.scrubber.register_name_pair( + resource_id, + "resource_id" + ) + else: + resource_location = REGION + resource_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rgname/providers/Microsoft.CognitiveServices/accounts/frname" + + return { + "client": client, + "container_sas_url": training_params["container_sas_url"], + "location": resource_location, + "resource_id": resource_id + } + + def create_resource(self, name, **kwargs): + client = self.create_form_client(**kwargs) + + if not self.training: + return {"client": client} + + training_params = self.get_training_parameters(client) + + if self.copy: + return self.get_copy_parameters(training_params, client, **kwargs) + + return training_params + + def create_form_client(self, **kwargs): + form_recognizer_account = self.client_kwargs.pop("form_recognizer_account", None) + if form_recognizer_account is None: + form_recognizer_account = kwargs.pop("form_recognizer_account") + + form_recognizer_account_key = self.client_kwargs.pop("form_recognizer_account_key", None) + if form_recognizer_account_key is None: + form_recognizer_account_key = kwargs.pop("form_recognizer_account_key") + + if self.is_live: + polling_interval = 5 + else: + polling_interval = 0 + return self.client_cls( form_recognizer_account, AzureKeyCredential(form_recognizer_account_key), + polling_interval=polling_interval, **self.client_kwargs - ), container_sas_url, blob_sas_url + ) @pytest.fixture(scope="session")