code.gitea.io/gitea@v1.21.7/models/project/project_test.go (about) 1 // Copyright 2020 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package project 5 6 import ( 7 "testing" 8 9 "code.gitea.io/gitea/models/db" 10 "code.gitea.io/gitea/models/unittest" 11 "code.gitea.io/gitea/modules/timeutil" 12 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestIsProjectTypeValid(t *testing.T) { 17 const UnknownType Type = 15 18 19 cases := []struct { 20 typ Type 21 valid bool 22 }{ 23 {TypeIndividual, true}, 24 {TypeRepository, true}, 25 {TypeOrganization, true}, 26 {UnknownType, false}, 27 } 28 29 for _, v := range cases { 30 assert.Equal(t, v.valid, IsTypeValid(v.typ)) 31 } 32 } 33 34 func TestGetProjects(t *testing.T) { 35 assert.NoError(t, unittest.PrepareTestDatabase()) 36 37 projects, _, err := FindProjects(db.DefaultContext, SearchOptions{RepoID: 1}) 38 assert.NoError(t, err) 39 40 // 1 value for this repo exists in the fixtures 41 assert.Len(t, projects, 1) 42 43 projects, _, err = FindProjects(db.DefaultContext, SearchOptions{RepoID: 3}) 44 assert.NoError(t, err) 45 46 // 1 value for this repo exists in the fixtures 47 assert.Len(t, projects, 1) 48 } 49 50 func TestProject(t *testing.T) { 51 assert.NoError(t, unittest.PrepareTestDatabase()) 52 53 project := &Project{ 54 Type: TypeRepository, 55 BoardType: BoardTypeBasicKanban, 56 CardType: CardTypeTextOnly, 57 Title: "New Project", 58 RepoID: 1, 59 CreatedUnix: timeutil.TimeStampNow(), 60 CreatorID: 2, 61 } 62 63 assert.NoError(t, NewProject(db.DefaultContext, project)) 64 65 _, err := GetProjectByID(db.DefaultContext, project.ID) 66 assert.NoError(t, err) 67 68 // Update project 69 project.Title = "Updated title" 70 assert.NoError(t, UpdateProject(db.DefaultContext, project)) 71 72 projectFromDB, err := GetProjectByID(db.DefaultContext, project.ID) 73 assert.NoError(t, err) 74 75 assert.Equal(t, project.Title, projectFromDB.Title) 76 77 assert.NoError(t, ChangeProjectStatus(db.DefaultContext, project, true)) 78 79 // Retrieve from DB afresh to check if it is truly closed 80 projectFromDB, err = GetProjectByID(db.DefaultContext, project.ID) 81 assert.NoError(t, err) 82 83 assert.True(t, projectFromDB.IsClosed) 84 } 85 86 func TestProjectsSort(t *testing.T) { 87 assert.NoError(t, unittest.PrepareTestDatabase()) 88 89 tests := []struct { 90 sortType string 91 wants []int64 92 }{ 93 { 94 sortType: "default", 95 wants: []int64{1, 3, 2, 4}, 96 }, 97 { 98 sortType: "oldest", 99 wants: []int64{4, 2, 3, 1}, 100 }, 101 { 102 sortType: "recentupdate", 103 wants: []int64{1, 3, 2, 4}, 104 }, 105 { 106 sortType: "leastupdate", 107 wants: []int64{4, 2, 3, 1}, 108 }, 109 } 110 111 for _, tt := range tests { 112 projects, count, err := FindProjects(db.DefaultContext, SearchOptions{ 113 OrderBy: GetSearchOrderByBySortType(tt.sortType), 114 }) 115 assert.NoError(t, err) 116 assert.EqualValues(t, int64(4), count) 117 if assert.Len(t, projects, 4) { 118 for i := range projects { 119 assert.EqualValues(t, tt.wants[i], projects[i].ID) 120 } 121 } 122 } 123 }