diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-09-19 19:49:59 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-19 19:49:59 +0800 |
commit | a4bfef265d9e512830350635a0489c2cdcd6508f (patch) | |
tree | 1e3c2ec94276dfcb2f8ba73a2ac075ba39c4a34a /modules/repofiles | |
parent | 462306e263db5a809dbe2cdf62e99307aeff28de (diff) | |
download | gitea-a4bfef265d9e512830350635a0489c2cdcd6508f.tar.gz gitea-a4bfef265d9e512830350635a0489c2cdcd6508f.zip |
Move db related basic functions to models/db (#17075)
* Move db related basic functions to models/db
* Fix lint
* Fix lint
* Fix test
* Fix lint
* Fix lint
* revert unnecessary change
* Fix test
* Fix wrong replace string
* Use *Context
* Correct committer spelling and fix wrong replaced words
Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'modules/repofiles')
-rw-r--r-- | modules/repofiles/action_test.go | 105 | ||||
-rw-r--r-- | modules/repofiles/blob_test.go | 4 | ||||
-rw-r--r-- | modules/repofiles/content_test.go | 16 | ||||
-rw-r--r-- | modules/repofiles/diff_test.go | 5 | ||||
-rw-r--r-- | modules/repofiles/file_test.go | 4 | ||||
-rw-r--r-- | modules/repofiles/tree_test.go | 4 |
6 files changed, 70 insertions, 68 deletions
diff --git a/modules/repofiles/action_test.go b/modules/repofiles/action_test.go index 97632df68f..c29cfd686d 100644 --- a/modules/repofiles/action_test.go +++ b/modules/repofiles/action_test.go @@ -8,6 +8,7 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" @@ -15,7 +16,7 @@ import ( ) func TestUpdateIssuesCommit(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) pushCommits := []*repository.PushCommit{ { Sha1: "abcdef1", @@ -43,8 +44,8 @@ func TestUpdateIssuesCommit(t *testing.T) { }, } - user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) repo.Owner = user commentBean := &models.Comment{ @@ -55,11 +56,11 @@ func TestUpdateIssuesCommit(t *testing.T) { } issueBean := &models.Issue{RepoID: repo.ID, Index: 4} - models.AssertNotExistsBean(t, commentBean) - models.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 2}, "is_closed=1") + db.AssertNotExistsBean(t, commentBean) + db.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 2}, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch)) - models.AssertExistsAndLoadBean(t, commentBean) - models.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") + db.AssertExistsAndLoadBean(t, commentBean) + db.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") models.CheckConsistencyFor(t, &models.Action{}) // Test that push to a non-default branch closes no issue. @@ -73,7 +74,7 @@ func TestUpdateIssuesCommit(t *testing.T) { Message: "close #1", }, } - repo = models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) + repo = db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) commentBean = &models.Comment{ Type: models.CommentTypeCommitRef, CommitSHA: "abcdef1", @@ -82,11 +83,11 @@ func TestUpdateIssuesCommit(t *testing.T) { } issueBean = &models.Issue{RepoID: repo.ID, Index: 1} - models.AssertNotExistsBean(t, commentBean) - models.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 1}, "is_closed=1") + db.AssertNotExistsBean(t, commentBean) + db.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 1}, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch")) - models.AssertExistsAndLoadBean(t, commentBean) - models.AssertNotExistsBean(t, issueBean, "is_closed=1") + db.AssertExistsAndLoadBean(t, commentBean) + db.AssertNotExistsBean(t, issueBean, "is_closed=1") models.CheckConsistencyFor(t, &models.Action{}) pushCommits = []*repository.PushCommit{ @@ -99,7 +100,7 @@ func TestUpdateIssuesCommit(t *testing.T) { Message: "close " + setting.AppURL + repo.FullName() + "/pulls/1", }, } - repo = models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) + repo = db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) commentBean = &models.Comment{ Type: models.CommentTypeCommitRef, CommitSHA: "abcdef3", @@ -108,16 +109,16 @@ func TestUpdateIssuesCommit(t *testing.T) { } issueBean = &models.Issue{RepoID: repo.ID, Index: 1} - models.AssertNotExistsBean(t, commentBean) - models.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 1}, "is_closed=1") + db.AssertNotExistsBean(t, commentBean) + db.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 1}, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch)) - models.AssertExistsAndLoadBean(t, commentBean) - models.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") + db.AssertExistsAndLoadBean(t, commentBean) + db.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") models.CheckConsistencyFor(t, &models.Action{}) } func TestUpdateIssuesCommit_Colon(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) pushCommits := []*repository.PushCommit{ { Sha1: "abcdef2", @@ -129,21 +130,21 @@ func TestUpdateIssuesCommit_Colon(t *testing.T) { }, } - user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) repo.Owner = user issueBean := &models.Issue{RepoID: repo.ID, Index: 4} - models.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 2}, "is_closed=1") + db.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 2}, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch)) - models.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") + db.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") models.CheckConsistencyFor(t, &models.Action{}) } func TestUpdateIssuesCommit_Issue5957(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) - user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + assert.NoError(t, db.PrepareTestDatabase()) + user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // Test that push to a non-default branch closes an issue. pushCommits := []*repository.PushCommit{ @@ -157,7 +158,7 @@ func TestUpdateIssuesCommit_Issue5957(t *testing.T) { }, } - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) commentBean := &models.Comment{ Type: models.CommentTypeCommitRef, CommitSHA: "abcdef1", @@ -167,17 +168,17 @@ func TestUpdateIssuesCommit_Issue5957(t *testing.T) { issueBean := &models.Issue{RepoID: repo.ID, Index: 2, ID: 7} - models.AssertNotExistsBean(t, commentBean) - models.AssertNotExistsBean(t, issueBean, "is_closed=1") + db.AssertNotExistsBean(t, commentBean) + db.AssertNotExistsBean(t, issueBean, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch")) - models.AssertExistsAndLoadBean(t, commentBean) - models.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") + db.AssertExistsAndLoadBean(t, commentBean) + db.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") models.CheckConsistencyFor(t, &models.Action{}) } func TestUpdateIssuesCommit_AnotherRepo(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) - user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + assert.NoError(t, db.PrepareTestDatabase()) + user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // Test that a push to default branch closes issue in another repo // If the user also has push permissions to that repo @@ -192,7 +193,7 @@ func TestUpdateIssuesCommit_AnotherRepo(t *testing.T) { }, } - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) commentBean := &models.Comment{ Type: models.CommentTypeCommitRef, CommitSHA: "abcdef1", @@ -202,17 +203,17 @@ func TestUpdateIssuesCommit_AnotherRepo(t *testing.T) { issueBean := &models.Issue{RepoID: 1, Index: 1, ID: 1} - models.AssertNotExistsBean(t, commentBean) - models.AssertNotExistsBean(t, issueBean, "is_closed=1") + db.AssertNotExistsBean(t, commentBean) + db.AssertNotExistsBean(t, issueBean, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch)) - models.AssertExistsAndLoadBean(t, commentBean) - models.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") + db.AssertExistsAndLoadBean(t, commentBean) + db.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") models.CheckConsistencyFor(t, &models.Action{}) } func TestUpdateIssuesCommit_AnotherRepo_FullAddress(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) - user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + assert.NoError(t, db.PrepareTestDatabase()) + user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // Test that a push to default branch closes issue in another repo // If the user also has push permissions to that repo @@ -227,7 +228,7 @@ func TestUpdateIssuesCommit_AnotherRepo_FullAddress(t *testing.T) { }, } - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) commentBean := &models.Comment{ Type: models.CommentTypeCommitRef, CommitSHA: "abcdef1", @@ -237,17 +238,17 @@ func TestUpdateIssuesCommit_AnotherRepo_FullAddress(t *testing.T) { issueBean := &models.Issue{RepoID: 1, Index: 1, ID: 1} - models.AssertNotExistsBean(t, commentBean) - models.AssertNotExistsBean(t, issueBean, "is_closed=1") + db.AssertNotExistsBean(t, commentBean) + db.AssertNotExistsBean(t, issueBean, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch)) - models.AssertExistsAndLoadBean(t, commentBean) - models.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") + db.AssertExistsAndLoadBean(t, commentBean) + db.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") models.CheckConsistencyFor(t, &models.Action{}) } func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) - user := models.AssertExistsAndLoadBean(t, &models.User{ID: 10}).(*models.User) + assert.NoError(t, db.PrepareTestDatabase()) + user := db.AssertExistsAndLoadBean(t, &models.User{ID: 10}).(*models.User) // Test that a push with close reference *can not* close issue // If the committer doesn't have push rights in that repo @@ -270,7 +271,7 @@ func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) { }, } - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 6}).(*models.Repository) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 6}).(*models.Repository) commentBean := &models.Comment{ Type: models.CommentTypeCommitRef, CommitSHA: "abcdef3", @@ -286,12 +287,12 @@ func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) { issueBean := &models.Issue{RepoID: 3, Index: 1, ID: 6} - models.AssertNotExistsBean(t, commentBean) - models.AssertNotExistsBean(t, commentBean2) - models.AssertNotExistsBean(t, issueBean, "is_closed=1") + db.AssertNotExistsBean(t, commentBean) + db.AssertNotExistsBean(t, commentBean2) + db.AssertNotExistsBean(t, issueBean, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch)) - models.AssertNotExistsBean(t, commentBean) - models.AssertNotExistsBean(t, commentBean2) - models.AssertNotExistsBean(t, issueBean, "is_closed=1") + db.AssertNotExistsBean(t, commentBean) + db.AssertNotExistsBean(t, commentBean2) + db.AssertNotExistsBean(t, issueBean, "is_closed=1") models.CheckConsistencyFor(t, &models.Action{}) } diff --git a/modules/repofiles/blob_test.go b/modules/repofiles/blob_test.go index 8ba80347bf..5238dd6e74 100644 --- a/modules/repofiles/blob_test.go +++ b/modules/repofiles/blob_test.go @@ -7,7 +7,7 @@ package repofiles import ( "testing" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/test" @@ -15,7 +15,7 @@ import ( ) func TestGetBlobBySHA(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") test.LoadRepo(t, ctx, 1) test.LoadRepoCommit(t, ctx) diff --git a/modules/repofiles/content_test.go b/modules/repofiles/content_test.go index 253d434459..f68460d7ec 100644 --- a/modules/repofiles/content_test.go +++ b/modules/repofiles/content_test.go @@ -8,7 +8,7 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/test" @@ -16,7 +16,7 @@ import ( ) func TestMain(m *testing.M) { - models.MainTest(m, filepath.Join("..", "..")) + db.MainTest(m, filepath.Join("..", "..")) } func getExpectedReadmeContentsResponse() *api.ContentsResponse { @@ -49,7 +49,7 @@ func getExpectedReadmeContentsResponse() *api.ContentsResponse { } func TestGetContents(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) @@ -77,7 +77,7 @@ func TestGetContents(t *testing.T) { } func TestGetContentsOrListForDir(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) @@ -112,7 +112,7 @@ func TestGetContentsOrListForDir(t *testing.T) { } func TestGetContentsOrListForFile(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) @@ -140,7 +140,7 @@ func TestGetContentsOrListForFile(t *testing.T) { } func TestGetContentsErrors(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) @@ -171,7 +171,7 @@ func TestGetContentsErrors(t *testing.T) { } func TestGetContentsOrListErrors(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) @@ -202,7 +202,7 @@ func TestGetContentsOrListErrors(t *testing.T) { } func TestGetContentsOrListOfEmptyRepos(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo15") ctx.SetParams(":id", "15") test.LoadRepo(t, ctx, 15) diff --git a/modules/repofiles/diff_test.go b/modules/repofiles/diff_test.go index da50284150..aeb4f76a45 100644 --- a/modules/repofiles/diff_test.go +++ b/modules/repofiles/diff_test.go @@ -8,6 +8,7 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/services/gitdiff" @@ -15,7 +16,7 @@ import ( ) func TestGetDiffPreview(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) @@ -128,7 +129,7 @@ func TestGetDiffPreview(t *testing.T) { } func TestGetDiffPreviewErrors(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) diff --git a/modules/repofiles/file_test.go b/modules/repofiles/file_test.go index aafe816a16..2974056549 100644 --- a/modules/repofiles/file_test.go +++ b/modules/repofiles/file_test.go @@ -7,7 +7,7 @@ package repofiles import ( "testing" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -81,7 +81,7 @@ func getExpectedFileResponse() *api.FileResponse { } func TestGetFileResponseFromCommit(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) diff --git a/modules/repofiles/tree_test.go b/modules/repofiles/tree_test.go index 274e89e969..9e2efa4216 100644 --- a/modules/repofiles/tree_test.go +++ b/modules/repofiles/tree_test.go @@ -7,7 +7,7 @@ package repofiles import ( "testing" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/test" @@ -15,7 +15,7 @@ import ( ) func TestGetTreeBySHA(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") test.LoadRepo(t, ctx, 1) test.LoadRepoCommit(t, ctx) |