github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/velodrome/fetcher/conversion_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "reflect" 21 "strconv" 22 "testing" 23 "time" 24 25 "k8s.io/test-infra/velodrome/sql" 26 27 "github.com/google/go-github/github" 28 ) 29 30 func makeIssue(number int, 31 title, body, state, user, prUrl, repository string, 32 comments int, 33 isPullRequest bool, 34 createdAt, updatedAt, closedAt time.Time) *sql.Issue { 35 36 var pClosedAt *time.Time 37 if !closedAt.IsZero() { 38 pClosedAt = &closedAt 39 } 40 41 return &sql.Issue{ 42 ID: strconv.Itoa(number), 43 Title: title, 44 Body: body, 45 User: user, 46 State: state, 47 Comments: comments, 48 IsPR: isPullRequest, 49 IssueClosedAt: pClosedAt, 50 IssueCreatedAt: createdAt, 51 IssueUpdatedAt: updatedAt, 52 Repository: repository, 53 } 54 } 55 56 func makeGithubIssue(number int, 57 title, body, state, user, prUrl string, 58 comments int, 59 isPullRequest bool, 60 createdAt, updatedAt, closedAt time.Time) *github.Issue { 61 62 var pBody *string 63 if body != "" { 64 pBody = &body 65 } 66 var pullRequest *github.PullRequestLinks 67 if prUrl != "" { 68 pullRequest = &github.PullRequestLinks{URL: &prUrl} 69 } 70 gUser := &github.User{Login: &user} 71 var pClosedAt *time.Time 72 if !closedAt.IsZero() { 73 pClosedAt = &closedAt 74 } 75 76 return &github.Issue{ 77 Number: &number, 78 Title: &title, 79 Body: pBody, 80 State: &state, 81 User: gUser, 82 Comments: &comments, 83 PullRequestLinks: pullRequest, 84 CreatedAt: &createdAt, 85 UpdatedAt: &updatedAt, 86 ClosedAt: pClosedAt, 87 } 88 } 89 90 func TestNewIssue(t *testing.T) { 91 tests := []struct { 92 gIssue *github.Issue 93 mIssue *sql.Issue 94 }{ 95 // Only mandatory 96 { 97 gIssue: makeGithubIssue(1, "Title", "", "State", "User", "", 98 5, false, 99 time.Date(1900, time.January, 1, 19, 30, 0, 0, time.UTC), 100 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC), 101 time.Time{}), 102 mIssue: makeIssue(1, "Title", "", "State", "User", "", "full/repo", 103 5, false, 104 time.Date(1900, time.January, 1, 19, 30, 0, 0, time.UTC), 105 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC), 106 time.Time{}), 107 }, 108 // All fields 109 { 110 gIssue: makeGithubIssue(1, "Title", "Body", "State", "User", 111 "PRLink", 5, true, 112 time.Date(1900, time.January, 1, 19, 30, 0, 0, time.UTC), 113 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC), 114 time.Date(2100, time.January, 1, 19, 30, 0, 0, time.UTC)), 115 mIssue: makeIssue(1, "Title", "Body", "State", "User", 116 "PRLink", "full/repo", 5, true, 117 time.Date(1900, time.January, 1, 19, 30, 0, 0, time.UTC), 118 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC), 119 time.Date(2100, time.January, 1, 19, 30, 0, 0, time.UTC)), 120 }, 121 // Missing mandatory fields returns nil 122 { 123 &github.Issue{}, 124 nil, 125 }, 126 } 127 128 for _, test := range tests { 129 // Ignore the error, we will compare the nil issue to expected 130 actualIssue, _ := NewIssue(test.gIssue, "FULL/REPO") 131 if actualIssue != nil && reflect.DeepEqual(actualIssue.Labels, []sql.Label{}) { 132 actualIssue.Labels = nil 133 } 134 if actualIssue != nil && reflect.DeepEqual(actualIssue.Assignees, []sql.Assignee{}) { 135 actualIssue.Assignees = nil 136 } 137 if !reflect.DeepEqual(actualIssue, test.mIssue) { 138 t.Error("Actual: ", actualIssue, 139 "doesn't match expected: ", test.mIssue) 140 } 141 } 142 } 143 144 func makeIssueEvent( 145 eventId, issueId int, 146 label, event, assignee, actor, repository string, 147 createdAt time.Time) *sql.IssueEvent { 148 149 var pLabel, pAssignee, pActor *string 150 if label != "" { 151 pLabel = &label 152 } 153 if actor != "" { 154 pActor = &actor 155 } 156 if assignee != "" { 157 pAssignee = &assignee 158 } 159 160 return &sql.IssueEvent{ 161 ID: strconv.Itoa(eventId), 162 Label: pLabel, 163 Event: event, 164 EventCreatedAt: createdAt, 165 IssueId: strconv.Itoa(issueId), 166 Assignee: pAssignee, 167 Actor: pActor, 168 Repository: repository, 169 } 170 } 171 172 func makeGithubIssueEvent( 173 eventId int, 174 label, event, assignee, actor string, 175 createdAt time.Time) *github.IssueEvent { 176 177 var gLabel *github.Label 178 if label != "" { 179 gLabel = &github.Label{Name: &label} 180 } 181 182 var gAssignee, gActor *github.User 183 if assignee != "" { 184 gAssignee = &github.User{Login: &assignee} 185 } 186 187 if actor != "" { 188 gActor = &github.User{Login: &actor} 189 } 190 191 return &github.IssueEvent{ 192 ID: &eventId, 193 Label: gLabel, 194 Event: &event, 195 CreatedAt: &createdAt, 196 Assignee: gAssignee, 197 Actor: gActor, 198 } 199 } 200 201 func TestNewIssueEvent(t *testing.T) { 202 tests := []struct { 203 gIssueEvent *github.IssueEvent 204 issueID int 205 mIssueEvent *sql.IssueEvent 206 }{ 207 // Only mandatory 208 { 209 gIssueEvent: makeGithubIssueEvent(1, "", "Event", "", "", 210 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC)), 211 issueID: 2, 212 mIssueEvent: makeIssueEvent(1, 2, "", "Event", "", "", "full/repo", 213 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC)), 214 }, 215 // All fields 216 { 217 gIssueEvent: makeGithubIssueEvent(1, "Label", "Event", "Assignee", "Actor", 218 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC)), 219 issueID: 2, 220 mIssueEvent: makeIssueEvent(1, 2, "Label", "Event", "Assignee", "Actor", "full/repo", 221 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC)), 222 }, 223 // Missing mandatory fields returns nil 224 { 225 &github.IssueEvent{}, 226 2, 227 nil, 228 }, 229 } 230 231 for _, test := range tests { 232 actualIssueEvent, _ := NewIssueEvent(test.gIssueEvent, test.issueID, "FULL/REPO") 233 if !reflect.DeepEqual(actualIssueEvent, test.mIssueEvent) { 234 t.Error("Actual: ", actualIssueEvent, 235 "doesn't match expected: ", test.mIssueEvent) 236 } 237 } 238 } 239 240 func createLabel(name string) github.Label { 241 return github.Label{Name: &name} 242 } 243 244 func TestNewLabels(t *testing.T) { 245 tests := []struct { 246 gLabels []github.Label 247 issueId int 248 expectedLabels []sql.Label 249 }{ 250 // Empty list gives empty list 251 { 252 []github.Label{}, 253 1, 254 []sql.Label{}, 255 }, 256 // Broken label 257 { 258 []github.Label{ 259 createLabel("SomeLabel"), 260 {}, 261 createLabel("OtherLabel"), 262 }, 263 2, 264 nil, 265 }, 266 } 267 268 for _, test := range tests { 269 actualLabels, _ := newLabels(test.issueId, test.gLabels, "FULL/REPO") 270 if !reflect.DeepEqual(actualLabels, test.expectedLabels) { 271 t.Error("Actual: ", actualLabels, 272 "doesn't match expected: ", test.expectedLabels) 273 } 274 } 275 } 276 277 func makeGithubIssueComment(id int, body, login string, createdAt, updatedAt time.Time) *github.IssueComment { 278 var user *github.User 279 if login != "" { 280 user = &github.User{Login: &login} 281 } 282 return &github.IssueComment{ 283 ID: &id, 284 User: user, 285 Body: &body, 286 CreatedAt: &createdAt, 287 UpdatedAt: &updatedAt, 288 } 289 } 290 291 func makeGithubPullComment(id int, body, login string, createdAt, updatedAt time.Time) *github.PullRequestComment { 292 var user *github.User 293 if login != "" { 294 user = &github.User{Login: &login} 295 } 296 return &github.PullRequestComment{ 297 ID: &id, 298 User: user, 299 Body: &body, 300 CreatedAt: &createdAt, 301 UpdatedAt: &updatedAt, 302 } 303 } 304 305 func makeComment(issueId, Id int, body, login, repository string, createdAt, updatedAt time.Time, pullRequest bool) *sql.Comment { 306 return &sql.Comment{ 307 ID: strconv.Itoa(Id), 308 IssueID: strconv.Itoa(issueId), 309 Body: body, 310 User: login, 311 CommentCreatedAt: createdAt, 312 CommentUpdatedAt: updatedAt, 313 PullRequest: pullRequest, 314 Repository: repository, 315 } 316 } 317 318 func TestNewIssueComment(t *testing.T) { 319 tests := []struct { 320 gComment *github.IssueComment 321 issueId int 322 expectedComment *sql.Comment 323 }{ 324 { 325 gComment: makeGithubIssueComment(1, "Body", "Login", 326 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC), 327 time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC)), 328 issueId: 12, 329 expectedComment: makeComment(12, 1, "Body", "Login", "full/repo", 330 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC), 331 time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC), false), 332 }, 333 { 334 gComment: makeGithubIssueComment(1, "Body", "", 335 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC), 336 time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC)), 337 issueId: 12, 338 expectedComment: makeComment(12, 1, "Body", "", "full/repo", 339 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC), 340 time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC), false), 341 }, 342 } 343 344 for _, test := range tests { 345 actualComment, _ := NewIssueComment(test.issueId, test.gComment, "FULL/REPO") 346 if !reflect.DeepEqual(actualComment, test.expectedComment) { 347 t.Error("Actual: ", actualComment, 348 "doesn't match expected: ", test.expectedComment) 349 } 350 } 351 } 352 353 func TestNewPullComment(t *testing.T) { 354 tests := []struct { 355 gComment *github.PullRequestComment 356 issueId int 357 repository string 358 expectedComment *sql.Comment 359 }{ 360 { 361 gComment: makeGithubPullComment(1, "Body", "Login", 362 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC), 363 time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC)), 364 issueId: 12, 365 repository: "FULL/REPO", 366 expectedComment: makeComment(12, 1, "Body", "Login", "full/repo", 367 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC), 368 time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC), true), 369 }, 370 { 371 gComment: makeGithubPullComment(1, "Body", "", 372 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC), 373 time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC)), 374 issueId: 12, 375 repository: "FULL/REPO", 376 expectedComment: makeComment(12, 1, "Body", "", "full/repo", 377 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC), 378 time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC), true), 379 }, 380 } 381 382 for _, test := range tests { 383 actualComment, _ := NewPullComment(test.issueId, test.gComment, test.repository) 384 if !reflect.DeepEqual(actualComment, test.expectedComment) { 385 t.Error("Actual: ", actualComment, 386 "doesn't match expected: ", test.expectedComment) 387 } 388 } 389 }