|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# pylint: disable=protected-access,bad-continuation |
| 16 | +import copy |
| 17 | +import importlib |
| 18 | +from unittest import mock |
| 19 | + |
| 20 | +from google.cloud import aiplatform |
| 21 | +import vertexai |
| 22 | +from google.cloud.aiplatform import initializer as aiplatform_initializer |
| 23 | +from google.cloud.aiplatform.compat.services import job_service_client |
| 24 | +from google.cloud.aiplatform.compat.types import ( |
| 25 | + custom_job as gca_custom_job_compat, |
| 26 | +) |
| 27 | +from google.cloud.aiplatform.compat.types import io as gca_io_compat |
| 28 | +from google.cloud.aiplatform.compat.types import ( |
| 29 | + job_state as gca_job_state_compat, |
| 30 | +) |
| 31 | +from google.cloud.aiplatform.utils import gcs_utils |
| 32 | +from google.genai import client |
| 33 | +import pytest |
| 34 | + |
| 35 | + |
| 36 | +_TEST_PROJECT = "test-project" |
| 37 | +_TEST_LOCATION = "us-central1" |
| 38 | +pytestmark = pytest.mark.usefixtures("google_auth_mock") |
| 39 | +_TEST_PROJECT_NUMBER = "12345678" |
| 40 | +_TEST_PARENT = f"projects/{_TEST_PROJECT}/locations/{_TEST_LOCATION}" |
| 41 | +_TEST_DISPLAY_NAME = f"{_TEST_PARENT}/customJobs/12345" |
| 42 | +_TEST_BASE_OUTPUT_DIR = "gs://test_bucket/test_base_output_dir" |
| 43 | + |
| 44 | +_TEST_CUSTOM_JOB_PROTO = gca_custom_job_compat.CustomJob( |
| 45 | + display_name=_TEST_DISPLAY_NAME, |
| 46 | + job_spec={ |
| 47 | + "base_output_directory": gca_io_compat.GcsDestination( |
| 48 | + output_uri_prefix=_TEST_BASE_OUTPUT_DIR |
| 49 | + ), |
| 50 | + }, |
| 51 | + labels={"trained_by_vertex_ai": "true"}, |
| 52 | +) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture |
| 56 | +def mock_create_custom_job(): |
| 57 | + with mock.patch.object( |
| 58 | + job_service_client.JobServiceClient, "create_custom_job" |
| 59 | + ) as create_custom_job_mock: |
| 60 | + custom_job_proto = copy.deepcopy(_TEST_CUSTOM_JOB_PROTO) |
| 61 | + custom_job_proto.name = _TEST_DISPLAY_NAME |
| 62 | + custom_job_proto.state = gca_job_state_compat.JobState.JOB_STATE_PENDING |
| 63 | + create_custom_job_mock.return_value = custom_job_proto |
| 64 | + yield create_custom_job_mock |
| 65 | + |
| 66 | + |
| 67 | +class TestPromptOptimizer: |
| 68 | + """Unit tests for the Prompt Optimizer client.""" |
| 69 | + |
| 70 | + def setup_method(self): |
| 71 | + importlib.reload(aiplatform_initializer) |
| 72 | + importlib.reload(aiplatform) |
| 73 | + importlib.reload(vertexai) |
| 74 | + vertexai.init( |
| 75 | + project=_TEST_PROJECT, |
| 76 | + location=_TEST_LOCATION, |
| 77 | + ) |
| 78 | + |
| 79 | + @pytest.mark.usefixtures("google_auth_mock") |
| 80 | + def test_prompt_optimizer_client(self): |
| 81 | + test_client = vertexai.Client(project=_TEST_PROJECT, location=_TEST_LOCATION) |
| 82 | + assert test_client is not None |
| 83 | + assert test_client._api_client.vertexai |
| 84 | + assert test_client._api_client.project == _TEST_PROJECT |
| 85 | + assert test_client._api_client.location == _TEST_LOCATION |
| 86 | + |
| 87 | + @mock.patch.object(client.Client, "_get_api_client") |
| 88 | + @mock.patch.object( |
| 89 | + gcs_utils.resource_manager_utils, "get_project_number", return_value=12345 |
| 90 | + ) |
| 91 | + def test_prompt_optimizer_optimize( |
| 92 | + self, mock_get_project_number, mock_client, mock_create_custom_job |
| 93 | + ): |
| 94 | + """Test that prompt_optimizer.optimize method creates a custom job.""" |
| 95 | + test_client = vertexai.Client(project=_TEST_PROJECT, location=_TEST_LOCATION) |
| 96 | + test_client.prompt_optimizer.optimize( |
| 97 | + method="vapo", |
| 98 | + config={ |
| 99 | + "config_path": "gs://ssusie-vapo-sdk-test/config.json", |
| 100 | + "wait_for_completion": False, |
| 101 | + }, |
| 102 | + ) |
| 103 | + mock_create_custom_job.assert_called_once() |
| 104 | + mock_get_project_number.assert_called_once() |
0 commit comments