summaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2021-11-12 22:36:47 +0800
committerGitHub <noreply@github.com>2021-11-12 22:36:47 +0800
commitdf64fa486555de6f403a795fd16c2e9e1d59e535 (patch)
treeb899e9b9e5d57409b1bf0e3afbd606b6a3900235 /services
parent7f802631c54d2e91301158380b273b872d62bd80 (diff)
downloadgitea-df64fa486555de6f403a795fd16c2e9e1d59e535.tar.gz
gitea-df64fa486555de6f403a795fd16c2e9e1d59e535.zip
Decouple unit test code from business code (#17623)
Diffstat (limited to 'services')
-rw-r--r--services/archiver/archiver_test.go6
-rw-r--r--services/attachment/attachment_test.go5
-rw-r--r--services/gitdiff/gitdiff_test.go3
-rw-r--r--services/gitdiff/main_test.go4
-rw-r--r--services/issue/assignee_test.go4
-rw-r--r--services/issue/label_test.go5
-rw-r--r--services/issue/main_test.go4
-rw-r--r--services/mailer/mail_test.go3
-rw-r--r--services/mailer/main_test.go4
-rw-r--r--services/pull/check_test.go3
-rw-r--r--services/pull/main_test.go4
-rw-r--r--services/release/release_test.go11
-rw-r--r--services/repository/main_test.go4
-rw-r--r--services/repository/transfer_test.go5
-rw-r--r--services/webhook/main_test.go4
-rw-r--r--services/webhook/webhook_test.go7
-rw-r--r--services/wiki/wiki_test.go17
17 files changed, 51 insertions, 42 deletions
diff --git a/services/archiver/archiver_test.go b/services/archiver/archiver_test.go
index 94b0423d9b..67484fdc78 100644
--- a/services/archiver/archiver_test.go
+++ b/services/archiver/archiver_test.go
@@ -9,14 +9,14 @@ import (
"testing"
"time"
- "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/test"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
- db.MainTest(m, filepath.Join("..", ".."))
+ unittest.MainTest(m, filepath.Join("..", ".."))
}
func waitForCount(t *testing.T, num int) {
@@ -24,7 +24,7 @@ func waitForCount(t *testing.T, num int) {
}
func TestArchive_Basic(t *testing.T) {
- assert.NoError(t, db.PrepareTestDatabase())
+ assert.NoError(t, unittest.PrepareTestDatabase())
ctx := test.MockContext(t, "user27/repo49")
firstCommit, secondCommit := "51f84af23134", "aacbdfe9e1c4"
diff --git a/services/attachment/attachment_test.go b/services/attachment/attachment_test.go
index 3e9e55a8f2..f44c4f9b5e 100644
--- a/services/attachment/attachment_test.go
+++ b/services/attachment/attachment_test.go
@@ -11,16 +11,17 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/unittest"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
- db.MainTest(m, filepath.Join("..", ".."))
+ unittest.MainTest(m, filepath.Join("..", ".."))
}
func TestUploadAttachment(t *testing.T) {
- assert.NoError(t, db.PrepareTestDatabase())
+ assert.NoError(t, unittest.PrepareTestDatabase())
user := db.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
diff --git a/services/gitdiff/gitdiff_test.go b/services/gitdiff/gitdiff_test.go
index 6decb59b64..359e2e8c66 100644
--- a/services/gitdiff/gitdiff_test.go
+++ b/services/gitdiff/gitdiff_test.go
@@ -14,6 +14,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/highlight"
"code.gitea.io/gitea/modules/json"
@@ -493,7 +494,7 @@ func setupDefaultDiff() *Diff {
}
}
func TestDiff_LoadComments(t *testing.T) {
- assert.NoError(t, db.PrepareTestDatabase())
+ assert.NoError(t, unittest.PrepareTestDatabase())
issue := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue)
user := db.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
diff --git a/services/gitdiff/main_test.go b/services/gitdiff/main_test.go
index 1b83cbd684..8c76e7e153 100644
--- a/services/gitdiff/main_test.go
+++ b/services/gitdiff/main_test.go
@@ -8,9 +8,9 @@ import (
"path/filepath"
"testing"
- "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/unittest"
)
func TestMain(m *testing.M) {
- db.MainTest(m, filepath.Join("..", ".."))
+ unittest.MainTest(m, filepath.Join("..", ".."))
}
diff --git a/services/issue/assignee_test.go b/services/issue/assignee_test.go
index 5684ed6d89..b0bbe42273 100644
--- a/services/issue/assignee_test.go
+++ b/services/issue/assignee_test.go
@@ -8,12 +8,12 @@ import (
"testing"
"code.gitea.io/gitea/models"
- "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/unittest"
"github.com/stretchr/testify/assert"
)
func TestDeleteNotPassedAssignee(t *testing.T) {
- assert.NoError(t, db.PrepareTestDatabase())
+ assert.NoError(t, unittest.PrepareTestDatabase())
// Fake issue with assignees
issue, err := models.GetIssueWithAttrsByID(1)
diff --git a/services/issue/label_test.go b/services/issue/label_test.go
index 8a3a77ecb0..758ef98b27 100644
--- a/services/issue/label_test.go
+++ b/services/issue/label_test.go
@@ -9,6 +9,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/unittest"
"github.com/stretchr/testify/assert"
)
@@ -24,7 +25,7 @@ func TestIssue_AddLabels(t *testing.T) {
{2, []int64{}, 1}, // pull-request, empty
}
for _, test := range tests {
- assert.NoError(t, db.PrepareTestDatabase())
+ assert.NoError(t, unittest.PrepareTestDatabase())
issue := db.AssertExistsAndLoadBean(t, &models.Issue{ID: test.issueID}).(*models.Issue)
labels := make([]*models.Label, len(test.labelIDs))
for i, labelID := range test.labelIDs {
@@ -50,7 +51,7 @@ func TestIssue_AddLabel(t *testing.T) {
{2, 1, 2}, // pull-request, already-added label
}
for _, test := range tests {
- assert.NoError(t, db.PrepareTestDatabase())
+ assert.NoError(t, unittest.PrepareTestDatabase())
issue := db.AssertExistsAndLoadBean(t, &models.Issue{ID: test.issueID}).(*models.Issue)
label := db.AssertExistsAndLoadBean(t, &models.Label{ID: test.labelID}).(*models.Label)
doer := db.AssertExistsAndLoadBean(t, &models.User{ID: test.doerID}).(*models.User)
diff --git a/services/issue/main_test.go b/services/issue/main_test.go
index 1349837949..cbcfd08a49 100644
--- a/services/issue/main_test.go
+++ b/services/issue/main_test.go
@@ -8,9 +8,9 @@ import (
"path/filepath"
"testing"
- "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/unittest"
)
func TestMain(m *testing.M) {
- db.MainTest(m, filepath.Join("..", ".."))
+ unittest.MainTest(m, filepath.Join("..", ".."))
}
diff --git a/services/mailer/mail_test.go b/services/mailer/mail_test.go
index cd730a13a4..aaa18681b8 100644
--- a/services/mailer/mail_test.go
+++ b/services/mailer/mail_test.go
@@ -12,6 +12,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/setting"
"github.com/stretchr/testify/assert"
@@ -41,7 +42,7 @@ const bodyTpl = `
`
func prepareMailerTest(t *testing.T) (doer *models.User, repo *models.Repository, issue *models.Issue, comment *models.Comment) {
- assert.NoError(t, db.PrepareTestDatabase())
+ assert.NoError(t, unittest.PrepareTestDatabase())
var mailService = setting.Mailer{
From: "test@gitea.com",
}
diff --git a/services/mailer/main_test.go b/services/mailer/main_test.go
index 2fbe9c54a9..ae3b2c12b4 100644
--- a/services/mailer/main_test.go
+++ b/services/mailer/main_test.go
@@ -8,9 +8,9 @@ import (
"path/filepath"
"testing"
- "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/unittest"
)
func TestMain(m *testing.M) {
- db.MainTest(m, filepath.Join("..", ".."))
+ unittest.MainTest(m, filepath.Join("..", ".."))
}
diff --git a/services/pull/check_test.go b/services/pull/check_test.go
index 8beea3d56d..bc3df0a4bb 100644
--- a/services/pull/check_test.go
+++ b/services/pull/check_test.go
@@ -12,13 +12,14 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/queue"
"github.com/stretchr/testify/assert"
)
func TestPullRequest_AddToTaskQueue(t *testing.T) {
- assert.NoError(t, db.PrepareTestDatabase())
+ assert.NoError(t, unittest.PrepareTestDatabase())
idChan := make(chan int64, 10)
diff --git a/services/pull/main_test.go b/services/pull/main_test.go
index c8d3394e8e..6059a291ad 100644
--- a/services/pull/main_test.go
+++ b/services/pull/main_test.go
@@ -9,9 +9,9 @@ import (
"path/filepath"
"testing"
- "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/unittest"
)
func TestMain(m *testing.M) {
- db.MainTest(m, filepath.Join("..", ".."))
+ unittest.MainTest(m, filepath.Join("..", ".."))
}
diff --git a/services/release/release_test.go b/services/release/release_test.go
index e53e4c935b..b855ec6647 100644
--- a/services/release/release_test.go
+++ b/services/release/release_test.go
@@ -12,6 +12,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/services/attachment"
@@ -19,11 +20,11 @@ import (
)
func TestMain(m *testing.M) {
- db.MainTest(m, filepath.Join("..", ".."))
+ unittest.MainTest(m, filepath.Join("..", ".."))
}
func TestRelease_Create(t *testing.T) {
- assert.NoError(t, db.PrepareTestDatabase())
+ assert.NoError(t, unittest.PrepareTestDatabase())
user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
@@ -127,7 +128,7 @@ func TestRelease_Create(t *testing.T) {
}
func TestRelease_Update(t *testing.T) {
- assert.NoError(t, db.PrepareTestDatabase())
+ assert.NoError(t, unittest.PrepareTestDatabase())
user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
@@ -269,7 +270,7 @@ func TestRelease_Update(t *testing.T) {
}
func TestRelease_createTag(t *testing.T) {
- assert.NoError(t, db.PrepareTestDatabase())
+ assert.NoError(t, unittest.PrepareTestDatabase())
user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
@@ -352,7 +353,7 @@ func TestRelease_createTag(t *testing.T) {
}
func TestCreateNewTag(t *testing.T) {
- assert.NoError(t, db.PrepareTestDatabase())
+ assert.NoError(t, unittest.PrepareTestDatabase())
user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
diff --git a/services/repository/main_test.go b/services/repository/main_test.go
index 91d0d36ca0..262d339481 100644
--- a/services/repository/main_test.go
+++ b/services/repository/main_test.go
@@ -8,9 +8,9 @@ import (
"path/filepath"
"testing"
- "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/unittest"
)
func TestMain(m *testing.M) {
- db.MainTest(m, filepath.Join("..", ".."))
+ unittest.MainTest(m, filepath.Join("..", ".."))
}
diff --git a/services/repository/transfer_test.go b/services/repository/transfer_test.go
index 40ccfdfb52..9ce60f30ee 100644
--- a/services/repository/transfer_test.go
+++ b/services/repository/transfer_test.go
@@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/notification/action"
"code.gitea.io/gitea/modules/util"
@@ -28,7 +29,7 @@ func registerNotifier() {
func TestTransferOwnership(t *testing.T) {
registerNotifier()
- assert.NoError(t, db.PrepareTestDatabase())
+ assert.NoError(t, unittest.PrepareTestDatabase())
doer := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository)
@@ -55,7 +56,7 @@ func TestTransferOwnership(t *testing.T) {
}
func TestStartRepositoryTransferSetPermission(t *testing.T) {
- assert.NoError(t, db.PrepareTestDatabase())
+ assert.NoError(t, unittest.PrepareTestDatabase())
doer := db.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User)
recipient := db.AssertExistsAndLoadBean(t, &models.User{ID: 5}).(*models.User)
diff --git a/services/webhook/main_test.go b/services/webhook/main_test.go
index 7aef4b3a51..fc58f72565 100644
--- a/services/webhook/main_test.go
+++ b/services/webhook/main_test.go
@@ -8,9 +8,9 @@ import (
"path/filepath"
"testing"
- "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/unittest"
)
func TestMain(m *testing.M) {
- db.MainTest(m, filepath.Join("..", ".."))
+ unittest.MainTest(m, filepath.Join("..", ".."))
}
diff --git a/services/webhook/webhook_test.go b/services/webhook/webhook_test.go
index 75f19e50ce..276d1e78cd 100644
--- a/services/webhook/webhook_test.go
+++ b/services/webhook/webhook_test.go
@@ -9,6 +9,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/unittest"
webhook_model "code.gitea.io/gitea/models/webhook"
api "code.gitea.io/gitea/modules/structs"
"github.com/stretchr/testify/assert"
@@ -27,7 +28,7 @@ func TestWebhook_GetSlackHook(t *testing.T) {
}
func TestPrepareWebhooks(t *testing.T) {
- assert.NoError(t, db.PrepareTestDatabase())
+ assert.NoError(t, unittest.PrepareTestDatabase())
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
hookTasks := []*webhook_model.HookTask{
@@ -43,7 +44,7 @@ func TestPrepareWebhooks(t *testing.T) {
}
func TestPrepareWebhooksBranchFilterMatch(t *testing.T) {
- assert.NoError(t, db.PrepareTestDatabase())
+ assert.NoError(t, unittest.PrepareTestDatabase())
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository)
hookTasks := []*webhook_model.HookTask{
@@ -60,7 +61,7 @@ func TestPrepareWebhooksBranchFilterMatch(t *testing.T) {
}
func TestPrepareWebhooksBranchFilterNoMatch(t *testing.T) {
- assert.NoError(t, db.PrepareTestDatabase())
+ assert.NoError(t, unittest.PrepareTestDatabase())
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository)
hookTasks := []*webhook_model.HookTask{
diff --git a/services/wiki/wiki_test.go b/services/wiki/wiki_test.go
index d6a65cc23a..7ecd3bb04e 100644
--- a/services/wiki/wiki_test.go
+++ b/services/wiki/wiki_test.go
@@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/util"
@@ -18,7 +19,7 @@ import (
)
func TestMain(m *testing.M) {
- db.MainTest(m, filepath.Join("..", ".."))
+ unittest.MainTest(m, filepath.Join("..", ".."))
}
func TestWikiNameToSubURL(t *testing.T) {
@@ -110,7 +111,7 @@ func TestWikiNameToFilenameToName(t *testing.T) {
}
func TestRepository_InitWiki(t *testing.T) {
- db.PrepareTestEnv(t)
+ unittest.PrepareTestEnv(t)
// repo1 already has a wiki
repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
assert.NoError(t, InitWiki(repo1))
@@ -122,7 +123,7 @@ func TestRepository_InitWiki(t *testing.T) {
}
func TestRepository_AddWikiPage(t *testing.T) {
- assert.NoError(t, db.PrepareTestDatabase())
+ assert.NoError(t, unittest.PrepareTestDatabase())
const wikiContent = "This is the wiki content"
const commitMsg = "Commit message"
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
@@ -166,7 +167,7 @@ func TestRepository_AddWikiPage(t *testing.T) {
}
func TestRepository_EditWikiPage(t *testing.T) {
- assert.NoError(t, db.PrepareTestDatabase())
+ assert.NoError(t, unittest.PrepareTestDatabase())
const newWikiContent = "This is the new content"
const commitMsg = "Commit message"
@@ -177,7 +178,7 @@ func TestRepository_EditWikiPage(t *testing.T) {
"New home",
"New/name/with/slashes",
} {
- db.PrepareTestEnv(t)
+ unittest.PrepareTestEnv(t)
assert.NoError(t, EditWikiPage(doer, repo, "Home", newWikiName, newWikiContent, commitMsg))
// Now need to show that the page has been added:
@@ -199,7 +200,7 @@ func TestRepository_EditWikiPage(t *testing.T) {
}
func TestRepository_DeleteWikiPage(t *testing.T) {
- db.PrepareTestEnv(t)
+ unittest.PrepareTestEnv(t)
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
doer := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
assert.NoError(t, DeleteWikiPage(doer, repo, "Home"))
@@ -216,7 +217,7 @@ func TestRepository_DeleteWikiPage(t *testing.T) {
}
func TestPrepareWikiFileName(t *testing.T) {
- db.PrepareTestEnv(t)
+ unittest.PrepareTestEnv(t)
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
gitRepo, err := git.OpenRepository(repo.WikiPath())
defer gitRepo.Close()
@@ -267,7 +268,7 @@ func TestPrepareWikiFileName(t *testing.T) {
}
func TestPrepareWikiFileName_FirstPage(t *testing.T) {
- db.PrepareTestEnv(t)
+ unittest.PrepareTestEnv(t)
// Now create a temporaryDirectory
tmpDir, err := os.MkdirTemp("", "empty-wiki")