feat: Implement GcsEvalSetResultsManager to handle storage of eval sets on GCS, and refactor eval set results manager

Eval results will be stored as json files under `gs://{bucket_name}/{app_name}/evals/eval_history/`

PiperOrigin-RevId: 770499242
This commit is contained in:
Google Team Member
2025-06-11 23:41:54 -07:00
committed by Copybara-Service
parent 1551bd4f4d
commit 0a5cf45a75
9 changed files with 503 additions and 155 deletions
@@ -0,0 +1,44 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import time
from .eval_result import EvalCaseResult
from .eval_result import EvalSetResult
def _sanitize_eval_set_result_name(eval_set_result_name: str) -> str:
"""Sanitizes the eval set result name."""
return eval_set_result_name.replace("/", "_")
def create_eval_set_result(
app_name: str,
eval_set_id: str,
eval_case_results: list[EvalCaseResult],
) -> EvalSetResult:
"""Creates a new EvalSetResult given eval_case_results."""
timestamp = time.time()
eval_set_result_id = f"{app_name}_{eval_set_id}_{timestamp}"
eval_set_result_name = _sanitize_eval_set_result_name(eval_set_result_id)
eval_set_result = EvalSetResult(
eval_set_result_id=eval_set_result_id,
eval_set_result_name=eval_set_result_name,
eval_set_id=eval_set_id,
eval_case_results=eval_case_results,
creation_timestamp=timestamp,
)
return eval_set_result
+9 -4
View File
@@ -36,8 +36,9 @@ class EvalCaseResult(BaseModel):
populate_by_name=True, populate_by_name=True,
) )
eval_set_file: str = Field( eval_set_file: Optional[str] = Field(
deprecated=True, deprecated=True,
default=None,
description="This field is deprecated, use eval_set_id instead.", description="This field is deprecated, use eval_set_id instead.",
) )
eval_set_id: str = "" eval_set_id: str = ""
@@ -49,12 +50,16 @@ class EvalCaseResult(BaseModel):
final_eval_status: EvalStatus final_eval_status: EvalStatus
"""Final eval status for this eval case.""" """Final eval status for this eval case."""
eval_metric_results: list[tuple[EvalMetric, EvalMetricResult]] = Field( eval_metric_results: Optional[list[tuple[EvalMetric, EvalMetricResult]]] = (
Field(
deprecated=True, deprecated=True,
default=None,
description=( description=(
"This field is deprecated, use overall_eval_metric_results instead." "This field is deprecated, use overall_eval_metric_results"
" instead."
), ),
) )
)
overall_eval_metric_results: list[EvalMetricResult] overall_eval_metric_results: list[EvalMetricResult]
"""Overall result for each metric for the entire eval case.""" """Overall result for each metric for the entire eval case."""
@@ -80,7 +85,7 @@ class EvalSetResult(BaseModel):
populate_by_name=True, populate_by_name=True,
) )
eval_set_result_id: str eval_set_result_id: str
eval_set_result_name: str eval_set_result_name: Optional[str] = None
eval_set_id: str eval_set_id: str
eval_case_results: list[EvalCaseResult] = Field(default_factory=list) eval_case_results: list[EvalCaseResult] = Field(default_factory=list)
creation_timestamp: float = 0.0 creation_timestamp: float = 0.0
@@ -16,6 +16,7 @@ from __future__ import annotations
from abc import ABC from abc import ABC
from abc import abstractmethod from abc import abstractmethod
from typing import Optional
from .eval_result import EvalCaseResult from .eval_result import EvalCaseResult
from .eval_result import EvalSetResult from .eval_result import EvalSetResult
@@ -38,7 +39,11 @@ class EvalSetResultsManager(ABC):
def get_eval_set_result( def get_eval_set_result(
self, app_name: str, eval_set_result_id: str self, app_name: str, eval_set_result_id: str
) -> EvalSetResult: ) -> EvalSetResult:
"""Returns an EvalSetResult identified by app_name and eval_set_result_id.""" """Returns the EvalSetResult from app_name and eval_set_result_id.
Raises:
NotFoundError: If the EvalSetResult is not found.
"""
raise NotImplementedError() raise NotImplementedError()
@abstractmethod @abstractmethod
@@ -0,0 +1,121 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import logging
from google.cloud import exceptions as cloud_exceptions
from google.cloud import storage
from typing_extensions import override
from ..errors.not_found_error import NotFoundError
from ._eval_set_results_manager_utils import create_eval_set_result
from .eval_result import EvalCaseResult
from .eval_result import EvalSetResult
from .eval_set_results_manager import EvalSetResultsManager
logger = logging.getLogger("google_adk." + __name__)
_EVAL_HISTORY_DIR = "evals/eval_history"
_EVAL_SET_RESULT_FILE_EXTENSION = ".evalset_result.json"
class GcsEvalSetResultsManager(EvalSetResultsManager):
"""An EvalSetResultsManager that stores eval results in a GCS bucket."""
def __init__(self, bucket_name: str, **kwargs):
"""Initializes the GcsEvalSetsManager.
Args:
bucket_name: The name of the bucket to use.
**kwargs: Keyword arguments to pass to the Google Cloud Storage client.
"""
self.bucket_name = bucket_name
self.storage_client = storage.Client(**kwargs)
self.bucket = self.storage_client.bucket(self.bucket_name)
# Check if the bucket exists.
if not self.bucket.exists():
raise ValueError(
f"Bucket `{self.bucket_name}` does not exist. Please create it before"
" using the GcsEvalSetsManager."
)
def _get_eval_history_dir(self, app_name: str) -> str:
return f"{app_name}/{_EVAL_HISTORY_DIR}"
def _get_eval_set_result_blob_name(
self, app_name: str, eval_set_result_id: str
) -> str:
eval_history_dir = self._get_eval_history_dir(app_name)
return f"{eval_history_dir}/{eval_set_result_id}{_EVAL_SET_RESULT_FILE_EXTENSION}"
def _write_eval_set_result(
self, blob_name: str, eval_set_result: EvalSetResult
):
"""Writes an EvalSetResult to GCS."""
blob = self.bucket.blob(blob_name)
blob.upload_from_string(
eval_set_result.model_dump_json(indent=2),
content_type="application/json",
)
@override
def save_eval_set_result(
self,
app_name: str,
eval_set_id: str,
eval_case_results: list[EvalCaseResult],
) -> None:
"""Creates and saves a new EvalSetResult given eval_case_results."""
eval_set_result = create_eval_set_result(
app_name, eval_set_id, eval_case_results
)
eval_set_result_blob_name = self._get_eval_set_result_blob_name(
app_name, eval_set_result.eval_set_result_id
)
logger.info("Writing eval result to blob: %s", eval_set_result_blob_name)
self._write_eval_set_result(eval_set_result_blob_name, eval_set_result)
@override
def get_eval_set_result(
self, app_name: str, eval_set_result_id: str
) -> EvalSetResult:
"""Returns an EvalSetResult from app_name and eval_set_result_id."""
eval_set_result_blob_name = self._get_eval_set_result_blob_name(
app_name, eval_set_result_id
)
blob = self.bucket.blob(eval_set_result_blob_name)
if not blob.exists():
raise NotFoundError(f"Eval set result `{eval_set_result_id}` not found.")
eval_set_result_data = blob.download_as_text()
return EvalSetResult.model_validate_json(eval_set_result_data)
@override
def list_eval_set_results(self, app_name: str) -> list[str]:
"""Returns the eval result ids that belong to the given app_name."""
eval_history_dir = self._get_eval_history_dir(app_name)
eval_set_results = []
try:
for blob in self.bucket.list_blobs(prefix=eval_history_dir):
eval_set_result_id = blob.name.split("/")[-1].removesuffix(
_EVAL_SET_RESULT_FILE_EXTENSION
)
eval_set_results.append(eval_set_result_id)
return sorted(eval_set_results)
except cloud_exceptions.NotFound as e:
raise ValueError(
f"App `{app_name}` not found in GCS bucket `{self.bucket_name}`."
) from e
@@ -17,10 +17,11 @@ from __future__ import annotations
import json import json
import logging import logging
import os import os
import time
from typing_extensions import override from typing_extensions import override
from ..errors.not_found_error import NotFoundError
from ._eval_set_results_manager_utils import create_eval_set_result
from .eval_result import EvalCaseResult from .eval_result import EvalCaseResult
from .eval_result import EvalSetResult from .eval_result import EvalSetResult
from .eval_set_results_manager import EvalSetResultsManager from .eval_set_results_manager import EvalSetResultsManager
@@ -31,10 +32,6 @@ _ADK_EVAL_HISTORY_DIR = ".adk/eval_history"
_EVAL_SET_RESULT_FILE_EXTENSION = ".evalset_result.json" _EVAL_SET_RESULT_FILE_EXTENSION = ".evalset_result.json"
def _sanitize_eval_set_result_name(eval_set_result_name: str) -> str:
return eval_set_result_name.replace("/", "_")
class LocalEvalSetResultsManager(EvalSetResultsManager): class LocalEvalSetResultsManager(EvalSetResultsManager):
"""An EvalSetResult manager that stores eval set results locally on disk.""" """An EvalSetResult manager that stores eval set results locally on disk."""
@@ -49,15 +46,8 @@ class LocalEvalSetResultsManager(EvalSetResultsManager):
eval_case_results: list[EvalCaseResult], eval_case_results: list[EvalCaseResult],
) -> None: ) -> None:
"""Creates and saves a new EvalSetResult given eval_case_results.""" """Creates and saves a new EvalSetResult given eval_case_results."""
timestamp = time.time() eval_set_result = create_eval_set_result(
eval_set_result_id = app_name + "_" + eval_set_id + "_" + str(timestamp) app_name, eval_set_id, eval_case_results
eval_set_result_name = _sanitize_eval_set_result_name(eval_set_result_id)
eval_set_result = EvalSetResult(
eval_set_result_id=eval_set_result_id,
eval_set_result_name=eval_set_result_name,
eval_set_id=eval_set_id,
eval_case_results=eval_case_results,
creation_timestamp=timestamp,
) )
# Write eval result file, with eval_set_result_name. # Write eval result file, with eval_set_result_name.
app_eval_history_dir = self._get_eval_history_dir(app_name) app_eval_history_dir = self._get_eval_history_dir(app_name)
@@ -67,7 +57,7 @@ class LocalEvalSetResultsManager(EvalSetResultsManager):
eval_set_result_json = eval_set_result.model_dump_json() eval_set_result_json = eval_set_result.model_dump_json()
eval_set_result_file_path = os.path.join( eval_set_result_file_path = os.path.join(
app_eval_history_dir, app_eval_history_dir,
eval_set_result_name + _EVAL_SET_RESULT_FILE_EXTENSION, eval_set_result.eval_set_result_name + _EVAL_SET_RESULT_FILE_EXTENSION,
) )
logger.info("Writing eval result to file: %s", eval_set_result_file_path) logger.info("Writing eval result to file: %s", eval_set_result_file_path)
with open(eval_set_result_file_path, "w") as f: with open(eval_set_result_file_path, "w") as f:
@@ -87,9 +77,7 @@ class LocalEvalSetResultsManager(EvalSetResultsManager):
+ _EVAL_SET_RESULT_FILE_EXTENSION + _EVAL_SET_RESULT_FILE_EXTENSION
) )
if not os.path.exists(maybe_eval_result_file_path): if not os.path.exists(maybe_eval_result_file_path):
raise ValueError( raise NotFoundError(f"Eval set result `{eval_set_result_id}` not found.")
f"Eval set result `{eval_set_result_id}` does not exist."
)
with open(maybe_eval_result_file_path, "r") as file: with open(maybe_eval_result_file_path, "r") as file:
eval_result_data = json.load(file) eval_result_data = json.load(file)
return EvalSetResult.model_validate_json(eval_result_data) return EvalSetResult.model_validate_json(eval_result_data)
@@ -0,0 +1,117 @@
from typing import Optional
from typing import Union
class MockBlob:
"""Mocks a GCS Blob object.
This class provides mock implementations for a few common GCS Blob methods,
allowing the user to test code that interacts with GCS without actually
connecting to a real bucket.
"""
def __init__(self, name: str) -> None:
"""Initializes a MockBlob.
Args:
name: The name of the blob.
"""
self.name = name
self.content: Optional[bytes] = None
self.content_type: Optional[str] = None
self._exists: bool = False
def upload_from_string(
self, data: Union[str, bytes], content_type: Optional[str] = None
) -> None:
"""Mocks uploading data to the blob (from a string or bytes).
Args:
data: The data to upload (string or bytes).
content_type: The content type of the data (optional).
"""
if isinstance(data, str):
self.content = data.encode("utf-8")
elif isinstance(data, bytes):
self.content = data
else:
raise TypeError("data must be str or bytes")
if content_type:
self.content_type = content_type
self._exists = True
def download_as_text(self) -> str:
"""Mocks downloading the blob's content as text.
Returns:
str: The content of the blob as text.
Raises:
Exception: If the blob doesn't exist (hasn't been uploaded to).
"""
if self.content is None:
return b""
return self.content
def delete(self) -> None:
"""Mocks deleting a blob."""
self.content = None
self.content_type = None
self._exists = False
def exists(self) -> bool:
"""Mocks checking if the blob exists."""
return self._exists
class MockBucket:
"""Mocks a GCS Bucket object."""
def __init__(self, name: str) -> None:
"""Initializes a MockBucket.
Args:
name: The name of the bucket.
"""
self.name = name
self.blobs: dict[str, MockBlob] = {}
def blob(self, blob_name: str) -> MockBlob:
"""Mocks getting a Blob object (doesn't create it in storage).
Args:
blob_name: The name of the blob.
Returns:
A MockBlob instance.
"""
if blob_name not in self.blobs:
self.blobs[blob_name] = MockBlob(blob_name)
return self.blobs[blob_name]
def list_blobs(self, prefix: Optional[str] = None) -> list[MockBlob]:
"""Mocks listing blobs in a bucket, optionally with a prefix."""
if prefix:
return [
blob for name, blob in self.blobs.items() if name.startswith(prefix)
]
return list(self.blobs.values())
def exists(self) -> bool:
"""Mocks checking if the bucket exists."""
return True
class MockClient:
"""Mocks the GCS Client."""
def __init__(self) -> None:
"""Initializes MockClient."""
self.buckets: dict[str, MockBucket] = {}
def bucket(self, bucket_name: str) -> MockBucket:
"""Mocks getting a Bucket object."""
if bucket_name not in self.buckets:
self.buckets[bucket_name] = MockBucket(bucket_name)
return self.buckets[bucket_name]
@@ -0,0 +1,191 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from google.adk.errors.not_found_error import NotFoundError
from google.adk.evaluation._eval_set_results_manager_utils import _sanitize_eval_set_result_name
from google.adk.evaluation._eval_set_results_manager_utils import create_eval_set_result
from google.adk.evaluation.eval_case import Invocation
from google.adk.evaluation.eval_metrics import EvalMetricResult
from google.adk.evaluation.eval_metrics import EvalMetricResultPerInvocation
from google.adk.evaluation.eval_result import EvalCaseResult
from google.adk.evaluation.evaluator import EvalStatus
from google.adk.evaluation.gcs_eval_set_results_manager import GcsEvalSetResultsManager
from google.genai import types as genai_types
import pytest
from .mock_gcs_utils import MockBucket
from .mock_gcs_utils import MockClient
def _get_test_eval_case_results():
# Create mock Invocation objects
actual_invocation_1 = Invocation(
invocation_id="actual_1",
user_content=genai_types.Content(
parts=[genai_types.Part(text="input_1")]
),
)
expected_invocation_1 = Invocation(
invocation_id="expected_1",
user_content=genai_types.Content(
parts=[genai_types.Part(text="expected_input_1")]
),
)
actual_invocation_2 = Invocation(
invocation_id="actual_2",
user_content=genai_types.Content(
parts=[genai_types.Part(text="input_2")]
),
)
expected_invocation_2 = Invocation(
invocation_id="expected_2",
user_content=genai_types.Content(
parts=[genai_types.Part(text="expected_input_2")]
),
)
eval_metric_result_1 = EvalMetricResult(
metric_name="metric",
threshold=0.8,
score=1.0,
eval_status=EvalStatus.PASSED,
)
eval_metric_result_2 = EvalMetricResult(
metric_name="metric",
threshold=0.8,
score=0.5,
eval_status=EvalStatus.FAILED,
)
eval_metric_result_per_invocation_1 = EvalMetricResultPerInvocation(
actual_invocation=actual_invocation_1,
expected_invocation=expected_invocation_1,
eval_metric_results=[eval_metric_result_1],
)
eval_metric_result_per_invocation_2 = EvalMetricResultPerInvocation(
actual_invocation=actual_invocation_2,
expected_invocation=expected_invocation_2,
eval_metric_results=[eval_metric_result_2],
)
return [
EvalCaseResult(
eval_set_id="eval_set",
eval_id="eval_case_1",
final_eval_status=EvalStatus.PASSED,
overall_eval_metric_results=[eval_metric_result_1],
eval_metric_result_per_invocation=[
eval_metric_result_per_invocation_1
],
session_id="session_1",
),
EvalCaseResult(
eval_set_id="eval_set",
eval_id="eval_case_2",
final_eval_status=EvalStatus.FAILED,
overall_eval_metric_results=[eval_metric_result_2],
eval_metric_result_per_invocation=[
eval_metric_result_per_invocation_2
],
session_id="session_2",
),
]
class TestGcsEvalSetResultsManager:
@pytest.fixture
def gcs_eval_set_results_manager(self, mocker):
mock_storage_client = MockClient()
bucket_name = "test_bucket"
mock_bucket = MockBucket(bucket_name)
mocker.patch.object(mock_storage_client, "bucket", return_value=mock_bucket)
mocker.patch(
"google.cloud.storage.Client", return_value=mock_storage_client
)
return GcsEvalSetResultsManager(bucket_name=bucket_name)
def test_save_eval_set_result(self, gcs_eval_set_results_manager, mocker):
mocker.patch("time.time", return_value=12345678)
app_name = "test_app"
eval_set_id = "test_eval_set"
eval_case_results = _get_test_eval_case_results()
eval_set_result = create_eval_set_result(
app_name, eval_set_id, eval_case_results
)
blob_name = gcs_eval_set_results_manager._get_eval_set_result_blob_name(
app_name, eval_set_result.eval_set_result_id
)
mock_write_eval_set_result = mocker.patch.object(
gcs_eval_set_results_manager,
"_write_eval_set_result",
)
gcs_eval_set_results_manager.save_eval_set_result(
app_name, eval_set_id, eval_case_results
)
mock_write_eval_set_result.assert_called_once_with(
blob_name,
eval_set_result,
)
def test_get_eval_set_result_not_found(
self, gcs_eval_set_results_manager, mocker
):
mocker.patch("time.time", return_value=12345678)
app_name = "test_app"
with pytest.raises(NotFoundError) as e:
gcs_eval_set_results_manager.get_eval_set_result(
app_name, "non_existent_id"
)
def test_get_eval_set_result(self, gcs_eval_set_results_manager, mocker):
mocker.patch("time.time", return_value=12345678)
app_name = "test_app"
eval_set_id = "test_eval_set"
eval_case_results = _get_test_eval_case_results()
gcs_eval_set_results_manager.save_eval_set_result(
app_name, eval_set_id, eval_case_results
)
eval_set_result = create_eval_set_result(
app_name, eval_set_id, eval_case_results
)
retrieved_eval_set_result = (
gcs_eval_set_results_manager.get_eval_set_result(
app_name, eval_set_result.eval_set_result_id
)
)
assert retrieved_eval_set_result == eval_set_result
def test_list_eval_set_results(self, gcs_eval_set_results_manager, mocker):
mocker.patch("time.time", return_value=123)
app_name = "test_app"
eval_set_ids = ["test_eval_set_1", "test_eval_set_2", "test_eval_set_3"]
for eval_set_id in eval_set_ids:
eval_case_results = _get_test_eval_case_results()
gcs_eval_set_results_manager.save_eval_set_result(
app_name, eval_set_id, eval_case_results
)
retrieved_eval_set_result_ids = (
gcs_eval_set_results_manager.list_eval_set_results(app_name)
)
assert retrieved_eval_set_result_ids == [
"test_app_test_eval_set_1_123",
"test_app_test_eval_set_2_123",
"test_app_test_eval_set_3_123",
]
def test_list_eval_set_results_empty(self, gcs_eval_set_results_manager):
app_name = "test_app"
retrieved_eval_set_result_ids = (
gcs_eval_set_results_manager.list_eval_set_results(app_name)
)
assert retrieved_eval_set_result_ids == []
@@ -12,9 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from typing import Optional
from typing import Union
from google.adk.errors.not_found_error import NotFoundError from google.adk.errors.not_found_error import NotFoundError
from google.adk.evaluation.eval_case import EvalCase from google.adk.evaluation.eval_case import EvalCase
from google.adk.evaluation.eval_set import EvalSet from google.adk.evaluation.eval_set import EvalSet
@@ -22,120 +19,9 @@ from google.adk.evaluation.gcs_eval_sets_manager import _EVAL_SET_FILE_EXTENSION
from google.adk.evaluation.gcs_eval_sets_manager import GcsEvalSetsManager from google.adk.evaluation.gcs_eval_sets_manager import GcsEvalSetsManager
import pytest import pytest
from .mock_gcs_utils import MockBlob
class MockBlob: from .mock_gcs_utils import MockBucket
"""Mocks a GCS Blob object. from .mock_gcs_utils import MockClient
This class provides mock implementations for a few common GCS Blob methods,
allowing the user to test code that interacts with GCS without actually
connecting to a real bucket.
"""
def __init__(self, name: str) -> None:
"""Initializes a MockBlob.
Args:
name: The name of the blob.
"""
self.name = name
self.content: Optional[bytes] = None
self.content_type: Optional[str] = None
self._exists: bool = False
def upload_from_string(
self, data: Union[str, bytes], content_type: Optional[str] = None
) -> None:
"""Mocks uploading data to the blob (from a string or bytes).
Args:
data: The data to upload (string or bytes).
content_type: The content type of the data (optional).
"""
if isinstance(data, str):
self.content = data.encode("utf-8")
elif isinstance(data, bytes):
self.content = data
else:
raise TypeError("data must be str or bytes")
if content_type:
self.content_type = content_type
self._exists = True
def download_as_text(self) -> str:
"""Mocks downloading the blob's content as text.
Returns:
str: The content of the blob as text.
Raises:
Exception: If the blob doesn't exist (hasn't been uploaded to).
"""
if self.content is None:
return b""
return self.content
def delete(self) -> None:
"""Mocks deleting a blob."""
self.content = None
self.content_type = None
self._exists = False
def exists(self) -> bool:
"""Mocks checking if the blob exists."""
return self._exists
class MockBucket:
"""Mocks a GCS Bucket object."""
def __init__(self, name: str) -> None:
"""Initializes a MockBucket.
Args:
name: The name of the bucket.
"""
self.name = name
self.blobs: dict[str, MockBlob] = {}
def blob(self, blob_name: str) -> MockBlob:
"""Mocks getting a Blob object (doesn't create it in storage).
Args:
blob_name: The name of the blob.
Returns:
A MockBlob instance.
"""
if blob_name not in self.blobs:
self.blobs[blob_name] = MockBlob(blob_name)
return self.blobs[blob_name]
def list_blobs(self, prefix: Optional[str] = None) -> list[MockBlob]:
"""Mocks listing blobs in a bucket, optionally with a prefix."""
if prefix:
return [
blob for name, blob in self.blobs.items() if name.startswith(prefix)
]
return list(self.blobs.values())
def exists(self) -> bool:
"""Mocks checking if the bucket exists."""
return True
class MockClient:
"""Mocks the GCS Client."""
def __init__(self) -> None:
"""Initializes MockClient."""
self.buckets: dict[str, MockBucket] = {}
def bucket(self, bucket_name: str) -> MockBucket:
"""Mocks getting a Bucket object."""
if bucket_name not in self.buckets:
self.buckets[bucket_name] = MockBucket(bucket_name)
return self.buckets[bucket_name]
class TestGcsEvalSetsManager: class TestGcsEvalSetsManager:
@@ -21,24 +21,17 @@ import tempfile
import time import time
from unittest.mock import patch from unittest.mock import patch
from google.adk.errors.not_found_error import NotFoundError
from google.adk.evaluation._eval_set_results_manager_utils import _sanitize_eval_set_result_name
from google.adk.evaluation.eval_result import EvalCaseResult from google.adk.evaluation.eval_result import EvalCaseResult
from google.adk.evaluation.eval_result import EvalSetResult from google.adk.evaluation.eval_result import EvalSetResult
from google.adk.evaluation.evaluator import EvalStatus from google.adk.evaluation.evaluator import EvalStatus
from google.adk.evaluation.local_eval_set_results_manager import _ADK_EVAL_HISTORY_DIR from google.adk.evaluation.local_eval_set_results_manager import _ADK_EVAL_HISTORY_DIR
from google.adk.evaluation.local_eval_set_results_manager import _EVAL_SET_RESULT_FILE_EXTENSION from google.adk.evaluation.local_eval_set_results_manager import _EVAL_SET_RESULT_FILE_EXTENSION
from google.adk.evaluation.local_eval_set_results_manager import _sanitize_eval_set_result_name
from google.adk.evaluation.local_eval_set_results_manager import LocalEvalSetResultsManager from google.adk.evaluation.local_eval_set_results_manager import LocalEvalSetResultsManager
import pytest import pytest
def test_sanitize_eval_set_result_name():
assert _sanitize_eval_set_result_name("app/name") == "app_name"
assert _sanitize_eval_set_result_name("app_name") == "app_name"
assert _sanitize_eval_set_result_name("app/name/with/slashes") == (
"app_name_with_slashes"
)
class TestLocalEvalSetResultsManager: class TestLocalEvalSetResultsManager:
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
@@ -115,11 +108,9 @@ class TestLocalEvalSetResultsManager:
def test_get_eval_set_result_not_found(self, mock_time): def test_get_eval_set_result_not_found(self, mock_time):
mock_time.return_value = self.timestamp mock_time.return_value = self.timestamp
with pytest.raises(ValueError) as e: with pytest.raises(NotFoundError) as e:
self.manager.get_eval_set_result(self.app_name, "non_existent_id") self.manager.get_eval_set_result(self.app_name, "non_existent_id")
assert "does not exist" in str(e.value)
@patch("time.time") @patch("time.time")
def test_list_eval_set_results(self, mock_time): def test_list_eval_set_results(self, mock_time):
mock_time.return_value = self.timestamp mock_time.return_value = self.timestamp