Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[formrecognizer] use ARM template for tests #16432

Merged
merged 13 commits into from
Feb 4, 2021
9 changes: 0 additions & 9 deletions sdk/formrecognizer/azure-ai-formrecognizer/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,7 @@

import sys

# fixture needs to be visible from conftest
from testcase import form_recognizer_account

# Ignore async tests for Python < 3.5
collect_ignore_glob = []
if sys.version_info < (3, 5):
collect_ignore_glob.append("*_async.py")

def pytest_configure(config):
# register an additional marker
config.addinivalue_line(
"usefixtures", "form_recognizer_account"
)
60 changes: 60 additions & 0 deletions sdk/formrecognizer/azure-ai-formrecognizer/tests/preparers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

# coding: utf-8
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

import os
import functools
from devtools_testutils import PowerShellPreparer, AzureMgmtPreparer
from azure.core.credentials import AzureKeyCredential

ENABLE_LOGGER = os.getenv('ENABLE_LOGGER', "False")
REGION = os.getenv('REGION', None)


FormRecognizerPreparer = functools.partial(
PowerShellPreparer,
'formrecognizer',
formrecognizer_test_endpoint="https://region.api.cognitive.microsoft.com/",
formrecognizer_test_api_key="fakeZmFrZV9hY29jdW50X2tleQ==",
formrecognizer_storage_container_sas_url="container_sas_url",
formrecognizer_testing_data_container_sas_url="container_sas_url",
formrecognizer_multipage_storage_container_sas_url="container_sas_url",
formrecognizer_multipage_storage_container_sas_url_2="container_sas_url",
formrecognizer_selection_mark_storage_container_sas_url="container_sas_url",
formrecognizer_resource_id="/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rgname/providers/Microsoft.CognitiveServices/accounts/frname",
formrecognizer_region="region"
)


class GlobalClientPreparer(AzureMgmtPreparer):
def __init__(self, client_cls, client_kwargs={}, **kwargs):
super(GlobalClientPreparer, self).__init__(
name_prefix='',
random_name_length=42
)
self.client_kwargs = client_kwargs
self.client_cls = client_cls

def create_resource(self, name, **kwargs):
if self.is_live:
form_recognizer_account = os.environ["FORMRECOGNIZER_TEST_ENDPOINT"]
form_recognizer_account_key = os.environ["FORMRECOGNIZER_TEST_API_KEY"]
polling_interval = 5
else:
form_recognizer_account = "https://region.api.cognitive.microsoft.com/"
form_recognizer_account_key = "fakeZmFrZV9hY29jdW50X2tleQ=="
polling_interval = 0

client = self.client_cls(
form_recognizer_account,
AzureKeyCredential(form_recognizer_account_key),
polling_interval=polling_interval,
logging_enable=True if ENABLE_LOGGER == "True" else False,
**self.client_kwargs
)
kwargs.update({"client": client})
return kwargs
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,39 @@
from azure.ai.formrecognizer._generated.models import AnalyzeOperationResult
from azure.ai.formrecognizer._response_handlers import prepare_prebuilt_models
from azure.ai.formrecognizer import FormRecognizerClient, FormContentType, FormRecognizerApiVersion
from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer
from testcase import GlobalClientPreparer as _GlobalClientPreparer
from testcase import FormRecognizerTest
from preparers import GlobalClientPreparer as _GlobalClientPreparer
from preparers import FormRecognizerPreparer


GlobalClientPreparer = functools.partial(_GlobalClientPreparer, FormRecognizerClient)


class TestBusinessCard(FormRecognizerTest):

