Skip to content

Commit 58881f2

Browse files
wxiaoguangStelios Malathouras
authored and
Stelios Malathouras
committed
Decouple unit test code from business code (go-gitea#17623)
1 parent 101f5d2 commit 58881f2

File tree

136 files changed

+1057
-829
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

136 files changed

+1057
-829
lines changed

contrib/fixtures/fixture_generation.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"path/filepath"
1111

1212
"code.gitea.io/gitea/models"
13-
"code.gitea.io/gitea/models/db"
13+
"code.gitea.io/gitea/models/unittest"
1414
)
1515

1616
// To generate derivative fixtures, execute the following from Gitea's repository base dir:
@@ -31,13 +31,13 @@ var (
3131
func main() {
3232
pathToGiteaRoot := "."
3333
fixturesDir = filepath.Join(pathToGiteaRoot, "models", "fixtures")
34-
if err := db.CreateTestEngine(db.FixturesOptions{
34+
if err := unittest.CreateTestEngine(unittest.FixturesOptions{
3535
Dir: fixturesDir,
3636
}); err != nil {
3737
fmt.Printf("CreateTestEngine: %+v", err)
3838
os.Exit(1)
3939
}
40-
if err := db.PrepareTestDatabase(); err != nil {
40+
if err := unittest.PrepareTestDatabase(); err != nil {
4141
fmt.Printf("PrepareTestDatabase: %+v\n", err)
4242
os.Exit(1)
4343
}

contrib/pr/checkout.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"code.gitea.io/gitea/models"
2828
"code.gitea.io/gitea/models/db"
29+
"code.gitea.io/gitea/models/unittest"
2930
gitea_git "code.gitea.io/gitea/modules/git"
3031
"code.gitea.io/gitea/modules/markup"
3132
"code.gitea.io/gitea/modules/markup/external"
@@ -99,16 +100,16 @@ func runPR() {
99100
})
100101
db.HasEngine = true
101102
//x.ShowSQL(true)
102-
err = db.InitFixtures(
103-
db.FixturesOptions{
103+
err = unittest.InitFixtures(
104+
unittest.FixturesOptions{
104105
Dir: path.Join(curDir, "models/fixtures/"),
105106
},
106107
)
107108
if err != nil {
108109
fmt.Printf("Error initializing test database: %v\n", err)
109110
os.Exit(1)
110111
}
111-
db.LoadFixtures()
112+
unittest.LoadFixtures()
112113
util.RemoveAll(setting.RepoRootPath)
113114
util.RemoveAll(models.LocalCopyPath())
114115
util.CopyDir(path.Join(curDir, "integrations/gitea-repositories-meta"), setting.RepoRootPath)

integrations/api_issue_label_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"strings"
1111
"testing"
1212

13+
"code.gitea.io/gitea/models/unittest"
14+
1315
"code.gitea.io/gitea/models"
1416
"code.gitea.io/gitea/models/db"
1517
api "code.gitea.io/gitea/modules/structs"
@@ -18,7 +20,7 @@ import (
1820
)
1921

2022
func TestAPIModifyLabels(t *testing.T) {
21-
assert.NoError(t, db.LoadFixtures())
23+
assert.NoError(t, unittest.LoadFixtures())
2224

2325
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository)
2426
owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
@@ -88,7 +90,7 @@ func TestAPIModifyLabels(t *testing.T) {
8890
}
8991

9092
func TestAPIAddIssueLabels(t *testing.T) {
91-
assert.NoError(t, db.LoadFixtures())
93+
assert.NoError(t, unittest.LoadFixtures())
9294

9395
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
9496
issue := db.AssertExistsAndLoadBean(t, &models.Issue{RepoID: repo.ID}).(*models.Issue)
@@ -111,7 +113,7 @@ func TestAPIAddIssueLabels(t *testing.T) {
111113
}
112114

113115
func TestAPIReplaceIssueLabels(t *testing.T) {
114-
assert.NoError(t, db.LoadFixtures())
116+
assert.NoError(t, unittest.LoadFixtures())
115117

116118
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
117119
issue := db.AssertExistsAndLoadBean(t, &models.Issue{RepoID: repo.ID}).(*models.Issue)
@@ -137,7 +139,7 @@ func TestAPIReplaceIssueLabels(t *testing.T) {
137139
}
138140

139141
func TestAPIModifyOrgLabels(t *testing.T) {
140-
assert.NoError(t, db.LoadFixtures())
142+
assert.NoError(t, unittest.LoadFixtures())
141143

142144
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository)
143145
owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)

integrations/integration_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"time"
2626

2727
"code.gitea.io/gitea/models"
28-
"code.gitea.io/gitea/models/db"
28+
"code.gitea.io/gitea/models/unittest"
2929
"code.gitea.io/gitea/modules/base"
3030
"code.gitea.io/gitea/modules/git"
3131
"code.gitea.io/gitea/modules/graceful"
@@ -84,6 +84,7 @@ func NewNilResponseHashSumRecorder() *NilResponseHashSumRecorder {
8484
func TestMain(m *testing.M) {
8585
defer log.Close()
8686

87+
unittest.InitUnitTestBridge()
8788
managerCtx, cancel := context.WithCancel(context.Background())
8889
graceful.InitManager(managerCtx)
8990
defer cancel()
@@ -112,8 +113,8 @@ func TestMain(m *testing.M) {
112113
}
113114
}
114115

115-
err := db.InitFixtures(
116-
db.FixturesOptions{
116+
err := unittest.InitFixtures(
117+
unittest.FixturesOptions{
117118
Dir: filepath.Join(filepath.Dir(setting.AppPath), "models/fixtures/"),
118119
},
119120
)
@@ -250,7 +251,7 @@ func prepareTestEnv(t testing.TB, skip ...int) func() {
250251
ourSkip += skip[0]
251252
}
252253
deferFn := PrintCurrentTest(t, ourSkip)
253-
assert.NoError(t, db.LoadFixtures())
254+
assert.NoError(t, unittest.LoadFixtures())
254255
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
255256

256257
assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
@@ -527,7 +528,7 @@ func GetCSRF(t testing.TB, session *TestSession, urlStr string) string {
527528
// within a single test this is required
528529
func resetFixtures(t *testing.T) {
529530
assert.NoError(t, queue.GetManager().FlushAll(context.Background(), -1))
530-
assert.NoError(t, db.LoadFixtures())
531+
assert.NoError(t, unittest.LoadFixtures())
531532
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
532533
assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
533534
}

integrations/migrate_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"os"
99
"testing"
1010

11+
"code.gitea.io/gitea/models/unittest"
12+
1113
"code.gitea.io/gitea/models"
1214
"code.gitea.io/gitea/models/db"
1315
"code.gitea.io/gitea/modules/migrations"
@@ -17,7 +19,7 @@ import (
1719
)
1820

1921
func TestMigrateLocalPath(t *testing.T) {
20-
assert.NoError(t, db.PrepareTestDatabase())
22+
assert.NoError(t, unittest.PrepareTestDatabase())
2123

2224
adminUser := db.AssertExistsAndLoadBean(t, &models.User{Name: "user1"}).(*models.User)
2325

integrations/repofiles_delete_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import (
88
"net/url"
99
"testing"
1010

11+
"code.gitea.io/gitea/models/unittest"
12+
1113
"code.gitea.io/gitea/models"
12-
"code.gitea.io/gitea/models/db"
1314
"code.gitea.io/gitea/modules/repofiles"
1415
api "code.gitea.io/gitea/modules/structs"
1516
"code.gitea.io/gitea/modules/test"
@@ -67,7 +68,7 @@ func TestDeleteRepoFile(t *testing.T) {
6768

6869
func testDeleteRepoFile(t *testing.T, u *url.URL) {
6970
// setup
70-
db.PrepareTestEnv(t)
71+
unittest.PrepareTestEnv(t)
7172
ctx := test.MockContext(t, "user2/repo1")
7273
ctx.SetParams(":id", "1")
7374
test.LoadRepo(t, ctx, 1)
@@ -106,7 +107,7 @@ func TestDeleteRepoFileWithoutBranchNames(t *testing.T) {
106107

107108
func testDeleteRepoFileWithoutBranchNames(t *testing.T, u *url.URL) {
108109
// setup
109-
db.PrepareTestEnv(t)
110+
unittest.PrepareTestEnv(t)
110111
ctx := test.MockContext(t, "user2/repo1")
111112
ctx.SetParams(":id", "1")
112113
test.LoadRepo(t, ctx, 1)
@@ -136,7 +137,7 @@ func testDeleteRepoFileWithoutBranchNames(t *testing.T, u *url.URL) {
136137

137138
func TestDeleteRepoFileErrors(t *testing.T) {
138139
// setup
139-
db.PrepareTestEnv(t)
140+
unittest.PrepareTestEnv(t)
140141
ctx := test.MockContext(t, "user2/repo1")
141142
ctx.SetParams(":id", "1")
142143
test.LoadRepo(t, ctx, 1)

models/access_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import (
88
"testing"
99

1010
"code.gitea.io/gitea/models/db"
11+
"code.gitea.io/gitea/models/unittest"
1112
"github.com/stretchr/testify/assert"
1213
)
1314

1415
func TestAccessLevel(t *testing.T) {
15-
assert.NoError(t, db.PrepareTestDatabase())
16+
assert.NoError(t, unittest.PrepareTestDatabase())
1617

1718
user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
1819
user5 := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
@@ -63,7 +64,7 @@ func TestAccessLevel(t *testing.T) {
6364
}
6465

6566
func TestHasAccess(t *testing.T) {
66-
assert.NoError(t, db.PrepareTestDatabase())
67+
assert.NoError(t, unittest.PrepareTestDatabase())
6768

6869
user1 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
6970
user2 := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
@@ -89,7 +90,7 @@ func TestHasAccess(t *testing.T) {
8990
}
9091

9192
func TestUser_GetRepositoryAccesses(t *testing.T) {
92-
assert.NoError(t, db.PrepareTestDatabase())
93+
assert.NoError(t, unittest.PrepareTestDatabase())
9394

9495
user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
9596
accesses, err := user1.GetRepositoryAccesses()
@@ -103,7 +104,7 @@ func TestUser_GetRepositoryAccesses(t *testing.T) {
103104
}
104105

105106
func TestUser_GetAccessibleRepositories(t *testing.T) {
106-
assert.NoError(t, db.PrepareTestDatabase())
107+
assert.NoError(t, unittest.PrepareTestDatabase())
107108

108109
user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
109110
repos, err := user1.GetAccessibleRepositories(0)
@@ -123,7 +124,7 @@ func TestUser_GetAccessibleRepositories(t *testing.T) {
123124

124125
func TestRepository_RecalculateAccesses(t *testing.T) {
125126
// test with organization repo
126-
assert.NoError(t, db.PrepareTestDatabase())
127+
assert.NoError(t, unittest.PrepareTestDatabase())
127128
repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
128129
assert.NoError(t, repo1.GetOwner())
129130

@@ -140,7 +141,7 @@ func TestRepository_RecalculateAccesses(t *testing.T) {
140141

141142
func TestRepository_RecalculateAccesses2(t *testing.T) {
142143
// test with non-organization repo
143-
assert.NoError(t, db.PrepareTestDatabase())
144+
assert.NoError(t, unittest.PrepareTestDatabase())
144145
repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
145146
assert.NoError(t, repo1.GetOwner())
146147

@@ -154,7 +155,7 @@ func TestRepository_RecalculateAccesses2(t *testing.T) {
154155
}
155156

156157
func TestRepository_RecalculateAccesses3(t *testing.T) {
157-
assert.NoError(t, db.PrepareTestDatabase())
158+
assert.NoError(t, unittest.PrepareTestDatabase())
158159
team5 := db.AssertExistsAndLoadBean(t, &Team{ID: 5}).(*Team)
159160
user29 := db.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User)
160161

models/action_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@ import (
99
"testing"
1010

1111
"code.gitea.io/gitea/models/db"
12+
"code.gitea.io/gitea/models/unittest"
1213
"code.gitea.io/gitea/modules/setting"
1314

1415
"github.com/stretchr/testify/assert"
1516
)
1617

1718
func TestAction_GetRepoPath(t *testing.T) {
18-
assert.NoError(t, db.PrepareTestDatabase())
19+
assert.NoError(t, unittest.PrepareTestDatabase())
1920
repo := db.AssertExistsAndLoadBean(t, &Repository{}).(*Repository)
2021
owner := db.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
2122
action := &Action{RepoID: repo.ID}
2223
assert.Equal(t, path.Join(owner.Name, repo.Name), action.GetRepoPath())
2324
}
2425

2526
func TestAction_GetRepoLink(t *testing.T) {
26-
assert.NoError(t, db.PrepareTestDatabase())
27+
assert.NoError(t, unittest.PrepareTestDatabase())
2728
repo := db.AssertExistsAndLoadBean(t, &Repository{}).(*Repository)
2829
owner := db.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
2930
action := &Action{RepoID: repo.ID}
@@ -34,7 +35,7 @@ func TestAction_GetRepoLink(t *testing.T) {
3435

3536
func TestGetFeeds(t *testing.T) {
3637
// test with an individual user
37-
assert.NoError(t, db.PrepareTestDatabase())
38+
assert.NoError(t, unittest.PrepareTestDatabase())
3839
user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
3940

4041
actions, err := GetFeeds(GetFeedsOptions{
@@ -62,7 +63,7 @@ func TestGetFeeds(t *testing.T) {
6263

6364
func TestGetFeeds2(t *testing.T) {
6465
// test with an organization user
65-
assert.NoError(t, db.PrepareTestDatabase())
66+
assert.NoError(t, unittest.PrepareTestDatabase())
6667
org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
6768
user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
6869

models/admin_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
"code.gitea.io/gitea/models/db"
11+
"code.gitea.io/gitea/models/unittest"
1112
"github.com/stretchr/testify/assert"
1213
)
1314

@@ -20,7 +21,7 @@ func TestNotice_TrStr(t *testing.T) {
2021
}
2122

2223
func TestCreateNotice(t *testing.T) {
23-
assert.NoError(t, db.PrepareTestDatabase())
24+
assert.NoError(t, unittest.PrepareTestDatabase())
2425

2526
noticeBean := &Notice{
2627
Type: NoticeRepository,
@@ -32,7 +33,7 @@ func TestCreateNotice(t *testing.T) {
3233
}
3334

3435
func TestCreateRepositoryNotice(t *testing.T) {
35-
assert.NoError(t, db.PrepareTestDatabase())
36+
assert.NoError(t, unittest.PrepareTestDatabase())
3637

3738
noticeBean := &Notice{
3839
Type: NoticeRepository,
@@ -46,12 +47,12 @@ func TestCreateRepositoryNotice(t *testing.T) {
4647
// TODO TestRemoveAllWithNotice
4748

4849
func TestCountNotices(t *testing.T) {
49-
assert.NoError(t, db.PrepareTestDatabase())
50+
assert.NoError(t, unittest.PrepareTestDatabase())
5051
assert.Equal(t, int64(3), CountNotices())
5152
}
5253

5354
func TestNotices(t *testing.T) {
54-
assert.NoError(t, db.PrepareTestDatabase())
55+
assert.NoError(t, unittest.PrepareTestDatabase())
5556

5657
notices, err := Notices(1, 2)
5758
assert.NoError(t, err)
@@ -68,7 +69,7 @@ func TestNotices(t *testing.T) {
6869
}
6970

7071
func TestDeleteNotice(t *testing.T) {
71-
assert.NoError(t, db.PrepareTestDatabase())
72+
assert.NoError(t, unittest.PrepareTestDatabase())
7273

7374
db.AssertExistsAndLoadBean(t, &Notice{ID: 3})
7475
assert.NoError(t, DeleteNotice(3))
@@ -77,7 +78,7 @@ func TestDeleteNotice(t *testing.T) {
7778

7879
func TestDeleteNotices(t *testing.T) {
7980
// delete a non-empty range
80-
assert.NoError(t, db.PrepareTestDatabase())
81+
assert.NoError(t, unittest.PrepareTestDatabase())
8182

8283
db.AssertExistsAndLoadBean(t, &Notice{ID: 1})
8384
db.AssertExistsAndLoadBean(t, &Notice{ID: 2})
@@ -90,7 +91,7 @@ func TestDeleteNotices(t *testing.T) {
9091

9192
func TestDeleteNotices2(t *testing.T) {
9293
// delete an empty range
93-
assert.NoError(t, db.PrepareTestDatabase())
94+
assert.NoError(t, unittest.PrepareTestDatabase())
9495

9596
db.AssertExistsAndLoadBean(t, &Notice{ID: 1})
9697
db.AssertExistsAndLoadBean(t, &Notice{ID: 2})
@@ -102,7 +103,7 @@ func TestDeleteNotices2(t *testing.T) {
102103
}
103104

104105
func TestDeleteNoticesByIDs(t *testing.T) {
105-
assert.NoError(t, db.PrepareTestDatabase())
106+
assert.NoError(t, unittest.PrepareTestDatabase())
106107

107108
db.AssertExistsAndLoadBean(t, &Notice{ID: 1})
108109
db.AssertExistsAndLoadBean(t, &Notice{ID: 2})

0 commit comments

Comments
 (0)