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