|
6 | 6 | "net/http/httptest" |
7 | 7 | "reflect" |
8 | 8 | "testing" |
| 9 | + "time" |
9 | 10 |
|
10 | 11 | "github.com/andygrunwald/go-gerrit" |
11 | 12 | ) |
@@ -79,3 +80,235 @@ func TestChangesService_ListFilesReviewed(t *testing.T) { |
79 | 80 | t.Errorf("client.Changes.ListFilesReviewed:\ngot: %q\nwant: %q", got, want) |
80 | 81 | } |
81 | 82 | } |
| 83 | + |
| 84 | +func TestChangesService_ListRevisionRobotComment(t *testing.T) { |
| 85 | + listRobotCommentResponse := `)]}' |
| 86 | + { |
| 87 | + "main.c": [ |
| 88 | + { |
| 89 | + "robot_id": "robot", |
| 90 | + "robot_run_id": "1", |
| 91 | + "fix_suggestions": [ |
| 92 | + { |
| 93 | + "fix_id": "c3302a6f_1578ee9e", |
| 94 | + "description": "suggestion", |
| 95 | + "replacements": [ |
| 96 | + { |
| 97 | + "path": "main.c", |
| 98 | + "range": { |
| 99 | + "start_line": 3, |
| 100 | + "start_character": 0, |
| 101 | + "end_line": 5, |
| 102 | + "end_character": 1 |
| 103 | + }, |
| 104 | + "replacement": "int main() { printf(\"Hello world!\"); }" |
| 105 | + } |
| 106 | + ] |
| 107 | + } |
| 108 | + ], |
| 109 | + "author": { |
| 110 | + "_account_id": 1000000, |
| 111 | + "name": "Jhon Smith", |
| 112 | + "username": "jhon" |
| 113 | + }, |
| 114 | + "change_message_id": "517e6c92e4bd105f1e611294a3010ea177771551", |
| 115 | + "patch_set": 1, |
| 116 | + "id": "f7d3d07f_bf17b66e", |
| 117 | + "line": 5, |
| 118 | + "range": { |
| 119 | + "start_line": 3, |
| 120 | + "start_character": 0, |
| 121 | + "end_line": 5, |
| 122 | + "end_character": 1 |
| 123 | + }, |
| 124 | + "updated": "2022-07-05 13:44:34.000000000", |
| 125 | + "message": "[clang-format] fix suggestion", |
| 126 | + "commit_id": "be8ce493368f2ce0fa73b56f5e2bd0dc17ca4359" |
| 127 | + } |
| 128 | + ] |
| 129 | + }` |
| 130 | + |
| 131 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 132 | + if r.URL.Path != "/changes/changeID/revisions/revisionID/robotcomments" { |
| 133 | + t.Errorf("%s != /changes/changeID/revisions/revisionID/robotcomments", r.URL.Path) |
| 134 | + } |
| 135 | + if r.Method != "GET" { |
| 136 | + t.Error("Method != GET") |
| 137 | + } |
| 138 | + fmt.Fprint(w, listRobotCommentResponse) |
| 139 | + })) |
| 140 | + defer ts.Close() |
| 141 | + |
| 142 | + client, err := gerrit.NewClient(ts.URL, nil) |
| 143 | + if err != nil { |
| 144 | + t.Error(err) |
| 145 | + } |
| 146 | + |
| 147 | + got, _, err := client.Changes.ListRevisionRobotComments("changeID", "revisionID") |
| 148 | + if err != nil { |
| 149 | + t.Errorf("Changes.ListRevisionRobotComments returned error: %v", err) |
| 150 | + } |
| 151 | + |
| 152 | + want := map[string][]gerrit.RobotCommentInfo{ |
| 153 | + "main.c": { |
| 154 | + { |
| 155 | + CommentInfo: gerrit.CommentInfo{ |
| 156 | + PatchSet: 1, |
| 157 | + ID: "f7d3d07f_bf17b66e", |
| 158 | + Line: 5, |
| 159 | + Range: &gerrit.CommentRange{ |
| 160 | + StartLine: 3, |
| 161 | + StartCharacter: 0, |
| 162 | + EndLine: 5, |
| 163 | + EndCharacter: 1, |
| 164 | + }, |
| 165 | + Message: "[clang-format] fix suggestion", |
| 166 | + Updated: &gerrit.Timestamp{ |
| 167 | + Time: time.Date(2022, 7, 5, 13, 44, 34, 0, time.UTC), |
| 168 | + }, |
| 169 | + Author: gerrit.AccountInfo{ |
| 170 | + AccountID: 1000000, |
| 171 | + Name: "Jhon Smith", |
| 172 | + Username: "jhon", |
| 173 | + }, |
| 174 | + }, |
| 175 | + RobotID: "robot", |
| 176 | + RobotRunID: "1", |
| 177 | + FixSuggestions: []gerrit.FixSuggestionInfo{ |
| 178 | + { |
| 179 | + FixID: "c3302a6f_1578ee9e", |
| 180 | + Description: "suggestion", |
| 181 | + Replacements: []gerrit.FixReplacementInfo{ |
| 182 | + { |
| 183 | + Path: "main.c", |
| 184 | + Range: gerrit.CommentRange{ |
| 185 | + StartLine: 3, |
| 186 | + StartCharacter: 0, |
| 187 | + EndLine: 5, |
| 188 | + EndCharacter: 1, |
| 189 | + }, |
| 190 | + Replacement: "int main() { printf(\"Hello world!\"); }", |
| 191 | + }, |
| 192 | + }, |
| 193 | + }, |
| 194 | + }, |
| 195 | + }, |
| 196 | + }, |
| 197 | + } |
| 198 | + |
| 199 | + if !reflect.DeepEqual(got, want) { |
| 200 | + t.Errorf("Change.ListRevisionRobotComments:\ngot: %+v\nwant: %+v", got, want) |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +func TestChangesService_GetRobotComment(t *testing.T) { |
| 205 | + getRobotCommentResponse := `)]}' |
| 206 | + { |
| 207 | + "robot_id": "robot", |
| 208 | + "robot_run_id": "1", |
| 209 | + "fix_suggestions": [ |
| 210 | + { |
| 211 | + "fix_id": "c3302a6f_1578ee9e", |
| 212 | + "description": "suggestion", |
| 213 | + "replacements": [ |
| 214 | + { |
| 215 | + "path": "main.c", |
| 216 | + "range": { |
| 217 | + "start_line": 3, |
| 218 | + "start_character": 0, |
| 219 | + "end_line": 5, |
| 220 | + "end_character": 1 |
| 221 | + }, |
| 222 | + "replacement": "int main() { printf(\"Hello world!\"); }" |
| 223 | + } |
| 224 | + ] |
| 225 | + } |
| 226 | + ], |
| 227 | + "author": { |
| 228 | + "_account_id": 1000000, |
| 229 | + "name": "Jhon Smith", |
| 230 | + "username": "jhon" |
| 231 | + }, |
| 232 | + "change_message_id": "517e6c92e4bd105f1e611294a3010ea177771551", |
| 233 | + "patch_set": 1, |
| 234 | + "id": "f7d3d07f_bf17b66e", |
| 235 | + "line": 5, |
| 236 | + "range": { |
| 237 | + "start_line": 3, |
| 238 | + "start_character": 0, |
| 239 | + "end_line": 5, |
| 240 | + "end_character": 1 |
| 241 | + }, |
| 242 | + "updated": "2022-07-05 13:44:34.000000000", |
| 243 | + "message": "[clang-format] fix suggestion", |
| 244 | + "commit_id": "be8ce493368f2ce0fa73b56f5e2bd0dc17ca4359" |
| 245 | + }` |
| 246 | + |
| 247 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 248 | + if r.URL.Path != "/changes/changeID/revisions/revisionID/robotcomments/commentID" { |
| 249 | + t.Errorf("%s != /changes/changeID/revisions/revisionID/robotcomments/commentID", r.URL.Path) |
| 250 | + } |
| 251 | + if r.Method != "GET" { |
| 252 | + t.Error("Method != GET") |
| 253 | + } |
| 254 | + fmt.Fprint(w, getRobotCommentResponse) |
| 255 | + })) |
| 256 | + defer ts.Close() |
| 257 | + |
| 258 | + client, err := gerrit.NewClient(ts.URL, nil) |
| 259 | + if err != nil { |
| 260 | + t.Error(err) |
| 261 | + } |
| 262 | + |
| 263 | + got, _, err := client.Changes.GetRobotComment("changeID", "revisionID", "commentID") |
| 264 | + if err != nil { |
| 265 | + t.Errorf("Changes.GetRobotComment returned error: %v", err) |
| 266 | + } |
| 267 | + |
| 268 | + want := &gerrit.RobotCommentInfo{ |
| 269 | + CommentInfo: gerrit.CommentInfo{ |
| 270 | + PatchSet: 1, |
| 271 | + ID: "f7d3d07f_bf17b66e", |
| 272 | + Line: 5, |
| 273 | + Range: &gerrit.CommentRange{ |
| 274 | + StartLine: 3, |
| 275 | + StartCharacter: 0, |
| 276 | + EndLine: 5, |
| 277 | + EndCharacter: 1, |
| 278 | + }, |
| 279 | + Message: "[clang-format] fix suggestion", |
| 280 | + Updated: &gerrit.Timestamp{ |
| 281 | + Time: time.Date(2022, 7, 5, 13, 44, 34, 0, time.UTC), |
| 282 | + }, |
| 283 | + Author: gerrit.AccountInfo{ |
| 284 | + AccountID: 1000000, |
| 285 | + Name: "Jhon Smith", |
| 286 | + Username: "jhon", |
| 287 | + }, |
| 288 | + }, |
| 289 | + RobotID: "robot", |
| 290 | + RobotRunID: "1", |
| 291 | + FixSuggestions: []gerrit.FixSuggestionInfo{ |
| 292 | + { |
| 293 | + FixID: "c3302a6f_1578ee9e", |
| 294 | + Description: "suggestion", |
| 295 | + Replacements: []gerrit.FixReplacementInfo{ |
| 296 | + { |
| 297 | + Path: "main.c", |
| 298 | + Range: gerrit.CommentRange{ |
| 299 | + StartLine: 3, |
| 300 | + StartCharacter: 0, |
| 301 | + EndLine: 5, |
| 302 | + EndCharacter: 1, |
| 303 | + }, |
| 304 | + Replacement: "int main() { printf(\"Hello world!\"); }", |
| 305 | + }, |
| 306 | + }, |
| 307 | + }, |
| 308 | + }, |
| 309 | + } |
| 310 | + |
| 311 | + if !reflect.DeepEqual(got, want) { |
| 312 | + t.Errorf("Change.GetRobotComment:\ngot: %+v\nwant: %+v", got, want) |
| 313 | + } |
| 314 | +} |
0 commit comments