diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2021-11-16 16:53:21 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-16 16:53:21 +0800 |
commit | 81926d61db3dac223a75ea49eab893b25a089587 (patch) | |
tree | 627d2f19a008089f3a688e9a94a2cc8d2017afe2 /models | |
parent | 23bd7b1211a80aa3b0dcb60ec4a1c0089ff28dd4 (diff) | |
download | gitea-81926d61db3dac223a75ea49eab893b25a089587.tar.gz gitea-81926d61db3dac223a75ea49eab893b25a089587.zip |
Decouple unit test, remove intermediate `unittestbridge` package (#17662)
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'models')
50 files changed, 1057 insertions, 1044 deletions
diff --git a/models/access_test.go b/models/access_test.go index af799ebcde..96aa34edb6 100644 --- a/models/access_test.go +++ b/models/access_test.go @@ -15,21 +15,21 @@ import ( func TestAccessLevel(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - user5 := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) - user29 := db.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User) + user2 := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user5 := unittest.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) + user29 := unittest.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User) // A public repository owned by User 2 - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) assert.False(t, repo1.IsPrivate) // A private repository owned by Org 3 - repo3 := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) + repo3 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) assert.True(t, repo3.IsPrivate) // Another public repository - repo4 := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) + repo4 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) assert.False(t, repo4.IsPrivate) // org. owned private repo - repo24 := db.AssertExistsAndLoadBean(t, &Repository{ID: 24}).(*Repository) + repo24 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 24}).(*Repository) level, err := AccessLevel(user2, repo1) assert.NoError(t, err) @@ -66,13 +66,13 @@ func TestAccessLevel(t *testing.T) { func TestHasAccess(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user1 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - user2 := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) + user1 := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user2 := unittest.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) // A public repository owned by User 2 - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) assert.False(t, repo1.IsPrivate) // A private repository owned by Org 3 - repo2 := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) + repo2 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) assert.True(t, repo2.IsPrivate) has, err := HasAccess(user1.ID, repo1) @@ -92,12 +92,12 @@ func TestHasAccess(t *testing.T) { func TestUser_GetRepositoryAccesses(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user1 := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) accesses, err := user1.GetRepositoryAccesses() assert.NoError(t, err) assert.Len(t, accesses, 0) - user29 := db.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User) + user29 := unittest.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User) accesses, err = user29.GetRepositoryAccesses() assert.NoError(t, err) assert.Len(t, accesses, 2) @@ -106,17 +106,17 @@ func TestUser_GetRepositoryAccesses(t *testing.T) { func TestUser_GetAccessibleRepositories(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user1 := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) repos, err := user1.GetAccessibleRepositories(0) assert.NoError(t, err) assert.Len(t, repos, 0) - user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user2 := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) repos, err = user2.GetAccessibleRepositories(0) assert.NoError(t, err) assert.Len(t, repos, 4) - user29 := db.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User) + user29 := unittest.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User) repos, err = user29.GetAccessibleRepositories(0) assert.NoError(t, err) assert.Len(t, repos, 2) @@ -125,7 +125,7 @@ func TestUser_GetAccessibleRepositories(t *testing.T) { func TestRepository_RecalculateAccesses(t *testing.T) { // test with organization repo assert.NoError(t, unittest.PrepareTestDatabase()) - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) assert.NoError(t, repo1.GetOwner()) _, err := db.GetEngine(db.DefaultContext).Delete(&Collaboration{UserID: 2, RepoID: 3}) @@ -142,7 +142,7 @@ func TestRepository_RecalculateAccesses(t *testing.T) { func TestRepository_RecalculateAccesses2(t *testing.T) { // test with non-organization repo assert.NoError(t, unittest.PrepareTestDatabase()) - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) assert.NoError(t, repo1.GetOwner()) _, err := db.GetEngine(db.DefaultContext).Delete(&Collaboration{UserID: 4, RepoID: 4}) @@ -156,8 +156,8 @@ func TestRepository_RecalculateAccesses2(t *testing.T) { func TestRepository_RecalculateAccesses3(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - team5 := db.AssertExistsAndLoadBean(t, &Team{ID: 5}).(*Team) - user29 := db.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User) + team5 := unittest.AssertExistsAndLoadBean(t, &Team{ID: 5}).(*Team) + user29 := unittest.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User) has, err := db.GetEngine(db.DefaultContext).Get(&Access{UserID: 29, RepoID: 23}) assert.NoError(t, err) diff --git a/models/action_test.go b/models/action_test.go index 85b8290cb2..87bbcf3fb7 100644 --- a/models/action_test.go +++ b/models/action_test.go @@ -8,7 +8,6 @@ import ( "path" "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" @@ -17,16 +16,16 @@ import ( func TestAction_GetRepoPath(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{}).(*Repository) - owner := db.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{}).(*Repository) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User) action := &Action{RepoID: repo.ID} assert.Equal(t, path.Join(owner.Name, repo.Name), action.GetRepoPath()) } func TestAction_GetRepoLink(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{}).(*Repository) - owner := db.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{}).(*Repository) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User) action := &Action{RepoID: repo.ID} setting.AppSubURL = "/suburl" expected := path.Join(setting.AppSubURL, owner.Name, repo.Name) @@ -36,7 +35,7 @@ func TestAction_GetRepoLink(t *testing.T) { func TestGetFeeds(t *testing.T) { // test with an individual user assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) actions, err := GetFeeds(GetFeedsOptions{ RequestedUser: user, @@ -64,8 +63,8 @@ func TestGetFeeds(t *testing.T) { func TestGetFeeds2(t *testing.T) { // test with an organization user assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) actions, err := GetFeeds(GetFeedsOptions{ RequestedUser: org, diff --git a/models/admin_test.go b/models/admin_test.go index ea060f9726..95415731b2 100644 --- a/models/admin_test.go +++ b/models/admin_test.go @@ -7,7 +7,6 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -27,9 +26,9 @@ func TestCreateNotice(t *testing.T) { Type: NoticeRepository, Description: "test description", } - db.AssertNotExistsBean(t, noticeBean) + unittest.AssertNotExistsBean(t, noticeBean) assert.NoError(t, CreateNotice(noticeBean.Type, noticeBean.Description)) - db.AssertExistsAndLoadBean(t, noticeBean) + unittest.AssertExistsAndLoadBean(t, noticeBean) } func TestCreateRepositoryNotice(t *testing.T) { @@ -39,9 +38,9 @@ func TestCreateRepositoryNotice(t *testing.T) { Type: NoticeRepository, Description: "test description", } - db.AssertNotExistsBean(t, noticeBean) + unittest.AssertNotExistsBean(t, noticeBean) assert.NoError(t, CreateRepositoryNotice(noticeBean.Description)) - db.AssertExistsAndLoadBean(t, noticeBean) + unittest.AssertExistsAndLoadBean(t, noticeBean) } // TODO TestRemoveAllWithNotice @@ -71,45 +70,45 @@ func TestNotices(t *testing.T) { func TestDeleteNotice(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - db.AssertExistsAndLoadBean(t, &Notice{ID: 3}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 3}) assert.NoError(t, DeleteNotice(3)) - db.AssertNotExistsBean(t, &Notice{ID: 3}) + unittest.AssertNotExistsBean(t, &Notice{ID: 3}) } func TestDeleteNotices(t *testing.T) { // delete a non-empty range assert.NoError(t, unittest.PrepareTestDatabase()) - db.AssertExistsAndLoadBean(t, &Notice{ID: 1}) - db.AssertExistsAndLoadBean(t, &Notice{ID: 2}) - db.AssertExistsAndLoadBean(t, &Notice{ID: 3}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 1}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 2}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 3}) assert.NoError(t, DeleteNotices(1, 2)) - db.AssertNotExistsBean(t, &Notice{ID: 1}) - db.AssertNotExistsBean(t, &Notice{ID: 2}) - db.AssertExistsAndLoadBean(t, &Notice{ID: 3}) + unittest.AssertNotExistsBean(t, &Notice{ID: 1}) + unittest.AssertNotExistsBean(t, &Notice{ID: 2}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 3}) } func TestDeleteNotices2(t *testing.T) { // delete an empty range assert.NoError(t, unittest.PrepareTestDatabase()) - db.AssertExistsAndLoadBean(t, &Notice{ID: 1}) - db.AssertExistsAndLoadBean(t, &Notice{ID: 2}) - db.AssertExistsAndLoadBean(t, &Notice{ID: 3}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 1}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 2}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 3}) assert.NoError(t, DeleteNotices(3, 2)) - db.AssertExistsAndLoadBean(t, &Notice{ID: 1}) - db.AssertExistsAndLoadBean(t, &Notice{ID: 2}) - db.AssertExistsAndLoadBean(t, &Notice{ID: 3}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 1}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 2}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 3}) } func TestDeleteNoticesByIDs(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - db.AssertExistsAndLoadBean(t, &Notice{ID: 1}) - db.AssertExistsAndLoadBean(t, &Notice{ID: 2}) - db.AssertExistsAndLoadBean(t, &Notice{ID: 3}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 1}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 2}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 3}) assert.NoError(t, DeleteNoticesByIDs([]int64{1, 3})) - db.AssertNotExistsBean(t, &Notice{ID: 1}) - db.AssertExistsAndLoadBean(t, &Notice{ID: 2}) - db.AssertNotExistsBean(t, &Notice{ID: 3}) + unittest.AssertNotExistsBean(t, &Notice{ID: 1}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 2}) + unittest.AssertNotExistsBean(t, &Notice{ID: 3}) } diff --git a/models/attachment_test.go b/models/attachment_test.go index bc8114fa67..c394990536 100644 --- a/models/attachment_test.go +++ b/models/attachment_test.go @@ -88,7 +88,7 @@ func TestUpdateAttachment(t *testing.T) { attach.Name = "new_name" assert.NoError(t, UpdateAttachment(attach)) - db.AssertExistsAndLoadBean(t, &Attachment{Name: "new_name"}) + unittest.AssertExistsAndLoadBean(t, &Attachment{Name: "new_name"}) } func TestGetAttachmentsByUUIDs(t *testing.T) { diff --git a/models/branches_test.go b/models/branches_test.go index d7233fba65..787dd7fe83 100644 --- a/models/branches_test.go +++ b/models/branches_test.go @@ -7,15 +7,14 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestAddDeletedBranch(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) - firstBranch := db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + firstBranch := unittest.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) assert.Error(t, repo.AddDeletedBranch(firstBranch.Name, firstBranch.Commit, firstBranch.DeletedByID)) assert.NoError(t, repo.AddDeletedBranch("test", "5655464564554545466464656", int64(1))) @@ -23,7 +22,7 @@ func TestAddDeletedBranch(t *testing.T) { func TestGetDeletedBranches(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) branches, err := repo.GetDeletedBranches() assert.NoError(t, err) @@ -32,7 +31,7 @@ func TestGetDeletedBranches(t *testing.T) { func TestGetDeletedBranch(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - firstBranch := db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) + firstBranch := unittest.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) assert.NotNil(t, getDeletedBranch(t, firstBranch)) } @@ -40,8 +39,8 @@ func TestGetDeletedBranch(t *testing.T) { func TestDeletedBranchLoadUser(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - firstBranch := db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) - secondBranch := db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 2}).(*DeletedBranch) + firstBranch := unittest.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) + secondBranch := unittest.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 2}).(*DeletedBranch) branch := getDeletedBranch(t, firstBranch) assert.Nil(t, branch.DeletedBy) @@ -58,18 +57,18 @@ func TestDeletedBranchLoadUser(t *testing.T) { func TestRemoveDeletedBranch(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) - firstBranch := db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) + firstBranch := unittest.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) err := repo.RemoveDeletedBranch(1) assert.NoError(t, err) - db.AssertNotExistsBean(t, firstBranch) - db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 2}) + unittest.AssertNotExistsBean(t, firstBranch) + unittest.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 2}) } func getDeletedBranch(t *testing.T, branch *DeletedBranch) *DeletedBranch { - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) deletedBranch, err := repo.GetDeletedBranchByID(branch.ID) assert.NoError(t, err) @@ -95,7 +94,7 @@ func TestFindRenamedBranch(t *testing.T) { func TestRenameBranch(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) _isDefault := false err := UpdateProtectBranch(repo1, &ProtectedBranch{ @@ -110,21 +109,21 @@ func TestRenameBranch(t *testing.T) { })) assert.Equal(t, true, _isDefault) - repo1 = db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo1 = unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) assert.Equal(t, "main", repo1.DefaultBranch) - pull := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) // merged + pull := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) // merged assert.Equal(t, "master", pull.BaseBranch) - pull = db.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) // open + pull = unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) // open assert.Equal(t, "main", pull.BaseBranch) - renamedBranch := db.AssertExistsAndLoadBean(t, &RenamedBranch{ID: 2}).(*RenamedBranch) + renamedBranch := unittest.AssertExistsAndLoadBean(t, &RenamedBranch{ID: 2}).(*RenamedBranch) assert.Equal(t, "master", renamedBranch.From) assert.Equal(t, "main", renamedBranch.To) assert.Equal(t, int64(1), renamedBranch.RepoID) - db.AssertExistsAndLoadBean(t, &ProtectedBranch{ + unittest.AssertExistsAndLoadBean(t, &ProtectedBranch{ RepoID: repo1.ID, BranchName: "main", }) @@ -136,7 +135,7 @@ func TestOnlyGetDeletedBranchOnCorrectRepo(t *testing.T) { // Get deletedBranch with ID of 1 on repo with ID 2. // This should return a nil branch as this deleted branch // is actually on repo with ID 1. - repo2 := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) + repo2 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) deletedBranch, err := repo2.GetDeletedBranchByID(1) @@ -146,7 +145,7 @@ func TestOnlyGetDeletedBranchOnCorrectRepo(t *testing.T) { // Now get the deletedBranch with ID of 1 on repo with ID 1. // This should return the deletedBranch. - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) deletedBranch, err = repo1.GetDeletedBranchByID(1) diff --git a/models/commit_status_test.go b/models/commit_status_test.go index b113346a41..32d6a433ce 100644 --- a/models/commit_status_test.go +++ b/models/commit_status_test.go @@ -16,7 +16,7 @@ import ( func TestGetCommitStatuses(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) sha1 := "1234123412341234123412341234123412341234" diff --git a/models/consistency.go b/models/consistency.go index 64c6b240a2..d7bf8ade57 100644 --- a/models/consistency.go +++ b/models/consistency.go @@ -5,177 +5,11 @@ package models import ( - "reflect" - "strings" - "code.gitea.io/gitea/models/db" - "code.gitea.io/gitea/modules/unittestbridge" "xorm.io/builder" ) -// CheckConsistencyFor test that all matching database entries are consistent -func CheckConsistencyFor(t unittestbridge.Tester, beansToCheck ...interface{}) { - ta := unittestbridge.NewAsserter(t) - for _, bean := range beansToCheck { - sliceType := reflect.SliceOf(reflect.TypeOf(bean)) - sliceValue := reflect.MakeSlice(sliceType, 0, 10) - - ptrToSliceValue := reflect.New(sliceType) - ptrToSliceValue.Elem().Set(sliceValue) - - ta.NoError(db.GetEngine(db.DefaultContext).Table(bean).Find(ptrToSliceValue.Interface())) - sliceValue = ptrToSliceValue.Elem() - - for i := 0; i < sliceValue.Len(); i++ { - entity := sliceValue.Index(i).Interface() - checkForConsistency(ta, entity) - } - } -} - -func checkForConsistency(ta unittestbridge.Asserter, bean interface{}) { - switch b := bean.(type) { - case *User: - checkForUserConsistency(b, ta) - case *Repository: - checkForRepoConsistency(b, ta) - case *Issue: - checkForIssueConsistency(b, ta) - case *PullRequest: - checkForPullRequestConsistency(b, ta) - case *Milestone: - checkForMilestoneConsistency(b, ta) - case *Label: - checkForLabelConsistency(b, ta) - case *Team: - checkForTeamConsistency(b, ta) - case *Action: - checkForActionConsistency(b, ta) - default: - ta.Errorf("unknown bean type: %#v", bean) - } -} - -// getCount get the count of database entries matching bean -func getCount(ta unittestbridge.Asserter, e db.Engine, bean interface{}) int64 { - count, err := e.Count(bean) - ta.NoError(err) - return count -} - -// assertCount test the count of database entries matching bean -func assertCount(ta unittestbridge.Asserter, bean interface{}, expected int) { - ta.EqualValues(expected, getCount(ta, db.GetEngine(db.DefaultContext), bean), - "Failed consistency test, the counted bean (of type %T) was %+v", bean, bean) -} - -func checkForUserConsistency(user *User, ta unittestbridge.Asserter) { - assertCount(ta, &Repository{OwnerID: user.ID}, user.NumRepos) - assertCount(ta, &Star{UID: user.ID}, user.NumStars) - assertCount(ta, &OrgUser{OrgID: user.ID}, user.NumMembers) - assertCount(ta, &Team{OrgID: user.ID}, user.NumTeams) - assertCount(ta, &Follow{UserID: user.ID}, user.NumFollowing) - assertCount(ta, &Follow{FollowID: user.ID}, user.NumFollowers) - if user.Type != UserTypeOrganization { - ta.EqualValues(0, user.NumMembers) - ta.EqualValues(0, user.NumTeams) - } -} - -func checkForRepoConsistency(repo *Repository, ta unittestbridge.Asserter) { - ta.Equal(repo.LowerName, strings.ToLower(repo.Name), "repo: %+v", repo) - assertCount(ta, &Star{RepoID: repo.ID}, repo.NumStars) - assertCount(ta, &Milestone{RepoID: repo.ID}, repo.NumMilestones) - assertCount(ta, &Repository{ForkID: repo.ID}, repo.NumForks) - if repo.IsFork { - db.AssertExistsAndLoadBean(ta, &Repository{ID: repo.ForkID}) - } - - actual := getCount(ta, db.GetEngine(db.DefaultContext).Where("Mode<>?", RepoWatchModeDont), &Watch{RepoID: repo.ID}) - ta.EqualValues(repo.NumWatches, actual, - "Unexpected number of watches for repo %+v", repo) - - actual = getCount(ta, db.GetEngine(db.DefaultContext).Where("is_pull=?", false), &Issue{RepoID: repo.ID}) - ta.EqualValues(repo.NumIssues, actual, - "Unexpected number of issues for repo %+v", repo) - - actual = getCount(ta, db.GetEngine(db.DefaultContext).Where("is_pull=? AND is_closed=?", false, true), &Issue{RepoID: repo.ID}) - ta.EqualValues(repo.NumClosedIssues, actual, - "Unexpected number of closed issues for repo %+v", repo) - - actual = getCount(ta, db.GetEngine(db.DefaultContext).Where("is_pull=?", true), &Issue{RepoID: repo.ID}) - ta.EqualValues(repo.NumPulls, actual, - "Unexpected number of pulls for repo %+v", repo) - - actual = getCount(ta, db.GetEngine(db.DefaultContext).Where("is_pull=? AND is_closed=?", true, true), &Issue{RepoID: repo.ID}) - ta.EqualValues(repo.NumClosedPulls, actual, - "Unexpected number of closed pulls for repo %+v", repo) - - actual = getCount(ta, db.GetEngine(db.DefaultContext).Where("is_closed=?", true), &Milestone{RepoID: repo.ID}) - ta.EqualValues(repo.NumClosedMilestones, actual, - "Unexpected number of closed milestones for repo %+v", repo) -} - -func checkForIssueConsistency(issue *Issue, ta unittestbridge.Asserter) { - actual := getCount(ta, db.GetEngine(db.DefaultContext).Where("type=?", CommentTypeComment), &Comment{IssueID: issue.ID}) - ta.EqualValues(issue.NumComments, actual, - "Unexpected number of comments for issue %+v", issue) - if issue.IsPull { - pr := db.AssertExistsAndLoadBean(ta, &PullRequest{IssueID: issue.ID}).(*PullRequest) - ta.EqualValues(pr.Index, issue.Index) - } -} - -func checkForPullRequestConsistency(pr *PullRequest, ta unittestbridge.Asserter) { - issue := db.AssertExistsAndLoadBean(ta, &Issue{ID: pr.IssueID}).(*Issue) - ta.True(issue.IsPull) - ta.EqualValues(issue.Index, pr.Index) -} - -func checkForMilestoneConsistency(milestone *Milestone, ta unittestbridge.Asserter) { - assertCount(ta, &Issue{MilestoneID: milestone.ID}, milestone.NumIssues) - - actual := getCount(ta, db.GetEngine(db.DefaultContext).Where("is_closed=?", true), &Issue{MilestoneID: milestone.ID}) - ta.EqualValues(milestone.NumClosedIssues, actual, - "Unexpected number of closed issues for milestone %+v", milestone) - - completeness := 0 - if milestone.NumIssues > 0 { - completeness = milestone.NumClosedIssues * 100 / milestone.NumIssues - } - ta.Equal(completeness, milestone.Completeness) -} - -func checkForLabelConsistency(label *Label, ta unittestbridge.Asserter) { - issueLabels := make([]*IssueLabel, 0, 10) - ta.NoError(db.GetEngine(db.DefaultContext).Find(&issueLabels, &IssueLabel{LabelID: label.ID})) - ta.EqualValues(label.NumIssues, len(issueLabels), - "Unexpected number of issue for label %+v", label) - - issueIDs := make([]int64, len(issueLabels)) - for i, issueLabel := range issueLabels { - issueIDs[i] = issueLabel.IssueID - } - - expected := int64(0) - if len(issueIDs) > 0 { - expected = getCount(ta, db.GetEngine(db.DefaultContext).In("id", issueIDs).Where("is_closed=?", true), &Issue{}) - } - ta.EqualValues(expected, label.NumClosedIssues, - "Unexpected number of closed issues for label %+v", label) -} - -func checkForTeamConsistency(team *Team, ta unittestbridge.Asserter) { - assertCount(ta, &TeamUser{TeamID: team.ID}, team.NumMembers) - assertCount(ta, &TeamRepo{TeamID: team.ID}, team.NumRepos) -} - -func checkForActionConsistency(action *Action, ta unittestbridge.Asserter) { - repo := db.AssertExistsAndLoadBean(ta, &Repository{ID: action.RepoID}).(*Repository) - ta.Equal(repo.IsPrivate, action.IsPrivate, "action: %+v", action) -} - // CountOrphanedLabels return count of labels witch are broken and not accessible via ui anymore func CountOrphanedLabels() (int64, error) { noref, err := db.GetEngine(db.DefaultContext).Table("label").Where("repo_id=? AND org_id=?", 0, 0).Count("label.id") diff --git a/models/db/engine.go b/models/db/engine.go index d1b279e016..e392008020 100755 --- a/models/db/engine.go +++ b/models/db/engine.go @@ -153,6 +153,15 @@ func InitEngine(ctx context.Context) (err error) { return nil } +// SetEngine is used by unit test code +func SetEngine(eng *xorm.Engine) { + x = eng + DefaultContext = &Context{ + Context: context.Background(), + e: x, + } +} + // InitEngineWithMigration initializes a new xorm.Engine // This function must never call .Sync2() if the provided migration function fails. // When called from the "doctor" command, the migration function is a version check diff --git a/models/db/unit_tests.go b/models/db/unit_tests.go deleted file mode 100644 index 2b1691726f..0000000000 --- a/models/db/unit_tests.go +++ /dev/null @@ -1,125 +0,0 @@ -// Copyright 2016 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package db - -import ( - "context" - "math" - - "code.gitea.io/gitea/modules/unittestbridge" - - "xorm.io/xorm" -) - -// Code in this file is mainly used by models.CheckConsistencyFor, which is not in the unit test for various reasons. -// In the future if we can decouple CheckConsistencyFor into separate unit test code, then this file can be moved into unittest package too. - -// NonexistentID an ID that will never exist -const NonexistentID = int64(math.MaxInt64) - -//SetUnitTestEngine is used by unit test code -func SetUnitTestEngine(eng *xorm.Engine) { - x = eng - DefaultContext = &Context{ - Context: context.Background(), - e: x, - } -} - -type testCond struct { - query interface{} - args []interface{} -} - -// Cond create a condition with arguments for a test -func Cond(query interface{}, args ...interface{}) interface{} { - return &testCond{query: query, args: args} -} - -func whereConditions(sess *xorm.Session, conditions []interface{}) { - for _, condition := range conditions { - switch cond := condition.(type) { - case *testCond: - sess.Where(cond.query, cond.args...) - default: - sess.Where(cond) - } - } -} - -// LoadBeanIfExists loads beans from fixture database if exist -func LoadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) { - sess := x.NewSession() - defer sess.Close() - whereConditions(sess, conditions) - return sess.Get(bean) -} - -// BeanExists for testing, check if a bean exists -func BeanExists(t unittestbridge.Tester, bean interface{}, conditions ...interface{}) bool { - ta := unittestbridge.NewAsserter(t) - exists, err := LoadBeanIfExists(bean, conditions...) - ta.NoError(err) - return exists -} - -// AssertExistsAndLoadBean assert that a bean exists and load it from the test database -func AssertExistsAndLoadBean(t unittestbridge.Tester, bean interface{}, conditions ...interface{}) interface{} { - ta := unittestbridge.NewAsserter(t) - exists, err := LoadBeanIfExists(bean, conditions...) - ta.NoError(err) - ta.True(exists, - "Expected to find %+v (of type %T, with conditions %+v), but did not", - bean, bean, conditions) - return bean -} - -// GetCount get the count of a bean -func GetCount(t unittestbridge.Tester, bean interface{}, conditions ...interface{}) int { - ta := unittestbridge.NewAsserter(t) - sess := x.NewSession() - defer sess.Close() - whereConditions(sess, conditions) - count, err := sess.Count(bean) - ta.NoError(err) - return int(count) -} - -// AssertNotExistsBean assert that a bean does not exist in the test database -func AssertNotExistsBean(t unittestbridge.Tester, bean interface{}, conditions ...interface{}) { - ta := unittestbridge.NewAsserter(t) - exists, err := LoadBeanIfExists(bean, conditions...) - ta.NoError(err) - ta.False(exists) -} - -// AssertExistsIf asserts that a bean exists or does not exist, depending on -// what is expected. -func AssertExistsIf(t unittestbridge.Tester, expected bool, bean interface{}, conditions ...interface{}) { - ta := unittestbridge.NewAsserter(t) - exists, err := LoadBeanIfExists(bean, conditions...) - ta.NoError(err) - ta.Equal(expected, exists) -} - -// AssertSuccessfulInsert assert that beans is successfully inserted -func AssertSuccessfulInsert(t unittestbridge.Tester, beans ...interface{}) { - ta := unittestbridge.NewAsserter(t) - _, err := x.Insert(beans...) - ta.NoError(err) -} - -// AssertCount assert the count of a bean -func AssertCount(t unittestbridge.Tester, bean, expected interface{}) { - ta := unittestbridge.NewAsserter(t) - ta.EqualValues(expected, GetCount(ta, bean)) -} - -// AssertInt64InRange assert value is in range [low, high] -func AssertInt64InRange(t unittestbridge.Tester, low, high, value int64) { - ta := unittestbridge.NewAsserter(t) - ta.True(value >= low && value <= high, - "Expected value in range [%d, %d], found %d", low, high, value) -} diff --git a/models/gpg_key_test.go b/models/gpg_key_test.go index 3df25a10bb..e5f4960b76 100644 --- a/models/gpg_key_test.go +++ b/models/gpg_key_test.go @@ -8,7 +8,6 @@ import ( "testing" "time" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/timeutil" @@ -196,7 +195,7 @@ Unknown GPG key with good email func TestCheckGPGUserEmail(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - _ = db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + _ = unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) testEmailWithUpperCaseLetters := `-----BEGIN PGP PUBLIC KEY BLOCK----- Version: GnuPG v1 diff --git a/models/issue_assignees_test.go b/models/issue_assignees_test.go index f0bd5fca4b..8e2256072a 100644 --- a/models/issue_assignees_test.go +++ b/models/issue_assignees_test.go @@ -7,7 +7,6 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -65,8 +64,8 @@ func TestUpdateAssignee(t *testing.T) { func TestMakeIDsFromAPIAssigneesToAdd(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - _ = db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) - _ = db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + _ = unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + _ = unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) IDs, err := MakeIDsFromAPIAssigneesToAdd("", []string{""}) assert.NoError(t, err) diff --git a/models/issue_comment_test.go b/models/issue_comment_test.go index d855b87bc4..27c50a3ae4 100644 --- a/models/issue_comment_test.go +++ b/models/issue_comment_test.go @@ -8,7 +8,6 @@ import ( "testing" "time" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -16,9 +15,9 @@ import ( func TestCreateComment(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{}).(*Issue) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: issue.RepoID}).(*Repository) - doer := db.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{}).(*Issue) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: issue.RepoID}).(*Repository) + doer := unittest.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User) now := time.Now().Unix() comment, err := CreateComment(&CreateCommentOptions{ @@ -35,18 +34,18 @@ func TestCreateComment(t *testing.T) { assert.EqualValues(t, "Hello", comment.Content) assert.EqualValues(t, issue.ID, comment.IssueID) assert.EqualValues(t, doer.ID, comment.PosterID) - db.AssertInt64InRange(t, now, then, int64(comment.CreatedUnix)) - db.AssertExistsAndLoadBean(t, comment) // assert actually added to DB + unittest.AssertInt64InRange(t, now, then, int64(comment.CreatedUnix)) + unittest.AssertExistsAndLoadBean(t, comment) // assert actually added to DB - updatedIssue := db.AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue) - db.AssertInt64InRange(t, now, then, int64(updatedIssue.UpdatedUnix)) + updatedIssue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue) + unittest.AssertInt64InRange(t, now, then, int64(updatedIssue.UpdatedUnix)) } func TestFetchCodeComments(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue) - user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) res, err := FetchCodeComments(issue, user) assert.NoError(t, err) assert.Contains(t, res, "README.md") @@ -54,7 +53,7 @@ func TestFetchCodeComments(t *testing.T) { assert.Len(t, res["README.md"][4], 1) assert.Equal(t, int64(4), res["README.md"][4][0].ID) - user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user2 := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) res, err = FetchCodeComments(issue, user2) assert.NoError(t, err) assert.Len(t, res, 1) diff --git a/models/issue_dependency_test.go b/models/issue_dependency_test.go index 60fa587389..c357d35c50 100644 --- a/models/issue_dependency_test.go +++ b/models/issue_dependency_test.go @@ -7,7 +7,6 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -39,7 +38,7 @@ func TestCreateIssueDependency(t *testing.T) { assert.Error(t, err) assert.True(t, IsErrCircularDependency(err)) - _ = db.AssertExistsAndLoadBean(t, &Comment{Type: CommentTypeAddDependency, PosterID: user1.ID, IssueID: issue1.ID}) + _ = unittest.AssertExistsAndLoadBean(t, &Comment{Type: CommentTypeAddDependency, PosterID: user1.ID, IssueID: issue1.ID}) // Check if dependencies left is correct left, err := IssueNoDependenciesLeft(issue1) diff --git a/models/issue_label_test.go b/models/issue_label_test.go index c77ddfefaf..658c459ba5 100644 --- a/models/issue_label_test.go +++ b/models/issue_label_test.go @@ -17,17 +17,17 @@ import ( func TestLabel_CalOpenIssues(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - label := db.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) + label := unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) label.CalOpenIssues() assert.EqualValues(t, 2, label.NumOpenIssues) } func TestLabel_ForegroundColor(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - label := db.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) + label := unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) assert.Equal(t, template.CSS("#000"), label.ForegroundColor()) - label = db.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) + label = unittest.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) assert.Equal(t, template.CSS("#fff"), label.ForegroundColor()) } @@ -41,13 +41,13 @@ func TestNewLabels(t *testing.T) { assert.Error(t, NewLabel(&Label{RepoID: 3, Name: "invalid Color", Color: "123456"})) assert.Error(t, NewLabel(&Label{RepoID: 3, Name: "invalid Color", Color: "#12345G"})) for _, label := range labels { - db.AssertNotExistsBean(t, label) + unittest.AssertNotExistsBean(t, label) } assert.NoError(t, NewLabels(labels...)) for _, label := range labels { - db.AssertExistsAndLoadBean(t, label, db.Cond("id = ?", label.ID)) + unittest.AssertExistsAndLoadBean(t, label, unittest.Cond("id = ?", label.ID)) } - CheckConsistencyFor(t, &Label{}, &Repository{}) + unittest.CheckConsistencyFor(t, &Label{}, &Repository{}) } func TestGetLabelByID(t *testing.T) { @@ -56,7 +56,7 @@ func TestGetLabelByID(t *testing.T) { assert.NoError(t, err) assert.EqualValues(t, 1, label.ID) - _, err = GetLabelByID(db.NonexistentID) + _, err = GetLabelByID(unittest.NonexistentID) assert.True(t, IsErrLabelNotExist(err)) } @@ -70,7 +70,7 @@ func TestGetLabelInRepoByName(t *testing.T) { _, err = GetLabelInRepoByName(1, "") assert.True(t, IsErrRepoLabelNotExist(err)) - _, err = GetLabelInRepoByName(db.NonexistentID, "nonexistent") + _, err = GetLabelInRepoByName(unittest.NonexistentID, "nonexistent") assert.True(t, IsErrRepoLabelNotExist(err)) } @@ -107,13 +107,13 @@ func TestGetLabelInRepoByID(t *testing.T) { _, err = GetLabelInRepoByID(1, -1) assert.True(t, IsErrRepoLabelNotExist(err)) - _, err = GetLabelInRepoByID(db.NonexistentID, db.NonexistentID) + _, err = GetLabelInRepoByID(unittest.NonexistentID, unittest.NonexistentID) assert.True(t, IsErrRepoLabelNotExist(err)) } func TestGetLabelsInRepoByIDs(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - labels, err := GetLabelsInRepoByIDs(1, []int64{1, 2, db.NonexistentID}) + labels, err := GetLabelsInRepoByIDs(1, []int64{1, 2, unittest.NonexistentID}) assert.NoError(t, err) if assert.Len(t, labels, 2) { assert.EqualValues(t, 1, labels[0].ID) @@ -155,7 +155,7 @@ func TestGetLabelInOrgByName(t *testing.T) { _, err = GetLabelInOrgByName(-1, "orglabel3") assert.True(t, IsErrOrgLabelNotExist(err)) - _, err = GetLabelInOrgByName(db.NonexistentID, "nonexistent") + _, err = GetLabelInOrgByName(unittest.NonexistentID, "nonexistent") assert.True(t, IsErrOrgLabelNotExist(err)) } @@ -198,13 +198,13 @@ func TestGetLabelInOrgByID(t *testing.T) { _, err = GetLabelInOrgByID(-1, 3) assert.True(t, IsErrOrgLabelNotExist(err)) - _, err = GetLabelInOrgByID(db.NonexistentID, db.NonexistentID) + _, err = GetLabelInOrgByID(unittest.NonexistentID, unittest.NonexistentID) assert.True(t, IsErrOrgLabelNotExist(err)) } func TestGetLabelsInOrgByIDs(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - labels, err := GetLabelsInOrgByIDs(3, []int64{3, 4, db.NonexistentID}) + labels, err := GetLabelsInOrgByIDs(3, []int64{3, 4, unittest.NonexistentID}) assert.NoError(t, err) if assert.Len(t, labels, 2) { assert.EqualValues(t, 3, labels[0].ID) @@ -245,14 +245,14 @@ func TestGetLabelsByIssueID(t *testing.T) { assert.EqualValues(t, 1, labels[0].ID) } - labels, err = GetLabelsByIssueID(db.NonexistentID) + labels, err = GetLabelsByIssueID(unittest.NonexistentID) assert.NoError(t, err) assert.Len(t, labels, 0) } func TestUpdateLabel(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - label := db.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) + label := unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) // make sure update wont overwrite it update := &Label{ ID: label.ID, @@ -263,99 +263,99 @@ func TestUpdateLabel(t *testing.T) { label.Color = update.Color label.Name = update.Name assert.NoError(t, UpdateLabel(update)) - newLabel := db.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) + newLabel := unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) assert.EqualValues(t, label.ID, newLabel.ID) assert.EqualValues(t, label.Color, newLabel.Color) assert.EqualValues(t, label.Name, newLabel.Name) assert.EqualValues(t, label.Description, newLabel.Description) - CheckConsistencyFor(t, &Label{}, &Repository{}) + unittest.CheckConsistencyFor(t, &Label{}, &Repository{}) } func TestDeleteLabel(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - label := db.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) + label := unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) assert.NoError(t, DeleteLabel(label.RepoID, label.ID)) - db.AssertNotExistsBean(t, &Label{ID: label.ID, RepoID: label.RepoID}) + unittest.AssertNotExistsBean(t, &Label{ID: label.ID, RepoID: label.RepoID}) assert.NoError(t, DeleteLabel(label.RepoID, label.ID)) - db.AssertNotExistsBean(t, &Label{ID: label.ID}) + unittest.AssertNotExistsBean(t, &Label{ID: label.ID}) - assert.NoError(t, DeleteLabel(db.NonexistentID, db.NonexistentID)) - CheckConsistencyFor(t, &Label{}, &Repository{}) + assert.NoError(t, DeleteLabel(unittest.NonexistentID, unittest.NonexistentID)) + unittest.CheckConsistencyFor(t, &Label{}, &Repository{}) } func TestHasIssueLabel(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) assert.True(t, HasIssueLabel(1, 1)) assert.False(t, HasIssueLabel(1, 2)) - assert.False(t, HasIssueLabel(db.NonexistentID, db.NonexistentID)) + assert.False(t, HasIssueLabel(unittest.NonexistentID, unittest.NonexistentID)) } func TestNewIssueLabel(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - label := db.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) - doer := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + label := unittest.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) + doer := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) // add new IssueLabel prevNumIssues := label.NumIssues assert.NoError(t, NewIssueLabel(issue, label, doer)) - db.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label.ID}) - db.AssertExistsAndLoadBean(t, &Comment{ + unittest.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label.ID}) + unittest.AssertExistsAndLoadBean(t, &Comment{ Type: CommentTypeLabel, PosterID: doer.ID, IssueID: issue.ID, LabelID: label.ID, Content: "1", }) - label = db.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) + label = unittest.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) assert.EqualValues(t, prevNumIssues+1, label.NumIssues) // re-add existing IssueLabel assert.NoError(t, NewIssueLabel(issue, label, doer)) - CheckConsistencyFor(t, &Issue{}, &Label{}) + unittest.CheckConsistencyFor(t, &Issue{}, &Label{}) } func TestNewIssueLabels(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - label1 := db.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) - label2 := db.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 5}).(*Issue) - doer := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + label1 := unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) + label2 := unittest.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 5}).(*Issue) + doer := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) assert.NoError(t, NewIssueLabels(issue, []*Label{label1, label2}, doer)) - db.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label1.ID}) - db.AssertExistsAndLoadBean(t, &Comment{ + unittest.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label1.ID}) + unittest.AssertExistsAndLoadBean(t, &Comment{ Type: CommentTypeLabel, PosterID: doer.ID, IssueID: issue.ID, LabelID: label1.ID, Content: "1", }) - db.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label1.ID}) - label1 = db.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) + unittest.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label1.ID}) + label1 = unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) assert.EqualValues(t, 3, label1.NumIssues) assert.EqualValues(t, 1, label1.NumClosedIssues) - label2 = db.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) + label2 = unittest.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) assert.EqualValues(t, 1, label2.NumIssues) assert.EqualValues(t, 1, label2.NumClosedIssues) // corner case: test empty slice assert.NoError(t, NewIssueLabels(issue, []*Label{}, doer)) - CheckConsistencyFor(t, &Issue{}, &Label{}) + unittest.CheckConsistencyFor(t, &Issue{}, &Label{}) } func TestDeleteIssueLabel(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(labelID, issueID, doerID int64) { - label := db.AssertExistsAndLoadBean(t, &Label{ID: labelID}).(*Label) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: issueID}).(*Issue) - doer := db.AssertExistsAndLoadBean(t, &User{ID: doerID}).(*User) + label := unittest.AssertExistsAndLoadBean(t, &Label{ID: labelID}).(*Label) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: issueID}).(*Issue) + doer := unittest.AssertExistsAndLoadBean(t, &User{ID: doerID}).(*User) expectedNumIssues := label.NumIssues expectedNumClosedIssues := label.NumClosedIssues - if db.BeanExists(t, &IssueLabel{IssueID: issueID, LabelID: labelID}) { + if unittest.BeanExists(t, &IssueLabel{IssueID: issueID, LabelID: labelID}) { expectedNumIssues-- if issue.IsClosed { expectedNumClosedIssues-- @@ -363,14 +363,14 @@ func TestDeleteIssueLabel(t *testing.T) { } assert.NoError(t, DeleteIssueLabel(issue, label, doer)) - db.AssertNotExistsBean(t, &IssueLabel{IssueID: issueID, LabelID: labelID}) - db.AssertExistsAndLoadBean(t, &Comment{ + unittest.AssertNotExistsBean(t, &IssueLabel{IssueID: issueID, LabelID: labelID}) + unittest.AssertExistsAndLoadBean(t, &Comment{ Type: CommentTypeLabel, PosterID: doerID, IssueID: issueID, LabelID: labelID, }, `content=""`) - label = db.AssertExistsAndLoadBean(t, &Label{ID: labelID}).(*Label) + label = unittest.AssertExistsAndLoadBean(t, &Label{ID: labelID}).(*Label) assert.EqualValues(t, expectedNumIssues, label.NumIssues) assert.EqualValues(t, expectedNumClosedIssues, label.NumClosedIssues) } @@ -378,5 +378,5 @@ func TestDeleteIssueLabel(t *testing.T) { testSuccess(2, 5, 2) testSuccess(1, 1, 2) // delete non-existent IssueLabel - CheckConsistencyFor(t, &Issue{}, &Label{}) + unittest.CheckConsistencyFor(t, &Issue{}, &Label{}) } diff --git a/models/issue_list_test.go b/models/issue_list_test.go index ea2f42e77b..2916a7302f 100644 --- a/models/issue_list_test.go +++ b/models/issue_list_test.go @@ -7,7 +7,6 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" @@ -18,9 +17,9 @@ func TestIssueList_LoadRepositories(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) issueList := IssueList{ - db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue), - db.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue), - db.AssertExistsAndLoadBean(t, &Issue{ID: 4}).(*Issue), + unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue), + unittest.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue), + unittest.AssertExistsAndLoadBean(t, &Issue{ID: 4}).(*Issue), } repos, err := issueList.LoadRepositories() @@ -35,8 +34,8 @@ func TestIssueList_LoadAttributes(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) setting.Service.EnableTimetracking = true issueList := IssueList{ - db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue), - db.AssertExistsAndLoadBean(t, &Issue{ID: 4}).(*Issue), + unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue), + unittest.AssertExistsAndLoadBean(t, &Issue{ID: 4}).(*Issue), } assert.NoError(t, issueList.LoadAttributes()) @@ -44,7 +43,7 @@ func TestIssueList_LoadAttributes(t *testing.T) { assert.EqualValues(t, issue.RepoID, issue.Repo.ID) for _, label := range issue.Labels { assert.EqualValues(t, issue.RepoID, label.RepoID) - db.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label.ID}) + unittest.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label.ID}) } if issue.PosterID > 0 { assert.EqualValues(t, issue.PosterID, issue.Poster.ID) diff --git a/models/issue_milestone_test.go b/models/issue_milestone_test.go index 8dee464066..2956e40f06 100644 --- a/models/issue_milestone_test.go +++ b/models/issue_milestone_test.go @@ -32,8 +32,8 @@ func TestNewMilestone(t *testing.T) { } assert.NoError(t, NewMilestone(milestone)) - db.AssertExistsAndLoadBean(t, milestone) - CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{}) + unittest.AssertExistsAndLoadBean(t, milestone) + unittest.CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{}) } func TestGetMilestoneByRepoID(t *testing.T) { @@ -44,14 +44,14 @@ func TestGetMilestoneByRepoID(t *testing.T) { assert.EqualValues(t, 1, milestone.ID) assert.EqualValues(t, 1, milestone.RepoID) - _, err = GetMilestoneByRepoID(db.NonexistentID, db.NonexistentID) + _, err = GetMilestoneByRepoID(unittest.NonexistentID, unittest.NonexistentID) assert.True(t, IsErrMilestoneNotExist(err)) } func TestGetMilestonesByRepoID(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(repoID int64, state api.StateType) { - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) milestones, _, err := GetMilestones(GetMilestonesOption{ RepoID: repo.ID, State: state, @@ -90,7 +90,7 @@ func TestGetMilestonesByRepoID(t *testing.T) { test(3, api.StateAll) milestones, _, err := GetMilestones(GetMilestonesOption{ - RepoID: db.NonexistentID, + RepoID: unittest.NonexistentID, State: api.StateOpen, }) assert.NoError(t, err) @@ -99,7 +99,7 @@ func TestGetMilestonesByRepoID(t *testing.T) { func TestGetMilestones(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) test := func(sortType string, sortCond func(*Milestone) int) { for _, page := range []int{0, 1} { milestones, _, err := GetMilestones(GetMilestonesOption{ @@ -161,19 +161,19 @@ func TestGetMilestones(t *testing.T) { func TestUpdateMilestone(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - milestone := db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone) + milestone := unittest.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone) milestone.Name = " newMilestoneName " milestone.Content = "newMilestoneContent" assert.NoError(t, UpdateMilestone(milestone, milestone.IsClosed)) - milestone = db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone) + milestone = unittest.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone) assert.EqualValues(t, "newMilestoneName", milestone.Name) - CheckConsistencyFor(t, &Milestone{}) + unittest.CheckConsistencyFor(t, &Milestone{}) } func TestCountRepoMilestones(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(repoID int64) { - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) count, err := countRepoMilestones(db.GetEngine(db.DefaultContext), repoID) assert.NoError(t, err) assert.EqualValues(t, repo.NumMilestones, count) @@ -182,7 +182,7 @@ func TestCountRepoMilestones(t *testing.T) { test(2) test(3) - count, err := countRepoMilestones(db.GetEngine(db.DefaultContext), db.NonexistentID) + count, err := countRepoMilestones(db.GetEngine(db.DefaultContext), unittest.NonexistentID) assert.NoError(t, err) assert.EqualValues(t, 0, count) } @@ -190,7 +190,7 @@ func TestCountRepoMilestones(t *testing.T) { func TestCountRepoClosedMilestones(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(repoID int64) { - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) count, err := CountRepoClosedMilestones(repoID) assert.NoError(t, err) assert.EqualValues(t, repo.NumClosedMilestones, count) @@ -199,27 +199,27 @@ func TestCountRepoClosedMilestones(t *testing.T) { test(2) test(3) - count, err := CountRepoClosedMilestones(db.NonexistentID) + count, err := CountRepoClosedMilestones(unittest.NonexistentID) assert.NoError(t, err) assert.EqualValues(t, 0, count) } func TestChangeMilestoneStatus(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - milestone := db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone) + milestone := unittest.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone) assert.NoError(t, ChangeMilestoneStatus(milestone, true)) - db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}, "is_closed=1") - CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{}) + unittest.AssertExistsAndLoadBean(t, &Milestone{ID: 1}, "is_closed=1") + unittest.CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{}) assert.NoError(t, ChangeMilestoneStatus(milestone, false)) - db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}, "is_closed=0") - CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{}) + unittest.AssertExistsAndLoadBean(t, &Milestone{ID: 1}, "is_closed=0") + unittest.CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{}) } func TestUpdateMilestoneCounters(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{MilestoneID: 1}, + issue := unittest.AssertExistsAndLoadBean(t, &Issue{MilestoneID: 1}, "is_closed=0").(*Issue) issue.IsClosed = true @@ -227,48 +227,48 @@ func TestUpdateMilestoneCounters(t *testing.T) { _, err := db.GetEngine(db.DefaultContext).ID(issue.ID).Cols("is_closed", "closed_unix").Update(issue) assert.NoError(t, err) assert.NoError(t, updateMilestoneCounters(db.GetEngine(db.DefaultContext), issue.MilestoneID)) - CheckConsistencyFor(t, &Milestone{}) + unittest.CheckConsistencyFor(t, &Milestone{}) issue.IsClosed = false issue.ClosedUnix = 0 _, err = db.GetEngine(db.DefaultContext).ID(issue.ID).Cols("is_closed", "closed_unix").Update(issue) assert.NoError(t, err) assert.NoError(t, updateMilestoneCounters(db.GetEngine(db.DefaultContext), issue.MilestoneID)) - CheckConsistencyFor(t, &Milestone{}) + unittest.CheckConsistencyFor(t, &Milestone{}) } func TestChangeMilestoneAssign(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{RepoID: 1}).(*Issue) - doer := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{RepoID: 1}).(*Issue) + doer := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) assert.NotNil(t, issue) assert.NotNil(t, doer) oldMilestoneID := issue.MilestoneID issue.MilestoneID = 2 assert.NoError(t, ChangeMilestoneAssign(issue, doer, oldMilestoneID)) - db.AssertExistsAndLoadBean(t, &Comment{ + unittest.AssertExistsAndLoadBean(t, &Comment{ IssueID: issue.ID, Type: CommentTypeMilestone, MilestoneID: issue.MilestoneID, OldMilestoneID: oldMilestoneID, }) - CheckConsistencyFor(t, &Milestone{}, &Issue{}) + unittest.CheckConsistencyFor(t, &Milestone{}, &Issue{}) } func TestDeleteMilestoneByRepoID(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, DeleteMilestoneByRepoID(1, 1)) - db.AssertNotExistsBean(t, &Milestone{ID: 1}) - CheckConsistencyFor(t, &Repository{ID: 1}) + unittest.AssertNotExistsBean(t, &Milestone{ID: 1}) + unittest.CheckConsistencyFor(t, &Repository{ID: 1}) - assert.NoError(t, DeleteMilestoneByRepoID(db.NonexistentID, db.NonexistentID)) + assert.NoError(t, DeleteMilestoneByRepoID(unittest.NonexistentID, unittest.NonexistentID)) } func TestMilestoneList_LoadTotalTrackedTimes(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) miles := MilestoneList{ - db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone), + unittest.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone), } assert.NoError(t, miles.LoadTotalTrackedTimes()) @@ -279,7 +279,7 @@ func TestMilestoneList_LoadTotalTrackedTimes(t *testing.T) { func TestCountMilestonesByRepoIDs(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) milestonesCount := func(repoID int64) (int, int) { - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) return repo.NumOpenMilestones, repo.NumClosedMilestones } repo1OpenCount, repo1ClosedCount := milestonesCount(1) @@ -298,8 +298,8 @@ func TestCountMilestonesByRepoIDs(t *testing.T) { func TestGetMilestonesByRepoIDs(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) - repo2 := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo2 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) test := func(sortType string, sortCond func(*Milestone) int) { for _, page := range []int{0, 1} { openMilestones, err := GetMilestonesByRepoIDs([]int64{repo1.ID, repo2.ID}, page, false, sortType) @@ -343,7 +343,7 @@ func TestGetMilestonesByRepoIDs(t *testing.T) { func TestLoadTotalTrackedTime(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - milestone := db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone) + milestone := unittest.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone) assert.NoError(t, milestone.LoadTotalTrackedTime()) @@ -354,7 +354,7 @@ func TestGetMilestonesStats(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(repoID int64) { - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) stats, err := GetMilestonesStatsByRepoCond(builder.And(builder.Eq{"repo_id": repoID})) assert.NoError(t, err) assert.EqualValues(t, repo.NumMilestones-repo.NumClosedMilestones, stats.OpenCount) @@ -364,13 +364,13 @@ func TestGetMilestonesStats(t *testing.T) { test(2) test(3) - stats, err := GetMilestonesStatsByRepoCond(builder.And(builder.Eq{"repo_id": db.NonexistentID})) + stats, err := GetMilestonesStatsByRepoCond(builder.And(builder.Eq{"repo_id": unittest.NonexistentID})) assert.NoError(t, err) assert.EqualValues(t, 0, stats.OpenCount) assert.EqualValues(t, 0, stats.ClosedCount) - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) - repo2 := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo2 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) milestoneStats, err := GetMilestonesStatsByRepoCond(builder.In("repo_id", []int64{repo1.ID, repo2.ID})) assert.NoError(t, err) diff --git a/models/issue_reaction_test.go b/models/issue_reaction_test.go index 5c0441623b..c01e4add7a 100644 --- a/models/issue_reaction_test.go +++ b/models/issue_reaction_test.go @@ -28,21 +28,21 @@ func addReaction(t *testing.T, doer *User, issue *Issue, comment *Comment, conte func TestIssueAddReaction(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user1 := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) - issue1 := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) + issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) addReaction(t, user1, issue1, nil, "heart") - db.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID}) + unittest.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID}) } func TestIssueAddDuplicateReaction(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user1 := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) - issue1 := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) + issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) addReaction(t, user1, issue1, nil, "heart") @@ -54,23 +54,23 @@ func TestIssueAddDuplicateReaction(t *testing.T) { assert.Error(t, err) assert.Equal(t, ErrReactionAlreadyExist{Reaction: "heart"}, err) - existingR := db.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID}).(*Reaction) + existingR := unittest.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID}).(*Reaction) assert.Equal(t, existingR.ID, reaction.ID) } func TestIssueDeleteReaction(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user1 := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) - issue1 := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) + issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) addReaction(t, user1, issue1, nil, "heart") err := DeleteIssueReaction(user1, issue1, "heart") assert.NoError(t, err) - db.AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID}) + unittest.AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID}) } func TestIssueReactionCount(t *testing.T) { @@ -78,13 +78,13 @@ func TestIssueReactionCount(t *testing.T) { setting.UI.ReactionMaxUserNum = 2 - user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) - user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - user3 := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) - user4 := db.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) + user1 := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user2 := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user3 := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + user4 := unittest.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) ghost := NewGhostUser() - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue) addReaction(t, user1, issue, nil, "heart") addReaction(t, user2, issue, nil, "heart") @@ -114,29 +114,29 @@ func TestIssueReactionCount(t *testing.T) { func TestIssueCommentAddReaction(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user1 := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) - issue1 := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) + issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) - comment1 := db.AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment) + comment1 := unittest.AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment) addReaction(t, user1, issue1, comment1, "heart") - db.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID, CommentID: comment1.ID}) + unittest.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID, CommentID: comment1.ID}) } func TestIssueCommentDeleteReaction(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) - user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - user3 := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) - user4 := db.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) + user1 := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user2 := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user3 := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + user4 := unittest.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) - issue1 := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: issue1.RepoID}).(*Repository) + issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: issue1.RepoID}).(*Repository) - comment1 := db.AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment) + comment1 := unittest.AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment) addReaction(t, user1, issue1, comment1, "heart") addReaction(t, user2, issue1, comment1, "heart") @@ -155,14 +155,14 @@ func TestIssueCommentDeleteReaction(t *testing.T) { func TestIssueCommentReactionCount(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user1 := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) - issue1 := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) + issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) - comment1 := db.AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment) + comment1 := unittest.AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment) addReaction(t, user1, issue1, comment1, "heart") assert.NoError(t, DeleteCommentReaction(user1, issue1, comment1, "heart")) - db.AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID, CommentID: comment1.ID}) + unittest.AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID, CommentID: comment1.ID}) } diff --git a/models/issue_stopwatch_test.go b/models/issue_stopwatch_test.go index 0de4dd0347..ceb579d095 100644 --- a/models/issue_stopwatch_test.go +++ b/models/issue_stopwatch_test.go @@ -7,7 +7,6 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/timeutil" @@ -27,9 +26,9 @@ func TestCancelStopwatch(t *testing.T) { err = CancelStopwatch(user1, issue1) assert.NoError(t, err) - db.AssertNotExistsBean(t, &Stopwatch{UserID: user1.ID, IssueID: issue1.ID}) + unittest.AssertNotExistsBean(t, &Stopwatch{UserID: user1.ID, IssueID: issue1.ID}) - _ = db.AssertExistsAndLoadBean(t, &Comment{Type: CommentTypeCancelTracking, PosterID: user1.ID, IssueID: issue1.ID}) + _ = unittest.AssertExistsAndLoadBean(t, &Comment{Type: CommentTypeCancelTracking, PosterID: user1.ID, IssueID: issue1.ID}) assert.Nil(t, CancelStopwatch(user1, issue2)) } @@ -68,10 +67,10 @@ func TestCreateOrStopIssueStopwatch(t *testing.T) { assert.NoError(t, err) assert.NoError(t, CreateOrStopIssueStopwatch(user3, issue1)) - sw := db.AssertExistsAndLoadBean(t, &Stopwatch{UserID: 3, IssueID: 1}).(*Stopwatch) + sw := unittest.AssertExistsAndLoadBean(t, &Stopwatch{UserID: 3, IssueID: 1}).(*Stopwatch) assert.LessOrEqual(t, sw.CreatedUnix, timeutil.TimeStampNow()) assert.NoError(t, CreateOrStopIssueStopwatch(user2, issue2)) - db.AssertNotExistsBean(t, &Stopwatch{UserID: 2, IssueID: 2}) - db.AssertExistsAndLoadBean(t, &TrackedTime{UserID: 2, IssueID: 2}) + unittest.AssertNotExistsBean(t, &Stopwatch{UserID: 2, IssueID: 2}) + unittest.AssertExistsAndLoadBean(t, &TrackedTime{UserID: 2, IssueID: 2}) } diff --git a/models/issue_test.go b/models/issue_test.go index 296e12a5d4..f53febb1b3 100644 --- a/models/issue_test.go +++ b/models/issue_test.go @@ -20,18 +20,18 @@ func TestIssue_ReplaceLabels(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(issueID int64, labelIDs []int64) { - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: issueID}).(*Issue) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: issue.RepoID}).(*Repository) - doer := db.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: issueID}).(*Issue) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: issue.RepoID}).(*Repository) + doer := unittest.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User) labels := make([]*Label, len(labelIDs)) for i, labelID := range labelIDs { - labels[i] = db.AssertExistsAndLoadBean(t, &Label{ID: labelID, RepoID: repo.ID}).(*Label) + labels[i] = unittest.AssertExistsAndLoadBean(t, &Label{ID: labelID, RepoID: repo.ID}).(*Label) } assert.NoError(t, issue.ReplaceLabels(labels, doer)) - db.AssertCount(t, &IssueLabel{IssueID: issueID}, len(labelIDs)) + unittest.AssertCount(t, &IssueLabel{IssueID: issueID}, len(labelIDs)) for _, labelID := range labelIDs { - db.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issueID, LabelID: labelID}) + unittest.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issueID, LabelID: labelID}) } } @@ -50,7 +50,7 @@ func Test_GetIssueIDsByRepoID(t *testing.T) { func TestIssueAPIURL(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) err := issue.LoadAttributes() assert.NoError(t, err) @@ -69,7 +69,7 @@ func TestGetIssuesByIDs(t *testing.T) { assert.Equal(t, expectedIssueIDs, actualIssueIDs) } testSuccess([]int64{1, 2, 3}, []int64{}) - testSuccess([]int64{1, 2, 3}, []int64{db.NonexistentID}) + testSuccess([]int64{1, 2, 3}, []int64{unittest.NonexistentID}) } func TestGetParticipantIDsByIssue(t *testing.T) { @@ -108,16 +108,16 @@ func TestIssue_ClearLabels(t *testing.T) { } for _, test := range tests { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: test.issueID}).(*Issue) - doer := db.AssertExistsAndLoadBean(t, &User{ID: test.doerID}).(*User) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: test.issueID}).(*Issue) + doer := unittest.AssertExistsAndLoadBean(t, &User{ID: test.doerID}).(*User) assert.NoError(t, issue.ClearLabels(doer)) - db.AssertNotExistsBean(t, &IssueLabel{IssueID: test.issueID}) + unittest.AssertNotExistsBean(t, &IssueLabel{IssueID: test.issueID}) } } func TestUpdateIssueCols(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{}).(*Issue) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{}).(*Issue) const newTitle = "New Title for unit test" issue.Title = newTitle @@ -129,10 +129,10 @@ func TestUpdateIssueCols(t *testing.T) { assert.NoError(t, updateIssueCols(db.GetEngine(db.DefaultContext), issue, "name")) then := time.Now().Unix() - updatedIssue := db.AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue) + updatedIssue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue) assert.EqualValues(t, newTitle, updatedIssue.Title) assert.EqualValues(t, prevContent, updatedIssue.Content) - db.AssertInt64InRange(t, now, then, int64(updatedIssue.UpdatedUnix)) + unittest.AssertInt64InRange(t, now, then, int64(updatedIssue.UpdatedUnix)) } func TestIssues(t *testing.T) { @@ -321,7 +321,7 @@ func TestIssue_SearchIssueIDsByKeyword(t *testing.T) { func TestGetRepoIDsForIssuesOptions(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) for _, test := range []struct { Opts IssuesOptions ExpectedRepoIDs []int64 @@ -352,8 +352,8 @@ func TestGetRepoIDsForIssuesOptions(t *testing.T) { func testInsertIssue(t *testing.T, title, content string, expectIndex int64) *Issue { var newIssue Issue t.Run(title, func(t *testing.T) { - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) issue := Issue{ RepoID: repo.ID, @@ -395,10 +395,10 @@ func TestIssue_ResolveMentions(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(owner, repo, doer string, mentions []string, expected []int64) { - o := db.AssertExistsAndLoadBean(t, &User{LowerName: owner}).(*User) - r := db.AssertExistsAndLoadBean(t, &Repository{OwnerID: o.ID, LowerName: repo}).(*Repository) + o := unittest.AssertExistsAndLoadBean(t, &User{LowerName: owner}).(*User) + r := unittest.AssertExistsAndLoadBean(t, &Repository{OwnerID: o.ID, LowerName: repo}).(*Repository) issue := &Issue{RepoID: r.ID} - d := db.AssertExistsAndLoadBean(t, &User{LowerName: doer}).(*User) + d := unittest.AssertExistsAndLoadBean(t, &User{LowerName: doer}).(*User) resolved, err := issue.ResolveMentionsByVisibility(db.DefaultContext, d, mentions) assert.NoError(t, err) ids := make([]int64, len(resolved)) diff --git a/models/issue_tracked_time_test.go b/models/issue_tracked_time_test.go index 2770065363..9bc9664521 100644 --- a/models/issue_tracked_time_test.go +++ b/models/issue_tracked_time_test.go @@ -8,7 +8,6 @@ import ( "testing" "time" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -29,10 +28,10 @@ func TestAddTime(t *testing.T) { assert.Equal(t, int64(1), trackedTime.IssueID) assert.Equal(t, int64(3661), trackedTime.Time) - tt := db.AssertExistsAndLoadBean(t, &TrackedTime{UserID: 3, IssueID: 1}).(*TrackedTime) + tt := unittest.AssertExistsAndLoadBean(t, &TrackedTime{UserID: 3, IssueID: 1}).(*TrackedTime) assert.Equal(t, int64(3661), tt.Time) - comment := db.AssertExistsAndLoadBean(t, &Comment{Type: CommentTypeAddTimeManual, PosterID: 3, IssueID: 1}).(*Comment) + comment := unittest.AssertExistsAndLoadBean(t, &Comment{Type: CommentTypeAddTimeManual, PosterID: 3, IssueID: 1}).(*Comment) assert.Equal(t, comment.Content, "1h 1min 1s") } diff --git a/models/issue_user_test.go b/models/issue_user_test.go index 1a9ae6395b..1be2763703 100644 --- a/models/issue_user_test.go +++ b/models/issue_user_test.go @@ -15,7 +15,7 @@ import ( func Test_newIssueUsers(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) newIssue := &Issue{ RepoID: repo.ID, PosterID: 4, @@ -25,35 +25,35 @@ func Test_newIssueUsers(t *testing.T) { } // artificially insert new issue - db.AssertSuccessfulInsert(t, newIssue) + unittest.AssertSuccessfulInsert(t, newIssue) assert.NoError(t, newIssueUsers(db.GetEngine(db.DefaultContext), repo, newIssue)) // issue_user table should now have entries for new issue - db.AssertExistsAndLoadBean(t, &IssueUser{IssueID: newIssue.ID, UID: newIssue.PosterID}) - db.AssertExistsAndLoadBean(t, &IssueUser{IssueID: newIssue.ID, UID: repo.OwnerID}) + unittest.AssertExistsAndLoadBean(t, &IssueUser{IssueID: newIssue.ID, UID: newIssue.PosterID}) + unittest.AssertExistsAndLoadBean(t, &IssueUser{IssueID: newIssue.ID, UID: repo.OwnerID}) } func TestUpdateIssueUserByRead(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) assert.NoError(t, UpdateIssueUserByRead(4, issue.ID)) - db.AssertExistsAndLoadBean(t, &IssueUser{IssueID: issue.ID, UID: 4}, "is_read=1") + unittest.AssertExistsAndLoadBean(t, &IssueUser{IssueID: issue.ID, UID: 4}, "is_read=1") assert.NoError(t, UpdateIssueUserByRead(4, issue.ID)) - db.AssertExistsAndLoadBean(t, &IssueUser{IssueID: issue.ID, UID: 4}, "is_read=1") + unittest.AssertExistsAndLoadBean(t, &IssueUser{IssueID: issue.ID, UID: 4}, "is_read=1") - assert.NoError(t, UpdateIssueUserByRead(db.NonexistentID, db.NonexistentID)) + assert.NoError(t, UpdateIssueUserByRead(unittest.NonexistentID, unittest.NonexistentID)) } func TestUpdateIssueUsersByMentions(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) uids := []int64{2, 5} assert.NoError(t, UpdateIssueUsersByMentions(db.DefaultContext, issue.ID, uids)) for _, uid := range uids { - db.AssertExistsAndLoadBean(t, &IssueUser{IssueID: issue.ID, UID: uid}, "is_mentioned=1") + unittest.AssertExistsAndLoadBean(t, &IssueUser{IssueID: issue.ID, UID: uid}, "is_mentioned=1") } } diff --git a/models/issue_watch_test.go b/models/issue_watch_test.go index 9aaa184a34..18d49bfce5 100644 --- a/models/issue_watch_test.go +++ b/models/issue_watch_test.go @@ -16,11 +16,11 @@ func TestCreateOrUpdateIssueWatch(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, CreateOrUpdateIssueWatch(3, 1, true)) - iw := db.AssertExistsAndLoadBean(t, &IssueWatch{UserID: 3, IssueID: 1}).(*IssueWatch) + iw := unittest.AssertExistsAndLoadBean(t, &IssueWatch{UserID: 3, IssueID: 1}).(*IssueWatch) assert.True(t, iw.IsWatching) assert.NoError(t, CreateOrUpdateIssueWatch(1, 1, false)) - iw = db.AssertExistsAndLoadBean(t, &IssueWatch{UserID: 1, IssueID: 1}).(*IssueWatch) + iw = unittest.AssertExistsAndLoadBean(t, &IssueWatch{UserID: 1, IssueID: 1}).(*IssueWatch) assert.False(t, iw.IsWatching) } diff --git a/models/issue_xref_test.go b/models/issue_xref_test.go index a1f4515b60..551c29dcba 100644 --- a/models/issue_xref_test.go +++ b/models/issue_xref_test.go @@ -24,7 +24,7 @@ func TestXRef_AddCrossReferences(t *testing.T) { // PR to close issue #1 content := fmt.Sprintf("content2, closes #%d", itarget.Index) pr := testCreateIssue(t, 1, 2, "title2", content, true) - ref := db.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: pr.ID, RefCommentID: 0}).(*Comment) + ref := unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: pr.ID, RefCommentID: 0}).(*Comment) assert.Equal(t, CommentTypePullRef, ref.Type) assert.Equal(t, pr.RepoID, ref.RefRepoID) assert.True(t, ref.RefIsPull) @@ -33,7 +33,7 @@ func TestXRef_AddCrossReferences(t *testing.T) { // Comment on PR to reopen issue #1 content = fmt.Sprintf("content2, reopens #%d", itarget.Index) c := testCreateComment(t, 1, 2, pr.ID, content) - ref = db.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: pr.ID, RefCommentID: c.ID}).(*Comment) + ref = unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: pr.ID, RefCommentID: c.ID}).(*Comment) assert.Equal(t, CommentTypeCommentRef, ref.Type) assert.Equal(t, pr.RepoID, ref.RefRepoID) assert.True(t, ref.RefIsPull) @@ -42,7 +42,7 @@ func TestXRef_AddCrossReferences(t *testing.T) { // Issue mentioning issue #1 content = fmt.Sprintf("content3, mentions #%d", itarget.Index) i := testCreateIssue(t, 1, 2, "title3", content, false) - ref = db.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*Comment) + ref = unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*Comment) assert.Equal(t, CommentTypeIssueRef, ref.Type) assert.Equal(t, pr.RepoID, ref.RefRepoID) assert.False(t, ref.RefIsPull) @@ -54,7 +54,7 @@ func TestXRef_AddCrossReferences(t *testing.T) { // Cross-reference to issue #4 by admin content = fmt.Sprintf("content5, mentions user3/repo3#%d", itarget.Index) i = testCreateIssue(t, 2, 1, "title5", content, false) - ref = db.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*Comment) + ref = unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*Comment) assert.Equal(t, CommentTypeIssueRef, ref.Type) assert.Equal(t, i.RepoID, ref.RefRepoID) assert.False(t, ref.RefIsPull) @@ -63,7 +63,7 @@ func TestXRef_AddCrossReferences(t *testing.T) { // Cross-reference to issue #4 with no permission content = fmt.Sprintf("content6, mentions user3/repo3#%d", itarget.Index) i = testCreateIssue(t, 4, 5, "title6", content, false) - db.AssertNotExistsBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}) + unittest.AssertNotExistsBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}) } func TestXRef_NeuterCrossReferences(t *testing.T) { @@ -75,15 +75,15 @@ func TestXRef_NeuterCrossReferences(t *testing.T) { // Issue mentioning issue #1 title := fmt.Sprintf("title2, mentions #%d", itarget.Index) i := testCreateIssue(t, 1, 2, title, "content2", false) - ref := db.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*Comment) + ref := unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*Comment) assert.Equal(t, CommentTypeIssueRef, ref.Type) assert.Equal(t, references.XRefActionNone, ref.RefAction) - d := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + d := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) i.Title = "title2, no mentions" assert.NoError(t, i.ChangeTitle(d, title)) - ref = db.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*Comment) + ref = unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*Comment) assert.Equal(t, CommentTypeIssueRef, ref.Type) assert.Equal(t, references.XRefActionNeutered, ref.RefAction) } @@ -91,7 +91,7 @@ func TestXRef_NeuterCrossReferences(t *testing.T) { func TestXRef_ResolveCrossReferences(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - d := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + d := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) i1 := testCreateIssue(t, 1, 2, "title1", "content1", false) i2 := testCreateIssue(t, 1, 2, "title2", "content2", false) @@ -100,21 +100,21 @@ func TestXRef_ResolveCrossReferences(t *testing.T) { assert.NoError(t, err) pr := testCreatePR(t, 1, 2, "titlepr", fmt.Sprintf("closes #%d", i1.Index)) - rp := db.AssertExistsAndLoadBean(t, &Comment{IssueID: i1.ID, RefIssueID: pr.Issue.ID, RefCommentID: 0}).(*Comment) + rp := unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: i1.ID, RefIssueID: pr.Issue.ID, RefCommentID: 0}).(*Comment) c1 := testCreateComment(t, 1, 2, pr.Issue.ID, fmt.Sprintf("closes #%d", i2.Index)) - r1 := db.AssertExistsAndLoadBean(t, &Comment{IssueID: i2.ID, RefIssueID: pr.Issue.ID, RefCommentID: c1.ID}).(*Comment) + r1 := unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: i2.ID, RefIssueID: pr.Issue.ID, RefCommentID: c1.ID}).(*Comment) // Must be ignored c2 := testCreateComment(t, 1, 2, pr.Issue.ID, fmt.Sprintf("mentions #%d", i2.Index)) - db.AssertExistsAndLoadBean(t, &Comment{IssueID: i2.ID, RefIssueID: pr.Issue.ID, RefCommentID: c2.ID}) + unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: i2.ID, RefIssueID: pr.Issue.ID, RefCommentID: c2.ID}) // Must be superseded by c4/r4 c3 := testCreateComment(t, 1, 2, pr.Issue.ID, fmt.Sprintf("reopens #%d", i3.Index)) - db.AssertExistsAndLoadBean(t, &Comment{IssueID: i3.ID, RefIssueID: pr.Issue.ID, RefCommentID: c3.ID}) + unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: i3.ID, RefIssueID: pr.Issue.ID, RefCommentID: c3.ID}) c4 := testCreateComment(t, 1, 2, pr.Issue.ID, fmt.Sprintf("closes #%d", i3.Index)) - r4 := db.AssertExistsAndLoadBean(t, &Comment{IssueID: i3.ID, RefIssueID: pr.Issue.ID, RefCommentID: c4.ID}).(*Comment) + r4 := unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: i3.ID, RefIssueID: pr.Issue.ID, RefCommentID: c4.ID}).(*Comment) refs, err := pr.ResolveCrossReferences() assert.NoError(t, err) @@ -125,8 +125,8 @@ func TestXRef_ResolveCrossReferences(t *testing.T) { } func testCreateIssue(t *testing.T, repo, doer int64, title, content string, ispull bool) *Issue { - r := db.AssertExistsAndLoadBean(t, &Repository{ID: repo}).(*Repository) - d := db.AssertExistsAndLoadBean(t, &User{ID: doer}).(*User) + r := unittest.AssertExistsAndLoadBean(t, &Repository{ID: repo}).(*Repository) + d := unittest.AssertExistsAndLoadBean(t, &User{ID: doer}).(*User) idx, err := db.GetNextResourceIndex("issue_index", r.ID) assert.NoError(t, err) @@ -157,8 +157,8 @@ func testCreateIssue(t *testing.T, repo, doer int64, title, content string, ispu } func testCreatePR(t *testing.T, repo, doer int64, title, content string) *PullRequest { - r := db.AssertExistsAndLoadBean(t, &Repository{ID: repo}).(*Repository) - d := db.AssertExistsAndLoadBean(t, &User{ID: doer}).(*User) + r := unittest.AssertExistsAndLoadBean(t, &Repository{ID: repo}).(*Repository) + d := unittest.AssertExistsAndLoadBean(t, &User{ID: doer}).(*User) i := &Issue{RepoID: r.ID, PosterID: d.ID, Poster: d, Title: title, Content: content, IsPull: true} pr := &PullRequest{HeadRepoID: repo, BaseRepoID: repo, HeadBranch: "head", BaseBranch: "base", Status: PullRequestStatusMergeable} assert.NoError(t, NewPullRequest(r, i, nil, nil, pr)) @@ -167,8 +167,8 @@ func testCreatePR(t *testing.T, repo, doer int64, title, content string) *PullRe } func testCreateComment(t *testing.T, repo, doer, issue int64, content string) *Comment { - d := db.AssertExistsAndLoadBean(t, &User{ID: doer}).(*User) - i := db.AssertExistsAndLoadBean(t, &Issue{ID: issue}).(*Issue) + d := unittest.AssertExistsAndLoadBean(t, &User{ID: doer}).(*User) + i := unittest.AssertExistsAndLoadBean(t, &Issue{ID: issue}).(*Issue) c := &Comment{Type: CommentTypeComment, PosterID: doer, Poster: d, IssueID: issue, Issue: i, Content: content} sess := db.NewSession(db.DefaultContext) diff --git a/models/login/oauth2_application_test.go b/models/login/oauth2_application_test.go index 876de5b52a..47e6a27ce9 100644 --- a/models/login/oauth2_application_test.go +++ b/models/login/oauth2_application_test.go @@ -7,7 +7,6 @@ package login import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" @@ -17,16 +16,16 @@ import ( func TestOAuth2Application_GenerateClientSecret(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - app := db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application) + app := unittest.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application) secret, err := app.GenerateClientSecret() assert.NoError(t, err) assert.True(t, len(secret) > 0) - db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1, ClientSecret: app.ClientSecret}) + unittest.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1, ClientSecret: app.ClientSecret}) } func BenchmarkOAuth2Application_GenerateClientSecret(b *testing.B) { assert.NoError(b, unittest.PrepareTestDatabase()) - app := db.AssertExistsAndLoadBean(b, &OAuth2Application{ID: 1}).(*OAuth2Application) + app := unittest.AssertExistsAndLoadBean(b, &OAuth2Application{ID: 1}).(*OAuth2Application) for i := 0; i < b.N; i++ { _, _ = app.GenerateClientSecret() } @@ -44,7 +43,7 @@ func TestOAuth2Application_ContainsRedirectURI(t *testing.T) { func TestOAuth2Application_ValidateClientSecret(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - app := db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application) + app := unittest.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application) secret, err := app.GenerateClientSecret() assert.NoError(t, err) assert.True(t, app.ValidateClientSecret([]byte(secret))) @@ -68,7 +67,7 @@ func TestCreateOAuth2Application(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "newapp", app.Name) assert.Len(t, app.ClientID, 36) - db.AssertExistsAndLoadBean(t, &OAuth2Application{Name: "newapp"}) + unittest.AssertExistsAndLoadBean(t, &OAuth2Application{Name: "newapp"}) } func TestOAuth2Application_TableName(t *testing.T) { @@ -77,7 +76,7 @@ func TestOAuth2Application_TableName(t *testing.T) { func TestOAuth2Application_GetGrantByUserID(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - app := db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application) + app := unittest.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application) grant, err := app.GetGrantByUserID(1) assert.NoError(t, err) assert.Equal(t, int64(1), grant.UserID) @@ -89,7 +88,7 @@ func TestOAuth2Application_GetGrantByUserID(t *testing.T) { func TestOAuth2Application_CreateGrant(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - app := db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application) + app := unittest.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application) grant, err := app.CreateGrant(2, "") assert.NoError(t, err) assert.NotNil(t, grant) @@ -113,15 +112,15 @@ func TestGetOAuth2GrantByID(t *testing.T) { func TestOAuth2Grant_IncreaseCounter(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - grant := db.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1, Counter: 1}).(*OAuth2Grant) + grant := unittest.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1, Counter: 1}).(*OAuth2Grant) assert.NoError(t, grant.IncreaseCounter()) assert.Equal(t, int64(2), grant.Counter) - db.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1, Counter: 2}) + unittest.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1, Counter: 2}) } func TestOAuth2Grant_ScopeContains(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - grant := db.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1, Scope: "openid profile"}).(*OAuth2Grant) + grant := unittest.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1, Scope: "openid profile"}).(*OAuth2Grant) assert.True(t, grant.ScopeContains("openid")) assert.True(t, grant.ScopeContains("profile")) assert.False(t, grant.ScopeContains("profil")) @@ -130,7 +129,7 @@ func TestOAuth2Grant_ScopeContains(t *testing.T) { func TestOAuth2Grant_GenerateNewAuthorizationCode(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - grant := db.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1}).(*OAuth2Grant) + grant := unittest.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1}).(*OAuth2Grant) code, err := grant.GenerateNewAuthorizationCode("https://example2.com/callback", "CjvyTLSdR47G5zYenDA-eDWW4lRrO8yvjcWwbD_deOg", "S256") assert.NoError(t, err) assert.NotNil(t, code) @@ -157,7 +156,7 @@ func TestGetOAuth2GrantsByUserID(t *testing.T) { func TestRevokeOAuth2Grant(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, RevokeOAuth2Grant(1, 1)) - db.AssertNotExistsBean(t, &OAuth2Grant{ID: 1, UserID: 1}) + unittest.AssertNotExistsBean(t, &OAuth2Grant{ID: 1, UserID: 1}) } //////////////////// Authorization Code @@ -224,9 +223,9 @@ func TestOAuth2AuthorizationCode_GenerateRedirectURI(t *testing.T) { func TestOAuth2AuthorizationCode_Invalidate(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - code := db.AssertExistsAndLoadBean(t, &OAuth2AuthorizationCode{Code: "authcode"}).(*OAuth2AuthorizationCode) + code := unittest.AssertExistsAndLoadBean(t, &OAuth2AuthorizationCode{Code: "authcode"}).(*OAuth2AuthorizationCode) assert.NoError(t, code.Invalidate()) - db.AssertNotExistsBean(t, &OAuth2AuthorizationCode{Code: "authcode"}) + unittest.AssertNotExistsBean(t, &OAuth2AuthorizationCode{Code: "authcode"}) } func TestOAuth2AuthorizationCode_TableName(t *testing.T) { diff --git a/models/login/u2f_test.go b/models/login/u2f_test.go index 11f20bc790..06a37f8132 100644 --- a/models/login/u2f_test.go +++ b/models/login/u2f_test.go @@ -8,7 +8,6 @@ import ( "encoding/hex" "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" @@ -43,18 +42,18 @@ func TestU2FRegistration_TableName(t *testing.T) { func TestU2FRegistration_UpdateCounter(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - reg := db.AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration) + reg := unittest.AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration) reg.Counter = 1 assert.NoError(t, reg.UpdateCounter()) - db.AssertExistsIf(t, true, &U2FRegistration{ID: 1, Counter: 1}) + unittest.AssertExistsIf(t, true, &U2FRegistration{ID: 1, Counter: 1}) } func TestU2FRegistration_UpdateLargeCounter(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - reg := db.AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration) + reg := unittest.AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration) reg.Counter = 0xffffffff assert.NoError(t, reg.UpdateCounter()) - db.AssertExistsIf(t, true, &U2FRegistration{ID: 1, Counter: 0xffffffff}) + unittest.AssertExistsIf(t, true, &U2FRegistration{ID: 1, Counter: 0xffffffff}) } func TestCreateRegistration(t *testing.T) { @@ -65,15 +64,15 @@ func TestCreateRegistration(t *testing.T) { assert.Equal(t, "U2F Created Key", res.Name) assert.Equal(t, []byte("Test"), res.Raw) - db.AssertExistsIf(t, true, &U2FRegistration{Name: "U2F Created Key", UserID: 1}) + unittest.AssertExistsIf(t, true, &U2FRegistration{Name: "U2F Created Key", UserID: 1}) } func TestDeleteRegistration(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - reg := db.AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration) + reg := unittest.AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration) assert.NoError(t, DeleteRegistration(reg)) - db.AssertNotExistsBean(t, &U2FRegistration{ID: 1}) + unittest.AssertNotExistsBean(t, &U2FRegistration{ID: 1}) } const validU2FRegistrationResponseHex = "0504b174bc49c7ca254b70d2e5c207cee9cf174820ebd77ea3c65508c26da51b657c1cc6b952f8621697936482da0a6d3d3826a59095daf6cd7c03e2e60385d2f6d9402a552dfdb7477ed65fd84133f86196010b2215b57da75d315b7b9e8fe2e3925a6019551bab61d16591659cbaf00b4950f7abfe6660e2e006f76868b772d70c253082013c3081e4a003020102020a47901280001155957352300a06082a8648ce3d0403023017311530130603550403130c476e756262792050696c6f74301e170d3132303831343138323933325a170d3133303831343138323933325a3031312f302d0603550403132650696c6f74476e756262792d302e342e312d34373930313238303030313135353935373335323059301306072a8648ce3d020106082a8648ce3d030107034200048d617e65c9508e64bcc5673ac82a6799da3c1446682c258c463fffdf58dfd2fa3e6c378b53d795c4a4dffb4199edd7862f23abaf0203b4b8911ba0569994e101300a06082a8648ce3d0403020347003044022060cdb6061e9c22262d1aac1d96d8c70829b2366531dda268832cb836bcd30dfa0220631b1459f09e6330055722c8d89b7f48883b9089b88d60d1d9795902b30410df304502201471899bcc3987e62e8202c9b39c33c19033f7340352dba80fcab017db9230e402210082677d673d891933ade6f617e5dbde2e247e70423fd5ad7804a6d3d3961ef871" diff --git a/models/main_test.go b/models/main_test.go index 15bece7bf4..5c99a2b2f7 100644 --- a/models/main_test.go +++ b/models/main_test.go @@ -15,7 +15,7 @@ import ( // TestFixturesAreConsistent assert that test fixtures are consistent func TestFixturesAreConsistent(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - CheckConsistencyFor(t, + unittest.CheckConsistencyFor(t, &User{}, &Repository{}, &Issue{}, diff --git a/models/notification_test.go b/models/notification_test.go index cdba409f3d..db2164a2f5 100644 --- a/models/notification_test.go +++ b/models/notification_test.go @@ -7,29 +7,28 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestCreateOrUpdateIssueNotifications(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) assert.NoError(t, CreateOrUpdateIssueNotifications(issue.ID, 0, 2, 0)) // User 9 is inactive, thus notifications for user 1 and 4 are created - notf := db.AssertExistsAndLoadBean(t, &Notification{UserID: 1, IssueID: issue.ID}).(*Notification) + notf := unittest.AssertExistsAndLoadBean(t, &Notification{UserID: 1, IssueID: issue.ID}).(*Notification) assert.Equal(t, NotificationStatusUnread, notf.Status) - CheckConsistencyFor(t, &Issue{ID: issue.ID}) + unittest.CheckConsistencyFor(t, &Issue{ID: issue.ID}) - notf = db.AssertExistsAndLoadBean(t, &Notification{UserID: 4, IssueID: issue.ID}).(*Notification) + notf = unittest.AssertExistsAndLoadBean(t, &Notification{UserID: 4, IssueID: issue.ID}).(*Notification) assert.Equal(t, NotificationStatusUnread, notf.Status) } func TestNotificationsForUser(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) statuses := []NotificationStatus{NotificationStatusRead, NotificationStatusUnread} notfs, err := NotificationsForUser(user, statuses, 1, 10) assert.NoError(t, err) @@ -45,7 +44,7 @@ func TestNotificationsForUser(t *testing.T) { func TestNotification_GetRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - notf := db.AssertExistsAndLoadBean(t, &Notification{RepoID: 1}).(*Notification) + notf := unittest.AssertExistsAndLoadBean(t, &Notification{RepoID: 1}).(*Notification) repo, err := notf.GetRepo() assert.NoError(t, err) assert.Equal(t, repo, notf.Repository) @@ -54,7 +53,7 @@ func TestNotification_GetRepo(t *testing.T) { func TestNotification_GetIssue(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - notf := db.AssertExistsAndLoadBean(t, &Notification{RepoID: 1}).(*Notification) + notf := unittest.AssertExistsAndLoadBean(t, &Notification{RepoID: 1}).(*Notification) issue, err := notf.GetIssue() assert.NoError(t, err) assert.Equal(t, issue, notf.Issue) @@ -63,7 +62,7 @@ func TestNotification_GetIssue(t *testing.T) { func TestGetNotificationCount(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) cnt, err := GetNotificationCount(user, NotificationStatusRead) assert.NoError(t, err) assert.EqualValues(t, 0, cnt) @@ -75,34 +74,34 @@ func TestGetNotificationCount(t *testing.T) { func TestSetNotificationStatus(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - notf := db.AssertExistsAndLoadBean(t, + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + notf := unittest.AssertExistsAndLoadBean(t, &Notification{UserID: user.ID, Status: NotificationStatusRead}).(*Notification) _, err := SetNotificationStatus(notf.ID, user, NotificationStatusPinned) assert.NoError(t, err) - db.AssertExistsAndLoadBean(t, + unittest.AssertExistsAndLoadBean(t, &Notification{ID: notf.ID, Status: NotificationStatusPinned}) _, err = SetNotificationStatus(1, user, NotificationStatusRead) assert.Error(t, err) - _, err = SetNotificationStatus(db.NonexistentID, user, NotificationStatusRead) + _, err = SetNotificationStatus(unittest.NonexistentID, user, NotificationStatusRead) assert.Error(t, err) } func TestUpdateNotificationStatuses(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - notfUnread := db.AssertExistsAndLoadBean(t, + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + notfUnread := unittest.AssertExistsAndLoadBean(t, &Notification{UserID: user.ID, Status: NotificationStatusUnread}).(*Notification) - notfRead := db.AssertExistsAndLoadBean(t, + notfRead := unittest.AssertExistsAndLoadBean(t, &Notification{UserID: user.ID, Status: NotificationStatusRead}).(*Notification) - notfPinned := db.AssertExistsAndLoadBean(t, + notfPinned := unittest.AssertExistsAndLoadBean(t, &Notification{UserID: user.ID, Status: NotificationStatusPinned}).(*Notification) assert.NoError(t, UpdateNotificationStatuses(user, NotificationStatusUnread, NotificationStatusRead)) - db.AssertExistsAndLoadBean(t, + unittest.AssertExistsAndLoadBean(t, &Notification{ID: notfUnread.ID, Status: NotificationStatusRead}) - db.AssertExistsAndLoadBean(t, + unittest.AssertExistsAndLoadBean(t, &Notification{ID: notfRead.ID, Status: NotificationStatusRead}) - db.AssertExistsAndLoadBean(t, + unittest.AssertExistsAndLoadBean(t, &Notification{ID: notfPinned.ID, Status: NotificationStatusPinned}) } diff --git a/models/org_team_test.go b/models/org_team_test.go index 114c8016e8..9277ac4f5e 100644 --- a/models/org_team_test.go +++ b/models/org_team_test.go @@ -8,7 +8,6 @@ import ( "strings" "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -16,36 +15,36 @@ import ( func TestTeam_IsOwnerTeam(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - team := db.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) assert.True(t, team.IsOwnerTeam()) - team = db.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) + team = unittest.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) assert.False(t, team.IsOwnerTeam()) } func TestTeam_IsMember(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - team := db.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) assert.True(t, team.IsMember(2)) assert.False(t, team.IsMember(4)) - assert.False(t, team.IsMember(db.NonexistentID)) + assert.False(t, team.IsMember(unittest.NonexistentID)) - team = db.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) + team = unittest.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) assert.True(t, team.IsMember(2)) assert.True(t, team.IsMember(4)) - assert.False(t, team.IsMember(db.NonexistentID)) + assert.False(t, team.IsMember(unittest.NonexistentID)) } func TestTeam_GetRepositories(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(teamID int64) { - team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.NoError(t, team.GetRepositories(&SearchTeamOptions{})) assert.Len(t, team.Repos, team.NumRepos) for _, repo := range team.Repos { - db.AssertExistsAndLoadBean(t, &TeamRepo{TeamID: teamID, RepoID: repo.ID}) + unittest.AssertExistsAndLoadBean(t, &TeamRepo{TeamID: teamID, RepoID: repo.ID}) } } test(1) @@ -56,11 +55,11 @@ func TestTeam_GetMembers(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(teamID int64) { - team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.NoError(t, team.GetMembers(&SearchMembersOptions{})) assert.Len(t, team.Members, team.NumMembers) for _, member := range team.Members { - db.AssertExistsAndLoadBean(t, &TeamUser{UID: member.ID, TeamID: teamID}) + unittest.AssertExistsAndLoadBean(t, &TeamUser{UID: member.ID, TeamID: teamID}) } } test(1) @@ -71,10 +70,10 @@ func TestTeam_AddMember(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(teamID, userID int64) { - team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.NoError(t, team.AddMember(userID)) - db.AssertExistsAndLoadBean(t, &TeamUser{UID: userID, TeamID: teamID}) - CheckConsistencyFor(t, &Team{ID: teamID}, &User{ID: team.OrgID}) + unittest.AssertExistsAndLoadBean(t, &TeamUser{UID: userID, TeamID: teamID}) + unittest.CheckConsistencyFor(t, &Team{ID: teamID}, &User{ID: team.OrgID}) } test(1, 2) test(1, 4) @@ -85,17 +84,17 @@ func TestTeam_RemoveMember(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(teamID, userID int64) { - team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.NoError(t, team.RemoveMember(userID)) - db.AssertNotExistsBean(t, &TeamUser{UID: userID, TeamID: teamID}) - CheckConsistencyFor(t, &Team{ID: teamID}) + unittest.AssertNotExistsBean(t, &TeamUser{UID: userID, TeamID: teamID}) + unittest.CheckConsistencyFor(t, &Team{ID: teamID}) } testSuccess(1, 4) testSuccess(2, 2) testSuccess(3, 2) - testSuccess(3, db.NonexistentID) + testSuccess(3, unittest.NonexistentID) - team := db.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) err := team.RemoveMember(2) assert.True(t, IsErrLastOrgOwner(err)) } @@ -104,13 +103,13 @@ func TestTeam_HasRepository(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(teamID, repoID int64, expected bool) { - team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.Equal(t, expected, team.HasRepository(repoID)) } test(1, 1, false) test(1, 3, true) test(1, 5, true) - test(1, db.NonexistentID, false) + test(1, unittest.NonexistentID, false) test(2, 3, true) test(2, 5, false) @@ -120,33 +119,33 @@ func TestTeam_AddRepository(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(teamID, repoID int64) { - team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) assert.NoError(t, team.AddRepository(repo)) - db.AssertExistsAndLoadBean(t, &TeamRepo{TeamID: teamID, RepoID: repoID}) - CheckConsistencyFor(t, &Team{ID: teamID}, &Repository{ID: repoID}) + unittest.AssertExistsAndLoadBean(t, &TeamRepo{TeamID: teamID, RepoID: repoID}) + unittest.CheckConsistencyFor(t, &Team{ID: teamID}, &Repository{ID: repoID}) } testSuccess(2, 3) testSuccess(2, 5) - team := db.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) assert.Error(t, team.AddRepository(repo)) - CheckConsistencyFor(t, &Team{ID: 1}, &Repository{ID: 1}) + unittest.CheckConsistencyFor(t, &Team{ID: 1}, &Repository{ID: 1}) } func TestTeam_RemoveRepository(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(teamID, repoID int64) { - team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.NoError(t, team.RemoveRepository(repoID)) - db.AssertNotExistsBean(t, &TeamRepo{TeamID: teamID, RepoID: repoID}) - CheckConsistencyFor(t, &Team{ID: teamID}, &Repository{ID: repoID}) + unittest.AssertNotExistsBean(t, &TeamRepo{TeamID: teamID, RepoID: repoID}) + unittest.CheckConsistencyFor(t, &Team{ID: teamID}, &Repository{ID: repoID}) } testSuccess(2, 3) testSuccess(2, 5) - testSuccess(1, db.NonexistentID) + testSuccess(1, unittest.NonexistentID) } func TestIsUsableTeamName(t *testing.T) { @@ -160,8 +159,8 @@ func TestNewTeam(t *testing.T) { const teamName = "newTeamName" team := &Team{Name: teamName, OrgID: 3} assert.NoError(t, NewTeam(team)) - db.AssertExistsAndLoadBean(t, &Team{Name: teamName}) - CheckConsistencyFor(t, &Team{}, &User{ID: team.OrgID}) + unittest.AssertExistsAndLoadBean(t, &Team{Name: teamName}) + unittest.CheckConsistencyFor(t, &Team{}, &User{ID: team.OrgID}) } func TestGetTeam(t *testing.T) { @@ -178,7 +177,7 @@ func TestGetTeam(t *testing.T) { _, err := GetTeam(3, "nonexistent") assert.Error(t, err) - _, err = GetTeam(db.NonexistentID, "Owners") + _, err = GetTeam(unittest.NonexistentID, "Owners") assert.Error(t, err) } @@ -195,7 +194,7 @@ func TestGetTeamByID(t *testing.T) { testSuccess(3) testSuccess(4) - _, err := GetTeamByID(db.NonexistentID) + _, err := GetTeamByID(unittest.NonexistentID) assert.Error(t, err) } @@ -203,48 +202,48 @@ func TestUpdateTeam(t *testing.T) { // successful update assert.NoError(t, unittest.PrepareTestDatabase()) - team := db.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) team.LowerName = "newname" team.Name = "newName" team.Description = strings.Repeat("A long description!", 100) team.Authorize = AccessModeAdmin assert.NoError(t, UpdateTeam(team, true, false)) - team = db.AssertExistsAndLoadBean(t, &Team{Name: "newName"}).(*Team) + team = unittest.AssertExistsAndLoadBean(t, &Team{Name: "newName"}).(*Team) assert.True(t, strings.HasPrefix(team.Description, "A long description!")) - access := db.AssertExistsAndLoadBean(t, &Access{UserID: 4, RepoID: 3}).(*Access) + access := unittest.AssertExistsAndLoadBean(t, &Access{UserID: 4, RepoID: 3}).(*Access) assert.EqualValues(t, AccessModeAdmin, access.Mode) - CheckConsistencyFor(t, &Team{ID: team.ID}) + unittest.CheckConsistencyFor(t, &Team{ID: team.ID}) } func TestUpdateTeam2(t *testing.T) { // update to already-existing team assert.NoError(t, unittest.PrepareTestDatabase()) - team := db.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) team.LowerName = "owners" team.Name = "Owners" team.Description = strings.Repeat("A long description!", 100) err := UpdateTeam(team, true, false) assert.True(t, IsErrTeamAlreadyExist(err)) - CheckConsistencyFor(t, &Team{ID: team.ID}) + unittest.CheckConsistencyFor(t, &Team{ID: team.ID}) } func TestDeleteTeam(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - team := db.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) assert.NoError(t, DeleteTeam(team)) - db.AssertNotExistsBean(t, &Team{ID: team.ID}) - db.AssertNotExistsBean(t, &TeamRepo{TeamID: team.ID}) - db.AssertNotExistsBean(t, &TeamUser{TeamID: team.ID}) + unittest.AssertNotExistsBean(t, &Team{ID: team.ID}) + unittest.AssertNotExistsBean(t, &TeamRepo{TeamID: team.ID}) + unittest.AssertNotExistsBean(t, &TeamUser{TeamID: team.ID}) // check that team members don't have "leftover" access to repos - user := db.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) accessMode, err := AccessLevel(user, repo) assert.NoError(t, err) assert.True(t, accessMode < AccessModeWrite) @@ -260,25 +259,25 @@ func TestIsTeamMember(t *testing.T) { test(3, 1, 2, true) test(3, 1, 4, false) - test(3, 1, db.NonexistentID, false) + test(3, 1, unittest.NonexistentID, false) test(3, 2, 2, true) test(3, 2, 4, true) - test(3, db.NonexistentID, db.NonexistentID, false) - test(db.NonexistentID, db.NonexistentID, db.NonexistentID, false) + test(3, unittest.NonexistentID, unittest.NonexistentID, false) + test(unittest.NonexistentID, unittest.NonexistentID, unittest.NonexistentID, false) } func TestGetTeamMembers(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(teamID int64) { - team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) members, err := GetTeamMembers(teamID) assert.NoError(t, err) assert.Len(t, members, team.NumMembers) for _, member := range members { - db.AssertExistsAndLoadBean(t, &TeamUser{UID: member.ID, TeamID: teamID}) + unittest.AssertExistsAndLoadBean(t, &TeamUser{UID: member.ID, TeamID: teamID}) } } test(1) @@ -291,12 +290,12 @@ func TestGetUserTeams(t *testing.T) { teams, _, err := SearchTeam(&SearchTeamOptions{UserID: userID}) assert.NoError(t, err) for _, team := range teams { - db.AssertExistsAndLoadBean(t, &TeamUser{TeamID: team.ID, UID: userID}) + unittest.AssertExistsAndLoadBean(t, &TeamUser{TeamID: team.ID, UID: userID}) } } test(2) test(5) - test(db.NonexistentID) + test(unittest.NonexistentID) } func TestGetUserOrgTeams(t *testing.T) { @@ -306,22 +305,22 @@ func TestGetUserOrgTeams(t *testing.T) { assert.NoError(t, err) for _, team := range teams { assert.EqualValues(t, orgID, team.OrgID) - db.AssertExistsAndLoadBean(t, &TeamUser{TeamID: team.ID, UID: userID}) + unittest.AssertExistsAndLoadBean(t, &TeamUser{TeamID: team.ID, UID: userID}) } } test(3, 2) test(3, 4) - test(3, db.NonexistentID) + test(3, unittest.NonexistentID) } func TestAddTeamMember(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(teamID, userID int64) { - team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.NoError(t, AddTeamMember(team, userID)) - db.AssertExistsAndLoadBean(t, &TeamUser{UID: userID, TeamID: teamID}) - CheckConsistencyFor(t, &Team{ID: teamID}, &User{ID: team.OrgID}) + unittest.AssertExistsAndLoadBean(t, &TeamUser{UID: userID, TeamID: teamID}) + unittest.CheckConsistencyFor(t, &Team{ID: teamID}, &User{ID: team.OrgID}) } test(1, 2) test(1, 4) @@ -332,17 +331,17 @@ func TestRemoveTeamMember(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(teamID, userID int64) { - team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.NoError(t, RemoveTeamMember(team, userID)) - db.AssertNotExistsBean(t, &TeamUser{UID: userID, TeamID: teamID}) - CheckConsistencyFor(t, &Team{ID: teamID}) + unittest.AssertNotExistsBean(t, &TeamUser{UID: userID, TeamID: teamID}) + unittest.CheckConsistencyFor(t, &Team{ID: teamID}) } testSuccess(1, 4) testSuccess(2, 2) testSuccess(3, 2) - testSuccess(3, db.NonexistentID) + testSuccess(3, unittest.NonexistentID) - team := db.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) err := RemoveTeamMember(team, 2) assert.True(t, IsErrLastOrgOwner(err)) } @@ -351,13 +350,13 @@ func TestHasTeamRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(teamID, repoID int64, expected bool) { - team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.Equal(t, expected, HasTeamRepo(team.OrgID, teamID, repoID)) } test(1, 1, false) test(1, 3, true) test(1, 5, true) - test(1, db.NonexistentID, false) + test(1, unittest.NonexistentID, false) test(2, 3, true) test(2, 5, false) diff --git a/models/org_test.go b/models/org_test.go index 9557e14edc..2bc4d74fc3 100644 --- a/models/org_test.go +++ b/models/org_test.go @@ -29,7 +29,7 @@ func TestUser_IsOwnedBy(t *testing.T) { {2, 2, false}, // user2 is not an organization {2, 3, false}, } { - org := db.AssertExistsAndLoadBean(t, &User{ID: testCase.OrgID}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: testCase.OrgID}).(*User) isOwner, err := org.IsOwnedBy(testCase.UserID) assert.NoError(t, err) assert.Equal(t, testCase.ExpectedOwner, isOwner) @@ -50,7 +50,7 @@ func TestUser_IsOrgMember(t *testing.T) { {2, 2, false}, // user2 is not an organization {2, 3, false}, } { - org := db.AssertExistsAndLoadBean(t, &User{ID: testCase.OrgID}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: testCase.OrgID}).(*User) isMember, err := org.IsOrgMember(testCase.UserID) assert.NoError(t, err) assert.Equal(t, testCase.ExpectedMember, isMember) @@ -59,7 +59,7 @@ func TestUser_IsOrgMember(t *testing.T) { func TestUser_GetTeam(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) team, err := org.GetTeam("team1") assert.NoError(t, err) assert.Equal(t, org.ID, team.OrgID) @@ -68,26 +68,26 @@ func TestUser_GetTeam(t *testing.T) { _, err = org.GetTeam("does not exist") assert.True(t, IsErrTeamNotExist(err)) - nonOrg := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + nonOrg := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) _, err = nonOrg.GetTeam("team") assert.True(t, IsErrTeamNotExist(err)) } func TestUser_GetOwnerTeam(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) team, err := org.GetOwnerTeam() assert.NoError(t, err) assert.Equal(t, org.ID, team.OrgID) - nonOrg := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + nonOrg := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) _, err = nonOrg.GetOwnerTeam() assert.True(t, IsErrTeamNotExist(err)) } func TestUser_GetTeams(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) assert.NoError(t, org.LoadTeams()) if assert.Len(t, org.Teams, 4) { assert.Equal(t, int64(1), org.Teams[0].ID) @@ -99,7 +99,7 @@ func TestUser_GetTeams(t *testing.T) { func TestUser_GetMembers(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) assert.NoError(t, org.GetMembers()) if assert.Len(t, org.Members, 3) { assert.Equal(t, int64(2), org.Members[0].ID) @@ -110,68 +110,68 @@ func TestUser_GetMembers(t *testing.T) { func TestUser_AddMember(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) // add a user that is not a member - db.AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3}) + unittest.AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3}) prevNumMembers := org.NumMembers assert.NoError(t, org.AddMember(5)) - db.AssertExistsAndLoadBean(t, &OrgUser{UID: 5, OrgID: 3}) - org = db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + unittest.AssertExistsAndLoadBean(t, &OrgUser{UID: 5, OrgID: 3}) + org = unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) assert.Equal(t, prevNumMembers+1, org.NumMembers) // add a user that is already a member - db.AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3}) + unittest.AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3}) prevNumMembers = org.NumMembers assert.NoError(t, org.AddMember(4)) - db.AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3}) - org = db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + unittest.AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3}) + org = unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) assert.Equal(t, prevNumMembers, org.NumMembers) - CheckConsistencyFor(t, &User{}) + unittest.CheckConsistencyFor(t, &User{}) } func TestUser_RemoveMember(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) // remove a user that is a member - db.AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3}) + unittest.AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3}) prevNumMembers := org.NumMembers assert.NoError(t, org.RemoveMember(4)) - db.AssertNotExistsBean(t, &OrgUser{UID: 4, OrgID: 3}) - org = db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + unittest.AssertNotExistsBean(t, &OrgUser{UID: 4, OrgID: 3}) + org = unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) assert.Equal(t, prevNumMembers-1, org.NumMembers) // remove a user that is not a member - db.AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3}) + unittest.AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3}) prevNumMembers = org.NumMembers assert.NoError(t, org.RemoveMember(5)) - db.AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3}) - org = db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + unittest.AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3}) + org = unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) assert.Equal(t, prevNumMembers, org.NumMembers) - CheckConsistencyFor(t, &User{}, &Team{}) + unittest.CheckConsistencyFor(t, &User{}, &Team{}) } func TestUser_RemoveOrgRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) - repo := db.AssertExistsAndLoadBean(t, &Repository{OwnerID: org.ID}).(*Repository) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{OwnerID: org.ID}).(*Repository) // remove a repo that does belong to org - db.AssertExistsAndLoadBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID}) + unittest.AssertExistsAndLoadBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID}) assert.NoError(t, org.RemoveOrgRepo(repo.ID)) - db.AssertNotExistsBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID}) - db.AssertExistsAndLoadBean(t, &Repository{ID: repo.ID}) // repo should still exist + unittest.AssertNotExistsBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID}) + unittest.AssertExistsAndLoadBean(t, &Repository{ID: repo.ID}) // repo should still exist // remove a repo that does not belong to org assert.NoError(t, org.RemoveOrgRepo(repo.ID)) - db.AssertNotExistsBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID}) + unittest.AssertNotExistsBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID}) - assert.NoError(t, org.RemoveOrgRepo(db.NonexistentID)) + assert.NoError(t, org.RemoveOrgRepo(unittest.NonexistentID)) - CheckConsistencyFor(t, + unittest.CheckConsistencyFor(t, &User{ID: org.ID}, &Team{OrgID: org.ID}, &Repository{ID: repo.ID}) @@ -181,62 +181,62 @@ func TestCreateOrganization(t *testing.T) { // successful creation of org assert.NoError(t, unittest.PrepareTestDatabase()) - owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) const newOrgName = "neworg" org := &User{ Name: newOrgName, } - db.AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization}) + unittest.AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization}) assert.NoError(t, CreateOrganization(org, owner)) - org = db.AssertExistsAndLoadBean(t, + org = unittest.AssertExistsAndLoadBean(t, &User{Name: newOrgName, Type: UserTypeOrganization}).(*User) - ownerTeam := db.AssertExistsAndLoadBean(t, + ownerTeam := unittest.AssertExistsAndLoadBean(t, &Team{Name: ownerTeamName, OrgID: org.ID}).(*Team) - db.AssertExistsAndLoadBean(t, &TeamUser{UID: owner.ID, TeamID: ownerTeam.ID}) - CheckConsistencyFor(t, &User{}, &Team{}) + unittest.AssertExistsAndLoadBean(t, &TeamUser{UID: owner.ID, TeamID: ownerTeam.ID}) + unittest.CheckConsistencyFor(t, &User{}, &Team{}) } func TestCreateOrganization2(t *testing.T) { // unauthorized creation of org assert.NoError(t, unittest.PrepareTestDatabase()) - owner := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) const newOrgName = "neworg" org := &User{ Name: newOrgName, } - db.AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization}) + unittest.AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization}) err := CreateOrganization(org, owner) assert.Error(t, err) assert.True(t, IsErrUserNotAllowedCreateOrg(err)) - db.AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization}) - CheckConsistencyFor(t, &User{}, &Team{}) + unittest.AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization}) + unittest.CheckConsistencyFor(t, &User{}, &Team{}) } func TestCreateOrganization3(t *testing.T) { // create org with same name as existent org assert.NoError(t, unittest.PrepareTestDatabase()) - owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - org := &User{Name: "user3"} // should already exist - db.AssertExistsAndLoadBean(t, &User{Name: org.Name}) // sanity check + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + org := &User{Name: "user3"} // should already exist + unittest.AssertExistsAndLoadBean(t, &User{Name: org.Name}) // sanity check err := CreateOrganization(org, owner) assert.Error(t, err) assert.True(t, IsErrUserAlreadyExist(err)) - CheckConsistencyFor(t, &User{}, &Team{}) + unittest.CheckConsistencyFor(t, &User{}, &Team{}) } func TestCreateOrganization4(t *testing.T) { // create org with unusable name assert.NoError(t, unittest.PrepareTestDatabase()) - owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) err := CreateOrganization(&User{Name: "assets"}, owner) assert.Error(t, err) assert.True(t, IsErrNameReserved(err)) - CheckConsistencyFor(t, &User{}, &Team{}) + unittest.CheckConsistencyFor(t, &User{}, &Team{}) } func TestGetOrgByName(t *testing.T) { @@ -263,20 +263,20 @@ func TestCountOrganizations(t *testing.T) { func TestDeleteOrganization(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 6}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 6}).(*User) assert.NoError(t, DeleteOrganization(org)) - db.AssertNotExistsBean(t, &User{ID: 6}) - db.AssertNotExistsBean(t, &OrgUser{OrgID: 6}) - db.AssertNotExistsBean(t, &Team{OrgID: 6}) + unittest.AssertNotExistsBean(t, &User{ID: 6}) + unittest.AssertNotExistsBean(t, &OrgUser{OrgID: 6}) + unittest.AssertNotExistsBean(t, &Team{OrgID: 6}) - org = db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org = unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) err := DeleteOrganization(org) assert.Error(t, err) assert.True(t, IsErrUserOwnRepos(err)) - user := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) assert.Error(t, DeleteOrganization(user)) - CheckConsistencyFor(t, &User{}, &Team{}) + unittest.CheckConsistencyFor(t, &User{}, &Team{}) } func TestIsOrganizationOwner(t *testing.T) { @@ -290,7 +290,7 @@ func TestIsOrganizationOwner(t *testing.T) { test(3, 3, false) test(6, 5, true) test(6, 4, false) - test(db.NonexistentID, db.NonexistentID, false) + test(unittest.NonexistentID, unittest.NonexistentID, false) } func TestIsOrganizationMember(t *testing.T) { @@ -305,7 +305,7 @@ func TestIsOrganizationMember(t *testing.T) { test(3, 4, true) test(6, 5, true) test(6, 4, false) - test(db.NonexistentID, db.NonexistentID, false) + test(unittest.NonexistentID, unittest.NonexistentID, false) } func TestIsPublicMembership(t *testing.T) { @@ -320,7 +320,7 @@ func TestIsPublicMembership(t *testing.T) { test(3, 4, false) test(6, 5, true) test(6, 4, false) - test(db.NonexistentID, db.NonexistentID, false) + test(unittest.NonexistentID, unittest.NonexistentID, false) } func TestGetOrgsByUserID(t *testing.T) { @@ -422,7 +422,7 @@ func TestGetOrgUsersByOrgID(t *testing.T) { orgUsers, err = GetOrgUsersByOrgID(&FindOrgMembersOpts{ ListOptions: db.ListOptions{}, - OrgID: db.NonexistentID, + OrgID: unittest.NonexistentID, PublicOnly: false, }) assert.NoError(t, err) @@ -434,29 +434,29 @@ func TestChangeOrgUserStatus(t *testing.T) { testSuccess := func(orgID, userID int64, public bool) { assert.NoError(t, ChangeOrgUserStatus(orgID, userID, public)) - orgUser := db.AssertExistsAndLoadBean(t, &OrgUser{OrgID: orgID, UID: userID}).(*OrgUser) + orgUser := unittest.AssertExistsAndLoadBean(t, &OrgUser{OrgID: orgID, UID: userID}).(*OrgUser) assert.Equal(t, public, orgUser.IsPublic) } testSuccess(3, 2, false) testSuccess(3, 2, false) testSuccess(3, 4, true) - assert.NoError(t, ChangeOrgUserStatus(db.NonexistentID, db.NonexistentID, true)) + assert.NoError(t, ChangeOrgUserStatus(unittest.NonexistentID, unittest.NonexistentID, true)) } func TestAddOrgUser(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(orgID, userID int64, isPublic bool) { - org := db.AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User) expectedNumMembers := org.NumMembers - if !db.BeanExists(t, &OrgUser{OrgID: orgID, UID: userID}) { + if !unittest.BeanExists(t, &OrgUser{OrgID: orgID, UID: userID}) { expectedNumMembers++ } assert.NoError(t, AddOrgUser(orgID, userID)) ou := &OrgUser{OrgID: orgID, UID: userID} - db.AssertExistsAndLoadBean(t, ou) + unittest.AssertExistsAndLoadBean(t, ou) assert.Equal(t, isPublic, ou.IsPublic) - org = db.AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User) + org = unittest.AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User) assert.EqualValues(t, expectedNumMembers, org.NumMembers) } @@ -468,20 +468,20 @@ func TestAddOrgUser(t *testing.T) { setting.Service.DefaultOrgMemberVisible = true testSuccess(6, 3, true) - CheckConsistencyFor(t, &User{}, &Team{}) + unittest.CheckConsistencyFor(t, &User{}, &Team{}) } func TestRemoveOrgUser(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(orgID, userID int64) { - org := db.AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User) expectedNumMembers := org.NumMembers - if db.BeanExists(t, &OrgUser{OrgID: orgID, UID: userID}) { + if unittest.BeanExists(t, &OrgUser{OrgID: orgID, UID: userID}) { expectedNumMembers-- } assert.NoError(t, RemoveOrgUser(orgID, userID)) - db.AssertNotExistsBean(t, &OrgUser{OrgID: orgID, UID: userID}) - org = db.AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User) + unittest.AssertNotExistsBean(t, &OrgUser{OrgID: orgID, UID: userID}) + org = unittest.AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User) assert.EqualValues(t, expectedNumMembers, org.NumMembers) } testSuccess(3, 4) @@ -490,13 +490,13 @@ func TestRemoveOrgUser(t *testing.T) { err := RemoveOrgUser(7, 5) assert.Error(t, err) assert.True(t, IsErrLastOrgOwner(err)) - db.AssertExistsAndLoadBean(t, &OrgUser{OrgID: 7, UID: 5}) - CheckConsistencyFor(t, &User{}, &Team{}) + unittest.AssertExistsAndLoadBean(t, &OrgUser{OrgID: 7, UID: 5}) + unittest.CheckConsistencyFor(t, &User{}, &Team{}) } func TestUser_GetUserTeamIDs(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) testSuccess := func(userID int64, expected []int64) { teamIDs, err := org.GetUserTeamIDs(userID) assert.NoError(t, err) @@ -504,12 +504,12 @@ func TestUser_GetUserTeamIDs(t *testing.T) { } testSuccess(2, []int64{1, 2}) testSuccess(4, []int64{2}) - testSuccess(db.NonexistentID, []int64{}) + testSuccess(unittest.NonexistentID, []int64{}) } func TestAccessibleReposEnv_CountRepos(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) testSuccess := func(userID, expectedCount int64) { env, err := org.AccessibleReposEnv(userID) assert.NoError(t, err) @@ -523,7 +523,7 @@ func TestAccessibleReposEnv_CountRepos(t *testing.T) { func TestAccessibleReposEnv_RepoIDs(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) testSuccess := func(userID, _, pageSize int64, expectedRepoIDs []int64) { env, err := org.AccessibleReposEnv(userID) assert.NoError(t, err) @@ -537,7 +537,7 @@ func TestAccessibleReposEnv_RepoIDs(t *testing.T) { func TestAccessibleReposEnv_Repos(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) testSuccess := func(userID int64, expectedRepoIDs []int64) { env, err := org.AccessibleReposEnv(userID) assert.NoError(t, err) @@ -545,7 +545,7 @@ func TestAccessibleReposEnv_Repos(t *testing.T) { assert.NoError(t, err) expectedRepos := make([]*Repository, len(expectedRepoIDs)) for i, repoID := range expectedRepoIDs { - expectedRepos[i] = db.AssertExistsAndLoadBean(t, + expectedRepos[i] = unittest.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) } assert.Equal(t, expectedRepos, repos) @@ -556,7 +556,7 @@ func TestAccessibleReposEnv_Repos(t *testing.T) { func TestAccessibleReposEnv_MirrorRepos(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) testSuccess := func(userID int64, expectedRepoIDs []int64) { env, err := org.AccessibleReposEnv(userID) assert.NoError(t, err) @@ -564,7 +564,7 @@ func TestAccessibleReposEnv_MirrorRepos(t *testing.T) { assert.NoError(t, err) expectedRepos := make([]*Repository, len(expectedRepoIDs)) for i, repoID := range expectedRepoIDs { - expectedRepos[i] = db.AssertExistsAndLoadBean(t, + expectedRepos[i] = unittest.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) } assert.Equal(t, expectedRepos, repos) @@ -575,8 +575,8 @@ func TestAccessibleReposEnv_MirrorRepos(t *testing.T) { func TestHasOrgVisibleTypePublic(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - user3 := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user3 := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) const newOrgName = "test-org-public" org := &User{ @@ -584,9 +584,9 @@ func TestHasOrgVisibleTypePublic(t *testing.T) { Visibility: structs.VisibleTypePublic, } - db.AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization}) + unittest.AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization}) assert.NoError(t, CreateOrganization(org, owner)) - org = db.AssertExistsAndLoadBean(t, + org = unittest.AssertExistsAndLoadBean(t, &User{Name: org.Name, Type: UserTypeOrganization}).(*User) test1 := HasOrgOrUserVisible(org, owner) test2 := HasOrgOrUserVisible(org, user3) @@ -598,8 +598,8 @@ func TestHasOrgVisibleTypePublic(t *testing.T) { func TestHasOrgVisibleTypeLimited(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - user3 := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user3 := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) const newOrgName = "test-org-limited" org := &User{ @@ -607,9 +607,9 @@ func TestHasOrgVisibleTypeLimited(t *testing.T) { Visibility: structs.VisibleTypeLimited, } - db.AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization}) + unittest.AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization}) assert.NoError(t, CreateOrganization(org, owner)) - org = db.AssertExistsAndLoadBean(t, + org = unittest.AssertExistsAndLoadBean(t, &User{Name: org.Name, Type: UserTypeOrganization}).(*User) test1 := HasOrgOrUserVisible(org, owner) test2 := HasOrgOrUserVisible(org, user3) @@ -621,8 +621,8 @@ func TestHasOrgVisibleTypeLimited(t *testing.T) { func TestHasOrgVisibleTypePrivate(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - user3 := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user3 := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) const newOrgName = "test-org-private" org := &User{ @@ -630,9 +630,9 @@ func TestHasOrgVisibleTypePrivate(t *testing.T) { Visibility: structs.VisibleTypePrivate, } - db.AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization}) + unittest.AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization}) assert.NoError(t, CreateOrganization(org, owner)) - org = db.AssertExistsAndLoadBean(t, + org = unittest.AssertExistsAndLoadBean(t, &User{Name: org.Name, Type: UserTypeOrganization}).(*User) test1 := HasOrgOrUserVisible(org, owner) test2 := HasOrgOrUserVisible(org, user3) diff --git a/models/pull_test.go b/models/pull_test.go index 54083355ab..0225b421fb 100644 --- a/models/pull_test.go +++ b/models/pull_test.go @@ -15,7 +15,7 @@ import ( func TestPullRequest_LoadAttributes(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) + pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) assert.NoError(t, pr.LoadAttributes()) assert.NotNil(t, pr.Merger) assert.Equal(t, pr.MergerID, pr.Merger.ID) @@ -23,7 +23,7 @@ func TestPullRequest_LoadAttributes(t *testing.T) { func TestPullRequest_LoadIssue(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) + pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) assert.NoError(t, pr.LoadIssue()) assert.NotNil(t, pr.Issue) assert.Equal(t, int64(2), pr.Issue.ID) @@ -34,7 +34,7 @@ func TestPullRequest_LoadIssue(t *testing.T) { func TestPullRequest_LoadBaseRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) + pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) assert.NoError(t, pr.LoadBaseRepo()) assert.NotNil(t, pr.BaseRepo) assert.Equal(t, pr.BaseRepoID, pr.BaseRepo.ID) @@ -45,7 +45,7 @@ func TestPullRequest_LoadBaseRepo(t *testing.T) { func TestPullRequest_LoadHeadRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) + pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) assert.NoError(t, pr.LoadHeadRepo()) assert.NotNil(t, pr.HeadRepo) assert.Equal(t, pr.HeadRepoID, pr.HeadRepo.ID) @@ -167,15 +167,15 @@ func TestGetPullRequestByIssueID(t *testing.T) { func TestPullRequest_Update(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) + pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) pr.BaseBranch = "baseBranch" pr.HeadBranch = "headBranch" pr.Update() - pr = db.AssertExistsAndLoadBean(t, &PullRequest{ID: pr.ID}).(*PullRequest) + pr = unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: pr.ID}).(*PullRequest) assert.Equal(t, "baseBranch", pr.BaseBranch) assert.Equal(t, "headBranch", pr.HeadBranch) - CheckConsistencyFor(t, pr) + unittest.CheckConsistencyFor(t, pr) } func TestPullRequest_UpdateCols(t *testing.T) { @@ -187,18 +187,18 @@ func TestPullRequest_UpdateCols(t *testing.T) { } assert.NoError(t, pr.UpdateCols("head_branch")) - pr = db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) + pr = unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) assert.Equal(t, "master", pr.BaseBranch) assert.Equal(t, "headBranch", pr.HeadBranch) - CheckConsistencyFor(t, pr) + unittest.CheckConsistencyFor(t, pr) } func TestPullRequestList_LoadAttributes(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) prs := []*PullRequest{ - db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest), - db.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest), + unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest), + unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest), } assert.NoError(t, PullRequestList(prs).LoadAttributes()) for _, pr := range prs { @@ -214,7 +214,7 @@ func TestPullRequestList_LoadAttributes(t *testing.T) { func TestPullRequest_IsWorkInProgress(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) + pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) pr.LoadIssue() assert.False(t, pr.IsWorkInProgress()) @@ -229,7 +229,7 @@ func TestPullRequest_IsWorkInProgress(t *testing.T) { func TestPullRequest_GetWorkInProgressPrefixWorkInProgress(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) + pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) pr.LoadIssue() assert.Empty(t, pr.GetWorkInProgressPrefix()) @@ -244,7 +244,7 @@ func TestPullRequest_GetWorkInProgressPrefixWorkInProgress(t *testing.T) { func TestPullRequest_GetDefaultMergeMessage_InternalTracker(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) + pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) assert.Equal(t, "Merge pull request 'issue3' (#3) from branch2 into master", pr.GetDefaultMergeMessage()) @@ -266,7 +266,7 @@ func TestPullRequest_GetDefaultMergeMessage_ExternalTracker(t *testing.T) { baseRepo.Owner = &User{Name: "testOwner"} baseRepo.Units = []*RepoUnit{&externalTracker} - pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 2, BaseRepo: baseRepo}).(*PullRequest) + pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2, BaseRepo: baseRepo}).(*PullRequest) assert.Equal(t, "Merge pull request 'issue3' (!3) from branch2 into master", pr.GetDefaultMergeMessage()) diff --git a/models/repo_collaboration_test.go b/models/repo_collaboration_test.go index 5e6a0a5d94..9b698c0af6 100644 --- a/models/repo_collaboration_test.go +++ b/models/repo_collaboration_test.go @@ -16,11 +16,11 @@ func TestRepository_AddCollaborator(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(repoID, userID int64) { - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) assert.NoError(t, repo.GetOwner()) - user := db.AssertExistsAndLoadBean(t, &User{ID: userID}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: userID}).(*User) assert.NoError(t, repo.AddCollaborator(user)) - CheckConsistencyFor(t, &Repository{ID: repoID}, &User{ID: userID}) + unittest.CheckConsistencyFor(t, &Repository{ID: repoID}, &User{ID: userID}) } testSuccess(1, 4) testSuccess(1, 4) @@ -30,7 +30,7 @@ func TestRepository_AddCollaborator(t *testing.T) { func TestRepository_GetCollaborators(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(repoID int64) { - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) collaborators, err := repo.GetCollaborators(db.ListOptions{}) assert.NoError(t, err) expectedLen, err := db.GetEngine(db.DefaultContext).Count(&Collaboration{RepoID: repoID}) @@ -51,13 +51,13 @@ func TestRepository_IsCollaborator(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(repoID, userID int64, expected bool) { - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) actual, err := repo.IsCollaborator(userID) assert.NoError(t, err) assert.Equal(t, expected, actual) } test(3, 2, true) - test(3, db.NonexistentID, false) + test(3, unittest.NonexistentID, false) test(4, 2, false) test(4, 4, true) } @@ -65,32 +65,32 @@ func TestRepository_IsCollaborator(t *testing.T) { func TestRepository_ChangeCollaborationAccessMode(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) assert.NoError(t, repo.ChangeCollaborationAccessMode(4, AccessModeAdmin)) - collaboration := db.AssertExistsAndLoadBean(t, &Collaboration{RepoID: repo.ID, UserID: 4}).(*Collaboration) + collaboration := unittest.AssertExistsAndLoadBean(t, &Collaboration{RepoID: repo.ID, UserID: 4}).(*Collaboration) assert.EqualValues(t, AccessModeAdmin, collaboration.Mode) - access := db.AssertExistsAndLoadBean(t, &Access{UserID: 4, RepoID: repo.ID}).(*Access) + access := unittest.AssertExistsAndLoadBean(t, &Access{UserID: 4, RepoID: repo.ID}).(*Access) assert.EqualValues(t, AccessModeAdmin, access.Mode) assert.NoError(t, repo.ChangeCollaborationAccessMode(4, AccessModeAdmin)) - assert.NoError(t, repo.ChangeCollaborationAccessMode(db.NonexistentID, AccessModeAdmin)) + assert.NoError(t, repo.ChangeCollaborationAccessMode(unittest.NonexistentID, AccessModeAdmin)) - CheckConsistencyFor(t, &Repository{ID: repo.ID}) + unittest.CheckConsistencyFor(t, &Repository{ID: repo.ID}) } func TestRepository_DeleteCollaboration(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) assert.NoError(t, repo.GetOwner()) assert.NoError(t, repo.DeleteCollaboration(4)) - db.AssertNotExistsBean(t, &Collaboration{RepoID: repo.ID, UserID: 4}) + unittest.AssertNotExistsBean(t, &Collaboration{RepoID: repo.ID, UserID: 4}) assert.NoError(t, repo.DeleteCollaboration(4)) - db.AssertNotExistsBean(t, &Collaboration{RepoID: repo.ID, UserID: 4}) + unittest.AssertNotExistsBean(t, &Collaboration{RepoID: repo.ID, UserID: 4}) - CheckConsistencyFor(t, &Repository{ID: repo.ID}) + unittest.CheckConsistencyFor(t, &Repository{ID: repo.ID}) } diff --git a/models/repo_list_test.go b/models/repo_list_test.go index eaf5e8a7ce..4722b073a3 100644 --- a/models/repo_list_test.go +++ b/models/repo_list_test.go @@ -78,7 +78,7 @@ func TestSearchRepository(t *testing.T) { assert.Len(t, repos, 3) // Test non existing owner - repos, count, err = SearchRepositoryByName(&SearchRepoOptions{OwnerID: db.NonexistentID}) + repos, count, err = SearchRepositoryByName(&SearchRepoOptions{OwnerID: unittest.NonexistentID}) assert.NoError(t, err) assert.Empty(t, repos) diff --git a/models/repo_permission_test.go b/models/repo_permission_test.go index 1f04a45315..b64fe37911 100644 --- a/models/repo_permission_test.go +++ b/models/repo_permission_test.go @@ -17,11 +17,11 @@ func TestRepoPermissionPublicNonOrgRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) // public non-organization repo - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) assert.NoError(t, repo.getUnits(db.GetEngine(db.DefaultContext))) // plain user - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) perm, err := GetUserRepoPermission(repo, user) assert.NoError(t, err) for _, unit := range repo.Units { @@ -39,7 +39,7 @@ func TestRepoPermissionPublicNonOrgRepo(t *testing.T) { } // collaborator - collaborator := db.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) + collaborator := unittest.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) perm, err = GetUserRepoPermission(repo, collaborator) assert.NoError(t, err) for _, unit := range repo.Units { @@ -48,7 +48,7 @@ func TestRepoPermissionPublicNonOrgRepo(t *testing.T) { } // owner - owner := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) perm, err = GetUserRepoPermission(repo, owner) assert.NoError(t, err) for _, unit := range repo.Units { @@ -57,7 +57,7 @@ func TestRepoPermissionPublicNonOrgRepo(t *testing.T) { } // admin - admin := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + admin := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) perm, err = GetUserRepoPermission(repo, admin) assert.NoError(t, err) for _, unit := range repo.Units { @@ -70,11 +70,11 @@ func TestRepoPermissionPrivateNonOrgRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) // private non-organization repo - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) assert.NoError(t, repo.getUnits(db.GetEngine(db.DefaultContext))) // plain user - user := db.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) perm, err := GetUserRepoPermission(repo, user) assert.NoError(t, err) for _, unit := range repo.Units { @@ -100,7 +100,7 @@ func TestRepoPermissionPrivateNonOrgRepo(t *testing.T) { } // owner - owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) perm, err = GetUserRepoPermission(repo, owner) assert.NoError(t, err) for _, unit := range repo.Units { @@ -109,7 +109,7 @@ func TestRepoPermissionPrivateNonOrgRepo(t *testing.T) { } // admin - admin := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + admin := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) perm, err = GetUserRepoPermission(repo, admin) assert.NoError(t, err) for _, unit := range repo.Units { @@ -122,11 +122,11 @@ func TestRepoPermissionPublicOrgRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) // public organization repo - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 32}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 32}).(*Repository) assert.NoError(t, repo.getUnits(db.GetEngine(db.DefaultContext))) // plain user - user := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) perm, err := GetUserRepoPermission(repo, user) assert.NoError(t, err) for _, unit := range repo.Units { @@ -152,7 +152,7 @@ func TestRepoPermissionPublicOrgRepo(t *testing.T) { } // org member team owner - owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) perm, err = GetUserRepoPermission(repo, owner) assert.NoError(t, err) for _, unit := range repo.Units { @@ -161,7 +161,7 @@ func TestRepoPermissionPublicOrgRepo(t *testing.T) { } // org member team tester - member := db.AssertExistsAndLoadBean(t, &User{ID: 15}).(*User) + member := unittest.AssertExistsAndLoadBean(t, &User{ID: 15}).(*User) perm, err = GetUserRepoPermission(repo, member) assert.NoError(t, err) for _, unit := range repo.Units { @@ -171,7 +171,7 @@ func TestRepoPermissionPublicOrgRepo(t *testing.T) { assert.False(t, perm.CanWrite(unit.TypeCode)) // admin - admin := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + admin := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) perm, err = GetUserRepoPermission(repo, admin) assert.NoError(t, err) for _, unit := range repo.Units { @@ -184,11 +184,11 @@ func TestRepoPermissionPrivateOrgRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) // private organization repo - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 24}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 24}).(*Repository) assert.NoError(t, repo.getUnits(db.GetEngine(db.DefaultContext))) // plain user - user := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) perm, err := GetUserRepoPermission(repo, user) assert.NoError(t, err) for _, unit := range repo.Units { @@ -214,7 +214,7 @@ func TestRepoPermissionPrivateOrgRepo(t *testing.T) { } // org member team owner - owner := db.AssertExistsAndLoadBean(t, &User{ID: 15}).(*User) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 15}).(*User) perm, err = GetUserRepoPermission(repo, owner) assert.NoError(t, err) for _, unit := range repo.Units { @@ -223,7 +223,7 @@ func TestRepoPermissionPrivateOrgRepo(t *testing.T) { } // update team information and then check permission - team := db.AssertExistsAndLoadBean(t, &Team{ID: 5}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 5}).(*Team) err = UpdateTeamUnits(team, nil) assert.NoError(t, err) perm, err = GetUserRepoPermission(repo, owner) @@ -234,7 +234,7 @@ func TestRepoPermissionPrivateOrgRepo(t *testing.T) { } // org member team tester - tester := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + tester := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) perm, err = GetUserRepoPermission(repo, tester) assert.NoError(t, err) assert.True(t, perm.CanWrite(unit.TypeIssues)) @@ -242,7 +242,7 @@ func TestRepoPermissionPrivateOrgRepo(t *testing.T) { assert.False(t, perm.CanRead(unit.TypeCode)) // org member team reviewer - reviewer := db.AssertExistsAndLoadBean(t, &User{ID: 20}).(*User) + reviewer := unittest.AssertExistsAndLoadBean(t, &User{ID: 20}).(*User) perm, err = GetUserRepoPermission(repo, reviewer) assert.NoError(t, err) assert.False(t, perm.CanRead(unit.TypeIssues)) @@ -250,7 +250,7 @@ func TestRepoPermissionPrivateOrgRepo(t *testing.T) { assert.True(t, perm.CanRead(unit.TypeCode)) // admin - admin := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + admin := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) perm, err = GetUserRepoPermission(repo, admin) assert.NoError(t, err) for _, unit := range repo.Units { diff --git a/models/repo_redirect_test.go b/models/repo_redirect_test.go index bf42d4854c..a9d3cc1494 100644 --- a/models/repo_redirect_test.go +++ b/models/repo_redirect_test.go @@ -19,7 +19,7 @@ func TestLookupRepoRedirect(t *testing.T) { assert.NoError(t, err) assert.EqualValues(t, 1, repoID) - _, err = LookupRepoRedirect(db.NonexistentID, "doesnotexist") + _, err = LookupRepoRedirect(unittest.NonexistentID, "doesnotexist") assert.True(t, IsErrRepoRedirectNotExist(err)) } @@ -27,15 +27,15 @@ func TestNewRepoRedirect(t *testing.T) { // redirect to a completely new name assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) assert.NoError(t, newRepoRedirect(db.GetEngine(db.DefaultContext), repo.OwnerID, repo.ID, repo.Name, "newreponame")) - db.AssertExistsAndLoadBean(t, &RepoRedirect{ + unittest.AssertExistsAndLoadBean(t, &RepoRedirect{ OwnerID: repo.OwnerID, LowerName: repo.LowerName, RedirectRepoID: repo.ID, }) - db.AssertExistsAndLoadBean(t, &RepoRedirect{ + unittest.AssertExistsAndLoadBean(t, &RepoRedirect{ OwnerID: repo.OwnerID, LowerName: "oldrepo1", RedirectRepoID: repo.ID, @@ -46,15 +46,15 @@ func TestNewRepoRedirect2(t *testing.T) { // redirect to previously used name assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) assert.NoError(t, newRepoRedirect(db.GetEngine(db.DefaultContext), repo.OwnerID, repo.ID, repo.Name, "oldrepo1")) - db.AssertExistsAndLoadBean(t, &RepoRedirect{ + unittest.AssertExistsAndLoadBean(t, &RepoRedirect{ OwnerID: repo.OwnerID, LowerName: repo.LowerName, RedirectRepoID: repo.ID, }) - db.AssertNotExistsBean(t, &RepoRedirect{ + unittest.AssertNotExistsBean(t, &RepoRedirect{ OwnerID: repo.OwnerID, LowerName: "oldrepo1", RedirectRepoID: repo.ID, @@ -65,10 +65,10 @@ func TestNewRepoRedirect3(t *testing.T) { // redirect for a previously-unredirected repo assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) assert.NoError(t, newRepoRedirect(db.GetEngine(db.DefaultContext), repo.OwnerID, repo.ID, repo.Name, "newreponame")) - db.AssertExistsAndLoadBean(t, &RepoRedirect{ + unittest.AssertExistsAndLoadBean(t, &RepoRedirect{ OwnerID: repo.OwnerID, LowerName: repo.LowerName, RedirectRepoID: repo.ID, diff --git a/models/repo_test.go b/models/repo_test.go index 2eb6e817ec..685570adc8 100644 --- a/models/repo_test.go +++ b/models/repo_test.go @@ -138,7 +138,7 @@ func TestGetUserFork(t *testing.T) { func TestRepoAPIURL(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository) assert.Equal(t, "https://try.gitea.io/api/v1/repos/user12/repo10", repo.APIURL()) } @@ -150,7 +150,7 @@ func TestUploadAvatar(t *testing.T) { png.Encode(&buff, myImage) assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository) err := repo.UploadAvatar(buff.Bytes()) assert.NoError(t, err) @@ -164,7 +164,7 @@ func TestUploadBigAvatar(t *testing.T) { png.Encode(&buff, myImage) assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository) err := repo.UploadAvatar(buff.Bytes()) assert.Error(t, err) @@ -177,7 +177,7 @@ func TestDeleteAvatar(t *testing.T) { png.Encode(&buff, myImage) assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository) err := repo.UploadAvatar(buff.Bytes()) assert.NoError(t, err) @@ -198,14 +198,14 @@ func TestRepoGetReviewers(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) // test public repo - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) reviewers, err := repo1.GetReviewers(2, 2) assert.NoError(t, err) assert.Len(t, reviewers, 4) // test private repo - repo2 := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) + repo2 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) reviewers, err = repo2.GetReviewers(2, 2) assert.NoError(t, err) assert.Empty(t, reviewers) @@ -214,12 +214,12 @@ func TestRepoGetReviewers(t *testing.T) { func TestRepoGetReviewerTeams(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo2 := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) + repo2 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) teams, err := repo2.GetReviewerTeams() assert.NoError(t, err) assert.Empty(t, teams) - repo3 := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) + repo3 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) teams, err = repo3.GetReviewerTeams() assert.NoError(t, err) assert.Len(t, teams, 2) diff --git a/models/repo_transfer_test.go b/models/repo_transfer_test.go index 71976def96..df4f83919b 100644 --- a/models/repo_transfer_test.go +++ b/models/repo_transfer_test.go @@ -7,7 +7,6 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -15,8 +14,8 @@ import ( func TestRepositoryTransfer(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - doer := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) + doer := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) transfer, err := GetPendingRepositoryTransfer(repo) assert.NoError(t, err) @@ -30,7 +29,7 @@ func TestRepositoryTransfer(t *testing.T) { assert.Nil(t, transfer) assert.True(t, IsErrNoPendingTransfer(err)) - user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user2 := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) assert.NoError(t, CreatePendingRepositoryTransfer(doer, user2, repo.ID, nil)) @@ -39,7 +38,7 @@ func TestRepositoryTransfer(t *testing.T) { assert.NoError(t, transfer.LoadAttributes()) assert.Equal(t, "user2", transfer.Recipient.Name) - user6 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user6 := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) // Only transfer can be started at any given time err = CreatePendingRepositoryTransfer(doer, user6, repo.ID, nil) diff --git a/models/repo_watch_test.go b/models/repo_watch_test.go index 9f20a088f5..b7efcba71a 100644 --- a/models/repo_watch_test.go +++ b/models/repo_watch_test.go @@ -23,7 +23,7 @@ func TestIsWatching(t *testing.T) { assert.False(t, IsWatching(1, 5)) assert.False(t, IsWatching(8, 1)) - assert.False(t, IsWatching(db.NonexistentID, db.NonexistentID)) + assert.False(t, IsWatching(unittest.NonexistentID, unittest.NonexistentID)) } func TestWatchRepo(t *testing.T) { @@ -32,18 +32,18 @@ func TestWatchRepo(t *testing.T) { const userID = 2 assert.NoError(t, WatchRepo(userID, repoID, true)) - db.AssertExistsAndLoadBean(t, &Watch{RepoID: repoID, UserID: userID}) - CheckConsistencyFor(t, &Repository{ID: repoID}) + unittest.AssertExistsAndLoadBean(t, &Watch{RepoID: repoID, UserID: userID}) + unittest.CheckConsistencyFor(t, &Repository{ID: repoID}) assert.NoError(t, WatchRepo(userID, repoID, false)) - db.AssertNotExistsBean(t, &Watch{RepoID: repoID, UserID: userID}) - CheckConsistencyFor(t, &Repository{ID: repoID}) + unittest.AssertNotExistsBean(t, &Watch{RepoID: repoID, UserID: userID}) + unittest.CheckConsistencyFor(t, &Repository{ID: repoID}) } func TestGetWatchers(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) watches, err := GetWatchers(repo.ID) assert.NoError(t, err) // One watchers are inactive, thus minus 1 @@ -52,7 +52,7 @@ func TestGetWatchers(t *testing.T) { assert.EqualValues(t, repo.ID, watch.RepoID) } - watches, err = GetWatchers(db.NonexistentID) + watches, err = GetWatchers(unittest.NonexistentID) assert.NoError(t, err) assert.Len(t, watches, 0) } @@ -60,15 +60,15 @@ func TestGetWatchers(t *testing.T) { func TestRepository_GetWatchers(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) watchers, err := repo.GetWatchers(db.ListOptions{Page: 1}) assert.NoError(t, err) assert.Len(t, watchers, repo.NumWatches) for _, watcher := range watchers { - db.AssertExistsAndLoadBean(t, &Watch{UserID: watcher.ID, RepoID: repo.ID}) + unittest.AssertExistsAndLoadBean(t, &Watch{UserID: watcher.ID, RepoID: repo.ID}) } - repo = db.AssertExistsAndLoadBean(t, &Repository{ID: 9}).(*Repository) + repo = unittest.AssertExistsAndLoadBean(t, &Repository{ID: 9}).(*Repository) watchers, err = repo.GetWatchers(db.ListOptions{Page: 1}) assert.NoError(t, err) assert.Len(t, watchers, 0) @@ -85,25 +85,25 @@ func TestNotifyWatchers(t *testing.T) { assert.NoError(t, NotifyWatchers(action)) // One watchers are inactive, thus action is only created for user 8, 1, 4, 11 - db.AssertExistsAndLoadBean(t, &Action{ + unittest.AssertExistsAndLoadBean(t, &Action{ ActUserID: action.ActUserID, UserID: 8, RepoID: action.RepoID, OpType: action.OpType, }) - db.AssertExistsAndLoadBean(t, &Action{ + unittest.AssertExistsAndLoadBean(t, &Action{ ActUserID: action.ActUserID, UserID: 1, RepoID: action.RepoID, OpType: action.OpType, }) - db.AssertExistsAndLoadBean(t, &Action{ + unittest.AssertExistsAndLoadBean(t, &Action{ ActUserID: action.ActUserID, UserID: 4, RepoID: action.RepoID, OpType: action.OpType, }) - db.AssertExistsAndLoadBean(t, &Action{ + unittest.AssertExistsAndLoadBean(t, &Action{ ActUserID: action.ActUserID, UserID: 11, RepoID: action.RepoID, @@ -114,7 +114,7 @@ func TestNotifyWatchers(t *testing.T) { func TestWatchIfAuto(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) watchers, err := repo.GetWatchers(db.ListOptions{Page: 1}) assert.NoError(t, err) assert.Len(t, watchers, repo.NumWatches) @@ -171,20 +171,20 @@ func TestWatchIfAuto(t *testing.T) { func TestWatchRepoMode(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - db.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 0) + unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 0) assert.NoError(t, WatchRepoMode(12, 1, RepoWatchModeAuto)) - db.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1) - db.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeAuto}, 1) + unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1) + unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeAuto}, 1) assert.NoError(t, WatchRepoMode(12, 1, RepoWatchModeNormal)) - db.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1) - db.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeNormal}, 1) + unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1) + unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeNormal}, 1) assert.NoError(t, WatchRepoMode(12, 1, RepoWatchModeDont)) - db.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1) - db.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeDont}, 1) + unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1) + unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeDont}, 1) assert.NoError(t, WatchRepoMode(12, 1, RepoWatchModeNone)) - db.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 0) + unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 0) } diff --git a/models/review_test.go b/models/review_test.go index 31d6c45535..2d07ea2ce4 100644 --- a/models/review_test.go +++ b/models/review_test.go @@ -7,7 +7,6 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -26,22 +25,22 @@ func TestGetReviewByID(t *testing.T) { func TestReview_LoadAttributes(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - review := db.AssertExistsAndLoadBean(t, &Review{ID: 1}).(*Review) + review := unittest.AssertExistsAndLoadBean(t, &Review{ID: 1}).(*Review) assert.NoError(t, review.LoadAttributes()) assert.NotNil(t, review.Issue) assert.NotNil(t, review.Reviewer) - invalidReview1 := db.AssertExistsAndLoadBean(t, &Review{ID: 2}).(*Review) + invalidReview1 := unittest.AssertExistsAndLoadBean(t, &Review{ID: 2}).(*Review) assert.Error(t, invalidReview1.LoadAttributes()) - invalidReview2 := db.AssertExistsAndLoadBean(t, &Review{ID: 3}).(*Review) + invalidReview2 := unittest.AssertExistsAndLoadBean(t, &Review{ID: 3}).(*Review) assert.Error(t, invalidReview2.LoadAttributes()) } func TestReview_LoadCodeComments(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - review := db.AssertExistsAndLoadBean(t, &Review{ID: 4}).(*Review) + review := unittest.AssertExistsAndLoadBean(t, &Review{ID: 4}).(*Review) assert.NoError(t, review.LoadAttributes()) assert.NoError(t, review.LoadCodeComments()) assert.Len(t, review.CodeComments, 1) @@ -71,8 +70,8 @@ func TestFindReviews(t *testing.T) { func TestGetCurrentReview(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue) - user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) review, err := GetCurrentReview(user, issue) assert.NoError(t, err) @@ -80,7 +79,7 @@ func TestGetCurrentReview(t *testing.T) { assert.Equal(t, ReviewTypePending, review.Type) assert.Equal(t, "Pending Review", review.Content) - user2 := db.AssertExistsAndLoadBean(t, &User{ID: 7}).(*User) + user2 := unittest.AssertExistsAndLoadBean(t, &User{ID: 7}).(*User) review2, err := GetCurrentReview(user2, issue) assert.Error(t, err) assert.True(t, IsErrReviewNotExist(err)) @@ -90,8 +89,8 @@ func TestGetCurrentReview(t *testing.T) { func TestCreateReview(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue) - user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) review, err := CreateReview(CreateReviewOptions{ Content: "New Review", @@ -101,16 +100,16 @@ func TestCreateReview(t *testing.T) { }) assert.NoError(t, err) assert.Equal(t, "New Review", review.Content) - db.AssertExistsAndLoadBean(t, &Review{Content: "New Review"}) + unittest.AssertExistsAndLoadBean(t, &Review{Content: "New Review"}) } func TestGetReviewersByIssueID(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 3}).(*Issue) - user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - user3 := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) - user4 := db.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 3}).(*Issue) + user2 := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user3 := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + user4 := unittest.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) expectedReviews := []*Review{} expectedReviews = append(expectedReviews, @@ -147,43 +146,43 @@ func TestGetReviewersByIssueID(t *testing.T) { func TestDismissReview(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - rejectReviewExample := db.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) - requestReviewExample := db.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) - approveReviewExample := db.AssertExistsAndLoadBean(t, &Review{ID: 8}).(*Review) + rejectReviewExample := unittest.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) + requestReviewExample := unittest.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) + approveReviewExample := unittest.AssertExistsAndLoadBean(t, &Review{ID: 8}).(*Review) assert.False(t, rejectReviewExample.Dismissed) assert.False(t, requestReviewExample.Dismissed) assert.False(t, approveReviewExample.Dismissed) assert.NoError(t, DismissReview(rejectReviewExample, true)) - rejectReviewExample = db.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) - requestReviewExample = db.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) + rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) + requestReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) assert.True(t, rejectReviewExample.Dismissed) assert.False(t, requestReviewExample.Dismissed) assert.NoError(t, DismissReview(requestReviewExample, true)) - rejectReviewExample = db.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) - requestReviewExample = db.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) + rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) + requestReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) assert.True(t, rejectReviewExample.Dismissed) assert.False(t, requestReviewExample.Dismissed) assert.False(t, approveReviewExample.Dismissed) assert.NoError(t, DismissReview(requestReviewExample, true)) - rejectReviewExample = db.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) - requestReviewExample = db.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) + rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) + requestReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) assert.True(t, rejectReviewExample.Dismissed) assert.False(t, requestReviewExample.Dismissed) assert.False(t, approveReviewExample.Dismissed) assert.NoError(t, DismissReview(requestReviewExample, false)) - rejectReviewExample = db.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) - requestReviewExample = db.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) + rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) + requestReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) assert.True(t, rejectReviewExample.Dismissed) assert.False(t, requestReviewExample.Dismissed) assert.False(t, approveReviewExample.Dismissed) assert.NoError(t, DismissReview(requestReviewExample, false)) - rejectReviewExample = db.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) - requestReviewExample = db.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) + rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) + requestReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) assert.True(t, rejectReviewExample.Dismissed) assert.False(t, requestReviewExample.Dismissed) assert.False(t, approveReviewExample.Dismissed) diff --git a/models/star_test.go b/models/star_test.go index 8e448ae7cf..e131372733 100644 --- a/models/star_test.go +++ b/models/star_test.go @@ -16,13 +16,13 @@ func TestStarRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) const userID = 2 const repoID = 1 - db.AssertNotExistsBean(t, &Star{UID: userID, RepoID: repoID}) + unittest.AssertNotExistsBean(t, &Star{UID: userID, RepoID: repoID}) assert.NoError(t, StarRepo(userID, repoID, true)) - db.AssertExistsAndLoadBean(t, &Star{UID: userID, RepoID: repoID}) + unittest.AssertExistsAndLoadBean(t, &Star{UID: userID, RepoID: repoID}) assert.NoError(t, StarRepo(userID, repoID, true)) - db.AssertExistsAndLoadBean(t, &Star{UID: userID, RepoID: repoID}) + unittest.AssertExistsAndLoadBean(t, &Star{UID: userID, RepoID: repoID}) assert.NoError(t, StarRepo(userID, repoID, false)) - db.AssertNotExistsBean(t, &Star{UID: userID, RepoID: repoID}) + unittest.AssertNotExistsBean(t, &Star{UID: userID, RepoID: repoID}) } func TestIsStaring(t *testing.T) { @@ -34,7 +34,7 @@ func TestIsStaring(t *testing.T) { func TestRepository_GetStargazers(t *testing.T) { // repo with stargazers assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) gazers, err := repo.GetStargazers(db.ListOptions{Page: 0}) assert.NoError(t, err) if assert.Len(t, gazers, 1) { @@ -45,7 +45,7 @@ func TestRepository_GetStargazers(t *testing.T) { func TestRepository_GetStargazers2(t *testing.T) { // repo with stargazers assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) gazers, err := repo.GetStargazers(db.ListOptions{Page: 0}) assert.NoError(t, err) assert.Len(t, gazers, 0) @@ -55,7 +55,7 @@ func TestUser_GetStarredRepos(t *testing.T) { // user who has starred repos assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) starred, err := user.GetStarredRepos(false, 1, 10, "") assert.NoError(t, err) if assert.Len(t, starred, 1) { @@ -74,7 +74,7 @@ func TestUser_GetStarredRepos2(t *testing.T) { // user who has no starred repos assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) starred, err := user.GetStarredRepos(false, 1, 10, "") assert.NoError(t, err) assert.Len(t, starred, 0) @@ -87,7 +87,7 @@ func TestUser_GetStarredRepos2(t *testing.T) { func TestUserGetStarredRepoCount(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) counts, err := user.GetStarredRepoCount(false) assert.NoError(t, err) assert.Equal(t, int64(1), counts) diff --git a/models/token_test.go b/models/token_test.go index 0161764596..191da7820e 100644 --- a/models/token_test.go +++ b/models/token_test.go @@ -7,7 +7,6 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -19,7 +18,7 @@ func TestNewAccessToken(t *testing.T) { Name: "Token C", } assert.NoError(t, NewAccessToken(token)) - db.AssertExistsAndLoadBean(t, token) + unittest.AssertExistsAndLoadBean(t, token) invalidToken := &AccessToken{ ID: token.ID, // duplicate @@ -45,7 +44,7 @@ func TestAccessTokenByNameExists(t *testing.T) { // Save it to the database assert.NoError(t, NewAccessToken(token)) - db.AssertExistsAndLoadBean(t, token) + unittest.AssertExistsAndLoadBean(t, token) // This token must be found by name in the DB now exist, err = AccessTokenByNameExists(token) @@ -112,7 +111,7 @@ func TestUpdateAccessToken(t *testing.T) { token.Name = "Token Z" assert.NoError(t, UpdateAccessToken(token)) - db.AssertExistsAndLoadBean(t, token) + unittest.AssertExistsAndLoadBean(t, token) } func TestDeleteAccessTokenByID(t *testing.T) { @@ -123,7 +122,7 @@ func TestDeleteAccessTokenByID(t *testing.T) { assert.Equal(t, int64(1), token.UID) assert.NoError(t, DeleteAccessTokenByID(token.ID, 1)) - db.AssertNotExistsBean(t, token) + unittest.AssertNotExistsBean(t, token) err = DeleteAccessTokenByID(100, 100) assert.Error(t, err) diff --git a/models/unittest/bridge.go b/models/unittest/bridge.go deleted file mode 100644 index 776dd69519..0000000000 --- a/models/unittest/bridge.go +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright 2021 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package unittest - -import ( - "code.gitea.io/gitea/modules/unittestbridge" - "github.com/stretchr/testify/assert" -) - -// For legacy code only, please refer to the `unittestbridge` package. - -// TestifyAsserter uses "stretchr/testify/assert" to do assert -type TestifyAsserter struct { - t unittestbridge.Tester -} - -// Errorf assert Errorf -func (ta TestifyAsserter) Errorf(format string, args ...interface{}) { - ta.t.Errorf(format, args) -} - -// NoError assert NoError -func (ta TestifyAsserter) NoError(err error, msgAndArgs ...interface{}) bool { - return assert.NoError(ta, err, msgAndArgs...) -} - -// EqualValues assert EqualValues -func (ta TestifyAsserter) EqualValues(expected, actual interface{}, msgAndArgs ...interface{}) bool { - return assert.EqualValues(ta, expected, actual, msgAndArgs...) -} - -// Equal assert Equal -func (ta TestifyAsserter) Equal(expected, actual interface{}, msgAndArgs ...interface{}) bool { - return assert.Equal(ta, expected, actual, msgAndArgs...) -} - -// True assert True -func (ta TestifyAsserter) True(value bool, msgAndArgs ...interface{}) bool { - return assert.True(ta, value, msgAndArgs...) -} - -// False assert False -func (ta TestifyAsserter) False(value bool, msgAndArgs ...interface{}) bool { - return assert.False(ta, value, msgAndArgs...) -} - -// InitUnitTestBridge init the unit test bridge. eg: models.CheckConsistencyFor can use testing and assert frameworks -func InitUnitTestBridge() { - unittestbridge.SetNewAsserterFunc(func(t unittestbridge.Tester) unittestbridge.Asserter { - return &TestifyAsserter{t: t} - }) -} diff --git a/models/unittest/consistency.go b/models/unittest/consistency.go new file mode 100644 index 0000000000..2645084d3e --- /dev/null +++ b/models/unittest/consistency.go @@ -0,0 +1,190 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package unittest + +import ( + "reflect" + "strconv" + "strings" + + "code.gitea.io/gitea/models/db" + + "github.com/stretchr/testify/assert" + "xorm.io/builder" +) + +const ( + // these const values are copied from `models` package to prevent from cycle-import + modelsUserTypeOrganization = 1 + modelsRepoWatchModeDont = 2 + modelsCommentTypeComment = 0 +) + +var consistencyCheckMap = make(map[string]func(t assert.TestingT, bean interface{})) + +// CheckConsistencyFor test that all matching database entries are consistent +func CheckConsistencyFor(t assert.TestingT, beansToCheck ...interface{}) { + for _, bean := range beansToCheck { + sliceType := reflect.SliceOf(reflect.TypeOf(bean)) + sliceValue := reflect.MakeSlice(sliceType, 0, 10) + + ptrToSliceValue := reflect.New(sliceType) + ptrToSliceValue.Elem().Set(sliceValue) + + assert.NoError(t, db.GetEngine(db.DefaultContext).Table(bean).Find(ptrToSliceValue.Interface())) + sliceValue = ptrToSliceValue.Elem() + + for i := 0; i < sliceValue.Len(); i++ { + entity := sliceValue.Index(i).Interface() + checkForConsistency(t, entity) + } + } +} + +func checkForConsistency(t assert.TestingT, bean interface{}) { + tb, err := db.TableInfo(bean) + assert.NoError(t, err) + f := consistencyCheckMap[tb.Name] + if f == nil { + assert.Fail(t, "unknown bean type: %#v", bean) + return + } + f(t, bean) +} + +func init() { + parseBool := func(v string) bool { + b, _ := strconv.ParseBool(v) + return b + } + parseInt := func(v string) int { + i, _ := strconv.Atoi(v) + return i + } + + checkForUserConsistency := func(t assert.TestingT, bean interface{}) { + user := reflectionWrap(bean) + AssertCountByCond(t, "repository", builder.Eq{"owner_id": user.int("ID")}, user.int("NumRepos")) + AssertCountByCond(t, "star", builder.Eq{"uid": user.int("ID")}, user.int("NumStars")) + AssertCountByCond(t, "org_user", builder.Eq{"org_id": user.int("ID")}, user.int("NumMembers")) + AssertCountByCond(t, "team", builder.Eq{"org_id": user.int("ID")}, user.int("NumTeams")) + AssertCountByCond(t, "follow", builder.Eq{"user_id": user.int("ID")}, user.int("NumFollowing")) + AssertCountByCond(t, "follow", builder.Eq{"follow_id": user.int("ID")}, user.int("NumFollowers")) + if user.int("Type") != modelsUserTypeOrganization { + assert.EqualValues(t, 0, user.int("NumMembers")) + assert.EqualValues(t, 0, user.int("NumTeams")) + } + } + + checkForRepoConsistency := func(t assert.TestingT, bean interface{}) { + repo := reflectionWrap(bean) + assert.Equal(t, repo.str("LowerName"), strings.ToLower(repo.str("Name")), "repo: %+v", repo) + AssertCountByCond(t, "star", builder.Eq{"repo_id": repo.int("ID")}, repo.int("NumStars")) + AssertCountByCond(t, "milestone", builder.Eq{"repo_id": repo.int("ID")}, repo.int("NumMilestones")) + AssertCountByCond(t, "repository", builder.Eq{"fork_id": repo.int("ID")}, repo.int("NumForks")) + if repo.bool("IsFork") { + AssertExistsAndLoadMap(t, "repository", builder.Eq{"id": repo.int("ForkID")}) + } + + actual := GetCountByCond(t, "watch", builder.Eq{"repo_id": repo.int("ID")}. + And(builder.Neq{"mode": modelsRepoWatchModeDont})) + assert.EqualValues(t, repo.int("NumWatches"), actual, + "Unexpected number of watches for repo %+v", repo) + + actual = GetCountByCond(t, "issue", builder.Eq{"is_pull": false, "repo_id": repo.int("ID")}) + assert.EqualValues(t, repo.int("NumIssues"), actual, + "Unexpected number of issues for repo %+v", repo) + + actual = GetCountByCond(t, "issue", builder.Eq{"is_pull": false, "is_closed": true, "repo_id": repo.int("ID")}) + assert.EqualValues(t, repo.int("NumClosedIssues"), actual, + "Unexpected number of closed issues for repo %+v", repo) + + actual = GetCountByCond(t, "issue", builder.Eq{"is_pull": true, "repo_id": repo.int("ID")}) + assert.EqualValues(t, repo.int("NumPulls"), actual, + "Unexpected number of pulls for repo %+v", repo) + + actual = GetCountByCond(t, "issue", builder.Eq{"is_pull": true, "is_closed": true, "repo_id": repo.int("ID")}) + assert.EqualValues(t, repo.int("NumClosedPulls"), actual, + "Unexpected number of closed pulls for repo %+v", repo) + + actual = GetCountByCond(t, "milestone", builder.Eq{"is_closed": true, "repo_id": repo.int("ID")}) + assert.EqualValues(t, repo.int("NumClosedMilestones"), actual, + "Unexpected number of closed milestones for repo %+v", repo) + } + + checkForIssueConsistency := func(t assert.TestingT, bean interface{}) { + issue := reflectionWrap(bean) + typeComment := modelsCommentTypeComment + actual := GetCountByCond(t, "comment", builder.Eq{"`type`": typeComment, "issue_id": issue.int("ID")}) + assert.EqualValues(t, issue.int("NumComments"), actual, "Unexpected number of comments for issue %+v", issue) + if issue.bool("IsPull") { + prRow := AssertExistsAndLoadMap(t, "pull_request", builder.Eq{"issue_id": issue.int("ID")}) + assert.EqualValues(t, parseInt(prRow["index"]), issue.int("Index")) + } + } + + checkForPullRequestConsistency := func(t assert.TestingT, bean interface{}) { + pr := reflectionWrap(bean) + issueRow := AssertExistsAndLoadMap(t, "issue", builder.Eq{"id": pr.int("IssueID")}) + assert.True(t, parseBool(issueRow["is_pull"])) + assert.EqualValues(t, parseInt(issueRow["index"]), pr.int("Index")) + } + + checkForMilestoneConsistency := func(t assert.TestingT, bean interface{}) { + milestone := reflectionWrap(bean) + AssertCountByCond(t, "issue", builder.Eq{"milestone_id": milestone.int("ID")}, milestone.int("NumIssues")) + + actual := GetCountByCond(t, "issue", builder.Eq{"is_closed": true, "milestone_id": milestone.int("ID")}) + assert.EqualValues(t, milestone.int("NumClosedIssues"), actual, "Unexpected number of closed issues for milestone %+v", milestone) + + completeness := 0 + if milestone.int("NumIssues") > 0 { + completeness = milestone.int("NumClosedIssues") * 100 / milestone.int("NumIssues") + } + assert.Equal(t, completeness, milestone.int("Completeness")) + } + + checkForLabelConsistency := func(t assert.TestingT, bean interface{}) { + label := reflectionWrap(bean) + issueLabels, err := db.GetEngine(db.DefaultContext).Table("issue_label"). + Where(builder.Eq{"label_id": label.int("ID")}). + Query() + assert.NoError(t, err) + + assert.EqualValues(t, label.int("NumIssues"), len(issueLabels), "Unexpected number of issue for label %+v", label) + + issueIDs := make([]int, len(issueLabels)) + for i, issueLabel := range issueLabels { + issueIDs[i], _ = strconv.Atoi(string(issueLabel["issue_id"])) + } + + expected := int64(0) + if len(issueIDs) > 0 { + expected = GetCountByCond(t, "issue", builder.In("id", issueIDs).And(builder.Eq{"is_closed": true})) + } + assert.EqualValues(t, expected, label.int("NumClosedIssues"), "Unexpected number of closed issues for label %+v", label) + } + + checkForTeamConsistency := func(t assert.TestingT, bean interface{}) { + team := reflectionWrap(bean) + AssertCountByCond(t, "team_user", builder.Eq{"team_id": team.int("ID")}, team.int("NumMembers")) + AssertCountByCond(t, "team_repo", builder.Eq{"team_id": team.int("ID")}, team.int("NumRepos")) + } + + checkForActionConsistency := func(t assert.TestingT, bean interface{}) { + action := reflectionWrap(bean) + repoRow := AssertExistsAndLoadMap(t, "repository", builder.Eq{"id": action.int("RepoID")}) + assert.Equal(t, parseBool(repoRow["is_private"]), action.bool("IsPrivate"), "action: %+v", action) + } + + consistencyCheckMap["user"] = checkForUserConsistency + consistencyCheckMap["repository"] = checkForRepoConsistency + consistencyCheckMap["issue"] = checkForIssueConsistency + consistencyCheckMap["pull_request"] = checkForPullRequestConsistency + consistencyCheckMap["milestone"] = checkForMilestoneConsistency + consistencyCheckMap["label"] = checkForLabelConsistency + consistencyCheckMap["team"] = checkForTeamConsistency + consistencyCheckMap["action"] = checkForActionConsistency +} diff --git a/models/unittest/reflection.go b/models/unittest/reflection.go new file mode 100644 index 0000000000..68c312ac9e --- /dev/null +++ b/models/unittest/reflection.go @@ -0,0 +1,41 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package unittest + +import ( + "log" + "reflect" +) + +func fieldByName(v reflect.Value, field string) reflect.Value { + if v.Kind() == reflect.Ptr { + v = v.Elem() + } + f := v.FieldByName(field) + if !f.IsValid() { + log.Panicf("can not read %s for %v", field, v) + } + return f +} + +type reflectionValue struct { + v reflect.Value +} + +func reflectionWrap(v interface{}) *reflectionValue { + return &reflectionValue{v: reflect.ValueOf(v)} +} + +func (rv *reflectionValue) int(field string) int { + return int(fieldByName(rv.v, field).Int()) +} + +func (rv *reflectionValue) str(field string) string { + return fieldByName(rv.v, field).String() +} + +func (rv *reflectionValue) bool(field string) bool { + return fieldByName(rv.v, field).Bool() +} diff --git a/models/unittest/testdb.go b/models/unittest/testdb.go index b4f8c813af..8771bc1d21 100644 --- a/models/unittest/testdb.go +++ b/models/unittest/testdb.go @@ -18,7 +18,6 @@ import ( "code.gitea.io/gitea/modules/util" "github.com/stretchr/testify/assert" - "xorm.io/xorm" "xorm.io/xorm/names" ) @@ -43,7 +42,7 @@ func fatalTestError(fmtStr string, args ...interface{}) { // test database. Creates the test database, and sets necessary settings. func MainTest(m *testing.M, pathToGiteaRoot string, fixtureFiles ...string) { var err error - InitUnitTestBridge() + giteaRoot = pathToGiteaRoot fixturesDir = filepath.Join(pathToGiteaRoot, "models", "fixtures") @@ -125,7 +124,7 @@ func CreateTestEngine(opts FixturesOptions) error { return err } x.SetMapper(names.GonicMapper{}) - db.SetUnitTestEngine(x) + db.SetEngine(x) if err = db.SyncAllTables(); err != nil { return err diff --git a/models/unittest/unit_tests.go b/models/unittest/unit_tests.go new file mode 100644 index 0000000000..6c20c2781b --- /dev/null +++ b/models/unittest/unit_tests.go @@ -0,0 +1,139 @@ +// Copyright 2016 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package unittest + +import ( + "math" + + "code.gitea.io/gitea/models/db" + + "github.com/stretchr/testify/assert" + "xorm.io/builder" +) + +// Code in this file is mainly used by unittest.CheckConsistencyFor, which is not in the unit test for various reasons. +// In the future if we can decouple CheckConsistencyFor into separate unit test code, then this file can be moved into unittest package too. + +// NonexistentID an ID that will never exist +const NonexistentID = int64(math.MaxInt64) + +type testCond struct { + query interface{} + args []interface{} +} + +// Cond create a condition with arguments for a test +func Cond(query interface{}, args ...interface{}) interface{} { + return &testCond{query: query, args: args} +} + +func whereConditions(e db.Engine, conditions []interface{}) db.Engine { + for _, condition := range conditions { + switch cond := condition.(type) { + case *testCond: + e = e.Where(cond.query, cond.args...) + default: + e = e.Where(cond) + } + } + return e +} + +// LoadBeanIfExists loads beans from fixture database if exist +func LoadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) { + e := db.GetEngine(db.DefaultContext) + return whereConditions(e, conditions).Get(bean) +} + +// BeanExists for testing, check if a bean exists +func BeanExists(t assert.TestingT, bean interface{}, conditions ...interface{}) bool { + exists, err := LoadBeanIfExists(bean, conditions...) + assert.NoError(t, err) + return exists +} + +// AssertExistsAndLoadBean assert that a bean exists and load it from the test database +func AssertExistsAndLoadBean(t assert.TestingT, bean interface{}, conditions ...interface{}) interface{} { + exists, err := LoadBeanIfExists(bean, conditions...) + assert.NoError(t, err) + assert.True(t, exists, + "Expected to find %+v (of type %T, with conditions %+v), but did not", + bean, bean, conditions) + return bean +} + +// AssertExistsAndLoadMap assert that a row exists and load it from the test database +func AssertExistsAndLoadMap(t assert.TestingT, table string, conditions ...interface{}) map[string]string { + e := db.GetEngine(db.DefaultContext).Table(table) + res, err := whereConditions(e, conditions).Query() + assert.NoError(t, err) + assert.True(t, len(res) == 1, + "Expected to find one row in %s (with conditions %+v), but found %d", + table, conditions, len(res), + ) + + if len(res) == 1 { + rec := map[string]string{} + for k, v := range res[0] { + rec[k] = string(v) + } + return rec + } + return nil +} + +// GetCount get the count of a bean +func GetCount(t assert.TestingT, bean interface{}, conditions ...interface{}) int { + e := db.GetEngine(db.DefaultContext) + count, err := whereConditions(e, conditions).Count(bean) + assert.NoError(t, err) + return int(count) +} + +// AssertNotExistsBean assert that a bean does not exist in the test database +func AssertNotExistsBean(t assert.TestingT, bean interface{}, conditions ...interface{}) { + exists, err := LoadBeanIfExists(bean, conditions...) + assert.NoError(t, err) + assert.False(t, exists) +} + +// AssertExistsIf asserts that a bean exists or does not exist, depending on +// what is expected. +func AssertExistsIf(t assert.TestingT, expected bool, bean interface{}, conditions ...interface{}) { + exists, err := LoadBeanIfExists(bean, conditions...) + assert.NoError(t, err) + assert.Equal(t, expected, exists) +} + +// AssertSuccessfulInsert assert that beans is successfully inserted +func AssertSuccessfulInsert(t assert.TestingT, beans ...interface{}) { + err := db.Insert(db.DefaultContext, beans...) + assert.NoError(t, err) +} + +// AssertCount assert the count of a bean +func AssertCount(t assert.TestingT, bean, expected interface{}) { + assert.EqualValues(t, expected, GetCount(t, bean)) +} + +// AssertInt64InRange assert value is in range [low, high] +func AssertInt64InRange(t assert.TestingT, low, high, value int64) { + assert.True(t, value >= low && value <= high, + "Expected value in range [%d, %d], found %d", low, high, value) +} + +// GetCountByCond get the count of database entries matching bean +func GetCountByCond(t assert.TestingT, tableName string, cond builder.Cond) int64 { + e := db.GetEngine(db.DefaultContext) + count, err := e.Table(tableName).Where(cond).Count() + assert.NoError(t, err) + return count +} + +// AssertCountByCond test the count of database entries matching bean +func AssertCountByCond(t assert.TestingT, tableName string, cond builder.Cond, expected int) { + assert.EqualValues(t, expected, GetCountByCond(t, tableName, cond), + "Failed consistency test, the counted bean (of table %s) was %+v", tableName, cond) +} diff --git a/models/user_follow_test.go b/models/user_follow_test.go index c7e56410f0..5ba922728a 100644 --- a/models/user_follow_test.go +++ b/models/user_follow_test.go @@ -7,7 +7,6 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -16,9 +15,9 @@ func TestIsFollowing(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) assert.True(t, IsFollowing(4, 2)) assert.False(t, IsFollowing(2, 4)) - assert.False(t, IsFollowing(5, db.NonexistentID)) - assert.False(t, IsFollowing(db.NonexistentID, 5)) - assert.False(t, IsFollowing(db.NonexistentID, db.NonexistentID)) + assert.False(t, IsFollowing(5, unittest.NonexistentID)) + assert.False(t, IsFollowing(unittest.NonexistentID, 5)) + assert.False(t, IsFollowing(unittest.NonexistentID, unittest.NonexistentID)) } func TestFollowUser(t *testing.T) { @@ -26,14 +25,14 @@ func TestFollowUser(t *testing.T) { testSuccess := func(followerID, followedID int64) { assert.NoError(t, FollowUser(followerID, followedID)) - db.AssertExistsAndLoadBean(t, &Follow{UserID: followerID, FollowID: followedID}) + unittest.AssertExistsAndLoadBean(t, &Follow{UserID: followerID, FollowID: followedID}) } testSuccess(4, 2) testSuccess(5, 2) assert.NoError(t, FollowUser(2, 2)) - CheckConsistencyFor(t, &User{}) + unittest.CheckConsistencyFor(t, &User{}) } func TestUnfollowUser(t *testing.T) { @@ -41,11 +40,11 @@ func TestUnfollowUser(t *testing.T) { testSuccess := func(followerID, followedID int64) { assert.NoError(t, UnfollowUser(followerID, followedID)) - db.AssertNotExistsBean(t, &Follow{UserID: followerID, FollowID: followedID}) + unittest.AssertNotExistsBean(t, &Follow{UserID: followerID, FollowID: followedID}) } testSuccess(4, 2) testSuccess(5, 2) testSuccess(2, 2) - CheckConsistencyFor(t, &User{}) + unittest.CheckConsistencyFor(t, &User{}) } diff --git a/models/user_heatmap_test.go b/models/user_heatmap_test.go index 85e678e933..2abbbfc6f3 100644 --- a/models/user_heatmap_test.go +++ b/models/user_heatmap_test.go @@ -9,7 +9,6 @@ import ( "testing" "time" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/timeutil" @@ -47,10 +46,10 @@ func TestGetUserHeatmapDataByUser(t *testing.T) { defer timeutil.Unset() for i, tc := range testCases { - user := db.AssertExistsAndLoadBean(t, &User{ID: tc.userID}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: tc.userID}).(*User) doer := &User{ID: tc.doerID} - _, err := db.LoadBeanIfExists(doer) + _, err := unittest.LoadBeanIfExists(doer) assert.NoError(t, err) if tc.doerID == 0 { doer = nil diff --git a/models/user_test.go b/models/user_test.go index 9a91a9b257..dc273ce1bc 100644 --- a/models/user_test.go +++ b/models/user_test.go @@ -23,7 +23,7 @@ import ( func TestOAuth2Application_LoadUser(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - app := db.AssertExistsAndLoadBean(t, &login.OAuth2Application{ID: 1}).(*login.OAuth2Application) + app := unittest.AssertExistsAndLoadBean(t, &login.OAuth2Application{ID: 1}).(*login.OAuth2Application) user, err := GetUserByID(app.UID) assert.NoError(t, err) assert.NotNil(t, user) @@ -94,10 +94,10 @@ func TestGetUserEmailsByNames(t *testing.T) { func TestCanCreateOrganization(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - admin := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + admin := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) assert.True(t, admin.CanCreateOrganization()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) assert.True(t, user.CanCreateOrganization()) // Disable user create organization permission. user.AllowCreateOrganization = false @@ -180,7 +180,7 @@ func TestSearchUsers(t *testing.T) { func TestDeleteUser(t *testing.T) { test := func(userID int64) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: userID}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: userID}).(*User) ownedRepos := make([]*Repository, 0, 10) assert.NoError(t, db.GetEngine(db.DefaultContext).Find(&ownedRepos, &Repository{OwnerID: userID})) @@ -200,15 +200,15 @@ func TestDeleteUser(t *testing.T) { } } assert.NoError(t, DeleteUser(user)) - db.AssertNotExistsBean(t, &User{ID: userID}) - CheckConsistencyFor(t, &User{}, &Repository{}) + unittest.AssertNotExistsBean(t, &User{ID: userID}) + unittest.CheckConsistencyFor(t, &User{}, &Repository{}) } test(2) test(4) test(8) test(11) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) assert.Error(t, DeleteUser(org)) } @@ -229,7 +229,7 @@ func TestEmailNotificationPreferences(t *testing.T) { {EmailNotificationsEnabled, 8}, {EmailNotificationsOnMention, 9}, } { - user := db.AssertExistsAndLoadBean(t, &User{ID: test.userID}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: test.userID}).(*User) assert.Equal(t, test.expected, user.EmailNotifications()) // Try all possible settings @@ -282,9 +282,9 @@ func BenchmarkHashPassword(b *testing.B) { func TestGetOrgRepositoryIDs(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - user4 := db.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) - user5 := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) + user2 := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user4 := unittest.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) + user5 := unittest.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) accessibleRepos, err := user2.GetOrgRepositoryIDs() assert.NoError(t, err) @@ -429,7 +429,7 @@ func TestGetMaileableUsersByIDs(t *testing.T) { func TestAddLdapSSHPublicKeys(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) s := &login.Source{ID: 1} testCases := []struct { @@ -496,18 +496,18 @@ ssh-dss AAAAB3NzaC1kc3MAAACBAOChCC7lf6Uo9n7BmZ6M8St19PZf4Tn59NriyboW2x/DZuYAz3ib func TestUpdateUser(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) user.KeepActivityPrivate = true assert.NoError(t, UpdateUser(user)) - user = db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user = unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) assert.True(t, user.KeepActivityPrivate) setting.Service.AllowedUserVisibilityModesSlice = []bool{true, false, false} user.KeepActivityPrivate = false user.Visibility = structs.VisibleTypePrivate assert.Error(t, UpdateUser(user)) - user = db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user = unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) assert.True(t, user.KeepActivityPrivate) user.Email = "no mail@mail.org" @@ -518,14 +518,14 @@ func TestNewUserRedirect(t *testing.T) { // redirect to a completely new name assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) assert.NoError(t, user_model.NewUserRedirect(db.DefaultContext, user.ID, user.Name, "newusername")) - db.AssertExistsAndLoadBean(t, &user_model.Redirect{ + unittest.AssertExistsAndLoadBean(t, &user_model.Redirect{ LowerName: user.LowerName, RedirectUserID: user.ID, }) - db.AssertExistsAndLoadBean(t, &user_model.Redirect{ + unittest.AssertExistsAndLoadBean(t, &user_model.Redirect{ LowerName: "olduser1", RedirectUserID: user.ID, }) @@ -535,14 +535,14 @@ func TestNewUserRedirect2(t *testing.T) { // redirect to previously used name assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) assert.NoError(t, user_model.NewUserRedirect(db.DefaultContext, user.ID, user.Name, "olduser1")) - db.AssertExistsAndLoadBean(t, &user_model.Redirect{ + unittest.AssertExistsAndLoadBean(t, &user_model.Redirect{ LowerName: user.LowerName, RedirectUserID: user.ID, }) - db.AssertNotExistsBean(t, &user_model.Redirect{ + unittest.AssertNotExistsBean(t, &user_model.Redirect{ LowerName: "olduser1", RedirectUserID: user.ID, }) @@ -552,10 +552,10 @@ func TestNewUserRedirect3(t *testing.T) { // redirect for a previously-unredirected user assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) assert.NoError(t, user_model.NewUserRedirect(db.DefaultContext, user.ID, user.Name, "newusername")) - db.AssertExistsAndLoadBean(t, &user_model.Redirect{ + unittest.AssertExistsAndLoadBean(t, &user_model.Redirect{ LowerName: user.LowerName, RedirectUserID: user.ID, }) diff --git a/models/webhook/webhook_test.go b/models/webhook/webhook_test.go index 6d7957ba64..d1a76795fd 100644 --- a/models/webhook/webhook_test.go +++ b/models/webhook/webhook_test.go @@ -31,14 +31,14 @@ func TestIsValidHookContentType(t *testing.T) { func TestWebhook_History(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - webhook := db.AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook) + webhook := unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook) tasks, err := webhook.History(0) assert.NoError(t, err) if assert.Len(t, tasks, 1) { assert.Equal(t, int64(1), tasks[0].ID) } - webhook = db.AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook) + webhook = unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook) tasks, err = webhook.History(0) assert.NoError(t, err) assert.Len(t, tasks, 0) @@ -46,7 +46,7 @@ func TestWebhook_History(t *testing.T) { func TestWebhook_UpdateEvent(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - webhook := db.AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook) + webhook := unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook) hookEvent := &HookEvent{ PushOnly: true, SendEverything: false, @@ -92,9 +92,9 @@ func TestCreateWebhook(t *testing.T) { ContentType: ContentTypeJSON, Events: `{"push_only":false,"send_everything":false,"choose_events":false,"events":{"create":false,"push":true,"pull_request":true}}`, } - db.AssertNotExistsBean(t, hook) + unittest.AssertNotExistsBean(t, hook) assert.NoError(t, CreateWebhook(db.DefaultContext, hook)) - db.AssertExistsAndLoadBean(t, hook) + unittest.AssertExistsAndLoadBean(t, hook) } func TestGetWebhookByRepoID(t *testing.T) { @@ -103,7 +103,7 @@ func TestGetWebhookByRepoID(t *testing.T) { assert.NoError(t, err) assert.Equal(t, int64(1), hook.ID) - _, err = GetWebhookByRepoID(db.NonexistentID, db.NonexistentID) + _, err = GetWebhookByRepoID(unittest.NonexistentID, unittest.NonexistentID) assert.Error(t, err) assert.True(t, IsErrWebhookNotExist(err)) } @@ -114,7 +114,7 @@ func TestGetWebhookByOrgID(t *testing.T) { assert.NoError(t, err) assert.Equal(t, int64(3), hook.ID) - _, err = GetWebhookByOrgID(db.NonexistentID, db.NonexistentID) + _, err = GetWebhookByOrgID(unittest.NonexistentID, unittest.NonexistentID) assert.Error(t, err) assert.True(t, IsErrWebhookNotExist(err)) } @@ -161,32 +161,32 @@ func TestGetWebhooksByOrgID(t *testing.T) { func TestUpdateWebhook(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - hook := db.AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook) + hook := unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook) hook.IsActive = true hook.ContentType = ContentTypeForm - db.AssertNotExistsBean(t, hook) + unittest.AssertNotExistsBean(t, hook) assert.NoError(t, UpdateWebhook(hook)) - db.AssertExistsAndLoadBean(t, hook) + unittest.AssertExistsAndLoadBean(t, hook) } func TestDeleteWebhookByRepoID(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - db.AssertExistsAndLoadBean(t, &Webhook{ID: 2, RepoID: 1}) + unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 2, RepoID: 1}) assert.NoError(t, DeleteWebhookByRepoID(1, 2)) - db.AssertNotExistsBean(t, &Webhook{ID: 2, RepoID: 1}) + unittest.AssertNotExistsBean(t, &Webhook{ID: 2, RepoID: 1}) - err := DeleteWebhookByRepoID(db.NonexistentID, db.NonexistentID) + err := DeleteWebhookByRepoID(unittest.NonexistentID, unittest.NonexistentID) assert.Error(t, err) assert.True(t, IsErrWebhookNotExist(err)) } func TestDeleteWebhookByOrgID(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - db.AssertExistsAndLoadBean(t, &Webhook{ID: 3, OrgID: 3}) + unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 3, OrgID: 3}) assert.NoError(t, DeleteWebhookByOrgID(3, 3)) - db.AssertNotExistsBean(t, &Webhook{ID: 3, OrgID: 3}) + unittest.AssertNotExistsBean(t, &Webhook{ID: 3, OrgID: 3}) - err := DeleteWebhookByOrgID(db.NonexistentID, db.NonexistentID) + err := DeleteWebhookByOrgID(unittest.NonexistentID, unittest.NonexistentID) assert.Error(t, err) assert.True(t, IsErrWebhookNotExist(err)) } @@ -199,7 +199,7 @@ func TestHookTasks(t *testing.T) { assert.Equal(t, int64(1), hookTasks[0].ID) } - hookTasks, err = HookTasks(db.NonexistentID, 1) + hookTasks, err = HookTasks(unittest.NonexistentID, 1) assert.NoError(t, err) assert.Len(t, hookTasks, 0) } @@ -211,21 +211,21 @@ func TestCreateHookTask(t *testing.T) { HookID: 3, Payloader: &api.PushPayload{}, } - db.AssertNotExistsBean(t, hookTask) + unittest.AssertNotExistsBean(t, hookTask) assert.NoError(t, CreateHookTask(hookTask)) - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) } func TestUpdateHookTask(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - hook := db.AssertExistsAndLoadBean(t, &HookTask{ID: 1}).(*HookTask) + hook := unittest.AssertExistsAndLoadBean(t, &HookTask{ID: 1}).(*HookTask) hook.PayloadContent = "new payload content" hook.DeliveredString = "new delivered string" hook.IsDelivered = true - db.AssertNotExistsBean(t, hook) + unittest.AssertNotExistsBean(t, hook) assert.NoError(t, UpdateHookTask(hook)) - db.AssertExistsAndLoadBean(t, hook) + unittest.AssertExistsAndLoadBean(t, hook) } func TestCleanupHookTaskTable_PerWebhook_DeletesDelivered(t *testing.T) { @@ -237,12 +237,12 @@ func TestCleanupHookTaskTable_PerWebhook_DeletesDelivered(t *testing.T) { IsDelivered: true, Delivered: time.Now().UnixNano(), } - db.AssertNotExistsBean(t, hookTask) + unittest.AssertNotExistsBean(t, hookTask) assert.NoError(t, CreateHookTask(hookTask)) - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) assert.NoError(t, CleanupHookTaskTable(context.Background(), PerWebhook, 168*time.Hour, 0)) - db.AssertNotExistsBean(t, hookTask) + unittest.AssertNotExistsBean(t, hookTask) } func TestCleanupHookTaskTable_PerWebhook_LeavesUndelivered(t *testing.T) { @@ -253,12 +253,12 @@ func TestCleanupHookTaskTable_PerWebhook_LeavesUndelivered(t *testing.T) { Payloader: &api.PushPayload{}, IsDelivered: false, } - db.AssertNotExistsBean(t, hookTask) + unittest.AssertNotExistsBean(t, hookTask) assert.NoError(t, CreateHookTask(hookTask)) - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) assert.NoError(t, CleanupHookTaskTable(context.Background(), PerWebhook, 168*time.Hour, 0)) - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) } func TestCleanupHookTaskTable_PerWebhook_LeavesMostRecentTask(t *testing.T) { @@ -270,12 +270,12 @@ func TestCleanupHookTaskTable_PerWebhook_LeavesMostRecentTask(t *testing.T) { IsDelivered: true, Delivered: time.Now().UnixNano(), } - db.AssertNotExistsBean(t, hookTask) + unittest.AssertNotExistsBean(t, hookTask) assert.NoError(t, CreateHookTask(hookTask)) - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) assert.NoError(t, CleanupHookTaskTable(context.Background(), PerWebhook, 168*time.Hour, 1)) - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) } func TestCleanupHookTaskTable_OlderThan_DeletesDelivered(t *testing.T) { @@ -287,12 +287,12 @@ func TestCleanupHookTaskTable_OlderThan_DeletesDelivered(t *testing.T) { IsDelivered: true, Delivered: time.Now().AddDate(0, 0, -8).UnixNano(), } - db.AssertNotExistsBean(t, hookTask) + unittest.AssertNotExistsBean(t, hookTask) assert.NoError(t, CreateHookTask(hookTask)) - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) assert.NoError(t, CleanupHookTaskTable(context.Background(), OlderThan, 168*time.Hour, 0)) - db.AssertNotExistsBean(t, hookTask) + unittest.AssertNotExistsBean(t, hookTask) } func TestCleanupHookTaskTable_OlderThan_LeavesUndelivered(t *testing.T) { @@ -303,12 +303,12 @@ func TestCleanupHookTaskTable_OlderThan_LeavesUndelivered(t *testing.T) { Payloader: &api.PushPayload{}, IsDelivered: false, } - db.AssertNotExistsBean(t, hookTask) + unittest.AssertNotExistsBean(t, hookTask) assert.NoError(t, CreateHookTask(hookTask)) - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) assert.NoError(t, CleanupHookTaskTable(context.Background(), OlderThan, 168*time.Hour, 0)) - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) } func TestCleanupHookTaskTable_OlderThan_LeavesTaskEarlierThanAgeToDelete(t *testing.T) { @@ -320,10 +320,10 @@ func TestCleanupHookTaskTable_OlderThan_LeavesTaskEarlierThanAgeToDelete(t *test IsDelivered: true, Delivered: time.Now().AddDate(0, 0, -6).UnixNano(), } - db.AssertNotExistsBean(t, hookTask) + unittest.AssertNotExistsBean(t, hookTask) assert.NoError(t, CreateHookTask(hookTask)) - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) assert.NoError(t, CleanupHookTaskTable(context.Background(), OlderThan, 168*time.Hour, 0)) - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) } diff --git a/models/wiki_test.go b/models/wiki_test.go index 2114425872..ae6b090db5 100644 --- a/models/wiki_test.go +++ b/models/wiki_test.go @@ -8,7 +8,6 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" @@ -18,7 +17,7 @@ import ( func TestRepository_WikiCloneLink(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) cloneLink := repo.WikiCloneLink() assert.Equal(t, "ssh://runuser@try.gitea.io:3000/user2/repo1.wiki.git", cloneLink.SSH) assert.Equal(t, "https://try.gitea.io/user2/repo1.wiki.git", cloneLink.HTTPS) @@ -32,15 +31,15 @@ func TestWikiPath(t *testing.T) { func TestRepository_WikiPath(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) expected := filepath.Join(setting.RepoRootPath, "user2/repo1.wiki.git") assert.Equal(t, expected, repo.WikiPath()) } func TestRepository_HasWiki(t *testing.T) { unittest.PrepareTestEnv(t) - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) assert.True(t, repo1.HasWiki()) - repo2 := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) + repo2 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) assert.False(t, repo2.HasWiki()) } |