File: test_profile.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 (125 lines) | stat: -rw-r--r-- 4,571 bytes parent folder | download | duplicates (4)
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
#-------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
#--------------------------------------------------------------------------
from azure.profiles import ProfileDefinition, KnownProfiles
from azure.profiles.multiapiclient import MultiApiClientMixin

import pytest

def test_profile_from_string():
    profile_from_string = KnownProfiles.from_name("2017-03-09-profile")
    assert profile_from_string is KnownProfiles.v2017_03_09_profile

    with pytest.raises(ValueError):
        KnownProfiles.from_name("blablabla")

def test_default_profile():
    with pytest.raises(ValueError):
        KnownProfiles.default.use("This is not a profile")

def test_multiapi_client():

    class SDKClient(object):
        # Mock msrest.SDKClient to not import it
        def __init__(self, creds, config):
            assert creds == "creds"
            assert config == "config"

    class TestClient(MultiApiClientMixin, SDKClient):
        DEFAULT_API_VERSION = "2216-08-09"
        _PROFILE_TAG = "azure.mgmt.compute.ComputeManagementClient"
        LATEST_PROFILE = ProfileDefinition({
            _PROFILE_TAG: {
                None: DEFAULT_API_VERSION
            }},
            _PROFILE_TAG + " latest"
        )

        def __init__(self, creds="creds", config="config", api_version=None, profile=KnownProfiles.default):
            super(TestClient, self).__init__(
                creds,
                config,
                api_version=api_version,
                profile=profile
            )

        def operations(self):
            return self._get_api_version("operations")

    # By default, use latest
    client = TestClient()
    assert client.operations() == TestClient.DEFAULT_API_VERSION

    # Dynamically change to a new profile
    KnownProfiles.default.use(KnownProfiles.v2017_03_09_profile)
    assert client.operations() == "2016-03-30"

    # I ask explicitly latest, where the default is not that
    client = TestClient(profile=KnownProfiles.latest)
    assert client.operations() == TestClient.DEFAULT_API_VERSION

    # Bring back default to latest for next tests
    KnownProfiles.default.use(KnownProfiles.latest)

    # I asked explicily a specific profile, must not be latest
    client = TestClient(profile=KnownProfiles.v2017_03_09_profile)
    assert client.operations() == "2016-03-30"

    # I refuse api_version and profile at the same time
    # https://github.com/Azure/azure-sdk-for-python/issues/1864
    with pytest.raises(ValueError):
        TestClient(api_version="something", profile=KnownProfiles.latest)

    # If I provide only api_version, this creates a profile with just that
    client = TestClient(api_version="2666-05-15")
    assert client.operations() == "2666-05-15"

    # I can specify old profile syntax with dict
    client = TestClient(profile={
        "operations": "1789-07-14"
    })
    assert client.operations() == "1789-07-14"

    # If I give a profile definition with no default api-version
    # and I call a method not define in the profile, this fails
    client = TestClient(profile={
        "operations2": "1789-07-14"
    })
    with pytest.raises(ValueError):
        client.operations() == "1789-07-14"

def test_multiapi_client_legacy():
    """The messed-up way old Profile was.
    Note that this was only released on RC packages, so as soon as the
    CLI does not use RC packages anymore, I have no trouble to
    remove that legacy.
    """

    class TestClient(MultiApiClientMixin):
        DEFAULT_API_VERSION = "2216-08-09"
        _PROFILE_TAG = "azure.mgmt.compute.ComputeManagementClient"
        LATEST_PROFILE = ProfileDefinition({
            _PROFILE_TAG: {
                None: DEFAULT_API_VERSION
            }},
            _PROFILE_TAG + " latest"
        )

        def __init__(self, creds="creds", config="config", api_version=None, profile=KnownProfiles.default):
            super(TestClient, self).__init__(
                credentials="credentials",
                subscription_id="subscription_id",
                api_version=api_version,
                base_url="base_url",
                profile=profile
            )

        def operations(self):
            return self._get_api_version("operations")

    # Creating a client that does not raise with:
    # TypeError: object.__init__() takes no parameters
    # is enough to show the legacy work
    TestClient()