|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright 2023 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 pytest |
| 19 | + |
| 20 | +from unittest import mock |
| 21 | +from importlib import reload |
| 22 | + |
| 23 | +from google.cloud import aiplatform |
| 24 | +from google.cloud.aiplatform import base |
| 25 | +from google.cloud.aiplatform import initializer |
| 26 | +from google.cloud.aiplatform.preview import _publisher_model |
| 27 | + |
| 28 | +from google.cloud.aiplatform.compat.services import ( |
| 29 | + model_garden_service_client_v1beta1, |
| 30 | +) |
| 31 | + |
| 32 | + |
| 33 | +_TEST_PROJECT = "test-project" |
| 34 | +_TEST_LOCATION = "us-central1" |
| 35 | + |
| 36 | +_TEST_RESOURCE_NAME = "publishers/google/models/chat-bison@001" |
| 37 | +_TEST_MODEL_GARDEN_ID = "google/chat-bison@001" |
| 38 | +_TEST_INVALID_RESOURCE_NAME = "google.chat-bison@001" |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture |
| 42 | +def mock_get_publisher_model(): |
| 43 | + with mock.patch.object( |
| 44 | + model_garden_service_client_v1beta1.ModelGardenServiceClient, |
| 45 | + "get_publisher_model", |
| 46 | + ) as mock_get_publisher_model: |
| 47 | + yield mock_get_publisher_model |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.usefixtures("google_auth_mock") |
| 51 | +class TestPublisherModel: |
| 52 | + def setup_method(self): |
| 53 | + reload(initializer) |
| 54 | + reload(aiplatform) |
| 55 | + |
| 56 | + def teardown_method(self): |
| 57 | + initializer.global_pool.shutdown(wait=True) |
| 58 | + |
| 59 | + def test_init_publisher_model_with_resource_name(self, mock_get_publisher_model): |
| 60 | + aiplatform.init( |
| 61 | + project=_TEST_PROJECT, |
| 62 | + location=_TEST_LOCATION, |
| 63 | + ) |
| 64 | + _ = _publisher_model._PublisherModel(_TEST_RESOURCE_NAME) |
| 65 | + mock_get_publisher_model.assert_called_once_with( |
| 66 | + name=_TEST_RESOURCE_NAME, retry=base._DEFAULT_RETRY |
| 67 | + ) |
| 68 | + |
| 69 | + def test_init_publisher_model_with_model_garden_id(self, mock_get_publisher_model): |
| 70 | + aiplatform.init( |
| 71 | + project=_TEST_PROJECT, |
| 72 | + location=_TEST_LOCATION, |
| 73 | + ) |
| 74 | + _ = _publisher_model._PublisherModel(_TEST_MODEL_GARDEN_ID) |
| 75 | + mock_get_publisher_model.assert_called_once_with( |
| 76 | + name=_TEST_RESOURCE_NAME, retry=base._DEFAULT_RETRY |
| 77 | + ) |
| 78 | + |
| 79 | + def test_init_publisher_model_with_invalid_resource_name( |
| 80 | + self, mock_get_publisher_model |
| 81 | + ): |
| 82 | + aiplatform.init( |
| 83 | + project=_TEST_PROJECT, |
| 84 | + location=_TEST_LOCATION, |
| 85 | + ) |
| 86 | + with pytest.raises( |
| 87 | + ValueError, |
| 88 | + match=( |
| 89 | + f"`{_TEST_INVALID_RESOURCE_NAME}` is not a valid PublisherModel " |
| 90 | + "resource name or model garden id." |
| 91 | + ), |
| 92 | + ): |
| 93 | + _ = _publisher_model._PublisherModel(_TEST_INVALID_RESOURCE_NAME) |
0 commit comments