Browse Source

Fix tests code to prevent some runtime errors (#2381)

* Fix tests code to prevent some runtime errors

* Reduce nesting
Morlinest 2 years ago
parent
commit
8e61823ec4

+ 3 - 2
integrations/api_issue_label_test.go

@@ -54,8 +54,9 @@ func TestAPIReplaceIssueLabels(t *testing.T) {
54 54
 	resp := session.MakeRequest(t, req, http.StatusOK)
55 55
 	var apiLabels []*api.Label
56 56
 	DecodeJSON(t, resp, &apiLabels)
57
-	assert.Len(t, apiLabels, 1)
58
-	assert.EqualValues(t, label.ID, apiLabels[0].ID)
57
+	if assert.Len(t, apiLabels, 1) {
58
+		assert.EqualValues(t, label.ID, apiLabels[0].ID)
59
+	}
59 60
 
60 61
 	models.AssertCount(t, &models.IssueLabel{IssueID: issue.ID}, 1)
61 62
 	models.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: issue.ID, LabelID: label.ID})

+ 25 - 21
models/action_test.go

@@ -103,22 +103,23 @@ func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
103 103
 	pushCommits.Len = len(pushCommits.Commits)
104 104
 
105 105
 	payloadCommits := pushCommits.ToAPIPayloadCommits("/username/reponame")
106
-	assert.Len(t, payloadCommits, 2)
107
-	assert.Equal(t, "abcdef1", payloadCommits[0].ID)
108
-	assert.Equal(t, "message1", payloadCommits[0].Message)
109
-	assert.Equal(t, "/username/reponame/commit/abcdef1", payloadCommits[0].URL)
110
-	assert.Equal(t, "User Two", payloadCommits[0].Committer.Name)
111
-	assert.Equal(t, "user2", payloadCommits[0].Committer.UserName)
112
-	assert.Equal(t, "User Four", payloadCommits[0].Author.Name)
113
-	assert.Equal(t, "user4", payloadCommits[0].Author.UserName)
114
-
115
-	assert.Equal(t, "abcdef2", payloadCommits[1].ID)
116
-	assert.Equal(t, "message2", payloadCommits[1].Message)
117
-	assert.Equal(t, "/username/reponame/commit/abcdef2", payloadCommits[1].URL)
118
-	assert.Equal(t, "User Two", payloadCommits[1].Committer.Name)
119
-	assert.Equal(t, "user2", payloadCommits[1].Committer.UserName)
120
-	assert.Equal(t, "User Two", payloadCommits[1].Author.Name)
121
-	assert.Equal(t, "user2", payloadCommits[1].Author.UserName)
106
+	if assert.Len(t, payloadCommits, 2) {
107
+		assert.Equal(t, "abcdef1", payloadCommits[0].ID)
108
+		assert.Equal(t, "message1", payloadCommits[0].Message)
109
+		assert.Equal(t, "/username/reponame/commit/abcdef1", payloadCommits[0].URL)
110
+		assert.Equal(t, "User Two", payloadCommits[0].Committer.Name)
111
+		assert.Equal(t, "user2", payloadCommits[0].Committer.UserName)
112
+		assert.Equal(t, "User Four", payloadCommits[0].Author.Name)
113
+		assert.Equal(t, "user4", payloadCommits[0].Author.UserName)
114
+
115
+		assert.Equal(t, "abcdef2", payloadCommits[1].ID)
116
+		assert.Equal(t, "message2", payloadCommits[1].Message)
117
+		assert.Equal(t, "/username/reponame/commit/abcdef2", payloadCommits[1].URL)
118
+		assert.Equal(t, "User Two", payloadCommits[1].Committer.Name)
119
+		assert.Equal(t, "user2", payloadCommits[1].Committer.UserName)
120
+		assert.Equal(t, "User Two", payloadCommits[1].Author.Name)
121
+		assert.Equal(t, "user2", payloadCommits[1].Author.UserName)
122
+	}
122 123
 }
123 124
 
124 125
 func TestPushCommits_AvatarLink(t *testing.T) {
@@ -313,9 +314,10 @@ func TestGetFeeds(t *testing.T) {
313 314
 		IncludeDeleted:   true,
314 315
 	})
