--------------------------------------------------------------------------- handleAnalyzeSQL ---------------------------------------------------------------------------
(t *testing.T)
| 356 | // --------------------------------------------------------------------------- |
| 357 | |
| 358 | func 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 | }) |
nothing calls this directly
no test coverage detected