@GlobalFormRecognizerAccountPreparer()
def test_business_card_bad_endpoint(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
@FormRecognizerPreparer()
def test_business_card_bad_endpoint(self, formrecognizer_test_endpoint, formrecognizer_test_api_key):
with open(self.business_card_jpg, "rb") as fd:
myfile = fd.read()
with self.assertRaises(ServiceRequestError):
client = FormRecognizerClient("http://notreal.azure.com", AzureKeyCredential(form_recognizer_account_key))
client = FormRecognizerClient("http://notreal.azure.com", AzureKeyCredential(formrecognizer_test_api_key))
poller = client.begin_recognize_business_cards(myfile)

@GlobalFormRecognizerAccountPreparer()
@GlobalClientPreparer()
def test_authentication_successful_key(self, client):
@FormRecognizerPreparer()
def test_authentication_successful_key(self, formrecognizer_test_endpoint, formrecognizer_test_api_key):
client = FormRecognizerClient(formrecognizer_test_endpoint, AzureKeyCredential(formrecognizer_test_api_key))
with open(self.business_card_jpg, "rb") as fd:
myfile = fd.read()
poller = client.begin_recognize_business_cards(myfile)
result = poller.result()

@GlobalFormRecognizerAccountPreparer()
def test_authentication_bad_key(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential("xxxx"))
@FormRecognizerPreparer()
def test_authentication_bad_key(self, formrecognizer_test_endpoint, formrecognizer_test_api_key):
client = FormRecognizerClient(formrecognizer_test_endpoint, AzureKeyCredential("xxxx"))
with self.assertRaises(ClientAuthenticationError):
poller = client.begin_recognize_business_cards(b"xx", content_type="image/jpeg")

@GlobalFormRecognizerAccountPreparer()
@FormRecognizerPreparer()
@GlobalClientPreparer()
def test_passing_enum_content_type(self, client):
with open(self.business_card_png, "rb") as fd:
Expand All @@ -56,7 +57,7 @@ def test_passing_enum_content_type(self, client):
result = poller.result()
self.assertIsNotNone(result)

@GlobalFormRecognizerAccountPreparer()
@FormRecognizerPreparer()
@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
Expand All @@ -65,7 +66,7 @@ def test_damaged_file_passed_as_bytes(self, client):
damaged_pdf
)

@GlobalFormRecognizerAccountPreparer()
@FormRecognizerPreparer()
@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
Expand All @@ -74,7 +75,7 @@ def test_damaged_file_bytes_fails_autodetect_content_type(self, client):
damaged_pdf
)

@GlobalFormRecognizerAccountPreparer()
@FormRecognizerPreparer()
@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
Expand All @@ -83,7 +84,7 @@ def test_damaged_file_passed_as_bytes_io(self, client):
damaged_pdf
)

@GlobalFormRecognizerAccountPreparer()
@FormRecognizerPreparer()
@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
Expand All @@ -92,7 +93,7 @@ def test_damaged_file_bytes_io_fails_autodetect(self, client):
damaged_pdf
)

@GlobalFormRecognizerAccountPreparer()
@FormRecognizerPreparer()
@GlobalClientPreparer()
def test_blank_page(self, client):

Expand All @@ -104,7 +105,7 @@ def test_blank_page(self, client):
result = poller.result()
self.assertIsNotNone(result)

@GlobalFormRecognizerAccountPreparer()
@FormRecognizerPreparer()
@GlobalClientPreparer()
def test_passing_bad_content_type_param_passed(self, client):
with open(self.business_card_jpg, "rb") as fd:
Expand All @@ -115,13 +116,13 @@ def test_passing_bad_content_type_param_passed(self, client):
content_type="application/jpeg"
)

@GlobalFormRecognizerAccountPreparer()
@FormRecognizerPreparer()
@GlobalClientPreparer()
def test_passing_unsupported_url_content_type(self, client):
with self.assertRaises(TypeError):
poller = client.begin_recognize_business_cards("https://badurl.jpg", content_type="application/json")

@GlobalFormRecognizerAccountPreparer()
@FormRecognizerPreparer()
@GlobalClientPreparer()
def test_auto_detect_unsupported_stream_content(self, client):

Expand All @@ -133,7 +134,7 @@ def test_auto_detect_unsupported_stream_content(self, client):
myfile
)

@GlobalFormRecognizerAccountPreparer()
@FormRecognizerPreparer()
@GlobalClientPreparer()
def test_business_card_stream_transform_png(self, client):
responses = []
Expand Down Expand Up @@ -170,7 +171,7 @@ def callback(raw_response, _, headers):
# Check page metadata
self.assertFormPagesTransformCorrect(business_card.pages, read_results)

