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

* Fix tests code to prevent some runtime errors

* Reduce nesting
release/v1.15
Morlinest 2017-08-28 11:17:45 +02:00 committed by Lauris BH
parent ced50e0ec1
commit 8e61823ec4
15 changed files with 181 additions and 150 deletions

View File

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

View File

@ -103,7 +103,7 @@ func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
pushCommits.Len = len(pushCommits.Commits)
payloadCommits := pushCommits.ToAPIPayloadCommits("/username/reponame")
assert.Len(t, payloadCommits, 2)
if assert.Len(t, payloadCommits, 2) {
assert.Equal(t, "abcdef1", payloadCommits[0].ID)
assert.Equal(t, "message1", payloadCommits[0].Message)
assert.Equal(t, "/username/reponame/commit/abcdef1", payloadCommits[0].URL)
@ -120,6 +120,7 @@ func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
assert.Equal(t, "User Two", payloadCommits[1].Author.Name)
assert.Equal(t, "user2", payloadCommits[1].Author.UserName)
}
}
func TestPushCommits_AvatarLink(t *testing.T) {
pushCommits := NewPushCommits()
@ -313,9 +314,10 @@ func TestGetFeeds(t *testing.T) {
IncludeDeleted: true,
})
assert.NoError(t, err)
assert.Len(t, actions, 1)
if assert.Len(t, actions, 1) {
assert.EqualValues(t, 1, actions[0].ID)
assert.EqualValues(t, user.ID, actions[0].UserID)
}
actions, err = GetFeeds(GetFeedsOptions{
RequestedUser: user,
@ -342,8 +344,10 @@ func TestGetFeeds2(t *testing.T) {
})
assert.NoError(t, err)
assert.Len(t, actions, 1)
if assert.Len(t, actions, 1) {
assert.EqualValues(t, 2, actions[0].ID)
assert.EqualValues(t, org.ID, actions[0].UserID)
}
actions, err = GetFeeds(GetFeedsOptions{
RequestedUser: org,

View File

@ -54,15 +54,17 @@ func TestNotices(t *testing.T) {
notices, err := Notices(1, 2)
assert.NoError(t, err)
assert.Len(t, notices, 2)
if assert.Len(t, notices, 2) {
assert.Equal(t, int64(3), notices[0].ID)
assert.Equal(t, int64(2), notices[1].ID)
}
notices, err = Notices(2, 2)
assert.NoError(t, err)
assert.Len(t, notices, 1)
if assert.Len(t, notices, 1) {
assert.Equal(t, int64(1), notices[0].ID)
}
}
func TestDeleteNotice(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

View File

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

View File

@ -98,10 +98,11 @@ func TestGetLabelsInRepoByIDs(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
labels, err := GetLabelsInRepoByIDs(1, []int64{1, 2, NonexistentID})
assert.NoError(t, err)
assert.Len(t, labels, 2)
if assert.Len(t, labels, 2) {
assert.EqualValues(t, 1, labels[0].ID)
assert.EqualValues(t, 2, labels[1].ID)
}
}
func TestGetLabelsByRepoID(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
@ -123,8 +124,9 @@ func TestGetLabelsByIssueID(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
labels, err := GetLabelsByIssueID(1)
assert.NoError(t, err)
assert.Len(t, labels, 1)
if assert.Len(t, labels, 1) {
assert.EqualValues(t, 1, labels[0].ID)
}
labels, err = GetLabelsByIssueID(NonexistentID)
assert.NoError(t, err)

View File

@ -29,10 +29,11 @@ func TestNotificationsForUser(t *testing.T) {
statuses := []NotificationStatus{NotificationStatusRead, NotificationStatusUnread}
notfs, err := NotificationsForUser(user, statuses, 1, 10)
assert.NoError(t, err)
assert.Len(t, notfs, 1)
if assert.Len(t, notfs, 1) {
assert.EqualValues(t, 2, notfs[0].ID)
assert.EqualValues(t, user.ID, notfs[0].UserID)
}
}
func TestNotification_GetRepo(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

View File

@ -68,19 +68,21 @@ func TestUser_GetTeams(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
assert.NoError(t, org.GetTeams())
assert.Len(t, org.Teams, 2)
if assert.Len(t, org.Teams, 2) {
assert.Equal(t, int64(1), org.Teams[0].ID)
assert.Equal(t, int64(2), org.Teams[1].ID)
}
}
func TestUser_GetMembers(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
assert.NoError(t, org.GetMembers())
assert.Len(t, org.Members, 2)
if assert.Len(t, org.Members, 2) {
assert.Equal(t, int64(2), org.Members[0].ID)
assert.Equal(t, int64(4), org.Members[1].ID)
}
}
func TestUser_AddMember(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
@ -240,11 +242,12 @@ func TestOrganizations(t *testing.T) {
testSuccess := func(opts *SearchUserOptions, expectedOrgIDs []int64) {
orgs, err := Organizations(opts)
assert.NoError(t, err)
assert.Len(t, orgs, len(expectedOrgIDs))
if assert.Len(t, orgs, len(expectedOrgIDs)) {
for i, expectedOrgID := range expectedOrgIDs {
assert.EqualValues(t, expectedOrgID, orgs[i].ID)
}
}
}
testSuccess(&SearchUserOptions{OrderBy: "id ASC", Page: 1, PageSize: 2},
[]int64{3, 6})
@ -307,8 +310,9 @@ func TestGetOrgsByUserID(t *testing.T) {
orgs, err := GetOrgsByUserID(4, true)
assert.NoError(t, err)
assert.Len(t, orgs, 1)
if assert.Len(t, orgs, 1) {
assert.EqualValues(t, 3, orgs[0].ID)
}
orgs, err = GetOrgsByUserID(4, false)
assert.NoError(t, err)
@ -320,8 +324,9 @@ func TestGetOwnedOrgsByUserID(t *testing.T) {
orgs, err := GetOwnedOrgsByUserID(2)
assert.NoError(t, err)
assert.Len(t, orgs, 1)
if assert.Len(t, orgs, 1) {
assert.EqualValues(t, 3, orgs[0].ID)
}
orgs, err = GetOwnedOrgsByUserID(4)
assert.NoError(t, err)
@ -333,9 +338,10 @@ func TestGetOwnedOrgsByUserIDDesc(t *testing.T) {
orgs, err := GetOwnedOrgsByUserIDDesc(5, "id")
assert.NoError(t, err)
assert.Len(t, orgs, 2)
if assert.Len(t, orgs, 2) {
assert.EqualValues(t, 7, orgs[0].ID)
assert.EqualValues(t, 6, orgs[1].ID)
}
orgs, err = GetOwnedOrgsByUserIDDesc(4, "id")
assert.NoError(t, err)
@ -347,7 +353,7 @@ func TestGetOrgUsersByUserID(t *testing.T) {
orgUsers, err := GetOrgUsersByUserID(5, true)
assert.NoError(t, err)
assert.Len(t, orgUsers, 2)
if assert.Len(t, orgUsers, 2) {
assert.Equal(t, OrgUser{
ID: orgUsers[0].ID,
OrgID: 6,
@ -362,6 +368,7 @@ func TestGetOrgUsersByUserID(t *testing.T) {
IsOwner: true,
IsPublic: false,
NumTeams: 1}, *orgUsers[1])
}
publicOrgUsers, err := GetOrgUsersByUserID(5, false)
assert.NoError(t, err)
@ -378,7 +385,7 @@ func TestGetOrgUsersByOrgID(t *testing.T) {
orgUsers, err := GetOrgUsersByOrgID(3)
assert.NoError(t, err)
assert.Len(t, orgUsers, 2)
if assert.Len(t, orgUsers, 2) {
assert.Equal(t, OrgUser{
ID: orgUsers[0].ID,
OrgID: 3,
@ -393,6 +400,7 @@ func TestGetOrgUsersByOrgID(t *testing.T) {
IsOwner: false,
IsPublic: false,
NumTeams: 0}, *orgUsers[1])
}
orgUsers, err = GetOrgUsersByOrgID(NonexistentID)
assert.NoError(t, err)

View File

@ -65,10 +65,11 @@ func TestPullRequestsNewest(t *testing.T) {
})
assert.NoError(t, err)
assert.Equal(t, int64(2), count)
assert.Len(t, prs, 2)
if assert.Len(t, prs, 2) {
assert.Equal(t, int64(2), prs[0].ID)
assert.Equal(t, int64(1), prs[1].ID)
}
}
func TestPullRequestsOldest(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
@ -80,10 +81,11 @@ func TestPullRequestsOldest(t *testing.T) {
})
assert.NoError(t, err)
assert.Equal(t, int64(2), count)
assert.Len(t, prs, 2)
if assert.Len(t, prs, 2) {
assert.Equal(t, int64(1), prs[0].ID)
assert.Equal(t, int64(2), prs[1].ID)
}
}
func TestGetUnmergedPullRequest(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

View File

@ -35,9 +35,10 @@ func TestRepository_GetStargazers(t *testing.T) {
repo := AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
gazers, err := repo.GetStargazers(0)
assert.NoError(t, err)
assert.Len(t, gazers, 1)
if assert.Len(t, gazers, 1) {
assert.Equal(t, int64(2), gazers[0].ID)
}
}
func TestRepository_GetStargazers2(t *testing.T) {
// repo with stargazers
@ -55,15 +56,17 @@ func TestUser_GetStarredRepos(t *testing.T) {
user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
starred, err := user.GetStarredRepos(false, 1, 10, "")
assert.NoError(t, err)
assert.Len(t, starred, 1)
if assert.Len(t, starred, 1) {
assert.Equal(t, int64(4), starred[0].ID)
}
starred, err = user.GetStarredRepos(true, 1, 10, "")
assert.NoError(t, err)
assert.Len(t, starred, 2)
if assert.Len(t, starred, 2) {
assert.Equal(t, int64(2), starred[0].ID)
assert.Equal(t, int64(4), starred[1].ID)
}
}
func TestUser_GetStarredRepos2(t *testing.T) {
// user who has no starred repos

View File

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

View File

@ -48,17 +48,19 @@ func TestListAccessTokens(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
tokens, err := ListAccessTokens(1)
assert.NoError(t, err)
assert.Len(t, tokens, 2)
if assert.Len(t, tokens, 2) {
assert.Equal(t, int64(1), tokens[0].UID)
assert.Equal(t, int64(1), tokens[1].UID)
assert.Contains(t, []string{tokens[0].Name, tokens[1].Name}, "Token A")
assert.Contains(t, []string{tokens[0].Name, tokens[1].Name}, "Token B")
}
tokens, err = ListAccessTokens(2)
assert.NoError(t, err)
assert.Len(t, tokens, 1)
if assert.Len(t, tokens, 1) {
assert.Equal(t, int64(2), tokens[0].UID)
assert.Equal(t, "Token A", tokens[0].Name)
}
tokens, err = ListAccessTokens(100)
assert.NoError(t, err)

View File

@ -70,8 +70,7 @@ func TestListToPushCommits(t *testing.T) {
pushCommits := ListToPushCommits(l)
assert.Equal(t, 2, pushCommits.Len)
assert.Equal(t, 2, len(pushCommits.Commits))
if assert.Len(t, pushCommits.Commits, 2) {
assert.Equal(t, "Message1", pushCommits.Commits[0].Message)
assert.Equal(t, hexString1, pushCommits.Commits[0].Sha1)
assert.Equal(t, "example@example.com", pushCommits.Commits[0].AuthorEmail)
@ -82,5 +81,6 @@ func TestListToPushCommits(t *testing.T) {
assert.Equal(t, "example@example.com", pushCommits.Commits[1].AuthorEmail)
assert.Equal(t, now, pushCommits.Commits[1].Timestamp)
}
}
// TODO TestPushUpdate

View File

@ -14,16 +14,18 @@ func TestGetEmailAddresses(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
emails, _ := GetEmailAddresses(int64(1))
assert.Len(t, emails, 3)
if assert.Len(t, emails, 3) {
assert.False(t, emails[0].IsPrimary)
assert.True(t, emails[2].IsActivated)
assert.True(t, emails[2].IsPrimary)
}
emails, _ = GetEmailAddresses(int64(2))
assert.Len(t, emails, 2)
if assert.Len(t, emails, 2) {
assert.True(t, emails[0].IsPrimary)
assert.True(t, emails[0].IsActivated)
}
}
func TestIsEmailUsed(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

View File

@ -14,8 +14,7 @@ func TestGetUserOpenIDs(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
oids, err := GetUserOpenIDs(int64(1))
if assert.NoError(t, err) {
assert.Len(t, oids, 2)
if assert.NoError(t, err) && assert.Len(t, oids, 2) {
assert.Equal(t, oids[0].URI, "https://user1.domain1.tld/")
assert.False(t, oids[0].Show)
assert.Equal(t, oids[1].URI, "http://user1.domain2.tld/")
@ -23,8 +22,7 @@ func TestGetUserOpenIDs(t *testing.T) {
}
oids, err = GetUserOpenIDs(int64(2))
if assert.NoError(t, err) {
assert.Len(t, oids, 1)
if assert.NoError(t, err) && assert.Len(t, oids, 1) {
assert.Equal(t, oids[0].URI, "https://domain1.tld/user2/")
assert.True(t, oids[0].Show)
}
@ -52,10 +50,9 @@ func TestGetUserByOpenID(t *testing.T) {
func TestToggleUserOpenIDVisibility(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
oids, err := GetUserOpenIDs(int64(2))
if !assert.NoError(t, err) {
if !assert.NoError(t, err) || !assert.Len(t, oids, 1) {
return
}
assert.Len(t, oids, 1)
assert.True(t, oids[0].Show)
err = ToggleUserOpenIDVisibility(oids[0].ID)
@ -64,10 +61,10 @@ func TestToggleUserOpenIDVisibility(t *testing.T) {
}
oids, err = GetUserOpenIDs(int64(2))
if assert.NoError(t, err) {
assert.Len(t, oids, 1)
if !assert.NoError(t, err) || !assert.Len(t, oids, 1) {
return
}
assert.False(t, oids[0].Show)
}
err = ToggleUserOpenIDVisibility(oids[0].ID)
if !assert.NoError(t, err) {
return
@ -77,6 +74,7 @@ func TestToggleUserOpenIDVisibility(t *testing.T) {
if !assert.NoError(t, err) {
return
}
assert.Len(t, oids, 1)
if assert.Len(t, oids, 1) {
assert.True(t, oids[0].Show)
}
}

View File

@ -41,8 +41,9 @@ func TestWebhook_History(t *testing.T) {
webhook := AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook)
tasks, err := webhook.History(0)
assert.NoError(t, err)
assert.Len(t, tasks, 1)
if assert.Len(t, tasks, 1) {
assert.Equal(t, int64(1), tasks[0].ID)
}
webhook = AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook)
tasks, err = webhook.History(0)
@ -123,36 +124,40 @@ func TestGetActiveWebhooksByRepoID(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
hooks, err := GetActiveWebhooksByRepoID(1)
assert.NoError(t, err)
assert.Len(t, hooks, 1)
if assert.Len(t, hooks, 1) {
assert.Equal(t, int64(1), hooks[0].ID)
assert.True(t, hooks[0].IsActive)
}
}
func TestGetWebhooksByRepoID(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
hooks, err := GetWebhooksByRepoID(1)
assert.NoError(t, err)
assert.Len(t, hooks, 2)
if assert.Len(t, hooks, 2) {
assert.Equal(t, int64(1), hooks[0].ID)
assert.Equal(t, int64(2), hooks[1].ID)
}
}
func TestGetActiveWebhooksByOrgID(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
hooks, err := GetActiveWebhooksByOrgID(3)
assert.NoError(t, err)
assert.Len(t, hooks, 1)
if assert.Len(t, hooks, 1) {
assert.Equal(t, int64(3), hooks[0].ID)
assert.True(t, hooks[0].IsActive)
}
}
func TestGetWebhooksByOrgID(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
hooks, err := GetWebhooksByOrgID(3)
assert.NoError(t, err)
assert.Len(t, hooks, 1)
if assert.Len(t, hooks, 1) {
assert.Equal(t, int64(3), hooks[0].ID)
assert.True(t, hooks[0].IsActive)
}
}
@ -211,8 +216,9 @@ func TestHookTasks(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
hookTasks, err := HookTasks(1, 1)
assert.NoError(t, err)
assert.Len(t, hookTasks, 1)
if assert.Len(t, hookTasks, 1) {
assert.Equal(t, int64(1), hookTasks[0].ID)
}
hookTasks, err = HookTasks(NonexistentID, 1)
assert.NoError(t, err)