aboutsummaryrefslogtreecommitdiffstats
path: root/models/issue_milestone_test.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-09-19 19:49:59 +0800
committerGitHub <noreply@github.com>2021-09-19 19:49:59 +0800
commita4bfef265d9e512830350635a0489c2cdcd6508f (patch)
tree1e3c2ec94276dfcb2f8ba73a2ac075ba39c4a34a /models/issue_milestone_test.go
parent462306e263db5a809dbe2cdf62e99307aeff28de (diff)
downloadgitea-a4bfef265d9e512830350635a0489c2cdcd6508f.tar.gz
gitea-a4bfef265d9e512830350635a0489c2cdcd6508f.zip
Move db related basic functions to models/db (#17075)
* Move db related basic functions to models/db * Fix lint * Fix lint * Fix test * Fix lint * Fix lint * revert unnecessary change * Fix test * Fix wrong replace string * Use *Context * Correct committer spelling and fix wrong replaced words Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'models/issue_milestone_test.go')
-rw-r--r--models/issue_milestone_test.go101
1 files changed, 51 insertions, 50 deletions
diff --git a/models/issue_milestone_test.go b/models/issue_milestone_test.go
index 1472c951eb..fd12efb146 100644
--- a/models/issue_milestone_test.go
+++ b/models/issue_milestone_test.go
@@ -8,6 +8,7 @@ import (
"sort"
"testing"
+ "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
@@ -22,7 +23,7 @@ func TestMilestone_State(t *testing.T) {
}
func TestNewMilestone(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
+ assert.NoError(t, db.PrepareTestDatabase())
milestone := &Milestone{
RepoID: 1,
Name: "milestoneName",
@@ -30,26 +31,26 @@ func TestNewMilestone(t *testing.T) {
}
assert.NoError(t, NewMilestone(milestone))
- AssertExistsAndLoadBean(t, milestone)
+ db.AssertExistsAndLoadBean(t, milestone)
CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{})
}
func TestGetMilestoneByRepoID(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
+ assert.NoError(t, db.PrepareTestDatabase())
milestone, err := GetMilestoneByRepoID(1, 1)
assert.NoError(t, err)
assert.EqualValues(t, 1, milestone.ID)
assert.EqualValues(t, 1, milestone.RepoID)
- _, err = GetMilestoneByRepoID(NonexistentID, NonexistentID)
+ _, err = GetMilestoneByRepoID(db.NonexistentID, db.NonexistentID)
assert.True(t, IsErrMilestoneNotExist(err))
}
func TestGetMilestonesByRepoID(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
+ assert.NoError(t, db.PrepareTestDatabase())
test := func(repoID int64, state api.StateType) {
- repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
+ repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
milestones, _, err := GetMilestones(GetMilestonesOption{
RepoID: repo.ID,
State: state,
@@ -88,7 +89,7 @@ func TestGetMilestonesByRepoID(t *testing.T) {
test(3, api.StateAll)
milestones, _, err := GetMilestones(GetMilestonesOption{
- RepoID: NonexistentID,
+ RepoID: db.NonexistentID,
State: api.StateOpen,
})
assert.NoError(t, err)
@@ -96,8 +97,8 @@ func TestGetMilestonesByRepoID(t *testing.T) {
}
func TestGetMilestones(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
- repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
+ assert.NoError(t, db.PrepareTestDatabase())
+ repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
test := func(sortType string, sortCond func(*Milestone) int) {
for _, page := range []int{0, 1} {
milestones, _, err := GetMilestones(GetMilestonesOption{
@@ -157,22 +158,22 @@ func TestGetMilestones(t *testing.T) {
}
func TestUpdateMilestone(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
+ assert.NoError(t, db.PrepareTestDatabase())
- milestone := AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
+ milestone := db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
milestone.Name = " newMilestoneName "
milestone.Content = "newMilestoneContent"
assert.NoError(t, UpdateMilestone(milestone, milestone.IsClosed))
- milestone = AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
+ milestone = db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
assert.EqualValues(t, "newMilestoneName", milestone.Name)
CheckConsistencyFor(t, &Milestone{})
}
func TestCountRepoMilestones(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
+ assert.NoError(t, db.PrepareTestDatabase())
test := func(repoID int64) {
- repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
- count, err := countRepoMilestones(x, repoID)
+ repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
+ count, err := countRepoMilestones(db.DefaultContext().Engine(), repoID)
assert.NoError(t, err)
assert.EqualValues(t, repo.NumMilestones, count)
}
@@ -180,15 +181,15 @@ func TestCountRepoMilestones(t *testing.T) {
test(2)
test(3)
- count, err := countRepoMilestones(x, NonexistentID)
+ count, err := countRepoMilestones(db.DefaultContext().Engine(), db.NonexistentID)
assert.NoError(t, err)
assert.EqualValues(t, 0, count)
}
func TestCountRepoClosedMilestones(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
+ assert.NoError(t, db.PrepareTestDatabase())
test := func(repoID int64) {
- repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
+ repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
count, err := CountRepoClosedMilestones(repoID)
assert.NoError(t, err)
assert.EqualValues(t, repo.NumClosedMilestones, count)
@@ -197,55 +198,55 @@ func TestCountRepoClosedMilestones(t *testing.T) {
test(2)
test(3)
- count, err := CountRepoClosedMilestones(NonexistentID)
+ count, err := CountRepoClosedMilestones(db.NonexistentID)
assert.NoError(t, err)
assert.EqualValues(t, 0, count)
}
func TestChangeMilestoneStatus(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
- milestone := AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
+ assert.NoError(t, db.PrepareTestDatabase())
+ milestone := db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
assert.NoError(t, ChangeMilestoneStatus(milestone, true))
- AssertExistsAndLoadBean(t, &Milestone{ID: 1}, "is_closed=1")
+ db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}, "is_closed=1")
CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{})
assert.NoError(t, ChangeMilestoneStatus(milestone, false))
- AssertExistsAndLoadBean(t, &Milestone{ID: 1}, "is_closed=0")
+ db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}, "is_closed=0")
CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{})
}
func TestUpdateMilestoneCounters(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
- issue := AssertExistsAndLoadBean(t, &Issue{MilestoneID: 1},
+ assert.NoError(t, db.PrepareTestDatabase())
+ issue := db.AssertExistsAndLoadBean(t, &Issue{MilestoneID: 1},
"is_closed=0").(*Issue)
issue.IsClosed = true
issue.ClosedUnix = timeutil.TimeStampNow()
- _, err := x.ID(issue.ID).Cols("is_closed", "closed_unix").Update(issue)
+ _, err := db.DefaultContext().Engine().ID(issue.ID).Cols("is_closed", "closed_unix").Update(issue)
assert.NoError(t, err)
- assert.NoError(t, updateMilestoneCounters(x, issue.MilestoneID))
+ assert.NoError(t, updateMilestoneCounters(db.DefaultContext().Engine(), issue.MilestoneID))
CheckConsistencyFor(t, &Milestone{})
issue.IsClosed = false
issue.ClosedUnix = 0
- _, err = x.ID(issue.ID).Cols("is_closed", "closed_unix").Update(issue)
+ _, err = db.DefaultContext().Engine().ID(issue.ID).Cols("is_closed", "closed_unix").Update(issue)
assert.NoError(t, err)
- assert.NoError(t, updateMilestoneCounters(x, issue.MilestoneID))
+ assert.NoError(t, updateMilestoneCounters(db.DefaultContext().Engine(), issue.MilestoneID))
CheckConsistencyFor(t, &Milestone{})
}
func TestChangeMilestoneAssign(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
- issue := AssertExistsAndLoadBean(t, &Issue{RepoID: 1}).(*Issue)
- doer := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
+ assert.NoError(t, db.PrepareTestDatabase())
+ issue := db.AssertExistsAndLoadBean(t, &Issue{RepoID: 1}).(*Issue)
+ doer := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
assert.NotNil(t, issue)
assert.NotNil(t, doer)
oldMilestoneID := issue.MilestoneID
issue.MilestoneID = 2
assert.NoError(t, ChangeMilestoneAssign(issue, doer, oldMilestoneID))
- AssertExistsAndLoadBean(t, &Comment{
+ db.AssertExistsAndLoadBean(t, &Comment{
IssueID: issue.ID,
Type: CommentTypeMilestone,
MilestoneID: issue.MilestoneID,
@@ -255,18 +256,18 @@ func TestChangeMilestoneAssign(t *testing.T) {
}
func TestDeleteMilestoneByRepoID(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
+ assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, DeleteMilestoneByRepoID(1, 1))
- AssertNotExistsBean(t, &Milestone{ID: 1})
+ db.AssertNotExistsBean(t, &Milestone{ID: 1})
CheckConsistencyFor(t, &Repository{ID: 1})
- assert.NoError(t, DeleteMilestoneByRepoID(NonexistentID, NonexistentID))
+ assert.NoError(t, DeleteMilestoneByRepoID(db.NonexistentID, db.NonexistentID))
}
func TestMilestoneList_LoadTotalTrackedTimes(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
+ assert.NoError(t, db.PrepareTestDatabase())
miles := MilestoneList{
- AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone),
+ db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone),
}
assert.NoError(t, miles.LoadTotalTrackedTimes())
@@ -275,9 +276,9 @@ func TestMilestoneList_LoadTotalTrackedTimes(t *testing.T) {
}
func TestCountMilestonesByRepoIDs(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
+ assert.NoError(t, db.PrepareTestDatabase())
milestonesCount := func(repoID int64) (int, int) {
- repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
+ repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
return repo.NumOpenMilestones, repo.NumClosedMilestones
}
repo1OpenCount, repo1ClosedCount := milestonesCount(1)
@@ -295,9 +296,9 @@ func TestCountMilestonesByRepoIDs(t *testing.T) {
}
func TestGetMilestonesByRepoIDs(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
- repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
- repo2 := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
+ assert.NoError(t, db.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) {
for _, page := range []int{0, 1} {
openMilestones, err := GetMilestonesByRepoIDs([]int64{repo1.ID, repo2.ID}, page, false, sortType)
@@ -340,8 +341,8 @@ func TestGetMilestonesByRepoIDs(t *testing.T) {
}
func TestLoadTotalTrackedTime(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
- milestone := AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
+ assert.NoError(t, db.PrepareTestDatabase())
+ milestone := db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
assert.NoError(t, milestone.LoadTotalTrackedTime())
@@ -349,10 +350,10 @@ func TestLoadTotalTrackedTime(t *testing.T) {
}
func TestGetMilestonesStats(t *testing.T) {
- assert.NoError(t, PrepareTestDatabase())
+ assert.NoError(t, db.PrepareTestDatabase())
test := func(repoID int64) {
- repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
+ repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
stats, err := GetMilestonesStatsByRepoCond(builder.And(builder.Eq{"repo_id": repoID}))
assert.NoError(t, err)
assert.EqualValues(t, repo.NumMilestones-repo.NumClosedMilestones, stats.OpenCount)
@@ -362,13 +363,13 @@ func TestGetMilestonesStats(t *testing.T) {
test(2)
test(3)
- stats, err := GetMilestonesStatsByRepoCond(builder.And(builder.Eq{"repo_id": NonexistentID}))
+ stats, err := GetMilestonesStatsByRepoCond(builder.And(builder.Eq{"repo_id": db.NonexistentID}))
assert.NoError(t, err)
assert.EqualValues(t, 0, stats.OpenCount)
assert.EqualValues(t, 0, stats.ClosedCount)
- repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
- repo2 := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
+ repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
+ repo2 := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
milestoneStats, err := GetMilestonesStatsByRepoCond(builder.In("repo_id", []int64{repo1.ID, repo2.ID}))
assert.NoError(t, err)