@GlobalFormRecognizerAccountPreparer()
@FormRecognizerPreparer()
@GlobalClientPreparer()
def test_business_card_stream_transform_jpg(self, client):
responses = []
Expand Down Expand Up @@ -208,7 +209,7 @@ def callback(raw_response, _, headers):
# Check page metadata
self.assertFormPagesTransformCorrect(business_card.pages, read_results)

@GlobalFormRecognizerAccountPreparer()
@FormRecognizerPreparer()
@GlobalClientPreparer()
def test_business_card_stream_multipage_transform_pdf(self, client):
responses = []
Expand Down Expand Up @@ -248,7 +249,7 @@ def callback(raw_response, _, headers):
# Check page metadata
self.assertFormPagesTransformCorrect(returned_model, read_results)

@GlobalFormRecognizerAccountPreparer()
@FormRecognizerPreparer()
@GlobalClientPreparer()
def test_business_card_jpg(self, client):

Expand Down Expand Up @@ -294,7 +295,7 @@ def test_business_card_jpg(self, client):
self.assertEqual(len(business_card.fields.get("CompanyNames").value), 1)
self.assertEqual(business_card.fields.get("CompanyNames").value[0].value, "Contoso")

@GlobalFormRecognizerAccountPreparer()
@FormRecognizerPreparer()
@GlobalClientPreparer()
def test_business_card_png(self, client):

Expand Down Expand Up @@ -338,7 +339,7 @@ def test_business_card_png(self, client):
self.assertEqual(len(business_card.fields.get("CompanyNames").value), 1)
self.assertEqual(business_card.fields.get("CompanyNames").value[0].value, "Contoso")

@GlobalFormRecognizerAccountPreparer()
@FormRecognizerPreparer()
@GlobalClientPreparer()
def test_business_card_multipage_pdf(self, client):

Expand Down Expand Up @@ -401,7 +402,7 @@ def test_business_card_multipage_pdf(self, client):
self.assertEqual(len(business_card.fields.get("CompanyNames").value), 1)
self.assertEqual(business_card.fields.get("CompanyNames").value[0].value, "Contoso")

@GlobalFormRecognizerAccountPreparer()
@FormRecognizerPreparer()
@GlobalClientPreparer()
def test_business_card_jpg_include_field_elements(self, client):
with open(self.business_card_jpg, "rb") as fd:
Expand All @@ -418,7 +419,7 @@ def test_business_card_jpg_include_field_elements(self, client):
for f in field.value:
self.assertFieldElementsHasValues(f.value_data.field_elements, business_card.page_range.first_page_number)

@GlobalFormRecognizerAccountPreparer()
@FormRecognizerPreparer()
@GlobalClientPreparer()
@pytest.mark.live_test_only
def test_business_card_continuation_token(self, client):
Expand All @@ -433,7 +434,7 @@ def test_business_card_continuation_token(self, client):
self.assertIsNotNone(result)
initial_poller.wait() # necessary so azure-devtools doesn't throw assertion error

@GlobalFormRecognizerAccountPreparer()
@FormRecognizerPreparer()
@GlobalClientPreparer(client_kwargs={"api_version": FormRecognizerApiVersion.V2_0})
def test_business_card_v2(self, client):
with open(self.business_card_jpg, "rb") as fd:
Expand All @@ -442,7 +443,7 @@ def test_business_card_v2(self, client):
client.begin_recognize_business_cards(business_card)
assert "Method 'begin_recognize_business_cards' is only available for API version V2_1_PREVIEW and up" in str(e.value)

@GlobalFormRecognizerAccountPreparer()
@FormRecognizerPreparer()
@GlobalClientPreparer()
def test_business_card_locale_specified(self, client):
with open(self.business_card_jpg, "rb") as fd:
Expand All @@ -451,7 +452,7 @@ def test_business_card_locale_specified(self, client):
assert 'en-IN' == poller._polling_method._initial_response.http_response.request.query['locale']
poller.wait()

@GlobalFormRecognizerAccountPreparer()
@FormRecognizerPreparer()
@GlobalClientPreparer()
def test_business_card_locale_error(self, client):
with open(self.business_card_jpg, "rb") as fd:
Expand Down
Loading