2020-08-17 05:07:38 +02:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-08-17 05:07:38 +02:00
|
|
|
|
2022-03-29 16:16:31 +02:00
|
|
|
package project
|
2020-08-17 05:07:38 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-05-20 16:08:52 +02:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-12 15:36:47 +01:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2020-08-17 05:07:38 +02:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIsProjectTypeValid(t *testing.T) {
|
2022-03-29 16:16:31 +02:00
|
|
|
const UnknownType Type = 15
|
2020-08-17 05:07:38 +02:00
|
|
|
|
2021-03-14 19:52:12 +01:00
|
|
|
cases := []struct {
|
2022-03-29 16:16:31 +02:00
|
|
|
typ Type
|
2020-08-17 05:07:38 +02:00
|
|
|
valid bool
|
|
|
|
}{
|
2023-03-17 14:07:23 +01:00
|
|
|
{TypeIndividual, true},
|
2022-03-29 16:16:31 +02:00
|
|
|
{TypeRepository, true},
|
2023-01-20 12:42:33 +01:00
|
|
|
{TypeOrganization, true},
|
2020-08-17 05:07:38 +02:00
|
|
|
{UnknownType, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range cases {
|
2022-03-29 16:16:31 +02:00
|
|
|
assert.Equal(t, v.valid, IsTypeValid(v.typ))
|
2020-08-17 05:07:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetProjects(t *testing.T) {
|
2021-11-12 15:36:47 +01:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2020-08-17 05:07:38 +02:00
|
|
|
|
2023-11-24 04:49:41 +01:00
|
|
|
projects, err := db.Find[Project](db.DefaultContext, SearchOptions{RepoID: 1})
|
2020-08-17 05:07:38 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// 1 value for this repo exists in the fixtures
|
|
|
|
assert.Len(t, projects, 1)
|
|
|
|
|
2023-11-24 04:49:41 +01:00
|
|
|
projects, err = db.Find[Project](db.DefaultContext, SearchOptions{RepoID: 3})
|
2020-08-17 05:07:38 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// 1 value for this repo exists in the fixtures
|
|
|
|
assert.Len(t, projects, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProject(t *testing.T) {
|
2021-11-12 15:36:47 +01:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2020-08-17 05:07:38 +02:00
|
|
|
|
|
|
|
project := &Project{
|
2022-03-29 16:16:31 +02:00
|
|
|
Type: TypeRepository,
|
|
|
|
BoardType: BoardTypeBasicKanban,
|
2023-02-11 09:12:41 +01:00
|
|
|
CardType: CardTypeTextOnly,
|
2020-08-17 05:07:38 +02:00
|
|
|
Title: "New Project",
|
|
|
|
RepoID: 1,
|
|
|
|
CreatedUnix: timeutil.TimeStampNow(),
|
|
|
|
CreatorID: 2,
|
|
|
|
}
|
|
|
|
|
2023-09-29 14:12:54 +02:00
|
|
|
assert.NoError(t, NewProject(db.DefaultContext, project))
|
2020-08-17 05:07:38 +02:00
|
|
|
|
2022-05-20 16:08:52 +02:00
|
|
|
_, err := GetProjectByID(db.DefaultContext, project.ID)
|
2020-08-17 05:07:38 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Update project
|
|
|
|
project.Title = "Updated title"
|
2022-05-20 16:08:52 +02:00
|
|
|
assert.NoError(t, UpdateProject(db.DefaultContext, project))
|
2020-08-17 05:07:38 +02:00
|
|
|
|
2022-05-20 16:08:52 +02:00
|
|
|
projectFromDB, err := GetProjectByID(db.DefaultContext, project.ID)
|
2020-08-17 05:07:38 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, project.Title, projectFromDB.Title)
|
|
|
|
|
2023-09-29 14:12:54 +02:00
|
|
|
assert.NoError(t, ChangeProjectStatus(db.DefaultContext, project, true))
|
2020-08-17 05:07:38 +02:00
|
|
|
|
|
|
|
// Retrieve from DB afresh to check if it is truly closed
|
2022-05-20 16:08:52 +02:00
|
|
|
projectFromDB, err = GetProjectByID(db.DefaultContext, project.ID)
|
2020-08-17 05:07:38 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.True(t, projectFromDB.IsClosed)
|
|
|
|
}
|
2023-07-11 20:47:50 +02:00
|
|
|
|
|
|
|
func TestProjectsSort(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
sortType string
|
|
|
|
wants []int64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
sortType: "default",
|
2024-03-27 21:54:32 +01:00
|
|
|
wants: []int64{1, 3, 2, 6, 5, 4},
|
2023-07-11 20:47:50 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
sortType: "oldest",
|
2024-03-27 21:54:32 +01:00
|
|
|
wants: []int64{4, 5, 6, 2, 3, 1},
|
2023-07-11 20:47:50 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
sortType: "recentupdate",
|
2024-03-27 21:54:32 +01:00
|
|
|
wants: []int64{1, 3, 2, 6, 5, 4},
|
2023-07-11 20:47:50 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
sortType: "leastupdate",
|
2024-03-27 21:54:32 +01:00
|
|
|
wants: []int64{4, 5, 6, 2, 3, 1},
|
2023-07-11 20:47:50 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2023-11-24 04:49:41 +01:00
|
|
|
projects, count, err := db.FindAndCount[Project](db.DefaultContext, SearchOptions{
|
2023-07-11 20:47:50 +02:00
|
|
|
OrderBy: GetSearchOrderByBySortType(tt.sortType),
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2024-03-27 21:54:32 +01:00
|
|
|
assert.EqualValues(t, int64(6), count)
|
|
|
|
if assert.Len(t, projects, 6) {
|
2023-07-11 20:47:50 +02:00
|
|
|
for i := range projects {
|
|
|
|
assert.EqualValues(t, tt.wants[i], projects[i].ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|