File: test_compose_model_async.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 (106 lines) | stat: -rw-r--r-- 5,188 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
# coding=utf-8
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------

import pytest
import functools
from azure.core.exceptions import ClientAuthenticationError, HttpResponseError
from azure.ai.formrecognizer.aio import FormTrainingClient
from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer
from testcase import GlobalClientPreparer as _GlobalClientPreparer
from asynctestcase import AsyncFormRecognizerTest


GlobalClientPreparer = functools.partial(_GlobalClientPreparer, FormTrainingClient)


class TestTrainingAsync(AsyncFormRecognizerTest):

    @GlobalFormRecognizerAccountPreparer()
    @GlobalClientPreparer(training=True)
    async def test_compose_model_with_model_name(self, client, container_sas_url):
        async with client:
            poller = await client.begin_training(container_sas_url, use_training_labels=True)
            model_1 = await poller.result()

            poller = await client.begin_training(container_sas_url, use_training_labels=True, model_name="second-labeled-model")
            model_2 = await poller.result()

            poller = await client.begin_create_composed_model([model_1.model_id, model_2.model_id], model_name="my composed model")

            composed_model = await poller.result()
            self.assertEqual(composed_model.model_name, "my composed model")
            self.assertComposedModelHasValues(composed_model, model_1, model_2)

    @GlobalFormRecognizerAccountPreparer()
    @GlobalClientPreparer(training=True)
    async def test_compose_model_no_model_name(self, client, container_sas_url):
        async with client:
            poller = await client.begin_training(container_sas_url, use_training_labels=True)
            model_1 = await poller.result()

            poller = await client.begin_training(container_sas_url, use_training_labels=True)
            model_2 = await poller.result()

            poller = await client.begin_create_composed_model([model_1.model_id, model_2.model_id])

            composed_model = await poller.result()
            self.assertIsNone(composed_model.model_name)
            self.assertComposedModelHasValues(composed_model, model_1, model_2)

    @GlobalFormRecognizerAccountPreparer()
    @GlobalClientPreparer(training=True)
    async def test_compose_model_invalid_unlabeled_models(self, client, container_sas_url):
        async with client:
            poller = await client.begin_training(container_sas_url, use_training_labels=False)
            model_1 = await poller.result()

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

            with pytest.raises(HttpResponseError) as e:
                poller = await client.begin_create_composed_model([model_1.model_id, model_2.model_id])
                composed_model = await poller.result()
            self.assertEqual(e.value.error.code, "1001")
            self.assertIsNotNone(e.value.error.message)

    @GlobalFormRecognizerAccountPreparer()
    @GlobalClientPreparer(training=True)
    async def test_compose_model_invalid_model(self, client, container_sas_url):
        async with client:
            with pytest.raises(HttpResponseError) as e:
                poller = await client.begin_create_composed_model(["00000000-0000-0000-0000-000000000000"])
                composed_model = await poller.result()
            self.assertEqual(e.value.error.code, "1001")
            self.assertIsNotNone(e.value.error.message)

    @GlobalFormRecognizerAccountPreparer()
    @GlobalClientPreparer(training=True)
    @pytest.mark.live_test_only
    async def test_compose_continuation_token(self, client, container_sas_url):
        async with client:
            poller = await client.begin_training(container_sas_url, use_training_labels=True)
            model_1 = await poller.result()

            poller = await client.begin_training(container_sas_url, use_training_labels=True)
            model_2 = await poller.result()

            initial_poller = await client.begin_create_composed_model([model_1.model_id, model_2.model_id])
            cont_token = initial_poller.continuation_token()

            poller = await client.begin_create_composed_model(None, continuation_token=cont_token)
            result = await poller.result()
            self.assertIsNotNone(result)

            await initial_poller.wait()  # necessary so azure-devtools doesn't throw assertion error

    @GlobalFormRecognizerAccountPreparer()
    @GlobalClientPreparer(training=True, client_kwargs={"api_version": "2.0"})
    async def test_compose_model_bad_api_version(self, client, container_sas_url):
        async with client:
            with pytest.raises(ValueError) as excinfo:
                poller = await client.begin_create_composed_model(["00000000-0000-0000-0000-000000000000", "00000000-0000-0000-0000-000000000000"])
                result = await poller.result()
            assert "Method 'begin_create_composed_model' is only available for API version V2_1_PREVIEW and up" in str(excinfo.value)