diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2021-11-12 22:36:47 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-12 22:36:47 +0800 |
commit | df64fa486555de6f403a795fd16c2e9e1d59e535 (patch) | |
tree | b899e9b9e5d57409b1bf0e3afbd606b6a3900235 | |
parent | 7f802631c54d2e91301158380b273b872d62bd80 (diff) | |
download | gitea-df64fa486555de6f403a795fd16c2e9e1d59e535.tar.gz gitea-df64fa486555de6f403a795fd16c2e9e1d59e535.zip |
Decouple unit test code from business code (#17623)
136 files changed, 1057 insertions, 829 deletions
diff --git a/contrib/fixtures/fixture_generation.go b/contrib/fixtures/fixture_generation.go index 5e7dd39a78..74996a1f35 100644 --- a/contrib/fixtures/fixture_generation.go +++ b/contrib/fixtures/fixture_generation.go @@ -10,7 +10,7 @@ import ( "path/filepath" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) // To generate derivative fixtures, execute the following from Gitea's repository base dir: @@ -31,13 +31,13 @@ var ( func main() { pathToGiteaRoot := "." fixturesDir = filepath.Join(pathToGiteaRoot, "models", "fixtures") - if err := db.CreateTestEngine(db.FixturesOptions{ + if err := unittest.CreateTestEngine(unittest.FixturesOptions{ Dir: fixturesDir, }); err != nil { fmt.Printf("CreateTestEngine: %+v", err) os.Exit(1) } - if err := db.PrepareTestDatabase(); err != nil { + if err := unittest.PrepareTestDatabase(); err != nil { fmt.Printf("PrepareTestDatabase: %+v\n", err) os.Exit(1) } diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index 89d92c8526..1e2a9714e3 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -26,6 +26,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" gitea_git "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/external" @@ -99,8 +100,8 @@ func runPR() { }) db.HasEngine = true //x.ShowSQL(true) - err = db.InitFixtures( - db.FixturesOptions{ + err = unittest.InitFixtures( + unittest.FixturesOptions{ Dir: path.Join(curDir, "models/fixtures/"), }, ) @@ -108,7 +109,7 @@ func runPR() { fmt.Printf("Error initializing test database: %v\n", err) os.Exit(1) } - db.LoadFixtures() + unittest.LoadFixtures() util.RemoveAll(setting.RepoRootPath) util.RemoveAll(models.LocalCopyPath()) util.CopyDir(path.Join(curDir, "integrations/gitea-repositories-meta"), setting.RepoRootPath) diff --git a/integrations/api_issue_label_test.go b/integrations/api_issue_label_test.go index 4508069e17..f9c297a18e 100644 --- a/integrations/api_issue_label_test.go +++ b/integrations/api_issue_label_test.go @@ -10,6 +10,8 @@ import ( "strings" "testing" + "code.gitea.io/gitea/models/unittest" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" api "code.gitea.io/gitea/modules/structs" @@ -18,7 +20,7 @@ import ( ) func TestAPIModifyLabels(t *testing.T) { - assert.NoError(t, db.LoadFixtures()) + assert.NoError(t, unittest.LoadFixtures()) repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) @@ -88,7 +90,7 @@ func TestAPIModifyLabels(t *testing.T) { } func TestAPIAddIssueLabels(t *testing.T) { - assert.NoError(t, db.LoadFixtures()) + assert.NoError(t, unittest.LoadFixtures()) repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) issue := db.AssertExistsAndLoadBean(t, &models.Issue{RepoID: repo.ID}).(*models.Issue) @@ -111,7 +113,7 @@ func TestAPIAddIssueLabels(t *testing.T) { } func TestAPIReplaceIssueLabels(t *testing.T) { - assert.NoError(t, db.LoadFixtures()) + assert.NoError(t, unittest.LoadFixtures()) repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) issue := db.AssertExistsAndLoadBean(t, &models.Issue{RepoID: repo.ID}).(*models.Issue) @@ -137,7 +139,7 @@ func TestAPIReplaceIssueLabels(t *testing.T) { } func TestAPIModifyOrgLabels(t *testing.T) { - assert.NoError(t, db.LoadFixtures()) + assert.NoError(t, unittest.LoadFixtures()) repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) diff --git a/integrations/integration_test.go b/integrations/integration_test.go index 1429893270..69c4ca4ae5 100644 --- a/integrations/integration_test.go +++ b/integrations/integration_test.go @@ -25,7 +25,7 @@ import ( "time" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/graceful" @@ -84,6 +84,7 @@ func NewNilResponseHashSumRecorder() *NilResponseHashSumRecorder { func TestMain(m *testing.M) { defer log.Close() + unittest.InitUnitTestBridge() managerCtx, cancel := context.WithCancel(context.Background()) graceful.InitManager(managerCtx) defer cancel() @@ -112,8 +113,8 @@ func TestMain(m *testing.M) { } } - err := db.InitFixtures( - db.FixturesOptions{ + err := unittest.InitFixtures( + unittest.FixturesOptions{ Dir: filepath.Join(filepath.Dir(setting.AppPath), "models/fixtures/"), }, ) @@ -250,7 +251,7 @@ func prepareTestEnv(t testing.TB, skip ...int) func() { ourSkip += skip[0] } deferFn := PrintCurrentTest(t, ourSkip) - assert.NoError(t, db.LoadFixtures()) + assert.NoError(t, unittest.LoadFixtures()) assert.NoError(t, util.RemoveAll(setting.RepoRootPath)) assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath)) @@ -527,7 +528,7 @@ func GetCSRF(t testing.TB, session *TestSession, urlStr string) string { // within a single test this is required func resetFixtures(t *testing.T) { assert.NoError(t, queue.GetManager().FlushAll(context.Background(), -1)) - assert.NoError(t, db.LoadFixtures()) + assert.NoError(t, unittest.LoadFixtures()) assert.NoError(t, util.RemoveAll(setting.RepoRootPath)) assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath)) } diff --git a/integrations/migrate_test.go b/integrations/migrate_test.go index c0d5d4fc75..b9fcfb2827 100644 --- a/integrations/migrate_test.go +++ b/integrations/migrate_test.go @@ -8,6 +8,8 @@ import ( "os" "testing" + "code.gitea.io/gitea/models/unittest" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/migrations" @@ -17,7 +19,7 @@ import ( ) func TestMigrateLocalPath(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) adminUser := db.AssertExistsAndLoadBean(t, &models.User{Name: "user1"}).(*models.User) diff --git a/integrations/repofiles_delete_test.go b/integrations/repofiles_delete_test.go index 22fef4b03e..8150673e2b 100644 --- a/integrations/repofiles_delete_test.go +++ b/integrations/repofiles_delete_test.go @@ -8,8 +8,9 @@ import ( "net/url" "testing" + "code.gitea.io/gitea/models/unittest" + "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/repofiles" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/test" @@ -67,7 +68,7 @@ func TestDeleteRepoFile(t *testing.T) { func testDeleteRepoFile(t *testing.T, u *url.URL) { // setup - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) @@ -106,7 +107,7 @@ func TestDeleteRepoFileWithoutBranchNames(t *testing.T) { func testDeleteRepoFileWithoutBranchNames(t *testing.T, u *url.URL) { // setup - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) @@ -136,7 +137,7 @@ func testDeleteRepoFileWithoutBranchNames(t *testing.T, u *url.URL) { func TestDeleteRepoFileErrors(t *testing.T) { // setup - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) diff --git a/models/access_test.go b/models/access_test.go index 2f641bb9b5..af799ebcde 100644 --- a/models/access_test.go +++ b/models/access_test.go @@ -8,11 +8,12 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestAccessLevel(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) user5 := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) @@ -63,7 +64,7 @@ func TestAccessLevel(t *testing.T) { } func TestHasAccess(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user1 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) user2 := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) @@ -89,7 +90,7 @@ func TestHasAccess(t *testing.T) { } func TestUser_GetRepositoryAccesses(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) accesses, err := user1.GetRepositoryAccesses() @@ -103,7 +104,7 @@ func TestUser_GetRepositoryAccesses(t *testing.T) { } func TestUser_GetAccessibleRepositories(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) repos, err := user1.GetAccessibleRepositories(0) @@ -123,7 +124,7 @@ func TestUser_GetAccessibleRepositories(t *testing.T) { func TestRepository_RecalculateAccesses(t *testing.T) { // test with organization repo - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) assert.NoError(t, repo1.GetOwner()) @@ -140,7 +141,7 @@ func TestRepository_RecalculateAccesses(t *testing.T) { func TestRepository_RecalculateAccesses2(t *testing.T) { // test with non-organization repo - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) assert.NoError(t, repo1.GetOwner()) @@ -154,7 +155,7 @@ func TestRepository_RecalculateAccesses2(t *testing.T) { } func TestRepository_RecalculateAccesses3(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) team5 := db.AssertExistsAndLoadBean(t, &Team{ID: 5}).(*Team) user29 := db.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User) diff --git a/models/action_test.go b/models/action_test.go index 78090e3aad..85b8290cb2 100644 --- a/models/action_test.go +++ b/models/action_test.go @@ -9,13 +9,14 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" "github.com/stretchr/testify/assert" ) func TestAction_GetRepoPath(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &Repository{}).(*Repository) owner := db.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User) action := &Action{RepoID: repo.ID} @@ -23,7 +24,7 @@ func TestAction_GetRepoPath(t *testing.T) { } func TestAction_GetRepoLink(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &Repository{}).(*Repository) owner := db.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User) action := &Action{RepoID: repo.ID} @@ -34,7 +35,7 @@ func TestAction_GetRepoLink(t *testing.T) { func TestGetFeeds(t *testing.T) { // test with an individual user - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) actions, err := GetFeeds(GetFeedsOptions{ @@ -62,7 +63,7 @@ func TestGetFeeds(t *testing.T) { func TestGetFeeds2(t *testing.T) { // test with an organization user - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) diff --git a/models/admin_test.go b/models/admin_test.go index 316ed5cb68..ea060f9726 100644 --- a/models/admin_test.go +++ b/models/admin_test.go @@ -8,6 +8,7 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -20,7 +21,7 @@ func TestNotice_TrStr(t *testing.T) { } func TestCreateNotice(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) noticeBean := &Notice{ Type: NoticeRepository, @@ -32,7 +33,7 @@ func TestCreateNotice(t *testing.T) { } func TestCreateRepositoryNotice(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) noticeBean := &Notice{ Type: NoticeRepository, @@ -46,12 +47,12 @@ func TestCreateRepositoryNotice(t *testing.T) { // TODO TestRemoveAllWithNotice func TestCountNotices(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) assert.Equal(t, int64(3), CountNotices()) } func TestNotices(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) notices, err := Notices(1, 2) assert.NoError(t, err) @@ -68,7 +69,7 @@ func TestNotices(t *testing.T) { } func TestDeleteNotice(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) db.AssertExistsAndLoadBean(t, &Notice{ID: 3}) assert.NoError(t, DeleteNotice(3)) @@ -77,7 +78,7 @@ func TestDeleteNotice(t *testing.T) { func TestDeleteNotices(t *testing.T) { // delete a non-empty range - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) db.AssertExistsAndLoadBean(t, &Notice{ID: 1}) db.AssertExistsAndLoadBean(t, &Notice{ID: 2}) @@ -90,7 +91,7 @@ func TestDeleteNotices(t *testing.T) { func TestDeleteNotices2(t *testing.T) { // delete an empty range - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) db.AssertExistsAndLoadBean(t, &Notice{ID: 1}) db.AssertExistsAndLoadBean(t, &Notice{ID: 2}) @@ -102,7 +103,7 @@ func TestDeleteNotices2(t *testing.T) { } func TestDeleteNoticesByIDs(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) db.AssertExistsAndLoadBean(t, &Notice{ID: 1}) db.AssertExistsAndLoadBean(t, &Notice{ID: 2}) diff --git a/models/attachment_test.go b/models/attachment_test.go index c7f456341c..bc8114fa67 100644 --- a/models/attachment_test.go +++ b/models/attachment_test.go @@ -9,11 +9,12 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unit" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestIncreaseDownloadCount(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) attachment, err := GetAttachmentByUUID("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11") assert.NoError(t, err) @@ -29,7 +30,7 @@ func TestIncreaseDownloadCount(t *testing.T) { } func TestGetByCommentOrIssueID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // count of attachments from issue ID attachments, err := GetAttachmentsByIssueID(1) @@ -42,7 +43,7 @@ func TestGetByCommentOrIssueID(t *testing.T) { } func TestDeleteAttachments(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) count, err := DeleteAttachmentsByIssue(4, false) assert.NoError(t, err) @@ -62,7 +63,7 @@ func TestDeleteAttachments(t *testing.T) { } func TestGetAttachmentByID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) attach, err := GetAttachmentByID(1) assert.NoError(t, err) @@ -78,7 +79,7 @@ func TestAttachment_DownloadURL(t *testing.T) { } func TestUpdateAttachment(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) attach, err := GetAttachmentByID(1) assert.NoError(t, err) @@ -91,7 +92,7 @@ func TestUpdateAttachment(t *testing.T) { } func TestGetAttachmentsByUUIDs(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) attachList, err := GetAttachmentsByUUIDs(db.DefaultContext, []string{"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a17", "not-existing-uuid"}) assert.NoError(t, err) @@ -103,7 +104,7 @@ func TestGetAttachmentsByUUIDs(t *testing.T) { } func TestLinkedRepository(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) testCases := []struct { name string attachID int64 diff --git a/models/branches_test.go b/models/branches_test.go index e9a32666f9..d7233fba65 100644 --- a/models/branches_test.go +++ b/models/branches_test.go @@ -8,11 +8,12 @@ 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, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) firstBranch := db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) @@ -21,7 +22,7 @@ func TestAddDeletedBranch(t *testing.T) { } func TestGetDeletedBranches(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) branches, err := repo.GetDeletedBranches() @@ -30,14 +31,14 @@ func TestGetDeletedBranches(t *testing.T) { } func TestGetDeletedBranch(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) firstBranch := db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) assert.NotNil(t, getDeletedBranch(t, firstBranch)) } func TestDeletedBranchLoadUser(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) firstBranch := db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) secondBranch := db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 2}).(*DeletedBranch) @@ -56,7 +57,7 @@ func TestDeletedBranchLoadUser(t *testing.T) { } func TestRemoveDeletedBranch(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) firstBranch := db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) @@ -81,7 +82,7 @@ func getDeletedBranch(t *testing.T, branch *DeletedBranch) *DeletedBranch { } func TestFindRenamedBranch(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) branch, exist, err := FindRenamedBranch(1, "dev") assert.NoError(t, err) assert.Equal(t, true, exist) @@ -93,7 +94,7 @@ func TestFindRenamedBranch(t *testing.T) { } func TestRenameBranch(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) _isDefault := false @@ -130,7 +131,7 @@ func TestRenameBranch(t *testing.T) { } func TestOnlyGetDeletedBranchOnCorrectRepo(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // Get deletedBranch with ID of 1 on repo with ID 2. // This should return a nil branch as this deleted branch diff --git a/models/commit_status_test.go b/models/commit_status_test.go index 7f4709144c..b113346a41 100644 --- a/models/commit_status_test.go +++ b/models/commit_status_test.go @@ -8,12 +8,13 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" ) func TestGetCommitStatuses(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) diff --git a/models/consistency.go b/models/consistency.go index ab81456916..64c6b240a2 100644 --- a/models/consistency.go +++ b/models/consistency.go @@ -7,29 +7,16 @@ package models import ( "reflect" "strings" - "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/modules/unittestbridge" - "github.com/stretchr/testify/assert" "xorm.io/builder" ) -// CheckConsistencyForAll test that the entire database is consistent -func CheckConsistencyForAll(t *testing.T) { - CheckConsistencyFor(t, - &User{}, - &Repository{}, - &Issue{}, - &PullRequest{}, - &Milestone{}, - &Label{}, - &Team{}, - &Action{}) -} - // CheckConsistencyFor test that all matching database entries are consistent -func CheckConsistencyFor(t *testing.T, beansToCheck ...interface{}) { +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) @@ -37,133 +24,133 @@ func CheckConsistencyFor(t *testing.T, beansToCheck ...interface{}) { ptrToSliceValue := reflect.New(sliceType) ptrToSliceValue.Elem().Set(sliceValue) - assert.NoError(t, db.GetEngine(db.DefaultContext).Table(bean).Find(ptrToSliceValue.Interface())) + 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(entity, t) + checkForConsistency(ta, entity) } } } -func checkForConsistency(bean interface{}, t *testing.T) { +func checkForConsistency(ta unittestbridge.Asserter, bean interface{}) { switch b := bean.(type) { case *User: - checkForUserConsistency(b, t) + checkForUserConsistency(b, ta) case *Repository: - checkForRepoConsistency(b, t) + checkForRepoConsistency(b, ta) case *Issue: - checkForIssueConsistency(b, t) + checkForIssueConsistency(b, ta) case *PullRequest: - checkForPullRequestConsistency(b, t) + checkForPullRequestConsistency(b, ta) case *Milestone: - checkForMilestoneConsistency(b, t) + checkForMilestoneConsistency(b, ta) case *Label: - checkForLabelConsistency(b, t) + checkForLabelConsistency(b, ta) case *Team: - checkForTeamConsistency(b, t) + checkForTeamConsistency(b, ta) case *Action: - checkForActionConsistency(b, t) + checkForActionConsistency(b, ta) default: - t.Errorf("unknown bean type: %#v", bean) + ta.Errorf("unknown bean type: %#v", bean) } } // getCount get the count of database entries matching bean -func getCount(t *testing.T, e db.Engine, bean interface{}) int64 { +func getCount(ta unittestbridge.Asserter, e db.Engine, bean interface{}) int64 { count, err := e.Count(bean) - assert.NoError(t, err) + ta.NoError(err) return count } // assertCount test the count of database entries matching bean -func assertCount(t *testing.T, bean interface{}, expected int) { - assert.EqualValues(t, expected, getCount(t, db.GetEngine(db.DefaultContext), 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, t *testing.T) { - assertCount(t, &Repository{OwnerID: user.ID}, user.NumRepos) - assertCount(t, &Star{UID: user.ID}, user.NumStars) - assertCount(t, &OrgUser{OrgID: user.ID}, user.NumMembers) - assertCount(t, &Team{OrgID: user.ID}, user.NumTeams) - assertCount(t, &Follow{UserID: user.ID}, user.NumFollowing) - assertCount(t, &Follow{FollowID: user.ID}, user.NumFollowers) +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 { - assert.EqualValues(t, 0, user.NumMembers) - assert.EqualValues(t, 0, user.NumTeams) + ta.EqualValues(0, user.NumMembers) + ta.EqualValues(0, user.NumTeams) } } -func checkForRepoConsistency(repo *Repository, t *testing.T) { - assert.Equal(t, repo.LowerName, strings.ToLower(repo.Name), "repo: %+v", repo) - assertCount(t, &Star{RepoID: repo.ID}, repo.NumStars) - assertCount(t, &Milestone{RepoID: repo.ID}, repo.NumMilestones) - assertCount(t, &Repository{ForkID: repo.ID}, repo.NumForks) +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(t, &Repository{ID: repo.ForkID}) + db.AssertExistsAndLoadBean(ta, &Repository{ID: repo.ForkID}) } - actual := getCount(t, db.GetEngine(db.DefaultContext).Where("Mode<>?", RepoWatchModeDont), &Watch{RepoID: repo.ID}) - assert.EqualValues(t, repo.NumWatches, actual, + 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(t, db.GetEngine(db.DefaultContext).Where("is_pull=?", false), &Issue{RepoID: repo.ID}) - assert.EqualValues(t, repo.NumIssues, actual, + 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(t, db.GetEngine(db.DefaultContext).Where("is_pull=? AND is_closed=?", false, true), &Issue{RepoID: repo.ID}) - assert.EqualValues(t, repo.NumClosedIssues, actual, + 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(t, db.GetEngine(db.DefaultContext).Where("is_pull=?", true), &Issue{RepoID: repo.ID}) - assert.EqualValues(t, repo.NumPulls, actual, + 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(t, db.GetEngine(db.DefaultContext).Where("is_pull=? AND is_closed=?", true, true), &Issue{RepoID: repo.ID}) - assert.EqualValues(t, repo.NumClosedPulls, actual, + 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(t, db.GetEngine(db.DefaultContext).Where("is_closed=?", true), &Milestone{RepoID: repo.ID}) - assert.EqualValues(t, repo.NumClosedMilestones, actual, + 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, t *testing.T) { - actual := getCount(t, db.GetEngine(db.DefaultContext).Where("type=?", CommentTypeComment), &Comment{IssueID: issue.ID}) - assert.EqualValues(t, issue.NumComments, actual, +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(t, &PullRequest{IssueID: issue.ID}).(*PullRequest) - assert.EqualValues(t, pr.Index, issue.Index) + pr := db.AssertExistsAndLoadBean(ta, &PullRequest{IssueID: issue.ID}).(*PullRequest) + ta.EqualValues(pr.Index, issue.Index) } } -func checkForPullRequestConsistency(pr *PullRequest, t *testing.T) { - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: pr.IssueID}).(*Issue) - assert.True(t, issue.IsPull) - assert.EqualValues(t, issue.Index, pr.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, t *testing.T) { - assertCount(t, &Issue{MilestoneID: milestone.ID}, milestone.NumIssues) +func checkForMilestoneConsistency(milestone *Milestone, ta unittestbridge.Asserter) { + assertCount(ta, &Issue{MilestoneID: milestone.ID}, milestone.NumIssues) - actual := getCount(t, db.GetEngine(db.DefaultContext).Where("is_closed=?", true), &Issue{MilestoneID: milestone.ID}) - assert.EqualValues(t, milestone.NumClosedIssues, actual, + 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 } - assert.Equal(t, completeness, milestone.Completeness) + ta.Equal(completeness, milestone.Completeness) } -func checkForLabelConsistency(label *Label, t *testing.T) { +func checkForLabelConsistency(label *Label, ta unittestbridge.Asserter) { issueLabels := make([]*IssueLabel, 0, 10) - assert.NoError(t, db.GetEngine(db.DefaultContext).Find(&issueLabels, &IssueLabel{LabelID: label.ID})) - assert.EqualValues(t, label.NumIssues, len(issueLabels), + 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)) @@ -173,20 +160,20 @@ func checkForLabelConsistency(label *Label, t *testing.T) { expected := int64(0) if len(issueIDs) > 0 { - expected = getCount(t, db.GetEngine(db.DefaultContext).In("id", issueIDs).Where("is_closed=?", true), &Issue{}) + expected = getCount(ta, db.GetEngine(db.DefaultContext).In("id", issueIDs).Where("is_closed=?", true), &Issue{}) } - assert.EqualValues(t, expected, label.NumClosedIssues, + ta.EqualValues(expected, label.NumClosedIssues, "Unexpected number of closed issues for label %+v", label) } -func checkForTeamConsistency(team *Team, t *testing.T) { - assertCount(t, &TeamUser{TeamID: team.ID}, team.NumMembers) - assertCount(t, &TeamRepo{TeamID: team.ID}, team.NumRepos) +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, t *testing.T) { - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: action.RepoID}).(*Repository) - assert.Equal(t, repo.IsPrivate, action.IsPrivate, "action: %+v", action) +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 diff --git a/models/consistency_test.go b/models/consistency_test.go index 8332b5d761..6995f47c87 100644 --- a/models/consistency_test.go +++ b/models/consistency_test.go @@ -8,11 +8,12 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestDeleteOrphanedObjects(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) countBefore, err := db.GetEngine(db.DefaultContext).Count(&PullRequest{}) assert.NoError(t, err) diff --git a/models/db/engine.go b/models/db/engine.go index d5f2470a7a..d1b279e016 100755 --- a/models/db/engine.go +++ b/models/db/engine.go @@ -124,7 +124,8 @@ func NewEngine() (*xorm.Engine, error) { return engine, nil } -func syncTables() error { +//SyncAllTables sync the schemas of all tables, is required by unit test code +func SyncAllTables() error { return x.StoreEngine("InnoDB").Sync2(tables...) } @@ -176,7 +177,7 @@ func InitEngineWithMigration(ctx context.Context, migrateFunc func(*xorm.Engine) return fmt.Errorf("migrate: %v", err) } - if err = syncTables(); err != nil { + if err = SyncAllTables(); err != nil { return fmt.Errorf("sync database struct error: %v", err) } diff --git a/models/db/main_test.go b/models/db/paginator/main_test.go index f34ff65813..601ed89710 100644 --- a/models/db/main_test.go +++ b/models/db/paginator/main_test.go @@ -2,13 +2,15 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package db +package paginator import ( "path/filepath" "testing" + + "code.gitea.io/gitea/models/unittest" ) func TestMain(m *testing.M) { - MainTest(m, filepath.Join("..", "..")) + unittest.MainTest(m, filepath.Join("..", "..", "..")) } diff --git a/models/db/paginator/paginator.go b/models/db/paginator/paginator.go new file mode 100644 index 0000000000..747539f30e --- /dev/null +++ b/models/db/paginator/paginator.go @@ -0,0 +1,8 @@ +// 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 paginator + +// dummy only. in the future, the models/db/list_options.go should be moved here to decouple from db package +// otherwise the unit test will cause cycle import diff --git a/models/db/list_options_test.go b/models/db/paginator/paginator_test.go index 2c860afdfb..fdb8eee441 100644 --- a/models/db/list_options_test.go +++ b/models/db/paginator/paginator_test.go @@ -2,11 +2,12 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package db +package paginator import ( "testing" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/setting" "github.com/stretchr/testify/assert" @@ -14,35 +15,35 @@ import ( func TestPaginator(t *testing.T) { cases := []struct { - Paginator + db.Paginator Skip int Take int Start int End int }{ { - Paginator: &ListOptions{Page: -1, PageSize: -1}, + Paginator: &db.ListOptions{Page: -1, PageSize: -1}, Skip: 0, Take: setting.API.DefaultPagingNum, Start: 0, End: setting.API.DefaultPagingNum, }, { - Paginator: &ListOptions{Page: 2, PageSize: 10}, + Paginator: &db.ListOptions{Page: 2, PageSize: 10}, Skip: 10, Take: 10, Start: 10, End: 20, }, { - Paginator: NewAbsoluteListOptions(-1, -1), + Paginator: db.NewAbsoluteListOptions(-1, -1), Skip: 0, Take: setting.API.DefaultPagingNum, Start: 0, End: setting.API.DefaultPagingNum, }, { - Paginator: NewAbsoluteListOptions(2, 10), + Paginator: db.NewAbsoluteListOptions(2, 10), Skip: 2, Take: 10, Start: 2, diff --git a/models/db/unit_tests.go b/models/db/unit_tests.go index 6f079c8676..2b1691726f 100644 --- a/models/db/unit_tests.go +++ b/models/db/unit_tests.go @@ -6,157 +6,26 @@ package db import ( "context" - "fmt" "math" - "net/url" - "os" - "path/filepath" - "testing" - "code.gitea.io/gitea/modules/base" - "code.gitea.io/gitea/modules/setting" - "code.gitea.io/gitea/modules/storage" - "code.gitea.io/gitea/modules/util" + "code.gitea.io/gitea/modules/unittestbridge" - "github.com/stretchr/testify/assert" "xorm.io/xorm" - "xorm.io/xorm/names" ) +// 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) -// giteaRoot a path to the gitea root -var ( - giteaRoot string - fixturesDir string -) - -// FixturesDir returns the fixture directory -func FixturesDir() string { - return fixturesDir -} - -func fatalTestError(fmtStr string, args ...interface{}) { - fmt.Fprintf(os.Stderr, fmtStr, args...) - os.Exit(1) -} - -// MainTest a reusable TestMain(..) function for unit tests that need to use a -// test database. Creates the test database, and sets necessary settings. -func MainTest(m *testing.M, pathToGiteaRoot string, fixtureFiles ...string) { - var err error - giteaRoot = pathToGiteaRoot - fixturesDir = filepath.Join(pathToGiteaRoot, "models", "fixtures") - - var opts FixturesOptions - if len(fixtureFiles) == 0 { - opts.Dir = fixturesDir - } else { - for _, f := range fixtureFiles { - if len(f) != 0 { - opts.Files = append(opts.Files, filepath.Join(fixturesDir, f)) - } - } - } - - if err = CreateTestEngine(opts); err != nil { - fatalTestError("Error creating test engine: %v\n", err) - } - - setting.AppURL = "https://try.gitea.io/" - setting.RunUser = "runuser" - setting.SSH.Port = 3000 - setting.SSH.Domain = "try.gitea.io" - setting.Database.UseSQLite3 = true - setting.RepoRootPath, err = os.MkdirTemp(os.TempDir(), "repos") - if err != nil { - fatalTestError("TempDir: %v\n", err) - } - setting.AppDataPath, err = os.MkdirTemp(os.TempDir(), "appdata") - if err != nil { - fatalTestError("TempDir: %v\n", err) - } - setting.AppWorkPath = pathToGiteaRoot - setting.StaticRootPath = pathToGiteaRoot - setting.GravatarSourceURL, err = url.Parse("https://secure.gravatar.com/avatar/") - if err != nil { - fatalTestError("url.Parse: %v\n", err) - } - setting.Attachment.Storage.Path = filepath.Join(setting.AppDataPath, "attachments") - - setting.LFS.Storage.Path = filepath.Join(setting.AppDataPath, "lfs") - - setting.Avatar.Storage.Path = filepath.Join(setting.AppDataPath, "avatars") - - setting.RepoAvatar.Storage.Path = filepath.Join(setting.AppDataPath, "repo-avatars") - - setting.RepoArchive.Storage.Path = filepath.Join(setting.AppDataPath, "repo-archive") - - if err = storage.Init(); err != nil { - fatalTestError("storage.Init: %v\n", err) - } - - if err = util.RemoveAll(setting.RepoRootPath); err != nil { - fatalTestError("util.RemoveAll: %v\n", err) - } - if err = util.CopyDir(filepath.Join(pathToGiteaRoot, "integrations", "gitea-repositories-meta"), setting.RepoRootPath); err != nil { - fatalTestError("util.CopyDir: %v\n", err) - } - - exitStatus := m.Run() - if err = util.RemoveAll(setting.RepoRootPath); err != nil { - fatalTestError("util.RemoveAll: %v\n", err) - } - if err = util.RemoveAll(setting.AppDataPath); err != nil { - fatalTestError("util.RemoveAll: %v\n", err) - } - os.Exit(exitStatus) -} - -// FixturesOptions fixtures needs to be loaded options -type FixturesOptions struct { - Dir string - Files []string -} - -// CreateTestEngine creates a memory database and loads the fixture data from fixturesDir -func CreateTestEngine(opts FixturesOptions) error { - var err error - x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared&_txlock=immediate") - if err != nil { - return err - } - x.SetMapper(names.GonicMapper{}) - if err = syncTables(); err != nil { - return err - } - switch os.Getenv("GITEA_UNIT_TESTS_VERBOSE") { - case "true", "1": - x.ShowSQL(true) - } - +//SetUnitTestEngine is used by unit test code +func SetUnitTestEngine(eng *xorm.Engine) { + x = eng DefaultContext = &Context{ Context: context.Background(), e: x, } - - return InitFixtures(opts) -} - -// PrepareTestDatabase load test fixtures into test database -func PrepareTestDatabase() error { - return LoadFixtures() -} - -// PrepareTestEnv prepares the environment for unit tests. Can only be called -// by tests that use the above MainTest(..) function. -func PrepareTestEnv(t testing.TB) { - assert.NoError(t, PrepareTestDatabase()) - assert.NoError(t, util.RemoveAll(setting.RepoRootPath)) - metaPath := filepath.Join(giteaRoot, "integrations", "gitea-repositories-meta") - assert.NoError(t, util.CopyDir(metaPath, setting.RepoRootPath)) - base.SetupGiteaRoot() // Makes sure GITEA_ROOT is set } type testCond struct { @@ -182,10 +51,6 @@ func whereConditions(sess *xorm.Session, conditions []interface{}) { // LoadBeanIfExists loads beans from fixture database if exist func LoadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) { - return loadBeanIfExists(bean, conditions...) -} - -func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) { sess := x.NewSession() defer sess.Close() whereConditions(sess, conditions) @@ -193,61 +58,68 @@ func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) } // BeanExists for testing, check if a bean exists -func BeanExists(t testing.TB, bean interface{}, conditions ...interface{}) bool { - exists, err := loadBeanIfExists(bean, conditions...) - assert.NoError(t, err) +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 testing.TB, bean interface{}, conditions ...interface{}) interface{} { - exists, err := loadBeanIfExists(bean, conditions...) - assert.NoError(t, err) - assert.True(t, 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 testing.TB, bean interface{}, conditions ...interface{}) int { +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) - assert.NoError(t, err) + ta.NoError(err) return int(count) } // AssertNotExistsBean assert that a bean does not exist in the test database -func AssertNotExistsBean(t testing.TB, bean interface{}, conditions ...interface{}) { - exists, err := loadBeanIfExists(bean, conditions...) - assert.NoError(t, err) - assert.False(t, exists) +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 *testing.T, expected bool, bean interface{}, conditions ...interface{}) { - exists, err := loadBeanIfExists(bean, conditions...) - assert.NoError(t, err) - assert.Equal(t, expected, exists) +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 testing.TB, beans ...interface{}) { +func AssertSuccessfulInsert(t unittestbridge.Tester, beans ...interface{}) { + ta := unittestbridge.NewAsserter(t) _, err := x.Insert(beans...) - assert.NoError(t, err) + ta.NoError(err) } // AssertCount assert the count of a bean -func AssertCount(t testing.TB, bean, expected interface{}) { - assert.EqualValues(t, expected, GetCount(t, 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 testing.TB, low, high, value int64) { - assert.True(t, value >= low && value <= 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/engine_test.go b/models/engine_test.go index d97fc3cc19..75c854b185 100644 --- a/models/engine_test.go +++ b/models/engine_test.go @@ -10,13 +10,14 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" "github.com/stretchr/testify/assert" ) func TestDumpDatabase(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) dir, err := os.MkdirTemp(os.TempDir(), "dump") assert.NoError(t, err) diff --git a/models/fixture_test.go b/models/fixture_test.go index 3c6ebc0685..0857341e44 100644 --- a/models/fixture_test.go +++ b/models/fixture_test.go @@ -9,21 +9,21 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/util" "github.com/stretchr/testify/assert" ) func TestFixtureGeneration(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) test := func(gen func() (string, error), name string) { expected, err := gen() if !assert.NoError(t, err) { return } - bytes, err := os.ReadFile(filepath.Join(db.FixturesDir(), name+".yml")) + bytes, err := os.ReadFile(filepath.Join(unittest.FixturesDir(), name+".yml")) if !assert.NoError(t, err) { return } diff --git a/models/gpg_key_test.go b/models/gpg_key_test.go index 7a3cbfd67f..3df25a10bb 100644 --- a/models/gpg_key_test.go +++ b/models/gpg_key_test.go @@ -9,6 +9,7 @@ import ( "time" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/timeutil" "github.com/stretchr/testify/assert" @@ -193,7 +194,7 @@ Unknown GPG key with good email } func TestCheckGPGUserEmail(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) _ = db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) diff --git a/models/issue_assignees_test.go b/models/issue_assignees_test.go index 5052df3dfb..f0bd5fca4b 100644 --- a/models/issue_assignees_test.go +++ b/models/issue_assignees_test.go @@ -8,11 +8,12 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestUpdateAssignee(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // Fake issue with assignees issue, err := GetIssueWithAttrsByID(1) @@ -62,7 +63,7 @@ func TestUpdateAssignee(t *testing.T) { } func TestMakeIDsFromAPIAssigneesToAdd(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) _ = db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) _ = db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) diff --git a/models/issue_comment_test.go b/models/issue_comment_test.go index 78199881c6..d855b87bc4 100644 --- a/models/issue_comment_test.go +++ b/models/issue_comment_test.go @@ -9,11 +9,12 @@ import ( "time" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestCreateComment(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) issue := db.AssertExistsAndLoadBean(t, &Issue{}).(*Issue) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: issue.RepoID}).(*Repository) @@ -42,7 +43,7 @@ func TestCreateComment(t *testing.T) { } func TestFetchCodeComments(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue) user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) diff --git a/models/issue_dependency_test.go b/models/issue_dependency_test.go index 10872645b0..60fa587389 100644 --- a/models/issue_dependency_test.go +++ b/models/issue_dependency_test.go @@ -8,12 +8,13 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestCreateIssueDependency(t *testing.T) { // Prepare - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user1, err := GetUserByID(1) assert.NoError(t, err) diff --git a/models/issue_label_test.go b/models/issue_label_test.go index 93807a326f..c77ddfefaf 100644 --- a/models/issue_label_test.go +++ b/models/issue_label_test.go @@ -9,20 +9,21 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) // TODO TestGetLabelTemplateFile func TestLabel_CalOpenIssues(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) label := db.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) label.CalOpenIssues() assert.EqualValues(t, 2, label.NumOpenIssues) } func TestLabel_ForegroundColor(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) label := db.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) assert.Equal(t, template.CSS("#000"), label.ForegroundColor()) @@ -31,7 +32,7 @@ func TestLabel_ForegroundColor(t *testing.T) { } func TestNewLabels(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) labels := []*Label{ {RepoID: 2, Name: "labelName2", Color: "#123456"}, {RepoID: 3, Name: "labelName3", Color: "#23456F"}, @@ -50,7 +51,7 @@ func TestNewLabels(t *testing.T) { } func TestGetLabelByID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) label, err := GetLabelByID(1) assert.NoError(t, err) assert.EqualValues(t, 1, label.ID) @@ -60,7 +61,7 @@ func TestGetLabelByID(t *testing.T) { } func TestGetLabelInRepoByName(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) label, err := GetLabelInRepoByName(1, "label1") assert.NoError(t, err) assert.EqualValues(t, 1, label.ID) @@ -74,7 +75,7 @@ func TestGetLabelInRepoByName(t *testing.T) { } func TestGetLabelInRepoByNames(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) labelIDs, err := GetLabelIDsInRepoByNames(1, []string{"label1", "label2"}) assert.NoError(t, err) @@ -85,7 +86,7 @@ func TestGetLabelInRepoByNames(t *testing.T) { } func TestGetLabelInRepoByNamesDiscardsNonExistentLabels(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // label3 doesn't exists.. See labels.yml labelIDs, err := GetLabelIDsInRepoByNames(1, []string{"label1", "label2", "label3"}) assert.NoError(t, err) @@ -98,7 +99,7 @@ func TestGetLabelInRepoByNamesDiscardsNonExistentLabels(t *testing.T) { } func TestGetLabelInRepoByID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) label, err := GetLabelInRepoByID(1, 1) assert.NoError(t, err) assert.EqualValues(t, 1, label.ID) @@ -111,7 +112,7 @@ func TestGetLabelInRepoByID(t *testing.T) { } func TestGetLabelsInRepoByIDs(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) labels, err := GetLabelsInRepoByIDs(1, []int64{1, 2, db.NonexistentID}) assert.NoError(t, err) if assert.Len(t, labels, 2) { @@ -121,7 +122,7 @@ func TestGetLabelsInRepoByIDs(t *testing.T) { } func TestGetLabelsByRepoID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(repoID int64, sortType string, expectedIssueIDs []int64) { labels, err := GetLabelsByRepoID(repoID, sortType, db.ListOptions{}) assert.NoError(t, err) @@ -139,7 +140,7 @@ func TestGetLabelsByRepoID(t *testing.T) { // Org versions func TestGetLabelInOrgByName(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) label, err := GetLabelInOrgByName(3, "orglabel3") assert.NoError(t, err) assert.EqualValues(t, 3, label.ID) @@ -159,7 +160,7 @@ func TestGetLabelInOrgByName(t *testing.T) { } func TestGetLabelInOrgByNames(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) labelIDs, err := GetLabelIDsInOrgByNames(3, []string{"orglabel3", "orglabel4"}) assert.NoError(t, err) @@ -170,7 +171,7 @@ func TestGetLabelInOrgByNames(t *testing.T) { } func TestGetLabelInOrgByNamesDiscardsNonExistentLabels(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // orglabel99 doesn't exists.. See labels.yml labelIDs, err := GetLabelIDsInOrgByNames(3, []string{"orglabel3", "orglabel4", "orglabel99"}) assert.NoError(t, err) @@ -183,7 +184,7 @@ func TestGetLabelInOrgByNamesDiscardsNonExistentLabels(t *testing.T) { } func TestGetLabelInOrgByID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) label, err := GetLabelInOrgByID(3, 3) assert.NoError(t, err) assert.EqualValues(t, 3, label.ID) @@ -202,7 +203,7 @@ func TestGetLabelInOrgByID(t *testing.T) { } func TestGetLabelsInOrgByIDs(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) labels, err := GetLabelsInOrgByIDs(3, []int64{3, 4, db.NonexistentID}) assert.NoError(t, err) if assert.Len(t, labels, 2) { @@ -212,7 +213,7 @@ func TestGetLabelsInOrgByIDs(t *testing.T) { } func TestGetLabelsByOrgID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(orgID int64, sortType string, expectedIssueIDs []int64) { labels, err := GetLabelsByOrgID(orgID, sortType, db.ListOptions{}) assert.NoError(t, err) @@ -237,7 +238,7 @@ func TestGetLabelsByOrgID(t *testing.T) { // func TestGetLabelsByIssueID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) labels, err := GetLabelsByIssueID(1) assert.NoError(t, err) if assert.Len(t, labels, 1) { @@ -250,7 +251,7 @@ func TestGetLabelsByIssueID(t *testing.T) { } func TestUpdateLabel(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) label := db.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) // make sure update wont overwrite it update := &Label{ @@ -271,7 +272,7 @@ func TestUpdateLabel(t *testing.T) { } func TestDeleteLabel(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) label := db.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) assert.NoError(t, DeleteLabel(label.RepoID, label.ID)) db.AssertNotExistsBean(t, &Label{ID: label.ID, RepoID: label.RepoID}) @@ -284,14 +285,14 @@ func TestDeleteLabel(t *testing.T) { } func TestHasIssueLabel(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + 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)) } func TestNewIssueLabel(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + 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) @@ -316,7 +317,7 @@ func TestNewIssueLabel(t *testing.T) { } func TestNewIssueLabels(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + 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) @@ -346,7 +347,7 @@ func TestNewIssueLabels(t *testing.T) { } func TestDeleteIssueLabel(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + 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) diff --git a/models/issue_list_test.go b/models/issue_list_test.go index dadca3844c..ea2f42e77b 100644 --- a/models/issue_list_test.go +++ b/models/issue_list_test.go @@ -8,13 +8,14 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" "github.com/stretchr/testify/assert" ) func TestIssueList_LoadRepositories(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) issueList := IssueList{ db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue), @@ -31,7 +32,7 @@ func TestIssueList_LoadRepositories(t *testing.T) { } func TestIssueList_LoadAttributes(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) setting.Service.EnableTimetracking = true issueList := IssueList{ db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue), diff --git a/models/issue_milestone_test.go b/models/issue_milestone_test.go index 099fe47c7c..8dee464066 100644 --- a/models/issue_milestone_test.go +++ b/models/issue_milestone_test.go @@ -9,6 +9,7 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" @@ -23,7 +24,7 @@ func TestMilestone_State(t *testing.T) { } func TestNewMilestone(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) milestone := &Milestone{ RepoID: 1, Name: "milestoneName", @@ -36,7 +37,7 @@ func TestNewMilestone(t *testing.T) { } func TestGetMilestoneByRepoID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) milestone, err := GetMilestoneByRepoID(1, 1) assert.NoError(t, err) @@ -48,7 +49,7 @@ func TestGetMilestoneByRepoID(t *testing.T) { } func TestGetMilestonesByRepoID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) test := func(repoID int64, state api.StateType) { repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) milestones, _, err := GetMilestones(GetMilestonesOption{ @@ -97,7 +98,7 @@ func TestGetMilestonesByRepoID(t *testing.T) { } func TestGetMilestones(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) test := func(sortType string, sortCond func(*Milestone) int) { for _, page := range []int{0, 1} { @@ -158,7 +159,7 @@ func TestGetMilestones(t *testing.T) { } func TestUpdateMilestone(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) milestone := db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone) milestone.Name = " newMilestoneName " @@ -170,7 +171,7 @@ func TestUpdateMilestone(t *testing.T) { } func TestCountRepoMilestones(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) test := func(repoID int64) { repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) count, err := countRepoMilestones(db.GetEngine(db.DefaultContext), repoID) @@ -187,7 +188,7 @@ func TestCountRepoMilestones(t *testing.T) { } func TestCountRepoClosedMilestones(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) test := func(repoID int64) { repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) count, err := CountRepoClosedMilestones(repoID) @@ -204,7 +205,7 @@ func TestCountRepoClosedMilestones(t *testing.T) { } func TestChangeMilestoneStatus(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) milestone := db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone) assert.NoError(t, ChangeMilestoneStatus(milestone, true)) @@ -217,7 +218,7 @@ func TestChangeMilestoneStatus(t *testing.T) { } func TestUpdateMilestoneCounters(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) issue := db.AssertExistsAndLoadBean(t, &Issue{MilestoneID: 1}, "is_closed=0").(*Issue) @@ -237,7 +238,7 @@ func TestUpdateMilestoneCounters(t *testing.T) { } func TestChangeMilestoneAssign(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) issue := db.AssertExistsAndLoadBean(t, &Issue{RepoID: 1}).(*Issue) doer := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) assert.NotNil(t, issue) @@ -256,7 +257,7 @@ func TestChangeMilestoneAssign(t *testing.T) { } func TestDeleteMilestoneByRepoID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, DeleteMilestoneByRepoID(1, 1)) db.AssertNotExistsBean(t, &Milestone{ID: 1}) CheckConsistencyFor(t, &Repository{ID: 1}) @@ -265,7 +266,7 @@ func TestDeleteMilestoneByRepoID(t *testing.T) { } func TestMilestoneList_LoadTotalTrackedTimes(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) miles := MilestoneList{ db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone), } @@ -276,7 +277,7 @@ func TestMilestoneList_LoadTotalTrackedTimes(t *testing.T) { } func TestCountMilestonesByRepoIDs(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) milestonesCount := func(repoID int64) (int, int) { repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) return repo.NumOpenMilestones, repo.NumClosedMilestones @@ -296,7 +297,7 @@ func TestCountMilestonesByRepoIDs(t *testing.T) { } func TestGetMilestonesByRepoIDs(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) repo2 := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) test := func(sortType string, sortCond func(*Milestone) int) { @@ -341,7 +342,7 @@ func TestGetMilestonesByRepoIDs(t *testing.T) { } func TestLoadTotalTrackedTime(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) milestone := db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone) assert.NoError(t, milestone.LoadTotalTrackedTime()) @@ -350,7 +351,7 @@ func TestLoadTotalTrackedTime(t *testing.T) { } func TestGetMilestonesStats(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) test := func(repoID int64) { repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) diff --git a/models/issue_reaction_test.go b/models/issue_reaction_test.go index dd15b816c7..5c0441623b 100644 --- a/models/issue_reaction_test.go +++ b/models/issue_reaction_test.go @@ -7,6 +7,7 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" "github.com/stretchr/testify/assert" @@ -25,7 +26,7 @@ func addReaction(t *testing.T, doer *User, issue *Issue, comment *Comment, conte } func TestIssueAddReaction(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) @@ -37,7 +38,7 @@ func TestIssueAddReaction(t *testing.T) { } func TestIssueAddDuplicateReaction(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) @@ -58,7 +59,7 @@ func TestIssueAddDuplicateReaction(t *testing.T) { } func TestIssueDeleteReaction(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) @@ -73,7 +74,7 @@ func TestIssueDeleteReaction(t *testing.T) { } func TestIssueReactionCount(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) setting.UI.ReactionMaxUserNum = 2 @@ -111,7 +112,7 @@ func TestIssueReactionCount(t *testing.T) { } func TestIssueCommentAddReaction(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) @@ -125,7 +126,7 @@ func TestIssueCommentAddReaction(t *testing.T) { } func TestIssueCommentDeleteReaction(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) @@ -152,7 +153,7 @@ func TestIssueCommentDeleteReaction(t *testing.T) { } func TestIssueCommentReactionCount(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) diff --git a/models/issue_stopwatch_test.go b/models/issue_stopwatch_test.go index 11306efcf5..0de4dd0347 100644 --- a/models/issue_stopwatch_test.go +++ b/models/issue_stopwatch_test.go @@ -8,13 +8,14 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/timeutil" "github.com/stretchr/testify/assert" ) func TestCancelStopwatch(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user1, err := GetUserByID(1) assert.NoError(t, err) @@ -34,14 +35,14 @@ func TestCancelStopwatch(t *testing.T) { } func TestStopwatchExists(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) assert.True(t, StopwatchExists(1, 1)) assert.False(t, StopwatchExists(1, 2)) } func TestHasUserStopwatch(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) exists, sw, err := HasUserStopwatch(1) assert.NoError(t, err) @@ -54,7 +55,7 @@ func TestHasUserStopwatch(t *testing.T) { } func TestCreateOrStopIssueStopwatch(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user2, err := GetUserByID(2) assert.NoError(t, err) diff --git a/models/issue_test.go b/models/issue_test.go index 9df91aeb99..296e12a5d4 100644 --- a/models/issue_test.go +++ b/models/issue_test.go @@ -12,11 +12,12 @@ import ( "time" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestIssue_ReplaceLabels(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(issueID int64, labelIDs []int64) { issue := db.AssertExistsAndLoadBean(t, &Issue{ID: issueID}).(*Issue) @@ -40,7 +41,7 @@ func TestIssue_ReplaceLabels(t *testing.T) { } func Test_GetIssueIDsByRepoID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) ids, err := GetIssueIDsByRepoID(1) assert.NoError(t, err) @@ -48,7 +49,7 @@ func Test_GetIssueIDsByRepoID(t *testing.T) { } func TestIssueAPIURL(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) err := issue.LoadAttributes() @@ -57,7 +58,7 @@ func TestIssueAPIURL(t *testing.T) { } func TestGetIssuesByIDs(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(expectedIssueIDs, nonExistentIssueIDs []int64) { issues, err := GetIssuesByIDs(append(expectedIssueIDs, nonExistentIssueIDs...)) assert.NoError(t, err) @@ -72,7 +73,7 @@ func TestGetIssuesByIDs(t *testing.T) { } func TestGetParticipantIDsByIssue(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) checkParticipants := func(issueID int64, userIDs []int) { issue, err := GetIssueByID(issueID) @@ -106,7 +107,7 @@ func TestIssue_ClearLabels(t *testing.T) { {3, 2}, // pull-request, has no labels } for _, test := range tests { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) issue := db.AssertExistsAndLoadBean(t, &Issue{ID: test.issueID}).(*Issue) doer := db.AssertExistsAndLoadBean(t, &User{ID: test.doerID}).(*User) assert.NoError(t, issue.ClearLabels(doer)) @@ -115,7 +116,7 @@ func TestIssue_ClearLabels(t *testing.T) { } func TestUpdateIssueCols(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) issue := db.AssertExistsAndLoadBean(t, &Issue{}).(*Issue) const newTitle = "New Title for unit test" @@ -135,7 +136,7 @@ func TestUpdateIssueCols(t *testing.T) { } func TestIssues(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) for _, test := range []struct { Opts IssuesOptions ExpectedIssueIDs []int64 @@ -190,7 +191,7 @@ func TestIssues(t *testing.T) { } func TestGetUserIssueStats(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) for _, test := range []struct { Opts UserIssueStatsOptions ExpectedIssueStats IssueStats @@ -287,7 +288,7 @@ func TestGetUserIssueStats(t *testing.T) { } func TestIssue_loadTotalTimes(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) ms, err := GetIssueByID(2) assert.NoError(t, err) assert.NoError(t, ms.loadTotalTimes(db.GetEngine(db.DefaultContext))) @@ -295,7 +296,7 @@ func TestIssue_loadTotalTimes(t *testing.T) { } func TestIssue_SearchIssueIDsByKeyword(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) total, ids, err := SearchIssueIDsByKeyword("issue2", []int64{1}, 10, 0) assert.NoError(t, err) assert.EqualValues(t, 1, total) @@ -319,7 +320,7 @@ func TestIssue_SearchIssueIDsByKeyword(t *testing.T) { } func TestGetRepoIDsForIssuesOptions(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) for _, test := range []struct { Opts IssuesOptions @@ -377,7 +378,7 @@ func testInsertIssue(t *testing.T, title, content string, expectIndex int64) *Is } func TestIssue_InsertIssue(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // there are 5 issues and max index is 5 on repository 1, so this one should 6 issue := testInsertIssue(t, "my issue1", "special issue's comments?", 6) @@ -391,7 +392,7 @@ func TestIssue_InsertIssue(t *testing.T) { } func TestIssue_ResolveMentions(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(owner, repo, doer string, mentions []string, expected []int64) { o := db.AssertExistsAndLoadBean(t, &User{LowerName: owner}).(*User) @@ -423,7 +424,7 @@ func TestIssue_ResolveMentions(t *testing.T) { } func TestResourceIndex(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) var wg sync.WaitGroup for i := 0; i < 100; i++ { @@ -437,7 +438,7 @@ func TestResourceIndex(t *testing.T) { } func TestCorrectIssueStats(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // Because the condition is to have chunked database look-ups, // We have to more issues than `maxQueryParameters`, we will insert. diff --git a/models/issue_tracked_time_test.go b/models/issue_tracked_time_test.go index 7643360727..2770065363 100644 --- a/models/issue_tracked_time_test.go +++ b/models/issue_tracked_time_test.go @@ -9,11 +9,12 @@ import ( "time" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestAddTime(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user3, err := GetUserByID(3) assert.NoError(t, err) @@ -36,7 +37,7 @@ func TestAddTime(t *testing.T) { } func TestGetTrackedTimes(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // by Issue times, err := GetTrackedTimes(&FindTrackedTimesOptions{IssueID: 1}) @@ -77,7 +78,7 @@ func TestGetTrackedTimes(t *testing.T) { } func TestTotalTimes(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) total, err := TotalTimes(&FindTrackedTimesOptions{IssueID: 1}) assert.NoError(t, err) diff --git a/models/issue_user_test.go b/models/issue_user_test.go index d4e504719f..1a9ae6395b 100644 --- a/models/issue_user_test.go +++ b/models/issue_user_test.go @@ -8,11 +8,12 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func Test_newIssueUsers(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) newIssue := &Issue{ @@ -34,7 +35,7 @@ func Test_newIssueUsers(t *testing.T) { } func TestUpdateIssueUserByRead(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) assert.NoError(t, UpdateIssueUserByRead(4, issue.ID)) @@ -47,7 +48,7 @@ func TestUpdateIssueUserByRead(t *testing.T) { } func TestUpdateIssueUsersByMentions(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) uids := []int64{2, 5} diff --git a/models/issue_watch_test.go b/models/issue_watch_test.go index 139ed41cb6..9aaa184a34 100644 --- a/models/issue_watch_test.go +++ b/models/issue_watch_test.go @@ -8,11 +8,12 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestCreateOrUpdateIssueWatch(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, CreateOrUpdateIssueWatch(3, 1, true)) iw := db.AssertExistsAndLoadBean(t, &IssueWatch{UserID: 3, IssueID: 1}).(*IssueWatch) @@ -24,7 +25,7 @@ func TestCreateOrUpdateIssueWatch(t *testing.T) { } func TestGetIssueWatch(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) _, exists, err := GetIssueWatch(9, 1) assert.True(t, exists) @@ -41,7 +42,7 @@ func TestGetIssueWatch(t *testing.T) { } func TestGetIssueWatchers(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) iws, err := GetIssueWatchers(1, db.ListOptions{}) assert.NoError(t, err) diff --git a/models/issue_xref_test.go b/models/issue_xref_test.go index bf498e4710..a1f4515b60 100644 --- a/models/issue_xref_test.go +++ b/models/issue_xref_test.go @@ -9,13 +9,14 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/references" "github.com/stretchr/testify/assert" ) func TestXRef_AddCrossReferences(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // Issue #1 to test against itarget := testCreateIssue(t, 1, 2, "title1", "content1", false) @@ -66,7 +67,7 @@ func TestXRef_AddCrossReferences(t *testing.T) { } func TestXRef_NeuterCrossReferences(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // Issue #1 to test against itarget := testCreateIssue(t, 1, 2, "title1", "content1", false) @@ -88,7 +89,7 @@ func TestXRef_NeuterCrossReferences(t *testing.T) { } func TestXRef_ResolveCrossReferences(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) d := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) diff --git a/models/issues/content_history_test.go b/models/issues/content_history_test.go index dadeb484b1..f040a7dc57 100644 --- a/models/issues/content_history_test.go +++ b/models/issues/content_history_test.go @@ -8,13 +8,14 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/timeutil" "github.com/stretchr/testify/assert" ) func TestContentHistory(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) dbCtx := db.DefaultContext dbEngine := db.GetEngine(dbCtx) diff --git a/models/issues/main_test.go b/models/issues/main_test.go index 61a15c53b7..af71f038d6 100644 --- a/models/issues/main_test.go +++ b/models/issues/main_test.go @@ -8,9 +8,9 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", ".."), "") + unittest.MainTest(m, filepath.Join("..", ".."), "") } diff --git a/models/login/main_test.go b/models/login/main_test.go index 141952a594..0666eeaad0 100644 --- a/models/login/main_test.go +++ b/models/login/main_test.go @@ -8,11 +8,11 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", ".."), + unittest.MainTest(m, filepath.Join("..", ".."), "login_source.yml", "oauth2_application.yml", "oauth2_authorization_code.yml", diff --git a/models/login/oauth2_application_test.go b/models/login/oauth2_application_test.go index cb064cef1b..876de5b52a 100644 --- a/models/login/oauth2_application_test.go +++ b/models/login/oauth2_application_test.go @@ -8,6 +8,7 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -15,7 +16,7 @@ import ( //////////////////// Application func TestOAuth2Application_GenerateClientSecret(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) app := db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application) secret, err := app.GenerateClientSecret() assert.NoError(t, err) @@ -24,7 +25,7 @@ func TestOAuth2Application_GenerateClientSecret(t *testing.T) { } func BenchmarkOAuth2Application_GenerateClientSecret(b *testing.B) { - assert.NoError(b, db.PrepareTestDatabase()) + assert.NoError(b, unittest.PrepareTestDatabase()) app := db.AssertExistsAndLoadBean(b, &OAuth2Application{ID: 1}).(*OAuth2Application) for i := 0; i < b.N; i++ { _, _ = app.GenerateClientSecret() @@ -42,7 +43,7 @@ func TestOAuth2Application_ContainsRedirectURI(t *testing.T) { } func TestOAuth2Application_ValidateClientSecret(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) app := db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application) secret, err := app.GenerateClientSecret() assert.NoError(t, err) @@ -51,7 +52,7 @@ func TestOAuth2Application_ValidateClientSecret(t *testing.T) { } func TestGetOAuth2ApplicationByClientID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) app, err := GetOAuth2ApplicationByClientID("da7da3ba-9a13-4167-856f-3899de0b0138") assert.NoError(t, err) assert.Equal(t, "da7da3ba-9a13-4167-856f-3899de0b0138", app.ClientID) @@ -62,7 +63,7 @@ func TestGetOAuth2ApplicationByClientID(t *testing.T) { } func TestCreateOAuth2Application(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) app, err := CreateOAuth2Application(CreateOAuth2ApplicationOptions{Name: "newapp", UserID: 1}) assert.NoError(t, err) assert.Equal(t, "newapp", app.Name) @@ -75,7 +76,7 @@ func TestOAuth2Application_TableName(t *testing.T) { } func TestOAuth2Application_GetGrantByUserID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) app := db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application) grant, err := app.GetGrantByUserID(1) assert.NoError(t, err) @@ -87,7 +88,7 @@ func TestOAuth2Application_GetGrantByUserID(t *testing.T) { } func TestOAuth2Application_CreateGrant(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) app := db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application) grant, err := app.CreateGrant(2, "") assert.NoError(t, err) @@ -100,7 +101,7 @@ func TestOAuth2Application_CreateGrant(t *testing.T) { //////////////////// Grant func TestGetOAuth2GrantByID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) grant, err := GetOAuth2GrantByID(1) assert.NoError(t, err) assert.Equal(t, int64(1), grant.ID) @@ -111,7 +112,7 @@ func TestGetOAuth2GrantByID(t *testing.T) { } func TestOAuth2Grant_IncreaseCounter(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) grant := db.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1, Counter: 1}).(*OAuth2Grant) assert.NoError(t, grant.IncreaseCounter()) assert.Equal(t, int64(2), grant.Counter) @@ -119,7 +120,7 @@ func TestOAuth2Grant_IncreaseCounter(t *testing.T) { } func TestOAuth2Grant_ScopeContains(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) grant := db.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1, Scope: "openid profile"}).(*OAuth2Grant) assert.True(t, grant.ScopeContains("openid")) assert.True(t, grant.ScopeContains("profile")) @@ -128,7 +129,7 @@ func TestOAuth2Grant_ScopeContains(t *testing.T) { } func TestOAuth2Grant_GenerateNewAuthorizationCode(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) grant := db.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1}).(*OAuth2Grant) code, err := grant.GenerateNewAuthorizationCode("https://example2.com/callback", "CjvyTLSdR47G5zYenDA-eDWW4lRrO8yvjcWwbD_deOg", "S256") assert.NoError(t, err) @@ -141,7 +142,7 @@ func TestOAuth2Grant_TableName(t *testing.T) { } func TestGetOAuth2GrantsByUserID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) result, err := GetOAuth2GrantsByUserID(1) assert.NoError(t, err) assert.Len(t, result, 1) @@ -154,7 +155,7 @@ func TestGetOAuth2GrantsByUserID(t *testing.T) { } func TestRevokeOAuth2Grant(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, RevokeOAuth2Grant(1, 1)) db.AssertNotExistsBean(t, &OAuth2Grant{ID: 1, UserID: 1}) } @@ -162,7 +163,7 @@ func TestRevokeOAuth2Grant(t *testing.T) { //////////////////// Authorization Code func TestGetOAuth2AuthorizationByCode(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) code, err := GetOAuth2AuthorizationByCode("authcode") assert.NoError(t, err) assert.NotNil(t, code) @@ -222,7 +223,7 @@ func TestOAuth2AuthorizationCode_GenerateRedirectURI(t *testing.T) { } func TestOAuth2AuthorizationCode_Invalidate(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) code := db.AssertExistsAndLoadBean(t, &OAuth2AuthorizationCode{Code: "authcode"}).(*OAuth2AuthorizationCode) assert.NoError(t, code.Invalidate()) db.AssertNotExistsBean(t, &OAuth2AuthorizationCode{Code: "authcode"}) diff --git a/models/login/source_test.go b/models/login/source_test.go index d98609037c..e7ef7c7048 100644 --- a/models/login/source_test.go +++ b/models/login/source_test.go @@ -9,6 +9,7 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/json" "github.com/stretchr/testify/assert" @@ -34,7 +35,7 @@ func (source *TestSource) ToDB() ([]byte, error) { } func TestDumpLoginSource(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) loginSourceSchema, err := db.TableInfo(new(Source)) assert.NoError(t, err) diff --git a/models/login/u2f_test.go b/models/login/u2f_test.go index 8f5cea6150..11f20bc790 100644 --- a/models/login/u2f_test.go +++ b/models/login/u2f_test.go @@ -9,13 +9,14 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" "github.com/tstranex/u2f" ) func TestGetU2FRegistrationByID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) res, err := GetU2FRegistrationByID(1) assert.NoError(t, err) @@ -27,7 +28,7 @@ func TestGetU2FRegistrationByID(t *testing.T) { } func TestGetU2FRegistrationsByUID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) res, err := GetU2FRegistrationsByUID(32) @@ -41,7 +42,7 @@ func TestU2FRegistration_TableName(t *testing.T) { } func TestU2FRegistration_UpdateCounter(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) reg := db.AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration) reg.Counter = 1 assert.NoError(t, reg.UpdateCounter()) @@ -49,7 +50,7 @@ func TestU2FRegistration_UpdateCounter(t *testing.T) { } func TestU2FRegistration_UpdateLargeCounter(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) reg := db.AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration) reg.Counter = 0xffffffff assert.NoError(t, reg.UpdateCounter()) @@ -57,7 +58,7 @@ func TestU2FRegistration_UpdateLargeCounter(t *testing.T) { } func TestCreateRegistration(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) res, err := CreateRegistration(1, "U2F Created Key", &u2f.Registration{Raw: []byte("Test")}) assert.NoError(t, err) @@ -68,7 +69,7 @@ func TestCreateRegistration(t *testing.T) { } func TestDeleteRegistration(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) reg := db.AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration) assert.NoError(t, DeleteRegistration(reg)) diff --git a/models/main_test.go b/models/main_test.go index ad9276330f..15bece7bf4 100644 --- a/models/main_test.go +++ b/models/main_test.go @@ -7,17 +7,25 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) // TestFixturesAreConsistent assert that test fixtures are consistent func TestFixturesAreConsistent(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) - CheckConsistencyForAll(t) + assert.NoError(t, unittest.PrepareTestDatabase()) + CheckConsistencyFor(t, + &User{}, + &Repository{}, + &Issue{}, + &PullRequest{}, + &Milestone{}, + &Label{}, + &Team{}, + &Action{}) } func TestMain(m *testing.M) { - db.MainTest(m, "..") + unittest.MainTest(m, "..") } diff --git a/models/migrations/migrations_test.go b/models/migrations/migrations_test.go index 46c8c66a24..f46070cf8e 100644 --- a/models/migrations/migrations_test.go +++ b/models/migrations/migrations_test.go @@ -15,6 +15,7 @@ import ( "time" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/setting" @@ -241,14 +242,14 @@ func prepareTestEnv(t *testing.T, skip int, syncModels ...interface{}) (*xorm.En if _, err := os.Stat(fixturesDir); err == nil { t.Logf("initializing fixtures from: %s", fixturesDir) - if err := db.InitFixtures( - db.FixturesOptions{ + if err := unittest.InitFixtures( + unittest.FixturesOptions{ Dir: fixturesDir, }, x); err != nil { t.Errorf("error whilst initializing fixtures from %s: %v", fixturesDir, err) return x, deferFn } - if err := db.LoadFixtures(x); err != nil { + if err := unittest.LoadFixtures(x); err != nil { t.Errorf("error whilst loading fixtures from %s: %v", fixturesDir, err) return x, deferFn } diff --git a/models/notification_test.go b/models/notification_test.go index 588882bed3..cdba409f3d 100644 --- a/models/notification_test.go +++ b/models/notification_test.go @@ -8,11 +8,12 @@ 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, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) assert.NoError(t, CreateOrUpdateIssueNotifications(issue.ID, 0, 2, 0)) @@ -27,7 +28,7 @@ func TestCreateOrUpdateIssueNotifications(t *testing.T) { } func TestNotificationsForUser(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) statuses := []NotificationStatus{NotificationStatusRead, NotificationStatusUnread} notfs, err := NotificationsForUser(user, statuses, 1, 10) @@ -43,7 +44,7 @@ func TestNotificationsForUser(t *testing.T) { } func TestNotification_GetRepo(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) notf := db.AssertExistsAndLoadBean(t, &Notification{RepoID: 1}).(*Notification) repo, err := notf.GetRepo() assert.NoError(t, err) @@ -52,7 +53,7 @@ func TestNotification_GetRepo(t *testing.T) { } func TestNotification_GetIssue(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) notf := db.AssertExistsAndLoadBean(t, &Notification{RepoID: 1}).(*Notification) issue, err := notf.GetIssue() assert.NoError(t, err) @@ -61,7 +62,7 @@ func TestNotification_GetIssue(t *testing.T) { } func TestGetNotificationCount(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) cnt, err := GetNotificationCount(user, NotificationStatusRead) assert.NoError(t, err) @@ -73,7 +74,7 @@ func TestGetNotificationCount(t *testing.T) { } func TestSetNotificationStatus(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) notf := db.AssertExistsAndLoadBean(t, &Notification{UserID: user.ID, Status: NotificationStatusRead}).(*Notification) @@ -89,7 +90,7 @@ func TestSetNotificationStatus(t *testing.T) { } func TestUpdateNotificationStatuses(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) notfUnread := db.AssertExistsAndLoadBean(t, &Notification{UserID: user.ID, Status: NotificationStatusUnread}).(*Notification) diff --git a/models/org_team_test.go b/models/org_team_test.go index f1c8f88879..114c8016e8 100644 --- a/models/org_team_test.go +++ b/models/org_team_test.go @@ -9,11 +9,12 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestTeam_IsOwnerTeam(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) team := db.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) assert.True(t, team.IsOwnerTeam()) @@ -23,7 +24,7 @@ func TestTeam_IsOwnerTeam(t *testing.T) { } func TestTeam_IsMember(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) team := db.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) assert.True(t, team.IsMember(2)) @@ -37,7 +38,7 @@ func TestTeam_IsMember(t *testing.T) { } func TestTeam_GetRepositories(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) test := func(teamID int64) { team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) @@ -52,7 +53,7 @@ func TestTeam_GetRepositories(t *testing.T) { } func TestTeam_GetMembers(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) test := func(teamID int64) { team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) @@ -67,7 +68,7 @@ func TestTeam_GetMembers(t *testing.T) { } func TestTeam_AddMember(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) test := func(teamID, userID int64) { team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) @@ -81,7 +82,7 @@ func TestTeam_AddMember(t *testing.T) { } func TestTeam_RemoveMember(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(teamID, userID int64) { team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) @@ -100,7 +101,7 @@ func TestTeam_RemoveMember(t *testing.T) { } func TestTeam_HasRepository(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) test := func(teamID, repoID int64, expected bool) { team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) @@ -116,7 +117,7 @@ func TestTeam_HasRepository(t *testing.T) { } func TestTeam_AddRepository(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(teamID, repoID int64) { team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) @@ -135,7 +136,7 @@ func TestTeam_AddRepository(t *testing.T) { } func TestTeam_RemoveRepository(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(teamID, repoID int64) { team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) @@ -154,7 +155,7 @@ func TestIsUsableTeamName(t *testing.T) { } func TestNewTeam(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) const teamName = "newTeamName" team := &Team{Name: teamName, OrgID: 3} @@ -164,7 +165,7 @@ func TestNewTeam(t *testing.T) { } func TestGetTeam(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(orgID int64, name string) { team, err := GetTeam(orgID, name) @@ -182,7 +183,7 @@ func TestGetTeam(t *testing.T) { } func TestGetTeamByID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(teamID int64) { team, err := GetTeamByID(teamID) @@ -200,7 +201,7 @@ func TestGetTeamByID(t *testing.T) { func TestUpdateTeam(t *testing.T) { // successful update - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) team := db.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) team.LowerName = "newname" @@ -220,7 +221,7 @@ func TestUpdateTeam(t *testing.T) { func TestUpdateTeam2(t *testing.T) { // update to already-existing team - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) team := db.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) team.LowerName = "owners" @@ -233,7 +234,7 @@ func TestUpdateTeam2(t *testing.T) { } func TestDeleteTeam(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) team := db.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) assert.NoError(t, DeleteTeam(team)) @@ -250,7 +251,7 @@ func TestDeleteTeam(t *testing.T) { } func TestIsTeamMember(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) test := func(orgID, teamID, userID int64, expected bool) { isMember, err := IsTeamMember(orgID, teamID, userID) assert.NoError(t, err) @@ -269,7 +270,7 @@ func TestIsTeamMember(t *testing.T) { } func TestGetTeamMembers(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) test := func(teamID int64) { team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) @@ -285,7 +286,7 @@ func TestGetTeamMembers(t *testing.T) { } func TestGetUserTeams(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) test := func(userID int64) { teams, _, err := SearchTeam(&SearchTeamOptions{UserID: userID}) assert.NoError(t, err) @@ -299,7 +300,7 @@ func TestGetUserTeams(t *testing.T) { } func TestGetUserOrgTeams(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) test := func(orgID, userID int64) { teams, err := GetUserOrgTeams(orgID, userID) assert.NoError(t, err) @@ -314,7 +315,7 @@ func TestGetUserOrgTeams(t *testing.T) { } func TestAddTeamMember(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) test := func(teamID, userID int64) { team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) @@ -328,7 +329,7 @@ func TestAddTeamMember(t *testing.T) { } func TestRemoveTeamMember(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(teamID, userID int64) { team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) @@ -347,7 +348,7 @@ func TestRemoveTeamMember(t *testing.T) { } func TestHasTeamRepo(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) test := func(teamID, repoID int64, expected bool) { team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) @@ -363,7 +364,7 @@ func TestHasTeamRepo(t *testing.T) { } func TestUsersInTeamsCount(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) test := func(teamIDs, userIDs []int64, expected int64) { count, err := UsersInTeamsCount(teamIDs, userIDs) diff --git a/models/org_test.go b/models/org_test.go index 2df89b2afc..9557e14edc 100644 --- a/models/org_test.go +++ b/models/org_test.go @@ -8,6 +8,7 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/structs" @@ -15,7 +16,7 @@ import ( ) func TestUser_IsOwnedBy(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) for _, testCase := range []struct { OrgID int64 UserID int64 @@ -36,7 +37,7 @@ func TestUser_IsOwnedBy(t *testing.T) { } func TestUser_IsOrgMember(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) for _, testCase := range []struct { OrgID int64 UserID int64 @@ -57,7 +58,7 @@ func TestUser_IsOrgMember(t *testing.T) { } func TestUser_GetTeam(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) team, err := org.GetTeam("team1") assert.NoError(t, err) @@ -73,7 +74,7 @@ func TestUser_GetTeam(t *testing.T) { } func TestUser_GetOwnerTeam(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) team, err := org.GetOwnerTeam() assert.NoError(t, err) @@ -85,7 +86,7 @@ func TestUser_GetOwnerTeam(t *testing.T) { } func TestUser_GetTeams(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) assert.NoError(t, org.LoadTeams()) if assert.Len(t, org.Teams, 4) { @@ -97,7 +98,7 @@ func TestUser_GetTeams(t *testing.T) { } func TestUser_GetMembers(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) assert.NoError(t, org.GetMembers()) if assert.Len(t, org.Members, 3) { @@ -108,7 +109,7 @@ func TestUser_GetMembers(t *testing.T) { } func TestUser_AddMember(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) // add a user that is not a member @@ -131,7 +132,7 @@ func TestUser_AddMember(t *testing.T) { } func TestUser_RemoveMember(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) // remove a user that is a member @@ -154,7 +155,7 @@ func TestUser_RemoveMember(t *testing.T) { } func TestUser_RemoveOrgRepo(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) repo := db.AssertExistsAndLoadBean(t, &Repository{OwnerID: org.ID}).(*Repository) @@ -178,7 +179,7 @@ func TestUser_RemoveOrgRepo(t *testing.T) { func TestCreateOrganization(t *testing.T) { // successful creation of org - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) const newOrgName = "neworg" @@ -198,7 +199,7 @@ func TestCreateOrganization(t *testing.T) { func TestCreateOrganization2(t *testing.T) { // unauthorized creation of org - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) owner := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) const newOrgName = "neworg" @@ -216,7 +217,7 @@ func TestCreateOrganization2(t *testing.T) { func TestCreateOrganization3(t *testing.T) { // create org with same name as existent org - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) org := &User{Name: "user3"} // should already exist @@ -229,7 +230,7 @@ func TestCreateOrganization3(t *testing.T) { func TestCreateOrganization4(t *testing.T) { // create org with unusable name - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) err := CreateOrganization(&User{Name: "assets"}, owner) @@ -239,7 +240,7 @@ func TestCreateOrganization4(t *testing.T) { } func TestGetOrgByName(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) org, err := GetOrgByName("user3") assert.NoError(t, err) @@ -254,14 +255,14 @@ func TestGetOrgByName(t *testing.T) { } func TestCountOrganizations(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) expected, err := db.GetEngine(db.DefaultContext).Where("type=?", UserTypeOrganization).Count(&User{}) assert.NoError(t, err) assert.Equal(t, expected, CountOrganizations()) } func TestDeleteOrganization(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) org := db.AssertExistsAndLoadBean(t, &User{ID: 6}).(*User) assert.NoError(t, DeleteOrganization(org)) db.AssertNotExistsBean(t, &User{ID: 6}) @@ -279,7 +280,7 @@ func TestDeleteOrganization(t *testing.T) { } func TestIsOrganizationOwner(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) test := func(orgID, userID int64, expected bool) { isOwner, err := IsOrganizationOwner(orgID, userID) assert.NoError(t, err) @@ -293,7 +294,7 @@ func TestIsOrganizationOwner(t *testing.T) { } func TestIsOrganizationMember(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) test := func(orgID, userID int64, expected bool) { isMember, err := IsOrganizationMember(orgID, userID) assert.NoError(t, err) @@ -308,7 +309,7 @@ func TestIsOrganizationMember(t *testing.T) { } func TestIsPublicMembership(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) test := func(orgID, userID int64, expected bool) { isMember, err := IsPublicMembership(orgID, userID) assert.NoError(t, err) @@ -323,7 +324,7 @@ func TestIsPublicMembership(t *testing.T) { } func TestGetOrgsByUserID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) orgs, err := GetOrgsByUserID(4, true) assert.NoError(t, err) @@ -337,7 +338,7 @@ func TestGetOrgsByUserID(t *testing.T) { } func TestGetOwnedOrgsByUserID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) orgs, err := GetOwnedOrgsByUserID(2) assert.NoError(t, err) @@ -351,7 +352,7 @@ func TestGetOwnedOrgsByUserID(t *testing.T) { } func TestGetOwnedOrgsByUserIDDesc(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) orgs, err := GetOwnedOrgsByUserIDDesc(5, "id") assert.NoError(t, err) @@ -366,7 +367,7 @@ func TestGetOwnedOrgsByUserIDDesc(t *testing.T) { } func TestGetOrgUsersByUserID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) orgUsers, err := GetOrgUsersByUserID(5, &SearchOrganizationsOptions{All: true}) assert.NoError(t, err) @@ -396,7 +397,7 @@ func TestGetOrgUsersByUserID(t *testing.T) { } func TestGetOrgUsersByOrgID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) orgUsers, err := GetOrgUsersByOrgID(&FindOrgMembersOpts{ ListOptions: db.ListOptions{}, @@ -429,7 +430,7 @@ func TestGetOrgUsersByOrgID(t *testing.T) { } func TestChangeOrgUserStatus(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(orgID, userID int64, public bool) { assert.NoError(t, ChangeOrgUserStatus(orgID, userID, public)) @@ -444,7 +445,7 @@ func TestChangeOrgUserStatus(t *testing.T) { } func TestAddOrgUser(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(orgID, userID int64, isPublic bool) { org := db.AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User) expectedNumMembers := org.NumMembers @@ -471,7 +472,7 @@ func TestAddOrgUser(t *testing.T) { } func TestRemoveOrgUser(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(orgID, userID int64) { org := db.AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User) expectedNumMembers := org.NumMembers @@ -494,7 +495,7 @@ func TestRemoveOrgUser(t *testing.T) { } func TestUser_GetUserTeamIDs(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) testSuccess := func(userID int64, expected []int64) { teamIDs, err := org.GetUserTeamIDs(userID) @@ -507,7 +508,7 @@ func TestUser_GetUserTeamIDs(t *testing.T) { } func TestAccessibleReposEnv_CountRepos(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) testSuccess := func(userID, expectedCount int64) { env, err := org.AccessibleReposEnv(userID) @@ -521,7 +522,7 @@ func TestAccessibleReposEnv_CountRepos(t *testing.T) { } func TestAccessibleReposEnv_RepoIDs(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) testSuccess := func(userID, _, pageSize int64, expectedRepoIDs []int64) { env, err := org.AccessibleReposEnv(userID) @@ -535,7 +536,7 @@ func TestAccessibleReposEnv_RepoIDs(t *testing.T) { } func TestAccessibleReposEnv_Repos(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) testSuccess := func(userID int64, expectedRepoIDs []int64) { env, err := org.AccessibleReposEnv(userID) @@ -554,7 +555,7 @@ func TestAccessibleReposEnv_Repos(t *testing.T) { } func TestAccessibleReposEnv_MirrorRepos(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) testSuccess := func(userID int64, expectedRepoIDs []int64) { env, err := org.AccessibleReposEnv(userID) @@ -573,7 +574,7 @@ func TestAccessibleReposEnv_MirrorRepos(t *testing.T) { } func TestHasOrgVisibleTypePublic(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) user3 := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) @@ -596,7 +597,7 @@ func TestHasOrgVisibleTypePublic(t *testing.T) { } func TestHasOrgVisibleTypeLimited(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) user3 := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) @@ -619,7 +620,7 @@ func TestHasOrgVisibleTypeLimited(t *testing.T) { } func TestHasOrgVisibleTypePrivate(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) user3 := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) @@ -642,7 +643,7 @@ func TestHasOrgVisibleTypePrivate(t *testing.T) { } func TestGetUsersWhoCanCreateOrgRepo(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) users, err := GetUsersWhoCanCreateOrgRepo(3) assert.NoError(t, err) diff --git a/models/project_test.go b/models/project_test.go index 8c630d8bed..70dabb7674 100644 --- a/models/project_test.go +++ b/models/project_test.go @@ -7,7 +7,7 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/timeutil" "github.com/stretchr/testify/assert" @@ -32,7 +32,7 @@ func TestIsProjectTypeValid(t *testing.T) { } func TestGetProjects(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) projects, _, err := GetProjects(ProjectSearchOptions{RepoID: 1}) assert.NoError(t, err) @@ -48,7 +48,7 @@ func TestGetProjects(t *testing.T) { } func TestProject(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) project := &Project{ Type: ProjectTypeRepository, diff --git a/models/protected_tag_test.go b/models/protected_tag_test.go index fd29f7e64b..ed838483d2 100644 --- a/models/protected_tag_test.go +++ b/models/protected_tag_test.go @@ -7,12 +7,13 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" + "github.com/stretchr/testify/assert" ) func TestIsUserAllowed(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) pt := &ProtectedTag{} allowed, err := pt.IsUserAllowed(1) diff --git a/models/pull_test.go b/models/pull_test.go index e6855240db..54083355ab 100644 --- a/models/pull_test.go +++ b/models/pull_test.go @@ -9,11 +9,12 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unit" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestPullRequest_LoadAttributes(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) assert.NoError(t, pr.LoadAttributes()) assert.NotNil(t, pr.Merger) @@ -21,7 +22,7 @@ func TestPullRequest_LoadAttributes(t *testing.T) { } func TestPullRequest_LoadIssue(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) assert.NoError(t, pr.LoadIssue()) assert.NotNil(t, pr.Issue) @@ -32,7 +33,7 @@ func TestPullRequest_LoadIssue(t *testing.T) { } func TestPullRequest_LoadBaseRepo(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) assert.NoError(t, pr.LoadBaseRepo()) assert.NotNil(t, pr.BaseRepo) @@ -43,7 +44,7 @@ func TestPullRequest_LoadBaseRepo(t *testing.T) { } func TestPullRequest_LoadHeadRepo(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) assert.NoError(t, pr.LoadHeadRepo()) assert.NotNil(t, pr.HeadRepo) @@ -55,7 +56,7 @@ func TestPullRequest_LoadHeadRepo(t *testing.T) { // TODO TestNewPullRequest func TestPullRequestsNewest(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) prs, count, err := PullRequests(1, &PullRequestsOptions{ ListOptions: db.ListOptions{ Page: 1, @@ -74,7 +75,7 @@ func TestPullRequestsNewest(t *testing.T) { } func TestPullRequestsOldest(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) prs, count, err := PullRequests(1, &PullRequestsOptions{ ListOptions: db.ListOptions{ Page: 1, @@ -93,7 +94,7 @@ func TestPullRequestsOldest(t *testing.T) { } func TestGetUnmergedPullRequest(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) pr, err := GetUnmergedPullRequest(1, 1, "branch2", "master", PullRequestFlowGithub) assert.NoError(t, err) assert.Equal(t, int64(2), pr.ID) @@ -104,7 +105,7 @@ func TestGetUnmergedPullRequest(t *testing.T) { } func TestGetUnmergedPullRequestsByHeadInfo(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) prs, err := GetUnmergedPullRequestsByHeadInfo(1, "branch2") assert.NoError(t, err) assert.Len(t, prs, 1) @@ -115,7 +116,7 @@ func TestGetUnmergedPullRequestsByHeadInfo(t *testing.T) { } func TestGetUnmergedPullRequestsByBaseInfo(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) prs, err := GetUnmergedPullRequestsByBaseInfo(1, "master") assert.NoError(t, err) assert.Len(t, prs, 1) @@ -126,7 +127,7 @@ func TestGetUnmergedPullRequestsByBaseInfo(t *testing.T) { } func TestGetPullRequestByIndex(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) pr, err := GetPullRequestByIndex(1, 2) assert.NoError(t, err) assert.Equal(t, int64(1), pr.BaseRepoID) @@ -142,7 +143,7 @@ func TestGetPullRequestByIndex(t *testing.T) { } func TestGetPullRequestByID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) pr, err := GetPullRequestByID(1) assert.NoError(t, err) assert.Equal(t, int64(1), pr.ID) @@ -154,7 +155,7 @@ func TestGetPullRequestByID(t *testing.T) { } func TestGetPullRequestByIssueID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) pr, err := GetPullRequestByIssueID(2) assert.NoError(t, err) assert.Equal(t, int64(2), pr.IssueID) @@ -165,7 +166,7 @@ func TestGetPullRequestByIssueID(t *testing.T) { } func TestPullRequest_Update(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) pr.BaseBranch = "baseBranch" pr.HeadBranch = "headBranch" @@ -178,7 +179,7 @@ func TestPullRequest_Update(t *testing.T) { } func TestPullRequest_UpdateCols(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) pr := &PullRequest{ ID: 1, BaseBranch: "baseBranch", @@ -193,7 +194,7 @@ func TestPullRequest_UpdateCols(t *testing.T) { } func TestPullRequestList_LoadAttributes(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) prs := []*PullRequest{ db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest), @@ -211,7 +212,7 @@ func TestPullRequestList_LoadAttributes(t *testing.T) { // TODO TestAddTestPullRequestTask func TestPullRequest_IsWorkInProgress(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) pr.LoadIssue() @@ -226,7 +227,7 @@ func TestPullRequest_IsWorkInProgress(t *testing.T) { } func TestPullRequest_GetWorkInProgressPrefixWorkInProgress(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) pr.LoadIssue() @@ -242,7 +243,7 @@ func TestPullRequest_GetWorkInProgressPrefixWorkInProgress(t *testing.T) { } func TestPullRequest_GetDefaultMergeMessage_InternalTracker(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) assert.Equal(t, "Merge pull request 'issue3' (#3) from branch2 into master", pr.GetDefaultMergeMessage()) @@ -253,7 +254,7 @@ func TestPullRequest_GetDefaultMergeMessage_InternalTracker(t *testing.T) { } func TestPullRequest_GetDefaultMergeMessage_ExternalTracker(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) externalTracker := RepoUnit{ Type: unit.TypeExternalTracker, diff --git a/models/repo_collaboration_test.go b/models/repo_collaboration_test.go index 326fb4dbf7..5e6a0a5d94 100644 --- a/models/repo_collaboration_test.go +++ b/models/repo_collaboration_test.go @@ -8,11 +8,12 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestRepository_AddCollaborator(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(repoID, userID int64) { repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) @@ -27,7 +28,7 @@ func TestRepository_AddCollaborator(t *testing.T) { } func TestRepository_GetCollaborators(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) test := func(repoID int64) { repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) collaborators, err := repo.GetCollaborators(db.ListOptions{}) @@ -47,7 +48,7 @@ func TestRepository_GetCollaborators(t *testing.T) { } func TestRepository_IsCollaborator(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) test := func(repoID, userID int64, expected bool) { repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) @@ -62,7 +63,7 @@ func TestRepository_IsCollaborator(t *testing.T) { } func TestRepository_ChangeCollaborationAccessMode(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) assert.NoError(t, repo.ChangeCollaborationAccessMode(4, AccessModeAdmin)) @@ -81,7 +82,7 @@ func TestRepository_ChangeCollaborationAccessMode(t *testing.T) { } func TestRepository_DeleteCollaboration(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) assert.NoError(t, repo.GetOwner()) diff --git a/models/repo_list_test.go b/models/repo_list_test.go index 3c30cad564..eaf5e8a7ce 100644 --- a/models/repo_list_test.go +++ b/models/repo_list_test.go @@ -8,13 +8,14 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/util" "github.com/stretchr/testify/assert" ) func TestSearchRepository(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // test search public repository on explore page repos, count, err := SearchRepositoryByName(&SearchRepoOptions{ @@ -324,7 +325,7 @@ func TestSearchRepository(t *testing.T) { } func TestSearchRepositoryByTopicName(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) testCases := []struct { name string diff --git a/models/repo_permission_test.go b/models/repo_permission_test.go index 5e43937776..1f04a45315 100644 --- a/models/repo_permission_test.go +++ b/models/repo_permission_test.go @@ -9,11 +9,12 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unit" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestRepoPermissionPublicNonOrgRepo(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // public non-organization repo repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) @@ -66,7 +67,7 @@ func TestRepoPermissionPublicNonOrgRepo(t *testing.T) { } func TestRepoPermissionPrivateNonOrgRepo(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // private non-organization repo repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) @@ -118,7 +119,7 @@ func TestRepoPermissionPrivateNonOrgRepo(t *testing.T) { } func TestRepoPermissionPublicOrgRepo(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // public organization repo repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 32}).(*Repository) @@ -180,7 +181,7 @@ func TestRepoPermissionPublicOrgRepo(t *testing.T) { } func TestRepoPermissionPrivateOrgRepo(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // private organization repo repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 24}).(*Repository) diff --git a/models/repo_pushmirror_test.go b/models/repo_pushmirror_test.go index 65ef918141..aa4465082f 100644 --- a/models/repo_pushmirror_test.go +++ b/models/repo_pushmirror_test.go @@ -8,14 +8,14 @@ import ( "testing" "time" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/timeutil" "github.com/stretchr/testify/assert" ) func TestPushMirrorsIterate(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) now := timeutil.TimeStampNow() diff --git a/models/repo_redirect_test.go b/models/repo_redirect_test.go index 9400422752..bf42d4854c 100644 --- a/models/repo_redirect_test.go +++ b/models/repo_redirect_test.go @@ -8,11 +8,12 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestLookupRepoRedirect(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repoID, err := LookupRepoRedirect(2, "oldrepo1") assert.NoError(t, err) @@ -24,7 +25,7 @@ func TestLookupRepoRedirect(t *testing.T) { func TestNewRepoRedirect(t *testing.T) { // redirect to a completely new name - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) assert.NoError(t, newRepoRedirect(db.GetEngine(db.DefaultContext), repo.OwnerID, repo.ID, repo.Name, "newreponame")) @@ -43,7 +44,7 @@ func TestNewRepoRedirect(t *testing.T) { func TestNewRepoRedirect2(t *testing.T) { // redirect to previously used name - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) assert.NoError(t, newRepoRedirect(db.GetEngine(db.DefaultContext), repo.OwnerID, repo.ID, repo.Name, "oldrepo1")) @@ -62,7 +63,7 @@ func TestNewRepoRedirect2(t *testing.T) { func TestNewRepoRedirect3(t *testing.T) { // redirect for a previously-unredirected repo - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) assert.NoError(t, newRepoRedirect(db.GetEngine(db.DefaultContext), repo.OwnerID, repo.ID, repo.Name, "newreponame")) diff --git a/models/repo_test.go b/models/repo_test.go index 425e8c0191..2eb6e817ec 100644 --- a/models/repo_test.go +++ b/models/repo_test.go @@ -14,13 +14,14 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unit" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/markup" "github.com/stretchr/testify/assert" ) func TestMetas(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := &Repository{Name: "testRepo"} repo.Owner = &User{Name: "testOwner"} @@ -68,7 +69,7 @@ func TestMetas(t *testing.T) { } func TestGetRepositoryCount(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) count, err1 := GetRepositoryCount(&User{ID: int64(10)}) privateCount, err2 := GetPrivateRepositoryCount(&User{ID: int64(10)}) @@ -81,7 +82,7 @@ func TestGetRepositoryCount(t *testing.T) { } func TestGetPublicRepositoryCount(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) count, err := GetPublicRepositoryCount(&User{ID: int64(10)}) assert.NoError(t, err) @@ -89,7 +90,7 @@ func TestGetPublicRepositoryCount(t *testing.T) { } func TestGetPrivateRepositoryCount(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) count, err := GetPrivateRepositoryCount(&User{ID: int64(10)}) assert.NoError(t, err) @@ -97,7 +98,7 @@ func TestGetPrivateRepositoryCount(t *testing.T) { } func TestUpdateRepositoryVisibilityChanged(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // Get sample repo and change visibility repo, err := GetRepositoryByID(9) @@ -117,7 +118,7 @@ func TestUpdateRepositoryVisibilityChanged(t *testing.T) { } func TestGetUserFork(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // User13 has repo 11 forked from repo10 repo, err := GetRepositoryByID(10) @@ -136,7 +137,7 @@ func TestGetUserFork(t *testing.T) { } func TestRepoAPIURL(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository) assert.Equal(t, "https://try.gitea.io/api/v1/repos/user12/repo10", repo.APIURL()) @@ -148,7 +149,7 @@ func TestUploadAvatar(t *testing.T) { var buff bytes.Buffer png.Encode(&buff, myImage) - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository) err := repo.UploadAvatar(buff.Bytes()) @@ -162,7 +163,7 @@ func TestUploadBigAvatar(t *testing.T) { var buff bytes.Buffer png.Encode(&buff, myImage) - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository) err := repo.UploadAvatar(buff.Bytes()) @@ -175,7 +176,7 @@ func TestDeleteAvatar(t *testing.T) { var buff bytes.Buffer png.Encode(&buff, myImage) - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository) err := repo.UploadAvatar(buff.Bytes()) @@ -188,13 +189,13 @@ func TestDeleteAvatar(t *testing.T) { } func TestDoctorUserStarNum(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, DoctorUserStarNum()) } func TestRepoGetReviewers(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // test public repo repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) @@ -211,7 +212,7 @@ func TestRepoGetReviewers(t *testing.T) { } func TestRepoGetReviewerTeams(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo2 := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) teams, err := repo2.GetReviewerTeams() diff --git a/models/repo_transfer_test.go b/models/repo_transfer_test.go index 4c6b7254c2..71976def96 100644 --- a/models/repo_transfer_test.go +++ b/models/repo_transfer_test.go @@ -8,11 +8,12 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestRepositoryTransfer(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) doer := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) diff --git a/models/repo_watch_test.go b/models/repo_watch_test.go index 52222af2ca..9f20a088f5 100644 --- a/models/repo_watch_test.go +++ b/models/repo_watch_test.go @@ -8,13 +8,14 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" "github.com/stretchr/testify/assert" ) func TestIsWatching(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) assert.True(t, IsWatching(1, 1)) assert.True(t, IsWatching(4, 1)) @@ -26,7 +27,7 @@ func TestIsWatching(t *testing.T) { } func TestWatchRepo(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) const repoID = 3 const userID = 2 @@ -40,7 +41,7 @@ func TestWatchRepo(t *testing.T) { } func TestGetWatchers(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) watches, err := GetWatchers(repo.ID) @@ -57,7 +58,7 @@ func TestGetWatchers(t *testing.T) { } func TestRepository_GetWatchers(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) watchers, err := repo.GetWatchers(db.ListOptions{Page: 1}) @@ -74,7 +75,7 @@ func TestRepository_GetWatchers(t *testing.T) { } func TestNotifyWatchers(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) action := &Action{ ActUserID: 8, @@ -111,7 +112,7 @@ func TestNotifyWatchers(t *testing.T) { } func TestWatchIfAuto(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) watchers, err := repo.GetWatchers(db.ListOptions{Page: 1}) @@ -168,7 +169,7 @@ func TestWatchIfAuto(t *testing.T) { } func TestWatchRepoMode(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) db.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 0) diff --git a/models/review_test.go b/models/review_test.go index c809a3d662..31d6c45535 100644 --- a/models/review_test.go +++ b/models/review_test.go @@ -8,11 +8,12 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestGetReviewByID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) review, err := GetReviewByID(1) assert.NoError(t, err) assert.Equal(t, "Demo Review", review.Content) @@ -24,7 +25,7 @@ func TestGetReviewByID(t *testing.T) { } func TestReview_LoadAttributes(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) review := db.AssertExistsAndLoadBean(t, &Review{ID: 1}).(*Review) assert.NoError(t, review.LoadAttributes()) assert.NotNil(t, review.Issue) @@ -38,7 +39,7 @@ func TestReview_LoadAttributes(t *testing.T) { } func TestReview_LoadCodeComments(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) review := db.AssertExistsAndLoadBean(t, &Review{ID: 4}).(*Review) assert.NoError(t, review.LoadAttributes()) @@ -57,7 +58,7 @@ func TestReviewType_Icon(t *testing.T) { } func TestFindReviews(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) reviews, err := FindReviews(FindReviewOptions{ Type: ReviewTypeApprove, IssueID: 2, @@ -69,7 +70,7 @@ func TestFindReviews(t *testing.T) { } func TestGetCurrentReview(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue) user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) @@ -87,7 +88,7 @@ func TestGetCurrentReview(t *testing.T) { } func TestCreateReview(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue) user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) @@ -104,7 +105,7 @@ func TestCreateReview(t *testing.T) { } func TestGetReviewersByIssueID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 3}).(*Issue) user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) @@ -144,7 +145,7 @@ func TestGetReviewersByIssueID(t *testing.T) { } func TestDismissReview(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) rejectReviewExample := db.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) requestReviewExample := db.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) diff --git a/models/star_test.go b/models/star_test.go index 326da8a861..8e448ae7cf 100644 --- a/models/star_test.go +++ b/models/star_test.go @@ -8,11 +8,12 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestStarRepo(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) const userID = 2 const repoID = 1 db.AssertNotExistsBean(t, &Star{UID: userID, RepoID: repoID}) @@ -25,14 +26,14 @@ func TestStarRepo(t *testing.T) { } func TestIsStaring(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) assert.True(t, IsStaring(2, 4)) assert.False(t, IsStaring(3, 4)) } func TestRepository_GetStargazers(t *testing.T) { // repo with stargazers - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) gazers, err := repo.GetStargazers(db.ListOptions{Page: 0}) assert.NoError(t, err) @@ -43,7 +44,7 @@ func TestRepository_GetStargazers(t *testing.T) { func TestRepository_GetStargazers2(t *testing.T) { // repo with stargazers - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) gazers, err := repo.GetStargazers(db.ListOptions{Page: 0}) assert.NoError(t, err) @@ -52,7 +53,7 @@ func TestRepository_GetStargazers2(t *testing.T) { func TestUser_GetStarredRepos(t *testing.T) { // user who has starred repos - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) starred, err := user.GetStarredRepos(false, 1, 10, "") @@ -71,7 +72,7 @@ func TestUser_GetStarredRepos(t *testing.T) { func TestUser_GetStarredRepos2(t *testing.T) { // user who has no starred repos - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) starred, err := user.GetStarredRepos(false, 1, 10, "") @@ -84,7 +85,7 @@ func TestUser_GetStarredRepos2(t *testing.T) { } func TestUserGetStarredRepoCount(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) counts, err := user.GetStarredRepoCount(false) diff --git a/models/token_test.go b/models/token_test.go index 21d827ea61..0161764596 100644 --- a/models/token_test.go +++ b/models/token_test.go @@ -8,11 +8,12 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestNewAccessToken(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) token := &AccessToken{ UID: 3, Name: "Token C", @@ -31,7 +32,7 @@ func TestNewAccessToken(t *testing.T) { func TestAccessTokenByNameExists(t *testing.T) { name := "Token Gitea" - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) token := &AccessToken{ UID: 3, Name: name, @@ -64,7 +65,7 @@ func TestAccessTokenByNameExists(t *testing.T) { } func TestGetAccessTokenBySHA(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) token, err := GetAccessTokenBySHA("d2c6c1ba3890b309189a8e618c72a162e4efbf36") assert.NoError(t, err) assert.Equal(t, int64(1), token.UID) @@ -82,7 +83,7 @@ func TestGetAccessTokenBySHA(t *testing.T) { } func TestListAccessTokens(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) tokens, err := ListAccessTokens(ListAccessTokensOptions{UserID: 1}) assert.NoError(t, err) if assert.Len(t, tokens, 2) { @@ -105,7 +106,7 @@ func TestListAccessTokens(t *testing.T) { } func TestUpdateAccessToken(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) token, err := GetAccessTokenBySHA("4c6f36e6cf498e2a448662f915d932c09c5a146c") assert.NoError(t, err) token.Name = "Token Z" @@ -115,7 +116,7 @@ func TestUpdateAccessToken(t *testing.T) { } func TestDeleteAccessTokenByID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) token, err := GetAccessTokenBySHA("4c6f36e6cf498e2a448662f915d932c09c5a146c") assert.NoError(t, err) diff --git a/models/topic_test.go b/models/topic_test.go index b069deaba3..def946b666 100644 --- a/models/topic_test.go +++ b/models/topic_test.go @@ -8,6 +8,7 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -16,7 +17,7 @@ func TestAddTopic(t *testing.T) { repo1NrOfTopics := 3 repo2NrOfTopics := 2 - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) topics, _, err := FindTopics(&FindTopicOptions{}) assert.NoError(t, err) diff --git a/models/unittest/bridge.go b/models/unittest/bridge.go new file mode 100644 index 0000000000..776dd69519 --- /dev/null +++ b/models/unittest/bridge.go @@ -0,0 +1,54 @@ +// 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/db/test_fixtures.go b/models/unittest/fixtures.go index 2715b688ea..af60df7b68 100644 --- a/models/db/test_fixtures.go +++ b/models/unittest/fixtures.go @@ -1,28 +1,34 @@ -// Copyright 2017 The Gitea Authors. All rights reserved. +// 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 db +package unittest import ( "fmt" "os" "time" + "code.gitea.io/gitea/models/db" + "github.com/go-testfixtures/testfixtures/v3" + "xorm.io/xorm" "xorm.io/xorm/schemas" ) var fixtures *testfixtures.Loader -// InitFixtures initialize test fixtures for a test database -func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) { - e := x +func getXORMEngine(engine ...*xorm.Engine) (x *xorm.Engine) { if len(engine) == 1 { - e = engine[0] + return engine[0] } + return db.DefaultContext.(*db.Context).Engine().(*xorm.Engine) +} +// InitFixtures initialize test fixtures for a test database +func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) { + e := getXORMEngine(engine...) var testfiles func(*testfixtures.Loader) error if opts.Dir != "" { testfiles = testfixtures.Directory(opts.Dir) @@ -64,10 +70,7 @@ func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) { // LoadFixtures load fixtures for a test database func LoadFixtures(engine ...*xorm.Engine) error { - e := x - if len(engine) == 1 { - e = engine[0] - } + e := getXORMEngine(engine...) var err error // Database transaction conflicts could occur and result in ROLLBACK // As a simple workaround, we just retry 20 times. diff --git a/models/unittest/testdb.go b/models/unittest/testdb.go new file mode 100644 index 0000000000..b4f8c813af --- /dev/null +++ b/models/unittest/testdb.go @@ -0,0 +1,154 @@ +// 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 ( + "fmt" + "net/url" + "os" + "path/filepath" + "testing" + + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/modules/base" + "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/storage" + "code.gitea.io/gitea/modules/util" + + "github.com/stretchr/testify/assert" + + "xorm.io/xorm" + "xorm.io/xorm/names" +) + +// giteaRoot a path to the gitea root +var ( + giteaRoot string + fixturesDir string +) + +// FixturesDir returns the fixture directory +func FixturesDir() string { + return fixturesDir +} + +func fatalTestError(fmtStr string, args ...interface{}) { + _, _ = fmt.Fprintf(os.Stderr, fmtStr, args...) + os.Exit(1) +} + +// MainTest a reusable TestMain(..) function for unit tests that need to use a +// 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") + + var opts FixturesOptions + if len(fixtureFiles) == 0 { + opts.Dir = fixturesDir + } else { + for _, f := range fixtureFiles { + if len(f) != 0 { + opts.Files = append(opts.Files, filepath.Join(fixturesDir, f)) + } + } + } + + if err = CreateTestEngine(opts); err != nil { + fatalTestError("Error creating test engine: %v\n", err) + } + + setting.AppURL = "https://try.gitea.io/" + setting.RunUser = "runuser" + setting.SSH.Port = 3000 + setting.SSH.Domain = "try.gitea.io" + setting.Database.UseSQLite3 = true + setting.RepoRootPath, err = os.MkdirTemp(os.TempDir(), "repos") + if err != nil { + fatalTestError("TempDir: %v\n", err) + } + setting.AppDataPath, err = os.MkdirTemp(os.TempDir(), "appdata") + if err != nil { + fatalTestError("TempDir: %v\n", err) + } + setting.AppWorkPath = pathToGiteaRoot + setting.StaticRootPath = pathToGiteaRoot + setting.GravatarSourceURL, err = url.Parse("https://secure.gravatar.com/avatar/") + if err != nil { + fatalTestError("url.Parse: %v\n", err) + } + setting.Attachment.Storage.Path = filepath.Join(setting.AppDataPath, "attachments") + + setting.LFS.Storage.Path = filepath.Join(setting.AppDataPath, "lfs") + + setting.Avatar.Storage.Path = filepath.Join(setting.AppDataPath, "avatars") + + setting.RepoAvatar.Storage.Path = filepath.Join(setting.AppDataPath, "repo-avatars") + + setting.RepoArchive.Storage.Path = filepath.Join(setting.AppDataPath, "repo-archive") + + if err = storage.Init(); err != nil { + fatalTestError("storage.Init: %v\n", err) + } + + if err = util.RemoveAll(setting.RepoRootPath); err != nil { + fatalTestError("util.RemoveAll: %v\n", err) + } + if err = util.CopyDir(filepath.Join(pathToGiteaRoot, "integrations", "gitea-repositories-meta"), setting.RepoRootPath); err != nil { + fatalTestError("util.CopyDir: %v\n", err) + } + + exitStatus := m.Run() + if err = util.RemoveAll(setting.RepoRootPath); err != nil { + fatalTestError("util.RemoveAll: %v\n", err) + } + if err = util.RemoveAll(setting.AppDataPath); err != nil { + fatalTestError("util.RemoveAll: %v\n", err) + } + os.Exit(exitStatus) +} + +// FixturesOptions fixtures needs to be loaded options +type FixturesOptions struct { + Dir string + Files []string +} + +// CreateTestEngine creates a memory database and loads the fixture data from fixturesDir +func CreateTestEngine(opts FixturesOptions) error { + x, err := xorm.NewEngine("sqlite3", "file::memory:?cache=shared&_txlock=immediate") + if err != nil { + return err + } + x.SetMapper(names.GonicMapper{}) + db.SetUnitTestEngine(x) + + if err = db.SyncAllTables(); err != nil { + return err + } + switch os.Getenv("GITEA_UNIT_TESTS_VERBOSE") { + case "true", "1": + x.ShowSQL(true) + } + + return InitFixtures(opts) +} + +// PrepareTestDatabase load test fixtures into test database +func PrepareTestDatabase() error { + return LoadFixtures() +} + +// PrepareTestEnv prepares the environment for unit tests. Can only be called +// by tests that use the above MainTest(..) function. +func PrepareTestEnv(t testing.TB) { + assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, util.RemoveAll(setting.RepoRootPath)) + metaPath := filepath.Join(giteaRoot, "integrations", "gitea-repositories-meta") + assert.NoError(t, util.CopyDir(metaPath, setting.RepoRootPath)) + base.SetupGiteaRoot() // Makes sure GITEA_ROOT is set +} diff --git a/models/user/email_address_test.go b/models/user/email_address_test.go index 5ed0bf8884..642c2b3876 100644 --- a/models/user/email_address_test.go +++ b/models/user/email_address_test.go @@ -8,12 +8,13 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestGetEmailAddresses(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) emails, _ := GetEmailAddresses(int64(1)) if assert.Len(t, emails, 3) { @@ -30,7 +31,7 @@ func TestGetEmailAddresses(t *testing.T) { } func TestIsEmailUsed(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) isExist, _ := IsEmailUsed(db.DefaultContext, "") assert.True(t, isExist) @@ -41,7 +42,7 @@ func TestIsEmailUsed(t *testing.T) { } func TestAddEmailAddress(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, AddEmailAddress(&EmailAddress{ Email: "user1234567890@example.com", @@ -60,7 +61,7 @@ func TestAddEmailAddress(t *testing.T) { } func TestAddEmailAddresses(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // insert multiple email address emails := make([]*EmailAddress, 2) @@ -83,7 +84,7 @@ func TestAddEmailAddresses(t *testing.T) { } func TestDeleteEmailAddress(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, DeleteEmailAddress(&EmailAddress{ UID: int64(1), @@ -108,7 +109,7 @@ func TestDeleteEmailAddress(t *testing.T) { } func TestDeleteEmailAddresses(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // delete multiple email address emails := make([]*EmailAddress, 2) diff --git a/models/user/main_test.go b/models/user/main_test.go index 2999c4c81d..1dd9fb2781 100644 --- a/models/user/main_test.go +++ b/models/user/main_test.go @@ -8,11 +8,11 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", ".."), + unittest.MainTest(m, filepath.Join("..", ".."), "email_address.yml", "user_redirect.yml", ) diff --git a/models/user/redirect_test.go b/models/user/redirect_test.go index b33c42cf3d..82a0a0a2e2 100644 --- a/models/user/redirect_test.go +++ b/models/user/redirect_test.go @@ -7,12 +7,13 @@ package user import ( "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" + "github.com/stretchr/testify/assert" ) func TestLookupUserRedirect(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) userID, err := LookupUserRedirect("olduser1") assert.NoError(t, err) diff --git a/models/user_email_test.go b/models/user_email_test.go index 8c2bb48b94..bde0778b64 100644 --- a/models/user_email_test.go +++ b/models/user_email_test.go @@ -8,6 +8,7 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/util" @@ -15,7 +16,7 @@ import ( ) func TestMakeEmailPrimary(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) email := &user_model.EmailAddress{ Email: "user567890@example.com", @@ -49,7 +50,7 @@ func TestMakeEmailPrimary(t *testing.T) { } func TestActivate(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) email := &user_model.EmailAddress{ ID: int64(1), @@ -68,7 +69,7 @@ func TestActivate(t *testing.T) { } func TestListEmails(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // Must find all users and their emails opts := &SearchEmailOptions{ diff --git a/models/user_follow_test.go b/models/user_follow_test.go index ae8faad6fa..c7e56410f0 100644 --- a/models/user_follow_test.go +++ b/models/user_follow_test.go @@ -8,11 +8,12 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestIsFollowing(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) assert.True(t, IsFollowing(4, 2)) assert.False(t, IsFollowing(2, 4)) assert.False(t, IsFollowing(5, db.NonexistentID)) @@ -21,7 +22,7 @@ func TestIsFollowing(t *testing.T) { } func TestFollowUser(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(followerID, followedID int64) { assert.NoError(t, FollowUser(followerID, followedID)) @@ -36,7 +37,7 @@ func TestFollowUser(t *testing.T) { } func TestUnfollowUser(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(followerID, followedID int64) { assert.NoError(t, UnfollowUser(followerID, followedID)) diff --git a/models/user_heatmap_test.go b/models/user_heatmap_test.go index 8d2002b1a0..85e678e933 100644 --- a/models/user_heatmap_test.go +++ b/models/user_heatmap_test.go @@ -10,6 +10,7 @@ import ( "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" @@ -39,7 +40,7 @@ func TestGetUserHeatmapDataByUser(t *testing.T) { {10, 10, 3, `[{"timestamp":1603009800,"contributions":1},{"timestamp":1603010700,"contributions":2}]`}, } // Prepare - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // Mock time timeutil.Set(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)) diff --git a/models/user_openid_test.go b/models/user_openid_test.go index 3c17b0742c..d0d801ad18 100644 --- a/models/user_openid_test.go +++ b/models/user_openid_test.go @@ -7,12 +7,13 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" + "github.com/stretchr/testify/assert" ) func TestGetUserOpenIDs(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) oids, err := GetUserOpenIDs(int64(1)) if assert.NoError(t, err) && assert.Len(t, oids, 2) { @@ -30,7 +31,7 @@ func TestGetUserOpenIDs(t *testing.T) { } func TestGetUserByOpenID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) _, err := GetUserByOpenID("https://unknown") if assert.Error(t, err) { @@ -49,7 +50,7 @@ func TestGetUserByOpenID(t *testing.T) { } func TestToggleUserOpenIDVisibility(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) oids, err := GetUserOpenIDs(int64(2)) if !assert.NoError(t, err) || !assert.Len(t, oids, 1) { return diff --git a/models/user_test.go b/models/user_test.go index 58044d1e99..9a91a9b257 100644 --- a/models/user_test.go +++ b/models/user_test.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/login" + "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/structs" @@ -21,7 +22,7 @@ import ( ) func TestOAuth2Application_LoadUser(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) app := db.AssertExistsAndLoadBean(t, &login.OAuth2Application{ID: 1}).(*login.OAuth2Application) user, err := GetUserByID(app.UID) assert.NoError(t, err) @@ -29,7 +30,7 @@ func TestOAuth2Application_LoadUser(t *testing.T) { } func TestUserIsPublicMember(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) tt := []struct { uid int64 @@ -55,7 +56,7 @@ func testUserIsPublicMember(t *testing.T, uid, orgID int64, expected bool) { } func TestIsUserOrgOwner(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) tt := []struct { uid int64 @@ -81,7 +82,7 @@ func testIsUserOrgOwner(t *testing.T, uid, orgID int64, expected bool) { } func TestGetUserEmailsByNames(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // ignore none active user email assert.Equal(t, []string{"user8@example.com"}, GetUserEmailsByNames([]string{"user8", "user9"})) @@ -91,7 +92,7 @@ func TestGetUserEmailsByNames(t *testing.T) { } func TestCanCreateOrganization(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) admin := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) assert.True(t, admin.CanCreateOrganization()) @@ -109,7 +110,7 @@ func TestCanCreateOrganization(t *testing.T) { } func TestSearchUsers(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(opts *SearchUserOptions, expectedUserOrOrgIDs []int64) { users, _, err := SearchUsers(opts) assert.NoError(t, err) @@ -178,7 +179,7 @@ func TestSearchUsers(t *testing.T) { func TestDeleteUser(t *testing.T) { test := func(userID int64) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &User{ID: userID}).(*User) ownedRepos := make([]*Repository, 0, 10) @@ -212,7 +213,7 @@ func TestDeleteUser(t *testing.T) { } func TestEmailNotificationPreferences(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) for _, test := range []struct { expected string @@ -280,7 +281,7 @@ func BenchmarkHashPassword(b *testing.B) { } func TestGetOrgRepositoryIDs(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + 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) @@ -393,7 +394,7 @@ func TestCreateUser_Issue5882(t *testing.T) { } func TestGetUserIDsByNames(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // ignore non existing IDs, err := GetUserIDsByNames([]string{"user1", "user2", "none_existing_user"}, true) @@ -407,7 +408,7 @@ func TestGetUserIDsByNames(t *testing.T) { } func TestGetMaileableUsersByIDs(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) results, err := GetMaileableUsersByIDs([]int64{1, 4}, false) assert.NoError(t, err) @@ -426,7 +427,7 @@ func TestGetMaileableUsersByIDs(t *testing.T) { } func TestAddLdapSSHPublicKeys(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) s := &login.Source{ID: 1} @@ -494,7 +495,7 @@ ssh-dss AAAAB3NzaC1kc3MAAACBAOChCC7lf6Uo9n7BmZ6M8St19PZf4Tn59NriyboW2x/DZuYAz3ib } func TestUpdateUser(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) user.KeepActivityPrivate = true @@ -515,7 +516,7 @@ func TestUpdateUser(t *testing.T) { func TestNewUserRedirect(t *testing.T) { // redirect to a completely new name - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) assert.NoError(t, user_model.NewUserRedirect(db.DefaultContext, user.ID, user.Name, "newusername")) @@ -532,7 +533,7 @@ func TestNewUserRedirect(t *testing.T) { func TestNewUserRedirect2(t *testing.T) { // redirect to previously used name - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) assert.NoError(t, user_model.NewUserRedirect(db.DefaultContext, user.ID, user.Name, "olduser1")) @@ -549,7 +550,7 @@ func TestNewUserRedirect2(t *testing.T) { func TestNewUserRedirect3(t *testing.T) { // redirect for a previously-unredirected user - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) assert.NoError(t, user_model.NewUserRedirect(db.DefaultContext, user.ID, user.Name, "newusername")) diff --git a/models/userlist_test.go b/models/userlist_test.go index 9e75f783f1..2b8518c92f 100644 --- a/models/userlist_test.go +++ b/models/userlist_test.go @@ -8,12 +8,13 @@ import ( "fmt" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" + "github.com/stretchr/testify/assert" ) func TestUserListIsPublicMember(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) tt := []struct { orgid int64 expected map[int64]bool @@ -39,7 +40,7 @@ func testUserListIsPublicMember(t *testing.T, orgID int64, expected map[int64]bo } func TestUserListIsUserOrgOwner(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) tt := []struct { orgid int64 expected map[int64]bool @@ -65,7 +66,7 @@ func testUserListIsUserOrgOwner(t *testing.T, orgID int64, expected map[int64]bo } func TestUserListIsTwoFaEnrolled(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) tt := []struct { orgid int64 expected map[int64]bool diff --git a/models/webhook/main_test.go b/models/webhook/main_test.go index f94612a755..89c947b061 100644 --- a/models/webhook/main_test.go +++ b/models/webhook/main_test.go @@ -8,9 +8,9 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", ".."), "webhook.yml", "hook_task.yml") + unittest.MainTest(m, filepath.Join("..", ".."), "webhook.yml", "hook_task.yml") } diff --git a/models/webhook/webhook_test.go b/models/webhook/webhook_test.go index df2c37b355..6d7957ba64 100644 --- a/models/webhook/webhook_test.go +++ b/models/webhook/webhook_test.go @@ -10,6 +10,7 @@ import ( "time" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" @@ -29,7 +30,7 @@ func TestIsValidHookContentType(t *testing.T) { } func TestWebhook_History(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) webhook := db.AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook) tasks, err := webhook.History(0) assert.NoError(t, err) @@ -44,7 +45,7 @@ func TestWebhook_History(t *testing.T) { } func TestWebhook_UpdateEvent(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) webhook := db.AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook) hookEvent := &HookEvent{ PushOnly: true, @@ -97,7 +98,7 @@ func TestCreateWebhook(t *testing.T) { } func TestGetWebhookByRepoID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) hook, err := GetWebhookByRepoID(1, 1) assert.NoError(t, err) assert.Equal(t, int64(1), hook.ID) @@ -108,7 +109,7 @@ func TestGetWebhookByRepoID(t *testing.T) { } func TestGetWebhookByOrgID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) hook, err := GetWebhookByOrgID(3, 3) assert.NoError(t, err) assert.Equal(t, int64(3), hook.ID) @@ -119,7 +120,7 @@ func TestGetWebhookByOrgID(t *testing.T) { } func TestGetActiveWebhooksByRepoID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) hooks, err := ListWebhooksByOpts(&ListWebhookOptions{RepoID: 1, IsActive: util.OptionalBoolTrue}) assert.NoError(t, err) if assert.Len(t, hooks, 1) { @@ -129,7 +130,7 @@ func TestGetActiveWebhooksByRepoID(t *testing.T) { } func TestGetWebhooksByRepoID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) hooks, err := ListWebhooksByOpts(&ListWebhookOptions{RepoID: 1}) assert.NoError(t, err) if assert.Len(t, hooks, 2) { @@ -139,7 +140,7 @@ func TestGetWebhooksByRepoID(t *testing.T) { } func TestGetActiveWebhooksByOrgID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) hooks, err := ListWebhooksByOpts(&ListWebhookOptions{OrgID: 3, IsActive: util.OptionalBoolTrue}) assert.NoError(t, err) if assert.Len(t, hooks, 1) { @@ -149,7 +150,7 @@ func TestGetActiveWebhooksByOrgID(t *testing.T) { } func TestGetWebhooksByOrgID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) hooks, err := ListWebhooksByOpts(&ListWebhookOptions{OrgID: 3}) assert.NoError(t, err) if assert.Len(t, hooks, 1) { @@ -159,7 +160,7 @@ func TestGetWebhooksByOrgID(t *testing.T) { } func TestUpdateWebhook(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) hook := db.AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook) hook.IsActive = true hook.ContentType = ContentTypeForm @@ -169,7 +170,7 @@ func TestUpdateWebhook(t *testing.T) { } func TestDeleteWebhookByRepoID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) db.AssertExistsAndLoadBean(t, &Webhook{ID: 2, RepoID: 1}) assert.NoError(t, DeleteWebhookByRepoID(1, 2)) db.AssertNotExistsBean(t, &Webhook{ID: 2, RepoID: 1}) @@ -180,7 +181,7 @@ func TestDeleteWebhookByRepoID(t *testing.T) { } func TestDeleteWebhookByOrgID(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) db.AssertExistsAndLoadBean(t, &Webhook{ID: 3, OrgID: 3}) assert.NoError(t, DeleteWebhookByOrgID(3, 3)) db.AssertNotExistsBean(t, &Webhook{ID: 3, OrgID: 3}) @@ -191,7 +192,7 @@ func TestDeleteWebhookByOrgID(t *testing.T) { } func TestHookTasks(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) hookTasks, err := HookTasks(1, 1) assert.NoError(t, err) if assert.Len(t, hookTasks, 1) { @@ -204,7 +205,7 @@ func TestHookTasks(t *testing.T) { } func TestCreateHookTask(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) hookTask := &HookTask{ RepoID: 3, HookID: 3, @@ -216,7 +217,7 @@ func TestCreateHookTask(t *testing.T) { } func TestUpdateHookTask(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) hook := db.AssertExistsAndLoadBean(t, &HookTask{ID: 1}).(*HookTask) hook.PayloadContent = "new payload content" @@ -228,7 +229,7 @@ func TestUpdateHookTask(t *testing.T) { } func TestCleanupHookTaskTable_PerWebhook_DeletesDelivered(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) hookTask := &HookTask{ RepoID: 3, HookID: 3, @@ -245,7 +246,7 @@ func TestCleanupHookTaskTable_PerWebhook_DeletesDelivered(t *testing.T) { } func TestCleanupHookTaskTable_PerWebhook_LeavesUndelivered(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) hookTask := &HookTask{ RepoID: 2, HookID: 4, @@ -261,7 +262,7 @@ func TestCleanupHookTaskTable_PerWebhook_LeavesUndelivered(t *testing.T) { } func TestCleanupHookTaskTable_PerWebhook_LeavesMostRecentTask(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) hookTask := &HookTask{ RepoID: 2, HookID: 4, @@ -278,7 +279,7 @@ func TestCleanupHookTaskTable_PerWebhook_LeavesMostRecentTask(t *testing.T) { } func TestCleanupHookTaskTable_OlderThan_DeletesDelivered(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) hookTask := &HookTask{ RepoID: 3, HookID: 3, @@ -295,7 +296,7 @@ func TestCleanupHookTaskTable_OlderThan_DeletesDelivered(t *testing.T) { } func TestCleanupHookTaskTable_OlderThan_LeavesUndelivered(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) hookTask := &HookTask{ RepoID: 2, HookID: 4, @@ -311,7 +312,7 @@ func TestCleanupHookTaskTable_OlderThan_LeavesUndelivered(t *testing.T) { } func TestCleanupHookTaskTable_OlderThan_LeavesTaskEarlierThanAgeToDelete(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) hookTask := &HookTask{ RepoID: 2, HookID: 4, diff --git a/models/wiki_test.go b/models/wiki_test.go index f1435e8178..2114425872 100644 --- a/models/wiki_test.go +++ b/models/wiki_test.go @@ -9,13 +9,14 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" "github.com/stretchr/testify/assert" ) func TestRepository_WikiCloneLink(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) cloneLink := repo.WikiCloneLink() @@ -24,20 +25,20 @@ func TestRepository_WikiCloneLink(t *testing.T) { } func TestWikiPath(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) expected := filepath.Join(setting.RepoRootPath, "user2/repo1.wiki.git") assert.Equal(t, expected, WikiPath("user2", "repo1")) } func TestRepository_WikiPath(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.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) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) assert.True(t, repo1.HasWiki()) repo2 := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) diff --git a/modules/appstate/appstate_test.go b/modules/appstate/appstate_test.go index d8ab0a45fd..15fbc829ba 100644 --- a/modules/appstate/appstate_test.go +++ b/modules/appstate/appstate_test.go @@ -8,13 +8,13 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", ".."), "") + unittest.MainTest(m, filepath.Join("..", ".."), "") } type testItem1 struct { @@ -35,7 +35,7 @@ func (*testItem2) Name() string { } func TestAppStateDB(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) as := &DBStore{} diff --git a/modules/convert/git_commit_test.go b/modules/convert/git_commit_test.go index 298a006cb1..3cb55c6a6f 100644 --- a/modules/convert/git_commit_test.go +++ b/modules/convert/git_commit_test.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" @@ -18,7 +19,7 @@ import ( ) func TestToCommitMeta(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) headRepo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) sha1, _ := git.NewIDFromString("0000000000000000000000000000000000000000") signature := &git.Signature{Name: "Test Signature", Email: "test@email.com", When: time.Unix(0, 0)} diff --git a/modules/convert/issue_test.go b/modules/convert/issue_test.go index 7c6b05e816..e44733c46d 100644 --- a/modules/convert/issue_test.go +++ b/modules/convert/issue_test.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" @@ -19,7 +20,7 @@ import ( ) func TestLabel_ToLabel(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) label := db.AssertExistsAndLoadBean(t, &models.Label{ID: 1}).(*models.Label) repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: label.RepoID}).(*models.Repository) assert.Equal(t, &api.Label{ diff --git a/modules/convert/main_test.go b/modules/convert/main_test.go index acb9802f97..307fd06197 100644 --- a/modules/convert/main_test.go +++ b/modules/convert/main_test.go @@ -8,9 +8,9 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..")) + unittest.MainTest(m, filepath.Join("..", "..")) } diff --git a/modules/convert/pull_test.go b/modules/convert/pull_test.go index a2b1e12a37..f5310eb825 100644 --- a/modules/convert/pull_test.go +++ b/modules/convert/pull_test.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -16,7 +17,7 @@ import ( func TestPullRequest_APIFormat(t *testing.T) { //with HeadRepo - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) headRepo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) pr := db.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 1}).(*models.PullRequest) assert.NoError(t, pr.LoadAttributes()) diff --git a/modules/convert/user_test.go b/modules/convert/user_test.go index acf918dd99..fe26456f5d 100644 --- a/modules/convert/user_test.go +++ b/modules/convert/user_test.go @@ -9,13 +9,14 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" ) func TestUser_ToUser(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user1 := db.AssertExistsAndLoadBean(t, &models.User{ID: 1, IsAdmin: true}).(*models.User) diff --git a/modules/indexer/code/bleve_test.go b/modules/indexer/code/bleve_test.go index 3d97e312f9..37ed2eb9f0 100644 --- a/modules/indexer/code/bleve_test.go +++ b/modules/indexer/code/bleve_test.go @@ -8,14 +8,14 @@ import ( "os" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/util" "github.com/stretchr/testify/assert" ) func TestBleveIndexAndSearch(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) dir, err := os.MkdirTemp("", "bleve.index") assert.NoError(t, err) diff --git a/modules/indexer/code/elastic_search_test.go b/modules/indexer/code/elastic_search_test.go index c9d2c297bb..fc58633f16 100644 --- a/modules/indexer/code/elastic_search_test.go +++ b/modules/indexer/code/elastic_search_test.go @@ -8,12 +8,13 @@ import ( "os" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" + "github.com/stretchr/testify/assert" ) func TestESIndexAndSearch(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) u := os.Getenv("TEST_INDEXER_CODE_ES_URL") if u == "" { diff --git a/modules/indexer/code/indexer_test.go b/modules/indexer/code/indexer_test.go index 34930a84ca..b8fedcb323 100644 --- a/modules/indexer/code/indexer_test.go +++ b/modules/indexer/code/indexer_test.go @@ -8,12 +8,13 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" + "github.com/stretchr/testify/assert" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..", "..")) + unittest.MainTest(m, filepath.Join("..", "..", "..")) } func testIndexer(name string, t *testing.T, indexer Indexer) { diff --git a/modules/indexer/issues/indexer_test.go b/modules/indexer/issues/indexer_test.go index 561f357f4d..edfaddb648 100644 --- a/modules/indexer/issues/indexer_test.go +++ b/modules/indexer/issues/indexer_test.go @@ -11,7 +11,7 @@ import ( "testing" "time" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" @@ -21,11 +21,11 @@ import ( ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..", "..")) + unittest.MainTest(m, filepath.Join("..", "..", "..")) } func TestBleveSearchIssues(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) setting.Cfg = ini.Empty() tmpIndexerDir, err := os.MkdirTemp("", "issues-indexer") @@ -74,7 +74,7 @@ func TestBleveSearchIssues(t *testing.T) { } func TestDBSearchIssues(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) setting.Indexer.IssueType = "db" InitIssueIndexer(true) diff --git a/modules/indexer/stats/indexer_test.go b/modules/indexer/stats/indexer_test.go index a4645b2083..c451e80238 100644 --- a/modules/indexer/stats/indexer_test.go +++ b/modules/indexer/stats/indexer_test.go @@ -10,7 +10,7 @@ import ( "time" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" "gopkg.in/ini.v1" @@ -19,11 +19,11 @@ import ( ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..", "..")) + unittest.MainTest(m, filepath.Join("..", "..", "..")) } func TestRepoStatsIndex(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) setting.Cfg = ini.Empty() setting.NewQueueService() diff --git a/modules/migrations/gitea_uploader_test.go b/modules/migrations/gitea_uploader_test.go index b8b947961f..b5763f30d9 100644 --- a/modules/migrations/gitea_uploader_test.go +++ b/modules/migrations/gitea_uploader_test.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/migrations/base" "code.gitea.io/gitea/modules/structs" @@ -24,7 +25,7 @@ func TestGiteaUploadRepo(t *testing.T) { // FIXME: Since no accesskey or user/password will trigger rate limit of github, just skip t.Skip() - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) user := db.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) diff --git a/modules/migrations/main_test.go b/modules/migrations/main_test.go index 5b29230659..e9108c3c93 100644 --- a/modules/migrations/main_test.go +++ b/modules/migrations/main_test.go @@ -10,14 +10,14 @@ import ( "testing" "time" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/migrations/base" "github.com/stretchr/testify/assert" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..")) + unittest.MainTest(m, filepath.Join("..", "..")) } func timePtr(t time.Time) *time.Time { diff --git a/modules/migrations/migrate_test.go b/modules/migrations/migrate_test.go index c050a9abc0..aecc263cc6 100644 --- a/modules/migrations/migrate_test.go +++ b/modules/migrations/migrate_test.go @@ -10,13 +10,14 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" "github.com/stretchr/testify/assert" ) func TestMigrateWhiteBlocklist(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) adminUser := db.AssertExistsAndLoadBean(t, &models.User{Name: "user1"}).(*models.User) nonAdminUser := db.AssertExistsAndLoadBean(t, &models.User{Name: "user2"}).(*models.User) diff --git a/modules/notification/action/action_test.go b/modules/notification/action/action_test.go index 705a4fb2f4..e57069ed9a 100644 --- a/modules/notification/action/action_test.go +++ b/modules/notification/action/action_test.go @@ -11,15 +11,16 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..", "..")) + unittest.MainTest(m, filepath.Join("..", "..", "..")) } func TestRenameRepoAction(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) repo := db.AssertExistsAndLoadBean(t, &models.Repository{OwnerID: user.ID}).(*models.Repository) diff --git a/modules/repofiles/action_test.go b/modules/repofiles/action_test.go index c29cfd686d..59cb4df160 100644 --- a/modules/repofiles/action_test.go +++ b/modules/repofiles/action_test.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" @@ -16,7 +17,7 @@ import ( ) func TestUpdateIssuesCommit(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) pushCommits := []*repository.PushCommit{ { Sha1: "abcdef1", @@ -118,7 +119,7 @@ func TestUpdateIssuesCommit(t *testing.T) { } func TestUpdateIssuesCommit_Colon(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) pushCommits := []*repository.PushCommit{ { Sha1: "abcdef2", @@ -143,7 +144,7 @@ func TestUpdateIssuesCommit_Colon(t *testing.T) { } func TestUpdateIssuesCommit_Issue5957(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // Test that push to a non-default branch closes an issue. @@ -177,7 +178,7 @@ func TestUpdateIssuesCommit_Issue5957(t *testing.T) { } func TestUpdateIssuesCommit_AnotherRepo(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // Test that a push to default branch closes issue in another repo @@ -212,7 +213,7 @@ func TestUpdateIssuesCommit_AnotherRepo(t *testing.T) { } func TestUpdateIssuesCommit_AnotherRepo_FullAddress(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // Test that a push to default branch closes issue in another repo @@ -247,7 +248,7 @@ func TestUpdateIssuesCommit_AnotherRepo_FullAddress(t *testing.T) { } func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &models.User{ID: 10}).(*models.User) // Test that a push with close reference *can not* close issue diff --git a/modules/repofiles/blob_test.go b/modules/repofiles/blob_test.go index 5238dd6e74..c219892aec 100644 --- a/modules/repofiles/blob_test.go +++ b/modules/repofiles/blob_test.go @@ -7,7 +7,8 @@ package repofiles import ( "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" + api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/test" @@ -15,7 +16,7 @@ import ( ) func TestGetBlobBySHA(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") test.LoadRepo(t, ctx, 1) test.LoadRepoCommit(t, ctx) diff --git a/modules/repofiles/content_test.go b/modules/repofiles/content_test.go index f68460d7ec..e3230698bc 100644 --- a/modules/repofiles/content_test.go +++ b/modules/repofiles/content_test.go @@ -8,7 +8,8 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" + api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/test" @@ -16,7 +17,7 @@ import ( ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..")) + unittest.MainTest(m, filepath.Join("..", "..")) } func getExpectedReadmeContentsResponse() *api.ContentsResponse { @@ -49,7 +50,7 @@ func getExpectedReadmeContentsResponse() *api.ContentsResponse { } func TestGetContents(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) @@ -77,7 +78,7 @@ func TestGetContents(t *testing.T) { } func TestGetContentsOrListForDir(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) @@ -112,7 +113,7 @@ func TestGetContentsOrListForDir(t *testing.T) { } func TestGetContentsOrListForFile(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) @@ -140,7 +141,7 @@ func TestGetContentsOrListForFile(t *testing.T) { } func TestGetContentsErrors(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) @@ -171,7 +172,7 @@ func TestGetContentsErrors(t *testing.T) { } func TestGetContentsOrListErrors(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) @@ -202,7 +203,7 @@ func TestGetContentsOrListErrors(t *testing.T) { } func TestGetContentsOrListOfEmptyRepos(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo15") ctx.SetParams(":id", "15") test.LoadRepo(t, ctx, 15) diff --git a/modules/repofiles/diff_test.go b/modules/repofiles/diff_test.go index aeb4f76a45..463ce4ec67 100644 --- a/modules/repofiles/diff_test.go +++ b/modules/repofiles/diff_test.go @@ -8,7 +8,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/services/gitdiff" @@ -16,7 +16,7 @@ import ( ) func TestGetDiffPreview(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) @@ -129,7 +129,7 @@ func TestGetDiffPreview(t *testing.T) { } func TestGetDiffPreviewErrors(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) diff --git a/modules/repofiles/file_test.go b/modules/repofiles/file_test.go index 2974056549..54205a89c7 100644 --- a/modules/repofiles/file_test.go +++ b/modules/repofiles/file_test.go @@ -7,7 +7,7 @@ package repofiles import ( "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -81,7 +81,7 @@ func getExpectedFileResponse() *api.FileResponse { } func TestGetFileResponseFromCommit(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) diff --git a/modules/repofiles/tree_test.go b/modules/repofiles/tree_test.go index 9e2efa4216..512313c43b 100644 --- a/modules/repofiles/tree_test.go +++ b/modules/repofiles/tree_test.go @@ -7,7 +7,8 @@ package repofiles import ( "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" + api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/test" @@ -15,7 +16,7 @@ import ( ) func TestGetTreeBySHA(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") test.LoadRepo(t, ctx, 1) test.LoadRepoCommit(t, ctx) diff --git a/modules/repository/commits_test.go b/modules/repository/commits_test.go index c62ad58449..c47d9cf48c 100644 --- a/modules/repository/commits_test.go +++ b/modules/repository/commits_test.go @@ -12,12 +12,13 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" "github.com/stretchr/testify/assert" ) func TestPushCommits_ToAPIPayloadCommits(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) pushCommits := NewPushCommits() pushCommits.Commits = []*PushCommit{ @@ -100,7 +101,7 @@ func TestPushCommits_ToAPIPayloadCommits(t *testing.T) { } func TestPushCommits_AvatarLink(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) pushCommits := NewPushCommits() pushCommits.Commits = []*PushCommit{ diff --git a/modules/repository/create_test.go b/modules/repository/create_test.go index 040c061d92..d833017160 100644 --- a/modules/repository/create_test.go +++ b/modules/repository/create_test.go @@ -10,13 +10,14 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" ) func TestIncludesAllRepositoriesTeams(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) testTeamRepositories := func(teamID int64, repoIds []int64) { team := db.AssertExistsAndLoadBean(t, &models.Team{ID: teamID}).(*models.Team) diff --git a/modules/repository/fork_test.go b/modules/repository/fork_test.go index 7a05f9dd97..bfb813adbc 100644 --- a/modules/repository/fork_test.go +++ b/modules/repository/fork_test.go @@ -9,11 +9,12 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestForkRepository(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // user 13 has already forked repo10 user := db.AssertExistsAndLoadBean(t, &models.User{ID: 13}).(*models.User) diff --git a/modules/repository/main_test.go b/modules/repository/main_test.go index 91d0d36ca0..262d339481 100644 --- a/modules/repository/main_test.go +++ b/modules/repository/main_test.go @@ -8,9 +8,9 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..")) + unittest.MainTest(m, filepath.Join("..", "..")) } diff --git a/modules/unittestbridge/unittestbridge.go b/modules/unittestbridge/unittestbridge.go new file mode 100644 index 0000000000..273cf5e70f --- /dev/null +++ b/modules/unittestbridge/unittestbridge.go @@ -0,0 +1,46 @@ +// 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 unittestbridge + +// Usage: generally, non-unit-test code shouldn't depend on unit test code. +// However, we have some code like models.CheckConsistencyFor, which need to do some unit test works. +// Now we can not decouple models.CheckConsistencyFor from unit test code easily (cycle-import reasons). +// So we introduce this `unit test bridge`: +// * When a release binary is built, no testing/assert framework would be compiled into the binary, and CheckConsistencyFor won't run unit test related code +// * When a unit test binary is built, the unit test code will init this bridge, then CheckConsistencyFor can run unit test related code +// +// Tester/Assert are intermediate interfaces, they should NOT be used in new code. +// One day, if CheckConsistencyFor is clean enough, we can remove these intermediate interfaces. + +// Tester is the same as TestingT in "stretchr/testify/assert" +// Tester can be used in non-unit-test code (ex: models.CheckConsistencyFor), it is used to isolate dependencies +type Tester interface { + Errorf(format string, args ...interface{}) +} + +// Asserter can be used in non-unit-test code (ex: models.CheckConsistencyFor), it is used to isolate dependencies +type Asserter interface { + Tester + NoError(err error, msgAndArgs ...interface{}) bool + EqualValues(expected, actual interface{}, msgAndArgs ...interface{}) bool + Equal(expected, actual interface{}, msgAndArgs ...interface{}) bool + True(value bool, msgAndArgs ...interface{}) bool + False(value bool, msgAndArgs ...interface{}) bool +} + +var newAsserterFunc func(t Tester) Asserter + +// NewAsserter returns a new asserter, only works in unit test +func NewAsserter(t Tester) Asserter { + if newAsserterFunc == nil { + panic("the newAsserterFunc is not set. you can only use assert in unit test.") + } + return newAsserterFunc(t) +} + +// SetNewAsserterFunc in unit test, the asserter must be set first +func SetNewAsserterFunc(f func(t Tester) Asserter) { + newAsserterFunc = f +} diff --git a/routers/api/v1/repo/hook_test.go b/routers/api/v1/repo/hook_test.go index 60fa6cead3..ff0aaf2938 100644 --- a/routers/api/v1/repo/hook_test.go +++ b/routers/api/v1/repo/hook_test.go @@ -9,6 +9,7 @@ import ( "testing" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/test" @@ -17,7 +18,7 @@ import ( ) func TestTestHook(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/wiki/_pages") ctx.SetParams(":id", "1") diff --git a/routers/api/v1/repo/main_test.go b/routers/api/v1/repo/main_test.go index 7a66370e05..f9ed886999 100644 --- a/routers/api/v1/repo/main_test.go +++ b/routers/api/v1/repo/main_test.go @@ -8,9 +8,9 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..", "..", "..")) + unittest.MainTest(m, filepath.Join("..", "..", "..", "..")) } diff --git a/routers/api/v1/repo/repo_test.go b/routers/api/v1/repo/repo_test.go index e4fd4f9424..b992b93fd4 100644 --- a/routers/api/v1/repo/repo_test.go +++ b/routers/api/v1/repo/repo_test.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/context" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/test" @@ -19,7 +20,7 @@ import ( ) func TestRepoEdit(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") test.LoadRepo(t, ctx, 1) @@ -66,7 +67,7 @@ func TestRepoEdit(t *testing.T) { } func TestRepoEditNameChange(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") test.LoadRepo(t, ctx, 1) diff --git a/routers/web/admin/main_test.go b/routers/web/admin/main_test.go index cfdefdb1b4..e41d8fea75 100644 --- a/routers/web/admin/main_test.go +++ b/routers/web/admin/main_test.go @@ -8,9 +8,9 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..", "..")) + unittest.MainTest(m, filepath.Join("..", "..", "..")) } diff --git a/routers/web/admin/users_test.go b/routers/web/admin/users_test.go index 022d8f662c..62b35ea286 100644 --- a/routers/web/admin/users_test.go +++ b/routers/web/admin/users_test.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/test" @@ -20,7 +21,7 @@ import ( func TestNewUserPost_MustChangePassword(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "admin/users/new") u := db.AssertExistsAndLoadBean(t, &models.User{ @@ -57,7 +58,7 @@ func TestNewUserPost_MustChangePassword(t *testing.T) { } func TestNewUserPost_MustChangePasswordFalse(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "admin/users/new") u := db.AssertExistsAndLoadBean(t, &models.User{ @@ -94,7 +95,7 @@ func TestNewUserPost_MustChangePasswordFalse(t *testing.T) { } func TestNewUserPost_InvalidEmail(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "admin/users/new") u := db.AssertExistsAndLoadBean(t, &models.User{ @@ -124,7 +125,7 @@ func TestNewUserPost_InvalidEmail(t *testing.T) { } func TestNewUserPost_VisibilityDefaultPublic(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "admin/users/new") u := db.AssertExistsAndLoadBean(t, &models.User{ @@ -162,7 +163,7 @@ func TestNewUserPost_VisibilityDefaultPublic(t *testing.T) { } func TestNewUserPost_VisibilityPrivate(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "admin/users/new") u := db.AssertExistsAndLoadBean(t, &models.User{ diff --git a/routers/web/repo/editor_test.go b/routers/web/repo/editor_test.go index 8ab1fe1ee8..77f94ff550 100644 --- a/routers/web/repo/editor_test.go +++ b/routers/web/repo/editor_test.go @@ -7,7 +7,7 @@ package repo import ( "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/test" @@ -15,7 +15,7 @@ import ( ) func TestCleanUploadName(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) var kases = map[string]string{ ".git/refs/master": "", @@ -41,7 +41,7 @@ func TestCleanUploadName(t *testing.T) { } func TestGetUniquePatchBranchName(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) @@ -56,7 +56,7 @@ func TestGetUniquePatchBranchName(t *testing.T) { } func TestGetClosestParentWithFiles(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) diff --git a/routers/web/repo/issue_label_test.go b/routers/web/repo/issue_label_test.go index 8c3caabe17..98fa9eb968 100644 --- a/routers/web/repo/issue_label_test.go +++ b/routers/web/repo/issue_label_test.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/services/forms" @@ -30,7 +31,7 @@ func int64SliceToCommaSeparated(a []int64) string { } func TestInitializeLabels(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/labels/initialize") test.LoadUser(t, ctx, 2) test.LoadRepo(t, ctx, 2) @@ -46,7 +47,7 @@ func TestInitializeLabels(t *testing.T) { } func TestRetrieveLabels(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) for _, testCase := range []struct { RepoID int64 Sort string @@ -73,7 +74,7 @@ func TestRetrieveLabels(t *testing.T) { } func TestNewLabel(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/labels/edit") test.LoadUser(t, ctx, 2) test.LoadRepo(t, ctx, 1) @@ -91,7 +92,7 @@ func TestNewLabel(t *testing.T) { } func TestUpdateLabel(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/labels/edit") test.LoadUser(t, ctx, 2) test.LoadRepo(t, ctx, 1) @@ -111,7 +112,7 @@ func TestUpdateLabel(t *testing.T) { } func TestDeleteLabel(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/labels/delete") test.LoadUser(t, ctx, 2) test.LoadRepo(t, ctx, 1) @@ -124,7 +125,7 @@ func TestDeleteLabel(t *testing.T) { } func TestUpdateIssueLabel_Clear(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/issues/labels") test.LoadUser(t, ctx, 2) test.LoadRepo(t, ctx, 1) @@ -149,7 +150,7 @@ func TestUpdateIssueLabel_Toggle(t *testing.T) { {"toggle", []int64{1, 3}, 1, false}, {"toggle", []int64{1, 2}, 2, true}, } { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/issues/labels") test.LoadUser(t, ctx, 2) test.LoadRepo(t, ctx, 1) diff --git a/routers/web/repo/main_test.go b/routers/web/repo/main_test.go index 8322567249..81e3a8e281 100644 --- a/routers/web/repo/main_test.go +++ b/routers/web/repo/main_test.go @@ -8,9 +8,9 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..", "..")) + unittest.MainTest(m, filepath.Join("..", "..", "..")) } diff --git a/routers/web/repo/projects_test.go b/routers/web/repo/projects_test.go index d3b78cc775..62fb045740 100644 --- a/routers/web/repo/projects_test.go +++ b/routers/web/repo/projects_test.go @@ -7,14 +7,14 @@ package repo import ( "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/test" "github.com/stretchr/testify/assert" ) func TestCheckProjectBoardChangePermissions(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/projects/1/2") test.LoadUser(t, ctx, 2) test.LoadRepo(t, ctx, 1) diff --git a/routers/web/repo/release_test.go b/routers/web/repo/release_test.go index 7ac49c012f..ff54232da7 100644 --- a/routers/web/repo/release_test.go +++ b/routers/web/repo/release_test.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/services/forms" @@ -44,7 +45,7 @@ func TestNewReleasePost(t *testing.T) { }, }, } { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/releases/new") test.LoadUser(t, ctx, 2) diff --git a/routers/web/repo/settings_test.go b/routers/web/repo/settings_test.go index a3ed271cce..78426f5b79 100644 --- a/routers/web/repo/settings_test.go +++ b/routers/web/repo/settings_test.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/test" @@ -43,7 +44,7 @@ func TestAddReadOnlyDeployKey(t *testing.T) { } else { return } - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/settings/keys") @@ -72,7 +73,7 @@ func TestAddReadWriteOnlyDeployKey(t *testing.T) { return } - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/settings/keys") @@ -97,7 +98,7 @@ func TestAddReadWriteOnlyDeployKey(t *testing.T) { func TestCollaborationPost(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/issues/labels") test.LoadUser(t, ctx, 2) test.LoadUser(t, ctx, 4) @@ -133,7 +134,7 @@ func TestCollaborationPost(t *testing.T) { func TestCollaborationPost_InactiveUser(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/issues/labels") test.LoadUser(t, ctx, 2) test.LoadUser(t, ctx, 9) @@ -157,7 +158,7 @@ func TestCollaborationPost_InactiveUser(t *testing.T) { func TestCollaborationPost_AddCollaboratorTwice(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/issues/labels") test.LoadUser(t, ctx, 2) test.LoadUser(t, ctx, 4) @@ -199,7 +200,7 @@ func TestCollaborationPost_AddCollaboratorTwice(t *testing.T) { func TestCollaborationPost_NonExistentUser(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/issues/labels") test.LoadUser(t, ctx, 2) test.LoadRepo(t, ctx, 1) @@ -221,7 +222,7 @@ func TestCollaborationPost_NonExistentUser(t *testing.T) { } func TestAddTeamPost(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "org26/repo43") ctx.Req.Form.Set("team", "team11") @@ -261,7 +262,7 @@ func TestAddTeamPost(t *testing.T) { } func TestAddTeamPost_NotAllowed(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "org26/repo43") ctx.Req.Form.Set("team", "team11") @@ -302,7 +303,7 @@ func TestAddTeamPost_NotAllowed(t *testing.T) { } func TestAddTeamPost_AddTeamTwice(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "org26/repo43") ctx.Req.Form.Set("team", "team11") @@ -343,7 +344,7 @@ func TestAddTeamPost_AddTeamTwice(t *testing.T) { } func TestAddTeamPost_NonExistentTeam(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "org26/repo43") ctx.Req.Form.Set("team", "team-non-existent") @@ -376,7 +377,7 @@ func TestAddTeamPost_NonExistentTeam(t *testing.T) { } func TestDeleteTeam(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "org3/team1/repo3") ctx.Req.Form.Set("id", "2") diff --git a/routers/web/repo/wiki_test.go b/routers/web/repo/wiki_test.go index 14cb893d46..cf49f19afe 100644 --- a/routers/web/repo/wiki_test.go +++ b/routers/web/repo/wiki_test.go @@ -10,7 +10,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/modules/web" @@ -74,7 +74,7 @@ func assertPagesMetas(t *testing.T, expectedNames []string, metas interface{}) { } func TestWiki(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/wiki/_pages") ctx.SetParams(":page", "Home") @@ -86,7 +86,7 @@ func TestWiki(t *testing.T) { } func TestWikiPages(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/wiki/_pages") test.LoadRepo(t, ctx, 1) @@ -96,7 +96,7 @@ func TestWikiPages(t *testing.T) { } func TestNewWiki(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/wiki/_new") test.LoadUser(t, ctx, 2) @@ -111,7 +111,7 @@ func TestNewWikiPost(t *testing.T) { "New page", "&&&&", } { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/wiki/_new") test.LoadUser(t, ctx, 2) @@ -129,7 +129,7 @@ func TestNewWikiPost(t *testing.T) { } func TestNewWikiPost_ReservedName(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/wiki/_new") test.LoadUser(t, ctx, 2) @@ -146,7 +146,7 @@ func TestNewWikiPost_ReservedName(t *testing.T) { } func TestEditWiki(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/wiki/_edit/Home") ctx.SetParams(":page", "Home") @@ -163,7 +163,7 @@ func TestEditWikiPost(t *testing.T) { "Home", "New/<page>", } { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/wiki/_new/Home") ctx.SetParams(":page", "Home") test.LoadUser(t, ctx, 2) @@ -184,7 +184,7 @@ func TestEditWikiPost(t *testing.T) { } func TestDeleteWikiPagePost(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/wiki/Home/delete") test.LoadUser(t, ctx, 2) @@ -203,7 +203,7 @@ func TestWikiRaw(t *testing.T) { "Page With Spaced Name.md": "text/plain; charset=utf-8", "Page-With-Spaced-Name.md": "text/plain; charset=utf-8", } { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1/wiki/raw/"+filepath) ctx.SetParams("*", filepath) diff --git a/routers/web/user/home_test.go b/routers/web/user/home_test.go index daf473b270..cd599abd04 100644 --- a/routers/web/user/home_test.go +++ b/routers/web/user/home_test.go @@ -9,7 +9,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/test" @@ -19,7 +19,7 @@ import ( func TestArchivedIssues(t *testing.T) { // Arrange setting.UI.IssuePagingNum = 1 - assert.NoError(t, db.LoadFixtures()) + assert.NoError(t, unittest.LoadFixtures()) ctx := test.MockContext(t, "issues") test.LoadUser(t, ctx, 30) @@ -52,7 +52,7 @@ func TestArchivedIssues(t *testing.T) { func TestIssues(t *testing.T) { setting.UI.IssuePagingNum = 1 - assert.NoError(t, db.LoadFixtures()) + assert.NoError(t, unittest.LoadFixtures()) ctx := test.MockContext(t, "issues") test.LoadUser(t, ctx, 2) @@ -68,7 +68,7 @@ func TestIssues(t *testing.T) { func TestPulls(t *testing.T) { setting.UI.IssuePagingNum = 20 - assert.NoError(t, db.LoadFixtures()) + assert.NoError(t, unittest.LoadFixtures()) ctx := test.MockContext(t, "pulls") test.LoadUser(t, ctx, 2) @@ -81,7 +81,7 @@ func TestPulls(t *testing.T) { func TestMilestones(t *testing.T) { setting.UI.IssuePagingNum = 1 - assert.NoError(t, db.LoadFixtures()) + assert.NoError(t, unittest.LoadFixtures()) ctx := test.MockContext(t, "milestones") test.LoadUser(t, ctx, 2) @@ -100,7 +100,7 @@ func TestMilestones(t *testing.T) { func TestMilestonesForSpecificRepo(t *testing.T) { setting.UI.IssuePagingNum = 1 - assert.NoError(t, db.LoadFixtures()) + assert.NoError(t, unittest.LoadFixtures()) ctx := test.MockContext(t, "milestones") test.LoadUser(t, ctx, 2) diff --git a/routers/web/user/main_test.go b/routers/web/user/main_test.go index 272e4b8b21..77b48d89fb 100644 --- a/routers/web/user/main_test.go +++ b/routers/web/user/main_test.go @@ -8,9 +8,9 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..", "..")) + unittest.MainTest(m, filepath.Join("..", "..", "..")) } diff --git a/routers/web/user/oauth_test.go b/routers/web/user/oauth_test.go index 27d339b778..e0c1bf4e77 100644 --- a/routers/web/user/oauth_test.go +++ b/routers/web/user/oauth_test.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/login" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/services/auth/source/oauth2" "github.com/golang-jwt/jwt" @@ -41,7 +42,7 @@ func createAndParseToken(t *testing.T, grant *login.OAuth2Grant) *oauth2.OIDCTok } func TestNewAccessTokenResponse_OIDCToken(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) grants, err := login.GetOAuth2GrantsByUserID(3) assert.NoError(t, err) diff --git a/routers/web/user/setting/account_test.go b/routers/web/user/setting/account_test.go index bfb7ac4872..cd5c77795e 100644 --- a/routers/web/user/setting/account_test.go +++ b/routers/web/user/setting/account_test.go @@ -8,7 +8,7 @@ import ( "net/http" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/modules/web" @@ -81,7 +81,7 @@ func TestChangePassword(t *testing.T) { PasswordComplexity: pcLU, }, } { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "user/settings/security") test.LoadUser(t, ctx, 2) test.LoadRepo(t, ctx, 1) diff --git a/routers/web/user/setting/main_test.go b/routers/web/user/setting/main_test.go index a0fbe55ee1..b6ed7f5b18 100644 --- a/routers/web/user/setting/main_test.go +++ b/routers/web/user/setting/main_test.go @@ -8,9 +8,9 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..", "..", "..")) + unittest.MainTest(m, filepath.Join("..", "..", "..", "..")) } diff --git a/services/archiver/archiver_test.go b/services/archiver/archiver_test.go index 94b0423d9b..67484fdc78 100644 --- a/services/archiver/archiver_test.go +++ b/services/archiver/archiver_test.go @@ -9,14 +9,14 @@ import ( "testing" "time" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/test" "github.com/stretchr/testify/assert" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..")) + unittest.MainTest(m, filepath.Join("..", "..")) } func waitForCount(t *testing.T, num int) { @@ -24,7 +24,7 @@ func waitForCount(t *testing.T, num int) { } func TestArchive_Basic(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) ctx := test.MockContext(t, "user27/repo49") firstCommit, secondCommit := "51f84af23134", "aacbdfe9e1c4" diff --git a/services/attachment/attachment_test.go b/services/attachment/attachment_test.go index 3e9e55a8f2..f44c4f9b5e 100644 --- a/services/attachment/attachment_test.go +++ b/services/attachment/attachment_test.go @@ -11,16 +11,17 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..")) + unittest.MainTest(m, filepath.Join("..", "..")) } func TestUploadAttachment(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) diff --git a/services/gitdiff/gitdiff_test.go b/services/gitdiff/gitdiff_test.go index 6decb59b64..359e2e8c66 100644 --- a/services/gitdiff/gitdiff_test.go +++ b/services/gitdiff/gitdiff_test.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/highlight" "code.gitea.io/gitea/modules/json" @@ -493,7 +494,7 @@ func setupDefaultDiff() *Diff { } } func TestDiff_LoadComments(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) issue := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue) user := db.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) diff --git a/services/gitdiff/main_test.go b/services/gitdiff/main_test.go index 1b83cbd684..8c76e7e153 100644 --- a/services/gitdiff/main_test.go +++ b/services/gitdiff/main_test.go @@ -8,9 +8,9 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..")) + unittest.MainTest(m, filepath.Join("..", "..")) } diff --git a/services/issue/assignee_test.go b/services/issue/assignee_test.go index 5684ed6d89..b0bbe42273 100644 --- a/services/issue/assignee_test.go +++ b/services/issue/assignee_test.go @@ -8,12 +8,12 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestDeleteNotPassedAssignee(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) // Fake issue with assignees issue, err := models.GetIssueWithAttrsByID(1) diff --git a/services/issue/label_test.go b/services/issue/label_test.go index 8a3a77ecb0..758ef98b27 100644 --- a/services/issue/label_test.go +++ b/services/issue/label_test.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -24,7 +25,7 @@ func TestIssue_AddLabels(t *testing.T) { {2, []int64{}, 1}, // pull-request, empty } for _, test := range tests { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) issue := db.AssertExistsAndLoadBean(t, &models.Issue{ID: test.issueID}).(*models.Issue) labels := make([]*models.Label, len(test.labelIDs)) for i, labelID := range test.labelIDs { @@ -50,7 +51,7 @@ func TestIssue_AddLabel(t *testing.T) { {2, 1, 2}, // pull-request, already-added label } for _, test := range tests { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) issue := db.AssertExistsAndLoadBean(t, &models.Issue{ID: test.issueID}).(*models.Issue) label := db.AssertExistsAndLoadBean(t, &models.Label{ID: test.labelID}).(*models.Label) doer := db.AssertExistsAndLoadBean(t, &models.User{ID: test.doerID}).(*models.User) diff --git a/services/issue/main_test.go b/services/issue/main_test.go index 1349837949..cbcfd08a49 100644 --- a/services/issue/main_test.go +++ b/services/issue/main_test.go @@ -8,9 +8,9 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..")) + unittest.MainTest(m, filepath.Join("..", "..")) } diff --git a/services/mailer/mail_test.go b/services/mailer/mail_test.go index cd730a13a4..aaa18681b8 100644 --- a/services/mailer/mail_test.go +++ b/services/mailer/mail_test.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" "github.com/stretchr/testify/assert" @@ -41,7 +42,7 @@ const bodyTpl = ` ` func prepareMailerTest(t *testing.T) (doer *models.User, repo *models.Repository, issue *models.Issue, comment *models.Comment) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) var mailService = setting.Mailer{ From: "test@gitea.com", } diff --git a/services/mailer/main_test.go b/services/mailer/main_test.go index 2fbe9c54a9..ae3b2c12b4 100644 --- a/services/mailer/main_test.go +++ b/services/mailer/main_test.go @@ -8,9 +8,9 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..")) + unittest.MainTest(m, filepath.Join("..", "..")) } diff --git a/services/pull/check_test.go b/services/pull/check_test.go index 8beea3d56d..bc3df0a4bb 100644 --- a/services/pull/check_test.go +++ b/services/pull/check_test.go @@ -12,13 +12,14 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/queue" "github.com/stretchr/testify/assert" ) func TestPullRequest_AddToTaskQueue(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) idChan := make(chan int64, 10) diff --git a/services/pull/main_test.go b/services/pull/main_test.go index c8d3394e8e..6059a291ad 100644 --- a/services/pull/main_test.go +++ b/services/pull/main_test.go @@ -9,9 +9,9 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..")) + unittest.MainTest(m, filepath.Join("..", "..")) } diff --git a/services/release/release_test.go b/services/release/release_test.go index e53e4c935b..b855ec6647 100644 --- a/services/release/release_test.go +++ b/services/release/release_test.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/services/attachment" @@ -19,11 +20,11 @@ import ( ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..")) + unittest.MainTest(m, filepath.Join("..", "..")) } func TestRelease_Create(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) @@ -127,7 +128,7 @@ func TestRelease_Create(t *testing.T) { } func TestRelease_Update(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) @@ -269,7 +270,7 @@ func TestRelease_Update(t *testing.T) { } func TestRelease_createTag(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) @@ -352,7 +353,7 @@ func TestRelease_createTag(t *testing.T) { } func TestCreateNewTag(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) diff --git a/services/repository/main_test.go b/services/repository/main_test.go index 91d0d36ca0..262d339481 100644 --- a/services/repository/main_test.go +++ b/services/repository/main_test.go @@ -8,9 +8,9 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..")) + unittest.MainTest(m, filepath.Join("..", "..")) } diff --git a/services/repository/transfer_test.go b/services/repository/transfer_test.go index 40ccfdfb52..9ce60f30ee 100644 --- a/services/repository/transfer_test.go +++ b/services/repository/transfer_test.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/notification/action" "code.gitea.io/gitea/modules/util" @@ -28,7 +29,7 @@ func registerNotifier() { func TestTransferOwnership(t *testing.T) { registerNotifier() - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) doer := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) @@ -55,7 +56,7 @@ func TestTransferOwnership(t *testing.T) { } func TestStartRepositoryTransferSetPermission(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) doer := db.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) recipient := db.AssertExistsAndLoadBean(t, &models.User{ID: 5}).(*models.User) diff --git a/services/webhook/main_test.go b/services/webhook/main_test.go index 7aef4b3a51..fc58f72565 100644 --- a/services/webhook/main_test.go +++ b/services/webhook/main_test.go @@ -8,9 +8,9 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..")) + unittest.MainTest(m, filepath.Join("..", "..")) } diff --git a/services/webhook/webhook_test.go b/services/webhook/webhook_test.go index 75f19e50ce..276d1e78cd 100644 --- a/services/webhook/webhook_test.go +++ b/services/webhook/webhook_test.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" webhook_model "code.gitea.io/gitea/models/webhook" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -27,7 +28,7 @@ func TestWebhook_GetSlackHook(t *testing.T) { } func TestPrepareWebhooks(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) hookTasks := []*webhook_model.HookTask{ @@ -43,7 +44,7 @@ func TestPrepareWebhooks(t *testing.T) { } func TestPrepareWebhooksBranchFilterMatch(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) hookTasks := []*webhook_model.HookTask{ @@ -60,7 +61,7 @@ func TestPrepareWebhooksBranchFilterMatch(t *testing.T) { } func TestPrepareWebhooksBranchFilterNoMatch(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) hookTasks := []*webhook_model.HookTask{ diff --git a/services/wiki/wiki_test.go b/services/wiki/wiki_test.go index d6a65cc23a..7ecd3bb04e 100644 --- a/services/wiki/wiki_test.go +++ b/services/wiki/wiki_test.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/util" @@ -18,7 +19,7 @@ import ( ) func TestMain(m *testing.M) { - db.MainTest(m, filepath.Join("..", "..")) + unittest.MainTest(m, filepath.Join("..", "..")) } func TestWikiNameToSubURL(t *testing.T) { @@ -110,7 +111,7 @@ func TestWikiNameToFilenameToName(t *testing.T) { } func TestRepository_InitWiki(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) // repo1 already has a wiki repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) assert.NoError(t, InitWiki(repo1)) @@ -122,7 +123,7 @@ func TestRepository_InitWiki(t *testing.T) { } func TestRepository_AddWikiPage(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) const wikiContent = "This is the wiki content" const commitMsg = "Commit message" repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) @@ -166,7 +167,7 @@ func TestRepository_AddWikiPage(t *testing.T) { } func TestRepository_EditWikiPage(t *testing.T) { - assert.NoError(t, db.PrepareTestDatabase()) + assert.NoError(t, unittest.PrepareTestDatabase()) const newWikiContent = "This is the new content" const commitMsg = "Commit message" @@ -177,7 +178,7 @@ func TestRepository_EditWikiPage(t *testing.T) { "New home", "New/name/with/slashes", } { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) assert.NoError(t, EditWikiPage(doer, repo, "Home", newWikiName, newWikiContent, commitMsg)) // Now need to show that the page has been added: @@ -199,7 +200,7 @@ func TestRepository_EditWikiPage(t *testing.T) { } func TestRepository_DeleteWikiPage(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) doer := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) assert.NoError(t, DeleteWikiPage(doer, repo, "Home")) @@ -216,7 +217,7 @@ func TestRepository_DeleteWikiPage(t *testing.T) { } func TestPrepareWikiFileName(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) gitRepo, err := git.OpenRepository(repo.WikiPath()) defer gitRepo.Close() @@ -267,7 +268,7 @@ func TestPrepareWikiFileName(t *testing.T) { } func TestPrepareWikiFileName_FirstPage(t *testing.T) { - db.PrepareTestEnv(t) + unittest.PrepareTestEnv(t) // Now create a temporaryDirectory tmpDir, err := os.MkdirTemp("", "empty-wiki") |