315 316
 	assert.NoError(t, err)
316
-	assert.Len(t, actions, 1)
317
-	assert.EqualValues(t, 1, actions[0].ID)
318
-	assert.EqualValues(t, user.ID, actions[0].UserID)
317
+	if assert.Len(t, actions, 1) {
318
+		assert.EqualValues(t, 1, actions[0].ID)
319
+		assert.EqualValues(t, user.ID, actions[0].UserID)
320
+	}
319 321
 
320 322
 	actions, err = GetFeeds(GetFeedsOptions{
321 323
 		RequestedUser:    user,
@@ -342,8 +344,10 @@ func TestGetFeeds2(t *testing.T) {
342 344
 	})
343 345
 	assert.NoError(t, err)
344 346
 	assert.Len(t, actions, 1)
345
-	assert.EqualValues(t, 2, actions[0].ID)
346
-	assert.EqualValues(t, org.ID, actions[0].UserID)
347
+	if assert.Len(t, actions, 1) {
348
+		assert.EqualValues(t, 2, actions[0].ID)
349
+		assert.EqualValues(t, org.ID, actions[0].UserID)
350
+	}
347 351
 
348 352
 	actions, err = GetFeeds(GetFeedsOptions{
349 353
 		RequestedUser:    org,

+ 7 - 5
models/admin_test.go

@@ -54,14 +54,16 @@ func TestNotices(t *testing.T) {
54 54
 
55 55
 	notices, err := Notices(1, 2)
56 56
 	assert.NoError(t, err)
57
-	assert.Len(t, notices, 2)
58
-	assert.Equal(t, int64(3), notices[0].ID)
59
-	assert.Equal(t, int64(2), notices[1].ID)
57
+	if assert.Len(t, notices, 2) {
58
+		assert.Equal(t, int64(3), notices[0].ID)
59
+		assert.Equal(t, int64(2), notices[1].ID)
60
+	}
60 61
 
61 62
 	notices, err = Notices(2, 2)
62 63
 	assert.NoError(t, err)
63
-	assert.Len(t, notices, 1)
64
-	assert.Equal(t, int64(1), notices[0].ID)
64
+	if assert.Len(t, notices, 1) {
65
+		assert.Equal(t, int64(1), notices[0].ID)
66
+	}
65 67
 }
66 68
 
67 69
 func TestDeleteNotice(t *testing.T) {

+ 3 - 2
models/gpg_key_test.go

@@ -219,6 +219,7 @@ Q0KHb+QcycSgbDx0ZAvdIacuKvBBcbxrsmFUI4LR+oIup0G9gUc0roPvr014jYQL
219 219
 	key, err := AddGPGKey(1, testEmailWithUpperCaseLetters)
220 220
 	assert.NoError(t, err)
221 221
 
222
-	assert.Len(t, key.Emails, 1)
223
-	assert.Equal(t, "user1@example.com", key.Emails[0].Email)
222
+	if assert.Len(t, key.Emails, 1) {
223
+		assert.Equal(t, "user1@example.com", key.Emails[0].Email)
224
+	}
224 225
 }

+ 7 - 5
models/issue_label_test.go

@@ -98,9 +98,10 @@ func TestGetLabelsInRepoByIDs(t *testing.T) {
98 98
 	assert.NoError(t, PrepareTestDatabase())
99 99
 	labels, err := GetLabelsInRepoByIDs(1, []int64{1, 2, NonexistentID})
100 100
 	assert.NoError(t, err)
101
-	assert.Len(t, labels, 2)
102
-	assert.EqualValues(t, 1, labels[0].ID)
103
-	assert.EqualValues(t, 2, labels[1].ID)
101
+	if assert.Len(t, labels, 2) {
102
+		assert.EqualValues(t, 1, labels[0].ID)
103
+		assert.EqualValues(t, 2, labels[1].ID)
104
+	}
104 105
 }
105 106
 
106 107
 func TestGetLabelsByRepoID(t *testing.T) {
@@ -123,8 +124,9 @@ func TestGetLabelsByIssueID(t *testing.T) {
123 124
 	assert.NoError(t, PrepareTestDatabase())
124 125
 	labels, err := GetLabelsByIssueID(1)
125 126
 	assert.NoError(t, err)
126
-	assert.Len(t, labels, 1)
127
-	assert.EqualValues(t, 1, labels[0].ID)
127
+	if assert.Len(t, labels, 1) {
128
+		assert.EqualValues(t, 1, labels[0].ID)
129
+	}
128 130
 
129 131
 	labels, err = GetLabelsByIssueID(NonexistentID)
130 132
 	assert.NoError(t, err)

+ 4 - 3
models/notification_test.go

@@ -29,9 +29,10 @@ func TestNotificationsForUser(t *testing.T) {
29 29
 	statuses := []NotificationStatus{NotificationStatusRead, NotificationStatusUnread}
30 30
 	notfs, err := NotificationsForUser(user, statuses, 1, 10)
31 31
 	assert.NoError(t, err)
32
-	assert.Len(t, notfs, 1)
33
-	assert.EqualValues(t, 2, notfs[0].ID)
34
-	assert.EqualValues(t, user.ID, notfs[0].UserID)
32
+	if assert.Len(t, notfs, 1) {
33
+		assert.EqualValues(t, 2, notfs[0].ID)
34
+		assert.EqualValues(t, user.ID, notfs[0].UserID)
35
+	}
35 36
 }
36 37
 
37 38
 func TestNotification_GetRepo(t *testing.T) {

+ 54 - 46
models/org_test.go

@@ -68,18 +68,20 @@ func TestUser_GetTeams(t *testing.T) {
68 68
 	assert.NoError(t, PrepareTestDatabase())
69 69
 	org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
70 70
 	assert.NoError(t, org.GetTeams())
71
-	assert.Len(t, org.Teams, 2)
72
-	assert.Equal(t, int64(1), org.Teams[0].ID)
73
-	assert.Equal(t, int64(2), org.Teams[1].ID)
71
+	if assert.Len(t, org.Teams, 2) {
72
+		assert.Equal(t, int64(1), org.Teams[0].ID)
73
+		assert.Equal(t, int64(2), org.Teams[1].ID)
74
+	}
74 75
 }
75 76
 
76 77
 func TestUser_GetMembers(t *testing.T) {
77 78
 	assert.NoError(t, PrepareTestDatabase())
78 79
 	org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
79 80
 	assert.NoError(t, org.GetMembers())
80
-	assert.Len(t, org.Members, 2)
81
-	assert.Equal(t, int64(2), org.Members[0].ID)
82
-	assert.Equal(t, int64(4), org.Members[1].ID)
81
+	if assert.Len(t, org.Members, 2) {
82
+		assert.Equal(t, int64(2), org.Members[0].ID)
83
+		assert.Equal(t, int64(4), org.Members[1].ID)
84
+	}
83 85
 }
84 86
 
85 87
 func TestUser_AddMember(t *testing.T) {
@@ -240,9 +242,10 @@ func TestOrganizations(t *testing.T) {
240 242
 	testSuccess := func(opts *SearchUserOptions, expectedOrgIDs []int64) {
241 243
 		orgs, err := Organizations(opts)
242 244
 		assert.NoError(t, err)
243
-		assert.Len(t, orgs, len(expectedOrgIDs))
244
-		for i, expectedOrgID := range expectedOrgIDs {
245
-			assert.EqualValues(t, expectedOrgID, orgs[i].ID)
245
+		if assert.Len(t, orgs, len(expectedOrgIDs)) {
246
+			for i, expectedOrgID := range expectedOrgIDs {
247
+				assert.EqualValues(t, expectedOrgID, orgs[i].ID)
248
+			}
246 249
 		}
247 250
 	}
248 251
 	testSuccess(&SearchUserOptions{OrderBy: "id ASC", Page: 1, PageSize: 2},
@@ -307,8 +310,9 @@ func TestGetOrgsByUserID(t *testing.T) {
307 310
 
308 311
 	orgs, err := GetOrgsByUserID(4, true)
309 312
 	assert.NoError(t, err)
310
-	assert.Len(t, orgs, 1)
311
-	assert.EqualValues(t, 3, orgs[0].ID)
313
+	if assert.Len(t, orgs, 1) {
314
+		assert.EqualValues(t, 3, orgs[0].ID)
315
+	}
312 316
 
313 317
 	orgs, err = GetOrgsByUserID(4, false)
314 318
 	assert.NoError(t, err)
@@ -320,8 +324,9 @@ func TestGetOwnedOrgsByUserID(t *testing.T) {
320 324
 
321 325
 	orgs, err := GetOwnedOrgsByUserID(2)
322 326
 	assert.NoError(t, err)
323
-	assert.Len(t, orgs, 1)
324
-	assert.EqualValues(t, 3, orgs[0].ID)
327
+	if assert.Len(t, orgs, 1) {
328
+		assert.EqualValues(t, 3, orgs[0].ID)
329
+	}
325 330
 
326 331
 	orgs, err = GetOwnedOrgsByUserID(4)
327 332
 	assert.NoError(t, err)
@@ -333,9 +338,10 @@ func TestGetOwnedOrgsByUserIDDesc(t *testing.T) {
333 338
 
334 339
 	orgs, err := GetOwnedOrgsByUserIDDesc(5, "id")
335 340
 	assert.NoError(t, err)
336
-	assert.Len(t, orgs, 2)
337
-	assert.EqualValues(t, 7, orgs[0].ID)
338
-	assert.EqualValues(t, 6, orgs[1].ID)
341
+	if assert.Len(t, orgs, 2) {
342
+		assert.EqualValues(t, 7, orgs[0].ID)
343
+		assert.EqualValues(t, 6, orgs[1].ID)
344
+	}
339 345
 
340 346
 	orgs, err = GetOwnedOrgsByUserIDDesc(4, "id")
341 347
 	assert.NoError(t, err)
@@ -347,21 +353,22 @@ func TestGetOrgUsersByUserID(t *testing.T) {
347 353
 
348 354
 	orgUsers, err := GetOrgUsersByUserID(5, true)
349 355
 	assert.NoError(t, err)
350
-	assert.Len(t, orgUsers, 2)
351
-	assert.Equal(t, OrgUser{
352
-		ID:       orgUsers[0].ID,
353
-		OrgID:    6,
354
-		UID:      5,
355
-		IsOwner:  true,
356
-		IsPublic: true,
357
-		NumTeams: 1}, *orgUsers[0])
358
-	assert.Equal(t, OrgUser{
359
-		ID:       orgUsers[1].ID,
360
-		OrgID:    7,
361
-		UID:      5,
362
-		IsOwner:  true,
363
-		IsPublic: false,
364
-		NumTeams: 1}, *orgUsers[1])
356
+	if assert.Len(t, orgUsers, 2) {
357
+		assert.Equal(t, OrgUser{
358
+			ID:       orgUsers[0].ID,
359
+			OrgID:    6,
360
+			UID:      5,
361
+			IsOwner:  true,
362
+			IsPublic: true,
363
+			NumTeams: 1}, *orgUsers[0])
364
+		assert.Equal(t, OrgUser{
365
+			ID:       orgUsers[1].ID,
366
+			OrgID:    7,
367
+			UID:      5,
368
+			IsOwner:  true,
369
+			IsPublic: false,
370
+			NumTeams: 1}, *orgUsers[1])
371
+	}
365 372
 
366 373
 	publicOrgUsers, err := GetOrgUsersByUserID(5, false)
367 374
 	assert.NoError(t, err)
@@ -378,21 +385,22 @@ func TestGetOrgUsersByOrgID(t *testing.T) {
378 385
 
379 386
 	orgUsers, err := GetOrgUsersByOrgID(3)
380 387
 	assert.NoError(t, err)
381
-	assert.Len(t, orgUsers, 2)
382
-	assert.Equal(t, OrgUser{
383
-		ID:       orgUsers[0].ID,
384
-		OrgID:    3,
385
-		UID:      2,
386
-		IsOwner:  true,
387
-		IsPublic: true,
388
-		NumTeams: 1}, *orgUsers[0])
389
-	assert.Equal(t, OrgUser{
390
-		ID:       orgUsers[1].ID,
391
-		OrgID:    3,
392
-		UID:      4,
393
-		IsOwner:  false,
394
-		IsPublic: false,
395
-		NumTeams: 0}, *orgUsers[1])
388
+	if assert.Len(t, orgUsers, 2) {
389
+		assert.Equal(t, OrgUser{
390
+			ID:       orgUsers[0].ID,
391
+			OrgID:    3,
392
+			UID:      2,
393
+			IsOwner:  true,
394
+			IsPublic: true,
395
+			NumTeams: 1}, *orgUsers[0])
396
+		assert.Equal(t, OrgUser{
397
+			ID:       orgUsers[1].ID,
398
+			OrgID:    3,
399
+			UID:      4,
400
+			IsOwner:  false,
401
+			IsPublic: false,
402
+			NumTeams: 0}, *orgUsers[1])
403
+	}
396 404
 
397 405
 	orgUsers, err = GetOrgUsersByOrgID(NonexistentID)
398 406
 	assert.NoError(t, err)

+ 8 - 6
models/pull_test.go

@@ -65,9 +65,10 @@ func TestPullRequestsNewest(t *testing.T) {
65 65
 	})
66 66
 	assert.NoError(t, err)
67 67
 	assert.Equal(t, int64(2), count)
68
-	assert.Len(t, prs, 2)
69
-	assert.Equal(t, int64(2), prs[0].ID)
70
-	assert.Equal(t, int64(1), prs[1].ID)
68
+	if assert.Len(t, prs, 2) {
69
+		assert.Equal(t, int64(2), prs[0].ID)
70
+		assert.Equal(t, int64(1), prs[1].ID)
71
+	}
71 72
 }
72 73
 
73 74
 func TestPullRequestsOldest(t *testing.T) {
@@ -80,9 +81,10 @@ func TestPullRequestsOldest(t *testing.T) {
80 81
 	})
81 82
 	assert.NoError(t, err)
82 83
 	assert.Equal(t, int64(2), count)
83
-	assert.Len(t, prs, 2)
84
-	assert.Equal(t, int64(1), prs[0].ID)
85
-	assert.Equal(t, int64(2), prs[1].ID)
84
+	if assert.Len(t, prs, 2) {
85
+		assert.Equal(t, int64(1), prs[0].ID)
86
+		assert.Equal(t, int64(2), prs[1].ID)
87
+	}
86 88
 }
87 89
 
88 90
 func TestGetUnmergedPullRequest(t *testing.T) {

+ 10 - 7
models/star_test.go

@@ -35,8 +35,9 @@ func TestRepository_GetStargazers(t *testing.T) {
35 35
 	repo := AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
36 36
 	gazers, err := repo.GetStargazers(0)
37 37
 	assert.NoError(t, err)
38
-	assert.Len(t, gazers, 1)
39
-	assert.Equal(t, int64(2), gazers[0].ID)
38
+	if assert.Len(t, gazers, 1) {
39
+		assert.Equal(t, int64(2), gazers[0].ID)
40
+	}
40 41
 }
41 42
 
42 43
 func TestRepository_GetStargazers2(t *testing.T) {
@@ -55,14 +56,16 @@ func TestUser_GetStarredRepos(t *testing.T) {
55 56
 	user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
56 57
 	starred, err := user.GetStarredRepos(false, 1, 10, "")
57 58
 	assert.NoError(t, err)
58
-	assert.Len(t, starred, 1)
59
-	assert.Equal(t, int64(4), starred[0].ID)
59
+	if assert.Len(t, starred, 1) {
60
+		assert.Equal(t, int64(4), starred[0].ID)
61
+	}
60 62
 
61 63
 	starred, err = user.GetStarredRepos(true, 1, 10, "")
62 64
 	assert.NoError(t, err)
63
-	assert.Len(t, starred, 2)
64
-	assert.Equal(t, int64(2), starred[0].ID)
65
-	assert.Equal(t, int64(4), starred[1].ID)
65
+	if assert.Len(t, starred, 2) {
66
+		assert.Equal(t, int64(2), starred[0].ID)
67
+		assert.Equal(t, int64(4), starred[1].ID)
68
+	}
66 69
 }
67 70
 
68 71
 func TestUser_GetStarredRepos2(t *testing.T) {

+ 1 - 2
models/status_test.go

@@ -19,8 +19,7 @@ func TestGetCommitStatuses(t *testing.T) {
19 19
 
20 20
 	statuses, err := GetCommitStatuses(repo1, sha1, 0)
21 21
 	assert.NoError(t, err)
22
-	if assert.Equal(t, 5, len(statuses), "Expected to get 5 statuses") {
23
-
22
+	if assert.Len(t, statuses, 5) {
24 23
 		assert.Equal(t, statuses[0].Context, "ci/awesomeness")
25 24
 		assert.Equal(t, statuses[0].State, CommitStatusPending)
26 25
 

+ 10 - 8
models/token_test.go

@@ -48,17 +48,19 @@ func TestListAccessTokens(t *testing.T) {
48 48
 	assert.NoError(t, PrepareTestDatabase())
49 49
 	tokens, err := ListAccessTokens(1)
50 50
 	assert.NoError(t, err)
51
-	assert.Len(t, tokens, 2)
52
-	assert.Equal(t, int64(1), tokens[0].UID)
53
-	assert.Equal(t, int64(1), tokens[1].UID)
54
-	assert.Contains(t, []string{tokens[0].Name, tokens[1].Name}, "Token A")
55
-	assert.Contains(t, []string{tokens[0].Name, tokens[1].Name}, "Token B")
51
+	if assert.Len(t, tokens, 2) {
52
+		assert.Equal(t, int64(1), tokens[0].UID)
53
+		assert.Equal(t, int64(1), tokens[1].UID)
54
+		assert.Contains(t, []string{tokens[0].Name, tokens[1].Name}, "Token A")
55
+		assert.Contains(t, []string{tokens[0].Name, tokens[1].Name}, "Token B")
56
+	}
56 57
 
57 58
 	tokens, err = ListAccessTokens(2)
58 59
 	assert.NoError(t, err)
59
-	assert.Len(t, tokens, 1)
60
-	assert.Equal(t, int64(2), tokens[0].UID)
61
-	assert.Equal(t, "Token A", tokens[0].Name)
60
+	if assert.Len(t, tokens, 1) {
61
+		assert.Equal(t, int64(2), tokens[0].UID)
62
+		assert.Equal(t, "Token A", tokens[0].Name)
63
+	}
62 64
 
63 65
 	tokens, err = ListAccessTokens(100)
64 66
 	assert.NoError(t, err)

+ 10 - 10
models/update_test.go

@@ -70,17 +70,17 @@ func TestListToPushCommits(t *testing.T) {
70 70
 
71 71
 	pushCommits := ListToPushCommits(l)
72 72
 	assert.Equal(t, 2, pushCommits.Len)
73
-	assert.Equal(t, 2, len(pushCommits.Commits))
73
+	if assert.Len(t, pushCommits.Commits, 2) {
74
+		assert.Equal(t, "Message1", pushCommits.Commits[0].Message)
75
+		assert.Equal(t, hexString1, pushCommits.Commits[0].Sha1)
76
+		assert.Equal(t, "example@example.com", pushCommits.Commits[0].AuthorEmail)
77
+		assert.Equal(t, now, pushCommits.Commits[0].Timestamp)
74 78
 
75
-	assert.Equal(t, "Message1", pushCommits.Commits[0].Message)
76
-	assert.Equal(t, hexString1, pushCommits.Commits[0].Sha1)
77
-	assert.Equal(t, "example@example.com", pushCommits.Commits[0].AuthorEmail)
78
-	assert.Equal(t, now, pushCommits.Commits[0].Timestamp)
79
-
80
-	assert.Equal(t, "Message2", pushCommits.Commits[1].Message)
81
-	assert.Equal(t, hexString2, pushCommits.Commits[1].Sha1)
82
-	assert.Equal(t, "example@example.com", pushCommits.Commits[1].AuthorEmail)
83
-	assert.Equal(t, now, pushCommits.Commits[1].Timestamp)
79
+		assert.Equal(t, "Message2", pushCommits.Commits[1].Message)
80
+		assert.Equal(t, hexString2, pushCommits.Commits[1].Sha1)
81
+		assert.Equal(t, "example@example.com", pushCommits.Commits[1].AuthorEmail)
82
+		assert.Equal(t, now, pushCommits.Commits[1].Timestamp)
83
+	}
84 84
 }
85 85
 
86 86
 // TODO TestPushUpdate

+ 9 - 7
models/user_mail_test.go

@@ -14,15 +14,17 @@ func TestGetEmailAddresses(t *testing.T) {
14 14
 	assert.NoError(t, PrepareTestDatabase())
15 15
 
16 16
 	emails, _ := GetEmailAddresses(int64(1))
17
-	assert.Len(t, emails, 3)
18
-	assert.False(t, emails[0].IsPrimary)
19
-	assert.True(t, emails[2].IsActivated)
20
-	assert.True(t, emails[2].IsPrimary)
17
+	if assert.Len(t, emails, 3) {
18
+		assert.False(t, emails[0].IsPrimary)
19
+		assert.True(t, emails[2].IsActivated)
20
+		assert.True(t, emails[2].IsPrimary)
21
+	}
21 22
 
22 23
 	emails, _ = GetEmailAddresses(int64(2))
23
-	assert.Len(t, emails, 2)
24
-	assert.True(t, emails[0].IsPrimary)
25
-	assert.True(t, emails[0].IsActivated)
24
+	if assert.Len(t, emails, 2) {
25
+		assert.True(t, emails[0].IsPrimary)
26
+		assert.True(t, emails[0].IsActivated)
27
+	}
26 28
 }
27 29
 
28 30
 func TestIsEmailUsed(t *testing.T) {

+ 9 - 11
models/user_openid_test.go

@@ -14,8 +14,7 @@ func TestGetUserOpenIDs(t *testing.T) {
14 14
 	assert.NoError(t, PrepareTestDatabase())
15 15
 
16 16
 	oids, err := GetUserOpenIDs(int64(1))
17
-	if assert.NoError(t, err) {
18
-		assert.Len(t, oids, 2)
17
+	if assert.NoError(t, err) && assert.Len(t, oids, 2) {
19 18
 		assert.Equal(t, oids[0].URI, "https://user1.domain1.tld/")
20 19
 		assert.False(t, oids[0].Show)
21 20
 		assert.Equal(t, oids[1].URI, "http://user1.domain2.tld/")
@@ -23,8 +22,7 @@ func TestGetUserOpenIDs(t *testing.T) {
23 22
 	}
24 23
 
25 24
 	oids, err = GetUserOpenIDs(int64(2))
26
-	if assert.NoError(t, err) {
27
-		assert.Len(t, oids, 1)
25
+	if assert.NoError(t, err) && assert.Len(t, oids, 1) {
28 26
 		assert.Equal(t, oids[0].URI, "https://domain1.tld/user2/")
29 27
 		assert.True(t, oids[0].Show)
30 28
 	}
@@ -52,10 +50,9 @@ func TestGetUserByOpenID(t *testing.T) {
52 50
 func TestToggleUserOpenIDVisibility(t *testing.T) {
53 51
 	assert.NoError(t, PrepareTestDatabase())
54 52
 	oids, err := GetUserOpenIDs(int64(2))
55
-	if !assert.NoError(t, err) {
53
+	if !assert.NoError(t, err) || !assert.Len(t, oids, 1) {
56 54
 		return
57 55
 	}
58
-	assert.Len(t, oids, 1)
59 56
 	assert.True(t, oids[0].Show)
60 57
 
61 58
 	err = ToggleUserOpenIDVisibility(oids[0].ID)
@@ -64,10 +61,10 @@ func TestToggleUserOpenIDVisibility(t *testing.T) {
64 61
 	}
65 62
 
66 63
 	oids, err = GetUserOpenIDs(int64(2))
67
-	if assert.NoError(t, err) {
68
-		assert.Len(t, oids, 1)
69
-		assert.False(t, oids[0].Show)
64
+	if !assert.NoError(t, err) || !assert.Len(t, oids, 1) {
65
+		return
70 66
 	}
67
+	assert.False(t, oids[0].Show)
71 68
 	err = ToggleUserOpenIDVisibility(oids[0].ID)
72 69
 	if !assert.NoError(t, err) {
73 70
 		return
@@ -77,6 +74,7 @@ func TestToggleUserOpenIDVisibility(t *testing.T) {
77 74
 	if !assert.NoError(t, err) {
78 75
 		return
79 76
 	}
80
-	assert.Len(t, oids, 1)
81
-	assert.True(t, oids[0].Show)
77
+	if assert.Len(t, oids, 1) {
78
+		assert.True(t, oids[0].Show)
79
+	}
82 80
 }

+ 22 - 16
models/webhook_test.go

@@ -41,8 +41,9 @@ func TestWebhook_History(t *testing.T) {
41 41
 	webhook := AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook)
42 42
 	tasks, err := webhook.History(0)
43 43
 	assert.NoError(t, err)
44
-	assert.Len(t, tasks, 1)
45
-	assert.Equal(t, int64(1), tasks[0].ID)
44
+	if assert.Len(t, tasks, 1) {
45
+		assert.Equal(t, int64(1), tasks[0].ID)
46
+	}
46 47
 
47 48
 	webhook = AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook)
48 49
 	tasks, err = webhook.History(0)
@@ -123,36 +124,40 @@ func TestGetActiveWebhooksByRepoID(t *testing.T) {
123 124
 	assert.NoError(t, PrepareTestDatabase())
124 125
 	hooks, err := GetActiveWebhooksByRepoID(1)
125 126
 	assert.NoError(t, err)
126
-	assert.Len(t, hooks, 1)
127
-	assert.Equal(t, int64(1), hooks[0].ID)
128
-	assert.True(t, hooks[0].IsActive)
127
+	if assert.Len(t, hooks, 1) {
128
+		assert.Equal(t, int64(1), hooks[0].ID)
129
+		assert.True(t, hooks[0].IsActive)
130
+	}
129 131
 }
130 132
 
131 133
 func TestGetWebhooksByRepoID(t *testing.T) {
132 134
 	assert.NoError(t, PrepareTestDatabase())
133 135
 	hooks, err := GetWebhooksByRepoID(1)
134 136
 	assert.NoError(t, err)
135
-	assert.Len(t, hooks, 2)
136
-	assert.Equal(t, int64(1), hooks[0].ID)
137
-	assert.Equal(t, int64(2), hooks[1].ID)
137
+	if assert.Len(t, hooks, 2) {
138
+		assert.Equal(t, int64(1), hooks[0].ID)
139
+		assert.Equal(t, int64(2), hooks[1].ID)
140
+	}
138 141
 }
139 142
 
140 143
 func TestGetActiveWebhooksByOrgID(t *testing.T) {
141 144
 	assert.NoError(t, PrepareTestDatabase())
142 145
 	hooks, err := GetActiveWebhooksByOrgID(3)
143 146
 	assert.NoError(t, err)
144
-	assert.Len(t, hooks, 1)
145
-	assert.Equal(t, int64(3), hooks[0].ID)
146
-	assert.True(t, hooks[0].IsActive)
147
+	if assert.Len(t, hooks, 1) {
148
+		assert.Equal(t, int64(3), hooks[0].ID)
149
+		assert.True(t, hooks[0].IsActive)
150
+	}
147 151
 }
148 152
 
149 153
 func TestGetWebhooksByOrgID(t *testing.T) {
150 154
 	assert.NoError(t, PrepareTestDatabase())
151 155
 	hooks, err := GetWebhooksByOrgID(3)
152 156
 	assert.NoError(t, err)
153
-	assert.Len(t, hooks, 1)
154
-	assert.Equal(t, int64(3), hooks[0].ID)
155
-	assert.True(t, hooks[0].IsActive)
157
+	if assert.Len(t, hooks, 1) {
158
+		assert.Equal(t, int64(3), hooks[0].ID)
159
+		assert.True(t, hooks[0].IsActive)
160
+	}
156 161
 
157 162
 }
158 163
 
@@ -211,8 +216,9 @@ func TestHookTasks(t *testing.T) {
211 216
 	assert.NoError(t, PrepareTestDatabase())
212 217
 	hookTasks, err := HookTasks(1, 1)
213 218
 	assert.NoError(t, err)
214
-	assert.Len(t, hookTasks, 1)
215
-	assert.Equal(t, int64(1), hookTasks[0].ID)
219
+	if assert.Len(t, hookTasks, 1) {
220
+		assert.Equal(t, int64(1), hookTasks[0].ID)
221
+	}
216 222
 
217 223
 	hookTasks, err = HookTasks(NonexistentID, 1)
218 224
 	assert.NoError(t, err)