Skip to content

Commit 7a575f5

Browse files
author
Rakshith Bhyravabhotla
authored
Test if samples are running (#11727)
* Test if samples are running * changes * Revert "changes" This reverts commit 4846e6e97431ddab57d211e8dadc2c1bc41d2f40. * changes * use test framework * sepearte async * changes * typo * typo * live test only * async to preparer * fix few tests * fix * windows 2.7 * oops * authentication * Update sdk/formrecognizer/azure-ai-formrecognizer/tests/test_samples.py
1 parent 93c1027 commit 7a575f5

9 files changed

+236
-9
lines changed

sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_authentication_async.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
3) AZURE_CLIENT_ID - the client ID of your active directory application.
2929
4) AZURE_TENANT_ID - the tenant ID of your active directory application.
3030
5) AZURE_CLIENT_SECRET - the secret of your active directory application.
31+
6) AZURE_FORM_RECOGNIZER_AAD_ENDPOINT - the endpoint to your Form Recognizer resource for using AAD.
3132
"""
3233

3334
import os
@@ -59,7 +60,7 @@ async def authentication_with_azure_active_directory_form_recognizer_client_asyn
5960
from azure.ai.formrecognizer.aio import FormRecognizerClient
6061
from azure.identity.aio import DefaultAzureCredential
6162

62-
endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
63+
endpoint = os.environ["AZURE_FORM_RECOGNIZER_AAD_ENDPOINT"]
6364
credential = DefaultAzureCredential()
6465

6566
form_recognizer_client = FormRecognizerClient(endpoint, credential)
@@ -88,7 +89,7 @@ async def authentication_with_azure_active_directory_form_training_client_async(
8889
from azure.ai.formrecognizer.aio import FormTrainingClient
8990
from azure.identity.aio import DefaultAzureCredential
9091

91-
endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
92+
endpoint = os.environ["AZURE_FORM_RECOGNIZER_AAD_ENDPOINT"]
9293
credential = DefaultAzureCredential()
9394

9495
form_training_client = FormTrainingClient(endpoint, credential)

sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_authentication.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
3) AZURE_CLIENT_ID - the client ID of your active directory application.
2929
4) AZURE_TENANT_ID - the tenant ID of your active directory application.
3030
5) AZURE_CLIENT_SECRET - the secret of your active directory application.
31+
6) AZURE_FORM_RECOGNIZER_AAD_ENDPOINT - the endpoint to your Form Recognizer resource for using AAD.
3132
"""
3233

3334
import os
@@ -57,7 +58,7 @@ def authentication_with_azure_active_directory_form_recognizer_client(self):
5758
from azure.ai.formrecognizer import FormRecognizerClient
5859
from azure.identity import DefaultAzureCredential
5960

60-
endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
61+
endpoint = os.environ["AZURE_FORM_RECOGNIZER_AAD_ENDPOINT"]
6162
credential = DefaultAzureCredential()
6263

6364
form_recognizer_client = FormRecognizerClient(endpoint, credential)
@@ -84,7 +85,7 @@ def authentication_with_azure_active_directory_form_training_client(self):
8485
from azure.ai.formrecognizer import FormTrainingClient
8586
from azure.identity import DefaultAzureCredential
8687

87-
endpoint = os.environ["AZURE_FORM_RECOGNIZER_ENDPOINT"]
88+
endpoint = os.environ["AZURE_FORM_RECOGNIZER_AAD_ENDPOINT"]
8889
credential = DefaultAzureCredential()
8990

9091
form_training_client = FormTrainingClient(endpoint, credential)

sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_differentiate_output_models_trained_with_and_without_labels.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ def recognize_custom_forms(self):
4646
endpoint=endpoint, credential=AzureKeyCredential(key)
4747
)
4848

49+
path_to_sample_forms = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "./sample_forms/forms/Form_1.jpg"))
4950
# Make sure your form's type is included in the list of form types the custom model can recognize
50-
with open("sample_forms/forms/Form_1.jpg", "rb") as f:
51+
with open(path_to_sample_forms, "rb") as f:
5152
stream = f.read()
5253
forms_with_labeled_model_poller = form_recognizer_client.begin_recognize_custom_forms(
5354
model_id=model_trained_with_labels_id, form=stream

sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_get_bounding_boxes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ def get_bounding_boxes(self):
4444
endpoint=endpoint, credential=AzureKeyCredential(key)
4545
)
4646

