|
| 1 | +# ------------------------------------ |
| 2 | +# Copyright (c) Microsoft Corporation. |
| 3 | +# Licensed under the MIT License. |
| 4 | +# ------------------------------------ |
| 5 | + |
| 6 | +import datetime |
| 7 | +import functools |
| 8 | +from azure.ai.translation.document.models import DocumentStatus, FileFormatType, StatusSummary, TranslationGlossary, TranslationStatus |
| 9 | +from testcase import DocumentTranslationTest |
| 10 | +from preparer import ( |
| 11 | + DocumentTranslationPreparer, |
| 12 | + DocumentTranslationClientPreparer as _DocumentTranslationClientPreparer, |
| 13 | +) |
| 14 | +from devtools_testutils import recorded_by_proxy |
| 15 | +from azure.ai.translation.document import DocumentTranslationClient, DocumentTranslationInput, TranslationTarget |
| 16 | + |
| 17 | +DocumentTranslationClientPreparer = functools.partial(_DocumentTranslationClientPreparer, DocumentTranslationClient) |
| 18 | + |
| 19 | +class TestModelUpdates(DocumentTranslationTest): |
| 20 | + @DocumentTranslationPreparer() |
| 21 | + @DocumentTranslationClientPreparer() |
| 22 | + @recorded_by_proxy |
| 23 | + def test_start_translation_details_model(self, **kwargs): |
| 24 | + client = kwargs.pop("client") |
| 25 | + variables = kwargs.pop("variables", {}) |
| 26 | + |
| 27 | + docs_count = 2 |
| 28 | + self._prepare_and_validate_start_translation_details(client, docs_count, wait=False, variables=variables) |
| 29 | + return variables |
| 30 | + |
| 31 | + @DocumentTranslationPreparer() |
| 32 | + @DocumentTranslationClientPreparer() |
| 33 | + @recorded_by_proxy |
| 34 | + def test_document_translation_input_args(self, **kwargs): |
| 35 | + # Creating an instance using required positional arguments |
| 36 | + source_container_url = "https://t7d8641d8f25ec940prim.blob.core.windows.net/source-12345" |
| 37 | + target_container_url = "https://t7d8641d8f25ec940prim.blob.core.windows.net/target-67890" |
| 38 | + doc_input_positional = DocumentTranslationInput( |
| 39 | + source_url=source_container_url, |
| 40 | + targets=[TranslationTarget(target_url=target_container_url, language="fr")] |
| 41 | + ) |
| 42 | + assert doc_input_positional is not None |
| 43 | + assert doc_input_positional.source_url is not None |
| 44 | + assert doc_input_positional.targets and doc_input_positional.targets[0].target_url and doc_input_positional.targets[0].language is not None |
| 45 | + |
| 46 | + # Using keyword-only arguments to specify additional optional parameters |
| 47 | + doc_input_keyword = DocumentTranslationInput( |
| 48 | + source_container_url, |
| 49 | + [TranslationTarget(target_url=target_container_url, language="fr")], |
| 50 | + source_language="en", |
| 51 | + storage_type="FOLDER", |
| 52 | + storage_source="AzureBlob", |
| 53 | + prefix="start_", |
| 54 | + suffix="_end" |
| 55 | + ) |
| 56 | + self.validate_document_translation(doc_input_keyword) |
| 57 | + |
| 58 | + # Creating an instance using a dictionary to pass parameters |
| 59 | + params = { |
| 60 | + "source_url": source_container_url, |
| 61 | + "targets": [TranslationTarget(target_url=target_container_url, language="fr")], |
| 62 | + "source_language": "en", |
| 63 | + "storage_type": "FOLDER", |
| 64 | + "storage_source": "AzureBlob", |
| 65 | + "prefix": "start_", |
| 66 | + "suffix": "_end" |
| 67 | + } |
| 68 | + doc_input_dict = DocumentTranslationInput(**params) |
| 69 | + self.validate_document_translation(doc_input_dict) |
| 70 | + |
| 71 | + @DocumentTranslationPreparer() |
| 72 | + @DocumentTranslationClientPreparer() |
| 73 | + @recorded_by_proxy |
| 74 | + def test_translation_target_args(self, **kwargs): |
| 75 | + # Creating an instance using required positional arguments |
| 76 | + target_positional = TranslationTarget( |
| 77 | + target_url="https://t7d8641d8f25ec940prim.blob.core.windows.net/target-67890", |
| 78 | + language="es" |
| 79 | + ) |
| 80 | + assert target_positional is not None |
| 81 | + assert target_positional.target_url is not None |
| 82 | + assert target_positional.language is not None |
| 83 | + |
| 84 | + # Using keyword arguments to specify additional optional parameters |
| 85 | + target_keyword = TranslationTarget( |
| 86 | + target_url="https://t7d8641d8f25ec940prim.blob.core.windows.net/target-67890", |
| 87 | + language="es", |
| 88 | + category_id="general", |
| 89 | + glossaries=[TranslationGlossary(glossary_url="https://glossaryfile.txt", file_format="txt")], |
| 90 | + storage_source="AzureBlob" |
| 91 | + ) |
| 92 | + self.validate_translation_target(target_keyword) |
| 93 | + |
| 94 | + # Creating an instance using a dictionary to pass parameters |
| 95 | + params = { |
| 96 | + "target_url": "https://t7d8641d8f25ec940prim.blob.core.windows.net/target-67890", |
| 97 | + "language": "es", |
| 98 | + "category_id": "general", |
| 99 | + "glossaries": [TranslationGlossary(glossary_url="https://glossaryfile.txt", file_format="txt")], |
| 100 | + "storage_source": "AzureBlob" |
| 101 | + } |
| 102 | + target_dict = TranslationTarget(**params) |
| 103 | + self.validate_translation_target(target_dict) |
| 104 | + |
| 105 | + @DocumentTranslationPreparer() |
| 106 | + @DocumentTranslationClientPreparer() |
| 107 | + @recorded_by_proxy |
| 108 | + def test_translation_glossary_args(self, **kwargs): |
| 109 | + # Creating an instance using required positional arguments |
| 110 | + glossary_positional = TranslationGlossary( |
| 111 | + glossary_url="https://glossaryfile.txt", |
| 112 | + file_format="txt" |
| 113 | + ) |
| 114 | + assert glossary_positional is not None |
| 115 | + assert glossary_positional.glossary_url is not None |
| 116 | + assert glossary_positional.file_format is not None |
| 117 | + |
| 118 | + # Using keyword arguments to specify additional optional parameters |
| 119 | + glossary_keyword = TranslationGlossary( |
| 120 | + glossary_url="https://glossaryfile.txt", |
| 121 | + file_format="txt", |
| 122 | + format_version="1.0", |
| 123 | + storage_source="AzureBlob" |
| 124 | + ) |
| 125 | + self.validate_translation_glossary(glossary_keyword) |
| 126 | + |
| 127 | + # Creating an instance using a dictionary to pass parameters |
| 128 | + params = { |
| 129 | + "glossary_url": "https://glossaryfile.txt", |
| 130 | + "file_format": "txt", |
| 131 | + "format_version": "1.0", |
| 132 | + "storage_source": "AzureBlob" |
| 133 | + } |
| 134 | + glossary_dict = TranslationGlossary(**params) |
| 135 | + self.validate_translation_glossary(glossary_dict) |
| 136 | + |
| 137 | + @DocumentTranslationPreparer() |
| 138 | + @DocumentTranslationClientPreparer() |
| 139 | + @recorded_by_proxy |
| 140 | + def test_document_status_args(self, **kwargs): |
| 141 | + # Using keyword arguments to specify additional optional parameters |
| 142 | + document_status_keyword = DocumentStatus( |
| 143 | + source_document_url="https://t7d8641d8f25ec940prim.blob.core.windows.net/source-12345/document.txt", |
| 144 | + created_on=datetime.datetime.now(), |
| 145 | + last_updated_on=datetime.datetime.now(), |
| 146 | + status="Running", |
| 147 | + translated_to="es", |
| 148 | + translation_progress=0.5, |
| 149 | + id="fd57e619-d7b2-48b7-81cf-24b76e002a8f", |
| 150 | + translated_document_url="https://t7d8641d8f25ec940prim.blob.core.windows.net/target-67890/document.txt", |
| 151 | + error=None, |
| 152 | + characters_charged=1000 |
| 153 | + ) |
| 154 | + self.validate_document_status(document_status_keyword) |
| 155 | + |
| 156 | + # Creating an instance using a dictionary to pass parameters |
| 157 | + params = { |
| 158 | + "source_document_url": "https://t7d8641d8f25ec940prim.blob.core.windows.net/source-12345/document.txt", |
| 159 | + "created_on": datetime.datetime.now(), |
| 160 | + "last_updated_on": datetime.datetime.now(), |
| 161 | + "status": "Succeeded", |
| 162 | + "translated_to": "fr", |
| 163 | + "translation_progress": 1.0, |
| 164 | + "id": "fd57e619-d7b2-48b7-81cf-24b76e002a8f", |
| 165 | + "translated_document_url": "https://t7d8641d8f25ec940prim.blob.core.windows.net/target-67890/document.txt", |
| 166 | + "error": None, |
| 167 | + "characters_charged": 2000 |
| 168 | + } |
| 169 | + document_status_dict = DocumentStatus(**params) |
| 170 | + self.validate_document_status(document_status_dict) |
| 171 | + |
| 172 | + @DocumentTranslationPreparer() |
| 173 | + @DocumentTranslationClientPreparer() |
| 174 | + @recorded_by_proxy |
| 175 | + def test_translation_status_args(self, **kwargs): |
| 176 | + # Using keyword-only arguments to specify additional optional parameters |
| 177 | + status_summary = StatusSummary( |
| 178 | + total=10, |
| 179 | + failed=2, |
| 180 | + success=5, |
| 181 | + in_progress=3, # Note the naming matches the class definition |
| 182 | + not_yet_started=0, |
| 183 | + canceled=0, |
| 184 | + total_characters_charged=10000 |
| 185 | + ) |
| 186 | + translation_status_keyword = TranslationStatus( |
| 187 | + id="fd57e619-d7b2-48b7-81cf-24b76e002a8f", |
| 188 | + created_on=datetime.datetime.now(), |
| 189 | + last_updated_on=datetime.datetime.now(), |
| 190 | + status="Succeeded", |
| 191 | + summary=status_summary, |
| 192 | + error=None |
| 193 | + ) |
| 194 | + self.validate_translation_status(translation_status_keyword) |
| 195 | + |
| 196 | + # Creating an instance using a dictionary to pass parameters |
| 197 | + params = { |
| 198 | + "id": "fd57e619-d7b2-48b7-81cf-24b76e002a8f", |
| 199 | + "created_on": datetime.datetime.now(), |
| 200 | + "last_updated_on": datetime.datetime.now(), |
| 201 | + "status": "Succeeded", |
| 202 | + "summary": status_summary, |
| 203 | + "error": None |
| 204 | + } |
| 205 | + translation_status_dict = TranslationStatus(**params) |
| 206 | + self.validate_translation_status(translation_status_dict) |
| 207 | + |
| 208 | + def validate_translation_target(self, translation_target): |
| 209 | + assert translation_target is not None |
| 210 | + assert translation_target.target_url is not None |
| 211 | + assert translation_target.language is not None |
| 212 | + assert translation_target.category_id is not None |
| 213 | + assert translation_target.glossaries and translation_target.glossaries[0].glossary_url and translation_target.glossaries[0].file_format is not None |
| 214 | + assert translation_target.storage_source is not None |
| 215 | + |
| 216 | + def validate_document_translation(self, document_translation): |
| 217 | + assert document_translation is not None |
| 218 | + assert document_translation.source_url is not None |
| 219 | + assert document_translation.targets and document_translation.targets[0].target_url and document_translation.targets[0].language is not None |
| 220 | + assert document_translation.source_language is not None |
| 221 | + assert document_translation.storage_type is not None |
| 222 | + assert document_translation.storage_source is not None |
| 223 | + assert document_translation.prefix is not None |
| 224 | + assert document_translation.suffix is not None |
| 225 | + |
| 226 | + def validate_translation_glossary(self, translation_glossary): |
| 227 | + assert translation_glossary is not None |
| 228 | + assert translation_glossary.glossary_url is not None |
| 229 | + assert translation_glossary.file_format is not None |
| 230 | + assert translation_glossary.format_version is not None |
| 231 | + assert translation_glossary.storage_source is not None |
| 232 | + |
| 233 | + def validate_document_status(self, document_status): |
| 234 | + assert document_status is not None |
| 235 | + assert document_status.source_document_url is not None |
| 236 | + assert document_status.created_on is not None |
| 237 | + assert document_status.last_updated_on is not None |
| 238 | + assert document_status.status is not None |
| 239 | + assert document_status.translated_to is not None |
| 240 | + assert document_status.translation_progress is not None |
| 241 | + assert document_status.id is not None |
| 242 | + assert document_status.translated_document_url is not None |
| 243 | + assert document_status.characters_charged is not None |
| 244 | + |
| 245 | + def validate_translation_status(self, translation_status): |
| 246 | + assert translation_status is not None |
| 247 | + assert translation_status.id is not None |
| 248 | + assert translation_status.created_on is not None |
| 249 | + assert translation_status.last_updated_on is not None |
| 250 | + assert translation_status.status is not None |
| 251 | + assert translation_status.summary is not None |
| 252 | + |
| 253 | + # verifying old attributes |
| 254 | + assert translation_status is not None |
| 255 | + assert translation_status.documents_total_count is not None |
| 256 | + assert translation_status.documents_failed_count is not None |
| 257 | + assert translation_status.documents_in_progress_count is not None |
| 258 | + assert translation_status.documents_succeeded_count is not None |
| 259 | + assert translation_status.documents_not_started_count is not None |
| 260 | + assert translation_status.documents_canceled_count is not None |
| 261 | + assert translation_status.total_characters_charged is not None |
0 commit comments