File size: 9,051 Bytes
1856027 | 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | # -*- coding: utf-8 -*-
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import re
from typing import Dict, List, Optional
from unittest import mock
from unittest.mock import call, patch
from google.api_core import operation as ga_operation
from google.cloud import aiplatform
from google.cloud.aiplatform import base
from google.cloud.aiplatform.compat import types
from google.cloud.aiplatform.compat.services import (
feature_registry_service_client,
)
from google.cloud.aiplatform_v1beta1.services.feature_registry_service import (
FeatureRegistryServiceClient,
)
from feature_store_constants import (
_TEST_FG1_F1_DESCRIPTION,
_TEST_FG1_F1_FEATURE_STATS_AND_ANOMALY,
_TEST_FG1_F1_ID,
_TEST_FG1_F1_LABELS,
_TEST_FG1_F1_PATH,
_TEST_FG1_F1_POINT_OF_CONTACT,
_TEST_FG1_F1_WITH_STATS,
_TEST_FG1_F2_DESCRIPTION,
_TEST_FG1_F2_ID,
_TEST_FG1_F2_LABELS,
_TEST_FG1_F2_PATH,
_TEST_FG1_F2_POINT_OF_CONTACT,
_TEST_FG1_F2_VERSION_COLUMN_NAME,
_TEST_FG1_ID,
_TEST_LOCATION,
_TEST_PROJECT,
)
from vertexai.resources.preview import (
Feature,
FeatureGroup,
)
import pytest
@pytest.fixture
def delete_feature_mock():
with patch.object(
feature_registry_service_client.FeatureRegistryServiceClient,
"delete_feature",
) as delete_feature_mock:
delete_feature_lro_mock = mock.Mock(ga_operation.Operation)
delete_feature_mock.return_value = delete_feature_lro_mock
yield delete_feature_mock
@pytest.fixture
def get_feature_with_stats_and_anomalies_mock():
with patch.object(
FeatureRegistryServiceClient,
"get_feature",
) as get_feature_with_stats_and_anomalies_mock:
get_feature_with_stats_and_anomalies_mock.return_value = _TEST_FG1_F1_WITH_STATS
yield get_feature_with_stats_and_anomalies_mock
pytestmark = pytest.mark.usefixtures("google_auth_mock")
def feature_eq(
feature_to_check: Feature,
name: str,
resource_name: str,
project: str,
location: str,
description: str,
labels: Dict[str, str],
point_of_contact: str,
version_column_name: Optional[str] = None,
feature_stats_and_anomalies: Optional[
List[types.feature_monitor.FeatureStatsAndAnomaly]
] = None,
):
"""Check if a Feature has the appropriate values set."""
assert feature_to_check.name == name
assert feature_to_check.resource_name == resource_name
assert feature_to_check.project == project
assert feature_to_check.location == location
assert feature_to_check.description == description
assert feature_to_check.labels == labels
assert feature_to_check.point_of_contact == point_of_contact
if version_column_name:
assert feature_to_check.version_column_name == version_column_name
if feature_stats_and_anomalies:
assert (
feature_to_check.feature_stats_and_anomalies == feature_stats_and_anomalies
)
def test_init_with_feature_id_and_no_fg_id_raises_error(get_feature_mock):
aiplatform.init(project=_TEST_PROJECT, location=_TEST_LOCATION)
with pytest.raises(
ValueError,
match=re.escape(
"Since feature 'my_fg1_f1' is not provided as a path, please specify"
+ " feature_group_id."
),
):
Feature(_TEST_FG1_F1_ID)
def test_init_with_feature_path_and_fg_id_raises_error(get_feature_mock):
aiplatform.init(project=_TEST_PROJECT, location=_TEST_LOCATION)
with pytest.raises(
ValueError,
match=re.escape(
"Since feature 'projects/test-project/locations/us-central1/featureGroups/my_fg1/features/my_fg1_f1' is provided as a path, feature_group_id should not be specified."
),
):
Feature(_TEST_FG1_F1_PATH, feature_group_id=_TEST_FG1_ID)
def test_init_with_feature_id(get_feature_mock):
aiplatform.init(project=_TEST_PROJECT, location=_TEST_LOCATION)
feature = Feature(_TEST_FG1_F1_ID, feature_group_id=_TEST_FG1_ID)
get_feature_mock.assert_called_once_with(
name=_TEST_FG1_F1_PATH,
retry=base._DEFAULT_RETRY,
)
feature_eq(
feature,
name=_TEST_FG1_F1_ID,
resource_name=_TEST_FG1_F1_PATH,
project=_TEST_PROJECT,
location=_TEST_LOCATION,
description=_TEST_FG1_F1_DESCRIPTION,
labels=_TEST_FG1_F1_LABELS,
point_of_contact=_TEST_FG1_F1_POINT_OF_CONTACT,
)
def test_init_with_feature_id_for_explicit_version_column(
get_feature_with_version_column_mock,
):
aiplatform.init(project=_TEST_PROJECT, location=_TEST_LOCATION)
feature = Feature(_TEST_FG1_F2_ID, feature_group_id=_TEST_FG1_ID)
get_feature_with_version_column_mock.assert_called_once_with(
name=_TEST_FG1_F2_PATH,
retry=base._DEFAULT_RETRY,
)
feature_eq(
feature,
name=_TEST_FG1_F2_ID,
resource_name=_TEST_FG1_F2_PATH,
project=_TEST_PROJECT,
location=_TEST_LOCATION,
description=_TEST_FG1_F2_DESCRIPTION,
labels=_TEST_FG1_F2_LABELS,
point_of_contact=_TEST_FG1_F2_POINT_OF_CONTACT,
version_column_name=_TEST_FG1_F2_VERSION_COLUMN_NAME,
)
def test_init_with_feature_path(get_feature_mock):
aiplatform.init(project=_TEST_PROJECT, location=_TEST_LOCATION)
feature = Feature(_TEST_FG1_F1_PATH)
get_feature_mock.assert_called_once_with(
name=_TEST_FG1_F1_PATH,
retry=base._DEFAULT_RETRY,
)
feature_eq(
feature,
name=_TEST_FG1_F1_ID,
resource_name=_TEST_FG1_F1_PATH,
project=_TEST_PROJECT,
location=_TEST_LOCATION,
description=_TEST_FG1_F1_DESCRIPTION,
labels=_TEST_FG1_F1_LABELS,
point_of_contact=_TEST_FG1_F1_POINT_OF_CONTACT,
)
def test_init_with_feature_path_for_explicit_version_column(
get_feature_with_version_column_mock,
):
aiplatform.init(project=_TEST_PROJECT, location=_TEST_LOCATION)
feature = Feature(_TEST_FG1_F2_PATH)
get_feature_with_version_column_mock.assert_called_once_with(
name=_TEST_FG1_F2_PATH,
retry=base._DEFAULT_RETRY,
)
feature_eq(
feature,
name=_TEST_FG1_F2_ID,
resource_name=_TEST_FG1_F2_PATH,
project=_TEST_PROJECT,
location=_TEST_LOCATION,
version_column_name=_TEST_FG1_F2_VERSION_COLUMN_NAME,
description=_TEST_FG1_F2_DESCRIPTION,
labels=_TEST_FG1_F2_LABELS,
point_of_contact=_TEST_FG1_F2_POINT_OF_CONTACT,
)
def test_init_with_latest_stats_count(get_feature_with_stats_and_anomalies_mock):
aiplatform.init(project=_TEST_PROJECT, location=_TEST_LOCATION)
feature = Feature(name=_TEST_FG1_F1_PATH, latest_stats_count=1)
get_feature_with_stats_and_anomalies_mock.assert_called_once_with(
request=types.featurestore_service_v1beta1.GetFeatureRequest(
name=_TEST_FG1_F1_PATH,
feature_stats_and_anomaly_spec=types.feature_monitor.FeatureStatsAndAnomalySpec(
latest_stats_count=1
),
)
)
feature_eq(
feature,
name=_TEST_FG1_F1_ID,
resource_name=_TEST_FG1_F1_PATH,
project=_TEST_PROJECT,
location=_TEST_LOCATION,
description=_TEST_FG1_F1_DESCRIPTION,
labels=_TEST_FG1_F1_LABELS,
point_of_contact=_TEST_FG1_F1_POINT_OF_CONTACT,
feature_stats_and_anomalies=[_TEST_FG1_F1_FEATURE_STATS_AND_ANOMALY],
)
@pytest.mark.parametrize("sync", [True])
def test_delete_feature(
get_fg_mock, get_feature_mock, delete_feature_mock, base_logger_mock, sync
):
aiplatform.init(project=_TEST_PROJECT, location=_TEST_LOCATION)
feature = FeatureGroup(_TEST_FG1_ID).get_feature(_TEST_FG1_F1_ID)
feature.delete(sync=sync)
if not sync:
feature.wait()
delete_feature_mock.assert_called_once_with(
name=_TEST_FG1_F1_PATH,
)
base_logger_mock.assert_has_calls(
[
call(
"Deleting Feature resource:"
" projects/test-project/locations/us-central1/featureGroups/my_fg1/features/my_fg1_f1"
),
call(
"Delete Feature backing LRO:"
f" {delete_feature_mock.return_value.operation.name}"
),
call(
"Feature resource"
" projects/test-project/locations/us-central1/featureGroups/my_fg1/features/my_fg1_f1"
" deleted."
),
]
)
|