47+
path_to_sample_forms = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "./sample_forms/forms/Form_1.jpg"))
4748
# Make sure your form's type is included in the list of form types the custom model can recognize
48-
with open("sample_forms/forms/Form_1.jpg", "rb") as f:
49+
with open(path_to_sample_forms, "rb") as f:
4950
poller = form_recognizer_client.begin_recognize_custom_forms(
5051
model_id=model_id, form=f, include_text_content=True
5152
)

sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_content.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def format_bounding_box(bounding_box):
3030
class RecognizeContentSample(object):
3131

3232
def recognize_content(self):
33+
path_to_sample_forms = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "./sample_forms/forms/Invoice_1.pdf"))
3334
# [START recognize_content]
3435
from azure.core.credentials import AzureKeyCredential
3536
from azure.ai.formrecognizer import FormRecognizerClient
@@ -38,7 +39,7 @@ def recognize_content(self):
3839
key = os.environ["AZURE_FORM_RECOGNIZER_KEY"]
3940

4041
form_recognizer_client = FormRecognizerClient(endpoint=endpoint, credential=AzureKeyCredential(key))
41-
with open("sample_forms/forms/Invoice_1.pdf", "rb") as f:
42+
with open(path_to_sample_forms, "rb") as f:
4243
poller = form_recognizer_client.begin_recognize_content(form=f)
4344
contents = poller.result()
4445

sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_custom_forms.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
class RecognizeCustomForms(object):
3030

3131
def recognize_custom_forms(self):
32+
path_to_sample_forms = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "./sample_forms/forms/Form_1.jpg"))
3233
# [START recognize_custom_forms]
3334
from azure.core.credentials import AzureKeyCredential
3435
from azure.ai.formrecognizer import FormRecognizerClient
@@ -42,7 +43,7 @@ def recognize_custom_forms(self):
4243
)
4344

4445
# Make sure your form's type is included in the list of form types the custom model can recognize
45-
with open("sample_forms/forms/Form_1.jpg", "rb") as f:
46+
with open(path_to_sample_forms, "rb") as f:
4647
poller = form_recognizer_client.begin_recognize_custom_forms(
4748
model_id=model_id, form=f
4849
)

sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
class RecognizeReceiptsSample(object):
2727

