|
1 | 1 | from typing import Iterable |
2 | | -from unittest.mock import Mock, patch |
| 2 | +from unittest.mock import Mock, call, patch |
3 | 3 |
|
4 | 4 | import pytest |
5 | 5 |
|
|
14 | 14 | from azure.ai.ml.constants._common import ARM_ID_PREFIX |
15 | 15 | from azure.ai.ml.entities._assets import Environment |
16 | 16 | from azure.ai.ml.operations import EnvironmentOperations |
17 | | -from azure.core.exceptions import ResourceNotFoundError |
| 17 | +from azure.core.exceptions import ResourceExistsError, ResourceNotFoundError |
18 | 18 |
|
19 | 19 |
|
20 | 20 | @pytest.fixture |
@@ -108,15 +108,106 @@ def test_create_autoincrement( |
108 | 108 | with patch( |
109 | 109 | "azure.ai.ml.operations._environment_operations.Environment._from_rest_object", return_value=None |
110 | 110 | ), patch( |
111 | | - "azure.ai.ml.operations._environment_operations._get_next_version_from_container", return_value="version" |
| 111 | + "azure.ai.ml.operations._environment_operations._get_next_latest_versions_from_container", |
| 112 | + return_value=("version", "latest"), |
112 | 113 | ) as mock_nextver: |
113 | 114 | mock_environment_operation.create_or_update(env) |
114 | 115 | mock_nextver.assert_called_once() |
115 | 116 |
|
116 | 117 | mock_environment_operation._version_operations.create_or_update.assert_called_once_with( |
117 | 118 | body=env._to_rest_object(), |
118 | 119 | name=env.name, |
119 | | - version=mock_nextver.return_value, |
| 120 | + version="latest", |
| 121 | + resource_group_name=mock_workspace_scope.resource_group_name, |
| 122 | + workspace_name=mock_workspace_scope.workspace_name, |
| 123 | + ) |
| 124 | + |
| 125 | + def test_create_when_latest_fail_with_resource_exist( |
| 126 | + self, |
| 127 | + mock_environment_operation: EnvironmentOperations, |
| 128 | + mock_workspace_scope: OperationScope, |
| 129 | + ) -> None: |
| 130 | + env = load_environment(source="./tests/test_configs/environment/environment_no_version.yml") |
| 131 | + assert env._auto_increment_version |
| 132 | + env.version = None |
| 133 | + mock_environment_operation._version_operations.create_or_update( |
| 134 | + body=env._to_rest_object(), |
| 135 | + name=env.name, |
| 136 | + version="latest_version", |
| 137 | + resource_group_name=mock_workspace_scope.resource_group_name, |
| 138 | + workspace_name=mock_workspace_scope.workspace_name, |
| 139 | + ).side_effect = ResourceExistsError() |
| 140 | + with patch( |
| 141 | + "azure.ai.ml.operations._environment_operations.Environment._from_rest_object", return_value=None |
| 142 | + ), patch( |
| 143 | + "azure.ai.ml.operations._environment_operations._get_next_latest_versions_from_container", |
| 144 | + return_value=("next_version", "latest_version"), |
| 145 | + ) as mock_nextver: |
| 146 | + |
| 147 | + def side_effect(name, version, body, resource_group_name, workspace_name): |
| 148 | + if version == "latest_version": |
| 149 | + raise ResourceExistsError |
| 150 | + |
| 151 | + mock_environment_operation._version_operations.create_or_update.side_effect = side_effect |
| 152 | + mock_environment_operation.create_or_update(env) |
| 153 | + mock_nextver.assert_called_once() |
| 154 | + |
| 155 | + mock_environment_operation._version_operations.create_or_update.assert_has_calls( |
| 156 | + [ |
| 157 | + call( |
| 158 | + body=env._to_rest_object(), |
| 159 | + name=env.name, |
| 160 | + version="latest_version", |
| 161 | + resource_group_name=mock_workspace_scope.resource_group_name, |
| 162 | + workspace_name=mock_workspace_scope.workspace_name, |
| 163 | + ), |
| 164 | + call( |
| 165 | + body=env._to_rest_object(), |
| 166 | + name=env.name, |
| 167 | + version="next_version", |
| 168 | + resource_group_name=mock_workspace_scope.resource_group_name, |
| 169 | + workspace_name=mock_workspace_scope.workspace_name, |
| 170 | + ), |
| 171 | + ] |
| 172 | + ) |
| 173 | + |
| 174 | + def test_create_when_latest_fail_with_unknown( |
| 175 | + self, |
| 176 | + mock_environment_operation: EnvironmentOperations, |
| 177 | + mock_workspace_scope: OperationScope, |
| 178 | + ) -> None: |
| 179 | + env = load_environment(source="./tests/test_configs/environment/environment_no_version.yml") |
| 180 | + assert env._auto_increment_version |
| 181 | + env.version = None |
| 182 | + mock_environment_operation._version_operations.create_or_update( |
| 183 | + body=env._to_rest_object(), |
| 184 | + name=env.name, |
| 185 | + version="latest_version", |
| 186 | + resource_group_name=mock_workspace_scope.resource_group_name, |
| 187 | + workspace_name=mock_workspace_scope.workspace_name, |
| 188 | + ).side_effect = ResourceExistsError() |
| 189 | + with patch( |
| 190 | + "azure.ai.ml.operations._environment_operations.Environment._from_rest_object", return_value=None |
| 191 | + ), patch( |
| 192 | + "azure.ai.ml.operations._environment_operations._get_next_latest_versions_from_container", |
| 193 | + return_value=("next_version", "latest_version"), |
| 194 | + ) as mock_nextver: |
| 195 | + |
| 196 | + def side_effect(name, version, body, resource_group_name, workspace_name): |
| 197 | + if version == "latest_version": |
| 198 | + raise Exception("Unknowm Error") |
| 199 | + |
| 200 | + mock_environment_operation._version_operations.create_or_update.side_effect = side_effect |
| 201 | + try: |
| 202 | + mock_environment_operation.create_or_update(env) |
| 203 | + except Exception: |
| 204 | + pass |
| 205 | + mock_nextver.assert_called_once() |
| 206 | + |
| 207 | + mock_environment_operation._version_operations.create_or_update.assert_called_with( |
| 208 | + body=env._to_rest_object(), |
| 209 | + name=env.name, |
| 210 | + version="latest_version", |
120 | 211 | resource_group_name=mock_workspace_scope.resource_group_name, |
121 | 212 | workspace_name=mock_workspace_scope.workspace_name, |
122 | 213 | ) |
@@ -177,6 +268,9 @@ def test_restore_container(self, mock_environment_operation: EnvironmentOperatio |
177 | 268 | resource_group_name=mock_environment_operation._resource_group_name, |
178 | 269 | ) |
179 | 270 |
|
| 271 | + def side_effect(self, args): |
| 272 | + print(args) |
| 273 | + |
180 | 274 | # #Mock(azure.ai.ml._restclient.v2021_10_01_dataplanepreview.operations._environment_versions_operations, "get") |
181 | 275 | # def test_promote_environment_from_workspace( |
182 | 276 | # self, |
|
0 commit comments