|
| 1 | +package gerrit_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/andygrunwald/go-gerrit" |
| 10 | +) |
| 11 | + |
| 12 | +func newClient(t *testing.T, server *httptest.Server) *gerrit.Client { |
| 13 | + client, err := gerrit.NewClient(server.URL, nil) |
| 14 | + if err != nil { |
| 15 | + t.Error(err) |
| 16 | + } |
| 17 | + return client |
| 18 | +} |
| 19 | + |
| 20 | +func TestChangesService_ListReviewers(t *testing.T) { |
| 21 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 22 | + expected := "/changes/123/reviewers/" |
| 23 | + if r.URL.Path != expected { |
| 24 | + t.Errorf("%s != %s", r.URL.Path, expected) |
| 25 | + } |
| 26 | + |
| 27 | + fmt.Fprint(w, `[{"_account_id": 1}]`) |
| 28 | + })) |
| 29 | + defer ts.Close() |
| 30 | + |
| 31 | + client := newClient(t, ts) |
| 32 | + data, _, err := client.Changes.ListReviewers("123") |
| 33 | + if err != nil { |
| 34 | + t.Error(err) |
| 35 | + } |
| 36 | + |
| 37 | + if len(*data) != 1 { |
| 38 | + t.Error("Length of data !=1 ") |
| 39 | + } |
| 40 | + |
| 41 | + if (*data)[0].AccountID != 1 { |
| 42 | + t.Error("AccountID != 1") |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestChangesService_SuggestReviewers(t *testing.T) { |
| 47 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 48 | + expected := "/changes/123/suggest_reviewers" |
| 49 | + if r.URL.Path != expected { |
| 50 | + t.Errorf("%s != %s", r.URL.Path, expected) |
| 51 | + } |
| 52 | + |
| 53 | + fmt.Fprint(w, `[{"account": {"_account_id": 1}}]`) |
| 54 | + })) |
| 55 | + defer ts.Close() |
| 56 | + |
| 57 | + client := newClient(t, ts) |
| 58 | + data, _, err := client.Changes.SuggestReviewers("123", nil) |
| 59 | + if err != nil { |
| 60 | + t.Error(err) |
| 61 | + } |
| 62 | + |
| 63 | + if len(*data) != 1 { |
| 64 | + t.Error("Length of data !=1 ") |
| 65 | + } |
| 66 | + |
| 67 | + if (*data)[0].Account.AccountID != 1 { |
| 68 | + t.Error("AccountID != 1") |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestChangesService_GetReviewer(t *testing.T) { |
| 73 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 74 | + expected := "/changes/123/reviewers/1" |
| 75 | + if r.URL.Path != expected { |
| 76 | + t.Errorf("%s != %s", r.URL.Path, expected) |
| 77 | + } |
| 78 | + |
| 79 | + fmt.Fprint(w, `{"_account_id": 1}`) |
| 80 | + })) |
| 81 | + defer ts.Close() |
| 82 | + |
| 83 | + client := newClient(t, ts) |
| 84 | + data, _, err := client.Changes.GetReviewer("123", "1") |
| 85 | + if err != nil { |
| 86 | + t.Error(err) |
| 87 | + } |
| 88 | + if data.AccountID != 1 { |
| 89 | + t.Error("AccountID != 1") |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestChangesService_AddReviewer(t *testing.T) { |
| 94 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 95 | + expected := "/changes/123/reviewers" |
| 96 | + if r.URL.Path != expected { |
| 97 | + t.Errorf("%s != %s", r.URL.Path, expected) |
| 98 | + } |
| 99 | + if r.Method != "POST" { |
| 100 | + t.Error("Method != POST") |
| 101 | + } |
| 102 | + |
| 103 | + fmt.Fprint(w, `{"confirm": true}`) |
| 104 | + })) |
| 105 | + defer ts.Close() |
| 106 | + |
| 107 | + client := newClient(t, ts) |
| 108 | + data, _, err := client.Changes.AddReviewer("123", &gerrit.ReviewerInput{}) |
| 109 | + if err != nil { |
| 110 | + t.Error(err) |
| 111 | + } |
| 112 | + if !data.Confirm { |
| 113 | + t.Error("Confirm != true") |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func TestChangesService_DeleteReviewer(t *testing.T) { |
| 118 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 119 | + expected := "/changes/123/reviewers/1" |
| 120 | + if r.URL.Path != expected { |
| 121 | + t.Errorf("%s != %s", r.URL.Path, expected) |
| 122 | + } |
| 123 | + if r.Method != "DELETE" { |
| 124 | + t.Error("Method != DELETE") |
| 125 | + } |
| 126 | + w.WriteHeader(http.StatusOK) |
| 127 | + })) |
| 128 | + defer ts.Close() |
| 129 | + |
| 130 | + client := newClient(t, ts) |
| 131 | + _, err := client.Changes.DeleteReviewer("123", "1") |
| 132 | + if err != nil { |
| 133 | + t.Error(err) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func TestChangesService_ListVotes(t *testing.T) { |
| 138 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 139 | + expected := "/changes/123/reviewers/1/votes/" |
| 140 | + if r.URL.Path != expected { |
| 141 | + t.Errorf("%s != %s", r.URL.Path, expected) |
| 142 | + } |
| 143 | + fmt.Fprint(w, `{"Code-Review": 2, "Verified": 1}`) |
| 144 | + })) |
| 145 | + defer ts.Close() |
| 146 | + |
| 147 | + client := newClient(t, ts) |
| 148 | + votes, _, err := client.Changes.ListVotes("123", "1") |
| 149 | + if err != nil { |
| 150 | + t.Error(err) |
| 151 | + } |
| 152 | + if votes["Code-Review"] != 2 { |
| 153 | + t.Error("Code-Review != 2") |
| 154 | + } |
| 155 | + if votes["Verified"] != 1 { |
| 156 | + t.Error("Verified != 1") |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +func TestChangesService_DeleteVote(t *testing.T) { |
| 161 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 162 | + expected := "/changes/123/reviewers/1/votes/Code-Review" |
| 163 | + if r.URL.Path != expected { |
| 164 | + t.Errorf("%s != %s", r.URL.Path, expected) |
| 165 | + } |
| 166 | + |
| 167 | + if r.Method != "DELETE" { |
| 168 | + t.Error("Method != DELETE") |
| 169 | + } |
| 170 | + w.WriteHeader(http.StatusOK) |
| 171 | + })) |
| 172 | + defer ts.Close() |
| 173 | + |
| 174 | + client := newClient(t, ts) |
| 175 | + _, err := client.Changes.DeleteVote("123", "1", "Code-Review", nil) |
| 176 | + if err != nil { |
| 177 | + t.Error(err) |
| 178 | + } |
| 179 | +} |
0 commit comments