2828
def recognize_receipts(self):
29+
path_to_sample_forms = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "./sample_forms/receipt/contoso-allinone.jpg"))
2930
# [START recognize_receipts]
3031
from azure.core.credentials import AzureKeyCredential
3132
from azure.ai.formrecognizer import FormRecognizerClient
@@ -36,7 +37,7 @@ def recognize_receipts(self):
3637
form_recognizer_client = FormRecognizerClient(
3738
endpoint=endpoint, credential=AzureKeyCredential(key)
3839
)
39-
with open("sample_forms/receipt/contoso-allinone.jpg", "rb") as f:
40+
with open(path_to_sample_forms, "rb") as f:
4041
poller = form_recognizer_client.begin_recognize_receipts(receipt=f)
4142
receipts = poller.result()
4243

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# coding: utf-8
2+
3+
# -------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See License.txt in the project root for
6+
# license information.
7+
# --------------------------------------------------------------------------
8+
9+
"""
10+
USAGE:
11+
python test_samples.py
12+
13+
Set the environment variables with your own values before running the samples.
14+
See independent sample files to check what env variables must be set.
15+
"""
16+
17+
18+
import subprocess
19+
import functools
20+
import sys
21+
import os
22+
import pytest
23+
import pprint
24+
from azure.core.credentials import AzureKeyCredential
25+
from azure.ai.formrecognizer import FormTrainingClient
26+
from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer
27+
28+
def _setenv(key, val):
29+
os.environ[key] = os.getenv(val) or os.getenv(key)
30+
31+
def run(cmd, my_env):
32+
os.environ['PYTHONUNBUFFERED'] = "1"
33+
proc = subprocess.Popen(cmd,
34+
stdout = subprocess.PIPE,
35+
stderr = subprocess.STDOUT,
36+
env = my_env
37+
)
38+
stdout, stderr = proc.communicate()
39+
40+
return proc.returncode, stdout, stderr
41+
42+
def _test_file(file_name, account, key, root_dir='./samples'):
43+
os.environ['AZURE_FORM_RECOGNIZER_ENDPOINT'] = account
44+
os.environ['AZURE_FORM_RECOGNIZER_KEY'] = key
45+
my_env = dict(os.environ)
46+
if sys.version_info < (3, 5):
47+
my_env = {key: str(val) for key, val in my_env.items()}
48+
code, _, err = run([sys.executable, root_dir + '/' + file_name], my_env=my_env)
49+
assert code == 0
50+
assert err is None
51+
52+
class TestSamples(FormRecognizerTest):
53+
@pytest.mark.live_test_only
54+
@GlobalFormRecognizerAccountPreparer()
55+
def test_sample_authentication(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
56+
_setenv('AZURE_FORM_RECOGNIZER_AAD_ENDPOINT', 'AZURE_FORM_RECOGNIZER_AAD_ENDPOINT')
57+
_test_file('sample_authentication.py', form_recognizer_account, form_recognizer_account_key)
58+
59+
@pytest.mark.live_test_only
60+
@GlobalFormRecognizerAccountPreparer()
61+
def test_sample_get_bounding_boxes(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
62+
_setenv('CONTAINER_SAS_URL', 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL')
63+
ftc = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key))
64+
container_sas_url = os.environ['CONTAINER_SAS_URL']
65+
poller = ftc.begin_training(container_sas_url, use_training_labels=False)
66+
model = poller.result()
67+
os.environ['CUSTOM_TRAINED_MODEL_ID'] = model.model_id
68+
_test_file('sample_get_bounding_boxes.py', form_recognizer_account, form_recognizer_account_key)
69+
70+
@pytest.mark.live_test_only
71+
@GlobalFormRecognizerAccountPreparer()
72+
def test_sample_manage_custom_models(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
73+
_test_file('sample_manage_custom_models.py', form_recognizer_account, form_recognizer_account_key)
74+
75+
@pytest.mark.live_test_only
76+
@GlobalFormRecognizerAccountPreparer()
77+
def test_sample_recognize_content(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
78+
_test_file('sample_recognize_content.py', form_recognizer_account, form_recognizer_account_key)
79+
80+
@pytest.mark.live_test_only
81+
@GlobalFormRecognizerAccountPreparer()
82+
def test_sample_recognize_custom_forms(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
83+
_setenv('CONTAINER_SAS_URL', 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL')
84+
ftc = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key))
85+
container_sas_url = os.environ['CONTAINER_SAS_URL']
86+
poller = ftc.begin_training(container_sas_url, use_training_labels=False)
87+
model = poller.result()
88+
os.environ['CUSTOM_TRAINED_MODEL_ID'] = model.model_id
89+
_test_file('sample_recognize_custom_forms.py', form_recognizer_account, form_recognizer_account_key)
90+
91+
@pytest.mark.live_test_only
92+
@GlobalFormRecognizerAccountPreparer()
93+
def test_sample_recognize_receipts_from_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
94+
_test_file('sample_recognize_receipts_from_url.py', form_recognizer_account, form_recognizer_account_key)
95+
96+
@pytest.mark.live_test_only
97+
@GlobalFormRecognizerAccountPreparer()
98+
def test_sample_recognize_receipts(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
99+
_test_file('sample_recognize_receipts.py', form_recognizer_account, form_recognizer_account_key)
100+
101+
@pytest.mark.live_test_only
102+
@GlobalFormRecognizerAccountPreparer()
103+
def test_sample_train_model_with_labels(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
104+
_setenv('CONTAINER_SAS_URL', 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL')
105+
_test_file('sample_train_model_with_labels.py', form_recognizer_account, form_recognizer_account_key)
106+
107+
@pytest.mark.live_test_only
108+
@GlobalFormRecognizerAccountPreparer()
109+
def test_sample_train_model_without_labels(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
110+
_setenv('CONTAINER_SAS_URL', 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL')
111+
_test_file('sample_train_model_without_labels.py', form_recognizer_account, form_recognizer_account_key)
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# coding: utf-8
2+
3+
# -------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See License.txt in the project root for
6+
# license information.
7+
# --------------------------------------------------------------------------
8+
9+
"""
10+
USAGE:
11+
python test_samples.py
12+
13+
Set the environment variables with your own values before running the samples.
14+
See independent sample files to check what env variables must be set.
15+
"""
16+
17+
18+
import subprocess
19+
import functools
20+
import sys
21+
import os
22+
import pytest
23+
from azure.core.credentials import AzureKeyCredential
24+
from azure.ai.formrecognizer.aio import FormTrainingClient
25+
from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer
26+
27+
def _setenv(key, val):
28+
os.environ[key] = os.getenv(val) or os.getenv(key)
29+
30+
def run(cmd, my_env):
31+
os.environ['PYTHONUNBUFFERED'] = "1"
32+
proc = subprocess.Popen(cmd,
33+
stdout = subprocess.PIPE,
34+
stderr = subprocess.STDOUT,
35+
env = my_env
36+
)
37+
stdout, stderr = proc.communicate()
38+
39+
return proc.returncode, stdout, stderr
40+
41+
def _test_file(file_name, account, key, root_dir='./samples/async_samples'):
42+
os.environ['AZURE_FORM_RECOGNIZER_ENDPOINT'] = account
43+
os.environ['AZURE_FORM_RECOGNIZER_KEY'] = key
44+
code, _, err = run([sys.executable, root_dir + '/' + file_name], my_env=dict(os.environ))
45+
assert code == 0
46+
assert err is None
47+
48+
49+
class TestSamplesAsync(FormRecognizerTest):
50+
# Async sample tests
51+
@pytest.mark.live_test_only
52+
@GlobalFormRecognizerAccountPreparer()
53+
def test_sample_authentication_async(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
54+
_test_file('sample_authentication_async.py', form_recognizer_account, form_recognizer_account_key)
55+
56+
@pytest.mark.live_test_only
57+
@GlobalFormRecognizerAccountPreparer()
58+
async def test_sample_get_bounding_boxes_async(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
59+
_setenv('CONTAINER_SAS_URL', 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL')
60+
ftc = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key))
61+
container_sas_url = os.environ['CONTAINER_SAS_URL']
62+
poller = await ftc.begin_training(container_sas_url, use_training_labels=False)
63+
model = await poller.result()
64+
os.environ['CUSTOM_TRAINED_MODEL_ID'] = model.model_id
65+
_test_file('sample_get_bounding_boxes_async.py', form_recognizer_account, form_recognizer_account_key)
66+
67+
@pytest.mark.live_test_only
68+
@GlobalFormRecognizerAccountPreparer()
69+
def test_sample_manage_custom_models_async(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
70+
_test_file('sample_manage_custom_models_async.py', form_recognizer_account, form_recognizer_account_key)
71+
72+
@pytest.mark.live_test_only
73+
@GlobalFormRecognizerAccountPreparer()
74+
def test_sample_recognize_content_async(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
75+
_test_file('sample_recognize_content_async.py', form_recognizer_account, form_recognizer_account_key)
76+
77+
@pytest.mark.live_test_only
78+
@GlobalFormRecognizerAccountPreparer()
79+
async def test_sample_recognize_custom_forms_async(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
80+
_setenv('CONTAINER_SAS_URL', 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL')
81+
ftc = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key))
82+
container_sas_url = os.environ['CONTAINER_SAS_URL']
83+
poller = await ftc.begin_training(container_sas_url, use_training_labels=False)
84+
model = await poller.result()
85+
os.environ['CUSTOM_TRAINED_MODEL_ID'] = model.model_id
86+
_test_file('sample_recognize_custom_forms_async.py', form_recognizer_account, form_recognizer_account_key)
87+
88+
@pytest.mark.live_test_only
89+
@GlobalFormRecognizerAccountPreparer()
90+
def test_sample_recognize_receipts_from_url_async(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
91+
_test_file('sample_recognize_receipts_from_url_async.py', form_recognizer_account, form_recognizer_account_key)
92+
93+
@pytest.mark.live_test_only
94+
@GlobalFormRecognizerAccountPreparer()
95+
def test_sample_recognize_receipts_async(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
96+
_test_file('sample_recognize_receipts_async.py', form_recognizer_account, form_recognizer_account_key)
97+
98+
@pytest.mark.live_test_only
99+
@GlobalFormRecognizerAccountPreparer()
100+
def test_sample_train_model_with_labels_async(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
101+
_setenv('CONTAINER_SAS_URL', 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL')
102+
_test_file('sample_train_model_with_labels_async.py', form_recognizer_account, form_recognizer_account_key)
103+
104+
@pytest.mark.live_test_only
105+
@GlobalFormRecognizerAccountPreparer()
106+
def test_sample_train_model_without_labels_async(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
107+
_setenv('CONTAINER_SAS_URL', 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL')
108+
_test_file('sample_train_model_without_labels_async.py', form_recognizer_account, form_recognizer_account_key)
109+

0 commit comments

Comments
 (0)