Skip to content

Commit

Permalink
Adding test cases for the new model and operation (Azure#37894)
Browse files Browse the repository at this point in the history
* Adding test cases for the new model and operation. Also updating test-resource file

* Enabling Key based auth on storage account

* Updating assets file

* Updating DT assets commit ID

* Uploaded new test case recording to assets repo

* Updated recording for start_translation_details test case(Accept-Encoding value - removed br)

* Addressing review comments.
  • Loading branch information
hamshavathimunibyraiah authored Oct 16, 2024
1 parent 0934c22 commit 231cc64
Show file tree
Hide file tree
Showing 24 changed files with 638 additions and 474 deletions.
2 changes: 1 addition & 1 deletion sdk/translation/azure-ai-translation-document/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "python",
"TagPrefix": "python/translation/azure-ai-translation-document",
"Tag": "python/translation/azure-ai-translation-document_cf5e8aa525"
"Tag": "python/translation/azure-ai-translation-document_c043fc2ede"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Licensed under the MIT License.
# ------------------------------------

from azure.ai.translation.document.models import BatchRequest, SourceInput, StartTranslationDetails
from devtools_testutils import AzureRecordedTestCase
from testcase import DocumentTranslationTest, Document
from azure.ai.translation.document import DocumentTranslationInput, TranslationTarget
Expand Down Expand Up @@ -104,3 +105,39 @@ async def _begin_and_validate_translation_with_multiple_docs_async(self, async_c
# validate
self._validate_translation_metadata(poller=poller)
return poller

async def _prepare_and_validate_start_translation_details_async(self, async_client, docs_count, **kwargs):
# get input params
wait_for_operation = kwargs.pop("wait", False)
variables = kwargs.get("variables", {})
language = kwargs.pop("language", "es")

# prepare test data
blob_data = Document.create_dummy_docs(docs_count=docs_count)
source_input = SourceInput(
source_url=self.create_source_container(data=blob_data, variables=variables)
)
target = TranslationTarget(
target_url=self.create_target_container(variables=variables),
language=language
)
batch_request = BatchRequest(
source=source_input,
targets=[target]
)
start_translation_details = StartTranslationDetails(
inputs=[batch_request]
)

# submit job
poller = await async_client.begin_translation(start_translation_details)
assert poller.id is not None
# wait for result
if wait_for_operation:
result = await poller.result()
async for doc in result:
self._validate_doc_status(doc, "es")

# validate
self._validate_translation_metadata(poller=poller)
return poller
17 changes: 3 additions & 14 deletions sdk/translation/azure-ai-translation-document/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
add_general_regex_sanitizer,
add_oauth_response_sanitizer,
remove_batch_sanitizers,
set_custom_default_matcher,
)
from azure.storage.blob import BlobServiceClient

Expand All @@ -27,22 +28,10 @@ def add_sanitizers(test_proxy):
regex="(?<=\\/\\/)[a-z]+(?=(?:|-secondary)\\.(?:table|blob|queue)\\.core\\.windows\\.net)",
value="fakeendpoint",
)
set_custom_default_matcher(ignored_headers="Accept-Encoding")
add_oauth_response_sanitizer()

# Remove the following sanitizers since certain fields are needed in tests and are non-sensitive:
# - AZSDK3430: $..id
# - AZSDK3424: $..to
remove_batch_sanitizers(["AZSDK3430", "AZSDK3424"])

# run tests
yield

# Dogfood env uses a static storage account so we clean up the blob resources
# This is unnecessary for AzureCloud where each storage account is deleted at the end of testing
if is_live() and os.getenv("TRANSLATION_ENVIRONMENT") == "Dogfood":
client = BlobServiceClient(
"https://" + os.getenv("TRANSLATION_DOCUMENT_STORAGE_NAME") + ".blob.core.windows.net/",
os.getenv("TRANSLATION_DOCUMENT_STORAGE_KEY"),
)
for container in client.list_containers():
client.delete_container(container)
remove_batch_sanitizers(["AZSDK3430", "AZSDK3424", "AZSDK4001"])
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ Start by creating a new virtual environment for your perf tests. This will need
The following environment variable will need to be set for the tests to access the live resources:

