| 1 | package github |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestFetchReviewThreads(t *testing.T) { |
| 9 | responseJSON := `{ |
| 10 | "data": { |
| 11 | "repository": { |
| 12 | "pullRequest": { |
| 13 | "reviewThreads": { |
| 14 | "nodes": [ |
| 15 | {"id": "RT_abc", "isResolved": false}, |
| 16 | {"id": "RT_def", "isResolved": true}, |
| 17 | {"id": "RT_ghi", "isResolved": false} |
| 18 | ] |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | }` |
| 24 | |
| 25 | runner := func(query string) ([]byte, error) { |
| 26 | return []byte(responseJSON), nil |
| 27 | } |
| 28 | |
| 29 | threads, err := fetchReviewThreadsImpl("ignored", runner) |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | if len(threads) != 3 { |
| 34 | t.Fatalf("expected 3 threads, got %d", len(threads)) |
| 35 | } |
| 36 | if threads[0].ID != "RT_abc" || threads[0].IsResolved { |
| 37 | t.Errorf("thread 0: got %+v", threads[0]) |
| 38 | } |
| 39 | if threads[1].ID != "RT_def" || !threads[1].IsResolved { |
| 40 | t.Errorf("thread 1: got %+v", threads[1]) |
| 41 | } |
| 42 | if threads[2].ID != "RT_ghi" || threads[2].IsResolved { |
| 43 | t.Errorf("thread 2: got %+v", threads[2]) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestFetchReviewThreadsEmpty(t *testing.T) { |
| 48 | responseJSON := `{ |
| 49 | "data": { |
| 50 | "repository": { |
| 51 | "pullRequest": { |
| 52 | "reviewThreads": { |
| 53 | "nodes": [] |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | }` |
| 59 | |
| 60 | runner := func(query string) ([]byte, error) { |
| 61 | return []byte(responseJSON), nil |
| 62 | } |
| 63 | |
| 64 | threads, err := fetchReviewThreadsImpl("ignored", runner) |
| 65 | if err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | if len(threads) != 0 { |
| 69 | t.Fatalf("expected 0 threads, got %d", len(threads)) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestFetchReviewThreadsAPIError(t *testing.T) { |
| 74 | runner := func(query string) ([]byte, error) { |
| 75 | return nil, fmt.Errorf("gh api graphql: exit status 1: not found") |
| 76 | } |
| 77 | |
| 78 | _, err := fetchReviewThreadsImpl("ignored", runner) |
| 79 | if err == nil { |
| 80 | t.Fatal("expected error") |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestResolveReviewThread(t *testing.T) { |
| 85 | responseJSON := `{ |
| 86 | "data": { |
| 87 | "resolveReviewThread": { |
| 88 | "thread": { |
| 89 | "isResolved": true |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | }` |
| 94 | |
| 95 | var calledQuery string |
| 96 | runner := func(query string) ([]byte, error) { |
| 97 | calledQuery = query |
| 98 | return []byte(responseJSON), nil |
| 99 | } |
| 100 | |
| 101 | err := resolveReviewThreadImpl("RT_abc", runner) |
| 102 | if err != nil { |
| 103 | t.Fatal(err) |
| 104 | } |
| 105 | if calledQuery == "" { |
| 106 | t.Error("expected query to be called") |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func TestResolveReviewThreadGraphQLError(t *testing.T) { |
| 111 | responseJSON := `{ |
| 112 | "data": null, |
| 113 | "errors": [{"message": "Could not resolve thread"}] |
| 114 | }` |
| 115 | |
| 116 | runner := func(query string) ([]byte, error) { |
| 117 | return []byte(responseJSON), nil |
| 118 | } |
| 119 | |
| 120 | err := resolveReviewThreadImpl("RT_abc", runner) |
| 121 | if err == nil { |
| 122 | t.Fatal("expected error from GraphQL error response") |
| 123 | } |
| 124 | if err.Error() != "GraphQL error: Could not resolve thread" { |
| 125 | t.Errorf("unexpected error: %v", err) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | func TestResolveReviewThreadAPIError(t *testing.T) { |
| 130 | runner := func(query string) ([]byte, error) { |
| 131 | return nil, fmt.Errorf("gh api graphql: exit status 1: forbidden") |
| 132 | } |
| 133 | |
| 134 | err := resolveReviewThreadImpl("RT_abc", runner) |
| 135 | if err == nil { |
| 136 | t.Fatal("expected error") |
| 137 | } |
| 138 | } |