1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
from datetime import datetime
import functools
from asynctestcase import AsyncDocumentTranslationTest
from preparer import (
DocumentTranslationPreparer,
DocumentTranslationClientPreparer as _DocumentTranslationClientPreparer,
)
from devtools_testutils.aio import recorded_by_proxy_async
from azure.ai.translation.document.aio import DocumentTranslationClient
import pytest
DocumentTranslationClientPreparer = functools.partial(_DocumentTranslationClientPreparer, DocumentTranslationClient)
class TestAllDocumentStatuses(AsyncDocumentTranslationTest):
@DocumentTranslationPreparer()
@DocumentTranslationClientPreparer()
@recorded_by_proxy_async
async def test_list_document_statuses(self, **kwargs):
client = kwargs.pop("client")
variables = kwargs.pop("variables", {})
docs_count = 5
target_language = "es"
# submit and validate operation
poller = await self._begin_and_validate_translation_with_multiple_docs_async(
client, docs_count, language=target_language, wait=True, variables=variables
)
# check doc statuses
doc_statuses = client.list_document_statuses(poller.id)
doc_statuses_list = []
async for document in doc_statuses:
doc_statuses_list.append(document)
self._validate_doc_status(document, target_language)
assert len(doc_statuses_list) == docs_count
return variables
@DocumentTranslationPreparer()
@DocumentTranslationClientPreparer()
@recorded_by_proxy_async
async def test_list_document_statuses_with_skip(self, **kwargs):
client = kwargs.pop("client")
variables = kwargs.pop("variables", {})
docs_count = 5
skip = 2
target_language = "es"
# submit and validate operation
poller = await self._begin_and_validate_translation_with_multiple_docs_async(
client, docs_count, language=target_language, wait=True, variables=variables
)
# check doc statuses
doc_statuses = client.list_document_statuses(translation_id=poller.id, skip=skip)
doc_statuses_list = []
# iterate over docs
async for document in doc_statuses:
doc_statuses_list.append(document)
self._validate_doc_status(document, target_language)
assert len(doc_statuses_list) == docs_count - skip
return variables
@DocumentTranslationPreparer()
@DocumentTranslationClientPreparer()
@recorded_by_proxy_async
async def test_list_document_statuses_filter_by_status(self, **kwargs):
client = kwargs.pop("client")
variables = kwargs.pop("variables", {})
docs_count = 10
target_language = "es"
# submit and validate operation
poller = await self._begin_and_validate_translation_with_multiple_docs_async(
client, docs_count, language=target_language, wait=True, variables=variables
)
# list operations
statuses = ["NotStarted"]
doc_statuses = client.list_document_statuses(poller.id, statuses=statuses)
counter = 0
async for doc in doc_statuses:
counter += 1
assert counter == 0
statuses = ["Succeeded"]
doc_statuses = client.list_document_statuses(poller.id, statuses=statuses)
counter = 0
async for doc in doc_statuses:
counter += 1
assert counter == docs_count
statuses = ["Failed"]
doc_statuses = client.list_document_statuses(poller.id, statuses=statuses)
counter = 0
async for doc in doc_statuses:
counter += 1
assert counter == 0
return variables
@DocumentTranslationPreparer()
@DocumentTranslationClientPreparer()
@recorded_by_proxy_async
async def test_list_document_statuses_filter_by_ids(self, **kwargs):
client = kwargs.pop("client")
variables = kwargs.pop("variables", {})
docs_count = 15
target_language = "es"
# submit and validate operation
poller = await self._begin_and_validate_translation_with_multiple_docs_async(
client, docs_count, language=target_language, wait=True, variables=variables
)
# filter ids
doc_statuses = client.list_document_statuses(poller.id)
ids = [document.id async for document in doc_statuses]
assert len(ids) == docs_count
ids = ids[: docs_count // 2]
# do the testing
doc_statuses = client.list_document_statuses(poller.id, document_ids=ids)
counter = 0
async for document in doc_statuses:
counter += 1
self._validate_doc_status(document, target_language, ids=ids)
assert counter == len(ids)
return variables
@DocumentTranslationPreparer()
@DocumentTranslationClientPreparer()
@recorded_by_proxy_async
async def test_list_document_statuses_order_by_creation_time_asc(self, **kwargs):
client = kwargs.pop("client")
variables = kwargs.pop("variables", {})
docs_count = 5
target_language = "es"
# submit and validate operation
poller = await self._begin_and_validate_translation_with_multiple_docs_async(
client, docs_count, language=target_language, wait=True, variables=variables
)
# check doc statuses
doc_statuses = client.list_document_statuses(poller.id, order_by=["created_on asc"])
current = None
docs = []
async for document in doc_statuses:
docs.append(document)
if current:
assert document.created_on.replace(tzinfo=None) >= current.replace(tzinfo=None)
current = document.created_on
assert len(docs) == docs_count
return variables
@DocumentTranslationPreparer()
@DocumentTranslationClientPreparer()
@recorded_by_proxy_async
async def test_list_document_statuses_order_by_creation_time_desc(self, **kwargs):
client = kwargs.pop("client")
variables = kwargs.pop("variables", {})
docs_count = 5
target_language = "es"
# submit and validate operation
poller = await self._begin_and_validate_translation_with_multiple_docs_async(
client, docs_count, language=target_language, wait=True, variables=variables
)
# check doc statuses
doc_statuses = client.list_document_statuses(poller.id, order_by=["created_on desc"])
current = None
docs = []
async for document in doc_statuses:
docs.append(document)
if current:
assert document.created_on.replace(tzinfo=None) <= current.replace(tzinfo=None)
current = document.created_on
assert len(docs) == docs_count
return variables
@DocumentTranslationPreparer()
@DocumentTranslationClientPreparer()
@recorded_by_proxy_async
async def test_list_document_statuses_mixed_filters(self, **kwargs):
client = kwargs.pop("client")
variables = kwargs.pop("variables", {})
docs_count = 25
target_language = "es"
statuses = ["Succeeded"]
skip = 3
# submit and validate operation
poller = await self._begin_and_validate_translation_with_multiple_docs_async(
client, docs_count, language=target_language, wait=True, variables=variables
)
# get ids
doc_statuses = client.list_document_statuses(poller.id)
ids = [document.id async for document in doc_statuses]
assert len(ids) == docs_count
ids = ids[: docs_count // 2]
filtered_docs = client.list_document_statuses(
poller.id,
# filters
document_ids=ids,
statuses=statuses,
# ordering
order_by=["created_on asc"],
# paging
skip=skip,
).by_page()
# check statuses
counter = 0
current = None
async for page in filtered_docs:
page_docs = []
async for doc in page:
counter += 1
page_docs.append(doc)
# assert ordering
if current:
assert doc.created_on.replace(tzinfo=None) >= current.replace(tzinfo=None)
current = doc.created_on
# assert filters
assert doc.status in statuses
assert doc.id in ids
assert counter == len(ids) - skip
return variables
|