```
TRANSLATION_DOCUMENT_TEST_ENDPOINT=<translation-endpoint>
TRANSLATION_DOCUMENT_STORAGE_NAME=<storage-blob-account-name>
TRANSLATION_DOCUMENT_STORAGE_KEY=<storage-shared-key>
DOCUMENT_TRANSLATION_ENDPOINT=<translation-endpoint>
DOCUMENT_TRANSLATION_STORAGE_NAME=<storage-blob-account-name>
DOCUMENT_TRANSLATION_STORAGE_KEY=<storage-shared-key>
```

### Setup for perf test runs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def __init__(self, arguments):
super().__init__(arguments)

# test related env vars
endpoint = os.environ["TRANSLATION_DOCUMENT_TEST_ENDPOINT"]
self.storage_name = os.environ["TRANSLATION_DOCUMENT_STORAGE_NAME"]
self.storage_key = os.environ["TRANSLATION_DOCUMENT_STORAGE_KEY"]
endpoint = os.environ["DOCUMENT_TRANSLATION_ENDPOINT"]
self.storage_name = os.environ["DOCUMENT_TRANSLATION_STORAGE_NAME"]
self.storage_key = os.environ["DOCUMENT_TRANSLATION_STORAGE_KEY"]
self.storage_endpoint = "https://" + self.storage_name + ".blob.core.windows.net/"
self.source_container_name = "source-perf-" + str(uuid.uuid4())
self.target_container_name = "target-perf-" + str(uuid.uuid4())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
DocumentTranslationPreparer = functools.partial(
PowerShellPreparer,
"translation",
translation_document_test_endpoint="https://fakeendpoint.cognitiveservices.azure.com",
translation_document_name="redacted",
translation_document_storage_name="redacted",
translation_document_storage_key="fakeZmFrZV9hY29jdW50X2tleQ==",
document_translation_endpoint="https://fakeendpoint.cognitiveservices.azure.com",
document_translation_storage_name="fakeendpoint",
document_translation_storage_key="fakeZmFrZV9hY29jdW50X2tleQ==",
)


Expand All @@ -26,7 +25,7 @@ def __init__(self, client_cls, client_kwargs={}, **kwargs):
self.client_cls = client_cls

def create_resource(self, name, **kwargs):
doctranslation_test_endpoint = kwargs.get("translation_document_test_endpoint")
doctranslation_test_endpoint = kwargs.get("document_translation_endpoint")

# set polling interval to 0 for recorded tests
if not self.is_live:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------

import datetime
import functools
from azure.ai.translation.document.models import DocumentStatus, FileFormatType, StatusSummary, TranslationGlossary, TranslationStatus
from testcase import DocumentTranslationTest
from preparer import (
DocumentTranslationPreparer,
DocumentTranslationClientPreparer as _DocumentTranslationClientPreparer,
)
from devtools_testutils import recorded_by_proxy
from azure.ai.translation.document import DocumentTranslationClient, DocumentTranslationInput, TranslationTarget

DocumentTranslationClientPreparer = functools.partial(_DocumentTranslationClientPreparer, DocumentTranslationClient)

class TestModelUpdates(DocumentTranslationTest):
@DocumentTranslationPreparer()
@DocumentTranslationClientPreparer()
@recorded_by_proxy
def test_start_translation_details_model(self, **kwargs):
client = kwargs.pop("client")
variables = kwargs.pop("variables", {})

docs_count = 2
self._prepare_and_validate_start_translation_details(client, docs_count, wait=False, variables=variables)
return variables

@DocumentTranslationPreparer()
@DocumentTranslationClientPreparer()
@recorded_by_proxy
def test_document_translation_input_args(self, **kwargs):
# Creating an instance using required positional arguments
source_container_url = "https://t7d8641d8f25ec940prim.blob.core.windows.net/source-12345"
target_container_url = "https://t7d8641d8f25ec940prim.blob.core.windows.net/target-67890"
doc_input_positional = DocumentTranslationInput(
source_url=source_container_url,
targets=[TranslationTarget(target_url=target_container_url, language="fr")]
)
assert doc_input_positional is not None
assert doc_input_positional.source_url is not None
assert doc_input_positional.targets and doc_input_positional.targets[0].target_url and doc_input_positional.targets[0].language is not None

