File: test_list_document_statuses.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 (227 lines) | stat: -rw-r--r-- 8,335 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
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
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------

from datetime import datetime
import functools
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
import pytest

DocumentTranslationClientPreparer = functools.partial(_DocumentTranslationClientPreparer, DocumentTranslationClient)


class TestAllDocumentStatuses(DocumentTranslationTest):
    @DocumentTranslationPreparer()
    @DocumentTranslationClientPreparer()
    @recorded_by_proxy
    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 = self._begin_and_validate_translation_with_multiple_docs(
            client, docs_count, language=target_language, wait=True, variables=variables
        )

        # list docs statuses
        doc_statuses = list(client.list_document_statuses(poller.id))  # convert from generic iterator to list
        assert len(doc_statuses) == docs_count

        for document in doc_statuses:
            self._validate_doc_status(document, target_language)
        return variables

    @DocumentTranslationPreparer()
    @DocumentTranslationClientPreparer()
    @recorded_by_proxy
    def test_list_document_statuses_with_skip(self, **kwargs):
        client = kwargs.pop("client")
        variables = kwargs.pop("variables", {})
        docs_count = 10
        skip = 2
        target_language = "es"

        # submit and validate operation
        poller = self._begin_and_validate_translation_with_multiple_docs(
            client, docs_count, language=target_language, wait=True, variables=variables
        )

        # check doc statuses
        doc_statuses = list(client.list_document_statuses(translation_id=poller.id, skip=skip))
        assert len(doc_statuses) == docs_count - skip

        # iterate over docs
        for document in doc_statuses:
            self._validate_doc_status(document, target_language)
        return variables

    @DocumentTranslationPreparer()
    @DocumentTranslationClientPreparer()
    @recorded_by_proxy
    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 = self._begin_and_validate_translation_with_multiple_docs(
            client, docs_count, language=target_language, wait=True, variables=variables
        )

        # list operations
        statuses = ["NotStarted"]
        doc_statuses = list(client.list_document_statuses(poller.id, statuses=statuses))
        assert len(doc_statuses) == 0

        statuses = ["Succeeded"]
        doc_statuses = list(client.list_document_statuses(poller.id, statuses=statuses))
        assert len(doc_statuses) == docs_count

        statuses = ["Failed"]
        doc_statuses = list(client.list_document_statuses(poller.id, statuses=statuses))
        assert len(doc_statuses) == 0
        return variables

    @DocumentTranslationPreparer()
    @DocumentTranslationClientPreparer()
    @recorded_by_proxy
    def test_list_document_statuses_filter_by_ids(self, **kwargs):
        client = kwargs.pop("client")
        variables = kwargs.pop("variables", {})
        docs_count = 5
        target_language = "es"

        # submit and validate operation
        poller = self._begin_and_validate_translation_with_multiple_docs(
            client, docs_count, language=target_language, wait=True, variables=variables
        )

        # filter ids
        doc_statuses = list(client.list_document_statuses(poller.id))  # convert from generic iterator to list
        assert len(doc_statuses) == docs_count
        ids = [doc.id for doc in doc_statuses]
        ids = ids[: docs_count // 2]

        # do the testing
        doc_statuses = list(client.list_document_statuses(poller.id, document_ids=ids))
        assert len(doc_statuses) == len(ids)
        for document in doc_statuses:
            self._validate_doc_status(document, target_language, ids=ids)
        return variables

    @DocumentTranslationPreparer()
    @DocumentTranslationClientPreparer()
    @recorded_by_proxy
    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 = self._begin_and_validate_translation_with_multiple_docs(
            client, docs_count, language=target_language, wait=True, variables=variables
        )

        # check doc statuses
        doc_statuses = list(
            client.list_document_statuses(poller.id, order_by=["created_on asc"])
        )  # convert from generic iterator to list
        assert len(doc_statuses) == docs_count

        current = None
        for document in doc_statuses:
            if current:
                assert document.created_on.replace(tzinfo=None) >= current.replace(tzinfo=None)
                current = document.created_on
        return variables

    @DocumentTranslationPreparer()
    @DocumentTranslationClientPreparer()
    @recorded_by_proxy
    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 = self._begin_and_validate_translation_with_multiple_docs(
            client, docs_count, language=target_language, wait=True, variables=variables
        )

        # check doc statuses
        doc_statuses = list(
            client.list_document_statuses(poller.id, order_by=["created_on desc"])
        )  # convert from generic iterator to list
        assert len(doc_statuses) == docs_count

        current = None
        for document in doc_statuses:
            if current:
                assert document.created_on.replace(tzinfo=None) <= current.replace(tzinfo=None)
                current = document.created_on

        return variables

    @DocumentTranslationPreparer()
    @DocumentTranslationClientPreparer()
    @recorded_by_proxy
    def test_list_document_statuses_mixed_filters(self, **kwargs):
        client = kwargs.pop("client")
        variables = kwargs.pop("variables", {})
        docs_count = 10
        target_language = "es"
        skip = 1
        statuses = ["Succeeded"]

        # submit and validate operation
        poller = self._begin_and_validate_translation_with_multiple_docs(
            client, docs_count, language=target_language, wait=True, variables=variables
        )

        # get ids
        doc_statuses = list(client.list_document_statuses(poller.id))  # convert from generic iterator to list
        assert len(doc_statuses) == docs_count
        ids = [doc.id for doc in doc_statuses]
        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()
        assert filtered_docs is not None

        # check statuses
        counter = 0
        current = None
        for page in filtered_docs:
            page_docs = list(page)
            for doc in page_docs:
                counter += 1
                # 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