File: test_image_analysis_client.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 (99 lines) | stat: -rw-r--r-- 3,669 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
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import inspect
import azure.ai.vision.imageanalysis as sdk

from image_analysis_test_base import ImageAnalysisTestBase, ServicePreparer
from devtools_testutils import recorded_by_proxy


# The test class name needs to start with "Test" to get collected by pytest
class TestImageAnalysisClient(ImageAnalysisTestBase):

    # **********************************************************************************
    #
    #                            HAPPY PATH TESTS
    #
    # **********************************************************************************

    # Test all visual features from an image URL, which settings specified
    @ServicePreparer()
    @recorded_by_proxy
    def test_analyze_sync_all_features_from_url(self, **kwargs):

        self._create_client_for_standard_analysis(sync=True, **kwargs)

        self._do_analysis(
            image_source=self.IMAGE_URL,
            visual_features=[
                sdk.models.VisualFeatures.TAGS,
                sdk.models.VisualFeatures.OBJECTS,
                sdk.models.VisualFeatures.CAPTION,
                sdk.models.VisualFeatures.DENSE_CAPTIONS,
                sdk.models.VisualFeatures.READ,
                sdk.models.VisualFeatures.SMART_CROPS,
                sdk.models.VisualFeatures.PEOPLE,
            ],
            language="en",
            gender_neutral_caption=True,
            smart_crops_aspect_ratios=[0.9, 1.33],
            model_version="latest",
            **kwargs
        )

        self.client.close()

    # Test some visual features, one after the other, from file, using default settings
    @ServicePreparer()
    @recorded_by_proxy
    def test_analyze_sync_single_feature_from_file(self, **kwargs):

        self._create_client_for_standard_analysis(sync=True, get_connection_url=True, **kwargs)

        self._do_analysis(
            image_source=self.IMAGE_FILE,
            visual_features=[sdk.models.VisualFeatures.CAPTION],
            query_params={"key1": "value1", "key2": "value2"},
            **kwargs
        )

        self._do_analysis(image_source=self.IMAGE_FILE, visual_features=[sdk.models.VisualFeatures.READ], **kwargs)

        self._do_analysis(image_source=self.IMAGE_FILE, visual_features=[sdk.models.VisualFeatures.TAGS], **kwargs)

        self.client.close()

    # Test a single visual feature from an image url, using Entra ID authentication
    @ServicePreparer()
    @recorded_by_proxy
    def test_analyze_sync_single_feature_from_url_entra_id_auth(self, **kwargs):

        self._create_client_for_standard_analysis_with_entra_id_auth(sync=True, **kwargs)

        self._do_analysis(image_source=self.IMAGE_URL,visual_features=[sdk.models.VisualFeatures.OBJECTS], **kwargs)

        self.client.close()

    # **********************************************************************************
    #
    #                            ERROR TESTS
    #
    # **********************************************************************************

    @ServicePreparer()
    @recorded_by_proxy
    def test_analyze_sync_image_url_does_not_exist(self, **kwargs):

        self._create_client_for_standard_analysis(sync=True, **kwargs)

        self._do_analysis_with_error(
            image_source="https://www.this.is.a.bad.url.com/for/sure.jpg",
            visual_features=[sdk.models.VisualFeatures.CAPTION],
            expected_status_code=400,
            expected_message_contains="image url is not accessible",
            **kwargs
        )

        self.client.close()