github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/inactive-review-handler_test.go (about) 1 /* 2 Copyright 2017 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 mungers 18 19 import ( 20 githubapi "github.com/google/go-github/github" 21 "runtime" 22 "testing" 23 ) 24 25 const ( 26 John = "John" //author 27 Ken = "Ken" 28 Lisa = "Lisa" 29 Max = "Max" 30 positiveComment = "The changes look great!" 31 negativeComment = "The changes break things!" 32 ) 33 34 func TestInactiveReviewHandler(t *testing.T) { 35 runtime.GOMAXPROCS(runtime.NumCPU()) 36 37 haveNonAuthorHumanTests := []struct { 38 name string 39 comments []*githubapi.IssueComment 40 reviewComments []*githubapi.PullRequestComment 41 expected bool 42 }{ 43 { 44 name: "IssueComment is empty, PullRequestComment is empty", 45 comments: []*githubapi.IssueComment{}, 46 reviewComments: []*githubapi.PullRequestComment{}, 47 expected: false, 48 }, 49 { 50 name: "IssueComment is not empty without non-author human, PullRequestComment is empty", 51 comments: []*githubapi.IssueComment{ 52 { 53 Body: stringPtr(positiveComment), 54 User: &githubapi.User{Login: githubapi.String(John)}}, 55 }, 56 reviewComments: []*githubapi.PullRequestComment{}, 57 expected: false, 58 }, 59 { 60 name: "IssueComment is not empty with non-author human, PullRequestComment is empty", 61 comments: []*githubapi.IssueComment{ 62 { 63 Body: stringPtr(positiveComment), 64 User: &githubapi.User{Login: githubapi.String(Lisa)}}, 65 { 66 Body: stringPtr(positiveComment), 67 User: &githubapi.User{Login: githubapi.String(John)}}, 68 }, 69 reviewComments: []*githubapi.PullRequestComment{}, 70 expected: true, 71 }, 72 { 73 name: "IssueComment is empty, PullRequestComment is not empty without non-author human", 74 comments: []*githubapi.IssueComment{}, 75 reviewComments: []*githubapi.PullRequestComment{ 76 { 77 Body: stringPtr(positiveComment), 78 User: &githubapi.User{Login: githubapi.String(John)}}, 79 }, 80 expected: false, 81 }, 82 { 83 name: "IssueComment is empty, PullRequestComment is not empty with non-author human", 84 comments: []*githubapi.IssueComment{}, 85 reviewComments: []*githubapi.PullRequestComment{ 86 { 87 Body: stringPtr(positiveComment), 88 User: &githubapi.User{Login: githubapi.String(Lisa)}}, 89 { 90 Body: stringPtr(positiveComment), 91 User: &githubapi.User{Login: githubapi.String(John)}}, 92 }, 93 expected: true, 94 }, 95 { 96 name: "IssueComment is not empty without non-author human, PullRequestComment is not empty without non-author human", 97 comments: []*githubapi.IssueComment{ 98 { 99 Body: stringPtr(positiveComment), 100 User: &githubapi.User{Login: githubapi.String(John)}}, 101 }, 102 reviewComments: []*githubapi.PullRequestComment{ 103 { 104 Body: stringPtr(positiveComment), 105 User: &githubapi.User{Login: githubapi.String(John)}}, 106 }, 107 expected: false, 108 }, 109 { 110 name: "IssueComment is not empty without non-author human, PullRequestComment is not empty with non-author human", 111 comments: []*githubapi.IssueComment{ 112 { 113 Body: stringPtr(positiveComment), 114 User: &githubapi.User{Login: githubapi.String(John)}}, 115 }, 116 reviewComments: []*githubapi.PullRequestComment{ 117 { 118 Body: stringPtr(positiveComment), 119 User: &githubapi.User{Login: githubapi.String(Lisa)}}, 120 { 121 Body: stringPtr(positiveComment), 122 User: &githubapi.User{Login: githubapi.String(John)}}, 123 }, 124 expected: true, 125 }, 126 { 127 name: "IssueComment is not empty with non-author human, PullRequestComment is not empty without non-author human", 128 comments: []*githubapi.IssueComment{ 129 { 130 Body: stringPtr(positiveComment), 131 User: &githubapi.User{Login: githubapi.String(Lisa)}}, 132 { 133 Body: stringPtr(positiveComment), 134 User: &githubapi.User{Login: githubapi.String(John)}}, 135 }, 136 reviewComments: []*githubapi.PullRequestComment{ 137 { 138 Body: stringPtr(positiveComment), 139 User: &githubapi.User{Login: githubapi.String(John)}}, 140 }, 141 expected: true, 142 }, 143 { 144 name: "IssueComment is not empty with non-author human, PullRequestComment is not empty with non-author human", 145 comments: []*githubapi.IssueComment{ 146 { 147 Body: stringPtr(positiveComment), 148 User: &githubapi.User{Login: githubapi.String(Lisa)}}, 149 { 150 Body: stringPtr(positiveComment), 151 User: &githubapi.User{Login: githubapi.String(John)}}, 152 }, 153 reviewComments: []*githubapi.PullRequestComment{ 154 { 155 Body: stringPtr(positiveComment), 156 User: &githubapi.User{Login: githubapi.String(Lisa)}}, 157 { 158 Body: stringPtr(positiveComment), 159 User: &githubapi.User{Login: githubapi.String(John)}}, 160 }, 161 expected: true, 162 }, 163 { 164 name: "IssueComment is not empty with multiple non-author humans, PullRequestComment is not empty with multiple non-author humans", 165 comments: []*githubapi.IssueComment{ 166 { 167 Body: stringPtr(positiveComment), 168 User: &githubapi.User{Login: githubapi.String(Lisa)}}, 169 { 170 Body: stringPtr(positiveComment), 171 User: &githubapi.User{Login: githubapi.String(Ken)}}, 172 { 173 Body: stringPtr(positiveComment), 174 User: &githubapi.User{Login: githubapi.String(John)}}, 175 }, 176 reviewComments: []*githubapi.PullRequestComment{ 177 { 178 Body: stringPtr(positiveComment), 179 User: &githubapi.User{Login: githubapi.String(Max)}}, 180 { 181 Body: stringPtr(positiveComment), 182 User: &githubapi.User{Login: githubapi.String(Lisa)}}, 183 { 184 Body: stringPtr(positiveComment), 185 User: &githubapi.User{Login: githubapi.String(John)}}, 186 }, 187 expected: true, 188 }, 189 } 190 191 for testNum, test := range haveNonAuthorHumanTests { 192 i := InactiveReviewHandler{} 193 found := i.haveNonAuthorHuman(stringPtr(John), test.comments, test.reviewComments) 194 195 if test.expected != found { 196 t.Errorf("%d:%s: expected: %t, saw: %t", testNum, test.name, test.expected, found) 197 } 198 } 199 200 suggestNewReviewerTests := []struct { 201 name string 202 issue *githubapi.Issue 203 potentialOwners map[string]int64 204 weightSum int64 205 expected string 206 }{ 207 { 208 name: "initial len(potentialOwners) == 0", 209 issue: &githubapi.Issue{ 210 PullRequestLinks: &githubapi.PullRequestLinks{}, 211 User: &githubapi.User{Login: githubapi.String(John)}, 212 Number: intPtr(1), 213 Assignees: []*githubapi.User{{Login: githubapi.String(Ken)}, 214 {Login: githubapi.String(Lisa)}}, 215 }, 216 potentialOwners: make(map[string]int64), 217 weightSum: 0, 218 expected: "", 219 }, 220 { 221 name: "initial len(potentialOwners) > 0, but final len(potentialOwners) == 0", 222 issue: &githubapi.Issue{ 223 PullRequestLinks: &githubapi.PullRequestLinks{}, 224 User: &githubapi.User{Login: githubapi.String(John)}, 225 Number: intPtr(1), 226 Assignees: []*githubapi.User{{Login: githubapi.String(Ken)}, 227 {Login: githubapi.String(Lisa)}, 228 {Login: githubapi.String(Max)}}, 229 }, 230 potentialOwners: map[string]int64{"Ken": 27, "Lisa": 39, "Max": 34}, 231 weightSum: 100, 232 expected: "", 233 }, 234 { 235 name: "initial len(potentialOwners) == 0, issue.Assignees == nil", 236 issue: &githubapi.Issue{ 237 PullRequestLinks: &githubapi.PullRequestLinks{}, 238 User: &githubapi.User{Login: githubapi.String(John)}, 239 Number: intPtr(1), 240 Assignees: nil, 241 }, 242 potentialOwners: make(map[string]int64), 243 weightSum: 0, 244 expected: "", 245 }, 246 { 247 name: "initial len(potentialOwners) > 0, issue.Assignees == nil", 248 issue: &githubapi.Issue{ 249 PullRequestLinks: &githubapi.PullRequestLinks{}, 250 User: &githubapi.User{Login: githubapi.String(John)}, 251 Number: intPtr(1), 252 Assignees: nil, 253 }, 254 potentialOwners: map[string]int64{"Lisa": 39}, 255 weightSum: 39, 256 expected: "Lisa", 257 }, 258 { 259 name: "initial len(potentialOwners) == 0, final len(issue.Assignees) == 0", 260 issue: &githubapi.Issue{ 261 PullRequestLinks: &githubapi.PullRequestLinks{}, 262 User: &githubapi.User{Login: githubapi.String(John)}, 263 Number: intPtr(1), 264 Assignees: []*githubapi.User{}, 265 }, 266 potentialOwners: make(map[string]int64), 267 weightSum: 39, 268 expected: "", 269 }, 270 { 271 name: "initial len(potentialOwners) > 0, len(issue.Assignees) == 0", 272 issue: &githubapi.Issue{ 273 PullRequestLinks: &githubapi.PullRequestLinks{}, 274 User: &githubapi.User{Login: githubapi.String(John)}, 275 Number: intPtr(1), 276 Assignees: []*githubapi.User{}, 277 }, 278 potentialOwners: map[string]int64{"Lisa": 39}, 279 weightSum: 39, 280 expected: "Lisa", 281 }, 282 { 283 name: "len(potentialOwners) > 0, issue.Assignees != nil, len(issue.Assignees) > 0, author is not a potential owner", 284 issue: &githubapi.Issue{ 285 PullRequestLinks: &githubapi.PullRequestLinks{}, 286 User: &githubapi.User{Login: githubapi.String(John)}, 287 Number: intPtr(1), 288 Assignees: []*githubapi.User{{Login: githubapi.String(Ken)}, 289 {Login: githubapi.String(Lisa)}}, 290 }, 291 potentialOwners: map[string]int64{"Ken": 27, "Lisa": 39, "Max": 34}, 292 weightSum: 100, 293 expected: "Max", 294 }, 295 { 296 name: "len(potentialOwners) > 0, issue.Assignees != nil, len(issue.Assignees) > 0, author is a potential owner", 297 issue: &githubapi.Issue{ 298 PullRequestLinks: &githubapi.PullRequestLinks{}, 299 User: &githubapi.User{Login: githubapi.String(Max)}, 300 Number: intPtr(1), 301 Assignees: []*githubapi.User{{Login: githubapi.String(Ken)}, 302 {Login: githubapi.String(Lisa)}}, 303 }, 304 potentialOwners: map[string]int64{"Ken": 27, "Lisa": 39}, 305 weightSum: 66, 306 expected: "", 307 }, 308 { 309 name: "len(potentialOwners) > 0, issue.Assignees != nil, len(issue.Assignees) > 0, all potential owners have already been assigned", 310 issue: &githubapi.Issue{ 311 PullRequestLinks: &githubapi.PullRequestLinks{}, 312 User: &githubapi.User{Login: githubapi.String(John)}, 313 Number: intPtr(1), 314 Assignees: []*githubapi.User{{Login: githubapi.String(Ken)}, 315 {Login: githubapi.String(Lisa)}, 316 {Login: githubapi.String(Max)}}, 317 }, 318 potentialOwners: map[string]int64{"Ken": 27, "Lisa": 39, "Max": 34}, 319 weightSum: 100, 320 expected: "", 321 }, 322 } 323 324 for testNum, test := range suggestNewReviewerTests { 325 i := InactiveReviewHandler{} 326 327 newReviewer := i.suggestNewReviewer(test.issue, test.potentialOwners, test.weightSum) 328 329 if test.expected != newReviewer { 330 t.Errorf("%d:%s: expected: %v, saw: %v, potentialOwners: %v", testNum, test.name, test.expected, newReviewer, test.potentialOwners) 331 } 332 } 333 }