From ef0d76143e7ecc7b2599b57cd4f866e3cad67018 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Wed, 17 Jun 2020 02:20:16 -0700 Subject: [PATCH 1/2] changes in samples --- .../tests/test_samples.py | 21 +++++++++++-------- .../tests/test_samples_async.py | 20 +++++++++++------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_samples.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_samples.py index 7e30f070c346..4bcd866bc832 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_samples.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_samples.py @@ -26,7 +26,7 @@ from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer def _setenv(key, val): - os.environ[key] = os.getenv(val) or os.getenv(key) + os.environ[key] = os.getenv(val) or key def run(cmd, my_env): os.environ['PYTHONUNBUFFERED'] = "1" @@ -45,21 +45,24 @@ def _test_file(file_name, account, key, root_dir='./samples'): my_env = dict(os.environ) if sys.version_info < (3, 5): my_env = {key: str(val) for key, val in my_env.items()} - code, _, err = run([sys.executable, root_dir + '/' + file_name], my_env=my_env) - assert code == 0 - assert err is None + code, out, err = run([sys.executable, root_dir + '/' + file_name], my_env=my_env) + try: + assert code == 0 + assert err is None + except AssertionError as e: + e.args += (out, ) + raise AssertionError(e) class TestSamples(FormRecognizerTest): @pytest.mark.live_test_only @GlobalFormRecognizerAccountPreparer() def test_sample_authentication(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - _setenv('AZURE_FORM_RECOGNIZER_AAD_ENDPOINT', 'AZURE_FORM_RECOGNIZER_AAD_ENDPOINT') _test_file('sample_authentication.py', form_recognizer_account, form_recognizer_account_key) @pytest.mark.live_test_only @GlobalFormRecognizerAccountPreparer() def test_sample_get_bounding_boxes(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - _setenv('CONTAINER_SAS_URL', 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') + _setenv(self.get_settings_value('CONTAINER_SAS_URL'), 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') ftc = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) container_sas_url = os.environ['CONTAINER_SAS_URL'] poller = ftc.begin_training(container_sas_url, use_training_labels=False) @@ -80,7 +83,7 @@ def test_sample_recognize_content(self, resource_group, location, form_recognize @pytest.mark.live_test_only @GlobalFormRecognizerAccountPreparer() def test_sample_recognize_custom_forms(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - _setenv('CONTAINER_SAS_URL', 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') + _setenv(self.get_settings_value('CONTAINER_SAS_URL'), 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') ftc = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) container_sas_url = os.environ['CONTAINER_SAS_URL'] poller = ftc.begin_training(container_sas_url, use_training_labels=False) @@ -101,11 +104,11 @@ def test_sample_recognize_receipts(self, resource_group, location, form_recogniz @pytest.mark.live_test_only @GlobalFormRecognizerAccountPreparer() def test_sample_train_model_with_labels(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - _setenv('CONTAINER_SAS_URL', 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') + _setenv(self.get_settings_value('CONTAINER_SAS_URL'), 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') _test_file('sample_train_model_with_labels.py', form_recognizer_account, form_recognizer_account_key) @pytest.mark.live_test_only @GlobalFormRecognizerAccountPreparer() def test_sample_train_model_without_labels(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - _setenv('CONTAINER_SAS_URL', 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') + _setenv(self.get_settings_value('CONTAINER_SAS_URL'), 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') _test_file('sample_train_model_without_labels.py', form_recognizer_account, form_recognizer_account_key) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_samples_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_samples_async.py index d1db2ca048b4..579d2f771839 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_samples_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_samples_async.py @@ -25,7 +25,7 @@ from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer def _setenv(key, val): - os.environ[key] = os.getenv(val) or os.getenv(key) + os.environ[key] = os.getenv(val) or key def run(cmd, my_env): os.environ['PYTHONUNBUFFERED'] = "1" @@ -41,9 +41,13 @@ def run(cmd, my_env): def _test_file(file_name, account, key, root_dir='./samples/async_samples'): os.environ['AZURE_FORM_RECOGNIZER_ENDPOINT'] = account os.environ['AZURE_FORM_RECOGNIZER_KEY'] = key - code, _, err = run([sys.executable, root_dir + '/' + file_name], my_env=dict(os.environ)) - assert code == 0 - assert err is None + code, out, err = run([sys.executable, root_dir + '/' + file_name], my_env=dict(os.environ)) + try: + assert code == 0 + assert err is None + except AssertionError as e: + e.args += (out, ) + raise AssertionError(e) class TestSamplesAsync(FormRecognizerTest): @@ -56,7 +60,7 @@ def test_sample_authentication_async(self, resource_group, location, form_recogn @pytest.mark.live_test_only @GlobalFormRecognizerAccountPreparer() async def test_sample_get_bounding_boxes_async(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - _setenv('CONTAINER_SAS_URL', 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') + _setenv(self.get_settings_value('CONTAINER_SAS_URL'), 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') ftc = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) container_sas_url = os.environ['CONTAINER_SAS_URL'] poller = await ftc.begin_training(container_sas_url, use_training_labels=False) @@ -77,7 +81,7 @@ def test_sample_recognize_content_async(self, resource_group, location, form_rec @pytest.mark.live_test_only @GlobalFormRecognizerAccountPreparer() async def test_sample_recognize_custom_forms_async(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - _setenv('CONTAINER_SAS_URL', 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') + _setenv(self.get_settings_value('CONTAINER_SAS_URL'), 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') ftc = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) container_sas_url = os.environ['CONTAINER_SAS_URL'] poller = await ftc.begin_training(container_sas_url, use_training_labels=False) @@ -98,12 +102,12 @@ def test_sample_recognize_receipts_async(self, resource_group, location, form_re @pytest.mark.live_test_only @GlobalFormRecognizerAccountPreparer() def test_sample_train_model_with_labels_async(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - _setenv('CONTAINER_SAS_URL', 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') + _setenv(self.get_settings_value('CONTAINER_SAS_URL'), 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') _test_file('sample_train_model_with_labels_async.py', form_recognizer_account, form_recognizer_account_key) @pytest.mark.live_test_only @GlobalFormRecognizerAccountPreparer() def test_sample_train_model_without_labels_async(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - _setenv('CONTAINER_SAS_URL', 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') + _setenv(self.get_settings_value('CONTAINER_SAS_URL'), 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') _test_file('sample_train_model_without_labels_async.py', form_recognizer_account, form_recognizer_account_key) From 0b4e9e6ae3147d45a20cb150f735d8aa58556b4a Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Wed, 17 Jun 2020 10:50:43 -0700 Subject: [PATCH 2/2] oops --- .../azure-ai-formrecognizer/tests/test_samples.py | 10 ++++------ .../tests/test_samples_async.py | 11 ++++------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_samples.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_samples.py index 4bcd866bc832..2c282f1976cd 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_samples.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_samples.py @@ -25,8 +25,6 @@ from azure.ai.formrecognizer import FormTrainingClient from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer -def _setenv(key, val): - os.environ[key] = os.getenv(val) or key def run(cmd, my_env): os.environ['PYTHONUNBUFFERED'] = "1" @@ -62,7 +60,7 @@ def test_sample_authentication(self, resource_group, location, form_recognizer_a @pytest.mark.live_test_only @GlobalFormRecognizerAccountPreparer() def test_sample_get_bounding_boxes(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - _setenv(self.get_settings_value('CONTAINER_SAS_URL'), 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') + os.environ['CONTAINER_SAS_URL'] = self.get_settings_value("FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL") ftc = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) container_sas_url = os.environ['CONTAINER_SAS_URL'] poller = ftc.begin_training(container_sas_url, use_training_labels=False) @@ -83,7 +81,7 @@ def test_sample_recognize_content(self, resource_group, location, form_recognize @pytest.mark.live_test_only @GlobalFormRecognizerAccountPreparer() def test_sample_recognize_custom_forms(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - _setenv(self.get_settings_value('CONTAINER_SAS_URL'), 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') + os.environ['CONTAINER_SAS_URL'] = self.get_settings_value("FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL") ftc = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) container_sas_url = os.environ['CONTAINER_SAS_URL'] poller = ftc.begin_training(container_sas_url, use_training_labels=False) @@ -104,11 +102,11 @@ def test_sample_recognize_receipts(self, resource_group, location, form_recogniz @pytest.mark.live_test_only @GlobalFormRecognizerAccountPreparer() def test_sample_train_model_with_labels(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - _setenv(self.get_settings_value('CONTAINER_SAS_URL'), 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') + os.environ['CONTAINER_SAS_URL'] = self.get_settings_value("FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL") _test_file('sample_train_model_with_labels.py', form_recognizer_account, form_recognizer_account_key) @pytest.mark.live_test_only @GlobalFormRecognizerAccountPreparer() def test_sample_train_model_without_labels(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - _setenv(self.get_settings_value('CONTAINER_SAS_URL'), 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') + os.environ['CONTAINER_SAS_URL'] = self.get_settings_value("FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL") _test_file('sample_train_model_without_labels.py', form_recognizer_account, form_recognizer_account_key) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_samples_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_samples_async.py index 579d2f771839..88c74dfef491 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_samples_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_samples_async.py @@ -24,9 +24,6 @@ from azure.ai.formrecognizer.aio import FormTrainingClient from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer -def _setenv(key, val): - os.environ[key] = os.getenv(val) or key - def run(cmd, my_env): os.environ['PYTHONUNBUFFERED'] = "1" proc = subprocess.Popen(cmd, @@ -60,7 +57,7 @@ def test_sample_authentication_async(self, resource_group, location, form_recogn @pytest.mark.live_test_only @GlobalFormRecognizerAccountPreparer() async def test_sample_get_bounding_boxes_async(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - _setenv(self.get_settings_value('CONTAINER_SAS_URL'), 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') + os.environ['CONTAINER_SAS_URL'] = self.get_settings_value("FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL") ftc = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) container_sas_url = os.environ['CONTAINER_SAS_URL'] poller = await ftc.begin_training(container_sas_url, use_training_labels=False) @@ -81,7 +78,7 @@ def test_sample_recognize_content_async(self, resource_group, location, form_rec @pytest.mark.live_test_only @GlobalFormRecognizerAccountPreparer() async def test_sample_recognize_custom_forms_async(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - _setenv(self.get_settings_value('CONTAINER_SAS_URL'), 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') + os.environ['CONTAINER_SAS_URL'] = self.get_settings_value("FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL") ftc = FormTrainingClient(form_recognizer_account, AzureKeyCredential(form_recognizer_account_key)) container_sas_url = os.environ['CONTAINER_SAS_URL'] poller = await ftc.begin_training(container_sas_url, use_training_labels=False) @@ -102,12 +99,12 @@ def test_sample_recognize_receipts_async(self, resource_group, location, form_re @pytest.mark.live_test_only @GlobalFormRecognizerAccountPreparer() def test_sample_train_model_with_labels_async(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - _setenv(self.get_settings_value('CONTAINER_SAS_URL'), 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') + os.environ['CONTAINER_SAS_URL'] = self.get_settings_value("FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL") _test_file('sample_train_model_with_labels_async.py', form_recognizer_account, form_recognizer_account_key) @pytest.mark.live_test_only @GlobalFormRecognizerAccountPreparer() def test_sample_train_model_without_labels_async(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): - _setenv(self.get_settings_value('CONTAINER_SAS_URL'), 'AZURE_FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL') + os.environ['CONTAINER_SAS_URL'] = self.get_settings_value("FORM_RECOGNIZER_STORAGE_CONTAINER_SAS_URL") _test_file('sample_train_model_without_labels_async.py', form_recognizer_account, form_recognizer_account_key)