Skip to content

Commit

Permalink
[textanalytics] convert to test proxy (#21696)
Browse files Browse the repository at this point in the history
* try one test

* test proxy initial work

* adding sanitizer and recording, not working in playback yet

* update conftest and testcases

* analyze actions - add new recordings and delete old

* analyze healthcare - add new recordings and delete old

* analyze sentiment - add new recordings and delete old

* auth tests should use get_credential, removes async base test class - not needed

* detect language - add new recordings and delete old

* test encoding - add new recordings and delete old

* extract key phrases - add new recordings and delete old

* recognize entities - add new recordings and delete old

* recognize linked entities - add new recordings and delete old

* recognize pii entities - add new recordings and delete old

* add testproxy config to ci/tests.yml

* delete unused recordings I missed

* rstrip endpoint

* add oauth sanitizer

* fix rstrip endpoint, start the proxy automatically, rerecord a few tests

* try something

* more logging

* update all recordings

* ignore body for tests where we scrub

* rerecord again

* remove from tests.yml
  • Loading branch information
kristapratico authored Dec 15, 2021
1 parent b818787 commit c95194a
Show file tree
Hide file tree
Showing 1,272 changed files with 78,270 additions and 67,392 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ def __init__(self, endpoint, credential, **kwargs):
"opinionMining",
}
)

try:
endpoint = endpoint.rstrip("/")
except AttributeError:
raise ValueError("Parameter 'endpoint' must be a string.")
self._client = _TextAnalyticsClient(
endpoint=endpoint,
credential=credential,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from azure.core.exceptions import (
HttpResponseError,
ClientAuthenticationError,
ResourceNotFoundError,
ODataV4Format,
)
from azure.core.paging import ItemPaged
Expand Down Expand Up @@ -57,6 +58,8 @@ def process_http_response_error(error):
raise_error = HttpResponseError
if error.status_code == 401:
raise_error = ClientAuthenticationError
if error.status_code == 404:
raise_error = ResourceNotFoundError
raise raise_error(response=error.response, error_format=CSODataV4Format)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ def __init__(self, endpoint, credential, **kwargs):
"opinionMining",
}
)

