2017-02-08 23:39:26 -07:00
|
|
|
// Copyright 2017 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.
|
|
|
|
|
2022-04-08 03:11:15 -06:00
|
|
|
package issues
|
2017-02-08 23:39:26 -07:00
|
|
|
|
|
|
|
import (
|
2017-06-12 09:01:09 -06:00
|
|
|
"sort"
|
2017-02-08 23:39:26 -07:00
|
|
|
"testing"
|
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-09 18:27:50 -07:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 07:36:47 -07:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2020-07-28 05:30:40 -06:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-05-11 04:21:34 -06:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2017-02-08 23:39:26 -07:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-05-12 15:54:35 -06:00
|
|
|
"xorm.io/builder"
|
2017-02-08 23:39:26 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMilestone_State(t *testing.T) {
|
|
|
|
assert.Equal(t, api.StateOpen, (&Milestone{IsClosed: false}).State())
|
|
|
|
assert.Equal(t, api.StateClosed, (&Milestone{IsClosed: true}).State())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetMilestoneByRepoID(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-08 23:39:26 -07:00
|
|
|
|
2022-04-08 03:11:15 -06:00
|
|
|
milestone, err := GetMilestoneByRepoID(db.DefaultContext, 1, 1)
|
2017-02-08 23:39:26 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 1, milestone.ID)
|
|
|
|
assert.EqualValues(t, 1, milestone.RepoID)
|
|
|
|
|
2022-04-08 03:11:15 -06:00
|
|
|
_, err = GetMilestoneByRepoID(db.DefaultContext, unittest.NonexistentID, unittest.NonexistentID)
|
2017-02-08 23:39:26 -07:00
|
|
|
assert.True(t, IsErrMilestoneNotExist(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetMilestonesByRepoID(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-06-05 18:37:45 -06:00
|
|
|
test := func(repoID int64, state api.StateType) {
|
2021-12-09 18:27:50 -07:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repoID}).(*repo_model.Repository)
|
2021-08-12 06:43:08 -06:00
|
|
|
milestones, _, err := GetMilestones(GetMilestonesOption{
|
2020-07-28 05:30:40 -06:00
|
|
|
RepoID: repo.ID,
|
|
|
|
State: state,
|
|
|
|
})
|
2017-02-08 23:39:26 -07:00
|
|
|
assert.NoError(t, err)
|
2019-06-05 18:37:45 -06:00
|
|
|
|
|
|
|
var n int
|
|
|
|
|
|
|
|
switch state {
|
|
|
|
case api.StateClosed:
|
|
|
|
n = repo.NumClosedMilestones
|
|
|
|
|
|
|
|
case api.StateAll:
|
|
|
|
n = repo.NumMilestones
|
|
|
|
|
|
|
|
case api.StateOpen:
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
default:
|
|
|
|
n = repo.NumOpenMilestones
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(t, milestones, n)
|
2017-02-08 23:39:26 -07:00
|
|
|
for _, milestone := range milestones {
|
|
|
|
assert.EqualValues(t, repoID, milestone.RepoID)
|
|
|
|
}
|
|
|
|
}
|
2019-06-05 18:37:45 -06:00
|
|
|
test(1, api.StateOpen)
|
|
|
|
test(1, api.StateAll)
|
|
|
|
test(1, api.StateClosed)
|
|
|
|
test(2, api.StateOpen)
|
|
|
|
test(2, api.StateAll)
|
|
|
|
test(2, api.StateClosed)
|
|
|
|
test(3, api.StateOpen)
|
|
|
|
test(3, api.StateClosed)
|
|
|
|
test(3, api.StateAll)
|
2017-02-08 23:39:26 -07:00
|
|
|
|
2021-08-12 06:43:08 -06:00
|
|
|
milestones, _, err := GetMilestones(GetMilestonesOption{
|
2021-11-16 01:53:21 -07:00
|
|
|
RepoID: unittest.NonexistentID,
|
2020-07-28 05:30:40 -06:00
|
|
|
State: api.StateOpen,
|
|
|
|
})
|
2017-02-08 23:39:26 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, milestones, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetMilestones(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-12-09 18:27:50 -07:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
|
2017-02-08 23:39:26 -07:00
|
|
|
test := func(sortType string, sortCond func(*Milestone) int) {
|
|
|
|
for _, page := range []int{0, 1} {
|
2021-08-12 06:43:08 -06:00
|
|
|
milestones, _, err := GetMilestones(GetMilestonesOption{
|
2021-09-24 05:32:56 -06:00
|
|
|
ListOptions: db.ListOptions{
|
2020-07-28 05:30:40 -06:00
|
|
|
Page: page,
|
|
|
|
PageSize: setting.UI.IssuePagingNum,
|
|
|
|
},
|
|
|
|
RepoID: repo.ID,
|
|
|
|
State: api.StateOpen,
|
|
|
|
SortType: sortType,
|
|
|
|
})
|
2017-02-08 23:39:26 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, milestones, repo.NumMilestones-repo.NumClosedMilestones)
|
|
|
|
values := make([]int, len(milestones))
|
|
|
|
for i, milestone := range milestones {
|
|
|
|
values[i] = sortCond(milestone)
|
|
|
|
}
|
|
|
|
assert.True(t, sort.IntsAreSorted(values))
|
|
|
|
|
2021-08-12 06:43:08 -06:00
|
|
|
milestones, _, err = GetMilestones(GetMilestonesOption{
|
2021-09-24 05:32:56 -06:00
|
|
|
ListOptions: db.ListOptions{
|
2020-07-28 05:30:40 -06:00
|
|
|
Page: page,
|
|
|
|
PageSize: setting.UI.IssuePagingNum,
|
|
|
|
},
|
|
|
|
RepoID: repo.ID,
|
|
|
|
State: api.StateClosed,
|
|
|
|
Name: "",
|
|
|
|
SortType: sortType,
|
|
|
|
})
|
2017-02-08 23:39:26 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, milestones, repo.NumClosedMilestones)
|
|
|
|
values = make([]int, len(milestones))
|
|
|
|
for i, milestone := range milestones {
|
|
|
|
values[i] = sortCond(milestone)
|
|
|
|
}
|
|
|
|
assert.True(t, sort.IntsAreSorted(values))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
test("furthestduedate", func(milestone *Milestone) int {
|
|
|
|
return -int(milestone.DeadlineUnix)
|
|
|
|
})
|
|
|
|
test("leastcomplete", func(milestone *Milestone) int {
|
|
|
|
return milestone.Completeness
|
|
|
|
})
|
|
|
|
test("mostcomplete", func(milestone *Milestone) int {
|
|
|
|
return -milestone.Completeness
|
|
|
|
})
|
|
|
|
test("leastissues", func(milestone *Milestone) int {
|
|
|
|
return milestone.NumIssues
|
|
|
|
})
|
|
|
|
test("mostissues", func(milestone *Milestone) int {
|
|
|
|
return -milestone.NumIssues
|
|
|
|
})
|
|
|
|
test("soonestduedate", func(milestone *Milestone) int {
|
|
|
|
return int(milestone.DeadlineUnix)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCountRepoMilestones(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-08 23:39:26 -07:00
|
|
|
test := func(repoID int64) {
|
2021-12-09 18:27:50 -07:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repoID}).(*repo_model.Repository)
|
2022-05-20 08:08:52 -06:00
|
|
|
count, err := countRepoMilestones(db.DefaultContext, repoID)
|
2017-12-18 07:06:51 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, repo.NumMilestones, count)
|
2017-02-08 23:39:26 -07:00
|
|
|
}
|
|
|
|
test(1)
|
|
|
|
test(2)
|
|
|
|
test(3)
|
2017-12-18 07:06:51 -07:00
|
|
|
|
2022-05-20 08:08:52 -06:00
|
|
|
count, err := countRepoMilestones(db.DefaultContext, unittest.NonexistentID)
|
2017-12-18 07:06:51 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 0, count)
|
2017-02-08 23:39:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCountRepoClosedMilestones(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-02-08 23:39:26 -07:00
|
|
|
test := func(repoID int64) {
|
2021-12-09 18:27:50 -07:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repoID}).(*repo_model.Repository)
|
2017-12-18 07:06:51 -07:00
|
|
|
count, err := CountRepoClosedMilestones(repoID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, repo.NumClosedMilestones, count)
|
2017-02-08 23:39:26 -07:00
|
|
|
}
|
|
|
|
test(1)
|
|
|
|
test(2)
|
|
|
|
test(3)
|
2017-12-18 07:06:51 -07:00
|
|
|
|
2021-11-16 01:53:21 -07:00
|
|
|
count, err := CountRepoClosedMilestones(unittest.NonexistentID)
|
2017-12-18 07:06:51 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 0, count)
|
2017-02-08 23:39:26 -07:00
|
|
|
}
|
|
|
|
|
2019-12-15 07:20:08 -07:00
|
|
|
func TestCountMilestonesByRepoIDs(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2019-12-15 07:20:08 -07:00
|
|
|
milestonesCount := func(repoID int64) (int, int) {
|
2021-12-09 18:27:50 -07:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repoID}).(*repo_model.Repository)
|
2019-12-15 07:20:08 -07:00
|
|
|
return repo.NumOpenMilestones, repo.NumClosedMilestones
|
|
|
|
}
|
|
|
|
repo1OpenCount, repo1ClosedCount := milestonesCount(1)
|
|
|
|
repo2OpenCount, repo2ClosedCount := milestonesCount(2)
|
|
|
|
|
2020-05-12 15:54:35 -06:00
|
|
|
openCounts, err := CountMilestonesByRepoCond(builder.In("repo_id", []int64{1, 2}), false)
|
2019-12-15 07:20:08 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, repo1OpenCount, openCounts[1])
|
|
|
|
assert.EqualValues(t, repo2OpenCount, openCounts[2])
|
|
|
|
|
2020-05-12 15:54:35 -06:00
|
|
|
closedCounts, err := CountMilestonesByRepoCond(builder.In("repo_id", []int64{1, 2}), true)
|
2019-12-15 07:20:08 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, repo1ClosedCount, closedCounts[1])
|
|
|
|
assert.EqualValues(t, repo2ClosedCount, closedCounts[2])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetMilestonesByRepoIDs(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-12-09 18:27:50 -07:00
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
|
|
|
|
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}).(*repo_model.Repository)
|
2019-12-15 07:20:08 -07:00
|
|
|
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)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, openMilestones, repo1.NumOpenMilestones+repo2.NumOpenMilestones)
|
|
|
|
values := make([]int, len(openMilestones))
|
|
|
|
for i, milestone := range openMilestones {
|
|
|
|
values[i] = sortCond(milestone)
|
|
|
|
}
|
|
|
|
assert.True(t, sort.IntsAreSorted(values))
|
|
|
|
|
|
|
|
closedMilestones, err := GetMilestonesByRepoIDs([]int64{repo1.ID, repo2.ID}, page, true, sortType)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, closedMilestones, repo1.NumClosedMilestones+repo2.NumClosedMilestones)
|
|
|
|
values = make([]int, len(closedMilestones))
|
|
|
|
for i, milestone := range closedMilestones {
|
|
|
|
values[i] = sortCond(milestone)
|
|
|
|
}
|
|
|
|
assert.True(t, sort.IntsAreSorted(values))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
test("furthestduedate", func(milestone *Milestone) int {
|
|
|
|
return -int(milestone.DeadlineUnix)
|
|
|
|
})
|
|
|
|
test("leastcomplete", func(milestone *Milestone) int {
|
|
|
|
return milestone.Completeness
|
|
|
|
})
|
|
|
|
test("mostcomplete", func(milestone *Milestone) int {
|
|
|
|
return -milestone.Completeness
|
|
|
|
})
|
|
|
|
test("leastissues", func(milestone *Milestone) int {
|
|
|
|
return milestone.NumIssues
|
|
|
|
})
|
|
|
|
test("mostissues", func(milestone *Milestone) int {
|
|
|
|
return -milestone.NumIssues
|
|
|
|
})
|
|
|
|
test("soonestduedate", func(milestone *Milestone) int {
|
|
|
|
return int(milestone.DeadlineUnix)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetMilestonesStats(t *testing.T) {
|
2021-11-12 07:36:47 -07:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2020-05-12 15:54:35 -06:00
|
|
|
|
|
|
|
test := func(repoID int64) {
|
2021-12-09 18:27:50 -07:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repoID}).(*repo_model.Repository)
|
2020-05-12 15:54:35 -06:00
|
|
|
stats, err := GetMilestonesStatsByRepoCond(builder.And(builder.Eq{"repo_id": repoID}))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, repo.NumMilestones-repo.NumClosedMilestones, stats.OpenCount)
|
|
|
|
assert.EqualValues(t, repo.NumClosedMilestones, stats.ClosedCount)
|
|
|
|
}
|
|
|
|
test(1)
|
|
|
|
test(2)
|
|
|
|
test(3)
|
|
|
|
|
2021-11-16 01:53:21 -07:00
|
|
|
stats, err := GetMilestonesStatsByRepoCond(builder.And(builder.Eq{"repo_id": unittest.NonexistentID}))
|
2020-05-12 15:54:35 -06:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 0, stats.OpenCount)
|
|
|
|
assert.EqualValues(t, 0, stats.ClosedCount)
|
|
|
|
|
2021-12-09 18:27:50 -07:00
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
|
|
|
|
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}).(*repo_model.Repository)
|
2019-12-15 07:20:08 -07:00
|
|
|
|
2020-05-12 15:54:35 -06:00
|
|
|
milestoneStats, err := GetMilestonesStatsByRepoCond(builder.In("repo_id", []int64{repo1.ID, repo2.ID}))
|
2019-12-15 07:20:08 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, repo1.NumOpenMilestones+repo2.NumOpenMilestones, milestoneStats.OpenCount)
|
|
|
|
assert.EqualValues(t, repo1.NumClosedMilestones+repo2.NumClosedMilestones, milestoneStats.ClosedCount)
|
|
|
|
}
|