# Using keyword-only arguments to specify additional optional parameters
doc_input_keyword = DocumentTranslationInput(
source_container_url,
[TranslationTarget(target_url=target_container_url, language="fr")],
source_language="en",
storage_type="FOLDER",
storage_source="AzureBlob",
prefix="start_",
suffix="_end"
)
self.validate_document_translation(doc_input_keyword)

# Creating an instance using a dictionary to pass parameters
params = {
"source_url": source_container_url,
"targets": [TranslationTarget(target_url=target_container_url, language="fr")],
"source_language": "en",
"storage_type": "FOLDER",
"storage_source": "AzureBlob",
"prefix": "start_",
"suffix": "_end"
}
doc_input_dict = DocumentTranslationInput(**params)
self.validate_document_translation(doc_input_dict)

@DocumentTranslationPreparer()
@DocumentTranslationClientPreparer()
@recorded_by_proxy
def test_translation_target_args(self, **kwargs):
# Creating an instance using required positional arguments
target_positional = TranslationTarget(
target_url="https://t7d8641d8f25ec940prim.blob.core.windows.net/target-67890",
language="es"
)
assert target_positional is not None
assert target_positional.target_url is not None
assert target_positional.language is not None

# Using keyword arguments to specify additional optional parameters
target_keyword = TranslationTarget(
target_url="https://t7d8641d8f25ec940prim.blob.core.windows.net/target-67890",
language="es",
category_id="general",
glossaries=[TranslationGlossary(glossary_url="https://glossaryfile.txt", file_format="txt")],
storage_source="AzureBlob"
)
self.validate_translation_target(target_keyword)

# Creating an instance using a dictionary to pass parameters
params = {
"target_url": "https://t7d8641d8f25ec940prim.blob.core.windows.net/target-67890",
"language": "es",
"category_id": "general",
"glossaries": [TranslationGlossary(glossary_url="https://glossaryfile.txt", file_format="txt")],
"storage_source": "AzureBlob"
}
target_dict = TranslationTarget(**params)
self.validate_translation_target(target_dict)

@DocumentTranslationPreparer()
@DocumentTranslationClientPreparer()
@recorded_by_proxy
def test_translation_glossary_args(self, **kwargs):
# Creating an instance using required positional arguments
glossary_positional = TranslationGlossary(
glossary_url="https://glossaryfile.txt",
file_format="txt"
)
assert glossary_positional is not None
assert glossary_positional.glossary_url is not None
assert glossary_positional.file_format is not None

# Using keyword arguments to specify additional optional parameters
glossary_keyword = TranslationGlossary(
glossary_url="https://glossaryfile.txt",
file_format="txt",
format_version="1.0",
storage_source="AzureBlob"
)
self.validate_translation_glossary(glossary_keyword)

# Creating an instance using a dictionary to pass parameters
params = {
"glossary_url": "https://glossaryfile.txt",
"file_format": "txt",
"format_version": "1.0",
"storage_source": "AzureBlob"
}
glossary_dict = TranslationGlossary(**params)
self.validate_translation_glossary(glossary_dict)

@DocumentTranslationPreparer()
@DocumentTranslationClientPreparer()
@recorded_by_proxy
def test_document_status_args(self, **kwargs):
# Using keyword arguments to specify additional optional parameters
document_status_keyword = DocumentStatus(
source_document_url="https://t7d8641d8f25ec940prim.blob.core.windows.net/source-12345/document.txt",
created_on=datetime.datetime.now(),
last_updated_on=datetime.datetime.now(),
status="Running",
translated_to="es",
translation_progress=0.5,
id="fd57e619-d7b2-48b7-81cf-24b76e002a8f",
translated_document_url="https://t7d8641d8f25ec940prim.blob.core.windows.net/target-67890/document.txt",
error=None,
characters_charged=1000
)
self.validate_document_status(document_status_keyword)

