Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple unit test code from business code #17623

Merged
merged 11 commits into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions contrib/fixtures/fixture_generation.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"path/filepath"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
)

// To generate derivative fixtures, execute the following from Gitea's repository base dir:
Expand All @@ -31,13 +31,13 @@ var (
func main() {
pathToGiteaRoot := "."
fixturesDir = filepath.Join(pathToGiteaRoot, "models", "fixtures")
if err := db.CreateTestEngine(db.FixturesOptions{
if err := unittest.CreateTestEngine(unittest.FixturesOptions{
Dir: fixturesDir,
}); err != nil {
fmt.Printf("CreateTestEngine: %+v", err)
os.Exit(1)
}
if err := db.PrepareTestDatabase(); err != nil {
if err := unittest.PrepareTestDatabase(); err != nil {
fmt.Printf("PrepareTestDatabase: %+v\n", err)
os.Exit(1)
}
Expand Down
7 changes: 4 additions & 3 deletions contrib/pr/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
gitea_git "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/external"
Expand Down Expand Up @@ -99,16 +100,16 @@ func runPR() {
})
db.HasEngine = true
//x.ShowSQL(true)
err = db.InitFixtures(
db.FixturesOptions{
err = unittest.InitFixtures(
unittest.FixturesOptions{
Dir: path.Join(curDir, "models/fixtures/"),
},
)
if err != nil {
fmt.Printf("Error initializing test database: %v\n", err)
os.Exit(1)
}
db.LoadFixtures()
unittest.LoadFixtures()
util.RemoveAll(setting.RepoRootPath)
util.RemoveAll(models.LocalCopyPath())
util.CopyDir(path.Join(curDir, "integrations/gitea-repositories-meta"), setting.RepoRootPath)
Expand Down
10 changes: 6 additions & 4 deletions integrations/api_issue_label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"testing"

"code.gitea.io/gitea/models/unittest"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
api "code.gitea.io/gitea/modules/structs"
Expand All @@ -18,7 +20,7 @@ import (
)

func TestAPIModifyLabels(t *testing.T) {
assert.NoError(t, db.LoadFixtures())
assert.NoError(t, unittest.LoadFixtures())

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

func TestAPIAddIssueLabels(t *testing.T) {
assert.NoError(t, db.LoadFixtures())
assert.NoError(t, unittest.LoadFixtures())

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

func TestAPIReplaceIssueLabels(t *testing.T) {
assert.NoError(t, db.LoadFixtures())
assert.NoError(t, unittest.LoadFixtures())

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

func TestAPIModifyOrgLabels(t *testing.T) {
assert.NoError(t, db.LoadFixtures())
assert.NoError(t, unittest.LoadFixtures())

repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository)
owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
Expand Down
11 changes: 6 additions & 5 deletions integrations/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"time"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/graceful"
Expand Down Expand Up @@ -84,6 +84,7 @@ func NewNilResponseHashSumRecorder() *NilResponseHashSumRecorder {
func TestMain(m *testing.M) {
defer log.Close()

unittest.InitUnitTestBridge()
managerCtx, cancel := context.WithCancel(context.Background())
graceful.InitManager(managerCtx)
defer cancel()
Expand Down Expand Up @@ -112,8 +113,8 @@ func TestMain(m *testing.M) {
}
}

err := db.InitFixtures(
db.FixturesOptions{
err := unittest.InitFixtures(
unittest.FixturesOptions{
Dir: filepath.Join(filepath.Dir(setting.AppPath), "models/fixtures/"),
},
)
Expand Down Expand Up @@ -250,7 +251,7 @@ func prepareTestEnv(t testing.TB, skip ...int) func() {
ourSkip += skip[0]
}
deferFn := PrintCurrentTest(t, ourSkip)
assert.NoError(t, db.LoadFixtures())
assert.NoError(t, unittest.LoadFixtures())
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))

assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
Expand Down Expand Up @@ -527,7 +528,7 @@ func GetCSRF(t testing.TB, session *TestSession, urlStr string) string {
// within a single test this is required
func resetFixtures(t *testing.T) {
assert.NoError(t, queue.GetManager().FlushAll(context.Background(), -1))
assert.NoError(t, db.LoadFixtures())
assert.NoError(t, unittest.LoadFixtures())
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
}
4 changes: 3 additions & 1 deletion integrations/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"testing"

"code.gitea.io/gitea/models/unittest"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/migrations"
Expand All @@ -17,7 +19,7 @@ import (
)

func TestMigrateLocalPath(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())

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

Expand Down
9 changes: 5 additions & 4 deletions integrations/repofiles_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"net/url"
"testing"

"code.gitea.io/gitea/models/unittest"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/repofiles"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/test"
Expand Down Expand Up @@ -67,7 +68,7 @@ func TestDeleteRepoFile(t *testing.T) {

func testDeleteRepoFile(t *testing.T, u *url.URL) {
// setup
db.PrepareTestEnv(t)
unittest.PrepareTestEnv(t)
ctx := test.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1")
test.LoadRepo(t, ctx, 1)
Expand Down Expand Up @@ -106,7 +107,7 @@ func TestDeleteRepoFileWithoutBranchNames(t *testing.T) {

func testDeleteRepoFileWithoutBranchNames(t *testing.T, u *url.URL) {
// setup
db.PrepareTestEnv(t)
unittest.PrepareTestEnv(t)
ctx := test.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1")
test.LoadRepo(t, ctx, 1)
Expand Down Expand Up @@ -136,7 +137,7 @@ func testDeleteRepoFileWithoutBranchNames(t *testing.T, u *url.URL) {

func TestDeleteRepoFileErrors(t *testing.T) {
// setup
db.PrepareTestEnv(t)
unittest.PrepareTestEnv(t)
ctx := test.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1")
test.LoadRepo(t, ctx, 1)
Expand Down
15 changes: 8 additions & 7 deletions models/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"testing"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"github.com/stretchr/testify/assert"
)

func TestAccessLevel(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())

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

func TestHasAccess(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())

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

func TestUser_GetRepositoryAccesses(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())

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

func TestUser_GetAccessibleRepositories(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())

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

func TestRepository_RecalculateAccesses(t *testing.T) {
// test with organization repo
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
assert.NoError(t, repo1.GetOwner())

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

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

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

func TestRepository_RecalculateAccesses3(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
team5 := db.AssertExistsAndLoadBean(t, &Team{ID: 5}).(*Team)
user29 := db.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User)

Expand Down
9 changes: 5 additions & 4 deletions models/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ import (
"testing"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/setting"

"github.com/stretchr/testify/assert"
)

func TestAction_GetRepoPath(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
repo := db.AssertExistsAndLoadBean(t, &Repository{}).(*Repository)
owner := db.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
action := &Action{RepoID: repo.ID}
assert.Equal(t, path.Join(owner.Name, repo.Name), action.GetRepoPath())
}

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

func TestGetFeeds(t *testing.T) {
// test with an individual user
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)

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

func TestGetFeeds2(t *testing.T) {
// test with an organization user
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)

Expand Down
17 changes: 9 additions & 8 deletions models/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"github.com/stretchr/testify/assert"
)

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

func TestCreateNotice(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())

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

func TestCreateRepositoryNotice(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())

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

func TestCountNotices(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
assert.Equal(t, int64(3), CountNotices())
}

func TestNotices(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())

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

func TestDeleteNotice(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())

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

func TestDeleteNotices(t *testing.T) {
// delete a non-empty range
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())

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

func TestDeleteNotices2(t *testing.T) {
// delete an empty range
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())

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

func TestDeleteNoticesByIDs(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())

db.AssertExistsAndLoadBean(t, &Notice{ID: 1})
db.AssertExistsAndLoadBean(t, &Notice{ID: 2})
Expand Down
Loading