diff options
author | yp05327 <576951401@qq.com> | 2023-07-12 03:47:50 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-11 20:47:50 +0200 |
commit | 44572e924347420d3e11823b93921e01a5529959 (patch) | |
tree | 83e8361c259c94318b97a8d1645025d4294a3e42 /models/project | |
parent | 491cc06ffe3491242ad9ff6227423d99e673d0c2 (diff) | |
download | gitea-44572e924347420d3e11823b93921e01a5529959.tar.gz gitea-44572e924347420d3e11823b93921e01a5529959.zip |
Fix incorrect oldest sort in project list (#25806)
sort type `oldest` should be `Asc`.
Added a test for this.
Diffstat (limited to 'models/project')
-rw-r--r-- | models/project/project.go | 28 | ||||
-rw-r--r-- | models/project/project_test.go | 39 |
2 files changed, 54 insertions, 13 deletions
diff --git a/models/project/project.go b/models/project/project.go index 44609e60b2..6fa2adf604 100644 --- a/models/project/project.go +++ b/models/project/project.go @@ -196,7 +196,7 @@ type SearchOptions struct { RepoID int64 Page int IsClosed util.OptionalBool - SortType string + OrderBy db.SearchOrderBy Type Type } @@ -226,26 +226,28 @@ func CountProjects(ctx context.Context, opts SearchOptions) (int64, error) { return db.GetEngine(ctx).Where(opts.toConds()).Count(new(Project)) } +func GetSearchOrderByBySortType(sortType string) db.SearchOrderBy { + switch sortType { + case "oldest": + return db.SearchOrderByOldest + case "recentupdate": + return db.SearchOrderByRecentUpdated + case "leastupdate": + return db.SearchOrderByLeastUpdated + default: + return db.SearchOrderByNewest + } +} + // FindProjects returns a list of all projects that have been created in the repository func FindProjects(ctx context.Context, opts SearchOptions) ([]*Project, int64, error) { - e := db.GetEngine(ctx).Where(opts.toConds()) + e := db.GetEngine(ctx).Where(opts.toConds()).OrderBy(opts.OrderBy.String()) projects := make([]*Project, 0, setting.UI.IssuePagingNum) if opts.Page > 0 { e = e.Limit(setting.UI.IssuePagingNum, (opts.Page-1)*setting.UI.IssuePagingNum) } - switch opts.SortType { - case "oldest": - e.Desc("created_unix") - case "recentupdate": - e.Desc("updated_unix") - case "leastupdate": - e.Asc("updated_unix") - default: - e.Asc("created_unix") - } - count, err := e.FindAndCount(&projects) return projects, count, err } diff --git a/models/project/project_test.go b/models/project/project_test.go index 71ceda7aa5..d1a4715653 100644 --- a/models/project/project_test.go +++ b/models/project/project_test.go @@ -82,3 +82,42 @@ func TestProject(t *testing.T) { assert.True(t, projectFromDB.IsClosed) } + +func TestProjectsSort(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + tests := []struct { + sortType string + wants []int64 + }{ + { + sortType: "default", + wants: []int64{1, 3, 2, 4}, + }, + { + sortType: "oldest", + wants: []int64{4, 2, 3, 1}, + }, + { + sortType: "recentupdate", + wants: []int64{1, 3, 2, 4}, + }, + { + sortType: "leastupdate", + wants: []int64{4, 2, 3, 1}, + }, + } + + for _, tt := range tests { + projects, count, err := FindProjects(db.DefaultContext, SearchOptions{ + OrderBy: GetSearchOrderByBySortType(tt.sortType), + }) + assert.NoError(t, err) + assert.EqualValues(t, int64(4), count) + if assert.Len(t, projects, 4) { + for i := range projects { + assert.EqualValues(t, tt.wants[i], projects[i].ID) + } + } + } +} |