File: test_copy_model.py

package info (click to toggle)
python-azure 20201208%2Bgit-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,437,920 kB
  • sloc: python: 4,287,452; javascript: 269; makefile: 198; sh: 187; xml: 106
file content (235 lines) | stat: -rw-r--r-- 10,508 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
228
229
230
231
232
233
234
235
# coding=utf-8
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------

import pytest
import functools
from azure.core.exceptions import HttpResponseError
from azure.ai.formrecognizer._generated.models import CopyOperationResult
from azure.ai.formrecognizer import CustomFormModelInfo
from azure.ai.formrecognizer import FormTrainingClient
from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer
from testcase import GlobalClientPreparer as _GlobalClientPreparer


GlobalClientPreparer = functools.partial(_GlobalClientPreparer, FormTrainingClient)


class TestCopyModel(FormRecognizerTest):

    @GlobalFormRecognizerAccountPreparer()
    @GlobalClientPreparer(training=True)
    def test_copy_model_none_model_id(self, client, container_sas_url):
        with self.assertRaises(ValueError):
            client.begin_copy_model(model_id=None, target={})

    @GlobalFormRecognizerAccountPreparer()
    @GlobalClientPreparer(training=True)
    def test_copy_model_empty_model_id(self, client, container_sas_url):
        with self.assertRaises(ValueError):
            client.begin_copy_model(model_id="", target={})

    @GlobalFormRecognizerAccountPreparer()
    @GlobalClientPreparer(training=True, copy=True)
    def test_copy_model_successful(self, client, container_sas_url, location, resource_id):

        poller = client.begin_training(container_sas_url, use_training_labels=False)
        model = poller.result()

        target = client.get_copy_authorization(resource_region=location, resource_id=resource_id)

        poller = client.begin_copy_model(model.model_id, target=target)
        copy = poller.result()

        copied_model = client.get_custom_model(copy.model_id)

        self.assertEqual(copy.status, "ready")
        self.assertIsNotNone(copy.training_started_on)
        self.assertIsNotNone(copy.training_completed_on)
        self.assertEqual(target["modelId"], copy.model_id)
        self.assertNotEqual(target["modelId"], model.model_id)
        self.assertIsNotNone(copied_model)

    @GlobalFormRecognizerAccountPreparer()
    @GlobalClientPreparer(training=True, copy=True)
    def test_copy_model_fail(self, client, container_sas_url, location, resource_id):

        poller = client.begin_training(container_sas_url, use_training_labels=False)
        model = poller.result()

        # give an incorrect region
        target = client.get_copy_authorization(resource_region="eastus", resource_id=resource_id)

        with pytest.raises(HttpResponseError) as e:
            poller = client.begin_copy_model(model.model_id, target=target)
            copy = poller.result()
        self.assertIsNotNone(e.value.error.code)
        self.assertIsNotNone(e.value.error.message)

    @GlobalFormRecognizerAccountPreparer()
    @GlobalClientPreparer(training=True, copy=True)
    def test_copy_model_case_insensitive_region(self, client, container_sas_url, location, resource_id):

        poller = client.begin_training(container_sas_url, use_training_labels=False)
        model = poller.result()

        # give region all uppercase
        target = client.get_copy_authorization(resource_region=location.upper(), resource_id=resource_id)

        poller = client.begin_copy_model(model.model_id, target=target)
        copy = poller.result()

        self.assertEqual(copy.status, "ready")
        self.assertIsNotNone(copy.training_started_on)
        self.assertIsNotNone(copy.training_completed_on)
        self.assertEqual(target["modelId"], copy.model_id)
        self.assertNotEqual(target["modelId"], model.model_id)

    @GlobalFormRecognizerAccountPreparer()
    @GlobalClientPreparer(training=True, copy=True)
    def test_copy_model_fail_bad_model_id(self, client, container_sas_url, location, resource_id):

        poller = client.begin_training(container_sas_url, use_training_labels=False)
        model = poller.result()

        target = client.get_copy_authorization(resource_region=location, resource_id=resource_id)

        with self.assertRaises(HttpResponseError):
            # give bad model_id
            poller = client.begin_copy_model("00000000-0000-0000-0000-000000000000", target=target)
            copy = poller.result()

    @GlobalFormRecognizerAccountPreparer()
    @GlobalClientPreparer(training=True, copy=True)
    def test_copy_model_transform(self, client, container_sas_url, location, resource_id):

        poller = client.begin_training(container_sas_url, use_training_labels=False)
        model = poller.result()

        target = client.get_copy_authorization(resource_region=location, resource_id=resource_id)

        raw_response = []

        def callback(response, _, headers):
            copy_result = client._deserialize(CopyOperationResult, response)
            model_info = CustomFormModelInfo._from_generated(copy_result, target["modelId"])
            raw_response.append(copy_result)
            raw_response.append(model_info)

        poller = client.begin_copy_model(model.model_id, target=target, cls=callback)
        copy = poller.result()

        actual = raw_response[0]
        copy = raw_response[1]
        self.assertEqual(copy.training_started_on, actual.created_date_time)
        self.assertEqual(copy.status, actual.status)
        self.assertEqual(copy.training_completed_on, actual.last_updated_date_time)
        self.assertEqual(copy.model_id, target["modelId"])

    @GlobalFormRecognizerAccountPreparer()
    @GlobalClientPreparer(training=True, copy=True)
    def test_copy_authorization(self, client, container_sas_url, location, resource_id):

        target = client.get_copy_authorization(resource_region="eastus", resource_id=resource_id)

        self.assertIsNotNone(target["modelId"])
        self.assertIsNotNone(target["accessToken"])
        self.assertIsNotNone(target["expirationDateTimeTicks"])
        self.assertEqual(target["resourceRegion"], "eastus")
        self.assertEqual(target["resourceId"], resource_id)

    @GlobalFormRecognizerAccountPreparer()
    @GlobalClientPreparer(training=True, copy=True)
    @pytest.mark.live_test_only
    def test_copy_continuation_token(self, client, container_sas_url, location, resource_id):

        poller = client.begin_training(container_sas_url, use_training_labels=False)
        model = poller.result()

        target = client.get_copy_authorization(resource_region=location, resource_id=resource_id)
        initial_poller = client.begin_copy_model(model.model_id, target=target)
        cont_token = initial_poller.continuation_token()

        poller = client.begin_copy_model(model.model_id, None, continuation_token=cont_token)
        result = poller.result()
        self.assertIsNotNone(result)

        copied_model = client.get_custom_model(result.model_id)
        self.assertIsNotNone(copied_model)
        initial_poller.wait()  # necessary so azure-devtools doesn't throw assertion error

    @GlobalFormRecognizerAccountPreparer()
    @GlobalClientPreparer(training=True, copy=True)
    def test_copy_model_with_labeled_model_name(self, client, container_sas_url, location, resource_id):

        poller = client.begin_training(container_sas_url, use_training_labels=True, model_name="mymodel")
        model = poller.result()

        target = client.get_copy_authorization(resource_region=location, resource_id=resource_id)

        poller = client.begin_copy_model(model.model_id, target=target)
        copy = poller.result()

        copied_model = client.get_custom_model(copy.model_id)

        self.assertEqual(copy.status, "ready")
        self.assertIsNotNone(copy.training_started_on)
        self.assertIsNotNone(copy.training_completed_on)
        self.assertEqual(target["modelId"], copy.model_id)
        self.assertNotEqual(target["modelId"], model.model_id)
        self.assertIsNotNone(copied_model)
        self.assertEqual(copied_model.model_name, "mymodel")

    @GlobalFormRecognizerAccountPreparer()
    @GlobalClientPreparer(training=True, copy=True)
    def test_copy_model_with_unlabeled_model_name(self, client, container_sas_url, location, resource_id):

        poller = client.begin_training(container_sas_url, use_training_labels=False, model_name="mymodel")
        model = poller.result()

        target = client.get_copy_authorization(resource_region=location, resource_id=resource_id)

        poller = client.begin_copy_model(model.model_id, target=target)
        copy = poller.result()

        copied_model = client.get_custom_model(copy.model_id)

        self.assertEqual(copy.status, "ready")
        self.assertIsNotNone(copy.training_started_on)
        self.assertIsNotNone(copy.training_completed_on)
        self.assertEqual(target["modelId"], copy.model_id)
        self.assertNotEqual(target["modelId"], model.model_id)
        self.assertIsNotNone(copied_model)
        # self.assertEqual(copied_model.model_name, "mymodel")  # FIXME: still is not returned for unlabeled

    @GlobalFormRecognizerAccountPreparer()
    @GlobalClientPreparer(training=True, copy=True)
    def test_copy_model_with_composed_model(self, client, container_sas_url, location, resource_id):

        poller_1 = client.begin_training(container_sas_url, use_training_labels=True, model_name="model1")
        model_1 = poller_1.result()

        poller_2 = client.begin_training(container_sas_url, use_training_labels=True, model_name="model2")
        model_2 = poller_2.result()

        composed_poller = client.begin_create_composed_model([model_1.model_id, model_2.model_id], model_name="composedmodel")
        composed_model = composed_poller.result()

        target = client.get_copy_authorization(resource_region=location, resource_id=resource_id)

        poller = client.begin_copy_model(composed_model.model_id, target=target)
        copy = poller.result()

        copied_model = client.get_custom_model(copy.model_id)

        self.assertEqual(copy.status, "ready")
        self.assertIsNotNone(copy.training_started_on)
        self.assertIsNotNone(copy.training_completed_on)
        self.assertEqual(target["modelId"], copy.model_id)
        self.assertNotEqual(target["modelId"], composed_model.model_id)
        self.assertIsNotNone(copied_model)
        self.assertEqual(copied_model.model_name, "composedmodel")
        for submodel in copied_model.submodels:
            assert submodel.model_id in [model_1.model_id, model_2.model_id]