-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathTableTest.java
More file actions
488 lines (446 loc) · 19.5 KB
/
Copy pathTableTest.java
File metadata and controls
488 lines (446 loc) · 19.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
* 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
*
* https://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.
*/
/** Runs the test table. */
package com.google.genai;
import static org.junit.jupiter.api.Assertions.fail;
import com.google.common.collect.ImmutableMap;
import com.google.genai.types.TestTableFile;
import com.google.genai.types.TestTableItem;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
/** Sample class to prototype GenAI SDK functionalities. */
public final class TableTest {
private static Collection<DynamicTest> createTableTests(String path, boolean vertexAI)
throws IOException {
String suffix = vertexAI ? "vertex" : "mldev";
// Reads JSON.
String data = ReplayApiClient.readString(Paths.get(path));
TestTableFile testTableFile = TestTableFile.fromJson(data);
int testTableIndex = path.lastIndexOf("/_test_table.json");
int replaysTestsIndex = path.lastIndexOf("/replays/tests/");
String testDirectory =
path.substring(replaysTestsIndex + "/replays/tests/".length(), testTableIndex);
// Gets module name and method name.
String testMethod = testTableFile.testMethod().get();
String[] segments = testMethod.split("\\.");
if (segments.length == 1) {
if (MultistepTest.customTestMethods.containsKey(testDirectory)) {
List<DynamicTest> dynamicTests = new ArrayList<>();
for (TestTableItem testTableItem : testTableFile.testTable().get()) {
String testName =
String.format("%s.%s.%s", testMethod, testTableItem.name().get(), suffix);
String replayId = testTableItem.name().get();
if (testTableItem.overrideReplayId().isPresent()) {
replayId = testTableItem.overrideReplayId().get();
}
String clientReplayId = testDirectory + "/" + replayId + "." + suffix + ".json";
dynamicTests.addAll(
createTestCasesForMultistep(
testName, testTableItem, vertexAI, testDirectory, clientReplayId));
}
return dynamicTests;
}
String msg =
" => Test skipped: multistep test "
+ testMethod
+ " ("
+ testDirectory
+ ") not supported in Java";
List<DynamicTest> dynamicTests = new ArrayList<>();
for (TestTableItem testTableItem : testTableFile.testTable().get()) {
String testName = String.format("%s.%s.%s", testMethod, testTableItem.name().get(), suffix);
dynamicTests.add(DynamicTest.dynamicTest(testName + msg, () -> {}));
}
return dynamicTests;
}
String originalMethodName = segments[segments.length - 1];
String methodName = Common.snakeToCamel(originalMethodName);
if (methodName.equals("embedContent")) {
methodName = "embedContentTest";
}
List<String> modulePath = new ArrayList<>();
for (int i = 0; i < segments.length - 1; i++) {
modulePath.add(segments[i]);
}
String originalModuleName = String.join(".", modulePath);
if (methodName.equals("privateTune") && !vertexAI) {
methodName = "privateTuneMldev";
}
String[] replayPathSegments = path.split("/");
String replayFilePath = "";
for (int i = 0; i < replayPathSegments.length; i++) {
if (replayPathSegments[i].endsWith("_test_table.json")) {
break;
}
replayFilePath += replayPathSegments[i] + "/";
}
// Finds the module and methods.
List<Method> methods = new ArrayList<>();
try {
// Traverse the module path to find the final module class.
Class<?> currentClass = Client.class;
for (String moduleSegment : modulePath) {
String camelSegment = Common.snakeToCamel(moduleSegment);
Field field = currentClass.getDeclaredField(camelSegment);
currentClass = field.getType();
}
Class<?> moduleClass = currentClass;
for (Method candidate : moduleClass.getDeclaredMethods()) {
if (candidate.getName().equals(methodName)) {
methods.add(candidate);
}
if (methodName.equals("generateVideos")
&& candidate.getName().equals("privateGenerateVideos")) {
candidate.setAccessible(true);
methods.add(candidate);
}
}
if (methods.isEmpty()) {
throw new NoSuchMethodException();
}
} catch (NoSuchFieldException | NoSuchMethodException e) {
String msg = " => Test skipped: method " + testMethod + " is not supported in Java";
String testName = String.format("%s.%s.%s", originalModuleName, originalMethodName, suffix);
return Collections.singletonList(DynamicTest.dynamicTest(testName + msg, () -> {}));
}
List<DynamicTest> dynamicTests = new ArrayList<>();
// Processes.
for (TestTableItem testTableItem : testTableFile.testTable().get()) {
String testName =
String.format(
"%s.%s.%s.%s",
originalModuleName, originalMethodName, testTableItem.name().get(), suffix);
String replayId = testTableItem.name().get();
if (testTableItem.overrideReplayId().isPresent()) {
replayId = testTableItem.overrideReplayId().get();
}
String clientReplayId = testDirectory + "/" + replayId + "." + suffix + ".json";
List<String> parameterNames = testTableFile.parameterNames().get();
// TODO(b/417799716): Fix generate_image tests in API mode.
if (testName.contains("models.generate_images.test_simple_prompt_001.mldev")) {
continue;
}
dynamicTests.addAll(
createTestCases(
testName,
testTableItem,
modulePath,
vertexAI,
methods,
parameterNames,
clientReplayId));
}
return dynamicTests;
}
@SuppressWarnings("unchecked")
private static Collection<DynamicTest> createTestCases(
String testName,
TestTableItem testTableItem,
List<String> modulePath,
boolean vertexAI,
List<Method> methods,
List<String> parameterNames,
String replayId) {
Client client = createClient(vertexAI);
if (testTableItem.hasUnion().isPresent() && testTableItem.hasUnion().get()) {
String msg = " => Test skipped: parameters contain unsupported union type";
return Collections.singletonList(DynamicTest.dynamicTest(testName + msg, () -> {}));
}
// Edit image ReferenceImages are not correctly deserialized for replay tests
if (testName.contains("models.edit_image")
|| testName.contains("batches.create.test_with_image_blob")) { // TODO(b/431798111)
String msg = " => Test skipped: replay tests are not supported for edit_image";
return Collections.singletonList(DynamicTest.dynamicTest(testName + msg, () -> {}));
}
// TODO(b/457846189): Support models.list filter parameter
if (testName.contains("models.list.test_tuned_models_with_filter")
|| testName.contains("models.list.test_tuned_models.vertex")) {
String msg = " => Test skipped: replay tests are not supported for models.list with filter";
return Collections.singletonList(DynamicTest.dynamicTest(testName + msg, () -> {}));
}
Map<String, Object> fromParameters = prepareParameters(testTableItem);
List<DynamicTest> dynamicTests = new ArrayList<>();
// Iterate through overloading methods and find a match.
for (Method method : methods) {
try {
List<Object> parameters = new ArrayList<>();
for (int i = 0; i < parameterNames.size(); i++) {
// May throw IndexOutOfBoundsException here
String parameterName = Common.snakeToCamel(parameterNames.get(i));
Object fromValue = fromParameters.getOrDefault(parameterName, null);
// May throw IllegalArgumentException here
Object parameter =
JsonSerializable.objectMapper.convertValue(fromValue, method.getParameterTypes()[i]);
if (method.getName().equals("embedContent") && parameter instanceof List) {
throw new IllegalArgumentException();
}
parameters.add(parameter);
}
Optional<String> skipMsg = getSkipMessageInApiMode(testTableItem, client);
if (skipMsg.isPresent()) {
dynamicTests.add(DynamicTest.dynamicTest(testName + skipMsg.get(), () -> {}));
continue;
}
dynamicTests.add(
DynamicTest.dynamicTest(
testName,
() -> {
try {
client.setReplayId(replayId);
// Get the target module instance by traversing the client object
Object targetModuleInstance = client;
for (String moduleSegment : modulePath) {
String camelSegment = Common.snakeToCamel(moduleSegment);
Field field = targetModuleInstance.getClass().getDeclaredField(camelSegment);
field.setAccessible(true);
targetModuleInstance = field.get(targetModuleInstance);
}
// May throw IllegalAccessException or InvocationTargetException here
// InvocationTargetException is sometimes expected, when exceptionIfMldev or
// exceptionIfVertex is present.
Object response = method.invoke(targetModuleInstance, parameters.toArray());
} catch (IllegalAccessException
| InvocationTargetException
| IllegalArgumentException
| NoSuchFieldException e) {
Throwable cause = e instanceof InvocationTargetException ? e.getCause() : e;
handleException(cause, testTableItem, client, testName);
} finally {
client.close();
}
}));
} catch (IndexOutOfBoundsException | IllegalArgumentException e) {
// Method parameters do not match, continue to the next overloading method
}
}
if (dynamicTests.isEmpty()) {
String msg =
String.format(
"Could not find a method '%s' that matches the parameters: %s",
testName, fromParameters);
dynamicTests.add(DynamicTest.dynamicTest(testName, () -> fail(msg)));
}
return dynamicTests;
}
@SuppressWarnings("unchecked")
private static Collection<DynamicTest> createTestCasesForMultistep(
String testName,
TestTableItem testTableItem,
boolean vertexAI,
String customMethodKey,
String replayId) {
Client client = createClient(vertexAI);
List<DynamicTest> dynamicTests = new ArrayList<>();
if (client.clientMode().equals("replay")) {
String msg = " => Test skipped: multistep tests run in api mode only";
dynamicTests.add(DynamicTest.dynamicTest(testName + msg, () -> {}));
return dynamicTests;
}
Map<String, Object> fromParameters = prepareParameters(testTableItem);
Optional<String> skipMsg = getSkipMessageInApiMode(testTableItem, client);
if (skipMsg.isPresent()) {
dynamicTests.add(DynamicTest.dynamicTest(testName + skipMsg.get(), () -> {}));
return dynamicTests;
}
dynamicTests.add(
DynamicTest.dynamicTest(
testName,
() -> {
try {
client.setReplayId(replayId);
MultistepTest.MultistepFunction method =
MultistepTest.customTestMethods.get(customMethodKey);
Object response = method.apply(client, fromParameters);
} catch (Exception e) {
Throwable cause = e instanceof InvocationTargetException ? e.getCause() : e;
handleException(cause, testTableItem, client, testName);
} finally {
client.close();
}
}));
return dynamicTests;
}
@SuppressWarnings("unchecked")
private static Map<String, Object> prepareParameters(TestTableItem testTableItem) {
Map<String, Object> fromParameters =
(Map<String, Object>) normalizeKeys((Map<String, Object>) testTableItem.parameters().get());
ReplaySanitizer.sanitizeMapByPath(
fromParameters, "image.imageBytes", new ReplayBase64Sanitizer(), false);
ReplaySanitizer.sanitizeMapByPath(
fromParameters, "source.image.imageBytes", new ReplayBase64Sanitizer(), false);
ReplaySanitizer.sanitizeMapByPath(
fromParameters,
"source.scribbleImage.image.imageBytes",
new ReplayBase64Sanitizer(),
false);
return fromParameters;
}
private static void handleException(
Throwable cause, TestTableItem testTableItem, Client client, String testName) {
Optional<String> exceptionIfMldev = testTableItem.exceptionIfMldev();
Optional<String> exceptionIfVertex = testTableItem.exceptionIfVertex();
if (exceptionIfMldev.isPresent() && !client.vertexAI()) {
verifyExceptionMatch(testName, cause, exceptionIfMldev.get(), "Gemini API");
} else if (exceptionIfVertex.isPresent() && client.vertexAI()) {
verifyExceptionMatch(testName, cause, exceptionIfVertex.get(), "Vertex AI");
} else {
fail(String.format("'%s' failed: %s", testName, cause));
}
}
private static void verifyExceptionMatch(
String testName, Throwable cause, String expectedException, String platformName) {
String exceptionMessage = cause.getMessage();
String parameterException = " parameter is not supported in " + platformName + ".";
if (exceptionMessage != null && exceptionMessage.endsWith(parameterException)) {
String camelCaseVariable = exceptionMessage.split(" ")[0];
String snakeCaseVariable = Transformers.camelToSnake(camelCaseVariable);
exceptionMessage = exceptionMessage.replace(camelCaseVariable, snakeCaseVariable);
}
if (!exceptionMessage.contains(expectedException)) {
String expected = expectedException.replace(" in ", " ");
String actual =
exceptionMessage == null
? ""
: exceptionMessage.replace(" in ", " ").replace(" for ", " ");
if (!actual.contains(expected)) {
fail(
String.format(
"'%s' failed to match expected exception:\n"
+ "Expected exception: %s\n"
+ " Actual exception: %s\n",
testName, expectedException, cause.getMessage()));
}
}
}
private static Optional<String> getSkipMessageInApiMode(
TestTableItem testTableItem, Client client) {
Optional<String> skipInApiMode = testTableItem.skipInApiMode();
if (skipInApiMode.isPresent()
&& (client.clientMode().equals("api") || client.clientMode().isEmpty())) {
return Optional.of(" => Test skipped: " + skipInApiMode.get());
}
return Optional.empty();
}
private static String getReplayFilePath(String testName) {
String[] replayPathSegments = testName.split("\\.");
String replayFilePath = "";
for (int i = 0; i < replayPathSegments.length; i++) {
if (replayPathSegments[i].endsWith("_test_table.json")) {
break;
}
replayFilePath += replayPathSegments[i] + "/";
}
return replayFilePath;
}
@TestFactory
@DisplayName("TableTest")
@EnabledIfEnvironmentVariable(
named = "GOOGLE_GENAI_REPLAYS_DIRECTORY",
matches = ".*genai/replays.*")
Collection<DynamicTest> createTests() throws IOException {
String replaysPath = System.getenv("GOOGLE_GENAI_REPLAYS_DIRECTORY");
String testsReplaysPath = replaysPath + "/tests";
String testsSubDir = System.getenv("GOOGLE_GENAI_TESTS_SUBDIR");
if (testsSubDir != null) {
testsReplaysPath += "/" + testsSubDir;
}
Collection<DynamicTest> dynamicTests = new ArrayList<>();
Files.walk(Paths.get(testsReplaysPath))
.filter(Files::isRegularFile)
.filter(path -> path.toString().endsWith("/_test_table.json"))
.forEach(
path -> {
try {
dynamicTests.addAll(createTableTests(path.toString(), false));
dynamicTests.addAll(createTableTests(path.toString(), true));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
return dynamicTests;
}
static Client createClient(boolean vertexAI) {
String replaysPath = System.getenv("GOOGLE_GENAI_REPLAYS_DIRECTORY");
if (replaysPath == null) {
throw new RuntimeException("GOOGLE_GENAI_REPLAYS_DIRECTORY is not set");
}
String testsReplaysPath = replaysPath + "/tests";
String replayMode = System.getenv("GOOGLE_GENAI_CLIENT_MODE");
if (replayMode == null || replayMode == "auto") {
replayMode = "replay";
}
DebugConfig debugConfig = new DebugConfig(replayMode, "", testsReplaysPath);
if (replayMode.equals("replay")) {
// Mock the default environment variables to avoid reading the actual environment variables in
// replay mode.
MockedStatic<ApiClient> mockedStaticApiClient =
Mockito.mockStatic(ApiClient.class, Mockito.CALLS_REAL_METHODS);
// Mock the default environment variables to avoid reading the actual environment variables.
mockedStaticApiClient
.when(ApiClient::defaultEnvironmentVariables)
.thenReturn(
ImmutableMap.builder()
.put("googleApiKey", "google-api-key")
.put("project", "project")
.put("location", "location")
.build());
Client client = Client.builder().vertexAI(vertexAI).debugConfig(debugConfig).build();
mockedStaticApiClient.close();
return client;
}
return Client.builder().vertexAI(vertexAI).debugConfig(debugConfig).build();
}
private static Object normalizeKeys(Object data) {
if (data instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> originalMap = (Map<String, Object>) data;
Map<String, Object> newNormalizedMap = new HashMap<>();
for (Map.Entry<String, Object> entry : originalMap.entrySet()) {
String normalizedKey = Common.snakeToCamel(entry.getKey());
Object normalizedValue = normalizeKeys(entry.getValue());
newNormalizedMap.put(normalizedKey, normalizedValue);
}
return newNormalizedMap;
} else if (data instanceof List) {
@SuppressWarnings("unchecked")
List<Object> originalList = (List<Object>) data;
List<Object> newNormalizedList = new ArrayList<>(originalList.size());
for (Object item : originalList) {
newNormalizedList.add(normalizeKeys(item));
}
return newNormalizedList;
} else {
return data; // Return as is for non-map/non-list types
}
}
}