try:
endpoint = endpoint.rstrip("/")
except AttributeError:
raise ValueError("Parameter 'endpoint' must be a string.")
self._client = _TextAnalyticsClient(
endpoint=endpoint,
credential=credential,
Expand Down
40 changes: 0 additions & 40 deletions sdk/textanalytics/azure-ai-textanalytics/tests/asynctestcase.py

This file was deleted.

18 changes: 18 additions & 0 deletions sdk/textanalytics/azure-ai-textanalytics/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,26 @@
# --------------------------------------------------------------------------

import sys
import pytest
from devtools_testutils import (
test_proxy,
add_remove_header_sanitizer,
add_general_regex_sanitizer,
add_oauth_response_sanitizer,
)


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


@pytest.fixture(scope="session", autouse=True)
def add_sanitizers(test_proxy):
add_remove_header_sanitizer(headers="Ocp-Apim-Subscription-Key")
add_general_regex_sanitizer(
value="fakeendpoint",
regex="(?<=\\/\\/)[a-z-]+(?=\\.cognitiveservices\\.azure\\.com)"
)
add_oauth_response_sanitizer()
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
{
"Entries": [
{
"RequestUri": "https://fakeendpoint.cognitiveservices.azure.com/text/analytics/v3.2-preview.2/analyze",
"RequestMethod": "POST",
"RequestHeaders": {
"Accept": "application/json, text/json",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"Content-Length": "2177",
"Content-Type": "application/json",
"User-Agent": "azsdk-python-ai-textanalytics/5.2.0b3 Python/3.10.0 (Windows-10-10.0.22000-SP0)",
"x-ms-client-request-id": "25293ed1-5d0f-11ec-aac3-b831b58100e8"
},
"RequestBody": "{\u0022tasks\u0022: {\u0022entityRecognitionTasks\u0022: [], \u0022entityRecognitionPiiTasks\u0022: [], \u0022keyPhraseExtractionTasks\u0022: [], \u0022entityLinkingTasks\u0022: [], \u0022sentimentAnalysisTasks\u0022: [], \u0022extractiveSummarizationTasks\u0022: [{\u0022parameters\u0022: {\u0022stringIndexType\u0022: \u0022UnicodeCodePoint\u0022}, \u0022taskName\u0022: \u00220\u0022}], \u0022customEntityRecognitionTasks\u0022: [], \u0022customSingleClassificationTasks\u0022: [], \u0022customMultiClassificationTasks\u0022: []}, \u0022analysisInput\u0022: {\u0022documents\u0022: [{\u0022id\u0022: \u00221\u0022, \u0022text\u0022: \u0022The government of British Prime Minster Theresa May has been plunged into turmoil with the resignation of two senior Cabinet ministers in a deep split over her Brexit strategy. The Foreign Secretary Boris Johnson, quit on Monday, hours after the resignation late on Sunday night of the minister in charge of Brexit negotiations, David Davis. Their decision to leave the government came three days after May appeared to have agreed a deal with herfractured Cabinet on the UK\u0027s post Brexit relationship with the EU. That plan is now in tatters and her political future appears uncertain. May appeared in Parliament on Monday afternoon to defend her plan, minutes after Downing Street confirmed the departure of Johnson. May acknowledged the splits in her statement to MPs, saying of the ministers who quit: We do not agree about the best way of delivering our shared commitment to honoring the result of the referendum. The Prime Minister\u0027s latest plitical drama began late on Sunday night when Davis quit, declaring he could not support May\u0027s Brexit plan. He said it involved too close a relationship with the EU and gave only an illusion of control being returned to the UK after it left the EU. It seems to me we\u0027re giving too much away, too easily, and that\u0027s a dangerous strategy at this time, Davis said in a BBC radio interview Monday morning. Johnson\u0027s resignation came Monday afternoon local time, just before the Prime Minister was due to make a scheduled statement in Parliament. This afternoon, the Prime Minister accepted the resignation of Boris Johnson as Foreign Secretary, a statement from Downing Street said.\u0022, \u0022language\u0022: \u0022en\u0022}, {\u0022id\u0022: \u00222\u0022, \u0022text\u0022: \u0022Microsoft fue fundado por Bill Gates y Paul Allen\u0022, \u0022language\u0022: \u0022es\u0022}]}}",
"StatusCode": 202,
"ResponseHeaders": {
"apim-request-id": "a03b6b25-f013-4368-bd31-ad425c9179ac",
"Date": "Tue, 14 Dec 2021 18:53:37 GMT",
"operation-location": "https://fakeendpoint.cognitiveservices.azure.com/text/analytics/v3.2-preview.2/analyze/jobs/0a0d1391-60f6-4b43-8d4f-41abe9f6f861",
"Strict-Transport-Security": "max-age=31536000; includeSubDomains; preload",
"Transfer-Encoding": "chunked",
"X-Content-Type-Options": "nosniff",
"x-envoy-upstream-service-time": "217"
},
"ResponseBody": null
},
{
"RequestUri": "https://fakeendpoint.cognitiveservices.azure.com/text/analytics/v3.2-preview.2/analyze/jobs/0a0d1391-60f6-4b43-8d4f-41abe9f6f861?showStats=True",
"RequestMethod": "GET",
"RequestHeaders": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"User-Agent": "azsdk-python-ai-textanalytics/5.2.0b3 Python/3.10.0 (Windows-10-10.0.22000-SP0)",
"x-ms-client-request-id": "25293ed1-5d0f-11ec-aac3-b831b58100e8"
},
"RequestBody": null,
"StatusCode": 200,
"ResponseHeaders": {
"apim-request-id": "3fa18449-f763-4f73-b925-eae5942bf462",
"Content-Type": "application/json; charset=utf-8",
"Date": "Tue, 14 Dec 2021 18:53:42 GMT",
"Strict-Transport-Security": "max-age=31536000; includeSubDomains; preload",
"Transfer-Encoding": "chunked",
"X-Content-Type-Options": "nosniff",
"x-envoy-upstream-service-time": "114"
},
"ResponseBody": {
"jobId": "0a0d1391-60f6-4b43-8d4f-41abe9f6f861",
"lastUpdateDateTime": "2021-12-14T18:53:41Z",
"createdDateTime": "2021-12-14T18:53:37Z",
"expirationDateTime": "2021-12-15T18:53:37Z",
"status": "succeeded",
"errors": [],
"tasks": {
"completed": 1,
"failed": 0,
"inProgress": 0,
"total": 1,
"extractiveSummarizationTasks": [
{
"lastUpdateDateTime": "2021-12-14T18:53:41.3509318Z",
"taskName": "0",
"state": "succeeded",
"results": {
"statistics": {
"documentsCount": 2,
"validDocumentsCount": 2,
"erroneousDocumentsCount": 0,
"transactionsCount": 3
},
"documents": [
{
"id": "1",
"statistics": {
"charactersCount": 1625,
"transactionsCount": 2
},
"sentences": [
{
"text": "The government of British Prime Minster Theresa May has been plunged into turmoil with the resignation of two senior Cabinet ministers in a deep split over her Brexit strategy.",
"rankScore": 0.57,
"offset": 0,
"length": 176
},
{
"text": "The Foreign Secretary Boris Johnson, quit on Monday, hours after the resignation late on Sunday night of the minister in charge of Brexit negotiations, David Davis.",
"rankScore": 1.0,
"offset": 177,
"length": 164
},
{
"text": "Their decision to leave the government came three days after May appeared to have agreed a deal with herfractured Cabinet on the UK\u0027s post Brexit relationship with the EU.",
"rankScore": 0.47,
"offset": 342,
"length": 171
}
],
"warnings": []
},
{
"id": "2",
"statistics": {
"charactersCount": 49,
"transactionsCount": 1
},
"sentences": [
{
"text": "Microsoft fue fundado por Bill Gates y Paul Allen",
"rankScore": 1.0,
"offset": 0,
"length": 49
}
],
"warnings": []
}
],
"errors": [],
"modelVersion": "2021-08-01"
}
}
]
}
}
}
],
"Variables": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
{
"Entries": [
{
"RequestUri": "https://fakeendpoint.cognitiveservices.azure.com/text/analytics/v3.2-preview.2/analyze",
"RequestMethod": "POST",
"RequestHeaders": {
"Accept": "application/json, text/json",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"Content-Length": "565",
"Content-Type": "application/json",
"User-Agent": "azsdk-python-ai-textanalytics/5.2.0b3 Python/3.10.0 (Windows-10-10.0.22000-SP0)",
"x-ms-client-request-id": "ad73a7fb-5d0e-11ec-8699-b831b58100e8"
},
"RequestBody": "{\u0022tasks\u0022: {\u0022entityRecognitionTasks\u0022: [], \u0022entityRecognitionPiiTasks\u0022: [], \u0022keyPhraseExtractionTasks\u0022: [{\u0022parameters\u0022: {}, \u0022taskName\u0022: \u00220\u0022}], \u0022entityLinkingTasks\u0022: [], \u0022sentimentAnalysisTasks\u0022: [], \u0022extractiveSummarizationTasks\u0022: [], \u0022customEntityRecognitionTasks\u0022: [], \u0022customSingleClassificationTasks\u0022: [], \u0022customMultiClassificationTasks\u0022: []}, \u0022analysisInput\u0022: {\u0022documents\u0022: [{\u0022id\u0022: \u00221\u0022, \u0022text\u0022: \u0022Microsoft was founded by Bill Gates and Paul Allen\u0022, \u0022language\u0022: \u0022en\u0022}, {\u0022id\u0022: \u00222\u0022, \u0022text\u0022: \u0022Microsoft fue fundado por Bill Gates y Paul Allen\u0022, \u0022language\u0022: \u0022es\u0022}]}}",
"StatusCode": 202,
"ResponseHeaders": {
"apim-request-id": "7c5ba259-3eda-456a-9f46-a35de4ba0e09",
"Date": "Tue, 14 Dec 2021 18:50:17 GMT",
"operation-location": "https://fakeendpoint.cognitiveservices.azure.com/text/analytics/v3.2-preview.2/analyze/jobs/307a5fae-a297-46da-b37d-bc28cf04eb24",
"Strict-Transport-Security": "max-age=31536000; includeSubDomains; preload",
"Transfer-Encoding": "chunked",
"X-Content-Type-Options": "nosniff",
"x-envoy-upstream-service-time": "318"
},
"ResponseBody": null
},
{
"RequestUri": "https://fakeendpoint.cognitiveservices.azure.com/text/analytics/v3.2-preview.2/analyze/jobs/307a5fae-a297-46da-b37d-bc28cf04eb24?showStats=True",
"RequestMethod": "GET",
"RequestHeaders": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"User-Agent": "azsdk-python-ai-textanalytics/5.2.0b3 Python/3.10.0 (Windows-10-10.0.22000-SP0)",
"x-ms-client-request-id": "ad73a7fb-5d0e-11ec-8699-b831b58100e8"
},
"RequestBody": null,
"StatusCode": 200,
"ResponseHeaders": {
"apim-request-id": "2b4749c7-31e2-4381-adb7-9a1a7c146612",
"Content-Type": "application/json; charset=utf-8",
"Date": "Tue, 14 Dec 2021 18:50:22 GMT",
"Strict-Transport-Security": "max-age=31536000; includeSubDomains; preload",
"Transfer-Encoding": "chunked",
"X-Content-Type-Options": "nosniff",
"x-envoy-upstream-service-time": "125"
},
"ResponseBody": {
"jobId": "307a5fae-a297-46da-b37d-bc28cf04eb24",
"lastUpdateDateTime": "2021-12-14T18:50:22Z",
"createdDateTime": "2021-12-14T18:50:17Z",
"expirationDateTime": "2021-12-15T18:50:17Z",
"status": "succeeded",
"errors": [],
"tasks": {
"completed": 1,
"failed": 0,
"inProgress": 0,
"total": 1,
"keyPhraseExtractionTasks": [
{
"lastUpdateDateTime": "2021-12-14T18:50:22.069458Z",
"taskName": "0",
"state": "succeeded",
"results": {
"statistics": {
"documentsCount": 2,
"validDocumentsCount": 2,
"erroneousDocumentsCount": 0,
"transactionsCount": 2
},
"documents": [
{
"id": "1",
"keyPhrases": [
"Bill Gates",
"Paul Allen",
"Microsoft"
],
"statistics": {
"charactersCount": 50,
"transactionsCount": 1
},
"warnings": []
},
{
"id": "2",
"keyPhrases": [
"Bill Gates",
"Paul Allen",
"Microsoft"
],
"statistics": {
"charactersCount": 49,
"transactionsCount": 1
},
"warnings": []
}
],
"errors": [],
"modelVersion": "2021-06-01"
}
}
]
}
}
}
],
"Variables": {}
}
Loading

0 comments on commit c95194a

Please sign in to comment.