|
| 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 | +# https://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 | +from unittest.mock import MagicMock, patch |
| 16 | + |
| 17 | +from google.genai import types |
| 18 | + |
| 19 | +import tuning_job_create |
| 20 | +import tuning_job_get |
| 21 | +import tuning_job_list |
| 22 | +import tuning_textgen_with_txt |
| 23 | + |
| 24 | + |
| 25 | +@patch("google.genai.Client") |
| 26 | +def test_tuning_job_create(mock_genai_client: MagicMock) -> None: |
| 27 | + # Mock the API response |
| 28 | + mock_tuning_job = types.TuningJob( |
| 29 | + name="test-tuning-job", |
| 30 | + experiment="test-experiment", |
| 31 | + tuned_model=types.TunedModel( |
| 32 | + model="test-model", |
| 33 | + endpoint="test-endpoint" |
| 34 | + ) |
| 35 | + ) |
| 36 | + mock_genai_client.return_value.tunings.tune.return_value = mock_tuning_job |
| 37 | + |
| 38 | + response = tuning_job_create.create_tuning_job() |
| 39 | + |
| 40 | + mock_genai_client.assert_called_once_with(http_options=types.HttpOptions(api_version="v1")) |
| 41 | + mock_genai_client.return_value.tunings.tune.assert_called_once() |
| 42 | + assert response == "test-tuning-job" |
| 43 | + |
| 44 | + |
| 45 | +@patch("google.genai.Client") |
| 46 | +def test_tuning_job_get(mock_genai_client: MagicMock) -> None: |
| 47 | + # Mock the API response |
| 48 | + mock_tuning_job = types.TuningJob( |
| 49 | + name="test-tuning-job", |
| 50 | + experiment="test-experiment", |
| 51 | + tuned_model=types.TunedModel( |
| 52 | + model="test-model", |
| 53 | + endpoint="test-endpoint" |
| 54 | + ) |
| 55 | + ) |
| 56 | + mock_genai_client.return_value.tunings.get.return_value = mock_tuning_job |
| 57 | + |
| 58 | + response = tuning_job_get.get_tuning_job("test-tuning-job") |
| 59 | + |
| 60 | + mock_genai_client.assert_called_once_with(http_options=types.HttpOptions(api_version="v1")) |
| 61 | + mock_genai_client.return_value.tunings.get.assert_called_once() |
| 62 | + assert response == "test-tuning-job" |
| 63 | + |
| 64 | + |
| 65 | +@patch("google.genai.Client") |
| 66 | +def test_tuning_job_list(mock_genai_client: MagicMock) -> None: |
| 67 | + # Mock the API response |
| 68 | + mock_tuning_job = types.TuningJob( |
| 69 | + name="test-tuning-job", |
| 70 | + experiment="test-experiment", |
| 71 | + tuned_model=types.TunedModel( |
| 72 | + model="test-model", |
| 73 | + endpoint="test-endpoint" |
| 74 | + ) |
| 75 | + ) |
| 76 | + mock_genai_client.return_value.tunings.list.return_value = [mock_tuning_job] |
| 77 | + |
| 78 | + tuning_job_list.list_tuning_jobs() |
| 79 | + |
| 80 | + mock_genai_client.assert_called_once_with(http_options=types.HttpOptions(api_version="v1")) |
| 81 | + mock_genai_client.return_value.tunings.list.assert_called_once() |
| 82 | + |
| 83 | + |
| 84 | +@patch("google.genai.Client") |
| 85 | +def test_tuning_textgen_with_txt(mock_genai_client: MagicMock) -> None: |
| 86 | + # Mock the API response |
| 87 | + mock_tuning_job = types.TuningJob( |
| 88 | + name="test-tuning-job", |
| 89 | + experiment="test-experiment", |
| 90 | + tuned_model=types.TunedModel( |
| 91 | + model="test-model", |
| 92 | + endpoint="test-endpoint" |
| 93 | + ) |
| 94 | + ) |
| 95 | + mock_response = types.GenerateContentResponse._from_response( # pylint: disable=protected-access |
| 96 | + response={ |
| 97 | + "candidates": [ |
| 98 | + { |
| 99 | + "content": { |
| 100 | + "parts": [{"text": "This is a mocked answer."}] |
| 101 | + } |
| 102 | + } |
| 103 | + ] |
| 104 | + }, |
| 105 | + kwargs={}, |
| 106 | + ) |
| 107 | + |
| 108 | + mock_genai_client.return_value.tunings.get.return_value = mock_tuning_job |
| 109 | + mock_genai_client.return_value.models.generate_content.return_value = mock_response |
| 110 | + |
| 111 | + tuning_textgen_with_txt.test_tuned_endpoint("test-tuning-job") |
| 112 | + |
| 113 | + mock_genai_client.assert_called_once_with(http_options=types.HttpOptions(api_version="v1")) |
| 114 | + mock_genai_client.return_value.tunings.get.assert_called_once() |
| 115 | + mock_genai_client.return_value.models.generate_content.assert_called_once() |
0 commit comments