Skip to content

Commit 3f37036

Browse files
authored
DI migrate tests (#36239)
1 parent c3e5a7a commit 3f37036

12 files changed

+66
-61
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
-e ../../../tools/azure-sdk-tools
22
../../core/azure-core
3-
aiohttp
3+
aiohttp
4+
azure-identity

sdk/documentintelligence/azure-ai-documentintelligence/tests/preparers.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
import os
99
import functools
10-
from devtools_testutils import PowerShellPreparer, AzureMgmtPreparer
11-
from azure.core.credentials import AzureKeyCredential
10+
from devtools_testutils import PowerShellPreparer, AzureMgmtPreparer, get_credential
1211

1312
ENABLE_LOGGER = os.getenv("ENABLE_LOGGER", "False")
1413
REGION = os.getenv("DOCUMENTINTELLIGENCE_LOCATION", None)
@@ -18,7 +17,6 @@
1817
PowerShellPreparer,
1918
"documentintelligence",
2019
documentintelligence_endpoint="https://fakeendpoint.cognitiveservices.azure.com",
21-
documentintelligence_api_key="fakeZmFrZV9hY29jdW50X2tleQ==",
2220
documentintelligence_storage_container_sas_url="https://blob_sas_url",
2321
documentintelligence_testing_data_container_sas_url="https://blob_sas_url",
2422
documentintelligence_training_data_classifier_sas_url="https://blob_sas_url",
@@ -36,16 +34,38 @@ def __init__(self, client_cls, client_kwargs={}, **kwargs):
3634
def create_resource(self, name, **kwargs):
3735
if self.is_live:
3836
documentintelligence_endpoint = os.environ["DOCUMENTINTELLIGENCE_ENDPOINT"]
39-
documentintelligence_api_key = os.environ["DOCUMENTINTELLIGENCE_API_KEY"]
4037
polling_interval = 5
4138
else:
4239
documentintelligence_endpoint = "https://fakeendpoint.cognitiveservices.azure.com"
43-
documentintelligence_api_key = "fakeZmFrZV9hY29jdW50X2tleQ=="
4440
polling_interval = 0
4541

4642
client = self.client_cls(
4743
documentintelligence_endpoint,
48-
AzureKeyCredential(documentintelligence_api_key),
44+
get_credential(),
45+
polling_interval=polling_interval,
46+
logging_enable=True if ENABLE_LOGGER == "True" else False,
47+
**self.client_kwargs
48+
)
49+
kwargs.update({"client": client})
50+
return kwargs
51+
52+
class GlobalClientPreparerAsync(AzureMgmtPreparer):
53+
def __init__(self, client_cls, client_kwargs={}, **kwargs):
54+
super(GlobalClientPreparerAsync, self).__init__(name_prefix="", random_name_length=42)
55+
self.client_kwargs = client_kwargs
56+
self.client_cls = client_cls
57+
58+
def create_resource(self, name, **kwargs):
59+
if self.is_live:
60+
documentintelligence_endpoint = os.environ["DOCUMENTINTELLIGENCE_ENDPOINT"]
61+
polling_interval = 5
62+
else:
63+
documentintelligence_endpoint = "https://fakeendpoint.cognitiveservices.azure.com"
64+
polling_interval = 0
65+
66+
client = self.client_cls(
67+
documentintelligence_endpoint,
68+
get_credential(is_async=True),
4969
polling_interval=polling_interval,
5070
logging_enable=True if ENABLE_LOGGER == "True" else False,
5171
**self.client_kwargs

sdk/documentintelligence/azure-ai-documentintelligence/tests/test_dac_analyze_custom_model.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
import pytest
88
import uuid
9-
from devtools_testutils import recorded_by_proxy, set_bodiless_matcher
10-
from azure.core.credentials import AzureKeyCredential
9+
from devtools_testutils import recorded_by_proxy, set_bodiless_matcher, get_credential
1110
from azure.core.exceptions import ResourceNotFoundError
1211
from azure.ai.documentintelligence import DocumentIntelligenceClient, DocumentIntelligenceAdministrationClient
1312
from azure.ai.documentintelligence.models import (
@@ -24,9 +23,8 @@ class TestDACAnalyzeCustomModel(DocumentIntelligenceTest):
2423
@DocumentIntelligencePreparer()
2524
def test_analyze_document_none_model_id(self, **kwargs):
2625
documentintelligence_endpoint = kwargs.pop("documentintelligence_endpoint")
27-
documentintelligence_api_key = kwargs.pop("documentintelligence_api_key")
2826
client = DocumentIntelligenceClient(
29-
documentintelligence_endpoint, AzureKeyCredential(documentintelligence_api_key)
27+
documentintelligence_endpoint, get_credential()
3028
)
3129
with pytest.raises(ValueError) as e:
3230
client.begin_analyze_document(model_id=None, analyze_request=b"xx")
@@ -35,9 +33,8 @@ def test_analyze_document_none_model_id(self, **kwargs):
3533
@DocumentIntelligencePreparer()
3634
def test_analyze_document_none_model_id_from_url(self, **kwargs):
3735
documentintelligence_endpoint = kwargs.pop("documentintelligence_endpoint")
38-
documentintelligence_api_key = kwargs.pop("documentintelligence_api_key")
3936
client = DocumentIntelligenceClient(
40-
documentintelligence_endpoint, AzureKeyCredential(documentintelligence_api_key)
37+
documentintelligence_endpoint, get_credential()
4138
)
4239
with pytest.raises(ValueError) as e:
4340
client.begin_analyze_document(
@@ -49,9 +46,8 @@ def test_analyze_document_none_model_id_from_url(self, **kwargs):
4946
@recorded_by_proxy
5047
def test_analyze_document_empty_model_id(self, **kwargs):
5148
documentintelligence_endpoint = kwargs.pop("documentintelligence_endpoint")
52-
documentintelligence_api_key = kwargs.pop("documentintelligence_api_key")
5349
client = DocumentIntelligenceClient(
54-
documentintelligence_endpoint, AzureKeyCredential(documentintelligence_api_key)
50+
documentintelligence_endpoint, get_credential()
5551
)
5652
with pytest.raises(ResourceNotFoundError) as e:
5753
client.begin_analyze_document(model_id="", analyze_request=b"xx")
@@ -62,9 +58,8 @@ def test_analyze_document_empty_model_id(self, **kwargs):
6258
@recorded_by_proxy
6359
def test_analyze_document_empty_model_id_from_url(self, **kwargs):
6460
documentintelligence_endpoint = kwargs.pop("documentintelligence_endpoint")
65-
documentintelligence_api_key = kwargs.pop("documentintelligence_api_key")
6661
client = DocumentIntelligenceClient(
67-
documentintelligence_endpoint, AzureKeyCredential(documentintelligence_api_key)
62+
documentintelligence_endpoint, get_credential()
6863
)
6964
with pytest.raises(ResourceNotFoundError) as e:
7065
client.begin_analyze_document(
@@ -78,12 +73,11 @@ def test_analyze_document_empty_model_id_from_url(self, **kwargs):
7873
def test_custom_document_transform(self, documentintelligence_storage_container_sas_url, **kwargs):
7974
set_bodiless_matcher()
8075
documentintelligence_endpoint = kwargs.pop("documentintelligence_endpoint")
81-
documentintelligence_api_key = kwargs.pop("documentintelligence_api_key")
8276
di_admin_client = DocumentIntelligenceAdministrationClient(
83-
documentintelligence_endpoint, AzureKeyCredential(documentintelligence_api_key)
77+
documentintelligence_endpoint, get_credential()
8478
)
8579
di_client = DocumentIntelligenceClient(
86-
documentintelligence_endpoint, AzureKeyCredential(documentintelligence_api_key)
80+
documentintelligence_endpoint, get_credential()
8781
)
8882

8983
recorded_variables = kwargs.pop("variables", {})

sdk/documentintelligence/azure-ai-documentintelligence/tests/test_dac_analyze_custom_model_async.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
import pytest
88
import uuid
99
from devtools_testutils.aio import recorded_by_proxy_async
10-
from devtools_testutils import set_bodiless_matcher
11-
from azure.core.credentials import AzureKeyCredential
10+
from devtools_testutils import set_bodiless_matcher, get_credential
1211
from azure.core.exceptions import ResourceNotFoundError
1312
from azure.ai.documentintelligence.aio import DocumentIntelligenceClient, DocumentIntelligenceAdministrationClient
1413
from azure.ai.documentintelligence.models import (
@@ -26,9 +25,8 @@ class TestDACAnalyzeCustomModelAsync(AsyncDocumentIntelligenceTest):
2625
@recorded_by_proxy_async
2726
async def test_analyze_document_none_model_id(self, **kwargs):
2827
documentintelligence_endpoint = kwargs.pop("documentintelligence_endpoint")
29-
documentintelligence_api_key = kwargs.pop("documentintelligence_api_key")
3028
client = DocumentIntelligenceClient(
31-
documentintelligence_endpoint, AzureKeyCredential(documentintelligence_api_key)
29+
documentintelligence_endpoint, get_credential(is_async=True)
3230
)
3331
with pytest.raises(ValueError) as e:
3432
async with client:
@@ -41,9 +39,8 @@ async def test_analyze_document_none_model_id(self, **kwargs):
4139
@recorded_by_proxy_async
4240
async def test_analyze_document_none_model_id_from_url(self, **kwargs):
4341
documentintelligence_endpoint = kwargs.pop("documentintelligence_endpoint")
44-
documentintelligence_api_key = kwargs.pop("documentintelligence_api_key")
4542
client = DocumentIntelligenceClient(
46-
documentintelligence_endpoint, AzureKeyCredential(documentintelligence_api_key)
43+
documentintelligence_endpoint, get_credential(is_async=True)
4744
)
4845
with pytest.raises(ValueError) as e:
4946
async with client:
@@ -56,9 +53,8 @@ async def test_analyze_document_none_model_id_from_url(self, **kwargs):
5653
@recorded_by_proxy_async
5754
async def test_analyze_document_empty_model_id(self, **kwargs):
5855
documentintelligence_endpoint = kwargs.pop("documentintelligence_endpoint")
59-
documentintelligence_api_key = kwargs.pop("documentintelligence_api_key")
6056
client = DocumentIntelligenceClient(
61-
documentintelligence_endpoint, AzureKeyCredential(documentintelligence_api_key)
57+
documentintelligence_endpoint, get_credential(is_async=True)
6258
)
6359
with pytest.raises(ResourceNotFoundError) as e:
6460
async with client:
@@ -72,9 +68,8 @@ async def test_analyze_document_empty_model_id(self, **kwargs):
7268
@recorded_by_proxy_async
7369
async def test_analyze_document_empty_model_id_from_url(self, **kwargs):
7470
documentintelligence_endpoint = kwargs.pop("documentintelligence_endpoint")
75-
documentintelligence_api_key = kwargs.pop("documentintelligence_api_key")
7671
client = DocumentIntelligenceClient(
77-
documentintelligence_endpoint, AzureKeyCredential(documentintelligence_api_key)
72+
documentintelligence_endpoint, get_credential(is_async=True)
7873
)
7974
with pytest.raises(ResourceNotFoundError) as e:
8075
async with client:
@@ -89,12 +84,11 @@ async def test_analyze_document_empty_model_id_from_url(self, **kwargs):
8984
async def test_custom_document_transform(self, documentintelligence_storage_container_sas_url, **kwargs):
9085
set_bodiless_matcher()
9186
documentintelligence_endpoint = kwargs.pop("documentintelligence_endpoint")
92-
documentintelligence_api_key = kwargs.pop("documentintelligence_api_key")
9387
di_admin_client = DocumentIntelligenceAdministrationClient(
94-
documentintelligence_endpoint, AzureKeyCredential(documentintelligence_api_key)
88+
documentintelligence_endpoint, get_credential(is_async=True)
9589
)
9690
di_client = DocumentIntelligenceClient(
97-
documentintelligence_endpoint, AzureKeyCredential(documentintelligence_api_key)
91+
documentintelligence_endpoint, get_credential(is_async=True)
9892
)
9993

10094
recorded_variables = kwargs.pop("variables", {})

sdk/documentintelligence/azure-ai-documentintelligence/tests/test_dac_analyze_layout.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
import pytest
88
import functools
9-
from devtools_testutils import recorded_by_proxy
10-
from azure.core.credentials import AzureKeyCredential
9+
from devtools_testutils import recorded_by_proxy, get_credential
1110
from azure.ai.documentintelligence import DocumentIntelligenceClient
1211
from azure.ai.documentintelligence.models import (
1312
DocumentAnalysisFeature,
@@ -217,14 +216,14 @@ def test_layout_url_barcode(self, client):
217216
@skip_flaky_test
218217
@DocumentIntelligencePreparer()
219218
@recorded_by_proxy
220-
def test_polling_interval(self, documentintelligence_endpoint, documentintelligence_api_key, **kwargs):
219+
def test_polling_interval(self, documentintelligence_endpoint, **kwargs):
221220
client = DocumentIntelligenceClient(
222-
documentintelligence_endpoint, AzureKeyCredential(documentintelligence_api_key)
221+
documentintelligence_endpoint, get_credential()
223222
)
224223
assert client._config.polling_interval == 1
225224

226225
client = DocumentIntelligenceClient(
227-
documentintelligence_endpoint, AzureKeyCredential(documentintelligence_api_key), polling_interval=7
226+
documentintelligence_endpoint, get_credential(), polling_interval=7
228227
)
229228
assert client._config.polling_interval == 7
230229
poller = client.begin_analyze_document(

sdk/documentintelligence/azure-ai-documentintelligence/tests/test_dac_analyze_layout_async.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
import pytest
88
import functools
9+
from devtools_testutils import get_credential
910
from devtools_testutils.aio import recorded_by_proxy_async
10-
from azure.core.credentials import AzureKeyCredential
1111
from azure.ai.documentintelligence.aio import DocumentIntelligenceClient
1212
from azure.ai.documentintelligence.models import (
1313
DocumentAnalysisFeature,
@@ -16,7 +16,7 @@
1616
)
1717
from asynctestcase import AsyncDocumentIntelligenceTest
1818
from conftest import skip_flaky_test
19-
from preparers import DocumentIntelligencePreparer, GlobalClientPreparer as _GlobalClientPreparer
19+
from preparers import DocumentIntelligencePreparer, GlobalClientPreparerAsync as _GlobalClientPreparer
2020

2121

2222
DocumentIntelligenceClientPreparer = functools.partial(_GlobalClientPreparer, DocumentIntelligenceClient)
@@ -228,14 +228,14 @@ async def test_layout_url_barcodes(self, client):
228228
@skip_flaky_test
229229
@DocumentIntelligencePreparer()
230230
@recorded_by_proxy_async
231-
async def test_polling_interval(self, documentintelligence_endpoint, documentintelligence_api_key, **kwargs):
231+
async def test_polling_interval(self, documentintelligence_endpoint, **kwargs):
232232
client = DocumentIntelligenceClient(
233-
documentintelligence_endpoint, AzureKeyCredential(documentintelligence_api_key)
233+
documentintelligence_endpoint, get_credential(is_async=True)
234234
)
235235
assert client._config.polling_interval == 1
236236

237237
client = DocumentIntelligenceClient(
238-
documentintelligence_endpoint, AzureKeyCredential(documentintelligence_api_key), polling_interval=7
238+
documentintelligence_endpoint, get_credential(is_async=True), polling_interval=7
239239
)
240240
assert client._config.polling_interval == 7
241241
async with client:

sdk/documentintelligence/azure-ai-documentintelligence/tests/test_dac_classify_document.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
import functools
88
import uuid
9-
from devtools_testutils import set_bodiless_matcher, recorded_by_proxy
10-
from azure.core.credentials import AzureKeyCredential
9+
from devtools_testutils import set_bodiless_matcher, recorded_by_proxy, get_credential
1110
from azure.ai.documentintelligence import DocumentIntelligenceAdministrationClient, DocumentIntelligenceClient
1211
from azure.ai.documentintelligence.models import (
1312
ClassifierDocumentTypeDetails,
@@ -32,12 +31,11 @@ class TestDACClassifyDocumentAsync(DocumentIntelligenceTest):
3231
def test_classify_document(self, documentintelligence_training_data_classifier_sas_url, **kwargs):
3332
set_bodiless_matcher()
3433
documentintelligence_endpoint = kwargs.pop("documentintelligence_endpoint")
35-
documentintelligence_api_key = kwargs.pop("documentintelligence_api_key")
3634
di_client = DocumentIntelligenceClient(
37-
documentintelligence_endpoint, AzureKeyCredential(documentintelligence_api_key)
35+
documentintelligence_endpoint, get_credential()
3836
)
3937
di_admin_client = DocumentIntelligenceAdministrationClient(
40-
documentintelligence_endpoint, AzureKeyCredential(documentintelligence_api_key)
38+
documentintelligence_endpoint, get_credential()
4139
)
4240

4341
recorded_variables = kwargs.pop("variables", {})

sdk/documentintelligence/azure-ai-documentintelligence/tests/test_dac_classify_document_async.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66

77
import functools
88
import uuid
9-
from devtools_testutils import set_bodiless_matcher
9+
from devtools_testutils import set_bodiless_matcher, get_credential
1010
from devtools_testutils.aio import recorded_by_proxy_async
11-
from azure.core.credentials import AzureKeyCredential
1211
from azure.ai.documentintelligence.aio import DocumentIntelligenceAdministrationClient, DocumentIntelligenceClient
1312
from azure.ai.documentintelligence.models import (
1413
ClassifierDocumentTypeDetails,
@@ -18,7 +17,7 @@
1817
)
1918
from asynctestcase import AsyncDocumentIntelligenceTest
2019
from conftest import skip_flaky_test
21-
from preparers import DocumentIntelligencePreparer, GlobalClientPreparer as _GlobalClientPreparer
20+
from preparers import DocumentIntelligencePreparer, GlobalClientPreparerAsync as _GlobalClientPreparer
2221

2322

2423
DocumentModelAdministrationClientPreparer = functools.partial(
@@ -33,12 +32,11 @@ class TestDACClassifyDocumentAsync(AsyncDocumentIntelligenceTest):
3332
async def test_classify_document(self, documentintelligence_training_data_classifier_sas_url, **kwargs):
3433
set_bodiless_matcher()
3534
documentintelligence_endpoint = kwargs.pop("documentintelligence_endpoint")
36-
documentintelligence_api_key = kwargs.pop("documentintelligence_api_key")
3735
di_client = DocumentIntelligenceClient(
38-
documentintelligence_endpoint, AzureKeyCredential(documentintelligence_api_key)
36+
documentintelligence_endpoint, get_credential(is_async=True)
3937
)
4038
di_admin_client = DocumentIntelligenceAdministrationClient(
41-
documentintelligence_endpoint, AzureKeyCredential(documentintelligence_api_key)
39+
documentintelligence_endpoint, get_credential(is_async=True)
4240
)
4341

4442
recorded_variables = kwargs.pop("variables", {})

sdk/documentintelligence/azure-ai-documentintelligence/tests/test_dmac_classifiers_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
BuildDocumentClassifierRequest,
1919
)
2020
from asynctestcase import AsyncDocumentIntelligenceTest
21-
from preparers import DocumentIntelligencePreparer, GlobalClientPreparer as _GlobalClientPreparer
21+
from preparers import DocumentIntelligencePreparer, GlobalClientPreparerAsync as _GlobalClientPreparer
2222

2323
DocumentModelAdministrationClientPreparer = functools.partial(
2424
_GlobalClientPreparer, DocumentIntelligenceAdministrationClient

sdk/documentintelligence/azure-ai-documentintelligence/tests/test_dmac_compose_model_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
)
1818
from asynctestcase import AsyncDocumentIntelligenceTest
1919
from conftest import skip_flaky_test
20-
from preparers import DocumentIntelligencePreparer, GlobalClientPreparer as _GlobalClientPreparer
20+
from preparers import DocumentIntelligencePreparer, GlobalClientPreparerAsync as _GlobalClientPreparer
2121

2222
DocumentModelAdministrationClientPreparer = functools.partial(
2323
_GlobalClientPreparer, DocumentIntelligenceAdministrationClient

0 commit comments

Comments
 (0)