|
| 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 | + |
| 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 vertexai import _genai |
| 24 | + |
| 25 | +import pytest |
| 26 | + |
| 27 | +_TEST_PROJECT = "test-project" |
| 28 | +_TEST_LOCATION = "us-central1" |
| 29 | + |
| 30 | + |
| 31 | +pytestmark = pytest.mark.usefixtures("google_auth_mock") |
| 32 | + |
| 33 | + |
| 34 | +class TestEvals: |
| 35 | + """Unit tests for the GenAI client.""" |
| 36 | + |
| 37 | + def setup_method(self): |
| 38 | + importlib.reload(aiplatform_initializer) |
| 39 | + importlib.reload(aiplatform) |
| 40 | + importlib.reload(vertexai) |
| 41 | + vertexai.init( |
| 42 | + project=_TEST_PROJECT, |
| 43 | + location=_TEST_LOCATION, |
| 44 | + ) |
| 45 | + |
| 46 | + @pytest.mark.usefixtures("google_auth_mock") |
| 47 | + def test_evaluate_instances(self): |
| 48 | + test_client = _genai.client.Client( |
| 49 | + project=_TEST_PROJECT, location=_TEST_LOCATION |
| 50 | + ) |
| 51 | + with mock.patch.object( |
| 52 | + test_client.evals, "_evaluate_instances" |
| 53 | + ) as mock_evaluate: |
| 54 | + test_client.evals._evaluate_instances(bleu_input=_genai.types.BleuInput()) |
| 55 | + mock_evaluate.assert_called_once_with(bleu_input=_genai.types.BleuInput()) |
| 56 | + |
| 57 | + @pytest.mark.usefixtures("google_auth_mock") |
| 58 | + def test_eval_run(self): |
| 59 | + test_client = _genai.client.Client( |
| 60 | + project=_TEST_PROJECT, location=_TEST_LOCATION |
| 61 | + ) |
| 62 | + with pytest.raises(NotImplementedError): |
| 63 | + test_client.evals.run() |
| 64 | + |
| 65 | + @pytest.mark.usefixtures("google_auth_mock") |
| 66 | + def test_eval_batch_eval(self): |
| 67 | + test_client = _genai.client.Client( |
| 68 | + project=_TEST_PROJECT, location=_TEST_LOCATION |
| 69 | + ) |
| 70 | + with mock.patch.object(test_client.evals, "batch_eval") as mock_batch_eval: |
| 71 | + test_client.evals.batch_eval( |
| 72 | + dataset=_genai.types.EvaluationDataset(), |
| 73 | + metrics=[_genai.types.Metric()], |
| 74 | + output_config=_genai.types.OutputConfig(), |
| 75 | + autorater_config=_genai.types.AutoraterConfig(), |
| 76 | + config=_genai.types.EvaluateDatasetConfig(), |
| 77 | + ) |
| 78 | + mock_batch_eval.assert_called_once() |
0 commit comments