Decouple unit test code from business code (#17623)

This commit is contained in:
wxiaoguang 2021-11-12 22:36:47 +08:00 committed by GitHub
parent 7f802631c5
commit df64fa4865
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
136 changed files with 1058 additions and 830 deletions

View file

@ -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)
}

View file

@ -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)

View file

@ -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)

View file

@ -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))
}

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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})

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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)

View file

@ -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)
}

View file

@ -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("..", "..", ".."))
}

View file

@ -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

View file

@ -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,

View file

@ -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)
}

View file

@ -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)

View file

@ -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
}

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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),

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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.

View file

@ -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)

View file

@ -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}

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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("..", ".."), "")
}

View file

@ -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",

View file

@ -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"})

View file

@ -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)

View file

@ -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))

View file

@ -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, "..")
}

View file

@ -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
}

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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,

View file

@ -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)

View file

@ -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,

View file

@ -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())

View file

@ -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

View file

@ -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)

View file

@ -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()

View file

@ -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"))

View file

@ -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()

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

54
models/unittest/bridge.go Normal file
View file

@ -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}
})
}

View file

@ -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
func getXORMEngine(engine ...*xorm.Engine) (x *xorm.Engine) {
if len(engine) == 1 {
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 := x
if len(engine) == 1 {
e = engine[0]
}
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.

154
models/unittest/testdb.go Normal file
View file

@ -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
}

View file

@ -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)

View file

@ -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",
)

View file

@ -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)

View file

@ -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{

View file

@ -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))

View file

@ -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))

View file

@ -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

View file

@ -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"))

View file

@ -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

View file

@ -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")
}

View file

@ -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,

View file

@ -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)

View file

@ -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{}

View file

@ -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)}

View file

@ -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{

View file

@ -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("..", ".."))
}

View file

@ -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())

View file

@ -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)

View file

@ -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)

View file

@ -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 == "" {

View file

@ -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) {

View file

@ -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)

View file

@ -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()

View file

@ -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)

View file

@ -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 {

View file

@ -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)

View file

@ -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)

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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{

View file

@ -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)

View file

@ -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)

Some files were not shown because too many files have changed in this diff Show more