From 8e61823ec403b661359373f8f8b0c9a7c9ea368d Mon Sep 17 00:00:00 2001 From: Morlinest Date: Mon, 28 Aug 2017 11:17:45 +0200 Subject: [PATCH] Fix tests code to prevent some runtime errors (#2381) * Fix tests code to prevent some runtime errors * Reduce nesting --- integrations/api_issue_label_test.go | 5 +- models/action_test.go | 44 ++++++------ models/admin_test.go | 12 ++-- models/gpg_key_test.go | 5 +- models/issue_label_test.go | 12 ++-- models/notification_test.go | 7 +- models/org_test.go | 100 +++++++++++++++------------ models/pull_test.go | 14 ++-- models/star_test.go | 17 +++-- models/status_test.go | 3 +- models/token_test.go | 18 ++--- models/update_test.go | 20 +++--- models/user_mail_test.go | 16 +++-- models/user_openid_test.go | 20 +++--- models/webhook_test.go | 38 +++++----- 15 files changed, 181 insertions(+), 150 deletions(-) diff --git a/integrations/api_issue_label_test.go b/integrations/api_issue_label_test.go index 195ec24ca8..1bfaa4303e 100644 --- a/integrations/api_issue_label_test.go +++ b/integrations/api_issue_label_test.go @@ -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) - assert.EqualValues(t, label.ID, apiLabels[0].ID) + 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}) diff --git a/models/action_test.go b/models/action_test.go index 431469e732..cecc83f86b 100644 --- a/models/action_test.go +++ b/models/action_test.go @@ -103,22 +103,23 @@ func TestPushCommits_ToAPIPayloadCommits(t *testing.T) { pushCommits.Len = len(pushCommits.Commits) payloadCommits := pushCommits.ToAPIPayloadCommits("/username/reponame") - 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) - assert.Equal(t, "User Two", payloadCommits[0].Committer.Name) - assert.Equal(t, "user2", payloadCommits[0].Committer.UserName) - assert.Equal(t, "User Four", payloadCommits[0].Author.Name) - assert.Equal(t, "user4", payloadCommits[0].Author.UserName) + 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) + assert.Equal(t, "User Two", payloadCommits[0].Committer.Name) + assert.Equal(t, "user2", payloadCommits[0].Committer.UserName) + assert.Equal(t, "User Four", payloadCommits[0].Author.Name) + assert.Equal(t, "user4", payloadCommits[0].Author.UserName) - assert.Equal(t, "abcdef2", payloadCommits[1].ID) - assert.Equal(t, "message2", payloadCommits[1].Message) - assert.Equal(t, "/username/reponame/commit/abcdef2", payloadCommits[1].URL) - assert.Equal(t, "User Two", payloadCommits[1].Committer.Name) - assert.Equal(t, "user2", payloadCommits[1].Committer.UserName) - assert.Equal(t, "User Two", payloadCommits[1].Author.Name) - assert.Equal(t, "user2", payloadCommits[1].Author.UserName) + assert.Equal(t, "abcdef2", payloadCommits[1].ID) + assert.Equal(t, "message2", payloadCommits[1].Message) + assert.Equal(t, "/username/reponame/commit/abcdef2", payloadCommits[1].URL) + assert.Equal(t, "User Two", payloadCommits[1].Committer.Name) + assert.Equal(t, "user2", payloadCommits[1].Committer.UserName) + assert.Equal(t, "User Two", payloadCommits[1].Author.Name) + assert.Equal(t, "user2", payloadCommits[1].Author.UserName) + } } func TestPushCommits_AvatarLink(t *testing.T) { @@ -313,9 +314,10 @@ func TestGetFeeds(t *testing.T) { IncludeDeleted: true, }) assert.NoError(t, err) - assert.Len(t, actions, 1) - assert.EqualValues(t, 1, actions[0].ID) - assert.EqualValues(t, user.ID, actions[0].UserID) + 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) - assert.EqualValues(t, 2, actions[0].ID) - assert.EqualValues(t, org.ID, actions[0].UserID) + 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, diff --git a/models/admin_test.go b/models/admin_test.go index 7fb8f1d0a0..b4383cb2d3 100644 --- a/models/admin_test.go +++ b/models/admin_test.go @@ -54,14 +54,16 @@ func TestNotices(t *testing.T) { notices, err := Notices(1, 2) assert.NoError(t, err) - assert.Len(t, notices, 2) - assert.Equal(t, int64(3), notices[0].ID) - assert.Equal(t, int64(2), notices[1].ID) + 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) - assert.Equal(t, int64(1), notices[0].ID) + if assert.Len(t, notices, 1) { + assert.Equal(t, int64(1), notices[0].ID) + } } func TestDeleteNotice(t *testing.T) { diff --git a/models/gpg_key_test.go b/models/gpg_key_test.go index 687e0cd594..6b34b10760 100644 --- a/models/gpg_key_test.go +++ b/models/gpg_key_test.go @@ -219,6 +219,7 @@ Q0KHb+QcycSgbDx0ZAvdIacuKvBBcbxrsmFUI4LR+oIup0G9gUc0roPvr014jYQL key, err := AddGPGKey(1, testEmailWithUpperCaseLetters) assert.NoError(t, err) - assert.Len(t, key.Emails, 1) - assert.Equal(t, "user1@example.com", key.Emails[0].Email) + if assert.Len(t, key.Emails, 1) { + assert.Equal(t, "user1@example.com", key.Emails[0].Email) + } } diff --git a/models/issue_label_test.go b/models/issue_label_test.go index 33630cd901..aef3f70c03 100644 --- a/models/issue_label_test.go +++ b/models/issue_label_test.go @@ -98,9 +98,10 @@ 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) - assert.EqualValues(t, 1, labels[0].ID) - assert.EqualValues(t, 2, labels[1].ID) + 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) { @@ -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) - assert.EqualValues(t, 1, labels[0].ID) + if assert.Len(t, labels, 1) { + assert.EqualValues(t, 1, labels[0].ID) + } labels, err = GetLabelsByIssueID(NonexistentID) assert.NoError(t, err) diff --git a/models/notification_test.go b/models/notification_test.go index 57d686a474..2166afa2a6 100644 --- a/models/notification_test.go +++ b/models/notification_test.go @@ -29,9 +29,10 @@ 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) - assert.EqualValues(t, 2, notfs[0].ID) - assert.EqualValues(t, user.ID, notfs[0].UserID) + 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) { diff --git a/models/org_test.go b/models/org_test.go index 801088c2e2..07da2d56af 100644 --- a/models/org_test.go +++ b/models/org_test.go @@ -68,18 +68,20 @@ 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) - assert.Equal(t, int64(1), org.Teams[0].ID) - assert.Equal(t, int64(2), org.Teams[1].ID) + 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) - assert.Equal(t, int64(2), org.Members[0].ID) - assert.Equal(t, int64(4), org.Members[1].ID) + 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) { @@ -240,9 +242,10 @@ 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)) - for i, expectedOrgID := range expectedOrgIDs { - assert.EqualValues(t, expectedOrgID, orgs[i].ID) + 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}, @@ -307,8 +310,9 @@ func TestGetOrgsByUserID(t *testing.T) { orgs, err := GetOrgsByUserID(4, true) assert.NoError(t, err) - assert.Len(t, orgs, 1) - assert.EqualValues(t, 3, orgs[0].ID) + 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) - assert.EqualValues(t, 3, orgs[0].ID) + 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) - assert.EqualValues(t, 7, orgs[0].ID) - assert.EqualValues(t, 6, orgs[1].ID) + 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,21 +353,22 @@ func TestGetOrgUsersByUserID(t *testing.T) { orgUsers, err := GetOrgUsersByUserID(5, true) assert.NoError(t, err) - assert.Len(t, orgUsers, 2) - assert.Equal(t, OrgUser{ - ID: orgUsers[0].ID, - OrgID: 6, - UID: 5, - IsOwner: true, - IsPublic: true, - NumTeams: 1}, *orgUsers[0]) - assert.Equal(t, OrgUser{ - ID: orgUsers[1].ID, - OrgID: 7, - UID: 5, - IsOwner: true, - IsPublic: false, - NumTeams: 1}, *orgUsers[1]) + if assert.Len(t, orgUsers, 2) { + assert.Equal(t, OrgUser{ + ID: orgUsers[0].ID, + OrgID: 6, + UID: 5, + IsOwner: true, + IsPublic: true, + NumTeams: 1}, *orgUsers[0]) + assert.Equal(t, OrgUser{ + ID: orgUsers[1].ID, + OrgID: 7, + UID: 5, + IsOwner: true, + IsPublic: false, + NumTeams: 1}, *orgUsers[1]) + } publicOrgUsers, err := GetOrgUsersByUserID(5, false) assert.NoError(t, err) @@ -378,21 +385,22 @@ func TestGetOrgUsersByOrgID(t *testing.T) { orgUsers, err := GetOrgUsersByOrgID(3) assert.NoError(t, err) - assert.Len(t, orgUsers, 2) - assert.Equal(t, OrgUser{ - ID: orgUsers[0].ID, - OrgID: 3, - UID: 2, - IsOwner: true, - IsPublic: true, - NumTeams: 1}, *orgUsers[0]) - assert.Equal(t, OrgUser{ - ID: orgUsers[1].ID, - OrgID: 3, - UID: 4, - IsOwner: false, - IsPublic: false, - NumTeams: 0}, *orgUsers[1]) + if assert.Len(t, orgUsers, 2) { + assert.Equal(t, OrgUser{ + ID: orgUsers[0].ID, + OrgID: 3, + UID: 2, + IsOwner: true, + IsPublic: true, + NumTeams: 1}, *orgUsers[0]) + assert.Equal(t, OrgUser{ + ID: orgUsers[1].ID, + OrgID: 3, + UID: 4, + IsOwner: false, + IsPublic: false, + NumTeams: 0}, *orgUsers[1]) + } orgUsers, err = GetOrgUsersByOrgID(NonexistentID) assert.NoError(t, err) diff --git a/models/pull_test.go b/models/pull_test.go index 86c71f5658..c8eb033aa5 100644 --- a/models/pull_test.go +++ b/models/pull_test.go @@ -65,9 +65,10 @@ func TestPullRequestsNewest(t *testing.T) { }) assert.NoError(t, err) assert.Equal(t, int64(2), count) - assert.Len(t, prs, 2) - assert.Equal(t, int64(2), prs[0].ID) - assert.Equal(t, int64(1), prs[1].ID) + 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) { @@ -80,9 +81,10 @@ func TestPullRequestsOldest(t *testing.T) { }) assert.NoError(t, err) assert.Equal(t, int64(2), count) - assert.Len(t, prs, 2) - assert.Equal(t, int64(1), prs[0].ID) - assert.Equal(t, int64(2), prs[1].ID) + 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) { diff --git a/models/star_test.go b/models/star_test.go index 714f5df5cd..55b4a1cfac 100644 --- a/models/star_test.go +++ b/models/star_test.go @@ -35,8 +35,9 @@ 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) - assert.Equal(t, int64(2), gazers[0].ID) + if assert.Len(t, gazers, 1) { + assert.Equal(t, int64(2), gazers[0].ID) + } } func TestRepository_GetStargazers2(t *testing.T) { @@ -55,14 +56,16 @@ 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) - assert.Equal(t, int64(4), starred[0].ID) + 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) - assert.Equal(t, int64(2), starred[0].ID) - assert.Equal(t, int64(4), starred[1].ID) + 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) { diff --git a/models/status_test.go b/models/status_test.go index 5585fb1dcc..78d2370f9d 100644 --- a/models/status_test.go +++ b/models/status_test.go @@ -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) diff --git a/models/token_test.go b/models/token_test.go index 9674be126a..540bd40deb 100644 --- a/models/token_test.go +++ b/models/token_test.go @@ -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) - 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") + 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) - assert.Equal(t, int64(2), tokens[0].UID) - assert.Equal(t, "Token A", tokens[0].Name) + 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) diff --git a/models/update_test.go b/models/update_test.go index ae95a9641b..45451ee437 100644 --- a/models/update_test.go +++ b/models/update_test.go @@ -70,17 +70,17 @@ 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) + assert.Equal(t, now, pushCommits.Commits[0].Timestamp) - 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) - assert.Equal(t, now, pushCommits.Commits[0].Timestamp) - - assert.Equal(t, "Message2", pushCommits.Commits[1].Message) - assert.Equal(t, hexString2, pushCommits.Commits[1].Sha1) - assert.Equal(t, "example@example.com", pushCommits.Commits[1].AuthorEmail) - assert.Equal(t, now, pushCommits.Commits[1].Timestamp) + assert.Equal(t, "Message2", pushCommits.Commits[1].Message) + assert.Equal(t, hexString2, pushCommits.Commits[1].Sha1) + assert.Equal(t, "example@example.com", pushCommits.Commits[1].AuthorEmail) + assert.Equal(t, now, pushCommits.Commits[1].Timestamp) + } } // TODO TestPushUpdate diff --git a/models/user_mail_test.go b/models/user_mail_test.go index 4c0b6804d6..3352194e1f 100644 --- a/models/user_mail_test.go +++ b/models/user_mail_test.go @@ -14,15 +14,17 @@ func TestGetEmailAddresses(t *testing.T) { assert.NoError(t, PrepareTestDatabase()) emails, _ := GetEmailAddresses(int64(1)) - assert.Len(t, emails, 3) - assert.False(t, emails[0].IsPrimary) - assert.True(t, emails[2].IsActivated) - assert.True(t, emails[2].IsPrimary) + 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) - assert.True(t, emails[0].IsPrimary) - assert.True(t, emails[0].IsActivated) + if assert.Len(t, emails, 2) { + assert.True(t, emails[0].IsPrimary) + assert.True(t, emails[0].IsActivated) + } } func TestIsEmailUsed(t *testing.T) { diff --git a/models/user_openid_test.go b/models/user_openid_test.go index e4734cc428..711f92b9ff 100644 --- a/models/user_openid_test.go +++ b/models/user_openid_test.go @@ -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) - assert.False(t, oids[0].Show) + 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) - assert.True(t, oids[0].Show) + if assert.Len(t, oids, 1) { + assert.True(t, oids[0].Show) + } } diff --git a/models/webhook_test.go b/models/webhook_test.go index a637a16999..eeae7efbcb 100644 --- a/models/webhook_test.go +++ b/models/webhook_test.go @@ -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) - assert.Equal(t, int64(1), tasks[0].ID) + 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) - assert.Equal(t, int64(1), hooks[0].ID) - assert.True(t, hooks[0].IsActive) + 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) - assert.Equal(t, int64(1), hooks[0].ID) - assert.Equal(t, int64(2), hooks[1].ID) + 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) - assert.Equal(t, int64(3), hooks[0].ID) - assert.True(t, hooks[0].IsActive) + 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) - assert.Equal(t, int64(3), hooks[0].ID) - assert.True(t, hooks[0].IsActive) + 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) - assert.Equal(t, int64(1), hookTasks[0].ID) + if assert.Len(t, hookTasks, 1) { + assert.Equal(t, int64(1), hookTasks[0].ID) + } hookTasks, err = HookTasks(NonexistentID, 1) assert.NoError(t, err)