|
5 | 5 | "encoding/json" |
6 | 6 | "errors" |
7 | 7 | "io" |
| 8 | + "strings" |
8 | 9 | "testing" |
9 | 10 | ) |
10 | 11 |
|
@@ -296,6 +297,136 @@ func TestClient_UpdatePRBase(t *testing.T) { |
296 | 297 | } |
297 | 298 | } |
298 | 299 |
|
| 300 | +func TestClient_GetPRTitles(t *testing.T) { |
| 301 | + t.Run("success with multiple PRs", func(t *testing.T) { |
| 302 | + mock := &mockREST{ |
| 303 | + postFn: func(path string, body io.Reader, response any) error { |
| 304 | + if path != "graphql" { |
| 305 | + t.Errorf("expected path %q, got %q", "graphql", path) |
| 306 | + } |
| 307 | + |
| 308 | + // Return mock GraphQL response |
| 309 | + if raw, ok := response.(*map[string]json.RawMessage); ok { |
| 310 | + *raw = map[string]json.RawMessage{ |
| 311 | + "data": json.RawMessage(`{ |
| 312 | + "repository": { |
| 313 | + "pr1": {"number": 1, "title": "First PR"}, |
| 314 | + "pr2": {"number": 2, "title": "Second PR"} |
| 315 | + } |
| 316 | + }`), |
| 317 | + } |
| 318 | + } |
| 319 | + return nil |
| 320 | + }, |
| 321 | + } |
| 322 | + |
| 323 | + client := NewClientWithREST(mock, "owner", "repo") |
| 324 | + result, err := client.GetPRTitles([]int{1, 2}) |
| 325 | + |
| 326 | + if err != nil { |
| 327 | + t.Fatalf("unexpected error: %v", err) |
| 328 | + } |
| 329 | + if len(result) != 2 { |
| 330 | + t.Errorf("expected 2 results, got %d", len(result)) |
| 331 | + } |
| 332 | + if result[1].Title != "First PR" { |
| 333 | + t.Errorf("expected title %q, got %q", "First PR", result[1].Title) |
| 334 | + } |
| 335 | + if result[2].Title != "Second PR" { |
| 336 | + t.Errorf("expected title %q, got %q", "Second PR", result[2].Title) |
| 337 | + } |
| 338 | + }) |
| 339 | + |
| 340 | + t.Run("empty input", func(t *testing.T) { |
| 341 | + mock := &mockREST{ |
| 342 | + postFn: func(path string, body io.Reader, response any) error { |
| 343 | + t.Error("should not make API call for empty input") |
| 344 | + return nil |
| 345 | + }, |
| 346 | + } |
| 347 | + |
| 348 | + client := NewClientWithREST(mock, "owner", "repo") |
| 349 | + result, err := client.GetPRTitles([]int{}) |
| 350 | + |
| 351 | + if err != nil { |
| 352 | + t.Fatalf("unexpected error: %v", err) |
| 353 | + } |
| 354 | + if len(result) != 0 { |
| 355 | + t.Errorf("expected empty result, got %d items", len(result)) |
| 356 | + } |
| 357 | + }) |
| 358 | + |
| 359 | + t.Run("GraphQL error in response", func(t *testing.T) { |
| 360 | + mock := &mockREST{ |
| 361 | + postFn: func(path string, body io.Reader, response any) error { |
| 362 | + if raw, ok := response.(*map[string]json.RawMessage); ok { |
| 363 | + *raw = map[string]json.RawMessage{ |
| 364 | + "errors": json.RawMessage(`[{"message": "Repository not found"}]`), |
| 365 | + } |
| 366 | + } |
| 367 | + return nil |
| 368 | + }, |
| 369 | + } |
| 370 | + |
| 371 | + client := NewClientWithREST(mock, "owner", "repo") |
| 372 | + _, err := client.GetPRTitles([]int{1}) |
| 373 | + |
| 374 | + if err == nil { |
| 375 | + t.Fatal("expected error, got nil") |
| 376 | + } |
| 377 | + if !strings.Contains(err.Error(), "Repository not found") { |
| 378 | + t.Errorf("expected error to contain 'Repository not found', got %q", err.Error()) |
| 379 | + } |
| 380 | + }) |
| 381 | + |
| 382 | + t.Run("API error", func(t *testing.T) { |
| 383 | + mock := &mockREST{ |
| 384 | + postFn: func(path string, body io.Reader, response any) error { |
| 385 | + return errors.New("network error") |
| 386 | + }, |
| 387 | + } |
| 388 | + |
| 389 | + client := NewClientWithREST(mock, "owner", "repo") |
| 390 | + _, err := client.GetPRTitles([]int{1}) |
| 391 | + |
| 392 | + if err == nil { |
| 393 | + t.Fatal("expected error, got nil") |
| 394 | + } |
| 395 | + }) |
| 396 | + |
| 397 | + t.Run("some PRs not found", func(t *testing.T) { |
| 398 | + mock := &mockREST{ |
| 399 | + postFn: func(path string, body io.Reader, response any) error { |
| 400 | + // PR 2 doesn't exist (null in response) |
| 401 | + if raw, ok := response.(*map[string]json.RawMessage); ok { |
| 402 | + *raw = map[string]json.RawMessage{ |
| 403 | + "data": json.RawMessage(`{ |
| 404 | + "repository": { |
| 405 | + "pr1": {"number": 1, "title": "First PR"}, |
| 406 | + "pr2": null |
| 407 | + } |
| 408 | + }`), |
| 409 | + } |
| 410 | + } |
| 411 | + return nil |
| 412 | + }, |
| 413 | + } |
| 414 | + |
| 415 | + client := NewClientWithREST(mock, "owner", "repo") |
| 416 | + result, err := client.GetPRTitles([]int{1, 2}) |
| 417 | + |
| 418 | + if err != nil { |
| 419 | + t.Fatalf("unexpected error: %v", err) |
| 420 | + } |
| 421 | + if len(result) != 1 { |
| 422 | + t.Errorf("expected 1 result (PR 2 not found), got %d", len(result)) |
| 423 | + } |
| 424 | + if _, ok := result[1]; !ok { |
| 425 | + t.Error("expected PR 1 to be in result") |
| 426 | + } |
| 427 | + }) |
| 428 | +} |
| 429 | + |
299 | 430 | func TestClient_FindPRByHead(t *testing.T) { |
300 | 431 | mock := &mockREST{ |
301 | 432 | getFn: func(path string, response any) error { |
|
0 commit comments