Skip to content

Commit 955633b

Browse files
committed
sbenp.prettier-vscode
1 parent c77244b commit 955633b

File tree

6 files changed

+67
-57
lines changed

6 files changed

+67
-57
lines changed

internal/sources/alloydbadmin/alloydbadmin.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,11 @@ func (s *Source) GetOperations(ctx context.Context, project, location, operation
361361
}
362362
}
363363

364-
return string(opBytes), nil
364+
var result any
365+
if err := json.Unmarshal(opBytes, &result); err != nil {
366+
return nil, fmt.Errorf("failed to unmarshal operation bytes: %w", err)
367+
}
368+
return result, nil
365369
}
366370
logger.DebugContext(ctx, fmt.Sprintf("Operation not complete, retrying in %v\n", delay))
367371
}

internal/tools/alloydb/alloydbwaitforoperation/alloydbwaitforoperation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
253253

254254
op, err := source.GetOperations(ctx, project, location, operation, alloyDBConnectionMessageTemplate, delay, string(accessToken))
255255
if err != nil {
256-
return nil, util.ProcessGcpError(err)
256+
return nil, util.ProcessGeneralError(err)
257257
}
258258
if op != nil {
259259
return op, nil

tests/alloydb/alloydb_integration_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,8 +1239,8 @@ func TestAlloyDBCreateInstance(t *testing.T) {
12391239
{
12401240
name: "api failure",
12411241
body: `{"project": "p1", "location": "l1", "cluster": "c1", "instance": "i2-api-failure", "instanceType": "PRIMARY", "displayName": "i1-success"}`,
1242-
want: "internal api error",
1243-
wantStatusCode: http.StatusBadRequest,
1242+
want: `{"error":"error processing GCP request: error creating AlloyDB instance: googleapi: Error 500: internal api error"}`,
1243+
wantStatusCode: http.StatusOK,
12441244
},
12451245
{
12461246
name: "missing project",
@@ -1269,8 +1269,8 @@ func TestAlloyDBCreateInstance(t *testing.T) {
12691269
{
12701270
name: "invalid instanceType",
12711271
body: `{"project": "p1", "location": "l1", "cluster": "c1", "instance": "i1", "instanceType": "INVALID", "displayName": "invalid"}`,
1272-
want: `invalid 'instanceType' parameter; expected 'PRIMARY' or 'READ_POOL'`,
1273-
wantStatusCode: http.StatusBadRequest,
1272+
want: `{"error":"invalid 'instanceType' parameter; expected 'PRIMARY' or 'READ_POOL'"}`,
1273+
wantStatusCode: http.StatusOK,
12741274
},
12751275
}
12761276

tests/alloydb/alloydb_wait_for_operation_test.go

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"net/http"
2424
"net/http/httptest"
2525
"net/url"
26-
"reflect"
2726
"regexp"
2827
"strings"
2928
"sync"
@@ -165,13 +164,13 @@ func TestWaitToolEndpoints(t *testing.T) {
165164
name: "successful operation",
166165
toolName: "wait-for-op1",
167166
body: `{"project": "p1", "location": "l1", "operation": "op1"}`,
168-
want: `{"name":"op1","done":true,"response":"success"}`,
167+
want: `{"done":true,"name":"op1","response":"success"}`,
169168
},
170169
{
171-
name: "failed operation",
172-
toolName: "wait-for-op2",
173-
body: `{"project": "p1", "location": "l1", "operation": "op2"}`,
174-
expectError: true,
170+
name: "failed operation",
171+
toolName: "wait-for-op2",
172+
body: `{"project": "p1", "location": "l1", "operation": "op2"}`,
173+
want: `{"error":"error processing request: operation finished with error: {\"code\":1,\"message\":\"failed\"}"}`,
175174
},
176175
}
177176

@@ -189,48 +188,42 @@ func TestWaitToolEndpoints(t *testing.T) {
189188
}
190189
defer resp.Body.Close()
191190

192-
if tc.expectError {
193-
if resp.StatusCode == http.StatusOK {
194-
t.Fatal("expected error but got status 200")
195-
}
196-
return
197-
}
198-
199191
if resp.StatusCode != http.StatusOK {
200192
bodyBytes, _ := io.ReadAll(resp.Body)
201193
t.Fatalf("response status code is not 200, got %d: %s", resp.StatusCode, string(bodyBytes))
202194
}
203-
204-
var result struct {
205-
Result string `json:"result"`
195+
var response struct {
196+
Result any `json:"result"`
206197
}
207-
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
198+
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
208199
t.Fatalf("failed to decode response: %v", err)
209200
}
210201

211-
if tc.wantSubstring {
212-
if !bytes.Contains([]byte(result.Result), []byte(tc.want)) {
213-
t.Fatalf("unexpected result: got %q, want substring %q", result.Result, tc.want)
202+
var got string
203+
// Check if the result is a string (which contains JSON)
204+
if s, ok := response.Result.(string); ok {
205+
got = s
206+
} else {
207+
b, err := json.Marshal(response.Result)
208+
if err != nil {
209+
t.Fatalf("failed to marshal result object: %v", err)
214210
}
215-
return
211+
got = string(b)
216212
}
217213

218-
// The result is a JSON-encoded string, so we need to unmarshal it twice.
219-
var tempString string
220-
if err := json.Unmarshal([]byte(result.Result), &tempString); err != nil {
221-
t.Fatalf("failed to unmarshal result string: %v", err)
222-
}
214+
// Clean up both strings to ignore whitespace differences
215+
got = strings.ReplaceAll(strings.ReplaceAll(got, " ", ""), "\n", "")
216+
want := strings.ReplaceAll(strings.ReplaceAll(tc.want, " ", ""), "\n", "")
223217

224-
var got, want map[string]any
225-
if err := json.Unmarshal([]byte(tempString), &got); err != nil {
226-
t.Fatalf("failed to unmarshal result: %v", err)
227-
}
228-
if err := json.Unmarshal([]byte(tc.want), &want); err != nil {
229-
t.Fatalf("failed to unmarshal want: %v", err)
218+
if tc.wantSubstring {
219+
if !strings.Contains(got, want) {
220+
t.Fatalf("unexpected result: got %q, want substring %q", got, want)
221+
}
222+
return
230223
}
231224

232-
if !reflect.DeepEqual(got, want) {
233-
t.Fatalf("unexpected result: got %+v, want %+v", got, want)
225+
if got != want {
226+
t.Fatalf("unexpected result: \ngot: %s\nwant: %s", got, want)
234227
}
235228
})
236229
}

tests/mongodb/mongodb_integration_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ func runToolUpdateInvokeTest(t *testing.T, update1Want, updateManyWant string) {
354354
})
355355
}
356356
}
357+
357358
func runToolAggregateInvokeTest(t *testing.T, aggregate1Want string, aggregateManyWant string) {
358359
// Test tool invoke endpoint
359360
invokeTcs := []struct {
@@ -385,8 +386,8 @@ func runToolAggregateInvokeTest(t *testing.T, aggregate1Want string, aggregateMa
385386
api: "http://127.0.0.1:5000/api/tool/my-read-only-aggregate-tool/invoke",
386387
requestHeader: map[string]string{},
387388
requestBody: bytes.NewBuffer([]byte(`{ "name" : "ToBeAggregated" }`)),
388-
want: "",
389-
isErr: true,
389+
want: `{"error":"error processing request: this is not a read-only pipeline: {\"$out\":\"target_collection\"}"}`,
390+
isErr: false,
390391
},
391392
{
392393
name: "invoke my-read-write-aggregate-tool",

tests/neo4j/neo4j_integration_test.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -287,25 +287,37 @@ func TestNeo4jToolEndpoints(t *testing.T) {
287287
},
288288
},
289289
{
290-
name: "invoke my-simple-execute-cypher-tool with dry_run and invalid syntax",
291-
api: "http://127.0.0.1:5000/api/tool/my-simple-execute-cypher-tool/invoke",
292-
requestBody: bytes.NewBuffer([]byte(`{"cypher": "RTN 1", "dry_run": true}`)),
293-
wantStatus: http.StatusBadRequest,
294-
wantErrorSubstring: "unable to execute query",
290+
name: "invoke my-simple-execute-cypher-tool with dry_run and invalid syntax",
291+
api: "http://127.0.0.1:5000/api/tool/my-simple-execute-cypher-tool/invoke",
292+
requestBody: bytes.NewBuffer([]byte(`{"cypher": "RTN 1", "dry_run": true}`)),
293+
wantStatus: http.StatusOK,
294+
validateFunc: func(t *testing.T, body string) {
295+
if !strings.Contains(body, "unable to execute query") {
296+
t.Errorf("expected error message not found in body: %s", body)
297+
}
298+
},
295299
},
296300
{
297-
name: "invoke readonly tool with write query",
298-
api: "http://127.0.0.1:5000/api/tool/my-readonly-execute-cypher-tool/invoke",
299-
requestBody: bytes.NewBuffer([]byte(`{"cypher": "CREATE (n:TestNode)"}`)),
300-
wantStatus: http.StatusBadRequest,
301-
wantErrorSubstring: "this tool is read-only and cannot execute write queries",
301+
name: "invoke readonly tool with write query",
302+
api: "http://127.0.0.1:5000/api/tool/my-readonly-execute-cypher-tool/invoke",
303+
requestBody: bytes.NewBuffer([]byte(`{"cypher": "CREATE (n:TestNode)"}`)),
304+
wantStatus: http.StatusOK,
305+
validateFunc: func(t *testing.T, body string) {
306+
if !strings.Contains(body, "this tool is read-only and cannot execute write queries") {
307+
t.Errorf("expected error message not found in body: %s", body)
308+
}
309+
},
302310
},
303311
{
304-
name: "invoke readonly tool with write query and dry_run",
305-
api: "http://127.0.0.1:5000/api/tool/my-readonly-execute-cypher-tool/invoke",
306-
requestBody: bytes.NewBuffer([]byte(`{"cypher": "CREATE (n:TestNode)", "dry_run": true}`)),
307-
wantStatus: http.StatusBadRequest,
308-
wantErrorSubstring: "this tool is read-only and cannot execute write queries",
312+
name: "invoke readonly tool with write query and dry_run",
313+
api: "http://127.0.0.1:5000/api/tool/my-readonly-execute-cypher-tool/invoke",
314+
requestBody: bytes.NewBuffer([]byte(`{"cypher": "CREATE (n:TestNode)", "dry_run": true}`)),
315+
wantStatus: http.StatusOK,
316+
validateFunc: func(t *testing.T, body string) {
317+
if !strings.Contains(body, "this tool is read-only and cannot execute write queries") {
318+
t.Errorf("expected error message not found in body: %s", body)
319+
}
320+
},
309321
},
310322
{
311323
name: "invoke my-schema-tool",

0 commit comments

Comments
 (0)