2222from vertexai ._genai import (
2323 client as vertexai_genai_client_module ,
2424)
25+ from google .cloud import storage , bigquery
2526from google .genai import _replay_api_client
2627from google .genai import client as google_genai_client_module
28+ from vertexai ._genai import _evals_utils
29+ from vertexai ._genai import prompt_optimizer
2730import pytest
2831
2932IS_KOKORO = os .getenv ("KOKORO_BUILD_NUMBER" ) is not None
@@ -82,11 +85,39 @@ def _get_replay_id(use_vertex: bool, replays_prefix: str) -> str:
8285 return "/" .join ([replays_prefix , test_name ])
8386
8487
88+ EVAL_CONFIG_GCS_URI = (
89+ "gs://vertex-ai-generative-ai-eval-sdk-resources/metrics/text_quality/v1.0.0.yaml"
90+ )
91+
92+
93+ def _mock_read_file_contents_side_effect (uri : str ):
94+ """
95+ Side effect to mock GcsUtils.read_file_contents for eval test test_batch_evaluate.
96+ """
97+ if uri == EVAL_CONFIG_GCS_URI :
98+ # Construct the absolute path to the local mock file.
99+ current_dir = os .path .dirname (__file__ )
100+ local_yaml_path = os .path .join (
101+ current_dir , "test_resources/mock_eval_config.yaml"
102+ )
103+ try :
104+ with open (local_yaml_path , "r" ) as f :
105+ return f .read ()
106+ except FileNotFoundError :
107+ raise FileNotFoundError (
108+ "The mock data file 'mock_eval_config.yaml' was not found."
109+ )
110+
111+ raise ValueError (
112+ f"Unexpected GCS URI '{ uri } ' in replay test. Only "
113+ f"'{ EVAL_CONFIG_GCS_URI } ' is mocked."
114+ )
115+
116+
85117@pytest .fixture
86118def client (use_vertex , replays_prefix , http_options , request ):
87119
88120 mode = request .config .getoption ("--mode" )
89- replays_directory_prefix = request .config .getoption ("--replays-directory-prefix" )
90121 if mode not in ["auto" , "record" , "replay" , "api" , "tap" ]:
91122 raise ValueError ("Invalid mode: " + mode )
92123 test_function_name = request .function .__name__
@@ -114,13 +145,14 @@ def client(use_vertex, replays_prefix, http_options, request):
114145 os .environ ["GOOGLE_CLOUD_LOCATION" ] = "location"
115146 os .environ ["VAPO_CONFIG_PATH" ] = "gs://dummy-test/dummy-config.json"
116147 os .environ ["VAPO_SERVICE_ACCOUNT_PROJECT_NUMBER" ] = "1234567890"
148+ os .environ ["GCS_BUCKET" ] = "test-bucket"
117149
118150 # Set the replay directory to the root directory of the replays.
119151 # This is needed to ensure that the replay files are found.
120152 replays_root_directory = os .path .abspath (
121153 os .path .join (
122154 os .path .dirname (__file__ ),
123- "../../../../../../../../../google/cloud/aiplatform/sdk/genai/replays" ,
155+ "../../../../../../../../../../ google/cloud/aiplatform/sdk/genai/replays" ,
124156 )
125157 )
126158 os .environ ["GOOGLE_GENAI_REPLAYS_DIRECTORY" ] = replays_root_directory
@@ -131,18 +163,46 @@ def client(use_vertex, replays_prefix, http_options, request):
131163 http_options = http_options ,
132164 )
133165
134- replay_client .replays_directory = (
135- f"{ replays_directory_prefix } /google/cloud/aiplatform/sdk/replays/"
136- )
137-
138166 with mock .patch .object (
139167 google_genai_client_module .Client , "_get_api_client"
140168 ) as patch_method :
141169 patch_method .return_value = replay_client
142170 google_genai_client = vertexai_genai_client_module .Client ()
143171
144- # Yield the client so that cleanup can be completed at the end of the test.
145- yield google_genai_client
172+ if mode != "replay" :
173+ yield google_genai_client
174+ else :
175+ # Eval tests make a call to GCS and BigQuery
176+ # Need to mock this so it doesn't call the service in replay mode
177+ with mock .patch .object (storage , "Client" ) as mock_storage_client :
178+ mock_client_instance = mock .MagicMock ()
179+
180+ mock_blob = mock .MagicMock ()
181+
182+ mock_blob .name = "v1.0.0.yaml"
183+
184+ mock_client_instance .list_blobs .return_value = [mock_blob ]
185+
186+ mock_storage_client .return_value = mock_client_instance
187+
188+ with mock .patch .object (bigquery , "Client" ) as mock_bigquery_client :
189+ mock_bigquery_client .return_value = mock .MagicMock ()
190+
191+ with mock .patch .object (
192+ _evals_utils .GcsUtils , "read_file_contents"
193+ ) as mock_read_file_contents :
194+ mock_read_file_contents .side_effect = (
195+ _mock_read_file_contents_side_effect
196+ )
197+
198+ with mock .patch .object (
199+ prompt_optimizer .time , "sleep"
200+ ) as mock_job_wait :
201+ mock_job_wait .return_value = None
202+
203+ google_genai_client = vertexai_genai_client_module .Client ()
204+
205+ # Yield the client so that cleanup can be completed at the end of the test.
206+ yield google_genai_client
146207
147- # Save the replay after the test if we're in recording mode.
148208 replay_client .close ()
0 commit comments