Skip to content

Commit e2b5852

Browse files
authored
Update TextAnalytics to enable live testing in sovereign clouds for multiple services (Azure#22461)
1 parent a968613 commit e2b5852

File tree

7 files changed

+77
-32
lines changed

7 files changed

+77
-32
lines changed

sdk/textanalytics/azure-ai-textanalytics/tests/test_analyze.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from unittest import mock
1515
from azure.core.exceptions import HttpResponseError, ClientAuthenticationError
1616
from azure.core.credentials import AzureKeyCredential
17-
from testcase import TextAnalyticsTest, TextAnalyticsPreparer
17+
from testcase import TextAnalyticsTest, TextAnalyticsPreparer, is_public_cloud
1818
from testcase import TextAnalyticsClientPreparer as _TextAnalyticsClientPreparer
1919
from devtools_testutils import recorded_by_proxy, set_bodiless_matcher
2020
from azure.ai.textanalytics import (
@@ -47,6 +47,17 @@
4747
# pre-apply the client_cls positional argument so it needn't be explicitly passed below
4848
TextAnalyticsClientPreparer = functools.partial(_TextAnalyticsClientPreparer, TextAnalyticsClient)
4949

50+
TextAnalyticsCustomPreparer = functools.partial(
51+
TextAnalyticsPreparer,
52+
textanalytics_custom_text_endpoint="https://fakeendpoint.cognitiveservices.azure.com",
53+
textanalytics_custom_text_key="fakeZmFrZV9hY29jdW50X2tleQ==",
54+
textanalytics_single_category_classify_project_name="single_category_classify_project_name",
55+
textanalytics_single_category_classify_deployment_name="single_category_classify_deployment_name",
56+
textanalytics_multi_category_classify_project_name="multi_category_classify_project_name",
57+
textanalytics_multi_category_classify_deployment_name="multi_category_classify_deployment_name",
58+
textanalytics_custom_entities_project_name="custom_entities_project_name",
59+
textanalytics_custom_entities_deployment_name="custom_entities_deployment_name",
60+
)
5061

5162
class TestAnalyze(TextAnalyticsTest):
5263

@@ -627,7 +638,8 @@ def test_too_many_documents(self, client):
627638
)
628639
assert excinfo.value.status_code == 400
629640

630-
@TextAnalyticsPreparer()
641+
@pytest.mark.skipif(not is_public_cloud(), reason='Usgov and China Cloud are not supported')
642+
@TextAnalyticsCustomPreparer()
631643
@recorded_by_proxy
632644
def test_disable_service_logs(
633645
self,
@@ -989,7 +1001,8 @@ def test_extract_summary_partial_results(self, client):
9891001
assert not document_results[1][0].is_error
9901002
assert isinstance(document_results[1][0], ExtractSummaryResult)
9911003

992-
@TextAnalyticsPreparer()
1004+
@pytest.mark.skipif(not is_public_cloud(), reason='Usgov and China Cloud are not supported')
1005+
@TextAnalyticsCustomPreparer()
9931006
@recorded_by_proxy
9941007
def test_single_category_classify(
9951008
self,
@@ -1028,7 +1041,8 @@ def test_single_category_classify(
10281041
assert result.classification.category
10291042
assert result.classification.confidence_score
10301043

1031-
@TextAnalyticsPreparer()
1044+
@pytest.mark.skipif(not is_public_cloud(), reason='Usgov and China Cloud are not supported')
1045+
@TextAnalyticsCustomPreparer()
10321046
@recorded_by_proxy
10331047
def test_multi_category_classify(
10341048
self,
@@ -1068,7 +1082,8 @@ def test_multi_category_classify(
10681082
assert classification.category
10691083
assert classification.confidence_score
10701084

1071-
@TextAnalyticsPreparer()
1085+
@pytest.mark.skipif(not is_public_cloud(), reason='Usgov and China Cloud are not supported')
1086+
@TextAnalyticsCustomPreparer()
10721087
@recorded_by_proxy
10731088
def test_recognize_custom_entities(
10741089
self,
@@ -1216,7 +1231,8 @@ def test_analyze_continuation_token(self, client):
12161231

12171232
initial_poller.wait() # necessary so azure-devtools doesn't throw assertion error
12181233

1219-
@TextAnalyticsPreparer()
1234+
@pytest.mark.skipif(not is_public_cloud(), reason='Usgov and China Cloud are not supported')
1235+
@TextAnalyticsCustomPreparer()
12201236
def test_generic_action_error_no_target(
12211237
self,
12221238
textanalytics_custom_text_endpoint,
@@ -1276,7 +1292,8 @@ def test_generic_action_error_no_target(
12761292
).result())
12771293
assert e.value.message == "(InternalServerError) 1 out of 3 job tasks failed. Failed job tasks : v3.2-preview.2/custom/entities/general."
12781294

1279-
@TextAnalyticsPreparer()
1295+
@pytest.mark.skipif(not is_public_cloud(), reason='Usgov and China Cloud are not supported')
1296+
@TextAnalyticsCustomPreparer()
12801297
def test_action_errors_with_targets(
12811298
self,
12821299
textanalytics_custom_text_endpoint,
@@ -1366,7 +1383,8 @@ def test_action_errors_with_targets(
13661383
assert result.error.code == "InvalidRequest"
13671384
assert result.error.message == "Some error" + str(idx) # confirms correct doc error order
13681385

1369-
@TextAnalyticsPreparer()
1386+
@pytest.mark.skipif(not is_public_cloud(), reason='Usgov and China Cloud are not supported')
1387+
@TextAnalyticsCustomPreparer()
13701388
def test_action_job_failure(
13711389
self,
13721390
textanalytics_custom_text_endpoint,

sdk/textanalytics/azure-ai-textanalytics/tests/test_analyze_async.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from azure.core.exceptions import HttpResponseError, ClientAuthenticationError
1919
from azure.core.credentials import AzureKeyCredential
20-
from testcase import TextAnalyticsPreparer
20+
from testcase import TextAnalyticsPreparer, is_public_cloud
2121
from testcase import TextAnalyticsClientPreparer as _TextAnalyticsClientPreparer
2222
from devtools_testutils import set_bodiless_matcher
2323
from devtools_testutils.aio import recorded_by_proxy_async
@@ -50,6 +50,18 @@
5050
# pre-apply the client_cls positional argument so it needn't be explicitly passed below
5151
TextAnalyticsClientPreparer = functools.partial(_TextAnalyticsClientPreparer, TextAnalyticsClient)
5252

53+
TextAnalyticsCustomPreparer = functools.partial(
54+
TextAnalyticsPreparer,
55+
textanalytics_custom_text_endpoint="https://fakeendpoint.cognitiveservices.azure.com",
56+
textanalytics_custom_text_key="fakeZmFrZV9hY29jdW50X2tleQ==",
57+
textanalytics_single_category_classify_project_name="single_category_classify_project_name",
58+
textanalytics_single_category_classify_deployment_name="single_category_classify_deployment_name",
59+
textanalytics_multi_category_classify_project_name="multi_category_classify_project_name",
60+
textanalytics_multi_category_classify_deployment_name="multi_category_classify_deployment_name",
61+
textanalytics_custom_entities_project_name="custom_entities_project_name",
62+
textanalytics_custom_entities_deployment_name="custom_entities_deployment_name",
63+
)
64+
5365
def get_completed_future(result=None):
5466
future = asyncio.Future()
5567
future.set_result(result)
@@ -682,7 +694,8 @@ async def test_too_many_documents(self, client):
682694
)).result()
683695
assert excinfo.value.status_code == 400
684696

685-
@TextAnalyticsPreparer()
697+
@pytest.mark.skipif(not is_public_cloud(), reason='Usgov and China Cloud are not supported')
698+
@TextAnalyticsCustomPreparer()
686699
@recorded_by_proxy_async
687700
async def test_disable_service_logs(
688701
self,
@@ -1058,7 +1071,8 @@ async def test_extract_summary_partial_results(self, client):
10581071
assert not document_results[1][0].is_error
10591072
assert isinstance(document_results[1][0], ExtractSummaryResult)
10601073

1061-
@TextAnalyticsPreparer()
1074+
@pytest.mark.skipif(not is_public_cloud(), reason='Usgov and China Cloud are not supported')
1075+
@TextAnalyticsCustomPreparer()
10621076
@recorded_by_proxy_async
10631077
async def test_single_category_classify(
10641078
self,
@@ -1100,7 +1114,8 @@ async def test_single_category_classify(
11001114
assert result.classification.category
11011115
assert result.classification.confidence_score
11021116

1103-
@TextAnalyticsPreparer()
1117+
@pytest.mark.skipif(not is_public_cloud(), reason='Usgov and China Cloud are not supported')
1118+
@TextAnalyticsCustomPreparer()
11041119
@recorded_by_proxy_async
11051120
async def test_multi_category_classify(
11061121
self,
@@ -1144,7 +1159,8 @@ async def test_multi_category_classify(
11441159
assert classification.category
11451160
assert classification.confidence_score
11461161

1147-
@TextAnalyticsPreparer()
1162+
@pytest.mark.skipif(not is_public_cloud(), reason='Usgov and China Cloud are not supported')
1163+
@TextAnalyticsCustomPreparer()
11481164
@recorded_by_proxy_async
11491165
async def test_recognize_custom_entities(
11501166
self,
@@ -1303,7 +1319,8 @@ async def test_analyze_continuation_token(self, client):
13031319

13041320
await initial_poller.wait() # necessary so azure-devtools doesn't throw assertion error
13051321

1306-
@TextAnalyticsPreparer()
1322+
@pytest.mark.skipif(not is_public_cloud(), reason='Usgov and China Cloud are not supported')
1323+
@TextAnalyticsCustomPreparer()
13071324
@recorded_by_proxy_async
13081325
async def test_generic_action_error_no_target(
13091326
self,
@@ -1367,7 +1384,8 @@ async def test_generic_action_error_no_target(
13671384
results.append(resp)
13681385
assert e.value.message == "(InternalServerError) 1 out of 3 job tasks failed. Failed job tasks : v3.2-preview.2/custom/entities/general."
13691386

1370-
@TextAnalyticsPreparer()
1387+
@pytest.mark.skipif(not is_public_cloud(), reason='Usgov and China Cloud are not supported')
1388+
@TextAnalyticsCustomPreparer()
13711389
@recorded_by_proxy_async
13721390
async def test_action_errors_with_targets(
13731391
self,
@@ -1463,7 +1481,8 @@ async def test_action_errors_with_targets(
14631481
assert result.error.code == "InvalidRequest"
14641482
assert result.error.message == "Some error" + str(idx) # confirms correct doc error order
14651483

1466-
@TextAnalyticsPreparer()
1484+
@pytest.mark.skipif(not is_public_cloud(), reason='Usgov and China Cloud are not supported')
1485+
@TextAnalyticsCustomPreparer()
14671486
@recorded_by_proxy_async
14681487
async def test_action_job_failure(
14691488
self,

sdk/textanalytics/azure-ai-textanalytics/tests/test_auth.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77
from testcase import TextAnalyticsTest, TextAnalyticsPreparer
88
from azure.ai.textanalytics import TextAnalyticsClient
99
from azure.core.credentials import AzureKeyCredential
10+
import os
11+
1012

1113
class TestAuth(TextAnalyticsTest):
1214

1315
@pytest.mark.live_test_only
1416
@TextAnalyticsPreparer()
1517
def test_active_directory_auth(self, textanalytics_test_endpoint):
1618
token = self.get_credential(TextAnalyticsClient)
17-
text_analytics = TextAnalyticsClient(textanalytics_test_endpoint, token)
19+
text_analytics_endpoint_suffix = os.environ.get("TEXTANALYTICS_ENDPOINT_SUFFIX",".cognitiveservices.azure.com")
20+
credential_scopes = ["https://{}/.default".format(text_analytics_endpoint_suffix[1:])]
21+
text_analytics = TextAnalyticsClient(textanalytics_test_endpoint, token, credential_scopes=credential_scopes)
1822

1923
docs = [{"id": "1", "text": "I should take my cat to the veterinarian."},
2024
{"id": "2", "text": "Este es un document escrito en Español."},

sdk/textanalytics/azure-ai-textanalytics/tests/test_auth_async.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from azure.core.credentials import AzureKeyCredential
99
from testcase import TextAnalyticsPreparer
1010
from testcase import TextAnalyticsTest
11+
import os
1112

1213

1314
class TestAuth(TextAnalyticsTest):
@@ -16,7 +17,9 @@ class TestAuth(TextAnalyticsTest):
1617
@TextAnalyticsPreparer()
1718
async def test_active_directory_auth(self, textanalytics_test_endpoint):
1819
token = self.get_credential(TextAnalyticsClient, is_async=True)
19-
text_analytics = TextAnalyticsClient(textanalytics_test_endpoint, token)
20+
text_analytics_endpoint_suffix = os.environ.get("TEXTANALYTICS_ENDPOINT_SUFFIX",".cognitiveservices.azure.com")
21+
credential_scopes = ["https://{}/.default".format(text_analytics_endpoint_suffix[1:])]
22+
text_analytics = TextAnalyticsClient(textanalytics_test_endpoint, token, credential_scopes=credential_scopes)
2023

2124
docs = [{"id": "1", "text": "I should take my cat to the veterinarian."},
2225
{"id": "2", "text": "Este es un document escrito en Español."},

sdk/textanalytics/azure-ai-textanalytics/tests/testcase.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,15 @@
2222
from devtools_testutils import PowerShellPreparer, AzureRecordedTestCase
2323

2424

25+
def is_public_cloud():
26+
return (".microsoftonline.com" in os.getenv('AZURE_AUTHORITY_HOST', ''))
27+
28+
2529
TextAnalyticsPreparer = functools.partial(
2630
PowerShellPreparer,
2731
'textanalytics',
2832
textanalytics_test_endpoint="https://fakeendpoint.cognitiveservices.azure.com",
2933
textanalytics_test_api_key="fakeZmFrZV9hY29jdW50X2tleQ==",
30-
textanalytics_custom_text_endpoint="https://fakeendpoint.cognitiveservices.azure.com",
31-
textanalytics_custom_text_key="fakeZmFrZV9hY29jdW50X2tleQ==",
32-
textanalytics_single_category_classify_project_name="single_category_classify_project_name",
33-
textanalytics_single_category_classify_deployment_name="single_category_classify_deployment_name",
34-
textanalytics_multi_category_classify_project_name="multi_category_classify_project_name",
35-
textanalytics_multi_category_classify_deployment_name="multi_category_classify_deployment_name",
36-
textanalytics_custom_entities_project_name="custom_entities_project_name",
37-
textanalytics_custom_entities_deployment_name="custom_entities_deployment_name",
3834
)
3935

4036

sdk/textanalytics/test-resources.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@
4242
"description": "The application client secret used to run tests."
4343
}
4444
},
45-
"textAnalyticsEndpointSuffix": {
46-
"defaultValue": ".cognitiveservices.azure.com/",
45+
"cognitiveServicesEndpointSuffix": {
46+
"defaultValue": ".cognitiveservices.azure.com",
4747
"type": "String"
48-
},
48+
}
4949
},
5050
"variables": {
5151
"authorizationApiVersion": "2018-09-01-preview",
5252
"textAnalyticsBaseName": "[concat('textanalytics', parameters('baseName'))]",
5353
"cognitiveApiVersion": "2017-04-18",
54-
"azureTextAnalyticsUrl": "[concat('https://', variables('textAnalyticsBaseName'), parameters('textAnalyticsEndpointSuffix'))]",
54+
"azureTextAnalyticsUrl": "[concat('https://', variables('textAnalyticsBaseName'), parameters('cognitiveServicesEndpointSuffix'))]",
5555
"cognitiveServiceUserRoleId": "[concat('/subscriptions/', subscription().subscriptionId, '/providers/Microsoft.Authorization/roleDefinitions/a97b65f3-24c7-4388-baec-2e87135dc908')]"
5656
},
5757
"resources": [
@@ -101,6 +101,10 @@
101101
"TEXTANALYTICS_TEST_ENDPOINT": {
102102
"type": "string",
103103
"value": "[variables('azureTextAnalyticsUrl')]"
104+
},
105+
"TEXTANALYTICS_ENDPOINT_SUFFIX": {
106+
"type": "string",
107+
"value": "[parameters('cognitiveServicesEndpointSuffix')]"
104108
}
105109
}
106110
}

sdk/textanalytics/tests.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ stages:
1010
SubscriptionConfigurations:
1111
- $(sub-config-azure-cloud-test-resources)
1212
- $(sub-config-text-analytics-azure-cloud-test-resources)
13+
MatrixReplace:
14+
- TestSamples=.*/true
1315
UsGov:
1416
SubscriptionConfiguration: $(sub-config-gov-test-resources)
1517
China:
1618
SubscriptionConfiguration: $(sub-config-cn-test-resources)
17-
MatrixReplace:
18-
- TestSamples=.*/true
19+
SupportedClouds: 'Public,UsGov,China'
1920
EnvVars:
2021
AZURE_SUBSCRIPTION_ID: $(TEXTANALYTICS_SUBSCRIPTION_ID)
2122
AZURE_TENANT_ID: $(TEXTANALYTICS_TENANT_ID)

0 commit comments

Comments
 (0)