|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright 2024 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +import re |
| 19 | +from typing import Dict |
| 20 | + |
| 21 | +from google.cloud import aiplatform |
| 22 | +from google.cloud.aiplatform import base |
| 23 | +from feature_store_constants import _TEST_FG1_FM1_DESCRIPTION |
| 24 | +from feature_store_constants import _TEST_FG1_FM1_ID |
| 25 | +from feature_store_constants import _TEST_FG1_FM1_LABELS |
| 26 | +from feature_store_constants import _TEST_FG1_FM1_PATH |
| 27 | +from feature_store_constants import _TEST_FG1_ID |
| 28 | +from feature_store_constants import _TEST_LOCATION |
| 29 | +from feature_store_constants import _TEST_PROJECT |
| 30 | +from vertexai.resources.preview import FeatureMonitor |
| 31 | +import pytest |
| 32 | + |
| 33 | + |
| 34 | +pytestmark = pytest.mark.usefixtures("google_auth_mock") |
| 35 | + |
| 36 | + |
| 37 | +def feature_monitor_eq( |
| 38 | + feature_monitor_to_check: FeatureMonitor, |
| 39 | + name: str, |
| 40 | + resource_name: str, |
| 41 | + project: str, |
| 42 | + location: str, |
| 43 | + description: str, |
| 44 | + labels: Dict[str, str], |
| 45 | +): |
| 46 | + """Check if a Feature Monitor has the appropriate values set.""" |
| 47 | + assert feature_monitor_to_check.name == name |
| 48 | + assert feature_monitor_to_check.resource_name == resource_name |
| 49 | + assert feature_monitor_to_check.project == project |
| 50 | + assert feature_monitor_to_check.location == location |
| 51 | + assert feature_monitor_to_check.description == description |
| 52 | + assert feature_monitor_to_check.labels == labels |
| 53 | + |
| 54 | + |
| 55 | +def test_init_with_feature_monitor_id_and_no_fg_id_raises_error(): |
| 56 | + aiplatform.init(project=_TEST_PROJECT, location=_TEST_LOCATION) |
| 57 | + |
| 58 | + with pytest.raises( |
| 59 | + ValueError, |
| 60 | + match=re.escape( |
| 61 | + "Since feature monitor 'my_fg1_fm1' is not provided as a path, please" |
| 62 | + " specify feature_group_id." |
| 63 | + ), |
| 64 | + ): |
| 65 | + FeatureMonitor(_TEST_FG1_FM1_ID) |
| 66 | + |
| 67 | + |
| 68 | +def test_init_with_feature_monitor_path_and_fg_id_raises_error(): |
| 69 | + aiplatform.init(project=_TEST_PROJECT, location=_TEST_LOCATION) |
| 70 | + |
| 71 | + with pytest.raises( |
| 72 | + ValueError, |
| 73 | + match=re.escape( |
| 74 | + "Since feature monitor 'projects/test-project/locations/us-central1/" |
| 75 | + "featureGroups/my_fg1/featureMonitors/my_fg1_fm1' is provided as a " |
| 76 | + "path, feature_group_id should not be specified." |
| 77 | + ), |
| 78 | + ): |
| 79 | + FeatureMonitor(_TEST_FG1_FM1_PATH, feature_group_id=_TEST_FG1_ID) |
| 80 | + |
| 81 | + |
| 82 | +def test_init_with_feature_monitor_id(get_feature_monitor_mock): |
| 83 | + aiplatform.init(project=_TEST_PROJECT, location=_TEST_LOCATION) |
| 84 | + |
| 85 | + feature_monitor = FeatureMonitor(_TEST_FG1_FM1_ID, feature_group_id=_TEST_FG1_ID) |
| 86 | + |
| 87 | + get_feature_monitor_mock.assert_called_once_with( |
| 88 | + name=_TEST_FG1_FM1_PATH, |
| 89 | + retry=base._DEFAULT_RETRY, |
| 90 | + ) |
| 91 | + |
| 92 | + feature_monitor_eq( |
| 93 | + feature_monitor, |
| 94 | + name=_TEST_FG1_FM1_ID, |
| 95 | + resource_name=_TEST_FG1_FM1_PATH, |
| 96 | + project=_TEST_PROJECT, |
| 97 | + location=_TEST_LOCATION, |
| 98 | + description=_TEST_FG1_FM1_DESCRIPTION, |
| 99 | + labels=_TEST_FG1_FM1_LABELS, |
| 100 | + ) |
| 101 | + |
| 102 | + |
| 103 | +def test_init_with_feature_path(get_feature_monitor_mock): |
| 104 | + aiplatform.init(project=_TEST_PROJECT, location=_TEST_LOCATION) |
| 105 | + |
| 106 | + feature_monitor = FeatureMonitor(_TEST_FG1_FM1_PATH) |
| 107 | + |
| 108 | + get_feature_monitor_mock.assert_called_once_with( |
| 109 | + name=_TEST_FG1_FM1_PATH, |
| 110 | + retry=base._DEFAULT_RETRY, |
| 111 | + ) |
| 112 | + |
| 113 | + feature_monitor_eq( |
| 114 | + feature_monitor, |
| 115 | + name=_TEST_FG1_FM1_ID, |
| 116 | + resource_name=_TEST_FG1_FM1_PATH, |
| 117 | + project=_TEST_PROJECT, |
| 118 | + location=_TEST_LOCATION, |
| 119 | + description=_TEST_FG1_FM1_DESCRIPTION, |
| 120 | + labels=_TEST_FG1_FM1_LABELS, |
| 121 | + ) |
0 commit comments