# Creating an instance using a dictionary to pass parameters
params = {
"source_document_url": "https://t7d8641d8f25ec940prim.blob.core.windows.net/source-12345/document.txt",
"created_on": datetime.datetime.now(),
"last_updated_on": datetime.datetime.now(),
"status": "Succeeded",
"translated_to": "fr",
"translation_progress": 1.0,
"id": "fd57e619-d7b2-48b7-81cf-24b76e002a8f",
"translated_document_url": "https://t7d8641d8f25ec940prim.blob.core.windows.net/target-67890/document.txt",
"error": None,
"characters_charged": 2000
}
document_status_dict = DocumentStatus(**params)
self.validate_document_status(document_status_dict)

@DocumentTranslationPreparer()
@DocumentTranslationClientPreparer()
@recorded_by_proxy
def test_translation_status_args(self, **kwargs):
# Using keyword-only arguments to specify additional optional parameters
status_summary = StatusSummary(
total=10,
failed=2,
success=5,
in_progress=3, # Note the naming matches the class definition
not_yet_started=0,
canceled=0,
total_characters_charged=10000
)
translation_status_keyword = TranslationStatus(
id="fd57e619-d7b2-48b7-81cf-24b76e002a8f",
created_on=datetime.datetime.now(),
last_updated_on=datetime.datetime.now(),
status="Succeeded",
summary=status_summary,
error=None
)
self.validate_translation_status(translation_status_keyword)

# Creating an instance using a dictionary to pass parameters
params = {
"id": "fd57e619-d7b2-48b7-81cf-24b76e002a8f",
"created_on": datetime.datetime.now(),
"last_updated_on": datetime.datetime.now(),
"status": "Succeeded",
"summary": status_summary,
"error": None
}
translation_status_dict = TranslationStatus(**params)
self.validate_translation_status(translation_status_dict)

def validate_translation_target(self, translation_target):
assert translation_target is not None
assert translation_target.target_url is not None
assert translation_target.language is not None
assert translation_target.category_id is not None
assert translation_target.glossaries and translation_target.glossaries[0].glossary_url and translation_target.glossaries[0].file_format is not None
assert translation_target.storage_source is not None

def validate_document_translation(self, document_translation):
assert document_translation is not None
assert document_translation.source_url is not None
assert document_translation.targets and document_translation.targets[0].target_url and document_translation.targets[0].language is not None
assert document_translation.source_language is not None
assert document_translation.storage_type is not None
assert document_translation.storage_source is not None
assert document_translation.prefix is not None
assert document_translation.suffix is not None

def validate_translation_glossary(self, translation_glossary):
assert translation_glossary is not None
assert translation_glossary.glossary_url is not None
assert translation_glossary.file_format is not None
assert translation_glossary.format_version is not None
assert translation_glossary.storage_source is not None

def validate_document_status(self, document_status):
assert document_status is not None
assert document_status.source_document_url is not None
assert document_status.created_on is not None
assert document_status.last_updated_on is not None
assert document_status.status is not None
assert document_status.translated_to is not None
assert document_status.translation_progress is not None
assert document_status.id is not None
assert document_status.translated_document_url is not None
assert document_status.characters_charged is not None

def validate_translation_status(self, translation_status):
assert translation_status is not None
assert translation_status.id is not None
assert translation_status.created_on is not None
assert translation_status.last_updated_on is not None
assert translation_status.status is not None
assert translation_status.summary is not None

# verifying old attributes
assert translation_status is not None
assert translation_status.documents_total_count is not None
assert translation_status.documents_failed_count is not None
assert translation_status.documents_in_progress_count is not None
assert translation_status.documents_succeeded_count is not None
assert translation_status.documents_not_started_count is not None
assert translation_status.documents_canceled_count is not None
assert translation_status.total_characters_charged is not None
Loading

0 comments on commit 231cc64

Please sign in to comment.