Skip to content

Commit 072f36b

Browse files
committed
code comments
1 parent 68d4063 commit 072f36b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

tests/tool.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ func RunToolInvokeParametersTest(t *testing.T, name string, params []byte, simpl
229229

230230
// RunToolInvoke runs the tool invoke endpoint
231231
func RunToolInvokeTest(t *testing.T, select1Want string, options ...InvokeTestOption) {
232+
// Resolve options
233+
// Default values for InvokeTestConfig
232234
configs := &InvokeTestConfig{
233235
myToolId3NameAliceWant: "[{\"id\":1,\"name\":\"Alice\"},{\"id\":3,\"name\":\"Sid\"}]",
234236
myToolById4Want: "[{\"id\":4,\"name\":null}]",
@@ -241,21 +243,25 @@ func RunToolInvokeTest(t *testing.T, select1Want string, options ...InvokeTestOp
241243
supportSelect1Auth: true,
242244
}
243245

246+
// Apply provided options
244247
for _, option := range options {
245248
option(configs)
246249
}
247250

251+
// Get ID token
248252
idToken, err := GetGoogleIdToken(ClientId)
249253
if err != nil {
250254
t.Fatalf("error getting Google ID token: %s", err)
251255
}
252256

257+
// Get access token
253258
accessToken, err := sources.GetIAMAccessToken(t.Context())
254259
if err != nil {
255260
t.Fatalf("error getting access token from ADC: %s", err)
256261
}
257262
accessToken = "Bearer " + accessToken
258263

264+
// Test tool invoke endpoint
259265
invokeTcs := []struct {
260266
name string
261267
api string
@@ -420,9 +426,11 @@ func RunToolInvokeTest(t *testing.T, select1Want string, options ...InvokeTestOp
420426
var actualStatusCode int
421427

422428
if configs.IsMCP {
429+
// Extract tool name from the API URL for MCP invocation
423430
parts := strings.Split(tc.api, "/")
424431
toolName := parts[len(parts)-2]
425432

433+
// Parse request body as map for MCP arguments
426434
var args map[string]any
427435
if len(reqBytes) > 0 {
428436
_ = json.Unmarshal(reqBytes, &args)
@@ -431,9 +439,11 @@ func RunToolInvokeTest(t *testing.T, select1Want string, options ...InvokeTestOp
431439
args = make(map[string]any)
432440
}
433441

442+
// Invoke the tool via MCP protocol
434443
mcpStatusCode, mcpResp, _ := InvokeMCPTool(t, toolName, args, tc.requestHeader)
435444
actualStatusCode = mcpStatusCode
436445

446+
// Translate MCP response back to string for unified assertion
437447
if actualStatusCode == http.StatusOK && tc.wantBody != "" {
438448
if mcpResp != nil && mcpResp.Error != nil {
439449
m := map[string]string{"error": mcpResp.Error.Message}
@@ -474,6 +484,7 @@ func RunToolInvokeTest(t *testing.T, select1Want string, options ...InvokeTestOp
474484
}
475485
}
476486

487+
// Check status code
477488
wantStatus := tc.wantStatusCode
478489
if configs.IsMCP && wantStatus == http.StatusUnauthorized {
479490
if actualStatusCode == http.StatusOK {
@@ -484,10 +495,12 @@ func RunToolInvokeTest(t *testing.T, select1Want string, options ...InvokeTestOp
484495
t.Errorf("StatusCode mismatch: got %d, want %d", actualStatusCode, wantStatus)
485496
}
486497

498+
// skip response body check
487499
if tc.wantBody == "" {
488500
return
489501
}
490502

503+
// Check response body
491504
// Unified JSON-Aware Validation
492505
if got != tc.wantBody {
493506
var gotJSON, wantJSON any
@@ -523,6 +536,8 @@ func RunToolInvokeTest(t *testing.T, select1Want string, options ...InvokeTestOp
523536

524537
// RunToolInvokeWithTemplateParameters runs tool invoke test cases with template parameters.
525538
func RunToolInvokeWithTemplateParameters(t *testing.T, tableName string, options ...TemplateParamOption) {
539+
// Resolve options
540+
// Default values for TemplateParameterTestConfig
526541
configs := &TemplateParameterTestConfig{
527542
ddlWant: "null",
528543
selectAllWant: "[{\"age\":21,\"id\":1,\"name\":\"Alex\"},{\"age\":100,\"id\":2,\"name\":\"Alice\"}]",
@@ -539,12 +554,14 @@ func RunToolInvokeWithTemplateParameters(t *testing.T, tableName string, options
539554
supportInsert: true,
540555
}
541556

557+
// Apply provided options
542558
for _, option := range options {
543559
option(configs)
544560
}
545561

546562
selectOnlyNamesWant := "[{\"name\":\"Alex\"},{\"name\":\"Alice\"}]"
547563

564+
// Test tool invoke endpoint
548565
invokeTcs := []struct {
549566
name string
550567
enabled bool
@@ -639,10 +656,13 @@ func RunToolInvokeWithTemplateParameters(t *testing.T, tableName string, options
639656
if !tc.enabled {
640657
return
641658
}
659+
// if test case is DDL and source support ddl test cases
642660
ddlAllow := !tc.ddl || (tc.ddl && configs.supportDdl)
661+
// if test case is insert statement and source support insert test cases
643662
insertAllow := !tc.insert || (tc.insert && configs.supportInsert)
644663

645664
if ddlAllow && insertAllow {
665+
// Send Tool invocation request
646666
reqBytes, _ := io.ReadAll(tc.requestBody)
647667
var got string
648668

@@ -724,22 +744,27 @@ func RunToolInvokeWithTemplateParameters(t *testing.T, tableName string, options
724744
}
725745

726746
func RunExecuteSqlToolInvokeTest(t *testing.T, createTableStatement, select1Want string, options ...ExecuteSqlOption) {
747+
// Resolve options
748+
// Default values for ExecuteSqlTestConfig
727749
configs := &ExecuteSqlTestConfig{
728750
select1Statement: `"SELECT 1"`,
729751
createWant: "null",
730752
dropWant: "null",
731753
selectEmptyWant: "null",
732754
}
733755

756+
// Apply provided options
734757
for _, option := range options {
735758
option(configs)
736759
}
737760

761+
// Get ID token
738762
idToken, err := GetGoogleIdToken(ClientId)
739763
if err != nil {
740764
t.Fatalf("error getting Google ID token: %s", err)
741765
}
742766

767+
// Test tool invoke endpoint
743768
invokeTcs := []struct {
744769
name string
745770
api string
@@ -827,6 +852,7 @@ func RunExecuteSqlToolInvokeTest(t *testing.T, createTableStatement, select1Want
827852
}
828853
for _, tc := range invokeTcs {
829854
t.Run(tc.name, func(t *testing.T) {
855+
// Send Tool invocation request
830856
reqBytes, _ := io.ReadAll(tc.requestBody)
831857
var got string
832858

@@ -3155,6 +3181,7 @@ func RunMySQLListTablesTest(t *testing.T, databaseName, tableNameParam, tableNam
31553181
cmpopts.SortSlices(func(a, b map[string]any) bool { return a["name"].(string) < b["name"].(string) }),
31563182
}
31573183

3184+
// Checking only the current database where the test tables are created to avoid brittle tests.
31583185
if tc.isAllTables {
31593186
filteredGot := []objectDetails{}
31603187
if got != nil {

0 commit comments

Comments
 (0)