File: test_cancel_translation_async.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (51 lines) | stat: -rw-r--r-- 2,142 bytes parent folder | download
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
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------

import functools
import pytest
from testcase import Document
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 import DocumentTranslationInput, TranslationTarget
from azure.ai.translation.document.aio import DocumentTranslationClient
from azure.core.exceptions import HttpResponseError

DocumentTranslationClientPreparer = functools.partial(_DocumentTranslationClientPreparer, DocumentTranslationClient)


class TestCancelTranslation(AsyncDocumentTranslationTest):

    @DocumentTranslationPreparer()
    @DocumentTranslationClientPreparer()
    @recorded_by_proxy_async
    async def test_cancel_translation(self, **kwargs):
        """
        some notes (test sporadically failing):
        1. use a large number of translations
            - because when running tests the translation sometimes finishes with status 'Succeeded'
              before we call the 'cancel' endpoint!
        2. wait sometime after calling 'cancel' and before calling 'get status'
            - in order for the cancel status to propagate
        """
        client = kwargs.pop("client")
        variables = kwargs.pop("variables", {})
        # submit translation operation
        docs_count = 8  # large number of docs
        poller = await self._begin_and_validate_translation_with_multiple_docs_async(
            client, docs_count, wait=False, variables=variables
        )

        # cancel translation
        await client.cancel_translation(poller.id)
        await poller.result()
        # check translation status
        translation_details = await client.get_translation_status(poller.id)
        assert translation_details.status in ["Canceled", "Canceling", "NotStarted"]
        self._validate_translations(translation_details)
        return variables