MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / TestHandleAnalyzeSQL

Function TestHandleAnalyzeSQL

pkg/mcp/tools_test.go:358–424  ·  view source on GitHub ↗

--------------------------------------------------------------------------- handleAnalyzeSQL ---------------------------------------------------------------------------

(t *testing.T)

Source from the content-addressed store, hash-verified

356// ---------------------------------------------------------------------------
357
358func TestHandleAnalyzeSQL(t *testing.T) {
359 ctx := context.Background()
360
361 t.Run("valid SQL returns all six analysis keys", func(t *testing.T) {
362 req := makeReq(map[string]any{"sql": "SELECT id, name FROM users WHERE id = 1"})
363 res, err := handleAnalyzeSQL(ctx, req)
364 if err != nil {
365 t.Fatalf("unexpected error: %v", err)
366 }
367 data := unmarshalResult(t, res)
368
369 expectedKeys := []string{"validate", "parse", "metadata", "security", "lint", "format"}
370 for _, key := range expectedKeys {
371 if _, ok := data[key]; !ok {
372 t.Errorf("expected key %q in analyze result", key)
373 }
374 }
375 })
376
377 t.Run("validate sub-result contains valid field", func(t *testing.T) {
378 req := makeReq(map[string]any{"sql": "SELECT 1"})
379 res, err := handleAnalyzeSQL(ctx, req)
380 if err != nil {
381 t.Fatalf("unexpected error: %v", err)
382 }
383 data := unmarshalResult(t, res)
384 validateData, ok := data["validate"].(map[string]any)
385 if !ok {
386 t.Fatal("expected 'validate' sub-result to be a map")
387 }
388 if _, ok := validateData["valid"]; !ok {
389 t.Error("expected 'valid' field in validate sub-result")
390 }
391 })
392
393 t.Run("format sub-result contains formatted_sql", func(t *testing.T) {
394 req := makeReq(map[string]any{"sql": "select id from users"})
395 res, err := handleAnalyzeSQL(ctx, req)
396 if err != nil {
397 t.Fatalf("unexpected error: %v", err)
398 }
399 data := unmarshalResult(t, res)
400 formatData, ok := data["format"].(map[string]any)
401 if !ok {
402 t.Fatal("expected 'format' sub-result to be a map")
403 }
404 if _, ok := formatData["formatted_sql"]; !ok {
405 t.Error("expected 'formatted_sql' field in format sub-result")
406 }
407 })
408
409 t.Run("empty sql returns protocol error", func(t *testing.T) {
410 req := makeReq(map[string]any{"sql": ""})
411 _, err := handleAnalyzeSQL(ctx, req)
412 if err == nil {
413 t.Fatal("expected error for empty sql, got nil")
414 }
415 })

Callers

nothing calls this directly

Calls 7

makeReqFunction · 0.85
handleAnalyzeSQLFunction · 0.85
unmarshalResultFunction · 0.85
RunMethod · 0.80
FatalfMethod · 0.65
ErrorfMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected