|
| 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 | +from typing import Dict, List |
| 19 | +from unittest.mock import patch |
| 20 | + |
| 21 | +from google.cloud import aiplatform |
| 22 | +from google.cloud.aiplatform import base |
| 23 | +from vertexai.resources.preview import ( |
| 24 | + FeatureGroup, |
| 25 | +) |
| 26 | +import vertexai.resources.preview.feature_store.utils as fs_utils |
| 27 | +import pytest |
| 28 | +from google.cloud.aiplatform.compat.services import ( |
| 29 | + feature_registry_service_client, |
| 30 | +) |
| 31 | + |
| 32 | + |
| 33 | +from feature_store_constants import ( |
| 34 | + _TEST_PROJECT, |
| 35 | + _TEST_LOCATION, |
| 36 | + _TEST_FG1, |
| 37 | + _TEST_FG1_ID, |
| 38 | + _TEST_FG1_PATH, |
| 39 | + _TEST_FG1_BQ_URI, |
| 40 | + _TEST_FG1_ENTITY_ID_COLUMNS, |
| 41 | + _TEST_FG1_LABELS, |
| 42 | +) |
| 43 | + |
| 44 | + |
| 45 | +pytestmark = pytest.mark.usefixtures("google_auth_mock") |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture |
| 49 | +def get_fg_mock(): |
| 50 | + with patch.object( |
| 51 | + feature_registry_service_client.FeatureRegistryServiceClient, |
| 52 | + "get_feature_group", |
| 53 | + ) as get_fg_mock: |
| 54 | + get_fg_mock.return_value = _TEST_FG1 |
| 55 | + yield get_fg_mock |
| 56 | + |
| 57 | + |
| 58 | +def fg_eq( |
| 59 | + fg_to_check: FeatureGroup, |
| 60 | + name: str, |
| 61 | + resource_name: str, |
| 62 | + source_uri: str, |
| 63 | + entity_id_columns: List[str], |
| 64 | + project: str, |
| 65 | + location: str, |
| 66 | + labels: Dict[str, str], |
| 67 | +): |
| 68 | + """Check if a FeatureGroup has the appropriate values set.""" |
| 69 | + assert fg_to_check.name == name |
| 70 | + assert fg_to_check.resource_name == resource_name |
| 71 | + assert fg_to_check.source == fs_utils.FeatureGroupBigQuerySource( |
| 72 | + uri=source_uri, |
| 73 | + entity_id_columns=entity_id_columns, |
| 74 | + ) |
| 75 | + assert fg_to_check.project == project |
| 76 | + assert fg_to_check.location == location |
| 77 | + assert fg_to_check.labels == labels |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.parametrize( |
| 81 | + "feature_group_name", |
| 82 | + [_TEST_FG1_ID, _TEST_FG1_PATH], |
| 83 | +) |
| 84 | +def test_init(feature_group_name, get_fg_mock): |
| 85 | + aiplatform.init(project=_TEST_PROJECT, location=_TEST_LOCATION) |
| 86 | + |
| 87 | + fg = FeatureGroup(feature_group_name) |
| 88 | + |
| 89 | + get_fg_mock.assert_called_once_with( |
| 90 | + name=_TEST_FG1_PATH, |
| 91 | + retry=base._DEFAULT_RETRY, |
| 92 | + ) |
| 93 | + |
| 94 | + fg_eq( |
| 95 | + fg, |
| 96 | + name=_TEST_FG1_ID, |
| 97 | + resource_name=_TEST_FG1_PATH, |
| 98 | + source_uri=_TEST_FG1_BQ_URI, |
| 99 | + entity_id_columns=_TEST_FG1_ENTITY_ID_COLUMNS, |
| 100 | + project=_TEST_PROJECT, |
| 101 | + location=_TEST_LOCATION, |
| 102 | + labels=_TEST_FG1_LABELS, |
| 103 | + ) |
0 commit comments