From 81926d61db3dac223a75ea49eab893b25a089587 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 16 Nov 2021 16:53:21 +0800 Subject: [PATCH] Decouple unit test, remove intermediate `unittestbridge` package (#17662) Co-authored-by: Lunny Xiao --- integrations/admin_user_test.go | 6 +- integrations/api_admin_org_test.go | 4 +- integrations/api_admin_test.go | 16 +- integrations/api_comment_test.go | 70 +++---- integrations/api_issue_label_test.go | 37 ++-- integrations/api_issue_milestone_test.go | 8 +- integrations/api_issue_reaction_test.go | 16 +- integrations/api_issue_stopwatch_test.go | 28 +-- integrations/api_issue_subscription_test.go | 20 +- integrations/api_issue_test.go | 28 +-- integrations/api_issue_tracked_time_test.go | 20 +- integrations/api_keys_test.go | 18 +- integrations/api_notification_test.go | 10 +- integrations/api_oauth2_apps_test.go | 30 +-- integrations/api_org_test.go | 4 +- integrations/api_pull_commits_test.go | 6 +- integrations/api_pull_review_test.go | 20 +- integrations/api_pull_test.go | 42 ++-- integrations/api_releases_test.go | 30 +-- integrations/api_repo_edit_test.go | 30 +-- integrations/api_repo_file_create_test.go | 22 +- integrations/api_repo_file_delete_test.go | 14 +- integrations/api_repo_file_update_test.go | 14 +- .../api_repo_get_contents_list_test.go | 16 +- integrations/api_repo_get_contents_test.go | 14 +- integrations/api_repo_git_blobs_test.go | 14 +- integrations/api_repo_git_commits_test.go | 12 +- integrations/api_repo_git_hook_test.go | 38 ++-- integrations/api_repo_git_notes_test.go | 4 +- integrations/api_repo_git_ref_test.go | 4 +- integrations/api_repo_git_tags_test.go | 10 +- integrations/api_repo_git_trees_test.go | 14 +- integrations/api_repo_lfs_locks_test.go | 18 +- integrations/api_repo_lfs_migrate_test.go | 4 +- integrations/api_repo_lfs_test.go | 10 +- integrations/api_repo_raw_test.go | 4 +- integrations/api_repo_tags_test.go | 4 +- integrations/api_repo_teams_test.go | 8 +- integrations/api_repo_test.go | 54 ++--- integrations/api_repo_topic_test.go | 12 +- integrations/api_team_test.go | 28 +-- integrations/api_team_user_test.go | 4 +- integrations/api_token_test.go | 14 +- integrations/api_user_orgs_test.go | 6 +- integrations/api_user_search_test.go | 4 +- integrations/benchmarks_test.go | 4 +- integrations/change_default_branch_test.go | 6 +- integrations/delete_user_test.go | 24 +-- integrations/empty_repo_test.go | 6 +- integrations/eventsource_test.go | 8 +- integrations/git_test.go | 16 +- integrations/gpg_git_test.go | 4 +- integrations/integration_test.go | 1 - integrations/issue_test.go | 32 +-- integrations/migrate_test.go | 3 +- integrations/mirror_pull_test.go | 6 +- integrations/mirror_push_test.go | 6 +- integrations/org_count_test.go | 4 +- integrations/privateactivity_test.go | 6 +- integrations/pull_merge_test.go | 14 +- integrations/pull_update_test.go | 12 +- integrations/release_test.go | 8 +- integrations/rename_branch_test.go | 4 +- integrations/repo_fork_test.go | 4 +- integrations/repo_generate_test.go | 4 +- integrations/repo_tag_test.go | 6 +- integrations/repo_watch_test.go | 6 +- integrations/signin_test.go | 6 +- integrations/signup_test.go | 4 +- integrations/user_avatar_test.go | 6 +- integrations/user_test.go | 10 +- integrations/xss_test.go | 4 +- models/access_test.go | 40 ++-- models/action_test.go | 15 +- models/admin_test.go | 49 +++-- models/attachment_test.go | 2 +- models/branches_test.go | 39 ++-- models/commit_status_test.go | 2 +- models/consistency.go | 166 --------------- models/db/engine.go | 9 + models/db/unit_tests.go | 125 ------------ models/gpg_key_test.go | 3 +- models/issue_assignees_test.go | 5 +- models/issue_comment_test.go | 21 +- models/issue_dependency_test.go | 3 +- models/issue_label_test.go | 96 ++++----- models/issue_list_test.go | 13 +- models/issue_milestone_test.go | 74 +++---- models/issue_reaction_test.go | 58 +++--- models/issue_stopwatch_test.go | 11 +- models/issue_test.go | 40 ++-- models/issue_tracked_time_test.go | 5 +- models/issue_user_test.go | 20 +- models/issue_watch_test.go | 4 +- models/issue_xref_test.go | 40 ++-- models/login/oauth2_application_test.go | 29 ++- models/login/u2f_test.go | 15 +- models/main_test.go | 2 +- models/notification_test.go | 39 ++-- models/org_team_test.go | 133 ++++++------ models/org_test.go | 184 ++++++++--------- models/pull_test.go | 30 +-- models/repo_collaboration_test.go | 30 +-- models/repo_list_test.go | 2 +- models/repo_permission_test.go | 42 ++-- models/repo_redirect_test.go | 18 +- models/repo_test.go | 16 +- models/repo_transfer_test.go | 9 +- models/repo_watch_test.go | 46 ++--- models/review_test.go | 55 +++-- models/star_test.go | 18 +- models/token_test.go | 9 +- models/unittest/bridge.go | 54 ----- models/unittest/consistency.go | 190 ++++++++++++++++++ models/unittest/reflection.go | 41 ++++ models/unittest/testdb.go | 5 +- models/unittest/unit_tests.go | 139 +++++++++++++ models/user_follow_test.go | 15 +- models/user_heatmap_test.go | 5 +- models/user_test.go | 46 ++--- models/webhook/webhook_test.go | 80 ++++---- models/wiki_test.go | 9 +- modules/convert/git_commit_test.go | 3 +- modules/convert/issue_test.go | 5 +- modules/convert/pull_test.go | 7 +- modules/convert/user_test.go | 7 +- modules/migrations/gitea_uploader_test.go | 4 +- modules/migrations/migrate_test.go | 5 +- modules/notification/action/action_test.go | 11 +- modules/repofiles/action_test.go | 109 +++++----- modules/repository/commits_test.go | 3 +- modules/repository/create_test.go | 3 +- modules/repository/fork_test.go | 5 +- modules/test/context_tests.go | 6 +- modules/unittestbridge/unittestbridge.go | 46 ----- routers/api/v1/repo/hook_test.go | 5 +- routers/api/v1/repo/repo_test.go | 9 +- routers/web/admin/users_test.go | 11 +- routers/web/repo/issue_label_test.go | 21 +- routers/web/repo/release_test.go | 5 +- routers/web/repo/settings_test.go | 5 +- routers/web/user/oauth_test.go | 3 +- services/attachment/attachment_test.go | 3 +- services/gitdiff/gitdiff_test.go | 5 +- services/issue/label_test.go | 17 +- services/mailer/mail_test.go | 13 +- services/pull/check_test.go | 7 +- services/release/release_test.go | 17 +- services/repository/transfer_test.go | 23 +-- services/webhook/webhook_test.go | 19 +- services/wiki/wiki_test.go | 19 +- 151 files changed, 1719 insertions(+), 1781 deletions(-) delete mode 100644 models/db/unit_tests.go delete mode 100644 models/unittest/bridge.go create mode 100644 models/unittest/consistency.go create mode 100644 models/unittest/reflection.go create mode 100644 models/unittest/unit_tests.go delete mode 100644 modules/unittestbridge/unittestbridge.go diff --git a/integrations/admin_user_test.go b/integrations/admin_user_test.go index 3acf34904..f9e9afac3 100644 --- a/integrations/admin_user_test.go +++ b/integrations/admin_user_test.go @@ -10,7 +10,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -61,7 +61,7 @@ func makeRequest(t *testing.T, formData models.User, headerCode int) { }) session.MakeRequest(t, req, headerCode) - user := db.AssertExistsAndLoadBean(t, &models.User{ID: formData.ID}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: formData.ID}).(*models.User) assert.Equal(t, formData.Name, user.Name) assert.Equal(t, formData.LoginName, user.LoginName) assert.Equal(t, formData.Email, user.Email) @@ -79,5 +79,5 @@ func TestAdminDeleteUser(t *testing.T) { session.MakeRequest(t, req, http.StatusOK) assertUserDeleted(t, 8) - models.CheckConsistencyFor(t, &models.User{}) + unittest.CheckConsistencyFor(t, &models.User{}) } diff --git a/integrations/api_admin_org_test.go b/integrations/api_admin_org_test.go index 7b149a38d..a4ef0ed5e 100644 --- a/integrations/api_admin_org_test.go +++ b/integrations/api_admin_org_test.go @@ -11,7 +11,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -43,7 +43,7 @@ func TestAPIAdminOrgCreate(t *testing.T) { assert.Equal(t, org.Location, apiOrg.Location) assert.Equal(t, org.Visibility, apiOrg.Visibility) - db.AssertExistsAndLoadBean(t, &models.User{ + unittest.AssertExistsAndLoadBean(t, &models.User{ Name: org.UserName, LowerName: strings.ToLower(org.UserName), FullName: org.FullName, diff --git a/integrations/api_admin_test.go b/integrations/api_admin_test.go index ae3cc7dcf..86a3784b1 100644 --- a/integrations/api_admin_test.go +++ b/integrations/api_admin_test.go @@ -10,7 +10,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" @@ -21,7 +21,7 @@ func TestAPIAdminCreateAndDeleteSSHKey(t *testing.T) { defer prepareTestEnv(t)() // user1 is an admin user session := loginUser(t, "user1") - keyOwner := db.AssertExistsAndLoadBean(t, &models.User{Name: "user2"}).(*models.User) + keyOwner := unittest.AssertExistsAndLoadBean(t, &models.User{Name: "user2"}).(*models.User) token := getTokenForLoggedInUser(t, session) urlStr := fmt.Sprintf("/api/v1/admin/users/%s/keys?token=%s", keyOwner.Name, token) @@ -33,7 +33,7 @@ func TestAPIAdminCreateAndDeleteSSHKey(t *testing.T) { var newPublicKey api.PublicKey DecodeJSON(t, resp, &newPublicKey) - db.AssertExistsAndLoadBean(t, &models.PublicKey{ + unittest.AssertExistsAndLoadBean(t, &models.PublicKey{ ID: newPublicKey.ID, Name: newPublicKey.Title, Content: newPublicKey.Key, @@ -44,7 +44,7 @@ func TestAPIAdminCreateAndDeleteSSHKey(t *testing.T) { req = NewRequestf(t, "DELETE", "/api/v1/admin/users/%s/keys/%d?token=%s", keyOwner.Name, newPublicKey.ID, token) session.MakeRequest(t, req, http.StatusNoContent) - db.AssertNotExistsBean(t, &models.PublicKey{ID: newPublicKey.ID}) + unittest.AssertNotExistsBean(t, &models.PublicKey{ID: newPublicKey.ID}) } func TestAPIAdminDeleteMissingSSHKey(t *testing.T) { @@ -53,7 +53,7 @@ func TestAPIAdminDeleteMissingSSHKey(t *testing.T) { session := loginUser(t, "user1") token := getTokenForLoggedInUser(t, session) - req := NewRequestf(t, "DELETE", "/api/v1/admin/users/user1/keys/%d?token=%s", db.NonexistentID, token) + req := NewRequestf(t, "DELETE", "/api/v1/admin/users/user1/keys/%d?token=%s", unittest.NonexistentID, token) session.MakeRequest(t, req, http.StatusNotFound) } @@ -128,7 +128,7 @@ func TestAPIListUsers(t *testing.T) { } } assert.True(t, found) - numberOfUsers := db.GetCount(t, &models.User{}, "type = 0") + numberOfUsers := unittest.GetCount(t, &models.User{}, "type = 0") assert.Equal(t, numberOfUsers, len(users)) } @@ -194,7 +194,7 @@ func TestAPIEditUser(t *testing.T) { json.Unmarshal(resp.Body.Bytes(), &errMap) assert.EqualValues(t, "email is not allowed to be empty string", errMap["message"].(string)) - user2 := db.AssertExistsAndLoadBean(t, &models.User{LoginName: "user2"}).(*models.User) + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{LoginName: "user2"}).(*models.User) assert.False(t, user2.IsRestricted) bTrue := true req = NewRequestWithJSON(t, "PATCH", urlStr, api.EditUserOption{ @@ -205,6 +205,6 @@ func TestAPIEditUser(t *testing.T) { Restricted: &bTrue, }) session.MakeRequest(t, req, http.StatusOK) - user2 = db.AssertExistsAndLoadBean(t, &models.User{LoginName: "user2"}).(*models.User) + user2 = unittest.AssertExistsAndLoadBean(t, &models.User{LoginName: "user2"}).(*models.User) assert.True(t, user2.IsRestricted) } diff --git a/integrations/api_comment_test.go b/integrations/api_comment_test.go index a5513af6e..6f8c5a85b 100644 --- a/integrations/api_comment_test.go +++ b/integrations/api_comment_test.go @@ -11,7 +11,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" @@ -21,11 +21,11 @@ import ( func TestAPIListRepoComments(t *testing.T) { defer prepareTestEnv(t)() - comment := db.AssertExistsAndLoadBean(t, &models.Comment{}, - db.Cond("type = ?", models.CommentTypeComment)).(*models.Comment) - issue := db.AssertExistsAndLoadBean(t, &models.Issue{ID: comment.IssueID}).(*models.Issue) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository) - repoOwner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + comment := unittest.AssertExistsAndLoadBean(t, &models.Comment{}, + unittest.Cond("type = ?", models.CommentTypeComment)).(*models.Comment) + issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: comment.IssueID}).(*models.Issue) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository) + repoOwner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, repoOwner.Name) link, _ := url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments", repoOwner.Name, repo.Name)) @@ -37,9 +37,9 @@ func TestAPIListRepoComments(t *testing.T) { assert.Len(t, apiComments, 2) for _, apiComment := range apiComments { c := &models.Comment{ID: apiComment.ID} - db.AssertExistsAndLoadBean(t, c, - db.Cond("type = ?", models.CommentTypeComment)) - db.AssertExistsAndLoadBean(t, &models.Issue{ID: c.IssueID, RepoID: repo.ID}) + unittest.AssertExistsAndLoadBean(t, c, + unittest.Cond("type = ?", models.CommentTypeComment)) + unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: c.IssueID, RepoID: repo.ID}) } //test before and since filters @@ -67,11 +67,11 @@ func TestAPIListRepoComments(t *testing.T) { func TestAPIListIssueComments(t *testing.T) { defer prepareTestEnv(t)() - comment := db.AssertExistsAndLoadBean(t, &models.Comment{}, - db.Cond("type = ?", models.CommentTypeComment)).(*models.Comment) - issue := db.AssertExistsAndLoadBean(t, &models.Issue{ID: comment.IssueID}).(*models.Issue) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository) - repoOwner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + comment := unittest.AssertExistsAndLoadBean(t, &models.Comment{}, + unittest.Cond("type = ?", models.CommentTypeComment)).(*models.Comment) + issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: comment.IssueID}).(*models.Issue) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository) + repoOwner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, repoOwner.Name) req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/%d/comments", @@ -80,8 +80,8 @@ func TestAPIListIssueComments(t *testing.T) { var comments []*api.Comment DecodeJSON(t, resp, &comments) - expectedCount := db.GetCount(t, &models.Comment{IssueID: issue.ID}, - db.Cond("type = ?", models.CommentTypeComment)) + expectedCount := unittest.GetCount(t, &models.Comment{IssueID: issue.ID}, + unittest.Cond("type = ?", models.CommentTypeComment)) assert.EqualValues(t, expectedCount, len(comments)) } @@ -89,9 +89,9 @@ func TestAPICreateComment(t *testing.T) { defer prepareTestEnv(t)() const commentBody = "Comment body" - issue := db.AssertExistsAndLoadBean(t, &models.Issue{}).(*models.Issue) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository) - repoOwner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{}).(*models.Issue) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository) + repoOwner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, repoOwner.Name) token := getTokenForLoggedInUser(t, session) @@ -105,16 +105,16 @@ func TestAPICreateComment(t *testing.T) { var updatedComment api.Comment DecodeJSON(t, resp, &updatedComment) assert.EqualValues(t, commentBody, updatedComment.Body) - db.AssertExistsAndLoadBean(t, &models.Comment{ID: updatedComment.ID, IssueID: issue.ID, Content: commentBody}) + unittest.AssertExistsAndLoadBean(t, &models.Comment{ID: updatedComment.ID, IssueID: issue.ID, Content: commentBody}) } func TestAPIGetComment(t *testing.T) { defer prepareTestEnv(t)() - comment := db.AssertExistsAndLoadBean(t, &models.Comment{ID: 2}).(*models.Comment) + comment := unittest.AssertExistsAndLoadBean(t, &models.Comment{ID: 2}).(*models.Comment) assert.NoError(t, comment.LoadIssue()) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: comment.Issue.RepoID}).(*models.Repository) - repoOwner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: comment.Issue.RepoID}).(*models.Repository) + repoOwner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, repoOwner.Name) token := getTokenForLoggedInUser(t, session) @@ -139,11 +139,11 @@ func TestAPIEditComment(t *testing.T) { defer prepareTestEnv(t)() const newCommentBody = "This is the new comment body" - comment := db.AssertExistsAndLoadBean(t, &models.Comment{}, - db.Cond("type = ?", models.CommentTypeComment)).(*models.Comment) - issue := db.AssertExistsAndLoadBean(t, &models.Issue{ID: comment.IssueID}).(*models.Issue) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository) - repoOwner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + comment := unittest.AssertExistsAndLoadBean(t, &models.Comment{}, + unittest.Cond("type = ?", models.CommentTypeComment)).(*models.Comment) + issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: comment.IssueID}).(*models.Issue) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository) + repoOwner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, repoOwner.Name) token := getTokenForLoggedInUser(t, session) @@ -158,17 +158,17 @@ func TestAPIEditComment(t *testing.T) { DecodeJSON(t, resp, &updatedComment) assert.EqualValues(t, comment.ID, updatedComment.ID) assert.EqualValues(t, newCommentBody, updatedComment.Body) - db.AssertExistsAndLoadBean(t, &models.Comment{ID: comment.ID, IssueID: issue.ID, Content: newCommentBody}) + unittest.AssertExistsAndLoadBean(t, &models.Comment{ID: comment.ID, IssueID: issue.ID, Content: newCommentBody}) } func TestAPIDeleteComment(t *testing.T) { defer prepareTestEnv(t)() - comment := db.AssertExistsAndLoadBean(t, &models.Comment{}, - db.Cond("type = ?", models.CommentTypeComment)).(*models.Comment) - issue := db.AssertExistsAndLoadBean(t, &models.Issue{ID: comment.IssueID}).(*models.Issue) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository) - repoOwner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + comment := unittest.AssertExistsAndLoadBean(t, &models.Comment{}, + unittest.Cond("type = ?", models.CommentTypeComment)).(*models.Comment) + issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: comment.IssueID}).(*models.Issue) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository) + repoOwner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, repoOwner.Name) token := getTokenForLoggedInUser(t, session) @@ -176,5 +176,5 @@ func TestAPIDeleteComment(t *testing.T) { repoOwner.Name, repo.Name, comment.ID, token) session.MakeRequest(t, req, http.StatusNoContent) - db.AssertNotExistsBean(t, &models.Comment{ID: comment.ID}) + unittest.AssertNotExistsBean(t, &models.Comment{ID: comment.ID}) } diff --git a/integrations/api_issue_label_test.go b/integrations/api_issue_label_test.go index f9c297a18..7cb5df812 100644 --- a/integrations/api_issue_label_test.go +++ b/integrations/api_issue_label_test.go @@ -13,7 +13,6 @@ import ( "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -22,8 +21,8 @@ import ( func TestAPIModifyLabels(t *testing.T) { 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) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, owner.Name) token := getTokenForLoggedInUser(t, session) urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/labels?token=%s", owner.Name, repo.Name, token) @@ -37,7 +36,7 @@ func TestAPIModifyLabels(t *testing.T) { resp := session.MakeRequest(t, req, http.StatusCreated) apiLabel := new(api.Label) DecodeJSON(t, resp, &apiLabel) - dbLabel := db.AssertExistsAndLoadBean(t, &models.Label{ID: apiLabel.ID, RepoID: repo.ID}).(*models.Label) + dbLabel := unittest.AssertExistsAndLoadBean(t, &models.Label{ID: apiLabel.ID, RepoID: repo.ID}).(*models.Label) assert.EqualValues(t, dbLabel.Name, apiLabel.Name) assert.EqualValues(t, strings.TrimLeft(dbLabel.Color, "#"), apiLabel.Color) @@ -92,10 +91,10 @@ func TestAPIModifyLabels(t *testing.T) { func TestAPIAddIssueLabels(t *testing.T) { 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) - _ = db.AssertExistsAndLoadBean(t, &models.Label{RepoID: repo.ID, ID: 2}).(*models.Label) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{RepoID: repo.ID}).(*models.Issue) + _ = unittest.AssertExistsAndLoadBean(t, &models.Label{RepoID: repo.ID, ID: 2}).(*models.Label) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, owner.Name) token := getTokenForLoggedInUser(t, session) @@ -107,18 +106,18 @@ func TestAPIAddIssueLabels(t *testing.T) { resp := session.MakeRequest(t, req, http.StatusOK) var apiLabels []*api.Label DecodeJSON(t, resp, &apiLabels) - assert.Len(t, apiLabels, db.GetCount(t, &models.IssueLabel{IssueID: issue.ID})) + assert.Len(t, apiLabels, unittest.GetCount(t, &models.IssueLabel{IssueID: issue.ID})) - db.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: issue.ID, LabelID: 2}) + unittest.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: issue.ID, LabelID: 2}) } func TestAPIReplaceIssueLabels(t *testing.T) { 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) - label := db.AssertExistsAndLoadBean(t, &models.Label{RepoID: repo.ID}).(*models.Label) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{RepoID: repo.ID}).(*models.Issue) + label := unittest.AssertExistsAndLoadBean(t, &models.Label{RepoID: repo.ID}).(*models.Label) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, owner.Name) token := getTokenForLoggedInUser(t, session) @@ -134,15 +133,15 @@ func TestAPIReplaceIssueLabels(t *testing.T) { assert.EqualValues(t, label.ID, apiLabels[0].ID) } - db.AssertCount(t, &models.IssueLabel{IssueID: issue.ID}, 1) - db.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: issue.ID, LabelID: label.ID}) + unittest.AssertCount(t, &models.IssueLabel{IssueID: issue.ID}, 1) + unittest.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: issue.ID, LabelID: label.ID}) } func TestAPIModifyOrgLabels(t *testing.T) { 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) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) user := "user1" session := loginUser(t, user) token := getTokenForLoggedInUser(t, session) @@ -157,7 +156,7 @@ func TestAPIModifyOrgLabels(t *testing.T) { resp := session.MakeRequest(t, req, http.StatusCreated) apiLabel := new(api.Label) DecodeJSON(t, resp, &apiLabel) - dbLabel := db.AssertExistsAndLoadBean(t, &models.Label{ID: apiLabel.ID, OrgID: owner.ID}).(*models.Label) + dbLabel := unittest.AssertExistsAndLoadBean(t, &models.Label{ID: apiLabel.ID, OrgID: owner.ID}).(*models.Label) assert.EqualValues(t, dbLabel.Name, apiLabel.Name) assert.EqualValues(t, strings.TrimLeft(dbLabel.Color, "#"), apiLabel.Color) diff --git a/integrations/api_issue_milestone_test.go b/integrations/api_issue_milestone_test.go index ba4ada121..233d8ef33 100644 --- a/integrations/api_issue_milestone_test.go +++ b/integrations/api_issue_milestone_test.go @@ -10,7 +10,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -19,9 +19,9 @@ import ( func TestAPIIssuesMilestone(t *testing.T) { defer prepareTestEnv(t)() - milestone := db.AssertExistsAndLoadBean(t, &models.Milestone{ID: 1}).(*models.Milestone) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: milestone.RepoID}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + milestone := unittest.AssertExistsAndLoadBean(t, &models.Milestone{ID: 1}).(*models.Milestone) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: milestone.RepoID}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) assert.Equal(t, int64(1), int64(milestone.NumIssues)) assert.Equal(t, structs.StateOpen, milestone.State()) diff --git a/integrations/api_issue_reaction_test.go b/integrations/api_issue_reaction_test.go index d064f4885..4591f8fa8 100644 --- a/integrations/api_issue_reaction_test.go +++ b/integrations/api_issue_reaction_test.go @@ -11,7 +11,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/convert" api "code.gitea.io/gitea/modules/structs" @@ -21,14 +21,14 @@ import ( func TestAPIIssuesReactions(t *testing.T) { defer prepareTestEnv(t)() - issue := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 1}).(*models.Issue) + issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 1}).(*models.Issue) _ = issue.LoadRepo() - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User) session := loginUser(t, owner.Name) token := getTokenForLoggedInUser(t, session) - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/reactions?token=%s", owner.Name, issue.Repo.Name, issue.Index, token) @@ -78,17 +78,17 @@ func TestAPIIssuesReactions(t *testing.T) { func TestAPICommentReactions(t *testing.T) { defer prepareTestEnv(t)() - comment := db.AssertExistsAndLoadBean(t, &models.Comment{ID: 2}).(*models.Comment) + comment := unittest.AssertExistsAndLoadBean(t, &models.Comment{ID: 2}).(*models.Comment) _ = comment.LoadIssue() issue := comment.Issue _ = issue.LoadRepo() - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User) session := loginUser(t, owner.Name) token := getTokenForLoggedInUser(t, session) - user1 := db.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user1 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d/reactions?token=%s", owner.Name, issue.Repo.Name, comment.ID, token) diff --git a/integrations/api_issue_stopwatch_test.go b/integrations/api_issue_stopwatch_test.go index 8ccd79f69..92822ed81 100644 --- a/integrations/api_issue_stopwatch_test.go +++ b/integrations/api_issue_stopwatch_test.go @@ -9,7 +9,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -18,8 +18,8 @@ import ( func TestAPIListStopWatches(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, owner.Name) token := getTokenForLoggedInUser(t, session) @@ -27,8 +27,8 @@ func TestAPIListStopWatches(t *testing.T) { resp := session.MakeRequest(t, req, http.StatusOK) var apiWatches []*api.StopWatch DecodeJSON(t, resp, &apiWatches) - stopwatch := db.AssertExistsAndLoadBean(t, &models.Stopwatch{UserID: owner.ID}).(*models.Stopwatch) - issue := db.AssertExistsAndLoadBean(t, &models.Issue{ID: stopwatch.IssueID}).(*models.Issue) + stopwatch := unittest.AssertExistsAndLoadBean(t, &models.Stopwatch{UserID: owner.ID}).(*models.Stopwatch) + issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: stopwatch.IssueID}).(*models.Issue) if assert.Len(t, apiWatches, 1) { assert.EqualValues(t, stopwatch.CreatedUnix.AsTime().Unix(), apiWatches[0].Created.Unix()) assert.EqualValues(t, issue.Index, apiWatches[0].IssueIndex) @@ -42,10 +42,10 @@ func TestAPIListStopWatches(t *testing.T) { func TestAPIStopStopWatches(t *testing.T) { defer prepareTestEnv(t)() - issue := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue) + issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue) _ = issue.LoadRepo() - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User) - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) @@ -58,10 +58,10 @@ func TestAPIStopStopWatches(t *testing.T) { func TestAPICancelStopWatches(t *testing.T) { defer prepareTestEnv(t)() - issue := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 1}).(*models.Issue) + issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 1}).(*models.Issue) _ = issue.LoadRepo() - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User) - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) @@ -74,10 +74,10 @@ func TestAPICancelStopWatches(t *testing.T) { func TestAPIStartStopWatches(t *testing.T) { defer prepareTestEnv(t)() - issue := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 3}).(*models.Issue) + issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 3}).(*models.Issue) _ = issue.LoadRepo() - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User) - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) diff --git a/integrations/api_issue_subscription_test.go b/integrations/api_issue_subscription_test.go index 30c307661..dd8e43114 100644 --- a/integrations/api_issue_subscription_test.go +++ b/integrations/api_issue_subscription_test.go @@ -10,7 +10,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -19,20 +19,20 @@ import ( func TestAPIIssueSubscriptions(t *testing.T) { defer prepareTestEnv(t)() - issue1 := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 1}).(*models.Issue) - issue2 := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue) - issue3 := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 3}).(*models.Issue) - issue4 := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 4}).(*models.Issue) - issue5 := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 8}).(*models.Issue) + issue1 := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 1}).(*models.Issue) + issue2 := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue) + issue3 := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 3}).(*models.Issue) + issue4 := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 4}).(*models.Issue) + issue5 := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 8}).(*models.Issue) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: issue1.PosterID}).(*models.User) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: issue1.PosterID}).(*models.User) session := loginUser(t, owner.Name) token := getTokenForLoggedInUser(t, session) testSubscription := func(issue *models.Issue, isWatching bool) { - issueRepo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository) + issueRepo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: issue.RepoID}).(*models.Repository) urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/subscriptions/check?token=%s", issueRepo.OwnerName, issueRepo.Name, issue.Index, token) req := NewRequest(t, "GET", urlStr) @@ -53,7 +53,7 @@ func TestAPIIssueSubscriptions(t *testing.T) { testSubscription(issue4, false) testSubscription(issue5, false) - issue1Repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: issue1.RepoID}).(*models.Repository) + issue1Repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: issue1.RepoID}).(*models.Repository) urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/subscriptions/%s?token=%s", issue1Repo.OwnerName, issue1Repo.Name, issue1.Index, owner.Name, token) req := NewRequest(t, "DELETE", urlStr) session.MakeRequest(t, req, http.StatusCreated) @@ -63,7 +63,7 @@ func TestAPIIssueSubscriptions(t *testing.T) { session.MakeRequest(t, req, http.StatusOK) testSubscription(issue1, false) - issue5Repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: issue5.RepoID}).(*models.Repository) + issue5Repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: issue5.RepoID}).(*models.Repository) urlStr = fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/subscriptions/%s?token=%s", issue5Repo.OwnerName, issue5Repo.Name, issue5.Index, owner.Name, token) req = NewRequest(t, "PUT", urlStr) session.MakeRequest(t, req, http.StatusCreated) diff --git a/integrations/api_issue_test.go b/integrations/api_issue_test.go index df258a983..a46cba599 100644 --- a/integrations/api_issue_test.go +++ b/integrations/api_issue_test.go @@ -12,7 +12,7 @@ import ( "time" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -21,8 +21,8 @@ import ( func TestAPIListIssues(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, owner.Name) token := getTokenForLoggedInUser(t, session) @@ -32,9 +32,9 @@ func TestAPIListIssues(t *testing.T) { resp := session.MakeRequest(t, NewRequest(t, "GET", link.String()), http.StatusOK) var apiIssues []*api.Issue DecodeJSON(t, resp, &apiIssues) - assert.Len(t, apiIssues, db.GetCount(t, &models.Issue{RepoID: repo.ID})) + assert.Len(t, apiIssues, unittest.GetCount(t, &models.Issue{RepoID: repo.ID})) for _, apiIssue := range apiIssues { - db.AssertExistsAndLoadBean(t, &models.Issue{ID: apiIssue.ID, RepoID: repo.ID}) + unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: apiIssue.ID, RepoID: repo.ID}) } // test milestone filter @@ -72,8 +72,8 @@ func TestAPICreateIssue(t *testing.T) { defer prepareTestEnv(t)() const body, title = "apiTestBody", "apiTestTitle" - repoBefore := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repoBefore.OwnerID}).(*models.User) + repoBefore := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repoBefore.OwnerID}).(*models.User) session := loginUser(t, owner.Name) token := getTokenForLoggedInUser(t, session) @@ -89,14 +89,14 @@ func TestAPICreateIssue(t *testing.T) { assert.Equal(t, body, apiIssue.Body) assert.Equal(t, title, apiIssue.Title) - db.AssertExistsAndLoadBean(t, &models.Issue{ + unittest.AssertExistsAndLoadBean(t, &models.Issue{ RepoID: repoBefore.ID, AssigneeID: owner.ID, Content: body, Title: title, }) - repoAfter := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + repoAfter := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) assert.Equal(t, repoBefore.NumIssues+1, repoAfter.NumIssues) assert.Equal(t, repoBefore.NumClosedIssues, repoAfter.NumClosedIssues) } @@ -104,9 +104,9 @@ func TestAPICreateIssue(t *testing.T) { func TestAPIEditIssue(t *testing.T) { defer prepareTestEnv(t)() - issueBefore := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 10}).(*models.Issue) - repoBefore := db.AssertExistsAndLoadBean(t, &models.Repository{ID: issueBefore.RepoID}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repoBefore.OwnerID}).(*models.User) + issueBefore := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 10}).(*models.Issue) + repoBefore := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: issueBefore.RepoID}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repoBefore.OwnerID}).(*models.User) assert.NoError(t, issueBefore.LoadAttributes()) assert.Equal(t, int64(1019307200), int64(issueBefore.DeadlineUnix)) assert.Equal(t, api.StateOpen, issueBefore.State()) @@ -135,8 +135,8 @@ func TestAPIEditIssue(t *testing.T) { var apiIssue api.Issue DecodeJSON(t, resp, &apiIssue) - issueAfter := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 10}).(*models.Issue) - repoAfter := db.AssertExistsAndLoadBean(t, &models.Repository{ID: issueBefore.RepoID}).(*models.Repository) + issueAfter := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 10}).(*models.Issue) + repoAfter := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: issueBefore.RepoID}).(*models.Repository) // check deleted user assert.Equal(t, int64(500), issueAfter.PosterID) diff --git a/integrations/api_issue_tracked_time_test.go b/integrations/api_issue_tracked_time_test.go index fcf18e45d..bcaa0213c 100644 --- a/integrations/api_issue_tracked_time_test.go +++ b/integrations/api_issue_tracked_time_test.go @@ -11,7 +11,7 @@ import ( "time" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -20,8 +20,8 @@ import ( func TestAPIGetTrackedTimes(t *testing.T) { defer prepareTestEnv(t)() - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - issue2 := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue) + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + issue2 := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue) assert.NoError(t, issue2.LoadRepo()) session := loginUser(t, user2.Name) @@ -62,10 +62,10 @@ func TestAPIGetTrackedTimes(t *testing.T) { func TestAPIDeleteTrackedTime(t *testing.T) { defer prepareTestEnv(t)() - time6 := db.AssertExistsAndLoadBean(t, &models.TrackedTime{ID: 6}).(*models.TrackedTime) - issue2 := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue) + time6 := unittest.AssertExistsAndLoadBean(t, &models.TrackedTime{ID: 6}).(*models.TrackedTime) + issue2 := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue) assert.NoError(t, issue2.LoadRepo()) - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) session := loginUser(t, user2.Name) token := getTokenForLoggedInUser(t, session) @@ -74,7 +74,7 @@ func TestAPIDeleteTrackedTime(t *testing.T) { req := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/%d/times/%d?token=%s", user2.Name, issue2.Repo.Name, issue2.Index, time6.ID, token) session.MakeRequest(t, req, http.StatusForbidden) - time3 := db.AssertExistsAndLoadBean(t, &models.TrackedTime{ID: 3}).(*models.TrackedTime) + time3 := unittest.AssertExistsAndLoadBean(t, &models.TrackedTime{ID: 3}).(*models.TrackedTime) req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/%d/times/%d?token=%s", user2.Name, issue2.Repo.Name, issue2.Index, time3.ID, token) session.MakeRequest(t, req, http.StatusNoContent) //Delete non existing time @@ -97,10 +97,10 @@ func TestAPIDeleteTrackedTime(t *testing.T) { func TestAPIAddTrackedTimes(t *testing.T) { defer prepareTestEnv(t)() - issue2 := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue) + issue2 := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue) assert.NoError(t, issue2.LoadRepo()) - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - admin := db.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + admin := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) session := loginUser(t, admin.Name) token := getTokenForLoggedInUser(t, session) diff --git a/integrations/api_keys_test.go b/integrations/api_keys_test.go index 161b05e8c..244ece56c 100644 --- a/integrations/api_keys_test.go +++ b/integrations/api_keys_test.go @@ -11,7 +11,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -46,8 +46,8 @@ func TestDeleteDeployKeyNoLogin(t *testing.T) { func TestCreateReadOnlyDeployKey(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{Name: "repo1"}).(*models.Repository) - repoOwner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{Name: "repo1"}).(*models.Repository) + repoOwner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, repoOwner.Name) token := getTokenForLoggedInUser(t, session) @@ -62,7 +62,7 @@ func TestCreateReadOnlyDeployKey(t *testing.T) { var newDeployKey api.DeployKey DecodeJSON(t, resp, &newDeployKey) - db.AssertExistsAndLoadBean(t, &models.DeployKey{ + unittest.AssertExistsAndLoadBean(t, &models.DeployKey{ ID: newDeployKey.ID, Name: rawKeyBody.Title, Content: rawKeyBody.Key, @@ -72,8 +72,8 @@ func TestCreateReadOnlyDeployKey(t *testing.T) { func TestCreateReadWriteDeployKey(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{Name: "repo1"}).(*models.Repository) - repoOwner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{Name: "repo1"}).(*models.Repository) + repoOwner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, repoOwner.Name) token := getTokenForLoggedInUser(t, session) @@ -87,7 +87,7 @@ func TestCreateReadWriteDeployKey(t *testing.T) { var newDeployKey api.DeployKey DecodeJSON(t, resp, &newDeployKey) - db.AssertExistsAndLoadBean(t, &models.DeployKey{ + unittest.AssertExistsAndLoadBean(t, &models.DeployKey{ ID: newDeployKey.ID, Name: rawKeyBody.Title, Content: rawKeyBody.Key, @@ -97,7 +97,7 @@ func TestCreateReadWriteDeployKey(t *testing.T) { func TestCreateUserKey(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{Name: "user1"}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{Name: "user1"}).(*models.User) session := loginUser(t, "user1") token := url.QueryEscape(getTokenForLoggedInUser(t, session)) @@ -113,7 +113,7 @@ func TestCreateUserKey(t *testing.T) { var newPublicKey api.PublicKey DecodeJSON(t, resp, &newPublicKey) - db.AssertExistsAndLoadBean(t, &models.PublicKey{ + unittest.AssertExistsAndLoadBean(t, &models.PublicKey{ ID: newPublicKey.ID, OwnerID: user.ID, Name: rawKeyBody.Title, diff --git a/integrations/api_notification_test.go b/integrations/api_notification_test.go index 3049f1b99..1c62ee5ab 100644 --- a/integrations/api_notification_test.go +++ b/integrations/api_notification_test.go @@ -10,7 +10,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -19,9 +19,9 @@ import ( func TestAPINotification(t *testing.T) { defer prepareTestEnv(t)() - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - thread5 := db.AssertExistsAndLoadBean(t, &models.Notification{ID: 5}).(*models.Notification) + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo1 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + thread5 := unittest.AssertExistsAndLoadBean(t, &models.Notification{ID: 5}).(*models.Notification) assert.NoError(t, thread5.LoadAttributes()) session := loginUser(t, user2.Name) token := getTokenForLoggedInUser(t, session) @@ -112,7 +112,7 @@ func TestAPINotification(t *testing.T) { resp = session.MakeRequest(t, req, http.StatusResetContent) assert.Equal(t, models.NotificationStatusUnread, thread5.Status) - thread5 = db.AssertExistsAndLoadBean(t, &models.Notification{ID: 5}).(*models.Notification) + thread5 = unittest.AssertExistsAndLoadBean(t, &models.Notification{ID: 5}).(*models.Notification) assert.Equal(t, models.NotificationStatusRead, thread5.Status) // -- check notifications -- diff --git a/integrations/api_oauth2_apps_test.go b/integrations/api_oauth2_apps_test.go index 6f0a46249..d84477ace 100644 --- a/integrations/api_oauth2_apps_test.go +++ b/integrations/api_oauth2_apps_test.go @@ -10,8 +10,8 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/login" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -27,7 +27,7 @@ func TestOAuth2Application(t *testing.T) { } func testAPICreateOAuth2Application(t *testing.T) { - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) appBody := api.CreateOAuth2ApplicationOptions{ Name: "test-app-1", RedirectURIs: []string{ @@ -47,15 +47,15 @@ func testAPICreateOAuth2Application(t *testing.T) { assert.Len(t, createdApp.ClientID, 36) assert.NotEmpty(t, createdApp.Created) assert.EqualValues(t, appBody.RedirectURIs[0], createdApp.RedirectURIs[0]) - db.AssertExistsAndLoadBean(t, &login.OAuth2Application{UID: user.ID, Name: createdApp.Name}) + unittest.AssertExistsAndLoadBean(t, &login.OAuth2Application{UID: user.ID, Name: createdApp.Name}) } func testAPIListOAuth2Applications(t *testing.T) { - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) - existApp := db.AssertExistsAndLoadBean(t, &login.OAuth2Application{ + existApp := unittest.AssertExistsAndLoadBean(t, &login.OAuth2Application{ UID: user.ID, Name: "test-app-1", RedirectURIs: []string{ @@ -76,15 +76,15 @@ func testAPIListOAuth2Applications(t *testing.T) { assert.Len(t, expectedApp.ClientID, 36) assert.Empty(t, expectedApp.ClientSecret) assert.EqualValues(t, existApp.RedirectURIs[0], expectedApp.RedirectURIs[0]) - db.AssertExistsAndLoadBean(t, &login.OAuth2Application{ID: expectedApp.ID, Name: expectedApp.Name}) + unittest.AssertExistsAndLoadBean(t, &login.OAuth2Application{ID: expectedApp.ID, Name: expectedApp.Name}) } func testAPIDeleteOAuth2Application(t *testing.T) { - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) - oldApp := db.AssertExistsAndLoadBean(t, &login.OAuth2Application{ + oldApp := unittest.AssertExistsAndLoadBean(t, &login.OAuth2Application{ UID: user.ID, Name: "test-app-1", }).(*login.OAuth2Application) @@ -93,7 +93,7 @@ func testAPIDeleteOAuth2Application(t *testing.T) { req := NewRequest(t, "DELETE", urlStr) session.MakeRequest(t, req, http.StatusNoContent) - db.AssertNotExistsBean(t, &login.OAuth2Application{UID: oldApp.UID, Name: oldApp.Name}) + unittest.AssertNotExistsBean(t, &login.OAuth2Application{UID: oldApp.UID, Name: oldApp.Name}) // Delete again will return not found req = NewRequest(t, "DELETE", urlStr) @@ -101,11 +101,11 @@ func testAPIDeleteOAuth2Application(t *testing.T) { } func testAPIGetOAuth2Application(t *testing.T) { - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) - existApp := db.AssertExistsAndLoadBean(t, &login.OAuth2Application{ + existApp := unittest.AssertExistsAndLoadBean(t, &login.OAuth2Application{ UID: user.ID, Name: "test-app-1", RedirectURIs: []string{ @@ -127,13 +127,13 @@ func testAPIGetOAuth2Application(t *testing.T) { assert.Empty(t, expectedApp.ClientSecret) assert.Len(t, expectedApp.RedirectURIs, 1) assert.EqualValues(t, existApp.RedirectURIs[0], expectedApp.RedirectURIs[0]) - db.AssertExistsAndLoadBean(t, &login.OAuth2Application{ID: expectedApp.ID, Name: expectedApp.Name}) + unittest.AssertExistsAndLoadBean(t, &login.OAuth2Application{ID: expectedApp.ID, Name: expectedApp.Name}) } func testAPIUpdateOAuth2Application(t *testing.T) { - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - existApp := db.AssertExistsAndLoadBean(t, &login.OAuth2Application{ + existApp := unittest.AssertExistsAndLoadBean(t, &login.OAuth2Application{ UID: user.ID, Name: "test-app-1", RedirectURIs: []string{ @@ -161,5 +161,5 @@ func testAPIUpdateOAuth2Application(t *testing.T) { assert.Len(t, expectedApp.RedirectURIs, 2) assert.EqualValues(t, expectedApp.RedirectURIs[0], appBody.RedirectURIs[0]) assert.EqualValues(t, expectedApp.RedirectURIs[1], appBody.RedirectURIs[1]) - db.AssertExistsAndLoadBean(t, &login.OAuth2Application{ID: expectedApp.ID, Name: expectedApp.Name}) + unittest.AssertExistsAndLoadBean(t, &login.OAuth2Application{ID: expectedApp.ID, Name: expectedApp.Name}) } diff --git a/integrations/api_org_test.go b/integrations/api_org_test.go index 6268fa4a1..ced78d7ee 100644 --- a/integrations/api_org_test.go +++ b/integrations/api_org_test.go @@ -11,7 +11,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -44,7 +44,7 @@ func TestAPIOrgCreate(t *testing.T) { assert.Equal(t, org.Location, apiOrg.Location) assert.Equal(t, org.Visibility, apiOrg.Visibility) - db.AssertExistsAndLoadBean(t, &models.User{ + unittest.AssertExistsAndLoadBean(t, &models.User{ Name: org.UserName, LowerName: strings.ToLower(org.UserName), FullName: org.FullName, diff --git a/integrations/api_pull_commits_test.go b/integrations/api_pull_commits_test.go index 615589fd1..c537f0020 100644 --- a/integrations/api_pull_commits_test.go +++ b/integrations/api_pull_commits_test.go @@ -9,16 +9,16 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" ) func TestAPIPullCommits(t *testing.T) { defer prepareTestEnv(t)() - pullIssue := db.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest) + pullIssue := unittest.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest) assert.NoError(t, pullIssue.LoadIssue()) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: pullIssue.HeadRepoID}).(*models.Repository) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: pullIssue.HeadRepoID}).(*models.Repository) session := loginUser(t, "user2") req := NewRequestf(t, http.MethodGet, "/api/v1/repos/%s/%s/pulls/%d/commits", repo.OwnerName, repo.Name, pullIssue.Index) diff --git a/integrations/api_pull_review_test.go b/integrations/api_pull_review_test.go index 123ba0a2b..ace2c89e8 100644 --- a/integrations/api_pull_review_test.go +++ b/integrations/api_pull_review_test.go @@ -10,7 +10,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" @@ -19,9 +19,9 @@ import ( func TestAPIPullReview(t *testing.T) { defer prepareTestEnv(t)() - pullIssue := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 3}).(*models.Issue) + pullIssue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 3}).(*models.Issue) assert.NoError(t, pullIssue.LoadAttributes()) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: pullIssue.RepoID}).(*models.Repository) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: pullIssue.RepoID}).(*models.Repository) // test ListPullReviews session := loginUser(t, "user2") @@ -63,7 +63,7 @@ func TestAPIPullReview(t *testing.T) { assert.EqualValues(t, *reviews[5], review) // test GetPullReviewComments - comment := db.AssertExistsAndLoadBean(t, &models.Comment{ID: 7}).(*models.Comment) + comment := unittest.AssertExistsAndLoadBean(t, &models.Comment{ID: 7}).(*models.Comment) req = NewRequestf(t, http.MethodGet, "/api/v1/repos/%s/%s/pulls/%d/reviews/%d/comments?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, 10, token) resp = session.MakeRequest(t, req, http.StatusOK) var reviewComments []*api.PullReviewComment @@ -196,9 +196,9 @@ func TestAPIPullReview(t *testing.T) { // test get review requests // to make it simple, use same api with get review - pullIssue12 := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 12}).(*models.Issue) + pullIssue12 := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 12}).(*models.Issue) assert.NoError(t, pullIssue12.LoadAttributes()) - repo3 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: pullIssue12.RepoID}).(*models.Repository) + repo3 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: pullIssue12.RepoID}).(*models.Repository) req = NewRequestf(t, http.MethodGet, "/api/v1/repos/%s/%s/pulls/%d/reviews?token=%s", repo3.OwnerName, repo3.Name, pullIssue12.Index, token) resp = session.MakeRequest(t, req, http.StatusOK) @@ -220,9 +220,9 @@ func TestAPIPullReview(t *testing.T) { func TestAPIPullReviewRequest(t *testing.T) { defer prepareTestEnv(t)() - pullIssue := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 3}).(*models.Issue) + pullIssue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 3}).(*models.Issue) assert.NoError(t, pullIssue.LoadAttributes()) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: pullIssue.RepoID}).(*models.Repository) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: pullIssue.RepoID}).(*models.Repository) // Test add Review Request session := loginUser(t, "user2") @@ -265,9 +265,9 @@ func TestAPIPullReviewRequest(t *testing.T) { session.MakeRequest(t, req, http.StatusNoContent) // Test team review request - pullIssue12 := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 12}).(*models.Issue) + pullIssue12 := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 12}).(*models.Issue) assert.NoError(t, pullIssue12.LoadAttributes()) - repo3 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: pullIssue12.RepoID}).(*models.Repository) + repo3 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: pullIssue12.RepoID}).(*models.Repository) // Test add Team Review Request req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers?token=%s", repo3.OwnerName, repo3.Name, pullIssue12.Index, token), &api.PullReviewRequestOptions{ diff --git a/integrations/api_pull_test.go b/integrations/api_pull_test.go index 069650993..060eb16d8 100644 --- a/integrations/api_pull_test.go +++ b/integrations/api_pull_test.go @@ -10,7 +10,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/services/forms" @@ -21,8 +21,8 @@ import ( func TestAPIViewPulls(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, "user2") token := getTokenForLoggedInUser(t, session) @@ -31,16 +31,16 @@ func TestAPIViewPulls(t *testing.T) { var pulls []*api.PullRequest DecodeJSON(t, resp, &pulls) - expectedLen := db.GetCount(t, &models.Issue{RepoID: repo.ID}, db.Cond("is_pull = ?", true)) + expectedLen := unittest.GetCount(t, &models.Issue{RepoID: repo.ID}, unittest.Cond("is_pull = ?", true)) assert.Len(t, pulls, expectedLen) } // TestAPIMergePullWIP ensures that we can't merge a WIP pull request func TestAPIMergePullWIP(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) - pr := db.AssertExistsAndLoadBean(t, &models.PullRequest{Status: models.PullRequestStatusMergeable}, db.Cond("has_merged = ?", false)).(*models.PullRequest) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + pr := unittest.AssertExistsAndLoadBean(t, &models.PullRequest{Status: models.PullRequestStatusMergeable}, unittest.Cond("has_merged = ?", false)).(*models.PullRequest) pr.LoadIssue() issue_service.ChangeTitle(pr.Issue, owner, setting.Repository.PullRequest.WorkInProgressPrefixes[0]+" "+pr.Issue.Title) @@ -61,12 +61,12 @@ func TestAPIMergePullWIP(t *testing.T) { func TestAPICreatePullSuccess(t *testing.T) { defer prepareTestEnv(t)() - repo10 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 10}).(*models.Repository) + repo10 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 10}).(*models.Repository) // repo10 have code, pulls units. - repo11 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 11}).(*models.Repository) + repo11 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 11}).(*models.Repository) // repo11 only have code unit but should still create pulls - owner10 := db.AssertExistsAndLoadBean(t, &models.User{ID: repo10.OwnerID}).(*models.User) - owner11 := db.AssertExistsAndLoadBean(t, &models.User{ID: repo11.OwnerID}).(*models.User) + owner10 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo10.OwnerID}).(*models.User) + owner11 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo11.OwnerID}).(*models.User) session := loginUser(t, owner11.Name) token := getTokenForLoggedInUser(t, session) @@ -82,11 +82,11 @@ func TestAPICreatePullSuccess(t *testing.T) { func TestAPICreatePullWithFieldsSuccess(t *testing.T) { defer prepareTestEnv(t)() // repo10 have code, pulls units. - repo10 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 10}).(*models.Repository) - owner10 := db.AssertExistsAndLoadBean(t, &models.User{ID: repo10.OwnerID}).(*models.User) + repo10 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 10}).(*models.Repository) + owner10 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo10.OwnerID}).(*models.User) // repo11 only have code unit but should still create pulls - repo11 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 11}).(*models.Repository) - owner11 := db.AssertExistsAndLoadBean(t, &models.User{ID: repo11.OwnerID}).(*models.User) + repo11 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 11}).(*models.Repository) + owner11 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo11.OwnerID}).(*models.User) session := loginUser(t, owner11.Name) token := getTokenForLoggedInUser(t, session) @@ -119,11 +119,11 @@ func TestAPICreatePullWithFieldsSuccess(t *testing.T) { func TestAPICreatePullWithFieldsFailure(t *testing.T) { defer prepareTestEnv(t)() // repo10 have code, pulls units. - repo10 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 10}).(*models.Repository) - owner10 := db.AssertExistsAndLoadBean(t, &models.User{ID: repo10.OwnerID}).(*models.User) + repo10 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 10}).(*models.Repository) + owner10 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo10.OwnerID}).(*models.User) // repo11 only have code unit but should still create pulls - repo11 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 11}).(*models.Repository) - owner11 := db.AssertExistsAndLoadBean(t, &models.User{ID: repo11.OwnerID}).(*models.User) + repo11 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 11}).(*models.Repository) + owner11 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo11.OwnerID}).(*models.User) session := loginUser(t, owner11.Name) token := getTokenForLoggedInUser(t, session) @@ -152,8 +152,8 @@ func TestAPICreatePullWithFieldsFailure(t *testing.T) { func TestAPIEditPull(t *testing.T) { defer prepareTestEnv(t)() - repo10 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 10}).(*models.Repository) - owner10 := db.AssertExistsAndLoadBean(t, &models.User{ID: repo10.OwnerID}).(*models.User) + repo10 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 10}).(*models.Repository) + owner10 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo10.OwnerID}).(*models.User) session := loginUser(t, owner10.Name) token := getTokenForLoggedInUser(t, session) diff --git a/integrations/api_releases_test.go b/integrations/api_releases_test.go index cb04c2a8d..ac2beb7bd 100644 --- a/integrations/api_releases_test.go +++ b/integrations/api_releases_test.go @@ -11,7 +11,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" @@ -21,8 +21,8 @@ import ( func TestAPIListReleases(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) session := loginUser(t, user2.LowerName) token := getTokenForLoggedInUser(t, session) @@ -85,7 +85,7 @@ func createNewReleaseUsingAPI(t *testing.T, session *TestSession, token string, var newRelease api.Release DecodeJSON(t, resp, &newRelease) - db.AssertExistsAndLoadBean(t, &models.Release{ + unittest.AssertExistsAndLoadBean(t, &models.Release{ ID: newRelease.ID, TagName: newRelease.TagName, Title: newRelease.Title, @@ -98,8 +98,8 @@ func createNewReleaseUsingAPI(t *testing.T, session *TestSession, token string, func TestAPICreateAndUpdateRelease(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, owner.LowerName) token := getTokenForLoggedInUser(t, session) @@ -138,7 +138,7 @@ func TestAPICreateAndUpdateRelease(t *testing.T) { resp = session.MakeRequest(t, req, http.StatusOK) DecodeJSON(t, resp, &newRelease) - db.AssertExistsAndLoadBean(t, &models.Release{ + unittest.AssertExistsAndLoadBean(t, &models.Release{ ID: newRelease.ID, TagName: newRelease.TagName, Title: newRelease.Title, @@ -149,8 +149,8 @@ func TestAPICreateAndUpdateRelease(t *testing.T) { func TestAPICreateReleaseToDefaultBranch(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, owner.LowerName) token := getTokenForLoggedInUser(t, session) @@ -160,8 +160,8 @@ func TestAPICreateReleaseToDefaultBranch(t *testing.T) { func TestAPICreateReleaseToDefaultBranchOnExistingTag(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, owner.LowerName) token := getTokenForLoggedInUser(t, session) @@ -178,8 +178,8 @@ func TestAPICreateReleaseToDefaultBranchOnExistingTag(t *testing.T) { func TestAPIGetReleaseByTag(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, owner.LowerName) tag := "v1.1" @@ -211,8 +211,8 @@ func TestAPIGetReleaseByTag(t *testing.T) { func TestAPIDeleteReleaseByTagName(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, owner.LowerName) token := getTokenForLoggedInUser(t, session) diff --git a/integrations/api_repo_edit_test.go b/integrations/api_repo_edit_test.go index f8e21a495..404e8e912 100644 --- a/integrations/api_repo_edit_test.go +++ b/integrations/api_repo_edit_test.go @@ -11,8 +11,8 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" unit_model "code.gitea.io/gitea/models/unit" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -134,13 +134,13 @@ func TestAPIRepoEdit(t *testing.T) { onGiteaRun(t, func(t *testing.T, u *url.URL) { bFalse, bTrue := false, true - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16 - user3 := db.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org - user4 := db.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos - repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo - repo3 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo - repo15 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 15}).(*models.Repository) // empty repo - repo16 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16 + user3 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org + user4 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos + repo1 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo + repo3 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo + repo15 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 15}).(*models.Repository) // empty repo + repo16 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo // Get user2's token session := loginUser(t, user2.Name) @@ -166,7 +166,7 @@ func TestAPIRepoEdit(t *testing.T) { assert.Equal(t, *repoEditOption.Website, repo.Website) assert.Equal(t, *repoEditOption.Archived, repo.Archived) // check repo1 from database - repo1edited := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + repo1edited := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) repo1editedOption := getRepoEditOptionFromRepo(repo1edited) assert.Equal(t, *repoEditOption.Name, *repo1editedOption.Name) assert.Equal(t, *repoEditOption.Description, *repo1editedOption.Description) @@ -191,7 +191,7 @@ func TestAPIRepoEdit(t *testing.T) { DecodeJSON(t, resp, &repo) assert.NotNil(t, repo) // check repo1 was written to database - repo1edited = db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + repo1edited = unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) repo1editedOption = getRepoEditOptionFromRepo(repo1edited) assert.Equal(t, *repo1editedOption.HasIssues, true) assert.Nil(t, repo1editedOption.ExternalTracker) @@ -213,7 +213,7 @@ func TestAPIRepoEdit(t *testing.T) { DecodeJSON(t, resp, &repo) assert.NotNil(t, repo) // check repo1 was written to database - repo1edited = db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + repo1edited = unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) repo1editedOption = getRepoEditOptionFromRepo(repo1edited) assert.Equal(t, *repo1editedOption.HasIssues, true) assert.Equal(t, *repo1editedOption.ExternalTracker, *repoEditOption.ExternalTracker) @@ -244,7 +244,7 @@ func TestAPIRepoEdit(t *testing.T) { DecodeJSON(t, resp, &repo) assert.NotNil(t, repo) // check repo1 was written to database - repo1edited = db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + repo1edited = unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) repo1editedOption = getRepoEditOptionFromRepo(repo1edited) assert.Equal(t, *repo1editedOption.Description, *repoEditOption.Description) assert.Equal(t, *repo1editedOption.HasIssues, true) @@ -289,7 +289,7 @@ func TestAPIRepoEdit(t *testing.T) { _ = session.MakeRequest(t, req, http.StatusOK) // Test making a repo public that is private - repo16 = db.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) + repo16 = unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) assert.True(t, repo16.IsPrivate) repoEditOption = &api.EditRepoOption{ Private: &bFalse, @@ -297,7 +297,7 @@ func TestAPIRepoEdit(t *testing.T) { url = fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", user2.Name, repo16.Name, token2) req = NewRequestWithJSON(t, "PATCH", url, &repoEditOption) _ = session.MakeRequest(t, req, http.StatusOK) - repo16 = db.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) + repo16 = unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) assert.False(t, repo16.IsPrivate) // Make it private again repoEditOption.Private = &bTrue @@ -311,7 +311,7 @@ func TestAPIRepoEdit(t *testing.T) { Archived: &bTrue, }) _ = session.MakeRequest(t, req, http.StatusOK) - repo15 = db.AssertExistsAndLoadBean(t, &models.Repository{ID: 15}).(*models.Repository) + repo15 = unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 15}).(*models.Repository) assert.True(t, repo15.IsArchived) req = NewRequestWithJSON(t, "PATCH", url, &api.EditRepoOption{ Archived: &bFalse, diff --git a/integrations/api_repo_file_create_test.go b/integrations/api_repo_file_create_test.go index 5b0e3927f..dd703a99b 100644 --- a/integrations/api_repo_file_create_test.go +++ b/integrations/api_repo_file_create_test.go @@ -14,7 +14,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/context" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/setting" @@ -109,8 +109,8 @@ func getExpectedFileResponseForCreate(commitID, treePath string) *api.FileRespon func BenchmarkAPICreateFileSmall(b *testing.B) { onGiteaRunTB(b, func(t testing.TB, u *url.URL) { b := t.(*testing.B) - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16 - repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16 + repo1 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo for n := 0; n < b.N; n++ { treePath := fmt.Sprintf("update/file%d.txt", n) @@ -124,8 +124,8 @@ func BenchmarkAPICreateFileMedium(b *testing.B) { onGiteaRunTB(b, func(t testing.TB, u *url.URL) { b := t.(*testing.B) - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16 - repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16 + repo1 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo b.ResetTimer() for n := 0; n < b.N; n++ { @@ -138,12 +138,12 @@ func BenchmarkAPICreateFileMedium(b *testing.B) { func TestAPICreateFile(t *testing.T) { onGiteaRun(t, func(t *testing.T, u *url.URL) { - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16 - user3 := db.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org - user4 := db.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos - repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo - repo3 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo - repo16 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16 + user3 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org + user4 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos + repo1 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo + repo3 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo + repo16 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo fileID := 0 // Get user2's token diff --git a/integrations/api_repo_file_delete_test.go b/integrations/api_repo_file_delete_test.go index 69e24d29c..e9877d08c 100644 --- a/integrations/api_repo_file_delete_test.go +++ b/integrations/api_repo_file_delete_test.go @@ -11,7 +11,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -38,12 +38,12 @@ func getDeleteFileOptions() *api.DeleteFileOptions { func TestAPIDeleteFile(t *testing.T) { onGiteaRun(t, func(t *testing.T, u *url.URL) { - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16 - user3 := db.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org - user4 := db.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos - repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo - repo3 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo - repo16 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16 + user3 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org + user4 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos + repo1 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo + repo3 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo + repo16 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo fileID := 0 // Get user2's token diff --git a/integrations/api_repo_file_update_test.go b/integrations/api_repo_file_update_test.go index b6d9be24f..ba1f309f1 100644 --- a/integrations/api_repo_file_update_test.go +++ b/integrations/api_repo_file_update_test.go @@ -13,7 +13,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/setting" @@ -104,12 +104,12 @@ func getExpectedFileResponseForUpdate(commitID, treePath string) *api.FileRespon func TestAPIUpdateFile(t *testing.T) { onGiteaRun(t, func(t *testing.T, u *url.URL) { - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16 - user3 := db.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org - user4 := db.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos - repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo - repo3 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo - repo16 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16 + user3 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org + user4 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos + repo1 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo + repo3 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo + repo16 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo fileID := 0 // Get user2's token diff --git a/integrations/api_repo_get_contents_list_test.go b/integrations/api_repo_get_contents_list_test.go index 625231a8b..c35a345e8 100644 --- a/integrations/api_repo_get_contents_list_test.go +++ b/integrations/api_repo_get_contents_list_test.go @@ -11,7 +11,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" @@ -53,13 +53,13 @@ func TestAPIGetContentsList(t *testing.T) { func testAPIGetContentsList(t *testing.T, u *url.URL) { /*** SETUP ***/ - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16 - user3 := db.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org - user4 := db.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos - repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo - repo3 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo - repo16 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo - treePath := "" // root dir + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16 + user3 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org + user4 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos + repo1 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo + repo3 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo + repo16 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo + treePath := "" // root dir // Get user2's token session := loginUser(t, user2.Name) diff --git a/integrations/api_repo_get_contents_test.go b/integrations/api_repo_get_contents_test.go index e096a718b..243ee229d 100644 --- a/integrations/api_repo_get_contents_test.go +++ b/integrations/api_repo_get_contents_test.go @@ -10,7 +10,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" @@ -54,12 +54,12 @@ func TestAPIGetContents(t *testing.T) { func testAPIGetContents(t *testing.T, u *url.URL) { /*** SETUP ***/ - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16 - user3 := db.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org - user4 := db.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos - repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo - repo3 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo - repo16 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16 + user3 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org + user4 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos + repo1 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo + repo3 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo + repo16 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo treePath := "README.md" // Get user2's token diff --git a/integrations/api_repo_git_blobs_test.go b/integrations/api_repo_git_blobs_test.go index 5bbf13ac4..53c266e9f 100644 --- a/integrations/api_repo_git_blobs_test.go +++ b/integrations/api_repo_git_blobs_test.go @@ -9,7 +9,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -17,12 +17,12 @@ import ( func TestAPIReposGitBlobs(t *testing.T) { defer prepareTestEnv(t)() - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16 - user3 := db.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3 - user4 := db.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos - repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo - repo3 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo - repo16 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16 + user3 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3 + user4 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos + repo1 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo + repo3 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo + repo16 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo repo1ReadmeSHA := "65f1bf27bc3bf70f64657658635e66094edbcb4d" repo3ReadmeSHA := "d56a3073c1dbb7b15963110a049d50cdb5db99fc" repo16ReadmeSHA := "f90451c72ef61a7645293d17b47be7a8e983da57" diff --git a/integrations/api_repo_git_commits_test.go b/integrations/api_repo_git_commits_test.go index 314416d26..627f563da 100644 --- a/integrations/api_repo_git_commits_test.go +++ b/integrations/api_repo_git_commits_test.go @@ -9,7 +9,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -25,7 +25,7 @@ func compareCommitFiles(t *testing.T, expect []string, files []*api.CommitAffect func TestAPIReposGitCommits(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // Login as User2. session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) @@ -53,7 +53,7 @@ func TestAPIReposGitCommits(t *testing.T) { func TestAPIReposGitCommitList(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // Login as User2. session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) @@ -76,7 +76,7 @@ func TestAPIReposGitCommitList(t *testing.T) { func TestAPIReposGitCommitListPage2Empty(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // Login as User2. session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) @@ -93,7 +93,7 @@ func TestAPIReposGitCommitListPage2Empty(t *testing.T) { func TestAPIReposGitCommitListDifferentBranch(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // Login as User2. session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) @@ -112,7 +112,7 @@ func TestAPIReposGitCommitListDifferentBranch(t *testing.T) { func TestDownloadCommitDiffOrPatch(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // Login as User2. session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) diff --git a/integrations/api_repo_git_hook_test.go b/integrations/api_repo_git_hook_test.go index 490251b30..f9cfdf449 100644 --- a/integrations/api_repo_git_hook_test.go +++ b/integrations/api_repo_git_hook_test.go @@ -10,7 +10,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -24,8 +24,8 @@ echo Hello, World! func TestAPIListGitHooks(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 37}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 37}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) // user1 is an admin user session := loginUser(t, "user1") @@ -50,8 +50,8 @@ func TestAPIListGitHooks(t *testing.T) { func TestAPIListGitHooksNoHooks(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) // user1 is an admin user session := loginUser(t, "user1") @@ -71,8 +71,8 @@ func TestAPIListGitHooksNoHooks(t *testing.T) { func TestAPIListGitHooksNoAccess(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, owner.Name) token := getTokenForLoggedInUser(t, session) @@ -84,8 +84,8 @@ func TestAPIListGitHooksNoAccess(t *testing.T) { func TestAPIGetGitHook(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 37}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 37}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) // user1 is an admin user session := loginUser(t, "user1") @@ -102,8 +102,8 @@ func TestAPIGetGitHook(t *testing.T) { func TestAPIGetGitHookNoAccess(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, owner.Name) token := getTokenForLoggedInUser(t, session) @@ -115,8 +115,8 @@ func TestAPIGetGitHookNoAccess(t *testing.T) { func TestAPIEditGitHook(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) // user1 is an admin user session := loginUser(t, "user1") @@ -145,8 +145,8 @@ func TestAPIEditGitHook(t *testing.T) { func TestAPIEditGitHookNoAccess(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, owner.Name) token := getTokenForLoggedInUser(t, session) @@ -161,8 +161,8 @@ func TestAPIEditGitHookNoAccess(t *testing.T) { func TestAPIDeleteGitHook(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 37}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 37}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) // user1 is an admin user session := loginUser(t, "user1") @@ -184,8 +184,8 @@ func TestAPIDeleteGitHook(t *testing.T) { func TestAPIDeleteGitHookNoAccess(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, owner.Name) token := getTokenForLoggedInUser(t, session) diff --git a/integrations/api_repo_git_notes_test.go b/integrations/api_repo_git_notes_test.go index 02235f9d9..21b8a931b 100644 --- a/integrations/api_repo_git_notes_test.go +++ b/integrations/api_repo_git_notes_test.go @@ -10,14 +10,14 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" ) func TestAPIReposGitNotes(t *testing.T) { onGiteaRun(t, func(*testing.T, *url.URL) { - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // Login as User2. session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) diff --git a/integrations/api_repo_git_ref_test.go b/integrations/api_repo_git_ref_test.go index fcf5edc75..9137f03ad 100644 --- a/integrations/api_repo_git_ref_test.go +++ b/integrations/api_repo_git_ref_test.go @@ -9,12 +9,12 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func TestAPIReposGitRefs(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // Login as User2. session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) diff --git a/integrations/api_repo_git_tags_test.go b/integrations/api_repo_git_tags_test.go index 01e0450c6..f091286e2 100644 --- a/integrations/api_repo_git_tags_test.go +++ b/integrations/api_repo_git_tags_test.go @@ -10,7 +10,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" @@ -20,8 +20,8 @@ import ( func TestAPIGitTags(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // Login as User2. session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) @@ -65,8 +65,8 @@ func TestAPIGitTags(t *testing.T) { func TestAPIDeleteTagByName(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, owner.LowerName) token := getTokenForLoggedInUser(t, session) diff --git a/integrations/api_repo_git_trees_test.go b/integrations/api_repo_git_trees_test.go index bf605d4d8..27088156b 100644 --- a/integrations/api_repo_git_trees_test.go +++ b/integrations/api_repo_git_trees_test.go @@ -9,17 +9,17 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func TestAPIReposGitTrees(t *testing.T) { defer prepareTestEnv(t)() - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16 - user3 := db.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3 - user4 := db.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos - repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo - repo3 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo - repo16 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16 + user3 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3 + user4 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos + repo1 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo + repo3 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo + repo16 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo repo1TreeSHA := "65f1bf27bc3bf70f64657658635e66094edbcb4d" repo3TreeSHA := "2a47ca4b614a9f5a43abbd5ad851a54a616ffee6" repo16TreeSHA := "69554a64c1e6030f051e5c3f94bfbd773cd6a324" diff --git a/integrations/api_repo_lfs_locks_test.go b/integrations/api_repo_lfs_locks_test.go index b2d8f0e63..6a6914558 100644 --- a/integrations/api_repo_lfs_locks_test.go +++ b/integrations/api_repo_lfs_locks_test.go @@ -11,7 +11,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/lfs" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -22,8 +22,8 @@ import ( func TestAPILFSLocksNotStarted(t *testing.T) { defer prepareTestEnv(t)() setting.LFS.StartServer = false - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) req := NewRequestf(t, "GET", "/%s/%s.git/info/lfs/locks", user.Name, repo.Name) MakeRequest(t, req, http.StatusNotFound) @@ -38,8 +38,8 @@ func TestAPILFSLocksNotStarted(t *testing.T) { func TestAPILFSLocksNotLogin(t *testing.T) { defer prepareTestEnv(t)() setting.LFS.StartServer = true - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) req := NewRequestf(t, "GET", "/%s/%s.git/info/lfs/locks", user.Name, repo.Name) req.Header.Set("Accept", lfs.MediaType) @@ -52,11 +52,11 @@ func TestAPILFSLocksNotLogin(t *testing.T) { func TestAPILFSLocksLogged(t *testing.T) { defer prepareTestEnv(t)() setting.LFS.StartServer = true - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) //in org 3 - user4 := db.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) //in org 3 + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) //in org 3 + user4 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) //in org 3 - repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - repo3 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // own by org 3 + repo1 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + repo3 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // own by org 3 tests := []struct { user *models.User diff --git a/integrations/api_repo_lfs_migrate_test.go b/integrations/api_repo_lfs_migrate_test.go index 9acc96e4f..47fcb4895 100644 --- a/integrations/api_repo_lfs_migrate_test.go +++ b/integrations/api_repo_lfs_migrate_test.go @@ -10,7 +10,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -26,7 +26,7 @@ func TestAPIRepoLFSMigrateLocal(t *testing.T) { setting.ImportLocalPaths = true setting.Migrations.AllowLocalNetworks = true - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) diff --git a/integrations/api_repo_lfs_test.go b/integrations/api_repo_lfs_test.go index 22c324f97..4addb422c 100644 --- a/integrations/api_repo_lfs_test.go +++ b/integrations/api_repo_lfs_test.go @@ -13,7 +13,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/setting" @@ -26,8 +26,8 @@ func TestAPILFSNotStarted(t *testing.T) { setting.LFS.StartServer = false - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) req := NewRequestf(t, "POST", "/%s/%s.git/info/lfs/objects/batch", user.Name, repo.Name) MakeRequest(t, req, http.StatusNotFound) @@ -46,8 +46,8 @@ func TestAPILFSMediaType(t *testing.T) { setting.LFS.StartServer = true - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) req := NewRequestf(t, "POST", "/%s/%s.git/info/lfs/objects/batch", user.Name, repo.Name) MakeRequest(t, req, http.StatusUnsupportedMediaType) diff --git a/integrations/api_repo_raw_test.go b/integrations/api_repo_raw_test.go index 77b33473c..e7eb60ac5 100644 --- a/integrations/api_repo_raw_test.go +++ b/integrations/api_repo_raw_test.go @@ -9,12 +9,12 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func TestAPIReposRaw(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // Login as User2. session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) diff --git a/integrations/api_repo_tags_test.go b/integrations/api_repo_tags_test.go index 8897bc44b..59eaedcbd 100644 --- a/integrations/api_repo_tags_test.go +++ b/integrations/api_repo_tags_test.go @@ -10,7 +10,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -19,7 +19,7 @@ import ( func TestAPIRepoTags(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // Login as User2. session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) diff --git a/integrations/api_repo_teams_test.go b/integrations/api_repo_teams_test.go index e718d79cb..56836fcee 100644 --- a/integrations/api_repo_teams_test.go +++ b/integrations/api_repo_teams_test.go @@ -10,7 +10,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -20,9 +20,9 @@ func TestAPIRepoTeams(t *testing.T) { defer prepareTestEnv(t)() // publicOrgRepo = user3/repo21 - publicOrgRepo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 32}).(*models.Repository) + publicOrgRepo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 32}).(*models.Repository) // user4 - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) @@ -62,7 +62,7 @@ func TestAPIRepoTeams(t *testing.T) { res = session.MakeRequest(t, req, http.StatusForbidden) // AddTeam with user2 - user = db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user = unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) session = loginUser(t, user.Name) token = getTokenForLoggedInUser(t, session) url = fmt.Sprintf("/api/v1/repos/%s/teams/%s?token=%s", publicOrgRepo.FullName(), "team1", token) diff --git a/integrations/api_repo_test.go b/integrations/api_repo_test.go index b93319be3..ec9f3d9ba 100644 --- a/integrations/api_repo_test.go +++ b/integrations/api_repo_test.go @@ -12,7 +12,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" @@ -22,15 +22,15 @@ import ( func TestAPIUserReposNotLogin(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) req := NewRequestf(t, "GET", "/api/v1/users/%s/repos", user.Name) resp := MakeRequest(t, req, http.StatusOK) var apiRepos []api.Repository DecodeJSON(t, resp, &apiRepos) - expectedLen := db.GetCount(t, models.Repository{OwnerID: user.ID}, - db.Cond("is_private = ?", false)) + expectedLen := unittest.GetCount(t, models.Repository{OwnerID: user.ID}, + unittest.Cond("is_private = ?", false)) assert.Len(t, apiRepos, expectedLen) for _, repo := range apiRepos { assert.EqualValues(t, user.ID, repo.Owner.ID) @@ -53,11 +53,11 @@ func TestAPISearchRepo(t *testing.T) { assert.False(t, repo.Private) } - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 15}).(*models.User) - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 16}).(*models.User) - user3 := db.AssertExistsAndLoadBean(t, &models.User{ID: 18}).(*models.User) - user4 := db.AssertExistsAndLoadBean(t, &models.User{ID: 20}).(*models.User) - orgUser := db.AssertExistsAndLoadBean(t, &models.User{ID: 17}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 15}).(*models.User) + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 16}).(*models.User) + user3 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 18}).(*models.User) + user4 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 20}).(*models.User) + orgUser := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 17}).(*models.User) oldAPIDefaultNum := setting.API.DefaultPagingNum defer func() { @@ -209,7 +209,7 @@ var repoCache = make(map[int64]*models.Repository) func getRepo(t *testing.T, repoID int64) *models.Repository { if _, ok := repoCache[repoID]; !ok { - repoCache[repoID] = db.AssertExistsAndLoadBean(t, &models.Repository{ID: repoID}).(*models.Repository) + repoCache[repoID] = unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: repoID}).(*models.Repository) } return repoCache[repoID] } @@ -246,11 +246,11 @@ func TestAPIViewRepo(t *testing.T) { func TestAPIOrgRepos(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) - user3 := db.AssertExistsAndLoadBean(t, &models.User{ID: 5}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) + user3 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 5}).(*models.User) // User3 is an Org. Check their repos. - sourceOrg := db.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) + sourceOrg := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) expectedResults := map[*models.User]struct { count int @@ -292,7 +292,7 @@ func TestAPIOrgRepos(t *testing.T) { func TestAPIGetRepoByIDUnauthorized(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) req := NewRequestf(t, "GET", "/api/v1/repositories/2?token="+token) @@ -316,7 +316,7 @@ func TestAPIRepoMigrate(t *testing.T) { defer prepareTestEnv(t)() for _, testCase := range testCases { - user := db.AssertExistsAndLoadBean(t, &models.User{ID: testCase.ctxUserID}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: testCase.ctxUserID}).(*models.User) session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) req := NewRequestWithJSON(t, "POST", "/api/v1/repos/migrate?token="+token, &api.MigrateRepoOptions{ @@ -395,7 +395,7 @@ func TestAPIOrgRepoCreate(t *testing.T) { defer prepareTestEnv(t)() for _, testCase := range testCases { - user := db.AssertExistsAndLoadBean(t, &models.User{ID: testCase.ctxUserID}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: testCase.ctxUserID}).(*models.User) session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/org/%s/repos?token="+token, testCase.orgName), &api.CreateRepoOption{ @@ -463,7 +463,7 @@ func TestAPIRepoTransfer(t *testing.T) { defer prepareTestEnv(t)() //create repo to move - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) repoName := "moveME" @@ -480,8 +480,8 @@ func TestAPIRepoTransfer(t *testing.T) { //start testing for _, testCase := range testCases { - user = db.AssertExistsAndLoadBean(t, &models.User{ID: testCase.ctxUserID}).(*models.User) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: apiRepo.ID}).(*models.Repository) + user = unittest.AssertExistsAndLoadBean(t, &models.User{ID: testCase.ctxUserID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: apiRepo.ID}).(*models.Repository) session = loginUser(t, user.Name) token = getTokenForLoggedInUser(t, session) req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/transfer?token=%s", repo.OwnerName, repo.Name, token), &api.TransferRepoOption{ @@ -492,18 +492,18 @@ func TestAPIRepoTransfer(t *testing.T) { } //cleanup - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: apiRepo.ID}).(*models.Repository) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: apiRepo.ID}).(*models.Repository) _ = models.DeleteRepository(user, repo.OwnerID, repo.ID) } func TestAPIGenerateRepo(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) - templateRepo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 44}).(*models.Repository) + templateRepo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 44}).(*models.Repository) // user repo := new(api.Repository) @@ -535,10 +535,10 @@ func TestAPIGenerateRepo(t *testing.T) { func TestAPIRepoGetReviewers(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/reviewers?token=%s", user.Name, repo.Name, token) resp := session.MakeRequest(t, req, http.StatusOK) @@ -549,10 +549,10 @@ func TestAPIRepoGetReviewers(t *testing.T) { func TestAPIRepoGetAssignees(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/assignees?token=%s", user.Name, repo.Name, token) resp := session.MakeRequest(t, req, http.StatusOK) diff --git a/integrations/api_repo_topic_test.go b/integrations/api_repo_topic_test.go index cc76f1885..6035dd3d5 100644 --- a/integrations/api_repo_topic_test.go +++ b/integrations/api_repo_topic_test.go @@ -11,7 +11,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -51,11 +51,11 @@ func TestAPITopicSearch(t *testing.T) { func TestAPIRepoTopic(t *testing.T) { defer prepareTestEnv(t)() - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of repo2 - user3 := db.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of repo3 - user4 := db.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // write access to repo 3 - repo2 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) - repo3 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of repo2 + user3 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of repo3 + user4 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // write access to repo 3 + repo2 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) + repo3 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // Get user2's token session := loginUser(t, user2.Name) diff --git a/integrations/api_team_test.go b/integrations/api_team_test.go index 086450033..141c887e1 100644 --- a/integrations/api_team_test.go +++ b/integrations/api_team_test.go @@ -11,7 +11,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" @@ -21,9 +21,9 @@ import ( func TestAPITeam(t *testing.T) { defer prepareTestEnv(t)() - teamUser := db.AssertExistsAndLoadBean(t, &models.TeamUser{}).(*models.TeamUser) - team := db.AssertExistsAndLoadBean(t, &models.Team{ID: teamUser.TeamID}).(*models.Team) - user := db.AssertExistsAndLoadBean(t, &models.User{ID: teamUser.UID}).(*models.User) + teamUser := unittest.AssertExistsAndLoadBean(t, &models.TeamUser{}).(*models.TeamUser) + team := unittest.AssertExistsAndLoadBean(t, &models.Team{ID: teamUser.TeamID}).(*models.Team) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: teamUser.UID}).(*models.User) session := loginUser(t, user.Name) token := getTokenForLoggedInUser(t, session) @@ -36,8 +36,8 @@ func TestAPITeam(t *testing.T) { assert.Equal(t, team.Name, apiTeam.Name) // non team member user will not access the teams details - teamUser2 := db.AssertExistsAndLoadBean(t, &models.TeamUser{ID: 3}).(*models.TeamUser) - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: teamUser2.UID}).(*models.User) + teamUser2 := unittest.AssertExistsAndLoadBean(t, &models.TeamUser{ID: 3}).(*models.TeamUser) + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: teamUser2.UID}).(*models.User) session = loginUser(t, user2.Name) token = getTokenForLoggedInUser(t, session) @@ -48,11 +48,11 @@ func TestAPITeam(t *testing.T) { _ = session.MakeRequest(t, req, http.StatusUnauthorized) // Get an admin user able to create, update and delete teams. - user = db.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) + user = unittest.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) session = loginUser(t, user.Name) token = getTokenForLoggedInUser(t, session) - org := db.AssertExistsAndLoadBean(t, &models.User{ID: 6}).(*models.User) + org := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 6}).(*models.User) // Create team. teamToCreate := &api.CreateTeamOption{ @@ -102,7 +102,7 @@ func TestAPITeam(t *testing.T) { teamToEdit.Permission, teamToEdit.Units) // Read team. - teamRead := db.AssertExistsAndLoadBean(t, &models.Team{ID: teamID}).(*models.Team) + teamRead := unittest.AssertExistsAndLoadBean(t, &models.Team{ID: teamID}).(*models.Team) req = NewRequestf(t, "GET", "/api/v1/teams/%d?token="+token, teamID) resp = session.MakeRequest(t, req, http.StatusOK) DecodeJSON(t, resp, &apiTeam) @@ -112,7 +112,7 @@ func TestAPITeam(t *testing.T) { // Delete team. req = NewRequestf(t, "DELETE", "/api/v1/teams/%d?token="+token, teamID) session.MakeRequest(t, req, http.StatusNoContent) - db.AssertNotExistsBean(t, &models.Team{ID: teamID}) + unittest.AssertNotExistsBean(t, &models.Team{ID: teamID}) } func checkTeamResponse(t *testing.T, apiTeam *api.Team, name, description string, includesAllRepositories bool, permission string, units []string) { @@ -126,7 +126,7 @@ func checkTeamResponse(t *testing.T, apiTeam *api.Team, name, description string } func checkTeamBean(t *testing.T, id int64, name, description string, includesAllRepositories bool, permission string, units []string) { - team := db.AssertExistsAndLoadBean(t, &models.Team{ID: id}).(*models.Team) + team := unittest.AssertExistsAndLoadBean(t, &models.Team{ID: id}).(*models.Team) assert.NoError(t, team.GetUnits(), "GetUnits") checkTeamResponse(t, convert.ToTeam(team), name, description, includesAllRepositories, permission, units) } @@ -139,8 +139,8 @@ type TeamSearchResults struct { func TestAPITeamSearch(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - org := db.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + org := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) var results TeamSearchResults @@ -155,7 +155,7 @@ func TestAPITeamSearch(t *testing.T) { assert.Equal(t, "test_team", results.Data[0].Name) // no access if not organization member - user5 := db.AssertExistsAndLoadBean(t, &models.User{ID: 5}).(*models.User) + user5 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 5}).(*models.User) session = loginUser(t, user5.Name) csrf = GetCSRF(t, session, "/"+org.Name) req = NewRequestf(t, "GET", "/api/v1/orgs/%s/teams/search?q=%s", org.Name, "team") diff --git a/integrations/api_team_user_test.go b/integrations/api_team_user_test.go index 747227269..7e5bb0802 100644 --- a/integrations/api_team_user_test.go +++ b/integrations/api_team_user_test.go @@ -10,7 +10,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/convert" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -30,7 +30,7 @@ func TestAPITeamUser(t *testing.T) { var user2 *api.User DecodeJSON(t, resp, &user2) user2.Created = user2.Created.In(time.Local) - user := db.AssertExistsAndLoadBean(t, &models.User{Name: "user2"}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{Name: "user2"}).(*models.User) expectedUser := convert.ToUser(user, user) diff --git a/integrations/api_token_test.go b/integrations/api_token_test.go index 1a6d53547..8314d5b63 100644 --- a/integrations/api_token_test.go +++ b/integrations/api_token_test.go @@ -9,14 +9,14 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" ) // TestAPICreateAndDeleteToken tests that token that was just created can be deleted func TestAPICreateAndDeleteToken(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) req := NewRequestWithJSON(t, "POST", "/api/v1/users/user1/tokens", map[string]string{ "name": "test-key-1", @@ -26,7 +26,7 @@ func TestAPICreateAndDeleteToken(t *testing.T) { var newAccessToken api.AccessToken DecodeJSON(t, resp, &newAccessToken) - db.AssertExistsAndLoadBean(t, &models.AccessToken{ + unittest.AssertExistsAndLoadBean(t, &models.AccessToken{ ID: newAccessToken.ID, Name: newAccessToken.Name, Token: newAccessToken.Token, @@ -37,7 +37,7 @@ func TestAPICreateAndDeleteToken(t *testing.T) { req = AddBasicAuthHeader(req, user.Name) MakeRequest(t, req, http.StatusNoContent) - db.AssertNotExistsBean(t, &models.AccessToken{ID: newAccessToken.ID}) + unittest.AssertNotExistsBean(t, &models.AccessToken{ID: newAccessToken.ID}) req = NewRequestWithJSON(t, "POST", "/api/v1/users/user1/tokens", map[string]string{ "name": "test-key-2", @@ -50,15 +50,15 @@ func TestAPICreateAndDeleteToken(t *testing.T) { req = AddBasicAuthHeader(req, user.Name) MakeRequest(t, req, http.StatusNoContent) - db.AssertNotExistsBean(t, &models.AccessToken{ID: newAccessToken.ID}) + unittest.AssertNotExistsBean(t, &models.AccessToken{ID: newAccessToken.ID}) } // TestAPIDeleteMissingToken ensures that error is thrown when token not found func TestAPIDeleteMissingToken(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) - req := NewRequestf(t, "DELETE", "/api/v1/users/user1/tokens/%d", db.NonexistentID) + req := NewRequestf(t, "DELETE", "/api/v1/users/user1/tokens/%d", unittest.NonexistentID) req = AddBasicAuthHeader(req, user.Name) MakeRequest(t, req, http.StatusNotFound) } diff --git a/integrations/api_user_orgs_test.go b/integrations/api_user_orgs_test.go index 36b11335c..d52ac980b 100644 --- a/integrations/api_user_orgs_test.go +++ b/integrations/api_user_orgs_test.go @@ -10,7 +10,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -25,7 +25,7 @@ func TestUserOrgs(t *testing.T) { orgs := getUserOrgs(t, adminUsername, normalUsername) - user3 := db.AssertExistsAndLoadBean(t, &models.User{Name: "user3"}).(*models.User) + user3 := unittest.AssertExistsAndLoadBean(t, &models.User{Name: "user3"}).(*models.User) assert.Equal(t, []*api.Organization{ { @@ -81,7 +81,7 @@ func TestMyOrgs(t *testing.T) { resp = session.MakeRequest(t, req, http.StatusOK) var orgs []*api.Organization DecodeJSON(t, resp, &orgs) - user3 := db.AssertExistsAndLoadBean(t, &models.User{Name: "user3"}).(*models.User) + user3 := unittest.AssertExistsAndLoadBean(t, &models.User{Name: "user3"}).(*models.User) assert.Equal(t, []*api.Organization{ { diff --git a/integrations/api_user_search_test.go b/integrations/api_user_search_test.go index d6144d9e8..67dc2f1ca 100644 --- a/integrations/api_user_search_test.go +++ b/integrations/api_user_search_test.go @@ -10,7 +10,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -52,7 +52,7 @@ func TestAPIUserSearchNotLoggedIn(t *testing.T) { var modelUser *models.User for _, user := range results.Data { assert.Contains(t, user.UserName, query) - modelUser = db.AssertExistsAndLoadBean(t, &models.User{ID: user.ID}).(*models.User) + modelUser = unittest.AssertExistsAndLoadBean(t, &models.User{ID: user.ID}).(*models.User) if modelUser.KeepEmailPrivate { assert.EqualValues(t, fmt.Sprintf("%s@%s", modelUser.LowerName, setting.Service.NoReplyAddress), user.Email) } else { diff --git a/integrations/benchmarks_test.go b/integrations/benchmarks_test.go index 5df0d7b74..627825083 100644 --- a/integrations/benchmarks_test.go +++ b/integrations/benchmarks_test.go @@ -11,7 +11,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" ) @@ -33,7 +33,7 @@ func BenchmarkRepoBranchCommit(b *testing.B) { for _, repoID := range samples { b.StopTimer() - repo := db.AssertExistsAndLoadBean(b, &models.Repository{ID: repoID}).(*models.Repository) + repo := unittest.AssertExistsAndLoadBean(b, &models.Repository{ID: repoID}).(*models.Repository) b.StartTimer() b.Run(repo.Name, func(b *testing.B) { session := loginUser(b, "user2") diff --git a/integrations/change_default_branch_test.go b/integrations/change_default_branch_test.go index d6a666435..d691e48e0 100644 --- a/integrations/change_default_branch_test.go +++ b/integrations/change_default_branch_test.go @@ -10,13 +10,13 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func TestChangeDefaultBranch(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) session := loginUser(t, owner.Name) branchesURL := fmt.Sprintf("/%s/%s/settings/branches", owner.Name, repo.Name) diff --git a/integrations/delete_user_test.go b/integrations/delete_user_test.go index f529734e1..86896c8ae 100644 --- a/integrations/delete_user_test.go +++ b/integrations/delete_user_test.go @@ -10,19 +10,19 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func assertUserDeleted(t *testing.T, userID int64) { - db.AssertNotExistsBean(t, &models.User{ID: userID}) - db.AssertNotExistsBean(t, &models.Follow{UserID: userID}) - db.AssertNotExistsBean(t, &models.Follow{FollowID: userID}) - db.AssertNotExistsBean(t, &models.Repository{OwnerID: userID}) - db.AssertNotExistsBean(t, &models.Access{UserID: userID}) - db.AssertNotExistsBean(t, &models.OrgUser{UID: userID}) - db.AssertNotExistsBean(t, &models.IssueUser{UID: userID}) - db.AssertNotExistsBean(t, &models.TeamUser{UID: userID}) - db.AssertNotExistsBean(t, &models.Star{UID: userID}) + unittest.AssertNotExistsBean(t, &models.User{ID: userID}) + unittest.AssertNotExistsBean(t, &models.Follow{UserID: userID}) + unittest.AssertNotExistsBean(t, &models.Follow{FollowID: userID}) + unittest.AssertNotExistsBean(t, &models.Repository{OwnerID: userID}) + unittest.AssertNotExistsBean(t, &models.Access{UserID: userID}) + unittest.AssertNotExistsBean(t, &models.OrgUser{UID: userID}) + unittest.AssertNotExistsBean(t, &models.IssueUser{UID: userID}) + unittest.AssertNotExistsBean(t, &models.TeamUser{UID: userID}) + unittest.AssertNotExistsBean(t, &models.Star{UID: userID}) } func TestUserDeleteAccount(t *testing.T) { @@ -37,7 +37,7 @@ func TestUserDeleteAccount(t *testing.T) { session.MakeRequest(t, req, http.StatusFound) assertUserDeleted(t, 8) - models.CheckConsistencyFor(t, &models.User{}) + unittest.CheckConsistencyFor(t, &models.User{}) } func TestUserDeleteAccountStillOwnRepos(t *testing.T) { @@ -52,5 +52,5 @@ func TestUserDeleteAccountStillOwnRepos(t *testing.T) { session.MakeRequest(t, req, http.StatusFound) // user should not have been deleted, because the user still owns repos - db.AssertExistsAndLoadBean(t, &models.User{ID: 2}) + unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}) } diff --git a/integrations/empty_repo_test.go b/integrations/empty_repo_test.go index 7346c34b2..cbab67ba1 100644 --- a/integrations/empty_repo_test.go +++ b/integrations/empty_repo_test.go @@ -9,7 +9,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" ) func TestEmptyRepo(t *testing.T) { @@ -20,8 +20,8 @@ func TestEmptyRepo(t *testing.T) { "commit/1ae57b34ccf7e18373", "graph", } - emptyRepo := db.AssertExistsAndLoadBean(t, &models.Repository{}, db.Cond("is_empty = ?", true)).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: emptyRepo.OwnerID}).(*models.User) + emptyRepo := unittest.AssertExistsAndLoadBean(t, &models.Repository{}, unittest.Cond("is_empty = ?", true)).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: emptyRepo.OwnerID}).(*models.User) for _, subpath := range subpaths { req := NewRequestf(t, "GET", "/%s/%s/%s", owner.Name, emptyRepo.Name, subpath) MakeRequest(t, req, http.StatusNotFound) diff --git a/integrations/eventsource_test.go b/integrations/eventsource_test.go index 17c5ff8ab..ead01da8c 100644 --- a/integrations/eventsource_test.go +++ b/integrations/eventsource_test.go @@ -11,7 +11,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/eventsource" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -50,9 +50,9 @@ func TestEventSourceManagerRun(t *testing.T) { } } - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - thread5 := db.AssertExistsAndLoadBean(t, &models.Notification{ID: 5}).(*models.Notification) + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo1 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + thread5 := unittest.AssertExistsAndLoadBean(t, &models.Notification{ID: 5}).(*models.Notification) assert.NoError(t, thread5.LoadAttributes()) session := loginUser(t, user2.Name) token := getTokenForLoggedInUser(t, session) diff --git a/integrations/git_test.go b/integrations/git_test.go index 9a264c944..cf84800ee 100644 --- a/integrations/git_test.go +++ b/integrations/git_test.go @@ -18,7 +18,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/git" "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/setting" @@ -631,7 +631,7 @@ func doCreateAgitFlowPull(dstPath string, ctx *APITestContext, baseBranch, headB return } - pullNum := db.GetCount(t, &models.PullRequest{}) + pullNum := unittest.GetCount(t, &models.PullRequest{}) t.Run("CreateHeadBranch", doGitCreateBranch(dstPath, headBranch)) @@ -667,8 +667,8 @@ func doCreateAgitFlowPull(dstPath string, ctx *APITestContext, baseBranch, headB if !assert.NoError(t, err) { return } - db.AssertCount(t, &models.PullRequest{}, pullNum+1) - pr1 = db.AssertExistsAndLoadBean(t, &models.PullRequest{ + unittest.AssertCount(t, &models.PullRequest{}, pullNum+1) + pr1 = unittest.AssertExistsAndLoadBean(t, &models.PullRequest{ HeadRepoID: repo.ID, Flow: models.PullRequestFlowAGit, }).(*models.PullRequest) @@ -688,8 +688,8 @@ func doCreateAgitFlowPull(dstPath string, ctx *APITestContext, baseBranch, headB if !assert.NoError(t, err) { return } - db.AssertCount(t, &models.PullRequest{}, pullNum+2) - pr2 = db.AssertExistsAndLoadBean(t, &models.PullRequest{ + unittest.AssertCount(t, &models.PullRequest{}, pullNum+2) + pr2 = unittest.AssertExistsAndLoadBean(t, &models.PullRequest{ HeadRepoID: repo.ID, Index: pr1.Index + 1, Flow: models.PullRequestFlowAGit, @@ -741,7 +741,7 @@ func doCreateAgitFlowPull(dstPath string, ctx *APITestContext, baseBranch, headB if !assert.NoError(t, err) { return } - db.AssertCount(t, &models.PullRequest{}, pullNum+2) + unittest.AssertCount(t, &models.PullRequest{}, pullNum+2) prMsg, err := doAPIGetPullRequest(*ctx, ctx.Username, ctx.Reponame, pr1.Index)(t) if !assert.NoError(t, err) { return @@ -753,7 +753,7 @@ func doCreateAgitFlowPull(dstPath string, ctx *APITestContext, baseBranch, headB if !assert.NoError(t, err) { return } - db.AssertCount(t, &models.PullRequest{}, pullNum+2) + unittest.AssertCount(t, &models.PullRequest{}, pullNum+2) prMsg, err = doAPIGetPullRequest(*ctx, ctx.Username, ctx.Reponame, pr2.Index)(t) if !assert.NoError(t, err) { return diff --git a/integrations/gpg_git_test.go b/integrations/gpg_git_test.go index 2cfb883fd..ecc253978 100644 --- a/integrations/gpg_git_test.go +++ b/integrations/gpg_git_test.go @@ -12,7 +12,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -60,7 +60,7 @@ func TestGPGGit(t *testing.T) { setting.Repository.Signing.SigningKey = rootKeyID setting.Repository.Signing.SigningName = "gitea" setting.Repository.Signing.SigningEmail = "gitea@fake.local" - user := db.AssertExistsAndLoadBean(t, &models.User{Name: username}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{Name: username}).(*models.User) setting.Repository.Signing.InitialCommit = []string{"never"} setting.Repository.Signing.CRUDActions = []string{"never"} diff --git a/integrations/integration_test.go b/integrations/integration_test.go index 69c4ca4ae..710e2b3b3 100644 --- a/integrations/integration_test.go +++ b/integrations/integration_test.go @@ -84,7 +84,6 @@ func NewNilResponseHashSumRecorder() *NilResponseHashSumRecorder { func TestMain(m *testing.M) { defer log.Close() - unittest.InitUnitTestBridge() managerCtx, cancel := context.WithCancel(context.Background()) graceful.InitManager(managerCtx) defer cancel() diff --git a/integrations/issue_test.go b/integrations/issue_test.go index 132f0822a..03ca382de 100644 --- a/integrations/issue_test.go +++ b/integrations/issue_test.go @@ -14,7 +14,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/indexer/issues" "code.gitea.io/gitea/modules/references" "code.gitea.io/gitea/modules/setting" @@ -36,7 +36,7 @@ func getIssue(t *testing.T, repoID int64, issueSelection *goquery.Selection) *mo indexStr := href[strings.LastIndexByte(href, '/')+1:] index, err := strconv.Atoi(indexStr) assert.NoError(t, err, "Invalid issue href: %s", href) - return db.AssertExistsAndLoadBean(t, &models.Issue{RepoID: repoID, Index: int64(index)}).(*models.Issue) + return unittest.AssertExistsAndLoadBean(t, &models.Issue{RepoID: repoID, Index: int64(index)}).(*models.Issue) } func assertMatch(t testing.TB, issue *models.Issue, keyword string) { @@ -61,8 +61,8 @@ func TestNoLoginViewIssues(t *testing.T) { func TestViewIssuesSortByType(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) session := loginUser(t, user.Name) req := NewRequest(t, "GET", repo.RelLink()+"/issues?type=created_by") @@ -70,10 +70,10 @@ func TestViewIssuesSortByType(t *testing.T) { htmlDoc := NewHTMLParser(t, resp.Body) issuesSelection := getIssuesSelection(t, htmlDoc) - expectedNumIssues := db.GetCount(t, + expectedNumIssues := unittest.GetCount(t, &models.Issue{RepoID: repo.ID, PosterID: user.ID}, - db.Cond("is_closed=?", false), - db.Cond("is_pull=?", false), + unittest.Cond("is_closed=?", false), + unittest.Cond("is_pull=?", false), ) if expectedNumIssues > setting.UI.IssuePagingNum { expectedNumIssues = setting.UI.IssuePagingNum @@ -89,8 +89,8 @@ func TestViewIssuesSortByType(t *testing.T) { func TestViewIssuesKeyword(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - issue := db.AssertExistsAndLoadBean(t, &models.Issue{ + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ RepoID: repo.ID, Index: 1, }).(*models.Issue) @@ -236,7 +236,7 @@ func TestIssueCrossReference(t *testing.T) { // Ref from issue title issueRefURL, issueRef := testIssueWithBean(t, "user2", 1, fmt.Sprintf("Title ref #%d", issueBase.Index), "Description") - db.AssertExistsAndLoadBean(t, &models.Comment{ + unittest.AssertExistsAndLoadBean(t, &models.Comment{ IssueID: issueBase.ID, RefRepoID: 1, RefIssueID: issueRef.ID, @@ -246,7 +246,7 @@ func TestIssueCrossReference(t *testing.T) { // Edit title, neuter ref testIssueChangeInfo(t, "user2", issueRefURL, "title", "Title no ref") - db.AssertExistsAndLoadBean(t, &models.Comment{ + unittest.AssertExistsAndLoadBean(t, &models.Comment{ IssueID: issueBase.ID, RefRepoID: 1, RefIssueID: issueRef.ID, @@ -256,7 +256,7 @@ func TestIssueCrossReference(t *testing.T) { // Ref from issue content issueRefURL, issueRef = testIssueWithBean(t, "user2", 1, "TitleXRef", fmt.Sprintf("Description ref #%d", issueBase.Index)) - db.AssertExistsAndLoadBean(t, &models.Comment{ + unittest.AssertExistsAndLoadBean(t, &models.Comment{ IssueID: issueBase.ID, RefRepoID: 1, RefIssueID: issueRef.ID, @@ -266,7 +266,7 @@ func TestIssueCrossReference(t *testing.T) { // Edit content, neuter ref testIssueChangeInfo(t, "user2", issueRefURL, "content", "Description no ref") - db.AssertExistsAndLoadBean(t, &models.Comment{ + unittest.AssertExistsAndLoadBean(t, &models.Comment{ IssueID: issueBase.ID, RefRepoID: 1, RefIssueID: issueRef.ID, @@ -284,11 +284,11 @@ func TestIssueCrossReference(t *testing.T) { RefCommentID: commentID, RefIsPull: false, RefAction: references.XRefActionNone} - db.AssertExistsAndLoadBean(t, comment) + unittest.AssertExistsAndLoadBean(t, comment) // Ref from a different repository issueRefURL, issueRef = testIssueWithBean(t, "user12", 10, "TitleXRef", fmt.Sprintf("Description ref user2/repo1#%d", issueBase.Index)) - db.AssertExistsAndLoadBean(t, &models.Comment{ + unittest.AssertExistsAndLoadBean(t, &models.Comment{ IssueID: issueBase.ID, RefRepoID: 10, RefIssueID: issueRef.ID, @@ -304,7 +304,7 @@ func testIssueWithBean(t *testing.T, user string, repoID int64, title, content s index, err := strconv.Atoi(indexStr) assert.NoError(t, err, "Invalid issue href: %s", issueURL) issue := &models.Issue{RepoID: repoID, Index: int64(index)} - db.AssertExistsAndLoadBean(t, issue) + unittest.AssertExistsAndLoadBean(t, issue) return issueURL, issue } diff --git a/integrations/migrate_test.go b/integrations/migrate_test.go index b9fcfb282..80eb75d40 100644 --- a/integrations/migrate_test.go +++ b/integrations/migrate_test.go @@ -11,7 +11,6 @@ import ( "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/migrations" "code.gitea.io/gitea/modules/setting" @@ -21,7 +20,7 @@ import ( func TestMigrateLocalPath(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - adminUser := db.AssertExistsAndLoadBean(t, &models.User{Name: "user1"}).(*models.User) + adminUser := unittest.AssertExistsAndLoadBean(t, &models.User{Name: "user1"}).(*models.User) old := setting.ImportLocalPaths setting.ImportLocalPaths = true diff --git a/integrations/mirror_pull_test.go b/integrations/mirror_pull_test.go index f396aac4b..d0823a6cd 100644 --- a/integrations/mirror_pull_test.go +++ b/integrations/mirror_pull_test.go @@ -9,7 +9,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" migration "code.gitea.io/gitea/modules/migrations/base" "code.gitea.io/gitea/modules/repository" @@ -22,8 +22,8 @@ import ( func TestMirrorPull(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) repoPath := models.RepoPath(user.Name, repo.Name) opts := migration.MigrateOptions{ diff --git a/integrations/mirror_push_test.go b/integrations/mirror_push_test.go index 3d2966cfd..d681b3c40 100644 --- a/integrations/mirror_push_test.go +++ b/integrations/mirror_push_test.go @@ -12,7 +12,7 @@ import ( "testing" "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/repository" "code.gitea.io/gitea/modules/setting" @@ -30,8 +30,8 @@ func testMirrorPush(t *testing.T, u *url.URL) { setting.Migrations.AllowLocalNetworks = true - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - srcRepo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + srcRepo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) mirrorRepo, err := repository.CreateRepository(user, user, models.CreateRepoOptions{ Name: "test-push-mirror", diff --git a/integrations/org_count_test.go b/integrations/org_count_test.go index bd64139b2..c473ecd68 100644 --- a/integrations/org_count_test.go +++ b/integrations/org_count_test.go @@ -10,7 +10,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" ) @@ -111,7 +111,7 @@ func doCheckOrgCounts(username string, orgCounts map[string]int, strict bool, ca } return func(t *testing.T) { - user := db.AssertExistsAndLoadBean(t, &models.User{ + user := unittest.AssertExistsAndLoadBean(t, &models.User{ Name: username, }).(*models.User) diff --git a/integrations/privateactivity_test.go b/integrations/privateactivity_test.go index e7b618abd..e45274ad4 100644 --- a/integrations/privateactivity_test.go +++ b/integrations/privateactivity_test.go @@ -10,7 +10,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -25,8 +25,8 @@ const privateActivityTestOtherUser = "user4" // activity helpers func testPrivateActivityDoSomethingForActionEntries(t *testing.T) { - repoBefore := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repoBefore.OwnerID}).(*models.User) + repoBefore := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repoBefore.OwnerID}).(*models.User) session := loginUser(t, privateActivityTestUser) token := getTokenForLoggedInUser(t, session) diff --git a/integrations/pull_merge_test.go b/integrations/pull_merge_test.go index 9ccc4043a..4d5699fa5 100644 --- a/integrations/pull_merge_test.go +++ b/integrations/pull_merge_test.go @@ -17,7 +17,7 @@ import ( "time" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" @@ -221,15 +221,15 @@ func TestCantMergeConflict(t *testing.T) { session.MakeRequest(t, req, 201) // Now this PR will be marked conflict - or at least a race will do - so drop down to pure code at this point... - user1 := db.AssertExistsAndLoadBean(t, &models.User{ + user1 := unittest.AssertExistsAndLoadBean(t, &models.User{ Name: "user1", }).(*models.User) - repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ + repo1 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ OwnerID: user1.ID, Name: "repo1", }).(*models.Repository) - pr := db.AssertExistsAndLoadBean(t, &models.PullRequest{ + pr := unittest.AssertExistsAndLoadBean(t, &models.PullRequest{ HeadRepoID: repo1.ID, BaseRepoID: repo1.ID, HeadBranch: "conflict", @@ -258,10 +258,10 @@ func TestCantMergeUnrelated(t *testing.T) { // Now we want to create a commit on a branch that is totally unrelated to our current head // Drop down to pure code at this point - user1 := db.AssertExistsAndLoadBean(t, &models.User{ + user1 := unittest.AssertExistsAndLoadBean(t, &models.User{ Name: "user1", }).(*models.User) - repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ + repo1 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ OwnerID: user1.ID, Name: "repo1", }).(*models.Repository) @@ -320,7 +320,7 @@ func TestCantMergeUnrelated(t *testing.T) { // Now this PR could be marked conflict - or at least a race may occur - so drop down to pure code at this point... gitRepo, err := git.OpenRepository(path) assert.NoError(t, err) - pr := db.AssertExistsAndLoadBean(t, &models.PullRequest{ + pr := unittest.AssertExistsAndLoadBean(t, &models.PullRequest{ HeadRepoID: repo1.ID, BaseRepoID: repo1.ID, HeadBranch: "unrelated", diff --git a/integrations/pull_update_test.go b/integrations/pull_update_test.go index ea3a4794a..d9112a4b9 100644 --- a/integrations/pull_update_test.go +++ b/integrations/pull_update_test.go @@ -11,7 +11,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/repofiles" repo_module "code.gitea.io/gitea/modules/repository" pull_service "code.gitea.io/gitea/services/pull" @@ -23,8 +23,8 @@ import ( func TestAPIPullUpdate(t *testing.T) { onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { //Create PR to test - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - org26 := db.AssertExistsAndLoadBean(t, &models.User{ID: 26}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + org26 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 26}).(*models.User) pr := createOutdatedPR(t, user, org26) //Test GetDiverging @@ -51,8 +51,8 @@ func TestAPIPullUpdate(t *testing.T) { func TestAPIPullUpdateByRebase(t *testing.T) { onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { //Create PR to test - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - org26 := db.AssertExistsAndLoadBean(t, &models.User{ID: 26}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + org26 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 26}).(*models.User) pr := createOutdatedPR(t, user, org26) //Test GetDiverging @@ -163,7 +163,7 @@ func createOutdatedPR(t *testing.T, actor, forkOrg *models.User) *models.PullReq err = pull_service.NewPullRequest(baseRepo, pullIssue, nil, nil, pullRequest, nil) assert.NoError(t, err) - issue := db.AssertExistsAndLoadBean(t, &models.Issue{Title: "Test Pull -to-update-"}).(*models.Issue) + issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{Title: "Test Pull -to-update-"}).(*models.Issue) pr, err := models.GetPullRequestByIssueID(issue.ID) assert.NoError(t, err) diff --git a/integrations/release_test.go b/integrations/release_test.go index 2e9de3169..1f15aba09 100644 --- a/integrations/release_test.go +++ b/integrations/release_test.go @@ -11,7 +11,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/setting" "code.gitea.io/gitea/modules/test" @@ -134,7 +134,7 @@ func TestCreateReleasePaging(t *testing.T) { func TestViewReleaseListNoLogin(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) link := repo.Link() + "/releases" @@ -160,7 +160,7 @@ func TestViewReleaseListNoLogin(t *testing.T) { func TestViewReleaseListLogin(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) link := repo.Link() + "/releases" @@ -191,7 +191,7 @@ func TestViewReleaseListLogin(t *testing.T) { func TestViewTagsList(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) link := repo.Link() + "/tags" diff --git a/integrations/rename_branch_test.go b/integrations/rename_branch_test.go index 90c1f4d15..a8138ff0c 100644 --- a/integrations/rename_branch_test.go +++ b/integrations/rename_branch_test.go @@ -9,7 +9,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -39,6 +39,6 @@ func TestRenameBranch(t *testing.T) { assert.Equal(t, "/user2/repo1/src/branch/main/README.md", location) // check db - repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) assert.Equal(t, "main", repo1.DefaultBranch) } diff --git a/integrations/repo_fork_test.go b/integrations/repo_fork_test.go index f9a994fd8..22b2169e2 100644 --- a/integrations/repo_fork_test.go +++ b/integrations/repo_fork_test.go @@ -11,13 +11,13 @@ 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 testRepoFork(t *testing.T, session *TestSession, ownerName, repoName, forkOwnerName, forkRepoName string) *httptest.ResponseRecorder { - forkOwner := db.AssertExistsAndLoadBean(t, &models.User{Name: forkOwnerName}).(*models.User) + forkOwner := unittest.AssertExistsAndLoadBean(t, &models.User{Name: forkOwnerName}).(*models.User) // Step0: check the existence of the to-fork repo req := NewRequestf(t, "GET", "/%s/%s", forkOwnerName, forkRepoName) diff --git a/integrations/repo_generate_test.go b/integrations/repo_generate_test.go index 7afce7474..d1e28c2f0 100644 --- a/integrations/repo_generate_test.go +++ b/integrations/repo_generate_test.go @@ -11,13 +11,13 @@ 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 testRepoGenerate(t *testing.T, session *TestSession, templateOwnerName, templateRepoName, generateOwnerName, generateRepoName string) *httptest.ResponseRecorder { - generateOwner := db.AssertExistsAndLoadBean(t, &models.User{Name: generateOwnerName}).(*models.User) + generateOwner := unittest.AssertExistsAndLoadBean(t, &models.User{Name: generateOwnerName}).(*models.User) // Step0: check the existence of the generated repo req := NewRequestf(t, "GET", "/%s/%s", generateOwnerName, generateRepoName) diff --git a/integrations/repo_tag_test.go b/integrations/repo_tag_test.go index abbc2c02f..7bba055e2 100644 --- a/integrations/repo_tag_test.go +++ b/integrations/repo_tag_test.go @@ -10,7 +10,7 @@ import ( "testing" "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" "code.gitea.io/gitea/services/release" @@ -21,8 +21,8 @@ import ( func TestCreateNewTagProtected(t *testing.T) { defer prepareTestEnv(t)() - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + owner := unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) t.Run("API", func(t *testing.T) { defer PrintCurrentTest(t)() diff --git a/integrations/repo_watch_test.go b/integrations/repo_watch_test.go index 7ae9f1fe7..6ff189975 100644 --- a/integrations/repo_watch_test.go +++ b/integrations/repo_watch_test.go @@ -9,7 +9,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" ) @@ -18,8 +18,8 @@ func TestRepoWatch(t *testing.T) { // Test round-trip auto-watch setting.Service.AutoWatchOnChanges = true session := loginUser(t, "user2") - db.AssertNotExistsBean(t, &models.Watch{UserID: 2, RepoID: 3}) + unittest.AssertNotExistsBean(t, &models.Watch{UserID: 2, RepoID: 3}) testEditFile(t, session, "user3", "repo3", "master", "README.md", "Hello, World (Edited for watch)\n") - db.AssertExistsAndLoadBean(t, &models.Watch{UserID: 2, RepoID: 3, Mode: models.RepoWatchModeAuto}) + unittest.AssertExistsAndLoadBean(t, &models.Watch{UserID: 2, RepoID: 3, Mode: models.RepoWatchModeAuto}) }) } diff --git a/integrations/signin_test.go b/integrations/signin_test.go index 081f87f0b..a920ed45d 100644 --- a/integrations/signin_test.go +++ b/integrations/signin_test.go @@ -10,7 +10,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" "github.com/unknwon/i18n" @@ -34,13 +34,13 @@ func testLoginFailed(t *testing.T, username, password, message string) { func TestSignin(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // add new user with user2's email user.Name = "testuser" user.LowerName = strings.ToLower(user.Name) user.ID = 0 - db.AssertSuccessfulInsert(t, user) + unittest.AssertSuccessfulInsert(t, user) samples := []struct { username string diff --git a/integrations/signup_test.go b/integrations/signup_test.go index dccb1f676..33e5809b2 100644 --- a/integrations/signup_test.go +++ b/integrations/signup_test.go @@ -11,7 +11,7 @@ import ( "testing" "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" "github.com/unknwon/i18n" @@ -53,7 +53,7 @@ func TestSignupAsRestricted(t *testing.T) { req = NewRequest(t, "GET", "/restrictedUser") MakeRequest(t, req, http.StatusOK) - user2 := db.AssertExistsAndLoadBean(t, &models.User{Name: "restrictedUser"}).(*models.User) + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{Name: "restrictedUser"}).(*models.User) assert.True(t, user2.IsRestricted) } diff --git a/integrations/user_avatar_test.go b/integrations/user_avatar_test.go index f07b7c7fc..e53f0e34f 100644 --- a/integrations/user_avatar_test.go +++ b/integrations/user_avatar_test.go @@ -14,14 +14,14 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/avatar" "github.com/stretchr/testify/assert" ) func TestUserAvatar(t *testing.T) { onGiteaRun(t, func(t *testing.T, u *url.URL) { - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo3, is an org + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo3, is an org seed := user2.Email if len(seed) == 0 { @@ -71,7 +71,7 @@ func TestUserAvatar(t *testing.T) { session.MakeRequest(t, req, http.StatusFound) - user2 = db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo3, is an org + user2 = unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo3, is an org req = NewRequest(t, "GET", user2.AvatarLinkWithSize(0)) _ = session.MakeRequest(t, req, http.StatusOK) diff --git a/integrations/user_test.go b/integrations/user_test.go index f9e507afb..b70bf212b 100644 --- a/integrations/user_test.go +++ b/integrations/user_test.go @@ -9,7 +9,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/test" "github.com/stretchr/testify/assert" @@ -35,8 +35,8 @@ func TestRenameUsername(t *testing.T) { }) session.MakeRequest(t, req, http.StatusFound) - db.AssertExistsAndLoadBean(t, &models.User{Name: "newUsername"}) - db.AssertNotExistsBean(t, &models.User{Name: "user2"}) + unittest.AssertExistsAndLoadBean(t, &models.User{Name: "newUsername"}) + unittest.AssertNotExistsBean(t, &models.User{Name: "user2"}) } func TestRenameInvalidUsername(t *testing.T) { @@ -67,7 +67,7 @@ func TestRenameInvalidUsername(t *testing.T) { i18n.Tr("en", "form.alpha_dash_dot_error"), ) - db.AssertNotExistsBean(t, &models.User{Name: invalidUsername}) + unittest.AssertNotExistsBean(t, &models.User{Name: invalidUsername}) } } @@ -113,7 +113,7 @@ func TestRenameReservedUsername(t *testing.T) { i18n.Tr("en", "user.form.name_reserved", reservedUsername), ) - db.AssertNotExistsBean(t, &models.User{Name: reservedUsername}) + unittest.AssertNotExistsBean(t, &models.User{Name: reservedUsername}) } } diff --git a/integrations/xss_test.go b/integrations/xss_test.go index 427c97de2..8128a54aa 100644 --- a/integrations/xss_test.go +++ b/integrations/xss_test.go @@ -9,14 +9,14 @@ 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 TestXSSUserFullName(t *testing.T) { defer prepareTestEnv(t)() - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) const fullName = `name & ` session := loginUser(t, user.Name) diff --git a/models/access_test.go b/models/access_test.go index af799ebcd..96aa34edb 100644 --- a/models/access_test.go +++ b/models/access_test.go @@ -15,21 +15,21 @@ import ( func TestAccessLevel(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - user5 := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) - user29 := db.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User) + user2 := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user5 := unittest.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) + user29 := unittest.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User) // A public repository owned by User 2 - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) assert.False(t, repo1.IsPrivate) // A private repository owned by Org 3 - repo3 := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) + repo3 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) assert.True(t, repo3.IsPrivate) // Another public repository - repo4 := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) + repo4 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) assert.False(t, repo4.IsPrivate) // org. owned private repo - repo24 := db.AssertExistsAndLoadBean(t, &Repository{ID: 24}).(*Repository) + repo24 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 24}).(*Repository) level, err := AccessLevel(user2, repo1) assert.NoError(t, err) @@ -66,13 +66,13 @@ func TestAccessLevel(t *testing.T) { func TestHasAccess(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user1 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - user2 := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) + user1 := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user2 := unittest.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) // A public repository owned by User 2 - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) assert.False(t, repo1.IsPrivate) // A private repository owned by Org 3 - repo2 := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) + repo2 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) assert.True(t, repo2.IsPrivate) has, err := HasAccess(user1.ID, repo1) @@ -92,12 +92,12 @@ func TestHasAccess(t *testing.T) { func TestUser_GetRepositoryAccesses(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user1 := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) accesses, err := user1.GetRepositoryAccesses() assert.NoError(t, err) assert.Len(t, accesses, 0) - user29 := db.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User) + user29 := unittest.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User) accesses, err = user29.GetRepositoryAccesses() assert.NoError(t, err) assert.Len(t, accesses, 2) @@ -106,17 +106,17 @@ func TestUser_GetRepositoryAccesses(t *testing.T) { func TestUser_GetAccessibleRepositories(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user1 := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) repos, err := user1.GetAccessibleRepositories(0) assert.NoError(t, err) assert.Len(t, repos, 0) - user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user2 := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) repos, err = user2.GetAccessibleRepositories(0) assert.NoError(t, err) assert.Len(t, repos, 4) - user29 := db.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User) + user29 := unittest.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User) repos, err = user29.GetAccessibleRepositories(0) assert.NoError(t, err) assert.Len(t, repos, 2) @@ -125,7 +125,7 @@ func TestUser_GetAccessibleRepositories(t *testing.T) { func TestRepository_RecalculateAccesses(t *testing.T) { // test with organization repo assert.NoError(t, unittest.PrepareTestDatabase()) - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) assert.NoError(t, repo1.GetOwner()) _, err := db.GetEngine(db.DefaultContext).Delete(&Collaboration{UserID: 2, RepoID: 3}) @@ -142,7 +142,7 @@ func TestRepository_RecalculateAccesses(t *testing.T) { func TestRepository_RecalculateAccesses2(t *testing.T) { // test with non-organization repo assert.NoError(t, unittest.PrepareTestDatabase()) - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) assert.NoError(t, repo1.GetOwner()) _, err := db.GetEngine(db.DefaultContext).Delete(&Collaboration{UserID: 4, RepoID: 4}) @@ -156,8 +156,8 @@ func TestRepository_RecalculateAccesses2(t *testing.T) { func TestRepository_RecalculateAccesses3(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - team5 := db.AssertExistsAndLoadBean(t, &Team{ID: 5}).(*Team) - user29 := db.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User) + team5 := unittest.AssertExistsAndLoadBean(t, &Team{ID: 5}).(*Team) + user29 := unittest.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User) has, err := db.GetEngine(db.DefaultContext).Get(&Access{UserID: 29, RepoID: 23}) assert.NoError(t, err) diff --git a/models/action_test.go b/models/action_test.go index 85b8290cb..87bbcf3fb 100644 --- a/models/action_test.go +++ b/models/action_test.go @@ -8,7 +8,6 @@ import ( "path" "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" @@ -17,16 +16,16 @@ import ( func TestAction_GetRepoPath(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{}).(*Repository) - owner := db.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{}).(*Repository) + owner := unittest.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, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{}).(*Repository) - owner := db.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{}).(*Repository) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User) action := &Action{RepoID: repo.ID} setting.AppSubURL = "/suburl" expected := path.Join(setting.AppSubURL, owner.Name, repo.Name) @@ -36,7 +35,7 @@ func TestAction_GetRepoLink(t *testing.T) { func TestGetFeeds(t *testing.T) { // test with an individual user assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) actions, err := GetFeeds(GetFeedsOptions{ RequestedUser: user, @@ -64,8 +63,8 @@ func TestGetFeeds(t *testing.T) { func TestGetFeeds2(t *testing.T) { // test with an organization user assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) actions, err := GetFeeds(GetFeedsOptions{ RequestedUser: org, diff --git a/models/admin_test.go b/models/admin_test.go index ea060f972..95415731b 100644 --- a/models/admin_test.go +++ b/models/admin_test.go @@ -7,7 +7,6 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -27,9 +26,9 @@ func TestCreateNotice(t *testing.T) { Type: NoticeRepository, Description: "test description", } - db.AssertNotExistsBean(t, noticeBean) + unittest.AssertNotExistsBean(t, noticeBean) assert.NoError(t, CreateNotice(noticeBean.Type, noticeBean.Description)) - db.AssertExistsAndLoadBean(t, noticeBean) + unittest.AssertExistsAndLoadBean(t, noticeBean) } func TestCreateRepositoryNotice(t *testing.T) { @@ -39,9 +38,9 @@ func TestCreateRepositoryNotice(t *testing.T) { Type: NoticeRepository, Description: "test description", } - db.AssertNotExistsBean(t, noticeBean) + unittest.AssertNotExistsBean(t, noticeBean) assert.NoError(t, CreateRepositoryNotice(noticeBean.Description)) - db.AssertExistsAndLoadBean(t, noticeBean) + unittest.AssertExistsAndLoadBean(t, noticeBean) } // TODO TestRemoveAllWithNotice @@ -71,45 +70,45 @@ func TestNotices(t *testing.T) { func TestDeleteNotice(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - db.AssertExistsAndLoadBean(t, &Notice{ID: 3}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 3}) assert.NoError(t, DeleteNotice(3)) - db.AssertNotExistsBean(t, &Notice{ID: 3}) + unittest.AssertNotExistsBean(t, &Notice{ID: 3}) } func TestDeleteNotices(t *testing.T) { // delete a non-empty range assert.NoError(t, unittest.PrepareTestDatabase()) - db.AssertExistsAndLoadBean(t, &Notice{ID: 1}) - db.AssertExistsAndLoadBean(t, &Notice{ID: 2}) - db.AssertExistsAndLoadBean(t, &Notice{ID: 3}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 1}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 2}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 3}) assert.NoError(t, DeleteNotices(1, 2)) - db.AssertNotExistsBean(t, &Notice{ID: 1}) - db.AssertNotExistsBean(t, &Notice{ID: 2}) - db.AssertExistsAndLoadBean(t, &Notice{ID: 3}) + unittest.AssertNotExistsBean(t, &Notice{ID: 1}) + unittest.AssertNotExistsBean(t, &Notice{ID: 2}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 3}) } func TestDeleteNotices2(t *testing.T) { // delete an empty range assert.NoError(t, unittest.PrepareTestDatabase()) - db.AssertExistsAndLoadBean(t, &Notice{ID: 1}) - db.AssertExistsAndLoadBean(t, &Notice{ID: 2}) - db.AssertExistsAndLoadBean(t, &Notice{ID: 3}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 1}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 2}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 3}) assert.NoError(t, DeleteNotices(3, 2)) - db.AssertExistsAndLoadBean(t, &Notice{ID: 1}) - db.AssertExistsAndLoadBean(t, &Notice{ID: 2}) - db.AssertExistsAndLoadBean(t, &Notice{ID: 3}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 1}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 2}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 3}) } func TestDeleteNoticesByIDs(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - db.AssertExistsAndLoadBean(t, &Notice{ID: 1}) - db.AssertExistsAndLoadBean(t, &Notice{ID: 2}) - db.AssertExistsAndLoadBean(t, &Notice{ID: 3}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 1}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 2}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 3}) assert.NoError(t, DeleteNoticesByIDs([]int64{1, 3})) - db.AssertNotExistsBean(t, &Notice{ID: 1}) - db.AssertExistsAndLoadBean(t, &Notice{ID: 2}) - db.AssertNotExistsBean(t, &Notice{ID: 3}) + unittest.AssertNotExistsBean(t, &Notice{ID: 1}) + unittest.AssertExistsAndLoadBean(t, &Notice{ID: 2}) + unittest.AssertNotExistsBean(t, &Notice{ID: 3}) } diff --git a/models/attachment_test.go b/models/attachment_test.go index bc8114fa6..c39499053 100644 --- a/models/attachment_test.go +++ b/models/attachment_test.go @@ -88,7 +88,7 @@ func TestUpdateAttachment(t *testing.T) { attach.Name = "new_name" assert.NoError(t, UpdateAttachment(attach)) - db.AssertExistsAndLoadBean(t, &Attachment{Name: "new_name"}) + unittest.AssertExistsAndLoadBean(t, &Attachment{Name: "new_name"}) } func TestGetAttachmentsByUUIDs(t *testing.T) { diff --git a/models/branches_test.go b/models/branches_test.go index d7233fba6..787dd7fe8 100644 --- a/models/branches_test.go +++ b/models/branches_test.go @@ -7,15 +7,14 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestAddDeletedBranch(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) - firstBranch := db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + firstBranch := unittest.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) assert.Error(t, repo.AddDeletedBranch(firstBranch.Name, firstBranch.Commit, firstBranch.DeletedByID)) assert.NoError(t, repo.AddDeletedBranch("test", "5655464564554545466464656", int64(1))) @@ -23,7 +22,7 @@ func TestAddDeletedBranch(t *testing.T) { func TestGetDeletedBranches(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) branches, err := repo.GetDeletedBranches() assert.NoError(t, err) @@ -32,7 +31,7 @@ func TestGetDeletedBranches(t *testing.T) { func TestGetDeletedBranch(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - firstBranch := db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) + firstBranch := unittest.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) assert.NotNil(t, getDeletedBranch(t, firstBranch)) } @@ -40,8 +39,8 @@ func TestGetDeletedBranch(t *testing.T) { func TestDeletedBranchLoadUser(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - firstBranch := db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) - secondBranch := db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 2}).(*DeletedBranch) + firstBranch := unittest.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) + secondBranch := unittest.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 2}).(*DeletedBranch) branch := getDeletedBranch(t, firstBranch) assert.Nil(t, branch.DeletedBy) @@ -58,18 +57,18 @@ func TestDeletedBranchLoadUser(t *testing.T) { func TestRemoveDeletedBranch(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) - firstBranch := db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) + firstBranch := unittest.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch) err := repo.RemoveDeletedBranch(1) assert.NoError(t, err) - db.AssertNotExistsBean(t, firstBranch) - db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 2}) + unittest.AssertNotExistsBean(t, firstBranch) + unittest.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 2}) } func getDeletedBranch(t *testing.T, branch *DeletedBranch) *DeletedBranch { - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) deletedBranch, err := repo.GetDeletedBranchByID(branch.ID) assert.NoError(t, err) @@ -95,7 +94,7 @@ func TestFindRenamedBranch(t *testing.T) { func TestRenameBranch(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) _isDefault := false err := UpdateProtectBranch(repo1, &ProtectedBranch{ @@ -110,21 +109,21 @@ func TestRenameBranch(t *testing.T) { })) assert.Equal(t, true, _isDefault) - repo1 = db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo1 = unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) assert.Equal(t, "main", repo1.DefaultBranch) - pull := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) // merged + pull := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) // merged assert.Equal(t, "master", pull.BaseBranch) - pull = db.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) // open + pull = unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) // open assert.Equal(t, "main", pull.BaseBranch) - renamedBranch := db.AssertExistsAndLoadBean(t, &RenamedBranch{ID: 2}).(*RenamedBranch) + renamedBranch := unittest.AssertExistsAndLoadBean(t, &RenamedBranch{ID: 2}).(*RenamedBranch) assert.Equal(t, "master", renamedBranch.From) assert.Equal(t, "main", renamedBranch.To) assert.Equal(t, int64(1), renamedBranch.RepoID) - db.AssertExistsAndLoadBean(t, &ProtectedBranch{ + unittest.AssertExistsAndLoadBean(t, &ProtectedBranch{ RepoID: repo1.ID, BranchName: "main", }) @@ -136,7 +135,7 @@ func TestOnlyGetDeletedBranchOnCorrectRepo(t *testing.T) { // Get deletedBranch with ID of 1 on repo with ID 2. // This should return a nil branch as this deleted branch // is actually on repo with ID 1. - repo2 := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) + repo2 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) deletedBranch, err := repo2.GetDeletedBranchByID(1) @@ -146,7 +145,7 @@ func TestOnlyGetDeletedBranchOnCorrectRepo(t *testing.T) { // Now get the deletedBranch with ID of 1 on repo with ID 1. // This should return the deletedBranch. - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) deletedBranch, err = repo1.GetDeletedBranchByID(1) diff --git a/models/commit_status_test.go b/models/commit_status_test.go index b113346a4..32d6a433c 100644 --- a/models/commit_status_test.go +++ b/models/commit_status_test.go @@ -16,7 +16,7 @@ import ( func TestGetCommitStatuses(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) sha1 := "1234123412341234123412341234123412341234" diff --git a/models/consistency.go b/models/consistency.go index 64c6b240a..d7bf8ade5 100644 --- a/models/consistency.go +++ b/models/consistency.go @@ -5,177 +5,11 @@ package models import ( - "reflect" - "strings" - "code.gitea.io/gitea/models/db" - "code.gitea.io/gitea/modules/unittestbridge" "xorm.io/builder" ) -// CheckConsistencyFor test that all matching database entries are consistent -func CheckConsistencyFor(t unittestbridge.Tester, beansToCheck ...interface{}) { - ta := unittestbridge.NewAsserter(t) - for _, bean := range beansToCheck { - sliceType := reflect.SliceOf(reflect.TypeOf(bean)) - sliceValue := reflect.MakeSlice(sliceType, 0, 10) - - ptrToSliceValue := reflect.New(sliceType) - ptrToSliceValue.Elem().Set(sliceValue) - - ta.NoError(db.GetEngine(db.DefaultContext).Table(bean).Find(ptrToSliceValue.Interface())) - sliceValue = ptrToSliceValue.Elem() - - for i := 0; i < sliceValue.Len(); i++ { - entity := sliceValue.Index(i).Interface() - checkForConsistency(ta, entity) - } - } -} - -func checkForConsistency(ta unittestbridge.Asserter, bean interface{}) { - switch b := bean.(type) { - case *User: - checkForUserConsistency(b, ta) - case *Repository: - checkForRepoConsistency(b, ta) - case *Issue: - checkForIssueConsistency(b, ta) - case *PullRequest: - checkForPullRequestConsistency(b, ta) - case *Milestone: - checkForMilestoneConsistency(b, ta) - case *Label: - checkForLabelConsistency(b, ta) - case *Team: - checkForTeamConsistency(b, ta) - case *Action: - checkForActionConsistency(b, ta) - default: - ta.Errorf("unknown bean type: %#v", bean) - } -} - -// getCount get the count of database entries matching bean -func getCount(ta unittestbridge.Asserter, e db.Engine, bean interface{}) int64 { - count, err := e.Count(bean) - ta.NoError(err) - return count -} - -// assertCount test the count of database entries matching bean -func assertCount(ta unittestbridge.Asserter, bean interface{}, expected int) { - ta.EqualValues(expected, getCount(ta, db.GetEngine(db.DefaultContext), bean), - "Failed consistency test, the counted bean (of type %T) was %+v", bean, bean) -} - -func checkForUserConsistency(user *User, ta unittestbridge.Asserter) { - assertCount(ta, &Repository{OwnerID: user.ID}, user.NumRepos) - assertCount(ta, &Star{UID: user.ID}, user.NumStars) - assertCount(ta, &OrgUser{OrgID: user.ID}, user.NumMembers) - assertCount(ta, &Team{OrgID: user.ID}, user.NumTeams) - assertCount(ta, &Follow{UserID: user.ID}, user.NumFollowing) - assertCount(ta, &Follow{FollowID: user.ID}, user.NumFollowers) - if user.Type != UserTypeOrganization { - ta.EqualValues(0, user.NumMembers) - ta.EqualValues(0, user.NumTeams) - } -} - -func checkForRepoConsistency(repo *Repository, ta unittestbridge.Asserter) { - ta.Equal(repo.LowerName, strings.ToLower(repo.Name), "repo: %+v", repo) - assertCount(ta, &Star{RepoID: repo.ID}, repo.NumStars) - assertCount(ta, &Milestone{RepoID: repo.ID}, repo.NumMilestones) - assertCount(ta, &Repository{ForkID: repo.ID}, repo.NumForks) - if repo.IsFork { - db.AssertExistsAndLoadBean(ta, &Repository{ID: repo.ForkID}) - } - - actual := getCount(ta, db.GetEngine(db.DefaultContext).Where("Mode<>?", RepoWatchModeDont), &Watch{RepoID: repo.ID}) - ta.EqualValues(repo.NumWatches, actual, - "Unexpected number of watches for repo %+v", repo) - - actual = getCount(ta, db.GetEngine(db.DefaultContext).Where("is_pull=?", false), &Issue{RepoID: repo.ID}) - ta.EqualValues(repo.NumIssues, actual, - "Unexpected number of issues for repo %+v", repo) - - actual = getCount(ta, db.GetEngine(db.DefaultContext).Where("is_pull=? AND is_closed=?", false, true), &Issue{RepoID: repo.ID}) - ta.EqualValues(repo.NumClosedIssues, actual, - "Unexpected number of closed issues for repo %+v", repo) - - actual = getCount(ta, db.GetEngine(db.DefaultContext).Where("is_pull=?", true), &Issue{RepoID: repo.ID}) - ta.EqualValues(repo.NumPulls, actual, - "Unexpected number of pulls for repo %+v", repo) - - actual = getCount(ta, db.GetEngine(db.DefaultContext).Where("is_pull=? AND is_closed=?", true, true), &Issue{RepoID: repo.ID}) - ta.EqualValues(repo.NumClosedPulls, actual, - "Unexpected number of closed pulls for repo %+v", repo) - - actual = getCount(ta, db.GetEngine(db.DefaultContext).Where("is_closed=?", true), &Milestone{RepoID: repo.ID}) - ta.EqualValues(repo.NumClosedMilestones, actual, - "Unexpected number of closed milestones for repo %+v", repo) -} - -func checkForIssueConsistency(issue *Issue, ta unittestbridge.Asserter) { - actual := getCount(ta, db.GetEngine(db.DefaultContext).Where("type=?", CommentTypeComment), &Comment{IssueID: issue.ID}) - ta.EqualValues(issue.NumComments, actual, - "Unexpected number of comments for issue %+v", issue) - if issue.IsPull { - pr := db.AssertExistsAndLoadBean(ta, &PullRequest{IssueID: issue.ID}).(*PullRequest) - ta.EqualValues(pr.Index, issue.Index) - } -} - -func checkForPullRequestConsistency(pr *PullRequest, ta unittestbridge.Asserter) { - issue := db.AssertExistsAndLoadBean(ta, &Issue{ID: pr.IssueID}).(*Issue) - ta.True(issue.IsPull) - ta.EqualValues(issue.Index, pr.Index) -} - -func checkForMilestoneConsistency(milestone *Milestone, ta unittestbridge.Asserter) { - assertCount(ta, &Issue{MilestoneID: milestone.ID}, milestone.NumIssues) - - actual := getCount(ta, db.GetEngine(db.DefaultContext).Where("is_closed=?", true), &Issue{MilestoneID: milestone.ID}) - ta.EqualValues(milestone.NumClosedIssues, actual, - "Unexpected number of closed issues for milestone %+v", milestone) - - completeness := 0 - if milestone.NumIssues > 0 { - completeness = milestone.NumClosedIssues * 100 / milestone.NumIssues - } - ta.Equal(completeness, milestone.Completeness) -} - -func checkForLabelConsistency(label *Label, ta unittestbridge.Asserter) { - issueLabels := make([]*IssueLabel, 0, 10) - ta.NoError(db.GetEngine(db.DefaultContext).Find(&issueLabels, &IssueLabel{LabelID: label.ID})) - ta.EqualValues(label.NumIssues, len(issueLabels), - "Unexpected number of issue for label %+v", label) - - issueIDs := make([]int64, len(issueLabels)) - for i, issueLabel := range issueLabels { - issueIDs[i] = issueLabel.IssueID - } - - expected := int64(0) - if len(issueIDs) > 0 { - expected = getCount(ta, db.GetEngine(db.DefaultContext).In("id", issueIDs).Where("is_closed=?", true), &Issue{}) - } - ta.EqualValues(expected, label.NumClosedIssues, - "Unexpected number of closed issues for label %+v", label) -} - -func checkForTeamConsistency(team *Team, ta unittestbridge.Asserter) { - assertCount(ta, &TeamUser{TeamID: team.ID}, team.NumMembers) - assertCount(ta, &TeamRepo{TeamID: team.ID}, team.NumRepos) -} - -func checkForActionConsistency(action *Action, ta unittestbridge.Asserter) { - repo := db.AssertExistsAndLoadBean(ta, &Repository{ID: action.RepoID}).(*Repository) - ta.Equal(repo.IsPrivate, action.IsPrivate, "action: %+v", action) -} - // CountOrphanedLabels return count of labels witch are broken and not accessible via ui anymore func CountOrphanedLabels() (int64, error) { noref, err := db.GetEngine(db.DefaultContext).Table("label").Where("repo_id=? AND org_id=?", 0, 0).Count("label.id") diff --git a/models/db/engine.go b/models/db/engine.go index d1b279e01..e39200802 100755 --- a/models/db/engine.go +++ b/models/db/engine.go @@ -153,6 +153,15 @@ func InitEngine(ctx context.Context) (err error) { return nil } +// SetEngine is used by unit test code +func SetEngine(eng *xorm.Engine) { + x = eng + DefaultContext = &Context{ + Context: context.Background(), + e: x, + } +} + // InitEngineWithMigration initializes a new xorm.Engine // This function must never call .Sync2() if the provided migration function fails. // When called from the "doctor" command, the migration function is a version check diff --git a/models/db/unit_tests.go b/models/db/unit_tests.go deleted file mode 100644 index 2b1691726..000000000 --- a/models/db/unit_tests.go +++ /dev/null @@ -1,125 +0,0 @@ -// Copyright 2016 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package db - -import ( - "context" - "math" - - "code.gitea.io/gitea/modules/unittestbridge" - - "xorm.io/xorm" -) - -// Code in this file is mainly used by models.CheckConsistencyFor, which is not in the unit test for various reasons. -// In the future if we can decouple CheckConsistencyFor into separate unit test code, then this file can be moved into unittest package too. - -// NonexistentID an ID that will never exist -const NonexistentID = int64(math.MaxInt64) - -//SetUnitTestEngine is used by unit test code -func SetUnitTestEngine(eng *xorm.Engine) { - x = eng - DefaultContext = &Context{ - Context: context.Background(), - e: x, - } -} - -type testCond struct { - query interface{} - args []interface{} -} - -// Cond create a condition with arguments for a test -func Cond(query interface{}, args ...interface{}) interface{} { - return &testCond{query: query, args: args} -} - -func whereConditions(sess *xorm.Session, conditions []interface{}) { - for _, condition := range conditions { - switch cond := condition.(type) { - case *testCond: - sess.Where(cond.query, cond.args...) - default: - sess.Where(cond) - } - } -} - -// LoadBeanIfExists loads beans from fixture database if exist -func LoadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) { - sess := x.NewSession() - defer sess.Close() - whereConditions(sess, conditions) - return sess.Get(bean) -} - -// BeanExists for testing, check if a bean exists -func BeanExists(t unittestbridge.Tester, bean interface{}, conditions ...interface{}) bool { - ta := unittestbridge.NewAsserter(t) - exists, err := LoadBeanIfExists(bean, conditions...) - ta.NoError(err) - return exists -} - -// AssertExistsAndLoadBean assert that a bean exists and load it from the test database -func AssertExistsAndLoadBean(t unittestbridge.Tester, bean interface{}, conditions ...interface{}) interface{} { - ta := unittestbridge.NewAsserter(t) - exists, err := LoadBeanIfExists(bean, conditions...) - ta.NoError(err) - ta.True(exists, - "Expected to find %+v (of type %T, with conditions %+v), but did not", - bean, bean, conditions) - return bean -} - -// GetCount get the count of a bean -func GetCount(t unittestbridge.Tester, bean interface{}, conditions ...interface{}) int { - ta := unittestbridge.NewAsserter(t) - sess := x.NewSession() - defer sess.Close() - whereConditions(sess, conditions) - count, err := sess.Count(bean) - ta.NoError(err) - return int(count) -} - -// AssertNotExistsBean assert that a bean does not exist in the test database -func AssertNotExistsBean(t unittestbridge.Tester, bean interface{}, conditions ...interface{}) { - ta := unittestbridge.NewAsserter(t) - exists, err := LoadBeanIfExists(bean, conditions...) - ta.NoError(err) - ta.False(exists) -} - -// AssertExistsIf asserts that a bean exists or does not exist, depending on -// what is expected. -func AssertExistsIf(t unittestbridge.Tester, expected bool, bean interface{}, conditions ...interface{}) { - ta := unittestbridge.NewAsserter(t) - exists, err := LoadBeanIfExists(bean, conditions...) - ta.NoError(err) - ta.Equal(expected, exists) -} - -// AssertSuccessfulInsert assert that beans is successfully inserted -func AssertSuccessfulInsert(t unittestbridge.Tester, beans ...interface{}) { - ta := unittestbridge.NewAsserter(t) - _, err := x.Insert(beans...) - ta.NoError(err) -} - -// AssertCount assert the count of a bean -func AssertCount(t unittestbridge.Tester, bean, expected interface{}) { - ta := unittestbridge.NewAsserter(t) - ta.EqualValues(expected, GetCount(ta, bean)) -} - -// AssertInt64InRange assert value is in range [low, high] -func AssertInt64InRange(t unittestbridge.Tester, low, high, value int64) { - ta := unittestbridge.NewAsserter(t) - ta.True(value >= low && value <= high, - "Expected value in range [%d, %d], found %d", low, high, value) -} diff --git a/models/gpg_key_test.go b/models/gpg_key_test.go index 3df25a10b..e5f4960b7 100644 --- a/models/gpg_key_test.go +++ b/models/gpg_key_test.go @@ -8,7 +8,6 @@ import ( "testing" "time" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/timeutil" @@ -196,7 +195,7 @@ Unknown GPG key with good email func TestCheckGPGUserEmail(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - _ = db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + _ = unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) testEmailWithUpperCaseLetters := `-----BEGIN PGP PUBLIC KEY BLOCK----- Version: GnuPG v1 diff --git a/models/issue_assignees_test.go b/models/issue_assignees_test.go index f0bd5fca4..8e2256072 100644 --- a/models/issue_assignees_test.go +++ b/models/issue_assignees_test.go @@ -7,7 +7,6 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -65,8 +64,8 @@ func TestUpdateAssignee(t *testing.T) { func TestMakeIDsFromAPIAssigneesToAdd(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - _ = db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) - _ = db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + _ = unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + _ = unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) IDs, err := MakeIDsFromAPIAssigneesToAdd("", []string{""}) assert.NoError(t, err) diff --git a/models/issue_comment_test.go b/models/issue_comment_test.go index d855b87bc..27c50a3ae 100644 --- a/models/issue_comment_test.go +++ b/models/issue_comment_test.go @@ -8,7 +8,6 @@ import ( "testing" "time" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -16,9 +15,9 @@ import ( func TestCreateComment(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{}).(*Issue) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: issue.RepoID}).(*Repository) - doer := db.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{}).(*Issue) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: issue.RepoID}).(*Repository) + doer := unittest.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User) now := time.Now().Unix() comment, err := CreateComment(&CreateCommentOptions{ @@ -35,18 +34,18 @@ func TestCreateComment(t *testing.T) { assert.EqualValues(t, "Hello", comment.Content) assert.EqualValues(t, issue.ID, comment.IssueID) assert.EqualValues(t, doer.ID, comment.PosterID) - db.AssertInt64InRange(t, now, then, int64(comment.CreatedUnix)) - db.AssertExistsAndLoadBean(t, comment) // assert actually added to DB + unittest.AssertInt64InRange(t, now, then, int64(comment.CreatedUnix)) + unittest.AssertExistsAndLoadBean(t, comment) // assert actually added to DB - updatedIssue := db.AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue) - db.AssertInt64InRange(t, now, then, int64(updatedIssue.UpdatedUnix)) + updatedIssue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue) + unittest.AssertInt64InRange(t, now, then, int64(updatedIssue.UpdatedUnix)) } func TestFetchCodeComments(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue) - user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) res, err := FetchCodeComments(issue, user) assert.NoError(t, err) assert.Contains(t, res, "README.md") @@ -54,7 +53,7 @@ func TestFetchCodeComments(t *testing.T) { assert.Len(t, res["README.md"][4], 1) assert.Equal(t, int64(4), res["README.md"][4][0].ID) - user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user2 := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) res, err = FetchCodeComments(issue, user2) assert.NoError(t, err) assert.Len(t, res, 1) diff --git a/models/issue_dependency_test.go b/models/issue_dependency_test.go index 60fa58738..c357d35c5 100644 --- a/models/issue_dependency_test.go +++ b/models/issue_dependency_test.go @@ -7,7 +7,6 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -39,7 +38,7 @@ func TestCreateIssueDependency(t *testing.T) { assert.Error(t, err) assert.True(t, IsErrCircularDependency(err)) - _ = db.AssertExistsAndLoadBean(t, &Comment{Type: CommentTypeAddDependency, PosterID: user1.ID, IssueID: issue1.ID}) + _ = unittest.AssertExistsAndLoadBean(t, &Comment{Type: CommentTypeAddDependency, PosterID: user1.ID, IssueID: issue1.ID}) // Check if dependencies left is correct left, err := IssueNoDependenciesLeft(issue1) diff --git a/models/issue_label_test.go b/models/issue_label_test.go index c77ddfefa..658c459ba 100644 --- a/models/issue_label_test.go +++ b/models/issue_label_test.go @@ -17,17 +17,17 @@ import ( func TestLabel_CalOpenIssues(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - label := db.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) + label := unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) label.CalOpenIssues() assert.EqualValues(t, 2, label.NumOpenIssues) } func TestLabel_ForegroundColor(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - label := db.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) + label := unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) assert.Equal(t, template.CSS("#000"), label.ForegroundColor()) - label = db.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) + label = unittest.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) assert.Equal(t, template.CSS("#fff"), label.ForegroundColor()) } @@ -41,13 +41,13 @@ func TestNewLabels(t *testing.T) { assert.Error(t, NewLabel(&Label{RepoID: 3, Name: "invalid Color", Color: "123456"})) assert.Error(t, NewLabel(&Label{RepoID: 3, Name: "invalid Color", Color: "#12345G"})) for _, label := range labels { - db.AssertNotExistsBean(t, label) + unittest.AssertNotExistsBean(t, label) } assert.NoError(t, NewLabels(labels...)) for _, label := range labels { - db.AssertExistsAndLoadBean(t, label, db.Cond("id = ?", label.ID)) + unittest.AssertExistsAndLoadBean(t, label, unittest.Cond("id = ?", label.ID)) } - CheckConsistencyFor(t, &Label{}, &Repository{}) + unittest.CheckConsistencyFor(t, &Label{}, &Repository{}) } func TestGetLabelByID(t *testing.T) { @@ -56,7 +56,7 @@ func TestGetLabelByID(t *testing.T) { assert.NoError(t, err) assert.EqualValues(t, 1, label.ID) - _, err = GetLabelByID(db.NonexistentID) + _, err = GetLabelByID(unittest.NonexistentID) assert.True(t, IsErrLabelNotExist(err)) } @@ -70,7 +70,7 @@ func TestGetLabelInRepoByName(t *testing.T) { _, err = GetLabelInRepoByName(1, "") assert.True(t, IsErrRepoLabelNotExist(err)) - _, err = GetLabelInRepoByName(db.NonexistentID, "nonexistent") + _, err = GetLabelInRepoByName(unittest.NonexistentID, "nonexistent") assert.True(t, IsErrRepoLabelNotExist(err)) } @@ -107,13 +107,13 @@ func TestGetLabelInRepoByID(t *testing.T) { _, err = GetLabelInRepoByID(1, -1) assert.True(t, IsErrRepoLabelNotExist(err)) - _, err = GetLabelInRepoByID(db.NonexistentID, db.NonexistentID) + _, err = GetLabelInRepoByID(unittest.NonexistentID, unittest.NonexistentID) assert.True(t, IsErrRepoLabelNotExist(err)) } func TestGetLabelsInRepoByIDs(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - labels, err := GetLabelsInRepoByIDs(1, []int64{1, 2, db.NonexistentID}) + labels, err := GetLabelsInRepoByIDs(1, []int64{1, 2, unittest.NonexistentID}) assert.NoError(t, err) if assert.Len(t, labels, 2) { assert.EqualValues(t, 1, labels[0].ID) @@ -155,7 +155,7 @@ func TestGetLabelInOrgByName(t *testing.T) { _, err = GetLabelInOrgByName(-1, "orglabel3") assert.True(t, IsErrOrgLabelNotExist(err)) - _, err = GetLabelInOrgByName(db.NonexistentID, "nonexistent") + _, err = GetLabelInOrgByName(unittest.NonexistentID, "nonexistent") assert.True(t, IsErrOrgLabelNotExist(err)) } @@ -198,13 +198,13 @@ func TestGetLabelInOrgByID(t *testing.T) { _, err = GetLabelInOrgByID(-1, 3) assert.True(t, IsErrOrgLabelNotExist(err)) - _, err = GetLabelInOrgByID(db.NonexistentID, db.NonexistentID) + _, err = GetLabelInOrgByID(unittest.NonexistentID, unittest.NonexistentID) assert.True(t, IsErrOrgLabelNotExist(err)) } func TestGetLabelsInOrgByIDs(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - labels, err := GetLabelsInOrgByIDs(3, []int64{3, 4, db.NonexistentID}) + labels, err := GetLabelsInOrgByIDs(3, []int64{3, 4, unittest.NonexistentID}) assert.NoError(t, err) if assert.Len(t, labels, 2) { assert.EqualValues(t, 3, labels[0].ID) @@ -245,14 +245,14 @@ func TestGetLabelsByIssueID(t *testing.T) { assert.EqualValues(t, 1, labels[0].ID) } - labels, err = GetLabelsByIssueID(db.NonexistentID) + labels, err = GetLabelsByIssueID(unittest.NonexistentID) assert.NoError(t, err) assert.Len(t, labels, 0) } func TestUpdateLabel(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - label := db.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) + label := unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) // make sure update wont overwrite it update := &Label{ ID: label.ID, @@ -263,99 +263,99 @@ func TestUpdateLabel(t *testing.T) { label.Color = update.Color label.Name = update.Name assert.NoError(t, UpdateLabel(update)) - newLabel := db.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) + newLabel := unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) assert.EqualValues(t, label.ID, newLabel.ID) assert.EqualValues(t, label.Color, newLabel.Color) assert.EqualValues(t, label.Name, newLabel.Name) assert.EqualValues(t, label.Description, newLabel.Description) - CheckConsistencyFor(t, &Label{}, &Repository{}) + unittest.CheckConsistencyFor(t, &Label{}, &Repository{}) } func TestDeleteLabel(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - label := db.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) + label := unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) assert.NoError(t, DeleteLabel(label.RepoID, label.ID)) - db.AssertNotExistsBean(t, &Label{ID: label.ID, RepoID: label.RepoID}) + unittest.AssertNotExistsBean(t, &Label{ID: label.ID, RepoID: label.RepoID}) assert.NoError(t, DeleteLabel(label.RepoID, label.ID)) - db.AssertNotExistsBean(t, &Label{ID: label.ID}) + unittest.AssertNotExistsBean(t, &Label{ID: label.ID}) - assert.NoError(t, DeleteLabel(db.NonexistentID, db.NonexistentID)) - CheckConsistencyFor(t, &Label{}, &Repository{}) + assert.NoError(t, DeleteLabel(unittest.NonexistentID, unittest.NonexistentID)) + unittest.CheckConsistencyFor(t, &Label{}, &Repository{}) } func TestHasIssueLabel(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) assert.True(t, HasIssueLabel(1, 1)) assert.False(t, HasIssueLabel(1, 2)) - assert.False(t, HasIssueLabel(db.NonexistentID, db.NonexistentID)) + assert.False(t, HasIssueLabel(unittest.NonexistentID, unittest.NonexistentID)) } func TestNewIssueLabel(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - label := db.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) - doer := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + label := unittest.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) + doer := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) // add new IssueLabel prevNumIssues := label.NumIssues assert.NoError(t, NewIssueLabel(issue, label, doer)) - db.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label.ID}) - db.AssertExistsAndLoadBean(t, &Comment{ + unittest.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label.ID}) + unittest.AssertExistsAndLoadBean(t, &Comment{ Type: CommentTypeLabel, PosterID: doer.ID, IssueID: issue.ID, LabelID: label.ID, Content: "1", }) - label = db.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) + label = unittest.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) assert.EqualValues(t, prevNumIssues+1, label.NumIssues) // re-add existing IssueLabel assert.NoError(t, NewIssueLabel(issue, label, doer)) - CheckConsistencyFor(t, &Issue{}, &Label{}) + unittest.CheckConsistencyFor(t, &Issue{}, &Label{}) } func TestNewIssueLabels(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - label1 := db.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) - label2 := db.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 5}).(*Issue) - doer := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + label1 := unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) + label2 := unittest.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 5}).(*Issue) + doer := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) assert.NoError(t, NewIssueLabels(issue, []*Label{label1, label2}, doer)) - db.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label1.ID}) - db.AssertExistsAndLoadBean(t, &Comment{ + unittest.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label1.ID}) + unittest.AssertExistsAndLoadBean(t, &Comment{ Type: CommentTypeLabel, PosterID: doer.ID, IssueID: issue.ID, LabelID: label1.ID, Content: "1", }) - db.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label1.ID}) - label1 = db.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) + unittest.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label1.ID}) + label1 = unittest.AssertExistsAndLoadBean(t, &Label{ID: 1}).(*Label) assert.EqualValues(t, 3, label1.NumIssues) assert.EqualValues(t, 1, label1.NumClosedIssues) - label2 = db.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) + label2 = unittest.AssertExistsAndLoadBean(t, &Label{ID: 2}).(*Label) assert.EqualValues(t, 1, label2.NumIssues) assert.EqualValues(t, 1, label2.NumClosedIssues) // corner case: test empty slice assert.NoError(t, NewIssueLabels(issue, []*Label{}, doer)) - CheckConsistencyFor(t, &Issue{}, &Label{}) + unittest.CheckConsistencyFor(t, &Issue{}, &Label{}) } func TestDeleteIssueLabel(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(labelID, issueID, doerID int64) { - label := db.AssertExistsAndLoadBean(t, &Label{ID: labelID}).(*Label) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: issueID}).(*Issue) - doer := db.AssertExistsAndLoadBean(t, &User{ID: doerID}).(*User) + label := unittest.AssertExistsAndLoadBean(t, &Label{ID: labelID}).(*Label) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: issueID}).(*Issue) + doer := unittest.AssertExistsAndLoadBean(t, &User{ID: doerID}).(*User) expectedNumIssues := label.NumIssues expectedNumClosedIssues := label.NumClosedIssues - if db.BeanExists(t, &IssueLabel{IssueID: issueID, LabelID: labelID}) { + if unittest.BeanExists(t, &IssueLabel{IssueID: issueID, LabelID: labelID}) { expectedNumIssues-- if issue.IsClosed { expectedNumClosedIssues-- @@ -363,14 +363,14 @@ func TestDeleteIssueLabel(t *testing.T) { } assert.NoError(t, DeleteIssueLabel(issue, label, doer)) - db.AssertNotExistsBean(t, &IssueLabel{IssueID: issueID, LabelID: labelID}) - db.AssertExistsAndLoadBean(t, &Comment{ + unittest.AssertNotExistsBean(t, &IssueLabel{IssueID: issueID, LabelID: labelID}) + unittest.AssertExistsAndLoadBean(t, &Comment{ Type: CommentTypeLabel, PosterID: doerID, IssueID: issueID, LabelID: labelID, }, `content=""`) - label = db.AssertExistsAndLoadBean(t, &Label{ID: labelID}).(*Label) + label = unittest.AssertExistsAndLoadBean(t, &Label{ID: labelID}).(*Label) assert.EqualValues(t, expectedNumIssues, label.NumIssues) assert.EqualValues(t, expectedNumClosedIssues, label.NumClosedIssues) } @@ -378,5 +378,5 @@ func TestDeleteIssueLabel(t *testing.T) { testSuccess(2, 5, 2) testSuccess(1, 1, 2) // delete non-existent IssueLabel - CheckConsistencyFor(t, &Issue{}, &Label{}) + unittest.CheckConsistencyFor(t, &Issue{}, &Label{}) } diff --git a/models/issue_list_test.go b/models/issue_list_test.go index ea2f42e77..2916a7302 100644 --- a/models/issue_list_test.go +++ b/models/issue_list_test.go @@ -7,7 +7,6 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" @@ -18,9 +17,9 @@ func TestIssueList_LoadRepositories(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) issueList := IssueList{ - db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue), - db.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue), - db.AssertExistsAndLoadBean(t, &Issue{ID: 4}).(*Issue), + unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue), + unittest.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue), + unittest.AssertExistsAndLoadBean(t, &Issue{ID: 4}).(*Issue), } repos, err := issueList.LoadRepositories() @@ -35,8 +34,8 @@ func TestIssueList_LoadAttributes(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) setting.Service.EnableTimetracking = true issueList := IssueList{ - db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue), - db.AssertExistsAndLoadBean(t, &Issue{ID: 4}).(*Issue), + unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue), + unittest.AssertExistsAndLoadBean(t, &Issue{ID: 4}).(*Issue), } assert.NoError(t, issueList.LoadAttributes()) @@ -44,7 +43,7 @@ func TestIssueList_LoadAttributes(t *testing.T) { assert.EqualValues(t, issue.RepoID, issue.Repo.ID) for _, label := range issue.Labels { assert.EqualValues(t, issue.RepoID, label.RepoID) - db.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label.ID}) + unittest.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issue.ID, LabelID: label.ID}) } if issue.PosterID > 0 { assert.EqualValues(t, issue.PosterID, issue.Poster.ID) diff --git a/models/issue_milestone_test.go b/models/issue_milestone_test.go index 8dee46406..2956e40f0 100644 --- a/models/issue_milestone_test.go +++ b/models/issue_milestone_test.go @@ -32,8 +32,8 @@ func TestNewMilestone(t *testing.T) { } assert.NoError(t, NewMilestone(milestone)) - db.AssertExistsAndLoadBean(t, milestone) - CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{}) + unittest.AssertExistsAndLoadBean(t, milestone) + unittest.CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{}) } func TestGetMilestoneByRepoID(t *testing.T) { @@ -44,14 +44,14 @@ func TestGetMilestoneByRepoID(t *testing.T) { assert.EqualValues(t, 1, milestone.ID) assert.EqualValues(t, 1, milestone.RepoID) - _, err = GetMilestoneByRepoID(db.NonexistentID, db.NonexistentID) + _, err = GetMilestoneByRepoID(unittest.NonexistentID, unittest.NonexistentID) assert.True(t, IsErrMilestoneNotExist(err)) } func TestGetMilestonesByRepoID(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(repoID int64, state api.StateType) { - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) milestones, _, err := GetMilestones(GetMilestonesOption{ RepoID: repo.ID, State: state, @@ -90,7 +90,7 @@ func TestGetMilestonesByRepoID(t *testing.T) { test(3, api.StateAll) milestones, _, err := GetMilestones(GetMilestonesOption{ - RepoID: db.NonexistentID, + RepoID: unittest.NonexistentID, State: api.StateOpen, }) assert.NoError(t, err) @@ -99,7 +99,7 @@ func TestGetMilestonesByRepoID(t *testing.T) { func TestGetMilestones(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) test := func(sortType string, sortCond func(*Milestone) int) { for _, page := range []int{0, 1} { milestones, _, err := GetMilestones(GetMilestonesOption{ @@ -161,19 +161,19 @@ func TestGetMilestones(t *testing.T) { func TestUpdateMilestone(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - milestone := db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone) + milestone := unittest.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone) milestone.Name = " newMilestoneName " milestone.Content = "newMilestoneContent" assert.NoError(t, UpdateMilestone(milestone, milestone.IsClosed)) - milestone = db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone) + milestone = unittest.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone) assert.EqualValues(t, "newMilestoneName", milestone.Name) - CheckConsistencyFor(t, &Milestone{}) + unittest.CheckConsistencyFor(t, &Milestone{}) } func TestCountRepoMilestones(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(repoID int64) { - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) count, err := countRepoMilestones(db.GetEngine(db.DefaultContext), repoID) assert.NoError(t, err) assert.EqualValues(t, repo.NumMilestones, count) @@ -182,7 +182,7 @@ func TestCountRepoMilestones(t *testing.T) { test(2) test(3) - count, err := countRepoMilestones(db.GetEngine(db.DefaultContext), db.NonexistentID) + count, err := countRepoMilestones(db.GetEngine(db.DefaultContext), unittest.NonexistentID) assert.NoError(t, err) assert.EqualValues(t, 0, count) } @@ -190,7 +190,7 @@ func TestCountRepoMilestones(t *testing.T) { func TestCountRepoClosedMilestones(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(repoID int64) { - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) count, err := CountRepoClosedMilestones(repoID) assert.NoError(t, err) assert.EqualValues(t, repo.NumClosedMilestones, count) @@ -199,27 +199,27 @@ func TestCountRepoClosedMilestones(t *testing.T) { test(2) test(3) - count, err := CountRepoClosedMilestones(db.NonexistentID) + count, err := CountRepoClosedMilestones(unittest.NonexistentID) assert.NoError(t, err) assert.EqualValues(t, 0, count) } func TestChangeMilestoneStatus(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - milestone := db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone) + milestone := unittest.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone) assert.NoError(t, ChangeMilestoneStatus(milestone, true)) - db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}, "is_closed=1") - CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{}) + unittest.AssertExistsAndLoadBean(t, &Milestone{ID: 1}, "is_closed=1") + unittest.CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{}) assert.NoError(t, ChangeMilestoneStatus(milestone, false)) - db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}, "is_closed=0") - CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{}) + unittest.AssertExistsAndLoadBean(t, &Milestone{ID: 1}, "is_closed=0") + unittest.CheckConsistencyFor(t, &Repository{ID: milestone.RepoID}, &Milestone{}) } func TestUpdateMilestoneCounters(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{MilestoneID: 1}, + issue := unittest.AssertExistsAndLoadBean(t, &Issue{MilestoneID: 1}, "is_closed=0").(*Issue) issue.IsClosed = true @@ -227,48 +227,48 @@ func TestUpdateMilestoneCounters(t *testing.T) { _, err := db.GetEngine(db.DefaultContext).ID(issue.ID).Cols("is_closed", "closed_unix").Update(issue) assert.NoError(t, err) assert.NoError(t, updateMilestoneCounters(db.GetEngine(db.DefaultContext), issue.MilestoneID)) - CheckConsistencyFor(t, &Milestone{}) + unittest.CheckConsistencyFor(t, &Milestone{}) issue.IsClosed = false issue.ClosedUnix = 0 _, err = db.GetEngine(db.DefaultContext).ID(issue.ID).Cols("is_closed", "closed_unix").Update(issue) assert.NoError(t, err) assert.NoError(t, updateMilestoneCounters(db.GetEngine(db.DefaultContext), issue.MilestoneID)) - CheckConsistencyFor(t, &Milestone{}) + unittest.CheckConsistencyFor(t, &Milestone{}) } func TestChangeMilestoneAssign(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{RepoID: 1}).(*Issue) - doer := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{RepoID: 1}).(*Issue) + doer := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) assert.NotNil(t, issue) assert.NotNil(t, doer) oldMilestoneID := issue.MilestoneID issue.MilestoneID = 2 assert.NoError(t, ChangeMilestoneAssign(issue, doer, oldMilestoneID)) - db.AssertExistsAndLoadBean(t, &Comment{ + unittest.AssertExistsAndLoadBean(t, &Comment{ IssueID: issue.ID, Type: CommentTypeMilestone, MilestoneID: issue.MilestoneID, OldMilestoneID: oldMilestoneID, }) - CheckConsistencyFor(t, &Milestone{}, &Issue{}) + unittest.CheckConsistencyFor(t, &Milestone{}, &Issue{}) } func TestDeleteMilestoneByRepoID(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, DeleteMilestoneByRepoID(1, 1)) - db.AssertNotExistsBean(t, &Milestone{ID: 1}) - CheckConsistencyFor(t, &Repository{ID: 1}) + unittest.AssertNotExistsBean(t, &Milestone{ID: 1}) + unittest.CheckConsistencyFor(t, &Repository{ID: 1}) - assert.NoError(t, DeleteMilestoneByRepoID(db.NonexistentID, db.NonexistentID)) + assert.NoError(t, DeleteMilestoneByRepoID(unittest.NonexistentID, unittest.NonexistentID)) } func TestMilestoneList_LoadTotalTrackedTimes(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) miles := MilestoneList{ - db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone), + unittest.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone), } assert.NoError(t, miles.LoadTotalTrackedTimes()) @@ -279,7 +279,7 @@ func TestMilestoneList_LoadTotalTrackedTimes(t *testing.T) { func TestCountMilestonesByRepoIDs(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) milestonesCount := func(repoID int64) (int, int) { - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) return repo.NumOpenMilestones, repo.NumClosedMilestones } repo1OpenCount, repo1ClosedCount := milestonesCount(1) @@ -298,8 +298,8 @@ func TestCountMilestonesByRepoIDs(t *testing.T) { func TestGetMilestonesByRepoIDs(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) - repo2 := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo2 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) test := func(sortType string, sortCond func(*Milestone) int) { for _, page := range []int{0, 1} { openMilestones, err := GetMilestonesByRepoIDs([]int64{repo1.ID, repo2.ID}, page, false, sortType) @@ -343,7 +343,7 @@ func TestGetMilestonesByRepoIDs(t *testing.T) { func TestLoadTotalTrackedTime(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - milestone := db.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone) + milestone := unittest.AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone) assert.NoError(t, milestone.LoadTotalTrackedTime()) @@ -354,7 +354,7 @@ func TestGetMilestonesStats(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(repoID int64) { - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) stats, err := GetMilestonesStatsByRepoCond(builder.And(builder.Eq{"repo_id": repoID})) assert.NoError(t, err) assert.EqualValues(t, repo.NumMilestones-repo.NumClosedMilestones, stats.OpenCount) @@ -364,13 +364,13 @@ func TestGetMilestonesStats(t *testing.T) { test(2) test(3) - stats, err := GetMilestonesStatsByRepoCond(builder.And(builder.Eq{"repo_id": db.NonexistentID})) + stats, err := GetMilestonesStatsByRepoCond(builder.And(builder.Eq{"repo_id": unittest.NonexistentID})) assert.NoError(t, err) assert.EqualValues(t, 0, stats.OpenCount) assert.EqualValues(t, 0, stats.ClosedCount) - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) - repo2 := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo2 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) milestoneStats, err := GetMilestonesStatsByRepoCond(builder.In("repo_id", []int64{repo1.ID, repo2.ID})) assert.NoError(t, err) diff --git a/models/issue_reaction_test.go b/models/issue_reaction_test.go index 5c0441623..c01e4add7 100644 --- a/models/issue_reaction_test.go +++ b/models/issue_reaction_test.go @@ -28,21 +28,21 @@ func addReaction(t *testing.T, doer *User, issue *Issue, comment *Comment, conte func TestIssueAddReaction(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user1 := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) - issue1 := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) + issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) addReaction(t, user1, issue1, nil, "heart") - db.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID}) + unittest.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID}) } func TestIssueAddDuplicateReaction(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user1 := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) - issue1 := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) + issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) addReaction(t, user1, issue1, nil, "heart") @@ -54,23 +54,23 @@ func TestIssueAddDuplicateReaction(t *testing.T) { assert.Error(t, err) assert.Equal(t, ErrReactionAlreadyExist{Reaction: "heart"}, err) - existingR := db.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID}).(*Reaction) + existingR := unittest.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID}).(*Reaction) assert.Equal(t, existingR.ID, reaction.ID) } func TestIssueDeleteReaction(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user1 := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) - issue1 := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) + issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) addReaction(t, user1, issue1, nil, "heart") err := DeleteIssueReaction(user1, issue1, "heart") assert.NoError(t, err) - db.AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID}) + unittest.AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID}) } func TestIssueReactionCount(t *testing.T) { @@ -78,13 +78,13 @@ func TestIssueReactionCount(t *testing.T) { setting.UI.ReactionMaxUserNum = 2 - user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) - user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - user3 := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) - user4 := db.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) + user1 := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user2 := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user3 := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + user4 := unittest.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) ghost := NewGhostUser() - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue) addReaction(t, user1, issue, nil, "heart") addReaction(t, user2, issue, nil, "heart") @@ -114,29 +114,29 @@ func TestIssueReactionCount(t *testing.T) { func TestIssueCommentAddReaction(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user1 := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) - issue1 := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) + issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) - comment1 := db.AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment) + comment1 := unittest.AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment) addReaction(t, user1, issue1, comment1, "heart") - db.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID, CommentID: comment1.ID}) + unittest.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID, CommentID: comment1.ID}) } func TestIssueCommentDeleteReaction(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) - user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - user3 := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) - user4 := db.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) + user1 := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user2 := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user3 := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + user4 := unittest.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) - issue1 := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: issue1.RepoID}).(*Repository) + issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: issue1.RepoID}).(*Repository) - comment1 := db.AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment) + comment1 := unittest.AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment) addReaction(t, user1, issue1, comment1, "heart") addReaction(t, user2, issue1, comment1, "heart") @@ -155,14 +155,14 @@ func TestIssueCommentDeleteReaction(t *testing.T) { func TestIssueCommentReactionCount(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user1 := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) - issue1 := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) + issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) - comment1 := db.AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment) + comment1 := unittest.AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment) addReaction(t, user1, issue1, comment1, "heart") assert.NoError(t, DeleteCommentReaction(user1, issue1, comment1, "heart")) - db.AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID, CommentID: comment1.ID}) + unittest.AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID, CommentID: comment1.ID}) } diff --git a/models/issue_stopwatch_test.go b/models/issue_stopwatch_test.go index 0de4dd034..ceb579d09 100644 --- a/models/issue_stopwatch_test.go +++ b/models/issue_stopwatch_test.go @@ -7,7 +7,6 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/timeutil" @@ -27,9 +26,9 @@ func TestCancelStopwatch(t *testing.T) { err = CancelStopwatch(user1, issue1) assert.NoError(t, err) - db.AssertNotExistsBean(t, &Stopwatch{UserID: user1.ID, IssueID: issue1.ID}) + unittest.AssertNotExistsBean(t, &Stopwatch{UserID: user1.ID, IssueID: issue1.ID}) - _ = db.AssertExistsAndLoadBean(t, &Comment{Type: CommentTypeCancelTracking, PosterID: user1.ID, IssueID: issue1.ID}) + _ = unittest.AssertExistsAndLoadBean(t, &Comment{Type: CommentTypeCancelTracking, PosterID: user1.ID, IssueID: issue1.ID}) assert.Nil(t, CancelStopwatch(user1, issue2)) } @@ -68,10 +67,10 @@ func TestCreateOrStopIssueStopwatch(t *testing.T) { assert.NoError(t, err) assert.NoError(t, CreateOrStopIssueStopwatch(user3, issue1)) - sw := db.AssertExistsAndLoadBean(t, &Stopwatch{UserID: 3, IssueID: 1}).(*Stopwatch) + sw := unittest.AssertExistsAndLoadBean(t, &Stopwatch{UserID: 3, IssueID: 1}).(*Stopwatch) assert.LessOrEqual(t, sw.CreatedUnix, timeutil.TimeStampNow()) assert.NoError(t, CreateOrStopIssueStopwatch(user2, issue2)) - db.AssertNotExistsBean(t, &Stopwatch{UserID: 2, IssueID: 2}) - db.AssertExistsAndLoadBean(t, &TrackedTime{UserID: 2, IssueID: 2}) + unittest.AssertNotExistsBean(t, &Stopwatch{UserID: 2, IssueID: 2}) + unittest.AssertExistsAndLoadBean(t, &TrackedTime{UserID: 2, IssueID: 2}) } diff --git a/models/issue_test.go b/models/issue_test.go index 296e12a5d..f53febb1b 100644 --- a/models/issue_test.go +++ b/models/issue_test.go @@ -20,18 +20,18 @@ func TestIssue_ReplaceLabels(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(issueID int64, labelIDs []int64) { - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: issueID}).(*Issue) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: issue.RepoID}).(*Repository) - doer := db.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: issueID}).(*Issue) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: issue.RepoID}).(*Repository) + doer := unittest.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User) labels := make([]*Label, len(labelIDs)) for i, labelID := range labelIDs { - labels[i] = db.AssertExistsAndLoadBean(t, &Label{ID: labelID, RepoID: repo.ID}).(*Label) + labels[i] = unittest.AssertExistsAndLoadBean(t, &Label{ID: labelID, RepoID: repo.ID}).(*Label) } assert.NoError(t, issue.ReplaceLabels(labels, doer)) - db.AssertCount(t, &IssueLabel{IssueID: issueID}, len(labelIDs)) + unittest.AssertCount(t, &IssueLabel{IssueID: issueID}, len(labelIDs)) for _, labelID := range labelIDs { - db.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issueID, LabelID: labelID}) + unittest.AssertExistsAndLoadBean(t, &IssueLabel{IssueID: issueID, LabelID: labelID}) } } @@ -50,7 +50,7 @@ func Test_GetIssueIDsByRepoID(t *testing.T) { func TestIssueAPIURL(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) err := issue.LoadAttributes() assert.NoError(t, err) @@ -69,7 +69,7 @@ func TestGetIssuesByIDs(t *testing.T) { assert.Equal(t, expectedIssueIDs, actualIssueIDs) } testSuccess([]int64{1, 2, 3}, []int64{}) - testSuccess([]int64{1, 2, 3}, []int64{db.NonexistentID}) + testSuccess([]int64{1, 2, 3}, []int64{unittest.NonexistentID}) } func TestGetParticipantIDsByIssue(t *testing.T) { @@ -108,16 +108,16 @@ func TestIssue_ClearLabels(t *testing.T) { } for _, test := range tests { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: test.issueID}).(*Issue) - doer := db.AssertExistsAndLoadBean(t, &User{ID: test.doerID}).(*User) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: test.issueID}).(*Issue) + doer := unittest.AssertExistsAndLoadBean(t, &User{ID: test.doerID}).(*User) assert.NoError(t, issue.ClearLabels(doer)) - db.AssertNotExistsBean(t, &IssueLabel{IssueID: test.issueID}) + unittest.AssertNotExistsBean(t, &IssueLabel{IssueID: test.issueID}) } } func TestUpdateIssueCols(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{}).(*Issue) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{}).(*Issue) const newTitle = "New Title for unit test" issue.Title = newTitle @@ -129,10 +129,10 @@ func TestUpdateIssueCols(t *testing.T) { assert.NoError(t, updateIssueCols(db.GetEngine(db.DefaultContext), issue, "name")) then := time.Now().Unix() - updatedIssue := db.AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue) + updatedIssue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: issue.ID}).(*Issue) assert.EqualValues(t, newTitle, updatedIssue.Title) assert.EqualValues(t, prevContent, updatedIssue.Content) - db.AssertInt64InRange(t, now, then, int64(updatedIssue.UpdatedUnix)) + unittest.AssertInt64InRange(t, now, then, int64(updatedIssue.UpdatedUnix)) } func TestIssues(t *testing.T) { @@ -321,7 +321,7 @@ func TestIssue_SearchIssueIDsByKeyword(t *testing.T) { func TestGetRepoIDsForIssuesOptions(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) for _, test := range []struct { Opts IssuesOptions ExpectedRepoIDs []int64 @@ -352,8 +352,8 @@ func TestGetRepoIDsForIssuesOptions(t *testing.T) { func testInsertIssue(t *testing.T, title, content string, expectIndex int64) *Issue { var newIssue Issue t.Run(title, func(t *testing.T) { - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) issue := Issue{ RepoID: repo.ID, @@ -395,10 +395,10 @@ func TestIssue_ResolveMentions(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(owner, repo, doer string, mentions []string, expected []int64) { - o := db.AssertExistsAndLoadBean(t, &User{LowerName: owner}).(*User) - r := db.AssertExistsAndLoadBean(t, &Repository{OwnerID: o.ID, LowerName: repo}).(*Repository) + o := unittest.AssertExistsAndLoadBean(t, &User{LowerName: owner}).(*User) + r := unittest.AssertExistsAndLoadBean(t, &Repository{OwnerID: o.ID, LowerName: repo}).(*Repository) issue := &Issue{RepoID: r.ID} - d := db.AssertExistsAndLoadBean(t, &User{LowerName: doer}).(*User) + d := unittest.AssertExistsAndLoadBean(t, &User{LowerName: doer}).(*User) resolved, err := issue.ResolveMentionsByVisibility(db.DefaultContext, d, mentions) assert.NoError(t, err) ids := make([]int64, len(resolved)) diff --git a/models/issue_tracked_time_test.go b/models/issue_tracked_time_test.go index 277006536..9bc966452 100644 --- a/models/issue_tracked_time_test.go +++ b/models/issue_tracked_time_test.go @@ -8,7 +8,6 @@ import ( "testing" "time" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -29,10 +28,10 @@ func TestAddTime(t *testing.T) { assert.Equal(t, int64(1), trackedTime.IssueID) assert.Equal(t, int64(3661), trackedTime.Time) - tt := db.AssertExistsAndLoadBean(t, &TrackedTime{UserID: 3, IssueID: 1}).(*TrackedTime) + tt := unittest.AssertExistsAndLoadBean(t, &TrackedTime{UserID: 3, IssueID: 1}).(*TrackedTime) assert.Equal(t, int64(3661), tt.Time) - comment := db.AssertExistsAndLoadBean(t, &Comment{Type: CommentTypeAddTimeManual, PosterID: 3, IssueID: 1}).(*Comment) + comment := unittest.AssertExistsAndLoadBean(t, &Comment{Type: CommentTypeAddTimeManual, PosterID: 3, IssueID: 1}).(*Comment) assert.Equal(t, comment.Content, "1h 1min 1s") } diff --git a/models/issue_user_test.go b/models/issue_user_test.go index 1a9ae6395..1be276370 100644 --- a/models/issue_user_test.go +++ b/models/issue_user_test.go @@ -15,7 +15,7 @@ import ( func Test_newIssueUsers(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) newIssue := &Issue{ RepoID: repo.ID, PosterID: 4, @@ -25,35 +25,35 @@ func Test_newIssueUsers(t *testing.T) { } // artificially insert new issue - db.AssertSuccessfulInsert(t, newIssue) + unittest.AssertSuccessfulInsert(t, newIssue) assert.NoError(t, newIssueUsers(db.GetEngine(db.DefaultContext), repo, newIssue)) // issue_user table should now have entries for new issue - db.AssertExistsAndLoadBean(t, &IssueUser{IssueID: newIssue.ID, UID: newIssue.PosterID}) - db.AssertExistsAndLoadBean(t, &IssueUser{IssueID: newIssue.ID, UID: repo.OwnerID}) + unittest.AssertExistsAndLoadBean(t, &IssueUser{IssueID: newIssue.ID, UID: newIssue.PosterID}) + unittest.AssertExistsAndLoadBean(t, &IssueUser{IssueID: newIssue.ID, UID: repo.OwnerID}) } func TestUpdateIssueUserByRead(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) assert.NoError(t, UpdateIssueUserByRead(4, issue.ID)) - db.AssertExistsAndLoadBean(t, &IssueUser{IssueID: issue.ID, UID: 4}, "is_read=1") + unittest.AssertExistsAndLoadBean(t, &IssueUser{IssueID: issue.ID, UID: 4}, "is_read=1") assert.NoError(t, UpdateIssueUserByRead(4, issue.ID)) - db.AssertExistsAndLoadBean(t, &IssueUser{IssueID: issue.ID, UID: 4}, "is_read=1") + unittest.AssertExistsAndLoadBean(t, &IssueUser{IssueID: issue.ID, UID: 4}, "is_read=1") - assert.NoError(t, UpdateIssueUserByRead(db.NonexistentID, db.NonexistentID)) + assert.NoError(t, UpdateIssueUserByRead(unittest.NonexistentID, unittest.NonexistentID)) } func TestUpdateIssueUsersByMentions(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) uids := []int64{2, 5} assert.NoError(t, UpdateIssueUsersByMentions(db.DefaultContext, issue.ID, uids)) for _, uid := range uids { - db.AssertExistsAndLoadBean(t, &IssueUser{IssueID: issue.ID, UID: uid}, "is_mentioned=1") + unittest.AssertExistsAndLoadBean(t, &IssueUser{IssueID: issue.ID, UID: uid}, "is_mentioned=1") } } diff --git a/models/issue_watch_test.go b/models/issue_watch_test.go index 9aaa184a3..18d49bfce 100644 --- a/models/issue_watch_test.go +++ b/models/issue_watch_test.go @@ -16,11 +16,11 @@ func TestCreateOrUpdateIssueWatch(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, CreateOrUpdateIssueWatch(3, 1, true)) - iw := db.AssertExistsAndLoadBean(t, &IssueWatch{UserID: 3, IssueID: 1}).(*IssueWatch) + iw := unittest.AssertExistsAndLoadBean(t, &IssueWatch{UserID: 3, IssueID: 1}).(*IssueWatch) assert.True(t, iw.IsWatching) assert.NoError(t, CreateOrUpdateIssueWatch(1, 1, false)) - iw = db.AssertExistsAndLoadBean(t, &IssueWatch{UserID: 1, IssueID: 1}).(*IssueWatch) + iw = unittest.AssertExistsAndLoadBean(t, &IssueWatch{UserID: 1, IssueID: 1}).(*IssueWatch) assert.False(t, iw.IsWatching) } diff --git a/models/issue_xref_test.go b/models/issue_xref_test.go index a1f4515b6..551c29dcb 100644 --- a/models/issue_xref_test.go +++ b/models/issue_xref_test.go @@ -24,7 +24,7 @@ func TestXRef_AddCrossReferences(t *testing.T) { // PR to close issue #1 content := fmt.Sprintf("content2, closes #%d", itarget.Index) pr := testCreateIssue(t, 1, 2, "title2", content, true) - ref := db.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: pr.ID, RefCommentID: 0}).(*Comment) + ref := unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: pr.ID, RefCommentID: 0}).(*Comment) assert.Equal(t, CommentTypePullRef, ref.Type) assert.Equal(t, pr.RepoID, ref.RefRepoID) assert.True(t, ref.RefIsPull) @@ -33,7 +33,7 @@ func TestXRef_AddCrossReferences(t *testing.T) { // Comment on PR to reopen issue #1 content = fmt.Sprintf("content2, reopens #%d", itarget.Index) c := testCreateComment(t, 1, 2, pr.ID, content) - ref = db.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: pr.ID, RefCommentID: c.ID}).(*Comment) + ref = unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: pr.ID, RefCommentID: c.ID}).(*Comment) assert.Equal(t, CommentTypeCommentRef, ref.Type) assert.Equal(t, pr.RepoID, ref.RefRepoID) assert.True(t, ref.RefIsPull) @@ -42,7 +42,7 @@ func TestXRef_AddCrossReferences(t *testing.T) { // Issue mentioning issue #1 content = fmt.Sprintf("content3, mentions #%d", itarget.Index) i := testCreateIssue(t, 1, 2, "title3", content, false) - ref = db.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*Comment) + ref = unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*Comment) assert.Equal(t, CommentTypeIssueRef, ref.Type) assert.Equal(t, pr.RepoID, ref.RefRepoID) assert.False(t, ref.RefIsPull) @@ -54,7 +54,7 @@ func TestXRef_AddCrossReferences(t *testing.T) { // Cross-reference to issue #4 by admin content = fmt.Sprintf("content5, mentions user3/repo3#%d", itarget.Index) i = testCreateIssue(t, 2, 1, "title5", content, false) - ref = db.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*Comment) + ref = unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*Comment) assert.Equal(t, CommentTypeIssueRef, ref.Type) assert.Equal(t, i.RepoID, ref.RefRepoID) assert.False(t, ref.RefIsPull) @@ -63,7 +63,7 @@ func TestXRef_AddCrossReferences(t *testing.T) { // Cross-reference to issue #4 with no permission content = fmt.Sprintf("content6, mentions user3/repo3#%d", itarget.Index) i = testCreateIssue(t, 4, 5, "title6", content, false) - db.AssertNotExistsBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}) + unittest.AssertNotExistsBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}) } func TestXRef_NeuterCrossReferences(t *testing.T) { @@ -75,15 +75,15 @@ func TestXRef_NeuterCrossReferences(t *testing.T) { // Issue mentioning issue #1 title := fmt.Sprintf("title2, mentions #%d", itarget.Index) i := testCreateIssue(t, 1, 2, title, "content2", false) - ref := db.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*Comment) + ref := unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*Comment) assert.Equal(t, CommentTypeIssueRef, ref.Type) assert.Equal(t, references.XRefActionNone, ref.RefAction) - d := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + d := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) i.Title = "title2, no mentions" assert.NoError(t, i.ChangeTitle(d, title)) - ref = db.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*Comment) + ref = unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*Comment) assert.Equal(t, CommentTypeIssueRef, ref.Type) assert.Equal(t, references.XRefActionNeutered, ref.RefAction) } @@ -91,7 +91,7 @@ func TestXRef_NeuterCrossReferences(t *testing.T) { func TestXRef_ResolveCrossReferences(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - d := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + d := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) i1 := testCreateIssue(t, 1, 2, "title1", "content1", false) i2 := testCreateIssue(t, 1, 2, "title2", "content2", false) @@ -100,21 +100,21 @@ func TestXRef_ResolveCrossReferences(t *testing.T) { assert.NoError(t, err) pr := testCreatePR(t, 1, 2, "titlepr", fmt.Sprintf("closes #%d", i1.Index)) - rp := db.AssertExistsAndLoadBean(t, &Comment{IssueID: i1.ID, RefIssueID: pr.Issue.ID, RefCommentID: 0}).(*Comment) + rp := unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: i1.ID, RefIssueID: pr.Issue.ID, RefCommentID: 0}).(*Comment) c1 := testCreateComment(t, 1, 2, pr.Issue.ID, fmt.Sprintf("closes #%d", i2.Index)) - r1 := db.AssertExistsAndLoadBean(t, &Comment{IssueID: i2.ID, RefIssueID: pr.Issue.ID, RefCommentID: c1.ID}).(*Comment) + r1 := unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: i2.ID, RefIssueID: pr.Issue.ID, RefCommentID: c1.ID}).(*Comment) // Must be ignored c2 := testCreateComment(t, 1, 2, pr.Issue.ID, fmt.Sprintf("mentions #%d", i2.Index)) - db.AssertExistsAndLoadBean(t, &Comment{IssueID: i2.ID, RefIssueID: pr.Issue.ID, RefCommentID: c2.ID}) + unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: i2.ID, RefIssueID: pr.Issue.ID, RefCommentID: c2.ID}) // Must be superseded by c4/r4 c3 := testCreateComment(t, 1, 2, pr.Issue.ID, fmt.Sprintf("reopens #%d", i3.Index)) - db.AssertExistsAndLoadBean(t, &Comment{IssueID: i3.ID, RefIssueID: pr.Issue.ID, RefCommentID: c3.ID}) + unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: i3.ID, RefIssueID: pr.Issue.ID, RefCommentID: c3.ID}) c4 := testCreateComment(t, 1, 2, pr.Issue.ID, fmt.Sprintf("closes #%d", i3.Index)) - r4 := db.AssertExistsAndLoadBean(t, &Comment{IssueID: i3.ID, RefIssueID: pr.Issue.ID, RefCommentID: c4.ID}).(*Comment) + r4 := unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: i3.ID, RefIssueID: pr.Issue.ID, RefCommentID: c4.ID}).(*Comment) refs, err := pr.ResolveCrossReferences() assert.NoError(t, err) @@ -125,8 +125,8 @@ func TestXRef_ResolveCrossReferences(t *testing.T) { } func testCreateIssue(t *testing.T, repo, doer int64, title, content string, ispull bool) *Issue { - r := db.AssertExistsAndLoadBean(t, &Repository{ID: repo}).(*Repository) - d := db.AssertExistsAndLoadBean(t, &User{ID: doer}).(*User) + r := unittest.AssertExistsAndLoadBean(t, &Repository{ID: repo}).(*Repository) + d := unittest.AssertExistsAndLoadBean(t, &User{ID: doer}).(*User) idx, err := db.GetNextResourceIndex("issue_index", r.ID) assert.NoError(t, err) @@ -157,8 +157,8 @@ func testCreateIssue(t *testing.T, repo, doer int64, title, content string, ispu } func testCreatePR(t *testing.T, repo, doer int64, title, content string) *PullRequest { - r := db.AssertExistsAndLoadBean(t, &Repository{ID: repo}).(*Repository) - d := db.AssertExistsAndLoadBean(t, &User{ID: doer}).(*User) + r := unittest.AssertExistsAndLoadBean(t, &Repository{ID: repo}).(*Repository) + d := unittest.AssertExistsAndLoadBean(t, &User{ID: doer}).(*User) i := &Issue{RepoID: r.ID, PosterID: d.ID, Poster: d, Title: title, Content: content, IsPull: true} pr := &PullRequest{HeadRepoID: repo, BaseRepoID: repo, HeadBranch: "head", BaseBranch: "base", Status: PullRequestStatusMergeable} assert.NoError(t, NewPullRequest(r, i, nil, nil, pr)) @@ -167,8 +167,8 @@ func testCreatePR(t *testing.T, repo, doer int64, title, content string) *PullRe } func testCreateComment(t *testing.T, repo, doer, issue int64, content string) *Comment { - d := db.AssertExistsAndLoadBean(t, &User{ID: doer}).(*User) - i := db.AssertExistsAndLoadBean(t, &Issue{ID: issue}).(*Issue) + d := unittest.AssertExistsAndLoadBean(t, &User{ID: doer}).(*User) + i := unittest.AssertExistsAndLoadBean(t, &Issue{ID: issue}).(*Issue) c := &Comment{Type: CommentTypeComment, PosterID: doer, Poster: d, IssueID: issue, Issue: i, Content: content} sess := db.NewSession(db.DefaultContext) diff --git a/models/login/oauth2_application_test.go b/models/login/oauth2_application_test.go index 876de5b52..47e6a27ce 100644 --- a/models/login/oauth2_application_test.go +++ b/models/login/oauth2_application_test.go @@ -7,7 +7,6 @@ package login import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" @@ -17,16 +16,16 @@ import ( func TestOAuth2Application_GenerateClientSecret(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - app := db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application) + app := unittest.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application) secret, err := app.GenerateClientSecret() assert.NoError(t, err) assert.True(t, len(secret) > 0) - db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1, ClientSecret: app.ClientSecret}) + unittest.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1, ClientSecret: app.ClientSecret}) } func BenchmarkOAuth2Application_GenerateClientSecret(b *testing.B) { assert.NoError(b, unittest.PrepareTestDatabase()) - app := db.AssertExistsAndLoadBean(b, &OAuth2Application{ID: 1}).(*OAuth2Application) + app := unittest.AssertExistsAndLoadBean(b, &OAuth2Application{ID: 1}).(*OAuth2Application) for i := 0; i < b.N; i++ { _, _ = app.GenerateClientSecret() } @@ -44,7 +43,7 @@ func TestOAuth2Application_ContainsRedirectURI(t *testing.T) { func TestOAuth2Application_ValidateClientSecret(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - app := db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application) + app := unittest.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application) secret, err := app.GenerateClientSecret() assert.NoError(t, err) assert.True(t, app.ValidateClientSecret([]byte(secret))) @@ -68,7 +67,7 @@ func TestCreateOAuth2Application(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "newapp", app.Name) assert.Len(t, app.ClientID, 36) - db.AssertExistsAndLoadBean(t, &OAuth2Application{Name: "newapp"}) + unittest.AssertExistsAndLoadBean(t, &OAuth2Application{Name: "newapp"}) } func TestOAuth2Application_TableName(t *testing.T) { @@ -77,7 +76,7 @@ func TestOAuth2Application_TableName(t *testing.T) { func TestOAuth2Application_GetGrantByUserID(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - app := db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application) + app := unittest.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application) grant, err := app.GetGrantByUserID(1) assert.NoError(t, err) assert.Equal(t, int64(1), grant.UserID) @@ -89,7 +88,7 @@ func TestOAuth2Application_GetGrantByUserID(t *testing.T) { func TestOAuth2Application_CreateGrant(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - app := db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application) + app := unittest.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application) grant, err := app.CreateGrant(2, "") assert.NoError(t, err) assert.NotNil(t, grant) @@ -113,15 +112,15 @@ func TestGetOAuth2GrantByID(t *testing.T) { func TestOAuth2Grant_IncreaseCounter(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - grant := db.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1, Counter: 1}).(*OAuth2Grant) + grant := unittest.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1, Counter: 1}).(*OAuth2Grant) assert.NoError(t, grant.IncreaseCounter()) assert.Equal(t, int64(2), grant.Counter) - db.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1, Counter: 2}) + unittest.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1, Counter: 2}) } func TestOAuth2Grant_ScopeContains(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - grant := db.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1, Scope: "openid profile"}).(*OAuth2Grant) + grant := unittest.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1, Scope: "openid profile"}).(*OAuth2Grant) assert.True(t, grant.ScopeContains("openid")) assert.True(t, grant.ScopeContains("profile")) assert.False(t, grant.ScopeContains("profil")) @@ -130,7 +129,7 @@ func TestOAuth2Grant_ScopeContains(t *testing.T) { func TestOAuth2Grant_GenerateNewAuthorizationCode(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - grant := db.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1}).(*OAuth2Grant) + grant := unittest.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1}).(*OAuth2Grant) code, err := grant.GenerateNewAuthorizationCode("https://example2.com/callback", "CjvyTLSdR47G5zYenDA-eDWW4lRrO8yvjcWwbD_deOg", "S256") assert.NoError(t, err) assert.NotNil(t, code) @@ -157,7 +156,7 @@ func TestGetOAuth2GrantsByUserID(t *testing.T) { func TestRevokeOAuth2Grant(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, RevokeOAuth2Grant(1, 1)) - db.AssertNotExistsBean(t, &OAuth2Grant{ID: 1, UserID: 1}) + unittest.AssertNotExistsBean(t, &OAuth2Grant{ID: 1, UserID: 1}) } //////////////////// Authorization Code @@ -224,9 +223,9 @@ func TestOAuth2AuthorizationCode_GenerateRedirectURI(t *testing.T) { func TestOAuth2AuthorizationCode_Invalidate(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - code := db.AssertExistsAndLoadBean(t, &OAuth2AuthorizationCode{Code: "authcode"}).(*OAuth2AuthorizationCode) + code := unittest.AssertExistsAndLoadBean(t, &OAuth2AuthorizationCode{Code: "authcode"}).(*OAuth2AuthorizationCode) assert.NoError(t, code.Invalidate()) - db.AssertNotExistsBean(t, &OAuth2AuthorizationCode{Code: "authcode"}) + unittest.AssertNotExistsBean(t, &OAuth2AuthorizationCode{Code: "authcode"}) } func TestOAuth2AuthorizationCode_TableName(t *testing.T) { diff --git a/models/login/u2f_test.go b/models/login/u2f_test.go index 11f20bc79..06a37f813 100644 --- a/models/login/u2f_test.go +++ b/models/login/u2f_test.go @@ -8,7 +8,6 @@ import ( "encoding/hex" "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" @@ -43,18 +42,18 @@ func TestU2FRegistration_TableName(t *testing.T) { func TestU2FRegistration_UpdateCounter(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - reg := db.AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration) + reg := unittest.AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration) reg.Counter = 1 assert.NoError(t, reg.UpdateCounter()) - db.AssertExistsIf(t, true, &U2FRegistration{ID: 1, Counter: 1}) + unittest.AssertExistsIf(t, true, &U2FRegistration{ID: 1, Counter: 1}) } func TestU2FRegistration_UpdateLargeCounter(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - reg := db.AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration) + reg := unittest.AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration) reg.Counter = 0xffffffff assert.NoError(t, reg.UpdateCounter()) - db.AssertExistsIf(t, true, &U2FRegistration{ID: 1, Counter: 0xffffffff}) + unittest.AssertExistsIf(t, true, &U2FRegistration{ID: 1, Counter: 0xffffffff}) } func TestCreateRegistration(t *testing.T) { @@ -65,15 +64,15 @@ func TestCreateRegistration(t *testing.T) { assert.Equal(t, "U2F Created Key", res.Name) assert.Equal(t, []byte("Test"), res.Raw) - db.AssertExistsIf(t, true, &U2FRegistration{Name: "U2F Created Key", UserID: 1}) + unittest.AssertExistsIf(t, true, &U2FRegistration{Name: "U2F Created Key", UserID: 1}) } func TestDeleteRegistration(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - reg := db.AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration) + reg := unittest.AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration) assert.NoError(t, DeleteRegistration(reg)) - db.AssertNotExistsBean(t, &U2FRegistration{ID: 1}) + unittest.AssertNotExistsBean(t, &U2FRegistration{ID: 1}) } const validU2FRegistrationResponseHex = "0504b174bc49c7ca254b70d2e5c207cee9cf174820ebd77ea3c65508c26da51b657c1cc6b952f8621697936482da0a6d3d3826a59095daf6cd7c03e2e60385d2f6d9402a552dfdb7477ed65fd84133f86196010b2215b57da75d315b7b9e8fe2e3925a6019551bab61d16591659cbaf00b4950f7abfe6660e2e006f76868b772d70c253082013c3081e4a003020102020a47901280001155957352300a06082a8648ce3d0403023017311530130603550403130c476e756262792050696c6f74301e170d3132303831343138323933325a170d3133303831343138323933325a3031312f302d0603550403132650696c6f74476e756262792d302e342e312d34373930313238303030313135353935373335323059301306072a8648ce3d020106082a8648ce3d030107034200048d617e65c9508e64bcc5673ac82a6799da3c1446682c258c463fffdf58dfd2fa3e6c378b53d795c4a4dffb4199edd7862f23abaf0203b4b8911ba0569994e101300a06082a8648ce3d0403020347003044022060cdb6061e9c22262d1aac1d96d8c70829b2366531dda268832cb836bcd30dfa0220631b1459f09e6330055722c8d89b7f48883b9089b88d60d1d9795902b30410df304502201471899bcc3987e62e8202c9b39c33c19033f7340352dba80fcab017db9230e402210082677d673d891933ade6f617e5dbde2e247e70423fd5ad7804a6d3d3961ef871" diff --git a/models/main_test.go b/models/main_test.go index 15bece7bf..5c99a2b2f 100644 --- a/models/main_test.go +++ b/models/main_test.go @@ -15,7 +15,7 @@ import ( // TestFixturesAreConsistent assert that test fixtures are consistent func TestFixturesAreConsistent(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - CheckConsistencyFor(t, + unittest.CheckConsistencyFor(t, &User{}, &Repository{}, &Issue{}, diff --git a/models/notification_test.go b/models/notification_test.go index cdba409f3..db2164a2f 100644 --- a/models/notification_test.go +++ b/models/notification_test.go @@ -7,29 +7,28 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) func TestCreateOrUpdateIssueNotifications(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) assert.NoError(t, CreateOrUpdateIssueNotifications(issue.ID, 0, 2, 0)) // User 9 is inactive, thus notifications for user 1 and 4 are created - notf := db.AssertExistsAndLoadBean(t, &Notification{UserID: 1, IssueID: issue.ID}).(*Notification) + notf := unittest.AssertExistsAndLoadBean(t, &Notification{UserID: 1, IssueID: issue.ID}).(*Notification) assert.Equal(t, NotificationStatusUnread, notf.Status) - CheckConsistencyFor(t, &Issue{ID: issue.ID}) + unittest.CheckConsistencyFor(t, &Issue{ID: issue.ID}) - notf = db.AssertExistsAndLoadBean(t, &Notification{UserID: 4, IssueID: issue.ID}).(*Notification) + notf = unittest.AssertExistsAndLoadBean(t, &Notification{UserID: 4, IssueID: issue.ID}).(*Notification) assert.Equal(t, NotificationStatusUnread, notf.Status) } func TestNotificationsForUser(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) statuses := []NotificationStatus{NotificationStatusRead, NotificationStatusUnread} notfs, err := NotificationsForUser(user, statuses, 1, 10) assert.NoError(t, err) @@ -45,7 +44,7 @@ func TestNotificationsForUser(t *testing.T) { func TestNotification_GetRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - notf := db.AssertExistsAndLoadBean(t, &Notification{RepoID: 1}).(*Notification) + notf := unittest.AssertExistsAndLoadBean(t, &Notification{RepoID: 1}).(*Notification) repo, err := notf.GetRepo() assert.NoError(t, err) assert.Equal(t, repo, notf.Repository) @@ -54,7 +53,7 @@ func TestNotification_GetRepo(t *testing.T) { func TestNotification_GetIssue(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - notf := db.AssertExistsAndLoadBean(t, &Notification{RepoID: 1}).(*Notification) + notf := unittest.AssertExistsAndLoadBean(t, &Notification{RepoID: 1}).(*Notification) issue, err := notf.GetIssue() assert.NoError(t, err) assert.Equal(t, issue, notf.Issue) @@ -63,7 +62,7 @@ func TestNotification_GetIssue(t *testing.T) { func TestGetNotificationCount(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) cnt, err := GetNotificationCount(user, NotificationStatusRead) assert.NoError(t, err) assert.EqualValues(t, 0, cnt) @@ -75,34 +74,34 @@ func TestGetNotificationCount(t *testing.T) { func TestSetNotificationStatus(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - notf := db.AssertExistsAndLoadBean(t, + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + notf := unittest.AssertExistsAndLoadBean(t, &Notification{UserID: user.ID, Status: NotificationStatusRead}).(*Notification) _, err := SetNotificationStatus(notf.ID, user, NotificationStatusPinned) assert.NoError(t, err) - db.AssertExistsAndLoadBean(t, + unittest.AssertExistsAndLoadBean(t, &Notification{ID: notf.ID, Status: NotificationStatusPinned}) _, err = SetNotificationStatus(1, user, NotificationStatusRead) assert.Error(t, err) - _, err = SetNotificationStatus(db.NonexistentID, user, NotificationStatusRead) + _, err = SetNotificationStatus(unittest.NonexistentID, user, NotificationStatusRead) assert.Error(t, err) } func TestUpdateNotificationStatuses(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - notfUnread := db.AssertExistsAndLoadBean(t, + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + notfUnread := unittest.AssertExistsAndLoadBean(t, &Notification{UserID: user.ID, Status: NotificationStatusUnread}).(*Notification) - notfRead := db.AssertExistsAndLoadBean(t, + notfRead := unittest.AssertExistsAndLoadBean(t, &Notification{UserID: user.ID, Status: NotificationStatusRead}).(*Notification) - notfPinned := db.AssertExistsAndLoadBean(t, + notfPinned := unittest.AssertExistsAndLoadBean(t, &Notification{UserID: user.ID, Status: NotificationStatusPinned}).(*Notification) assert.NoError(t, UpdateNotificationStatuses(user, NotificationStatusUnread, NotificationStatusRead)) - db.AssertExistsAndLoadBean(t, + unittest.AssertExistsAndLoadBean(t, &Notification{ID: notfUnread.ID, Status: NotificationStatusRead}) - db.AssertExistsAndLoadBean(t, + unittest.AssertExistsAndLoadBean(t, &Notification{ID: notfRead.ID, Status: NotificationStatusRead}) - db.AssertExistsAndLoadBean(t, + unittest.AssertExistsAndLoadBean(t, &Notification{ID: notfPinned.ID, Status: NotificationStatusPinned}) } diff --git a/models/org_team_test.go b/models/org_team_test.go index 114c8016e..9277ac4f5 100644 --- a/models/org_team_test.go +++ b/models/org_team_test.go @@ -8,7 +8,6 @@ import ( "strings" "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -16,36 +15,36 @@ import ( func TestTeam_IsOwnerTeam(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - team := db.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) assert.True(t, team.IsOwnerTeam()) - team = db.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) + team = unittest.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) assert.False(t, team.IsOwnerTeam()) } func TestTeam_IsMember(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - team := db.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) assert.True(t, team.IsMember(2)) assert.False(t, team.IsMember(4)) - assert.False(t, team.IsMember(db.NonexistentID)) + assert.False(t, team.IsMember(unittest.NonexistentID)) - team = db.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) + team = unittest.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) assert.True(t, team.IsMember(2)) assert.True(t, team.IsMember(4)) - assert.False(t, team.IsMember(db.NonexistentID)) + assert.False(t, team.IsMember(unittest.NonexistentID)) } func TestTeam_GetRepositories(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(teamID int64) { - team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.NoError(t, team.GetRepositories(&SearchTeamOptions{})) assert.Len(t, team.Repos, team.NumRepos) for _, repo := range team.Repos { - db.AssertExistsAndLoadBean(t, &TeamRepo{TeamID: teamID, RepoID: repo.ID}) + unittest.AssertExistsAndLoadBean(t, &TeamRepo{TeamID: teamID, RepoID: repo.ID}) } } test(1) @@ -56,11 +55,11 @@ func TestTeam_GetMembers(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(teamID int64) { - team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.NoError(t, team.GetMembers(&SearchMembersOptions{})) assert.Len(t, team.Members, team.NumMembers) for _, member := range team.Members { - db.AssertExistsAndLoadBean(t, &TeamUser{UID: member.ID, TeamID: teamID}) + unittest.AssertExistsAndLoadBean(t, &TeamUser{UID: member.ID, TeamID: teamID}) } } test(1) @@ -71,10 +70,10 @@ func TestTeam_AddMember(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(teamID, userID int64) { - team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.NoError(t, team.AddMember(userID)) - db.AssertExistsAndLoadBean(t, &TeamUser{UID: userID, TeamID: teamID}) - CheckConsistencyFor(t, &Team{ID: teamID}, &User{ID: team.OrgID}) + unittest.AssertExistsAndLoadBean(t, &TeamUser{UID: userID, TeamID: teamID}) + unittest.CheckConsistencyFor(t, &Team{ID: teamID}, &User{ID: team.OrgID}) } test(1, 2) test(1, 4) @@ -85,17 +84,17 @@ func TestTeam_RemoveMember(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(teamID, userID int64) { - team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.NoError(t, team.RemoveMember(userID)) - db.AssertNotExistsBean(t, &TeamUser{UID: userID, TeamID: teamID}) - CheckConsistencyFor(t, &Team{ID: teamID}) + unittest.AssertNotExistsBean(t, &TeamUser{UID: userID, TeamID: teamID}) + unittest.CheckConsistencyFor(t, &Team{ID: teamID}) } testSuccess(1, 4) testSuccess(2, 2) testSuccess(3, 2) - testSuccess(3, db.NonexistentID) + testSuccess(3, unittest.NonexistentID) - team := db.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) err := team.RemoveMember(2) assert.True(t, IsErrLastOrgOwner(err)) } @@ -104,13 +103,13 @@ func TestTeam_HasRepository(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(teamID, repoID int64, expected bool) { - team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.Equal(t, expected, team.HasRepository(repoID)) } test(1, 1, false) test(1, 3, true) test(1, 5, true) - test(1, db.NonexistentID, false) + test(1, unittest.NonexistentID, false) test(2, 3, true) test(2, 5, false) @@ -120,33 +119,33 @@ func TestTeam_AddRepository(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(teamID, repoID int64) { - team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) assert.NoError(t, team.AddRepository(repo)) - db.AssertExistsAndLoadBean(t, &TeamRepo{TeamID: teamID, RepoID: repoID}) - CheckConsistencyFor(t, &Team{ID: teamID}, &Repository{ID: repoID}) + unittest.AssertExistsAndLoadBean(t, &TeamRepo{TeamID: teamID, RepoID: repoID}) + unittest.CheckConsistencyFor(t, &Team{ID: teamID}, &Repository{ID: repoID}) } testSuccess(2, 3) testSuccess(2, 5) - team := db.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) assert.Error(t, team.AddRepository(repo)) - CheckConsistencyFor(t, &Team{ID: 1}, &Repository{ID: 1}) + unittest.CheckConsistencyFor(t, &Team{ID: 1}, &Repository{ID: 1}) } func TestTeam_RemoveRepository(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(teamID, repoID int64) { - team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.NoError(t, team.RemoveRepository(repoID)) - db.AssertNotExistsBean(t, &TeamRepo{TeamID: teamID, RepoID: repoID}) - CheckConsistencyFor(t, &Team{ID: teamID}, &Repository{ID: repoID}) + unittest.AssertNotExistsBean(t, &TeamRepo{TeamID: teamID, RepoID: repoID}) + unittest.CheckConsistencyFor(t, &Team{ID: teamID}, &Repository{ID: repoID}) } testSuccess(2, 3) testSuccess(2, 5) - testSuccess(1, db.NonexistentID) + testSuccess(1, unittest.NonexistentID) } func TestIsUsableTeamName(t *testing.T) { @@ -160,8 +159,8 @@ func TestNewTeam(t *testing.T) { const teamName = "newTeamName" team := &Team{Name: teamName, OrgID: 3} assert.NoError(t, NewTeam(team)) - db.AssertExistsAndLoadBean(t, &Team{Name: teamName}) - CheckConsistencyFor(t, &Team{}, &User{ID: team.OrgID}) + unittest.AssertExistsAndLoadBean(t, &Team{Name: teamName}) + unittest.CheckConsistencyFor(t, &Team{}, &User{ID: team.OrgID}) } func TestGetTeam(t *testing.T) { @@ -178,7 +177,7 @@ func TestGetTeam(t *testing.T) { _, err := GetTeam(3, "nonexistent") assert.Error(t, err) - _, err = GetTeam(db.NonexistentID, "Owners") + _, err = GetTeam(unittest.NonexistentID, "Owners") assert.Error(t, err) } @@ -195,7 +194,7 @@ func TestGetTeamByID(t *testing.T) { testSuccess(3) testSuccess(4) - _, err := GetTeamByID(db.NonexistentID) + _, err := GetTeamByID(unittest.NonexistentID) assert.Error(t, err) } @@ -203,48 +202,48 @@ func TestUpdateTeam(t *testing.T) { // successful update assert.NoError(t, unittest.PrepareTestDatabase()) - team := db.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) team.LowerName = "newname" team.Name = "newName" team.Description = strings.Repeat("A long description!", 100) team.Authorize = AccessModeAdmin assert.NoError(t, UpdateTeam(team, true, false)) - team = db.AssertExistsAndLoadBean(t, &Team{Name: "newName"}).(*Team) + team = unittest.AssertExistsAndLoadBean(t, &Team{Name: "newName"}).(*Team) assert.True(t, strings.HasPrefix(team.Description, "A long description!")) - access := db.AssertExistsAndLoadBean(t, &Access{UserID: 4, RepoID: 3}).(*Access) + access := unittest.AssertExistsAndLoadBean(t, &Access{UserID: 4, RepoID: 3}).(*Access) assert.EqualValues(t, AccessModeAdmin, access.Mode) - CheckConsistencyFor(t, &Team{ID: team.ID}) + unittest.CheckConsistencyFor(t, &Team{ID: team.ID}) } func TestUpdateTeam2(t *testing.T) { // update to already-existing team assert.NoError(t, unittest.PrepareTestDatabase()) - team := db.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) team.LowerName = "owners" team.Name = "Owners" team.Description = strings.Repeat("A long description!", 100) err := UpdateTeam(team, true, false) assert.True(t, IsErrTeamAlreadyExist(err)) - CheckConsistencyFor(t, &Team{ID: team.ID}) + unittest.CheckConsistencyFor(t, &Team{ID: team.ID}) } func TestDeleteTeam(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - team := db.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team) assert.NoError(t, DeleteTeam(team)) - db.AssertNotExistsBean(t, &Team{ID: team.ID}) - db.AssertNotExistsBean(t, &TeamRepo{TeamID: team.ID}) - db.AssertNotExistsBean(t, &TeamUser{TeamID: team.ID}) + unittest.AssertNotExistsBean(t, &Team{ID: team.ID}) + unittest.AssertNotExistsBean(t, &TeamRepo{TeamID: team.ID}) + unittest.AssertNotExistsBean(t, &TeamUser{TeamID: team.ID}) // check that team members don't have "leftover" access to repos - user := db.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) accessMode, err := AccessLevel(user, repo) assert.NoError(t, err) assert.True(t, accessMode < AccessModeWrite) @@ -260,25 +259,25 @@ func TestIsTeamMember(t *testing.T) { test(3, 1, 2, true) test(3, 1, 4, false) - test(3, 1, db.NonexistentID, false) + test(3, 1, unittest.NonexistentID, false) test(3, 2, 2, true) test(3, 2, 4, true) - test(3, db.NonexistentID, db.NonexistentID, false) - test(db.NonexistentID, db.NonexistentID, db.NonexistentID, false) + test(3, unittest.NonexistentID, unittest.NonexistentID, false) + test(unittest.NonexistentID, unittest.NonexistentID, unittest.NonexistentID, false) } func TestGetTeamMembers(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(teamID int64) { - team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) members, err := GetTeamMembers(teamID) assert.NoError(t, err) assert.Len(t, members, team.NumMembers) for _, member := range members { - db.AssertExistsAndLoadBean(t, &TeamUser{UID: member.ID, TeamID: teamID}) + unittest.AssertExistsAndLoadBean(t, &TeamUser{UID: member.ID, TeamID: teamID}) } } test(1) @@ -291,12 +290,12 @@ func TestGetUserTeams(t *testing.T) { teams, _, err := SearchTeam(&SearchTeamOptions{UserID: userID}) assert.NoError(t, err) for _, team := range teams { - db.AssertExistsAndLoadBean(t, &TeamUser{TeamID: team.ID, UID: userID}) + unittest.AssertExistsAndLoadBean(t, &TeamUser{TeamID: team.ID, UID: userID}) } } test(2) test(5) - test(db.NonexistentID) + test(unittest.NonexistentID) } func TestGetUserOrgTeams(t *testing.T) { @@ -306,22 +305,22 @@ func TestGetUserOrgTeams(t *testing.T) { assert.NoError(t, err) for _, team := range teams { assert.EqualValues(t, orgID, team.OrgID) - db.AssertExistsAndLoadBean(t, &TeamUser{TeamID: team.ID, UID: userID}) + unittest.AssertExistsAndLoadBean(t, &TeamUser{TeamID: team.ID, UID: userID}) } } test(3, 2) test(3, 4) - test(3, db.NonexistentID) + test(3, unittest.NonexistentID) } func TestAddTeamMember(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(teamID, userID int64) { - team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.NoError(t, AddTeamMember(team, userID)) - db.AssertExistsAndLoadBean(t, &TeamUser{UID: userID, TeamID: teamID}) - CheckConsistencyFor(t, &Team{ID: teamID}, &User{ID: team.OrgID}) + unittest.AssertExistsAndLoadBean(t, &TeamUser{UID: userID, TeamID: teamID}) + unittest.CheckConsistencyFor(t, &Team{ID: teamID}, &User{ID: team.OrgID}) } test(1, 2) test(1, 4) @@ -332,17 +331,17 @@ func TestRemoveTeamMember(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(teamID, userID int64) { - team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.NoError(t, RemoveTeamMember(team, userID)) - db.AssertNotExistsBean(t, &TeamUser{UID: userID, TeamID: teamID}) - CheckConsistencyFor(t, &Team{ID: teamID}) + unittest.AssertNotExistsBean(t, &TeamUser{UID: userID, TeamID: teamID}) + unittest.CheckConsistencyFor(t, &Team{ID: teamID}) } testSuccess(1, 4) testSuccess(2, 2) testSuccess(3, 2) - testSuccess(3, db.NonexistentID) + testSuccess(3, unittest.NonexistentID) - team := db.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team) err := RemoveTeamMember(team, 2) assert.True(t, IsErrLastOrgOwner(err)) } @@ -351,13 +350,13 @@ func TestHasTeamRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(teamID, repoID int64, expected bool) { - team := db.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team) assert.Equal(t, expected, HasTeamRepo(team.OrgID, teamID, repoID)) } test(1, 1, false) test(1, 3, true) test(1, 5, true) - test(1, db.NonexistentID, false) + test(1, unittest.NonexistentID, false) test(2, 3, true) test(2, 5, false) diff --git a/models/org_test.go b/models/org_test.go index 9557e14ed..2bc4d74fc 100644 --- a/models/org_test.go +++ b/models/org_test.go @@ -29,7 +29,7 @@ func TestUser_IsOwnedBy(t *testing.T) { {2, 2, false}, // user2 is not an organization {2, 3, false}, } { - org := db.AssertExistsAndLoadBean(t, &User{ID: testCase.OrgID}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: testCase.OrgID}).(*User) isOwner, err := org.IsOwnedBy(testCase.UserID) assert.NoError(t, err) assert.Equal(t, testCase.ExpectedOwner, isOwner) @@ -50,7 +50,7 @@ func TestUser_IsOrgMember(t *testing.T) { {2, 2, false}, // user2 is not an organization {2, 3, false}, } { - org := db.AssertExistsAndLoadBean(t, &User{ID: testCase.OrgID}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: testCase.OrgID}).(*User) isMember, err := org.IsOrgMember(testCase.UserID) assert.NoError(t, err) assert.Equal(t, testCase.ExpectedMember, isMember) @@ -59,7 +59,7 @@ func TestUser_IsOrgMember(t *testing.T) { func TestUser_GetTeam(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) team, err := org.GetTeam("team1") assert.NoError(t, err) assert.Equal(t, org.ID, team.OrgID) @@ -68,26 +68,26 @@ func TestUser_GetTeam(t *testing.T) { _, err = org.GetTeam("does not exist") assert.True(t, IsErrTeamNotExist(err)) - nonOrg := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + nonOrg := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) _, err = nonOrg.GetTeam("team") assert.True(t, IsErrTeamNotExist(err)) } func TestUser_GetOwnerTeam(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) team, err := org.GetOwnerTeam() assert.NoError(t, err) assert.Equal(t, org.ID, team.OrgID) - nonOrg := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + nonOrg := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) _, err = nonOrg.GetOwnerTeam() assert.True(t, IsErrTeamNotExist(err)) } func TestUser_GetTeams(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) assert.NoError(t, org.LoadTeams()) if assert.Len(t, org.Teams, 4) { assert.Equal(t, int64(1), org.Teams[0].ID) @@ -99,7 +99,7 @@ func TestUser_GetTeams(t *testing.T) { func TestUser_GetMembers(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) assert.NoError(t, org.GetMembers()) if assert.Len(t, org.Members, 3) { assert.Equal(t, int64(2), org.Members[0].ID) @@ -110,68 +110,68 @@ func TestUser_GetMembers(t *testing.T) { func TestUser_AddMember(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) // add a user that is not a member - db.AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3}) + unittest.AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3}) prevNumMembers := org.NumMembers assert.NoError(t, org.AddMember(5)) - db.AssertExistsAndLoadBean(t, &OrgUser{UID: 5, OrgID: 3}) - org = db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + unittest.AssertExistsAndLoadBean(t, &OrgUser{UID: 5, OrgID: 3}) + org = unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) assert.Equal(t, prevNumMembers+1, org.NumMembers) // add a user that is already a member - db.AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3}) + unittest.AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3}) prevNumMembers = org.NumMembers assert.NoError(t, org.AddMember(4)) - db.AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3}) - org = db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + unittest.AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3}) + org = unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) assert.Equal(t, prevNumMembers, org.NumMembers) - CheckConsistencyFor(t, &User{}) + unittest.CheckConsistencyFor(t, &User{}) } func TestUser_RemoveMember(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) // remove a user that is a member - db.AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3}) + unittest.AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3}) prevNumMembers := org.NumMembers assert.NoError(t, org.RemoveMember(4)) - db.AssertNotExistsBean(t, &OrgUser{UID: 4, OrgID: 3}) - org = db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + unittest.AssertNotExistsBean(t, &OrgUser{UID: 4, OrgID: 3}) + org = unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) assert.Equal(t, prevNumMembers-1, org.NumMembers) // remove a user that is not a member - db.AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3}) + unittest.AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3}) prevNumMembers = org.NumMembers assert.NoError(t, org.RemoveMember(5)) - db.AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3}) - org = db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + unittest.AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3}) + org = unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) assert.Equal(t, prevNumMembers, org.NumMembers) - CheckConsistencyFor(t, &User{}, &Team{}) + unittest.CheckConsistencyFor(t, &User{}, &Team{}) } func TestUser_RemoveOrgRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) - repo := db.AssertExistsAndLoadBean(t, &Repository{OwnerID: org.ID}).(*Repository) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{OwnerID: org.ID}).(*Repository) // remove a repo that does belong to org - db.AssertExistsAndLoadBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID}) + unittest.AssertExistsAndLoadBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID}) assert.NoError(t, org.RemoveOrgRepo(repo.ID)) - db.AssertNotExistsBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID}) - db.AssertExistsAndLoadBean(t, &Repository{ID: repo.ID}) // repo should still exist + unittest.AssertNotExistsBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID}) + unittest.AssertExistsAndLoadBean(t, &Repository{ID: repo.ID}) // repo should still exist // remove a repo that does not belong to org assert.NoError(t, org.RemoveOrgRepo(repo.ID)) - db.AssertNotExistsBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID}) + unittest.AssertNotExistsBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID}) - assert.NoError(t, org.RemoveOrgRepo(db.NonexistentID)) + assert.NoError(t, org.RemoveOrgRepo(unittest.NonexistentID)) - CheckConsistencyFor(t, + unittest.CheckConsistencyFor(t, &User{ID: org.ID}, &Team{OrgID: org.ID}, &Repository{ID: repo.ID}) @@ -181,62 +181,62 @@ func TestCreateOrganization(t *testing.T) { // successful creation of org assert.NoError(t, unittest.PrepareTestDatabase()) - owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) const newOrgName = "neworg" org := &User{ Name: newOrgName, } - db.AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization}) + unittest.AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization}) assert.NoError(t, CreateOrganization(org, owner)) - org = db.AssertExistsAndLoadBean(t, + org = unittest.AssertExistsAndLoadBean(t, &User{Name: newOrgName, Type: UserTypeOrganization}).(*User) - ownerTeam := db.AssertExistsAndLoadBean(t, + ownerTeam := unittest.AssertExistsAndLoadBean(t, &Team{Name: ownerTeamName, OrgID: org.ID}).(*Team) - db.AssertExistsAndLoadBean(t, &TeamUser{UID: owner.ID, TeamID: ownerTeam.ID}) - CheckConsistencyFor(t, &User{}, &Team{}) + unittest.AssertExistsAndLoadBean(t, &TeamUser{UID: owner.ID, TeamID: ownerTeam.ID}) + unittest.CheckConsistencyFor(t, &User{}, &Team{}) } func TestCreateOrganization2(t *testing.T) { // unauthorized creation of org assert.NoError(t, unittest.PrepareTestDatabase()) - owner := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) const newOrgName = "neworg" org := &User{ Name: newOrgName, } - db.AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization}) + unittest.AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization}) err := CreateOrganization(org, owner) assert.Error(t, err) assert.True(t, IsErrUserNotAllowedCreateOrg(err)) - db.AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization}) - CheckConsistencyFor(t, &User{}, &Team{}) + unittest.AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization}) + unittest.CheckConsistencyFor(t, &User{}, &Team{}) } func TestCreateOrganization3(t *testing.T) { // create org with same name as existent org assert.NoError(t, unittest.PrepareTestDatabase()) - owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - org := &User{Name: "user3"} // should already exist - db.AssertExistsAndLoadBean(t, &User{Name: org.Name}) // sanity check + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + org := &User{Name: "user3"} // should already exist + unittest.AssertExistsAndLoadBean(t, &User{Name: org.Name}) // sanity check err := CreateOrganization(org, owner) assert.Error(t, err) assert.True(t, IsErrUserAlreadyExist(err)) - CheckConsistencyFor(t, &User{}, &Team{}) + unittest.CheckConsistencyFor(t, &User{}, &Team{}) } func TestCreateOrganization4(t *testing.T) { // create org with unusable name assert.NoError(t, unittest.PrepareTestDatabase()) - owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) err := CreateOrganization(&User{Name: "assets"}, owner) assert.Error(t, err) assert.True(t, IsErrNameReserved(err)) - CheckConsistencyFor(t, &User{}, &Team{}) + unittest.CheckConsistencyFor(t, &User{}, &Team{}) } func TestGetOrgByName(t *testing.T) { @@ -263,20 +263,20 @@ func TestCountOrganizations(t *testing.T) { func TestDeleteOrganization(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 6}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 6}).(*User) assert.NoError(t, DeleteOrganization(org)) - db.AssertNotExistsBean(t, &User{ID: 6}) - db.AssertNotExistsBean(t, &OrgUser{OrgID: 6}) - db.AssertNotExistsBean(t, &Team{OrgID: 6}) + unittest.AssertNotExistsBean(t, &User{ID: 6}) + unittest.AssertNotExistsBean(t, &OrgUser{OrgID: 6}) + unittest.AssertNotExistsBean(t, &Team{OrgID: 6}) - org = db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org = unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) err := DeleteOrganization(org) assert.Error(t, err) assert.True(t, IsErrUserOwnRepos(err)) - user := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) assert.Error(t, DeleteOrganization(user)) - CheckConsistencyFor(t, &User{}, &Team{}) + unittest.CheckConsistencyFor(t, &User{}, &Team{}) } func TestIsOrganizationOwner(t *testing.T) { @@ -290,7 +290,7 @@ func TestIsOrganizationOwner(t *testing.T) { test(3, 3, false) test(6, 5, true) test(6, 4, false) - test(db.NonexistentID, db.NonexistentID, false) + test(unittest.NonexistentID, unittest.NonexistentID, false) } func TestIsOrganizationMember(t *testing.T) { @@ -305,7 +305,7 @@ func TestIsOrganizationMember(t *testing.T) { test(3, 4, true) test(6, 5, true) test(6, 4, false) - test(db.NonexistentID, db.NonexistentID, false) + test(unittest.NonexistentID, unittest.NonexistentID, false) } func TestIsPublicMembership(t *testing.T) { @@ -320,7 +320,7 @@ func TestIsPublicMembership(t *testing.T) { test(3, 4, false) test(6, 5, true) test(6, 4, false) - test(db.NonexistentID, db.NonexistentID, false) + test(unittest.NonexistentID, unittest.NonexistentID, false) } func TestGetOrgsByUserID(t *testing.T) { @@ -422,7 +422,7 @@ func TestGetOrgUsersByOrgID(t *testing.T) { orgUsers, err = GetOrgUsersByOrgID(&FindOrgMembersOpts{ ListOptions: db.ListOptions{}, - OrgID: db.NonexistentID, + OrgID: unittest.NonexistentID, PublicOnly: false, }) assert.NoError(t, err) @@ -434,29 +434,29 @@ func TestChangeOrgUserStatus(t *testing.T) { testSuccess := func(orgID, userID int64, public bool) { assert.NoError(t, ChangeOrgUserStatus(orgID, userID, public)) - orgUser := db.AssertExistsAndLoadBean(t, &OrgUser{OrgID: orgID, UID: userID}).(*OrgUser) + orgUser := unittest.AssertExistsAndLoadBean(t, &OrgUser{OrgID: orgID, UID: userID}).(*OrgUser) assert.Equal(t, public, orgUser.IsPublic) } testSuccess(3, 2, false) testSuccess(3, 2, false) testSuccess(3, 4, true) - assert.NoError(t, ChangeOrgUserStatus(db.NonexistentID, db.NonexistentID, true)) + assert.NoError(t, ChangeOrgUserStatus(unittest.NonexistentID, unittest.NonexistentID, true)) } func TestAddOrgUser(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(orgID, userID int64, isPublic bool) { - org := db.AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User) expectedNumMembers := org.NumMembers - if !db.BeanExists(t, &OrgUser{OrgID: orgID, UID: userID}) { + if !unittest.BeanExists(t, &OrgUser{OrgID: orgID, UID: userID}) { expectedNumMembers++ } assert.NoError(t, AddOrgUser(orgID, userID)) ou := &OrgUser{OrgID: orgID, UID: userID} - db.AssertExistsAndLoadBean(t, ou) + unittest.AssertExistsAndLoadBean(t, ou) assert.Equal(t, isPublic, ou.IsPublic) - org = db.AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User) + org = unittest.AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User) assert.EqualValues(t, expectedNumMembers, org.NumMembers) } @@ -468,20 +468,20 @@ func TestAddOrgUser(t *testing.T) { setting.Service.DefaultOrgMemberVisible = true testSuccess(6, 3, true) - CheckConsistencyFor(t, &User{}, &Team{}) + unittest.CheckConsistencyFor(t, &User{}, &Team{}) } func TestRemoveOrgUser(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(orgID, userID int64) { - org := db.AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User) expectedNumMembers := org.NumMembers - if db.BeanExists(t, &OrgUser{OrgID: orgID, UID: userID}) { + if unittest.BeanExists(t, &OrgUser{OrgID: orgID, UID: userID}) { expectedNumMembers-- } assert.NoError(t, RemoveOrgUser(orgID, userID)) - db.AssertNotExistsBean(t, &OrgUser{OrgID: orgID, UID: userID}) - org = db.AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User) + unittest.AssertNotExistsBean(t, &OrgUser{OrgID: orgID, UID: userID}) + org = unittest.AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User) assert.EqualValues(t, expectedNumMembers, org.NumMembers) } testSuccess(3, 4) @@ -490,13 +490,13 @@ func TestRemoveOrgUser(t *testing.T) { err := RemoveOrgUser(7, 5) assert.Error(t, err) assert.True(t, IsErrLastOrgOwner(err)) - db.AssertExistsAndLoadBean(t, &OrgUser{OrgID: 7, UID: 5}) - CheckConsistencyFor(t, &User{}, &Team{}) + unittest.AssertExistsAndLoadBean(t, &OrgUser{OrgID: 7, UID: 5}) + unittest.CheckConsistencyFor(t, &User{}, &Team{}) } func TestUser_GetUserTeamIDs(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) testSuccess := func(userID int64, expected []int64) { teamIDs, err := org.GetUserTeamIDs(userID) assert.NoError(t, err) @@ -504,12 +504,12 @@ func TestUser_GetUserTeamIDs(t *testing.T) { } testSuccess(2, []int64{1, 2}) testSuccess(4, []int64{2}) - testSuccess(db.NonexistentID, []int64{}) + testSuccess(unittest.NonexistentID, []int64{}) } func TestAccessibleReposEnv_CountRepos(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) testSuccess := func(userID, expectedCount int64) { env, err := org.AccessibleReposEnv(userID) assert.NoError(t, err) @@ -523,7 +523,7 @@ func TestAccessibleReposEnv_CountRepos(t *testing.T) { func TestAccessibleReposEnv_RepoIDs(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) testSuccess := func(userID, _, pageSize int64, expectedRepoIDs []int64) { env, err := org.AccessibleReposEnv(userID) assert.NoError(t, err) @@ -537,7 +537,7 @@ func TestAccessibleReposEnv_RepoIDs(t *testing.T) { func TestAccessibleReposEnv_Repos(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) testSuccess := func(userID int64, expectedRepoIDs []int64) { env, err := org.AccessibleReposEnv(userID) assert.NoError(t, err) @@ -545,7 +545,7 @@ func TestAccessibleReposEnv_Repos(t *testing.T) { assert.NoError(t, err) expectedRepos := make([]*Repository, len(expectedRepoIDs)) for i, repoID := range expectedRepoIDs { - expectedRepos[i] = db.AssertExistsAndLoadBean(t, + expectedRepos[i] = unittest.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) } assert.Equal(t, expectedRepos, repos) @@ -556,7 +556,7 @@ func TestAccessibleReposEnv_Repos(t *testing.T) { func TestAccessibleReposEnv_MirrorRepos(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) testSuccess := func(userID int64, expectedRepoIDs []int64) { env, err := org.AccessibleReposEnv(userID) assert.NoError(t, err) @@ -564,7 +564,7 @@ func TestAccessibleReposEnv_MirrorRepos(t *testing.T) { assert.NoError(t, err) expectedRepos := make([]*Repository, len(expectedRepoIDs)) for i, repoID := range expectedRepoIDs { - expectedRepos[i] = db.AssertExistsAndLoadBean(t, + expectedRepos[i] = unittest.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) } assert.Equal(t, expectedRepos, repos) @@ -575,8 +575,8 @@ func TestAccessibleReposEnv_MirrorRepos(t *testing.T) { func TestHasOrgVisibleTypePublic(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - user3 := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user3 := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) const newOrgName = "test-org-public" org := &User{ @@ -584,9 +584,9 @@ func TestHasOrgVisibleTypePublic(t *testing.T) { Visibility: structs.VisibleTypePublic, } - db.AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization}) + unittest.AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization}) assert.NoError(t, CreateOrganization(org, owner)) - org = db.AssertExistsAndLoadBean(t, + org = unittest.AssertExistsAndLoadBean(t, &User{Name: org.Name, Type: UserTypeOrganization}).(*User) test1 := HasOrgOrUserVisible(org, owner) test2 := HasOrgOrUserVisible(org, user3) @@ -598,8 +598,8 @@ func TestHasOrgVisibleTypePublic(t *testing.T) { func TestHasOrgVisibleTypeLimited(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - user3 := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user3 := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) const newOrgName = "test-org-limited" org := &User{ @@ -607,9 +607,9 @@ func TestHasOrgVisibleTypeLimited(t *testing.T) { Visibility: structs.VisibleTypeLimited, } - db.AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization}) + unittest.AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization}) assert.NoError(t, CreateOrganization(org, owner)) - org = db.AssertExistsAndLoadBean(t, + org = unittest.AssertExistsAndLoadBean(t, &User{Name: org.Name, Type: UserTypeOrganization}).(*User) test1 := HasOrgOrUserVisible(org, owner) test2 := HasOrgOrUserVisible(org, user3) @@ -621,8 +621,8 @@ func TestHasOrgVisibleTypeLimited(t *testing.T) { func TestHasOrgVisibleTypePrivate(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - user3 := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user3 := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) const newOrgName = "test-org-private" org := &User{ @@ -630,9 +630,9 @@ func TestHasOrgVisibleTypePrivate(t *testing.T) { Visibility: structs.VisibleTypePrivate, } - db.AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization}) + unittest.AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization}) assert.NoError(t, CreateOrganization(org, owner)) - org = db.AssertExistsAndLoadBean(t, + org = unittest.AssertExistsAndLoadBean(t, &User{Name: org.Name, Type: UserTypeOrganization}).(*User) test1 := HasOrgOrUserVisible(org, owner) test2 := HasOrgOrUserVisible(org, user3) diff --git a/models/pull_test.go b/models/pull_test.go index 54083355a..0225b421f 100644 --- a/models/pull_test.go +++ b/models/pull_test.go @@ -15,7 +15,7 @@ import ( func TestPullRequest_LoadAttributes(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) + pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) assert.NoError(t, pr.LoadAttributes()) assert.NotNil(t, pr.Merger) assert.Equal(t, pr.MergerID, pr.Merger.ID) @@ -23,7 +23,7 @@ func TestPullRequest_LoadAttributes(t *testing.T) { func TestPullRequest_LoadIssue(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) + pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) assert.NoError(t, pr.LoadIssue()) assert.NotNil(t, pr.Issue) assert.Equal(t, int64(2), pr.Issue.ID) @@ -34,7 +34,7 @@ func TestPullRequest_LoadIssue(t *testing.T) { func TestPullRequest_LoadBaseRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) + pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) assert.NoError(t, pr.LoadBaseRepo()) assert.NotNil(t, pr.BaseRepo) assert.Equal(t, pr.BaseRepoID, pr.BaseRepo.ID) @@ -45,7 +45,7 @@ func TestPullRequest_LoadBaseRepo(t *testing.T) { func TestPullRequest_LoadHeadRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) + pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) assert.NoError(t, pr.LoadHeadRepo()) assert.NotNil(t, pr.HeadRepo) assert.Equal(t, pr.HeadRepoID, pr.HeadRepo.ID) @@ -167,15 +167,15 @@ func TestGetPullRequestByIssueID(t *testing.T) { func TestPullRequest_Update(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) + pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) pr.BaseBranch = "baseBranch" pr.HeadBranch = "headBranch" pr.Update() - pr = db.AssertExistsAndLoadBean(t, &PullRequest{ID: pr.ID}).(*PullRequest) + pr = unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: pr.ID}).(*PullRequest) assert.Equal(t, "baseBranch", pr.BaseBranch) assert.Equal(t, "headBranch", pr.HeadBranch) - CheckConsistencyFor(t, pr) + unittest.CheckConsistencyFor(t, pr) } func TestPullRequest_UpdateCols(t *testing.T) { @@ -187,18 +187,18 @@ func TestPullRequest_UpdateCols(t *testing.T) { } assert.NoError(t, pr.UpdateCols("head_branch")) - pr = db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) + pr = unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) assert.Equal(t, "master", pr.BaseBranch) assert.Equal(t, "headBranch", pr.HeadBranch) - CheckConsistencyFor(t, pr) + unittest.CheckConsistencyFor(t, pr) } func TestPullRequestList_LoadAttributes(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) prs := []*PullRequest{ - db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest), - db.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest), + unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest), + unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest), } assert.NoError(t, PullRequestList(prs).LoadAttributes()) for _, pr := range prs { @@ -214,7 +214,7 @@ func TestPullRequestList_LoadAttributes(t *testing.T) { func TestPullRequest_IsWorkInProgress(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) + pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) pr.LoadIssue() assert.False(t, pr.IsWorkInProgress()) @@ -229,7 +229,7 @@ func TestPullRequest_IsWorkInProgress(t *testing.T) { func TestPullRequest_GetWorkInProgressPrefixWorkInProgress(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) + pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) pr.LoadIssue() assert.Empty(t, pr.GetWorkInProgressPrefix()) @@ -244,7 +244,7 @@ func TestPullRequest_GetWorkInProgressPrefixWorkInProgress(t *testing.T) { func TestPullRequest_GetDefaultMergeMessage_InternalTracker(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) + pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) assert.Equal(t, "Merge pull request 'issue3' (#3) from branch2 into master", pr.GetDefaultMergeMessage()) @@ -266,7 +266,7 @@ func TestPullRequest_GetDefaultMergeMessage_ExternalTracker(t *testing.T) { baseRepo.Owner = &User{Name: "testOwner"} baseRepo.Units = []*RepoUnit{&externalTracker} - pr := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 2, BaseRepo: baseRepo}).(*PullRequest) + pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2, BaseRepo: baseRepo}).(*PullRequest) assert.Equal(t, "Merge pull request 'issue3' (!3) from branch2 into master", pr.GetDefaultMergeMessage()) diff --git a/models/repo_collaboration_test.go b/models/repo_collaboration_test.go index 5e6a0a5d9..9b698c0af 100644 --- a/models/repo_collaboration_test.go +++ b/models/repo_collaboration_test.go @@ -16,11 +16,11 @@ func TestRepository_AddCollaborator(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) testSuccess := func(repoID, userID int64) { - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) assert.NoError(t, repo.GetOwner()) - user := db.AssertExistsAndLoadBean(t, &User{ID: userID}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: userID}).(*User) assert.NoError(t, repo.AddCollaborator(user)) - CheckConsistencyFor(t, &Repository{ID: repoID}, &User{ID: userID}) + unittest.CheckConsistencyFor(t, &Repository{ID: repoID}, &User{ID: userID}) } testSuccess(1, 4) testSuccess(1, 4) @@ -30,7 +30,7 @@ func TestRepository_AddCollaborator(t *testing.T) { func TestRepository_GetCollaborators(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(repoID int64) { - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) collaborators, err := repo.GetCollaborators(db.ListOptions{}) assert.NoError(t, err) expectedLen, err := db.GetEngine(db.DefaultContext).Count(&Collaboration{RepoID: repoID}) @@ -51,13 +51,13 @@ func TestRepository_IsCollaborator(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) test := func(repoID, userID int64, expected bool) { - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) actual, err := repo.IsCollaborator(userID) assert.NoError(t, err) assert.Equal(t, expected, actual) } test(3, 2, true) - test(3, db.NonexistentID, false) + test(3, unittest.NonexistentID, false) test(4, 2, false) test(4, 4, true) } @@ -65,32 +65,32 @@ func TestRepository_IsCollaborator(t *testing.T) { func TestRepository_ChangeCollaborationAccessMode(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) assert.NoError(t, repo.ChangeCollaborationAccessMode(4, AccessModeAdmin)) - collaboration := db.AssertExistsAndLoadBean(t, &Collaboration{RepoID: repo.ID, UserID: 4}).(*Collaboration) + collaboration := unittest.AssertExistsAndLoadBean(t, &Collaboration{RepoID: repo.ID, UserID: 4}).(*Collaboration) assert.EqualValues(t, AccessModeAdmin, collaboration.Mode) - access := db.AssertExistsAndLoadBean(t, &Access{UserID: 4, RepoID: repo.ID}).(*Access) + access := unittest.AssertExistsAndLoadBean(t, &Access{UserID: 4, RepoID: repo.ID}).(*Access) assert.EqualValues(t, AccessModeAdmin, access.Mode) assert.NoError(t, repo.ChangeCollaborationAccessMode(4, AccessModeAdmin)) - assert.NoError(t, repo.ChangeCollaborationAccessMode(db.NonexistentID, AccessModeAdmin)) + assert.NoError(t, repo.ChangeCollaborationAccessMode(unittest.NonexistentID, AccessModeAdmin)) - CheckConsistencyFor(t, &Repository{ID: repo.ID}) + unittest.CheckConsistencyFor(t, &Repository{ID: repo.ID}) } func TestRepository_DeleteCollaboration(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) assert.NoError(t, repo.GetOwner()) assert.NoError(t, repo.DeleteCollaboration(4)) - db.AssertNotExistsBean(t, &Collaboration{RepoID: repo.ID, UserID: 4}) + unittest.AssertNotExistsBean(t, &Collaboration{RepoID: repo.ID, UserID: 4}) assert.NoError(t, repo.DeleteCollaboration(4)) - db.AssertNotExistsBean(t, &Collaboration{RepoID: repo.ID, UserID: 4}) + unittest.AssertNotExistsBean(t, &Collaboration{RepoID: repo.ID, UserID: 4}) - CheckConsistencyFor(t, &Repository{ID: repo.ID}) + unittest.CheckConsistencyFor(t, &Repository{ID: repo.ID}) } diff --git a/models/repo_list_test.go b/models/repo_list_test.go index eaf5e8a7c..4722b073a 100644 --- a/models/repo_list_test.go +++ b/models/repo_list_test.go @@ -78,7 +78,7 @@ func TestSearchRepository(t *testing.T) { assert.Len(t, repos, 3) // Test non existing owner - repos, count, err = SearchRepositoryByName(&SearchRepoOptions{OwnerID: db.NonexistentID}) + repos, count, err = SearchRepositoryByName(&SearchRepoOptions{OwnerID: unittest.NonexistentID}) assert.NoError(t, err) assert.Empty(t, repos) diff --git a/models/repo_permission_test.go b/models/repo_permission_test.go index 1f04a4531..b64fe3791 100644 --- a/models/repo_permission_test.go +++ b/models/repo_permission_test.go @@ -17,11 +17,11 @@ func TestRepoPermissionPublicNonOrgRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) // public non-organization repo - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) assert.NoError(t, repo.getUnits(db.GetEngine(db.DefaultContext))) // plain user - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) perm, err := GetUserRepoPermission(repo, user) assert.NoError(t, err) for _, unit := range repo.Units { @@ -39,7 +39,7 @@ func TestRepoPermissionPublicNonOrgRepo(t *testing.T) { } // collaborator - collaborator := db.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) + collaborator := unittest.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) perm, err = GetUserRepoPermission(repo, collaborator) assert.NoError(t, err) for _, unit := range repo.Units { @@ -48,7 +48,7 @@ func TestRepoPermissionPublicNonOrgRepo(t *testing.T) { } // owner - owner := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) perm, err = GetUserRepoPermission(repo, owner) assert.NoError(t, err) for _, unit := range repo.Units { @@ -57,7 +57,7 @@ func TestRepoPermissionPublicNonOrgRepo(t *testing.T) { } // admin - admin := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + admin := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) perm, err = GetUserRepoPermission(repo, admin) assert.NoError(t, err) for _, unit := range repo.Units { @@ -70,11 +70,11 @@ func TestRepoPermissionPrivateNonOrgRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) // private non-organization repo - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) assert.NoError(t, repo.getUnits(db.GetEngine(db.DefaultContext))) // plain user - user := db.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) perm, err := GetUserRepoPermission(repo, user) assert.NoError(t, err) for _, unit := range repo.Units { @@ -100,7 +100,7 @@ func TestRepoPermissionPrivateNonOrgRepo(t *testing.T) { } // owner - owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) perm, err = GetUserRepoPermission(repo, owner) assert.NoError(t, err) for _, unit := range repo.Units { @@ -109,7 +109,7 @@ func TestRepoPermissionPrivateNonOrgRepo(t *testing.T) { } // admin - admin := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + admin := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) perm, err = GetUserRepoPermission(repo, admin) assert.NoError(t, err) for _, unit := range repo.Units { @@ -122,11 +122,11 @@ func TestRepoPermissionPublicOrgRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) // public organization repo - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 32}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 32}).(*Repository) assert.NoError(t, repo.getUnits(db.GetEngine(db.DefaultContext))) // plain user - user := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) perm, err := GetUserRepoPermission(repo, user) assert.NoError(t, err) for _, unit := range repo.Units { @@ -152,7 +152,7 @@ func TestRepoPermissionPublicOrgRepo(t *testing.T) { } // org member team owner - owner := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) perm, err = GetUserRepoPermission(repo, owner) assert.NoError(t, err) for _, unit := range repo.Units { @@ -161,7 +161,7 @@ func TestRepoPermissionPublicOrgRepo(t *testing.T) { } // org member team tester - member := db.AssertExistsAndLoadBean(t, &User{ID: 15}).(*User) + member := unittest.AssertExistsAndLoadBean(t, &User{ID: 15}).(*User) perm, err = GetUserRepoPermission(repo, member) assert.NoError(t, err) for _, unit := range repo.Units { @@ -171,7 +171,7 @@ func TestRepoPermissionPublicOrgRepo(t *testing.T) { assert.False(t, perm.CanWrite(unit.TypeCode)) // admin - admin := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + admin := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) perm, err = GetUserRepoPermission(repo, admin) assert.NoError(t, err) for _, unit := range repo.Units { @@ -184,11 +184,11 @@ func TestRepoPermissionPrivateOrgRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) // private organization repo - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 24}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 24}).(*Repository) assert.NoError(t, repo.getUnits(db.GetEngine(db.DefaultContext))) // plain user - user := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) perm, err := GetUserRepoPermission(repo, user) assert.NoError(t, err) for _, unit := range repo.Units { @@ -214,7 +214,7 @@ func TestRepoPermissionPrivateOrgRepo(t *testing.T) { } // org member team owner - owner := db.AssertExistsAndLoadBean(t, &User{ID: 15}).(*User) + owner := unittest.AssertExistsAndLoadBean(t, &User{ID: 15}).(*User) perm, err = GetUserRepoPermission(repo, owner) assert.NoError(t, err) for _, unit := range repo.Units { @@ -223,7 +223,7 @@ func TestRepoPermissionPrivateOrgRepo(t *testing.T) { } // update team information and then check permission - team := db.AssertExistsAndLoadBean(t, &Team{ID: 5}).(*Team) + team := unittest.AssertExistsAndLoadBean(t, &Team{ID: 5}).(*Team) err = UpdateTeamUnits(team, nil) assert.NoError(t, err) perm, err = GetUserRepoPermission(repo, owner) @@ -234,7 +234,7 @@ func TestRepoPermissionPrivateOrgRepo(t *testing.T) { } // org member team tester - tester := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + tester := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) perm, err = GetUserRepoPermission(repo, tester) assert.NoError(t, err) assert.True(t, perm.CanWrite(unit.TypeIssues)) @@ -242,7 +242,7 @@ func TestRepoPermissionPrivateOrgRepo(t *testing.T) { assert.False(t, perm.CanRead(unit.TypeCode)) // org member team reviewer - reviewer := db.AssertExistsAndLoadBean(t, &User{ID: 20}).(*User) + reviewer := unittest.AssertExistsAndLoadBean(t, &User{ID: 20}).(*User) perm, err = GetUserRepoPermission(repo, reviewer) assert.NoError(t, err) assert.False(t, perm.CanRead(unit.TypeIssues)) @@ -250,7 +250,7 @@ func TestRepoPermissionPrivateOrgRepo(t *testing.T) { assert.True(t, perm.CanRead(unit.TypeCode)) // admin - admin := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + admin := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) perm, err = GetUserRepoPermission(repo, admin) assert.NoError(t, err) for _, unit := range repo.Units { diff --git a/models/repo_redirect_test.go b/models/repo_redirect_test.go index bf42d4854..a9d3cc149 100644 --- a/models/repo_redirect_test.go +++ b/models/repo_redirect_test.go @@ -19,7 +19,7 @@ func TestLookupRepoRedirect(t *testing.T) { assert.NoError(t, err) assert.EqualValues(t, 1, repoID) - _, err = LookupRepoRedirect(db.NonexistentID, "doesnotexist") + _, err = LookupRepoRedirect(unittest.NonexistentID, "doesnotexist") assert.True(t, IsErrRepoRedirectNotExist(err)) } @@ -27,15 +27,15 @@ func TestNewRepoRedirect(t *testing.T) { // redirect to a completely new name assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) assert.NoError(t, newRepoRedirect(db.GetEngine(db.DefaultContext), repo.OwnerID, repo.ID, repo.Name, "newreponame")) - db.AssertExistsAndLoadBean(t, &RepoRedirect{ + unittest.AssertExistsAndLoadBean(t, &RepoRedirect{ OwnerID: repo.OwnerID, LowerName: repo.LowerName, RedirectRepoID: repo.ID, }) - db.AssertExistsAndLoadBean(t, &RepoRedirect{ + unittest.AssertExistsAndLoadBean(t, &RepoRedirect{ OwnerID: repo.OwnerID, LowerName: "oldrepo1", RedirectRepoID: repo.ID, @@ -46,15 +46,15 @@ func TestNewRepoRedirect2(t *testing.T) { // redirect to previously used name assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) assert.NoError(t, newRepoRedirect(db.GetEngine(db.DefaultContext), repo.OwnerID, repo.ID, repo.Name, "oldrepo1")) - db.AssertExistsAndLoadBean(t, &RepoRedirect{ + unittest.AssertExistsAndLoadBean(t, &RepoRedirect{ OwnerID: repo.OwnerID, LowerName: repo.LowerName, RedirectRepoID: repo.ID, }) - db.AssertNotExistsBean(t, &RepoRedirect{ + unittest.AssertNotExistsBean(t, &RepoRedirect{ OwnerID: repo.OwnerID, LowerName: "oldrepo1", RedirectRepoID: repo.ID, @@ -65,10 +65,10 @@ func TestNewRepoRedirect3(t *testing.T) { // redirect for a previously-unredirected repo assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) assert.NoError(t, newRepoRedirect(db.GetEngine(db.DefaultContext), repo.OwnerID, repo.ID, repo.Name, "newreponame")) - db.AssertExistsAndLoadBean(t, &RepoRedirect{ + unittest.AssertExistsAndLoadBean(t, &RepoRedirect{ OwnerID: repo.OwnerID, LowerName: repo.LowerName, RedirectRepoID: repo.ID, diff --git a/models/repo_test.go b/models/repo_test.go index 2eb6e817e..685570adc 100644 --- a/models/repo_test.go +++ b/models/repo_test.go @@ -138,7 +138,7 @@ func TestGetUserFork(t *testing.T) { func TestRepoAPIURL(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository) assert.Equal(t, "https://try.gitea.io/api/v1/repos/user12/repo10", repo.APIURL()) } @@ -150,7 +150,7 @@ func TestUploadAvatar(t *testing.T) { png.Encode(&buff, myImage) assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository) err := repo.UploadAvatar(buff.Bytes()) assert.NoError(t, err) @@ -164,7 +164,7 @@ func TestUploadBigAvatar(t *testing.T) { png.Encode(&buff, myImage) assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository) err := repo.UploadAvatar(buff.Bytes()) assert.Error(t, err) @@ -177,7 +177,7 @@ func TestDeleteAvatar(t *testing.T) { png.Encode(&buff, myImage) assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 10}).(*Repository) err := repo.UploadAvatar(buff.Bytes()) assert.NoError(t, err) @@ -198,14 +198,14 @@ func TestRepoGetReviewers(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) // test public repo - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) reviewers, err := repo1.GetReviewers(2, 2) assert.NoError(t, err) assert.Len(t, reviewers, 4) // test private repo - repo2 := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) + repo2 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) reviewers, err = repo2.GetReviewers(2, 2) assert.NoError(t, err) assert.Empty(t, reviewers) @@ -214,12 +214,12 @@ func TestRepoGetReviewers(t *testing.T) { func TestRepoGetReviewerTeams(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo2 := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) + repo2 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) teams, err := repo2.GetReviewerTeams() assert.NoError(t, err) assert.Empty(t, teams) - repo3 := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) + repo3 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) teams, err = repo3.GetReviewerTeams() assert.NoError(t, err) assert.Len(t, teams, 2) diff --git a/models/repo_transfer_test.go b/models/repo_transfer_test.go index 71976def9..df4f83919 100644 --- a/models/repo_transfer_test.go +++ b/models/repo_transfer_test.go @@ -7,7 +7,6 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -15,8 +14,8 @@ import ( func TestRepositoryTransfer(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - doer := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) + doer := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) transfer, err := GetPendingRepositoryTransfer(repo) assert.NoError(t, err) @@ -30,7 +29,7 @@ func TestRepositoryTransfer(t *testing.T) { assert.Nil(t, transfer) assert.True(t, IsErrNoPendingTransfer(err)) - user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user2 := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) assert.NoError(t, CreatePendingRepositoryTransfer(doer, user2, repo.ID, nil)) @@ -39,7 +38,7 @@ func TestRepositoryTransfer(t *testing.T) { assert.NoError(t, transfer.LoadAttributes()) assert.Equal(t, "user2", transfer.Recipient.Name) - user6 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user6 := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) // Only transfer can be started at any given time err = CreatePendingRepositoryTransfer(doer, user6, repo.ID, nil) diff --git a/models/repo_watch_test.go b/models/repo_watch_test.go index 9f20a088f..b7efcba71 100644 --- a/models/repo_watch_test.go +++ b/models/repo_watch_test.go @@ -23,7 +23,7 @@ func TestIsWatching(t *testing.T) { assert.False(t, IsWatching(1, 5)) assert.False(t, IsWatching(8, 1)) - assert.False(t, IsWatching(db.NonexistentID, db.NonexistentID)) + assert.False(t, IsWatching(unittest.NonexistentID, unittest.NonexistentID)) } func TestWatchRepo(t *testing.T) { @@ -32,18 +32,18 @@ func TestWatchRepo(t *testing.T) { const userID = 2 assert.NoError(t, WatchRepo(userID, repoID, true)) - db.AssertExistsAndLoadBean(t, &Watch{RepoID: repoID, UserID: userID}) - CheckConsistencyFor(t, &Repository{ID: repoID}) + unittest.AssertExistsAndLoadBean(t, &Watch{RepoID: repoID, UserID: userID}) + unittest.CheckConsistencyFor(t, &Repository{ID: repoID}) assert.NoError(t, WatchRepo(userID, repoID, false)) - db.AssertNotExistsBean(t, &Watch{RepoID: repoID, UserID: userID}) - CheckConsistencyFor(t, &Repository{ID: repoID}) + unittest.AssertNotExistsBean(t, &Watch{RepoID: repoID, UserID: userID}) + unittest.CheckConsistencyFor(t, &Repository{ID: repoID}) } func TestGetWatchers(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) watches, err := GetWatchers(repo.ID) assert.NoError(t, err) // One watchers are inactive, thus minus 1 @@ -52,7 +52,7 @@ func TestGetWatchers(t *testing.T) { assert.EqualValues(t, repo.ID, watch.RepoID) } - watches, err = GetWatchers(db.NonexistentID) + watches, err = GetWatchers(unittest.NonexistentID) assert.NoError(t, err) assert.Len(t, watches, 0) } @@ -60,15 +60,15 @@ func TestGetWatchers(t *testing.T) { func TestRepository_GetWatchers(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) watchers, err := repo.GetWatchers(db.ListOptions{Page: 1}) assert.NoError(t, err) assert.Len(t, watchers, repo.NumWatches) for _, watcher := range watchers { - db.AssertExistsAndLoadBean(t, &Watch{UserID: watcher.ID, RepoID: repo.ID}) + unittest.AssertExistsAndLoadBean(t, &Watch{UserID: watcher.ID, RepoID: repo.ID}) } - repo = db.AssertExistsAndLoadBean(t, &Repository{ID: 9}).(*Repository) + repo = unittest.AssertExistsAndLoadBean(t, &Repository{ID: 9}).(*Repository) watchers, err = repo.GetWatchers(db.ListOptions{Page: 1}) assert.NoError(t, err) assert.Len(t, watchers, 0) @@ -85,25 +85,25 @@ func TestNotifyWatchers(t *testing.T) { assert.NoError(t, NotifyWatchers(action)) // One watchers are inactive, thus action is only created for user 8, 1, 4, 11 - db.AssertExistsAndLoadBean(t, &Action{ + unittest.AssertExistsAndLoadBean(t, &Action{ ActUserID: action.ActUserID, UserID: 8, RepoID: action.RepoID, OpType: action.OpType, }) - db.AssertExistsAndLoadBean(t, &Action{ + unittest.AssertExistsAndLoadBean(t, &Action{ ActUserID: action.ActUserID, UserID: 1, RepoID: action.RepoID, OpType: action.OpType, }) - db.AssertExistsAndLoadBean(t, &Action{ + unittest.AssertExistsAndLoadBean(t, &Action{ ActUserID: action.ActUserID, UserID: 4, RepoID: action.RepoID, OpType: action.OpType, }) - db.AssertExistsAndLoadBean(t, &Action{ + unittest.AssertExistsAndLoadBean(t, &Action{ ActUserID: action.ActUserID, UserID: 11, RepoID: action.RepoID, @@ -114,7 +114,7 @@ func TestNotifyWatchers(t *testing.T) { func TestWatchIfAuto(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) watchers, err := repo.GetWatchers(db.ListOptions{Page: 1}) assert.NoError(t, err) assert.Len(t, watchers, repo.NumWatches) @@ -171,20 +171,20 @@ func TestWatchIfAuto(t *testing.T) { func TestWatchRepoMode(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - db.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 0) + unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 0) assert.NoError(t, WatchRepoMode(12, 1, RepoWatchModeAuto)) - db.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1) - db.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeAuto}, 1) + unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1) + unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeAuto}, 1) assert.NoError(t, WatchRepoMode(12, 1, RepoWatchModeNormal)) - db.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1) - db.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeNormal}, 1) + unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1) + unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeNormal}, 1) assert.NoError(t, WatchRepoMode(12, 1, RepoWatchModeDont)) - db.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1) - db.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeDont}, 1) + unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 1) + unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1, Mode: RepoWatchModeDont}, 1) assert.NoError(t, WatchRepoMode(12, 1, RepoWatchModeNone)) - db.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 0) + unittest.AssertCount(t, &Watch{UserID: 12, RepoID: 1}, 0) } diff --git a/models/review_test.go b/models/review_test.go index 31d6c4553..2d07ea2ce 100644 --- a/models/review_test.go +++ b/models/review_test.go @@ -7,7 +7,6 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -26,22 +25,22 @@ func TestGetReviewByID(t *testing.T) { func TestReview_LoadAttributes(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - review := db.AssertExistsAndLoadBean(t, &Review{ID: 1}).(*Review) + review := unittest.AssertExistsAndLoadBean(t, &Review{ID: 1}).(*Review) assert.NoError(t, review.LoadAttributes()) assert.NotNil(t, review.Issue) assert.NotNil(t, review.Reviewer) - invalidReview1 := db.AssertExistsAndLoadBean(t, &Review{ID: 2}).(*Review) + invalidReview1 := unittest.AssertExistsAndLoadBean(t, &Review{ID: 2}).(*Review) assert.Error(t, invalidReview1.LoadAttributes()) - invalidReview2 := db.AssertExistsAndLoadBean(t, &Review{ID: 3}).(*Review) + invalidReview2 := unittest.AssertExistsAndLoadBean(t, &Review{ID: 3}).(*Review) assert.Error(t, invalidReview2.LoadAttributes()) } func TestReview_LoadCodeComments(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - review := db.AssertExistsAndLoadBean(t, &Review{ID: 4}).(*Review) + review := unittest.AssertExistsAndLoadBean(t, &Review{ID: 4}).(*Review) assert.NoError(t, review.LoadAttributes()) assert.NoError(t, review.LoadCodeComments()) assert.Len(t, review.CodeComments, 1) @@ -71,8 +70,8 @@ func TestFindReviews(t *testing.T) { func TestGetCurrentReview(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue) - user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) review, err := GetCurrentReview(user, issue) assert.NoError(t, err) @@ -80,7 +79,7 @@ func TestGetCurrentReview(t *testing.T) { assert.Equal(t, ReviewTypePending, review.Type) assert.Equal(t, "Pending Review", review.Content) - user2 := db.AssertExistsAndLoadBean(t, &User{ID: 7}).(*User) + user2 := unittest.AssertExistsAndLoadBean(t, &User{ID: 7}).(*User) review2, err := GetCurrentReview(user2, issue) assert.Error(t, err) assert.True(t, IsErrReviewNotExist(err)) @@ -90,8 +89,8 @@ func TestGetCurrentReview(t *testing.T) { func TestCreateReview(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue) - user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) review, err := CreateReview(CreateReviewOptions{ Content: "New Review", @@ -101,16 +100,16 @@ func TestCreateReview(t *testing.T) { }) assert.NoError(t, err) assert.Equal(t, "New Review", review.Content) - db.AssertExistsAndLoadBean(t, &Review{Content: "New Review"}) + unittest.AssertExistsAndLoadBean(t, &Review{Content: "New Review"}) } func TestGetReviewersByIssueID(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 3}).(*Issue) - user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - user3 := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) - user4 := db.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) + issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 3}).(*Issue) + user2 := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user3 := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + user4 := unittest.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) expectedReviews := []*Review{} expectedReviews = append(expectedReviews, @@ -147,43 +146,43 @@ func TestGetReviewersByIssueID(t *testing.T) { func TestDismissReview(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - rejectReviewExample := db.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) - requestReviewExample := db.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) - approveReviewExample := db.AssertExistsAndLoadBean(t, &Review{ID: 8}).(*Review) + rejectReviewExample := unittest.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) + requestReviewExample := unittest.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) + approveReviewExample := unittest.AssertExistsAndLoadBean(t, &Review{ID: 8}).(*Review) assert.False(t, rejectReviewExample.Dismissed) assert.False(t, requestReviewExample.Dismissed) assert.False(t, approveReviewExample.Dismissed) assert.NoError(t, DismissReview(rejectReviewExample, true)) - rejectReviewExample = db.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) - requestReviewExample = db.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) + rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) + requestReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) assert.True(t, rejectReviewExample.Dismissed) assert.False(t, requestReviewExample.Dismissed) assert.NoError(t, DismissReview(requestReviewExample, true)) - rejectReviewExample = db.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) - requestReviewExample = db.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) + rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) + requestReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) assert.True(t, rejectReviewExample.Dismissed) assert.False(t, requestReviewExample.Dismissed) assert.False(t, approveReviewExample.Dismissed) assert.NoError(t, DismissReview(requestReviewExample, true)) - rejectReviewExample = db.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) - requestReviewExample = db.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) + rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) + requestReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) assert.True(t, rejectReviewExample.Dismissed) assert.False(t, requestReviewExample.Dismissed) assert.False(t, approveReviewExample.Dismissed) assert.NoError(t, DismissReview(requestReviewExample, false)) - rejectReviewExample = db.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) - requestReviewExample = db.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) + rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) + requestReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) assert.True(t, rejectReviewExample.Dismissed) assert.False(t, requestReviewExample.Dismissed) assert.False(t, approveReviewExample.Dismissed) assert.NoError(t, DismissReview(requestReviewExample, false)) - rejectReviewExample = db.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) - requestReviewExample = db.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) + rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review) + requestReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review) assert.True(t, rejectReviewExample.Dismissed) assert.False(t, requestReviewExample.Dismissed) assert.False(t, approveReviewExample.Dismissed) diff --git a/models/star_test.go b/models/star_test.go index 8e448ae7c..e13137273 100644 --- a/models/star_test.go +++ b/models/star_test.go @@ -16,13 +16,13 @@ func TestStarRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) const userID = 2 const repoID = 1 - db.AssertNotExistsBean(t, &Star{UID: userID, RepoID: repoID}) + unittest.AssertNotExistsBean(t, &Star{UID: userID, RepoID: repoID}) assert.NoError(t, StarRepo(userID, repoID, true)) - db.AssertExistsAndLoadBean(t, &Star{UID: userID, RepoID: repoID}) + unittest.AssertExistsAndLoadBean(t, &Star{UID: userID, RepoID: repoID}) assert.NoError(t, StarRepo(userID, repoID, true)) - db.AssertExistsAndLoadBean(t, &Star{UID: userID, RepoID: repoID}) + unittest.AssertExistsAndLoadBean(t, &Star{UID: userID, RepoID: repoID}) assert.NoError(t, StarRepo(userID, repoID, false)) - db.AssertNotExistsBean(t, &Star{UID: userID, RepoID: repoID}) + unittest.AssertNotExistsBean(t, &Star{UID: userID, RepoID: repoID}) } func TestIsStaring(t *testing.T) { @@ -34,7 +34,7 @@ func TestIsStaring(t *testing.T) { func TestRepository_GetStargazers(t *testing.T) { // repo with stargazers assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) gazers, err := repo.GetStargazers(db.ListOptions{Page: 0}) assert.NoError(t, err) if assert.Len(t, gazers, 1) { @@ -45,7 +45,7 @@ func TestRepository_GetStargazers(t *testing.T) { func TestRepository_GetStargazers2(t *testing.T) { // repo with stargazers assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) gazers, err := repo.GetStargazers(db.ListOptions{Page: 0}) assert.NoError(t, err) assert.Len(t, gazers, 0) @@ -55,7 +55,7 @@ func TestUser_GetStarredRepos(t *testing.T) { // user who has starred repos assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) starred, err := user.GetStarredRepos(false, 1, 10, "") assert.NoError(t, err) if assert.Len(t, starred, 1) { @@ -74,7 +74,7 @@ func TestUser_GetStarredRepos2(t *testing.T) { // user who has no starred repos assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) starred, err := user.GetStarredRepos(false, 1, 10, "") assert.NoError(t, err) assert.Len(t, starred, 0) @@ -87,7 +87,7 @@ func TestUser_GetStarredRepos2(t *testing.T) { func TestUserGetStarredRepoCount(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) counts, err := user.GetStarredRepoCount(false) assert.NoError(t, err) assert.Equal(t, int64(1), counts) diff --git a/models/token_test.go b/models/token_test.go index 016176459..191da7820 100644 --- a/models/token_test.go +++ b/models/token_test.go @@ -7,7 +7,6 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -19,7 +18,7 @@ func TestNewAccessToken(t *testing.T) { Name: "Token C", } assert.NoError(t, NewAccessToken(token)) - db.AssertExistsAndLoadBean(t, token) + unittest.AssertExistsAndLoadBean(t, token) invalidToken := &AccessToken{ ID: token.ID, // duplicate @@ -45,7 +44,7 @@ func TestAccessTokenByNameExists(t *testing.T) { // Save it to the database assert.NoError(t, NewAccessToken(token)) - db.AssertExistsAndLoadBean(t, token) + unittest.AssertExistsAndLoadBean(t, token) // This token must be found by name in the DB now exist, err = AccessTokenByNameExists(token) @@ -112,7 +111,7 @@ func TestUpdateAccessToken(t *testing.T) { token.Name = "Token Z" assert.NoError(t, UpdateAccessToken(token)) - db.AssertExistsAndLoadBean(t, token) + unittest.AssertExistsAndLoadBean(t, token) } func TestDeleteAccessTokenByID(t *testing.T) { @@ -123,7 +122,7 @@ func TestDeleteAccessTokenByID(t *testing.T) { assert.Equal(t, int64(1), token.UID) assert.NoError(t, DeleteAccessTokenByID(token.ID, 1)) - db.AssertNotExistsBean(t, token) + unittest.AssertNotExistsBean(t, token) err = DeleteAccessTokenByID(100, 100) assert.Error(t, err) diff --git a/models/unittest/bridge.go b/models/unittest/bridge.go deleted file mode 100644 index 776dd6951..000000000 --- a/models/unittest/bridge.go +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright 2021 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package unittest - -import ( - "code.gitea.io/gitea/modules/unittestbridge" - "github.com/stretchr/testify/assert" -) - -// For legacy code only, please refer to the `unittestbridge` package. - -// TestifyAsserter uses "stretchr/testify/assert" to do assert -type TestifyAsserter struct { - t unittestbridge.Tester -} - -// Errorf assert Errorf -func (ta TestifyAsserter) Errorf(format string, args ...interface{}) { - ta.t.Errorf(format, args) -} - -// NoError assert NoError -func (ta TestifyAsserter) NoError(err error, msgAndArgs ...interface{}) bool { - return assert.NoError(ta, err, msgAndArgs...) -} - -// EqualValues assert EqualValues -func (ta TestifyAsserter) EqualValues(expected, actual interface{}, msgAndArgs ...interface{}) bool { - return assert.EqualValues(ta, expected, actual, msgAndArgs...) -} - -// Equal assert Equal -func (ta TestifyAsserter) Equal(expected, actual interface{}, msgAndArgs ...interface{}) bool { - return assert.Equal(ta, expected, actual, msgAndArgs...) -} - -// True assert True -func (ta TestifyAsserter) True(value bool, msgAndArgs ...interface{}) bool { - return assert.True(ta, value, msgAndArgs...) -} - -// False assert False -func (ta TestifyAsserter) False(value bool, msgAndArgs ...interface{}) bool { - return assert.False(ta, value, msgAndArgs...) -} - -// InitUnitTestBridge init the unit test bridge. eg: models.CheckConsistencyFor can use testing and assert frameworks -func InitUnitTestBridge() { - unittestbridge.SetNewAsserterFunc(func(t unittestbridge.Tester) unittestbridge.Asserter { - return &TestifyAsserter{t: t} - }) -} diff --git a/models/unittest/consistency.go b/models/unittest/consistency.go new file mode 100644 index 000000000..2645084d3 --- /dev/null +++ b/models/unittest/consistency.go @@ -0,0 +1,190 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package unittest + +import ( + "reflect" + "strconv" + "strings" + + "code.gitea.io/gitea/models/db" + + "github.com/stretchr/testify/assert" + "xorm.io/builder" +) + +const ( + // these const values are copied from `models` package to prevent from cycle-import + modelsUserTypeOrganization = 1 + modelsRepoWatchModeDont = 2 + modelsCommentTypeComment = 0 +) + +var consistencyCheckMap = make(map[string]func(t assert.TestingT, bean interface{})) + +// CheckConsistencyFor test that all matching database entries are consistent +func CheckConsistencyFor(t assert.TestingT, beansToCheck ...interface{}) { + for _, bean := range beansToCheck { + sliceType := reflect.SliceOf(reflect.TypeOf(bean)) + sliceValue := reflect.MakeSlice(sliceType, 0, 10) + + ptrToSliceValue := reflect.New(sliceType) + ptrToSliceValue.Elem().Set(sliceValue) + + assert.NoError(t, db.GetEngine(db.DefaultContext).Table(bean).Find(ptrToSliceValue.Interface())) + sliceValue = ptrToSliceValue.Elem() + + for i := 0; i < sliceValue.Len(); i++ { + entity := sliceValue.Index(i).Interface() + checkForConsistency(t, entity) + } + } +} + +func checkForConsistency(t assert.TestingT, bean interface{}) { + tb, err := db.TableInfo(bean) + assert.NoError(t, err) + f := consistencyCheckMap[tb.Name] + if f == nil { + assert.Fail(t, "unknown bean type: %#v", bean) + return + } + f(t, bean) +} + +func init() { + parseBool := func(v string) bool { + b, _ := strconv.ParseBool(v) + return b + } + parseInt := func(v string) int { + i, _ := strconv.Atoi(v) + return i + } + + checkForUserConsistency := func(t assert.TestingT, bean interface{}) { + user := reflectionWrap(bean) + AssertCountByCond(t, "repository", builder.Eq{"owner_id": user.int("ID")}, user.int("NumRepos")) + AssertCountByCond(t, "star", builder.Eq{"uid": user.int("ID")}, user.int("NumStars")) + AssertCountByCond(t, "org_user", builder.Eq{"org_id": user.int("ID")}, user.int("NumMembers")) + AssertCountByCond(t, "team", builder.Eq{"org_id": user.int("ID")}, user.int("NumTeams")) + AssertCountByCond(t, "follow", builder.Eq{"user_id": user.int("ID")}, user.int("NumFollowing")) + AssertCountByCond(t, "follow", builder.Eq{"follow_id": user.int("ID")}, user.int("NumFollowers")) + if user.int("Type") != modelsUserTypeOrganization { + assert.EqualValues(t, 0, user.int("NumMembers")) + assert.EqualValues(t, 0, user.int("NumTeams")) + } + } + + checkForRepoConsistency := func(t assert.TestingT, bean interface{}) { + repo := reflectionWrap(bean) + assert.Equal(t, repo.str("LowerName"), strings.ToLower(repo.str("Name")), "repo: %+v", repo) + AssertCountByCond(t, "star", builder.Eq{"repo_id": repo.int("ID")}, repo.int("NumStars")) + AssertCountByCond(t, "milestone", builder.Eq{"repo_id": repo.int("ID")}, repo.int("NumMilestones")) + AssertCountByCond(t, "repository", builder.Eq{"fork_id": repo.int("ID")}, repo.int("NumForks")) + if repo.bool("IsFork") { + AssertExistsAndLoadMap(t, "repository", builder.Eq{"id": repo.int("ForkID")}) + } + + actual := GetCountByCond(t, "watch", builder.Eq{"repo_id": repo.int("ID")}. + And(builder.Neq{"mode": modelsRepoWatchModeDont})) + assert.EqualValues(t, repo.int("NumWatches"), actual, + "Unexpected number of watches for repo %+v", repo) + + actual = GetCountByCond(t, "issue", builder.Eq{"is_pull": false, "repo_id": repo.int("ID")}) + assert.EqualValues(t, repo.int("NumIssues"), actual, + "Unexpected number of issues for repo %+v", repo) + + actual = GetCountByCond(t, "issue", builder.Eq{"is_pull": false, "is_closed": true, "repo_id": repo.int("ID")}) + assert.EqualValues(t, repo.int("NumClosedIssues"), actual, + "Unexpected number of closed issues for repo %+v", repo) + + actual = GetCountByCond(t, "issue", builder.Eq{"is_pull": true, "repo_id": repo.int("ID")}) + assert.EqualValues(t, repo.int("NumPulls"), actual, + "Unexpected number of pulls for repo %+v", repo) + + actual = GetCountByCond(t, "issue", builder.Eq{"is_pull": true, "is_closed": true, "repo_id": repo.int("ID")}) + assert.EqualValues(t, repo.int("NumClosedPulls"), actual, + "Unexpected number of closed pulls for repo %+v", repo) + + actual = GetCountByCond(t, "milestone", builder.Eq{"is_closed": true, "repo_id": repo.int("ID")}) + assert.EqualValues(t, repo.int("NumClosedMilestones"), actual, + "Unexpected number of closed milestones for repo %+v", repo) + } + + checkForIssueConsistency := func(t assert.TestingT, bean interface{}) { + issue := reflectionWrap(bean) + typeComment := modelsCommentTypeComment + actual := GetCountByCond(t, "comment", builder.Eq{"`type`": typeComment, "issue_id": issue.int("ID")}) + assert.EqualValues(t, issue.int("NumComments"), actual, "Unexpected number of comments for issue %+v", issue) + if issue.bool("IsPull") { + prRow := AssertExistsAndLoadMap(t, "pull_request", builder.Eq{"issue_id": issue.int("ID")}) + assert.EqualValues(t, parseInt(prRow["index"]), issue.int("Index")) + } + } + + checkForPullRequestConsistency := func(t assert.TestingT, bean interface{}) { + pr := reflectionWrap(bean) + issueRow := AssertExistsAndLoadMap(t, "issue", builder.Eq{"id": pr.int("IssueID")}) + assert.True(t, parseBool(issueRow["is_pull"])) + assert.EqualValues(t, parseInt(issueRow["index"]), pr.int("Index")) + } + + checkForMilestoneConsistency := func(t assert.TestingT, bean interface{}) { + milestone := reflectionWrap(bean) + AssertCountByCond(t, "issue", builder.Eq{"milestone_id": milestone.int("ID")}, milestone.int("NumIssues")) + + actual := GetCountByCond(t, "issue", builder.Eq{"is_closed": true, "milestone_id": milestone.int("ID")}) + assert.EqualValues(t, milestone.int("NumClosedIssues"), actual, "Unexpected number of closed issues for milestone %+v", milestone) + + completeness := 0 + if milestone.int("NumIssues") > 0 { + completeness = milestone.int("NumClosedIssues") * 100 / milestone.int("NumIssues") + } + assert.Equal(t, completeness, milestone.int("Completeness")) + } + + checkForLabelConsistency := func(t assert.TestingT, bean interface{}) { + label := reflectionWrap(bean) + issueLabels, err := db.GetEngine(db.DefaultContext).Table("issue_label"). + Where(builder.Eq{"label_id": label.int("ID")}). + Query() + assert.NoError(t, err) + + assert.EqualValues(t, label.int("NumIssues"), len(issueLabels), "Unexpected number of issue for label %+v", label) + + issueIDs := make([]int, len(issueLabels)) + for i, issueLabel := range issueLabels { + issueIDs[i], _ = strconv.Atoi(string(issueLabel["issue_id"])) + } + + expected := int64(0) + if len(issueIDs) > 0 { + expected = GetCountByCond(t, "issue", builder.In("id", issueIDs).And(builder.Eq{"is_closed": true})) + } + assert.EqualValues(t, expected, label.int("NumClosedIssues"), "Unexpected number of closed issues for label %+v", label) + } + + checkForTeamConsistency := func(t assert.TestingT, bean interface{}) { + team := reflectionWrap(bean) + AssertCountByCond(t, "team_user", builder.Eq{"team_id": team.int("ID")}, team.int("NumMembers")) + AssertCountByCond(t, "team_repo", builder.Eq{"team_id": team.int("ID")}, team.int("NumRepos")) + } + + checkForActionConsistency := func(t assert.TestingT, bean interface{}) { + action := reflectionWrap(bean) + repoRow := AssertExistsAndLoadMap(t, "repository", builder.Eq{"id": action.int("RepoID")}) + assert.Equal(t, parseBool(repoRow["is_private"]), action.bool("IsPrivate"), "action: %+v", action) + } + + consistencyCheckMap["user"] = checkForUserConsistency + consistencyCheckMap["repository"] = checkForRepoConsistency + consistencyCheckMap["issue"] = checkForIssueConsistency + consistencyCheckMap["pull_request"] = checkForPullRequestConsistency + consistencyCheckMap["milestone"] = checkForMilestoneConsistency + consistencyCheckMap["label"] = checkForLabelConsistency + consistencyCheckMap["team"] = checkForTeamConsistency + consistencyCheckMap["action"] = checkForActionConsistency +} diff --git a/models/unittest/reflection.go b/models/unittest/reflection.go new file mode 100644 index 000000000..68c312ac9 --- /dev/null +++ b/models/unittest/reflection.go @@ -0,0 +1,41 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package unittest + +import ( + "log" + "reflect" +) + +func fieldByName(v reflect.Value, field string) reflect.Value { + if v.Kind() == reflect.Ptr { + v = v.Elem() + } + f := v.FieldByName(field) + if !f.IsValid() { + log.Panicf("can not read %s for %v", field, v) + } + return f +} + +type reflectionValue struct { + v reflect.Value +} + +func reflectionWrap(v interface{}) *reflectionValue { + return &reflectionValue{v: reflect.ValueOf(v)} +} + +func (rv *reflectionValue) int(field string) int { + return int(fieldByName(rv.v, field).Int()) +} + +func (rv *reflectionValue) str(field string) string { + return fieldByName(rv.v, field).String() +} + +func (rv *reflectionValue) bool(field string) bool { + return fieldByName(rv.v, field).Bool() +} diff --git a/models/unittest/testdb.go b/models/unittest/testdb.go index b4f8c813a..8771bc1d2 100644 --- a/models/unittest/testdb.go +++ b/models/unittest/testdb.go @@ -18,7 +18,6 @@ import ( "code.gitea.io/gitea/modules/util" "github.com/stretchr/testify/assert" - "xorm.io/xorm" "xorm.io/xorm/names" ) @@ -43,7 +42,7 @@ func fatalTestError(fmtStr string, args ...interface{}) { // test database. Creates the test database, and sets necessary settings. func MainTest(m *testing.M, pathToGiteaRoot string, fixtureFiles ...string) { var err error - InitUnitTestBridge() + giteaRoot = pathToGiteaRoot fixturesDir = filepath.Join(pathToGiteaRoot, "models", "fixtures") @@ -125,7 +124,7 @@ func CreateTestEngine(opts FixturesOptions) error { return err } x.SetMapper(names.GonicMapper{}) - db.SetUnitTestEngine(x) + db.SetEngine(x) if err = db.SyncAllTables(); err != nil { return err diff --git a/models/unittest/unit_tests.go b/models/unittest/unit_tests.go new file mode 100644 index 000000000..6c20c2781 --- /dev/null +++ b/models/unittest/unit_tests.go @@ -0,0 +1,139 @@ +// Copyright 2016 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package unittest + +import ( + "math" + + "code.gitea.io/gitea/models/db" + + "github.com/stretchr/testify/assert" + "xorm.io/builder" +) + +// Code in this file is mainly used by unittest.CheckConsistencyFor, which is not in the unit test for various reasons. +// In the future if we can decouple CheckConsistencyFor into separate unit test code, then this file can be moved into unittest package too. + +// NonexistentID an ID that will never exist +const NonexistentID = int64(math.MaxInt64) + +type testCond struct { + query interface{} + args []interface{} +} + +// Cond create a condition with arguments for a test +func Cond(query interface{}, args ...interface{}) interface{} { + return &testCond{query: query, args: args} +} + +func whereConditions(e db.Engine, conditions []interface{}) db.Engine { + for _, condition := range conditions { + switch cond := condition.(type) { + case *testCond: + e = e.Where(cond.query, cond.args...) + default: + e = e.Where(cond) + } + } + return e +} + +// LoadBeanIfExists loads beans from fixture database if exist +func LoadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) { + e := db.GetEngine(db.DefaultContext) + return whereConditions(e, conditions).Get(bean) +} + +// BeanExists for testing, check if a bean exists +func BeanExists(t assert.TestingT, bean interface{}, conditions ...interface{}) bool { + exists, err := LoadBeanIfExists(bean, conditions...) + assert.NoError(t, err) + return exists +} + +// AssertExistsAndLoadBean assert that a bean exists and load it from the test database +func AssertExistsAndLoadBean(t assert.TestingT, bean interface{}, conditions ...interface{}) interface{} { + exists, err := LoadBeanIfExists(bean, conditions...) + assert.NoError(t, err) + assert.True(t, exists, + "Expected to find %+v (of type %T, with conditions %+v), but did not", + bean, bean, conditions) + return bean +} + +// AssertExistsAndLoadMap assert that a row exists and load it from the test database +func AssertExistsAndLoadMap(t assert.TestingT, table string, conditions ...interface{}) map[string]string { + e := db.GetEngine(db.DefaultContext).Table(table) + res, err := whereConditions(e, conditions).Query() + assert.NoError(t, err) + assert.True(t, len(res) == 1, + "Expected to find one row in %s (with conditions %+v), but found %d", + table, conditions, len(res), + ) + + if len(res) == 1 { + rec := map[string]string{} + for k, v := range res[0] { + rec[k] = string(v) + } + return rec + } + return nil +} + +// GetCount get the count of a bean +func GetCount(t assert.TestingT, bean interface{}, conditions ...interface{}) int { + e := db.GetEngine(db.DefaultContext) + count, err := whereConditions(e, conditions).Count(bean) + assert.NoError(t, err) + return int(count) +} + +// AssertNotExistsBean assert that a bean does not exist in the test database +func AssertNotExistsBean(t assert.TestingT, bean interface{}, conditions ...interface{}) { + exists, err := LoadBeanIfExists(bean, conditions...) + assert.NoError(t, err) + assert.False(t, exists) +} + +// AssertExistsIf asserts that a bean exists or does not exist, depending on +// what is expected. +func AssertExistsIf(t assert.TestingT, expected bool, bean interface{}, conditions ...interface{}) { + exists, err := LoadBeanIfExists(bean, conditions...) + assert.NoError(t, err) + assert.Equal(t, expected, exists) +} + +// AssertSuccessfulInsert assert that beans is successfully inserted +func AssertSuccessfulInsert(t assert.TestingT, beans ...interface{}) { + err := db.Insert(db.DefaultContext, beans...) + assert.NoError(t, err) +} + +// AssertCount assert the count of a bean +func AssertCount(t assert.TestingT, bean, expected interface{}) { + assert.EqualValues(t, expected, GetCount(t, bean)) +} + +// AssertInt64InRange assert value is in range [low, high] +func AssertInt64InRange(t assert.TestingT, low, high, value int64) { + assert.True(t, value >= low && value <= high, + "Expected value in range [%d, %d], found %d", low, high, value) +} + +// GetCountByCond get the count of database entries matching bean +func GetCountByCond(t assert.TestingT, tableName string, cond builder.Cond) int64 { + e := db.GetEngine(db.DefaultContext) + count, err := e.Table(tableName).Where(cond).Count() + assert.NoError(t, err) + return count +} + +// AssertCountByCond test the count of database entries matching bean +func AssertCountByCond(t assert.TestingT, tableName string, cond builder.Cond, expected int) { + assert.EqualValues(t, expected, GetCountByCond(t, tableName, cond), + "Failed consistency test, the counted bean (of table %s) was %+v", tableName, cond) +} diff --git a/models/user_follow_test.go b/models/user_follow_test.go index c7e56410f..5ba922728 100644 --- a/models/user_follow_test.go +++ b/models/user_follow_test.go @@ -7,7 +7,6 @@ package models import ( "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -16,9 +15,9 @@ func TestIsFollowing(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) assert.True(t, IsFollowing(4, 2)) assert.False(t, IsFollowing(2, 4)) - assert.False(t, IsFollowing(5, db.NonexistentID)) - assert.False(t, IsFollowing(db.NonexistentID, 5)) - assert.False(t, IsFollowing(db.NonexistentID, db.NonexistentID)) + assert.False(t, IsFollowing(5, unittest.NonexistentID)) + assert.False(t, IsFollowing(unittest.NonexistentID, 5)) + assert.False(t, IsFollowing(unittest.NonexistentID, unittest.NonexistentID)) } func TestFollowUser(t *testing.T) { @@ -26,14 +25,14 @@ func TestFollowUser(t *testing.T) { testSuccess := func(followerID, followedID int64) { assert.NoError(t, FollowUser(followerID, followedID)) - db.AssertExistsAndLoadBean(t, &Follow{UserID: followerID, FollowID: followedID}) + unittest.AssertExistsAndLoadBean(t, &Follow{UserID: followerID, FollowID: followedID}) } testSuccess(4, 2) testSuccess(5, 2) assert.NoError(t, FollowUser(2, 2)) - CheckConsistencyFor(t, &User{}) + unittest.CheckConsistencyFor(t, &User{}) } func TestUnfollowUser(t *testing.T) { @@ -41,11 +40,11 @@ func TestUnfollowUser(t *testing.T) { testSuccess := func(followerID, followedID int64) { assert.NoError(t, UnfollowUser(followerID, followedID)) - db.AssertNotExistsBean(t, &Follow{UserID: followerID, FollowID: followedID}) + unittest.AssertNotExistsBean(t, &Follow{UserID: followerID, FollowID: followedID}) } testSuccess(4, 2) testSuccess(5, 2) testSuccess(2, 2) - CheckConsistencyFor(t, &User{}) + unittest.CheckConsistencyFor(t, &User{}) } diff --git a/models/user_heatmap_test.go b/models/user_heatmap_test.go index 85e678e93..2abbbfc6f 100644 --- a/models/user_heatmap_test.go +++ b/models/user_heatmap_test.go @@ -9,7 +9,6 @@ import ( "testing" "time" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/timeutil" @@ -47,10 +46,10 @@ func TestGetUserHeatmapDataByUser(t *testing.T) { defer timeutil.Unset() for i, tc := range testCases { - user := db.AssertExistsAndLoadBean(t, &User{ID: tc.userID}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: tc.userID}).(*User) doer := &User{ID: tc.doerID} - _, err := db.LoadBeanIfExists(doer) + _, err := unittest.LoadBeanIfExists(doer) assert.NoError(t, err) if tc.doerID == 0 { doer = nil diff --git a/models/user_test.go b/models/user_test.go index 9a91a9b25..dc273ce1b 100644 --- a/models/user_test.go +++ b/models/user_test.go @@ -23,7 +23,7 @@ import ( func TestOAuth2Application_LoadUser(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - app := db.AssertExistsAndLoadBean(t, &login.OAuth2Application{ID: 1}).(*login.OAuth2Application) + app := unittest.AssertExistsAndLoadBean(t, &login.OAuth2Application{ID: 1}).(*login.OAuth2Application) user, err := GetUserByID(app.UID) assert.NoError(t, err) assert.NotNil(t, user) @@ -94,10 +94,10 @@ func TestGetUserEmailsByNames(t *testing.T) { func TestCanCreateOrganization(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - admin := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + admin := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) assert.True(t, admin.CanCreateOrganization()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) assert.True(t, user.CanCreateOrganization()) // Disable user create organization permission. user.AllowCreateOrganization = false @@ -180,7 +180,7 @@ func TestSearchUsers(t *testing.T) { func TestDeleteUser(t *testing.T) { test := func(userID int64) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: userID}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: userID}).(*User) ownedRepos := make([]*Repository, 0, 10) assert.NoError(t, db.GetEngine(db.DefaultContext).Find(&ownedRepos, &Repository{OwnerID: userID})) @@ -200,15 +200,15 @@ func TestDeleteUser(t *testing.T) { } } assert.NoError(t, DeleteUser(user)) - db.AssertNotExistsBean(t, &User{ID: userID}) - CheckConsistencyFor(t, &User{}, &Repository{}) + unittest.AssertNotExistsBean(t, &User{ID: userID}) + unittest.CheckConsistencyFor(t, &User{}, &Repository{}) } test(2) test(4) test(8) test(11) - org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) + org := unittest.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User) assert.Error(t, DeleteUser(org)) } @@ -229,7 +229,7 @@ func TestEmailNotificationPreferences(t *testing.T) { {EmailNotificationsEnabled, 8}, {EmailNotificationsOnMention, 9}, } { - user := db.AssertExistsAndLoadBean(t, &User{ID: test.userID}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: test.userID}).(*User) assert.Equal(t, test.expected, user.EmailNotifications()) // Try all possible settings @@ -282,9 +282,9 @@ func BenchmarkHashPassword(b *testing.B) { func TestGetOrgRepositoryIDs(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) - user4 := db.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) - user5 := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) + user2 := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user4 := unittest.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User) + user5 := unittest.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User) accessibleRepos, err := user2.GetOrgRepositoryIDs() assert.NoError(t, err) @@ -429,7 +429,7 @@ func TestGetMaileableUsersByIDs(t *testing.T) { func TestAddLdapSSHPublicKeys(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) s := &login.Source{ID: 1} testCases := []struct { @@ -496,18 +496,18 @@ ssh-dss AAAAB3NzaC1kc3MAAACBAOChCC7lf6Uo9n7BmZ6M8St19PZf4Tn59NriyboW2x/DZuYAz3ib func TestUpdateUser(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) user.KeepActivityPrivate = true assert.NoError(t, UpdateUser(user)) - user = db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user = unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) assert.True(t, user.KeepActivityPrivate) setting.Service.AllowedUserVisibilityModesSlice = []bool{true, false, false} user.KeepActivityPrivate = false user.Visibility = structs.VisibleTypePrivate assert.Error(t, UpdateUser(user)) - user = db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user = unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) assert.True(t, user.KeepActivityPrivate) user.Email = "no mail@mail.org" @@ -518,14 +518,14 @@ func TestNewUserRedirect(t *testing.T) { // redirect to a completely new name assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) assert.NoError(t, user_model.NewUserRedirect(db.DefaultContext, user.ID, user.Name, "newusername")) - db.AssertExistsAndLoadBean(t, &user_model.Redirect{ + unittest.AssertExistsAndLoadBean(t, &user_model.Redirect{ LowerName: user.LowerName, RedirectUserID: user.ID, }) - db.AssertExistsAndLoadBean(t, &user_model.Redirect{ + unittest.AssertExistsAndLoadBean(t, &user_model.Redirect{ LowerName: "olduser1", RedirectUserID: user.ID, }) @@ -535,14 +535,14 @@ func TestNewUserRedirect2(t *testing.T) { // redirect to previously used name assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) assert.NoError(t, user_model.NewUserRedirect(db.DefaultContext, user.ID, user.Name, "olduser1")) - db.AssertExistsAndLoadBean(t, &user_model.Redirect{ + unittest.AssertExistsAndLoadBean(t, &user_model.Redirect{ LowerName: user.LowerName, RedirectUserID: user.ID, }) - db.AssertNotExistsBean(t, &user_model.Redirect{ + unittest.AssertNotExistsBean(t, &user_model.Redirect{ LowerName: "olduser1", RedirectUserID: user.ID, }) @@ -552,10 +552,10 @@ func TestNewUserRedirect3(t *testing.T) { // redirect for a previously-unredirected user assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) + user := unittest.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) assert.NoError(t, user_model.NewUserRedirect(db.DefaultContext, user.ID, user.Name, "newusername")) - db.AssertExistsAndLoadBean(t, &user_model.Redirect{ + unittest.AssertExistsAndLoadBean(t, &user_model.Redirect{ LowerName: user.LowerName, RedirectUserID: user.ID, }) diff --git a/models/webhook/webhook_test.go b/models/webhook/webhook_test.go index 6d7957ba6..d1a76795f 100644 --- a/models/webhook/webhook_test.go +++ b/models/webhook/webhook_test.go @@ -31,14 +31,14 @@ func TestIsValidHookContentType(t *testing.T) { func TestWebhook_History(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - webhook := db.AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook) + webhook := unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook) tasks, err := webhook.History(0) assert.NoError(t, err) if assert.Len(t, tasks, 1) { assert.Equal(t, int64(1), tasks[0].ID) } - webhook = db.AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook) + webhook = unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook) tasks, err = webhook.History(0) assert.NoError(t, err) assert.Len(t, tasks, 0) @@ -46,7 +46,7 @@ func TestWebhook_History(t *testing.T) { func TestWebhook_UpdateEvent(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - webhook := db.AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook) + webhook := unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook) hookEvent := &HookEvent{ PushOnly: true, SendEverything: false, @@ -92,9 +92,9 @@ func TestCreateWebhook(t *testing.T) { ContentType: ContentTypeJSON, Events: `{"push_only":false,"send_everything":false,"choose_events":false,"events":{"create":false,"push":true,"pull_request":true}}`, } - db.AssertNotExistsBean(t, hook) + unittest.AssertNotExistsBean(t, hook) assert.NoError(t, CreateWebhook(db.DefaultContext, hook)) - db.AssertExistsAndLoadBean(t, hook) + unittest.AssertExistsAndLoadBean(t, hook) } func TestGetWebhookByRepoID(t *testing.T) { @@ -103,7 +103,7 @@ func TestGetWebhookByRepoID(t *testing.T) { assert.NoError(t, err) assert.Equal(t, int64(1), hook.ID) - _, err = GetWebhookByRepoID(db.NonexistentID, db.NonexistentID) + _, err = GetWebhookByRepoID(unittest.NonexistentID, unittest.NonexistentID) assert.Error(t, err) assert.True(t, IsErrWebhookNotExist(err)) } @@ -114,7 +114,7 @@ func TestGetWebhookByOrgID(t *testing.T) { assert.NoError(t, err) assert.Equal(t, int64(3), hook.ID) - _, err = GetWebhookByOrgID(db.NonexistentID, db.NonexistentID) + _, err = GetWebhookByOrgID(unittest.NonexistentID, unittest.NonexistentID) assert.Error(t, err) assert.True(t, IsErrWebhookNotExist(err)) } @@ -161,32 +161,32 @@ func TestGetWebhooksByOrgID(t *testing.T) { func TestUpdateWebhook(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - hook := db.AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook) + hook := unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook) hook.IsActive = true hook.ContentType = ContentTypeForm - db.AssertNotExistsBean(t, hook) + unittest.AssertNotExistsBean(t, hook) assert.NoError(t, UpdateWebhook(hook)) - db.AssertExistsAndLoadBean(t, hook) + unittest.AssertExistsAndLoadBean(t, hook) } func TestDeleteWebhookByRepoID(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - db.AssertExistsAndLoadBean(t, &Webhook{ID: 2, RepoID: 1}) + unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 2, RepoID: 1}) assert.NoError(t, DeleteWebhookByRepoID(1, 2)) - db.AssertNotExistsBean(t, &Webhook{ID: 2, RepoID: 1}) + unittest.AssertNotExistsBean(t, &Webhook{ID: 2, RepoID: 1}) - err := DeleteWebhookByRepoID(db.NonexistentID, db.NonexistentID) + err := DeleteWebhookByRepoID(unittest.NonexistentID, unittest.NonexistentID) assert.Error(t, err) assert.True(t, IsErrWebhookNotExist(err)) } func TestDeleteWebhookByOrgID(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - db.AssertExistsAndLoadBean(t, &Webhook{ID: 3, OrgID: 3}) + unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 3, OrgID: 3}) assert.NoError(t, DeleteWebhookByOrgID(3, 3)) - db.AssertNotExistsBean(t, &Webhook{ID: 3, OrgID: 3}) + unittest.AssertNotExistsBean(t, &Webhook{ID: 3, OrgID: 3}) - err := DeleteWebhookByOrgID(db.NonexistentID, db.NonexistentID) + err := DeleteWebhookByOrgID(unittest.NonexistentID, unittest.NonexistentID) assert.Error(t, err) assert.True(t, IsErrWebhookNotExist(err)) } @@ -199,7 +199,7 @@ func TestHookTasks(t *testing.T) { assert.Equal(t, int64(1), hookTasks[0].ID) } - hookTasks, err = HookTasks(db.NonexistentID, 1) + hookTasks, err = HookTasks(unittest.NonexistentID, 1) assert.NoError(t, err) assert.Len(t, hookTasks, 0) } @@ -211,21 +211,21 @@ func TestCreateHookTask(t *testing.T) { HookID: 3, Payloader: &api.PushPayload{}, } - db.AssertNotExistsBean(t, hookTask) + unittest.AssertNotExistsBean(t, hookTask) assert.NoError(t, CreateHookTask(hookTask)) - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) } func TestUpdateHookTask(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - hook := db.AssertExistsAndLoadBean(t, &HookTask{ID: 1}).(*HookTask) + hook := unittest.AssertExistsAndLoadBean(t, &HookTask{ID: 1}).(*HookTask) hook.PayloadContent = "new payload content" hook.DeliveredString = "new delivered string" hook.IsDelivered = true - db.AssertNotExistsBean(t, hook) + unittest.AssertNotExistsBean(t, hook) assert.NoError(t, UpdateHookTask(hook)) - db.AssertExistsAndLoadBean(t, hook) + unittest.AssertExistsAndLoadBean(t, hook) } func TestCleanupHookTaskTable_PerWebhook_DeletesDelivered(t *testing.T) { @@ -237,12 +237,12 @@ func TestCleanupHookTaskTable_PerWebhook_DeletesDelivered(t *testing.T) { IsDelivered: true, Delivered: time.Now().UnixNano(), } - db.AssertNotExistsBean(t, hookTask) + unittest.AssertNotExistsBean(t, hookTask) assert.NoError(t, CreateHookTask(hookTask)) - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) assert.NoError(t, CleanupHookTaskTable(context.Background(), PerWebhook, 168*time.Hour, 0)) - db.AssertNotExistsBean(t, hookTask) + unittest.AssertNotExistsBean(t, hookTask) } func TestCleanupHookTaskTable_PerWebhook_LeavesUndelivered(t *testing.T) { @@ -253,12 +253,12 @@ func TestCleanupHookTaskTable_PerWebhook_LeavesUndelivered(t *testing.T) { Payloader: &api.PushPayload{}, IsDelivered: false, } - db.AssertNotExistsBean(t, hookTask) + unittest.AssertNotExistsBean(t, hookTask) assert.NoError(t, CreateHookTask(hookTask)) - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) assert.NoError(t, CleanupHookTaskTable(context.Background(), PerWebhook, 168*time.Hour, 0)) - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) } func TestCleanupHookTaskTable_PerWebhook_LeavesMostRecentTask(t *testing.T) { @@ -270,12 +270,12 @@ func TestCleanupHookTaskTable_PerWebhook_LeavesMostRecentTask(t *testing.T) { IsDelivered: true, Delivered: time.Now().UnixNano(), } - db.AssertNotExistsBean(t, hookTask) + unittest.AssertNotExistsBean(t, hookTask) assert.NoError(t, CreateHookTask(hookTask)) - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) assert.NoError(t, CleanupHookTaskTable(context.Background(), PerWebhook, 168*time.Hour, 1)) - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) } func TestCleanupHookTaskTable_OlderThan_DeletesDelivered(t *testing.T) { @@ -287,12 +287,12 @@ func TestCleanupHookTaskTable_OlderThan_DeletesDelivered(t *testing.T) { IsDelivered: true, Delivered: time.Now().AddDate(0, 0, -8).UnixNano(), } - db.AssertNotExistsBean(t, hookTask) + unittest.AssertNotExistsBean(t, hookTask) assert.NoError(t, CreateHookTask(hookTask)) - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) assert.NoError(t, CleanupHookTaskTable(context.Background(), OlderThan, 168*time.Hour, 0)) - db.AssertNotExistsBean(t, hookTask) + unittest.AssertNotExistsBean(t, hookTask) } func TestCleanupHookTaskTable_OlderThan_LeavesUndelivered(t *testing.T) { @@ -303,12 +303,12 @@ func TestCleanupHookTaskTable_OlderThan_LeavesUndelivered(t *testing.T) { Payloader: &api.PushPayload{}, IsDelivered: false, } - db.AssertNotExistsBean(t, hookTask) + unittest.AssertNotExistsBean(t, hookTask) assert.NoError(t, CreateHookTask(hookTask)) - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) assert.NoError(t, CleanupHookTaskTable(context.Background(), OlderThan, 168*time.Hour, 0)) - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) } func TestCleanupHookTaskTable_OlderThan_LeavesTaskEarlierThanAgeToDelete(t *testing.T) { @@ -320,10 +320,10 @@ func TestCleanupHookTaskTable_OlderThan_LeavesTaskEarlierThanAgeToDelete(t *test IsDelivered: true, Delivered: time.Now().AddDate(0, 0, -6).UnixNano(), } - db.AssertNotExistsBean(t, hookTask) + unittest.AssertNotExistsBean(t, hookTask) assert.NoError(t, CreateHookTask(hookTask)) - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) assert.NoError(t, CleanupHookTaskTable(context.Background(), OlderThan, 168*time.Hour, 0)) - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) } diff --git a/models/wiki_test.go b/models/wiki_test.go index 211442587..ae6b090db 100644 --- a/models/wiki_test.go +++ b/models/wiki_test.go @@ -8,7 +8,6 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" @@ -18,7 +17,7 @@ import ( func TestRepository_WikiCloneLink(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) cloneLink := repo.WikiCloneLink() assert.Equal(t, "ssh://runuser@try.gitea.io:3000/user2/repo1.wiki.git", cloneLink.SSH) assert.Equal(t, "https://try.gitea.io/user2/repo1.wiki.git", cloneLink.HTTPS) @@ -32,15 +31,15 @@ func TestWikiPath(t *testing.T) { func TestRepository_WikiPath(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) expected := filepath.Join(setting.RepoRootPath, "user2/repo1.wiki.git") assert.Equal(t, expected, repo.WikiPath()) } func TestRepository_HasWiki(t *testing.T) { unittest.PrepareTestEnv(t) - repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) assert.True(t, repo1.HasWiki()) - repo2 := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) + repo2 := unittest.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) assert.False(t, repo2.HasWiki()) } diff --git a/modules/convert/git_commit_test.go b/modules/convert/git_commit_test.go index 3cb55c6a6..aacdb1ad7 100644 --- a/modules/convert/git_commit_test.go +++ b/modules/convert/git_commit_test.go @@ -9,7 +9,6 @@ import ( "time" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" @@ -20,7 +19,7 @@ import ( func TestToCommitMeta(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - headRepo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + headRepo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) sha1, _ := git.NewIDFromString("0000000000000000000000000000000000000000") signature := &git.Signature{Name: "Test Signature", Email: "test@email.com", When: time.Unix(0, 0)} tag := &git.Tag{ diff --git a/modules/convert/issue_test.go b/modules/convert/issue_test.go index e44733c46..21ca7469e 100644 --- a/modules/convert/issue_test.go +++ b/modules/convert/issue_test.go @@ -10,7 +10,6 @@ import ( "time" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -21,8 +20,8 @@ import ( func TestLabel_ToLabel(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - label := db.AssertExistsAndLoadBean(t, &models.Label{ID: 1}).(*models.Label) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: label.RepoID}).(*models.Repository) + label := unittest.AssertExistsAndLoadBean(t, &models.Label{ID: 1}).(*models.Label) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: label.RepoID}).(*models.Repository) assert.Equal(t, &api.Label{ ID: label.ID, Name: label.Name, diff --git a/modules/convert/pull_test.go b/modules/convert/pull_test.go index f5310eb82..844011b8c 100644 --- a/modules/convert/pull_test.go +++ b/modules/convert/pull_test.go @@ -8,7 +8,6 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/structs" @@ -18,8 +17,8 @@ import ( func TestPullRequest_APIFormat(t *testing.T) { //with HeadRepo assert.NoError(t, unittest.PrepareTestDatabase()) - headRepo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - pr := db.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 1}).(*models.PullRequest) + headRepo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + pr := unittest.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 1}).(*models.PullRequest) assert.NoError(t, pr.LoadAttributes()) assert.NoError(t, pr.LoadIssue()) apiPullRequest := ToAPIPullRequest(pr, nil) @@ -33,7 +32,7 @@ func TestPullRequest_APIFormat(t *testing.T) { }, apiPullRequest.Head) //withOut HeadRepo - pr = db.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 1}).(*models.PullRequest) + pr = unittest.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 1}).(*models.PullRequest) assert.NoError(t, pr.LoadIssue()) assert.NoError(t, pr.LoadAttributes()) // simulate fork deletion diff --git a/modules/convert/user_test.go b/modules/convert/user_test.go index fe26456f5..ce174e307 100644 --- a/modules/convert/user_test.go +++ b/modules/convert/user_test.go @@ -8,7 +8,6 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" api "code.gitea.io/gitea/modules/structs" @@ -18,13 +17,13 @@ import ( func TestUser_ToUser(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user1 := db.AssertExistsAndLoadBean(t, &models.User{ID: 1, IsAdmin: true}).(*models.User) + user1 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 1, IsAdmin: true}).(*models.User) apiUser := toUser(user1, true, true) assert.True(t, apiUser.IsAdmin) assert.Contains(t, apiUser.AvatarURL, "://") - user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2, IsAdmin: false}).(*models.User) + user2 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2, IsAdmin: false}).(*models.User) apiUser = toUser(user2, true, true) assert.False(t, apiUser.IsAdmin) @@ -33,7 +32,7 @@ func TestUser_ToUser(t *testing.T) { assert.False(t, apiUser.IsAdmin) assert.EqualValues(t, api.VisibleTypePublic.String(), apiUser.Visibility) - user31 := db.AssertExistsAndLoadBean(t, &models.User{ID: 31, IsAdmin: false, Visibility: api.VisibleTypePrivate}).(*models.User) + user31 := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 31, IsAdmin: false, Visibility: api.VisibleTypePrivate}).(*models.User) apiUser = toUser(user31, true, true) assert.False(t, apiUser.IsAdmin) diff --git a/modules/migrations/gitea_uploader_test.go b/modules/migrations/gitea_uploader_test.go index b5763f30d..99de3884c 100644 --- a/modules/migrations/gitea_uploader_test.go +++ b/modules/migrations/gitea_uploader_test.go @@ -27,7 +27,7 @@ func TestGiteaUploadRepo(t *testing.T) { unittest.PrepareTestEnv(t) - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) var ( downloader = NewGithubDownloaderV3(context.Background(), "https://github.com", "", "", "", "go-xorm", "builder") @@ -52,7 +52,7 @@ func TestGiteaUploadRepo(t *testing.T) { }, nil) assert.NoError(t, err) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{OwnerID: user.ID, Name: repoName}).(*models.Repository) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{OwnerID: user.ID, Name: repoName}).(*models.Repository) assert.True(t, repo.HasWiki()) assert.EqualValues(t, models.RepositoryReady, repo.Status) diff --git a/modules/migrations/migrate_test.go b/modules/migrations/migrate_test.go index aecc263cc..325064697 100644 --- a/modules/migrations/migrate_test.go +++ b/modules/migrations/migrate_test.go @@ -9,7 +9,6 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" @@ -19,8 +18,8 @@ import ( func TestMigrateWhiteBlocklist(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - adminUser := db.AssertExistsAndLoadBean(t, &models.User{Name: "user1"}).(*models.User) - nonAdminUser := db.AssertExistsAndLoadBean(t, &models.User{Name: "user2"}).(*models.User) + adminUser := unittest.AssertExistsAndLoadBean(t, &models.User{Name: "user1"}).(*models.User) + nonAdminUser := unittest.AssertExistsAndLoadBean(t, &models.User{Name: "user2"}).(*models.User) setting.Migrations.AllowedDomains = []string{"github.com"} assert.NoError(t, Init()) diff --git a/modules/notification/action/action_test.go b/modules/notification/action/action_test.go index e57069ed9..3adcae83f 100644 --- a/modules/notification/action/action_test.go +++ b/modules/notification/action/action_test.go @@ -10,7 +10,6 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -22,8 +21,8 @@ func TestMain(m *testing.M) { func TestRenameRepoAction(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{OwnerID: user.ID}).(*models.Repository) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{OwnerID: user.ID}).(*models.Repository) repo.Owner = user oldRepoName := repo.Name @@ -40,10 +39,10 @@ func TestRenameRepoAction(t *testing.T) { IsPrivate: repo.IsPrivate, Content: oldRepoName, } - db.AssertNotExistsBean(t, actionBean) + unittest.AssertNotExistsBean(t, actionBean) NewNotifier().NotifyRenameRepository(user, repo, oldRepoName) - db.AssertExistsAndLoadBean(t, actionBean) - models.CheckConsistencyFor(t, &models.Action{}) + unittest.AssertExistsAndLoadBean(t, actionBean) + unittest.CheckConsistencyFor(t, &models.Action{}) } diff --git a/modules/repofiles/action_test.go b/modules/repofiles/action_test.go index 59cb4df16..d320413db 100644 --- a/modules/repofiles/action_test.go +++ b/modules/repofiles/action_test.go @@ -8,7 +8,6 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" @@ -45,8 +44,8 @@ func TestUpdateIssuesCommit(t *testing.T) { }, } - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) repo.Owner = user commentBean := &models.Comment{ @@ -57,12 +56,12 @@ func TestUpdateIssuesCommit(t *testing.T) { } issueBean := &models.Issue{RepoID: repo.ID, Index: 4} - db.AssertNotExistsBean(t, commentBean) - db.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 2}, "is_closed=1") + unittest.AssertNotExistsBean(t, commentBean) + unittest.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 2}, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch)) - db.AssertExistsAndLoadBean(t, commentBean) - db.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") - models.CheckConsistencyFor(t, &models.Action{}) + unittest.AssertExistsAndLoadBean(t, commentBean) + unittest.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") + unittest.CheckConsistencyFor(t, &models.Action{}) // Test that push to a non-default branch closes no issue. pushCommits = []*repository.PushCommit{ @@ -75,7 +74,7 @@ func TestUpdateIssuesCommit(t *testing.T) { Message: "close #1", }, } - repo = db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) + repo = unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) commentBean = &models.Comment{ Type: models.CommentTypeCommitRef, CommitSHA: "abcdef1", @@ -84,12 +83,12 @@ func TestUpdateIssuesCommit(t *testing.T) { } issueBean = &models.Issue{RepoID: repo.ID, Index: 1} - db.AssertNotExistsBean(t, commentBean) - db.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 1}, "is_closed=1") + unittest.AssertNotExistsBean(t, commentBean) + unittest.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 1}, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch")) - db.AssertExistsAndLoadBean(t, commentBean) - db.AssertNotExistsBean(t, issueBean, "is_closed=1") - models.CheckConsistencyFor(t, &models.Action{}) + unittest.AssertExistsAndLoadBean(t, commentBean) + unittest.AssertNotExistsBean(t, issueBean, "is_closed=1") + unittest.CheckConsistencyFor(t, &models.Action{}) pushCommits = []*repository.PushCommit{ { @@ -101,7 +100,7 @@ func TestUpdateIssuesCommit(t *testing.T) { Message: "close " + setting.AppURL + repo.FullName() + "/pulls/1", }, } - repo = db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) + repo = unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) commentBean = &models.Comment{ Type: models.CommentTypeCommitRef, CommitSHA: "abcdef3", @@ -110,12 +109,12 @@ func TestUpdateIssuesCommit(t *testing.T) { } issueBean = &models.Issue{RepoID: repo.ID, Index: 1} - db.AssertNotExistsBean(t, commentBean) - db.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 1}, "is_closed=1") + unittest.AssertNotExistsBean(t, commentBean) + unittest.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 1}, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch)) - db.AssertExistsAndLoadBean(t, commentBean) - db.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") - models.CheckConsistencyFor(t, &models.Action{}) + unittest.AssertExistsAndLoadBean(t, commentBean) + unittest.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") + unittest.CheckConsistencyFor(t, &models.Action{}) } func TestUpdateIssuesCommit_Colon(t *testing.T) { @@ -131,21 +130,21 @@ func TestUpdateIssuesCommit_Colon(t *testing.T) { }, } - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) repo.Owner = user issueBean := &models.Issue{RepoID: repo.ID, Index: 4} - db.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 2}, "is_closed=1") + unittest.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 2}, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch)) - db.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") - models.CheckConsistencyFor(t, &models.Action{}) + unittest.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") + unittest.CheckConsistencyFor(t, &models.Action{}) } func TestUpdateIssuesCommit_Issue5957(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // Test that push to a non-default branch closes an issue. pushCommits := []*repository.PushCommit{ @@ -159,7 +158,7 @@ func TestUpdateIssuesCommit_Issue5957(t *testing.T) { }, } - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) commentBean := &models.Comment{ Type: models.CommentTypeCommitRef, CommitSHA: "abcdef1", @@ -169,17 +168,17 @@ func TestUpdateIssuesCommit_Issue5957(t *testing.T) { issueBean := &models.Issue{RepoID: repo.ID, Index: 2, ID: 7} - db.AssertNotExistsBean(t, commentBean) - db.AssertNotExistsBean(t, issueBean, "is_closed=1") + unittest.AssertNotExistsBean(t, commentBean) + unittest.AssertNotExistsBean(t, issueBean, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch")) - db.AssertExistsAndLoadBean(t, commentBean) - db.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") - models.CheckConsistencyFor(t, &models.Action{}) + unittest.AssertExistsAndLoadBean(t, commentBean) + unittest.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") + unittest.CheckConsistencyFor(t, &models.Action{}) } func TestUpdateIssuesCommit_AnotherRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user := unittest.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 @@ -194,7 +193,7 @@ func TestUpdateIssuesCommit_AnotherRepo(t *testing.T) { }, } - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) commentBean := &models.Comment{ Type: models.CommentTypeCommitRef, CommitSHA: "abcdef1", @@ -204,17 +203,17 @@ func TestUpdateIssuesCommit_AnotherRepo(t *testing.T) { issueBean := &models.Issue{RepoID: 1, Index: 1, ID: 1} - db.AssertNotExistsBean(t, commentBean) - db.AssertNotExistsBean(t, issueBean, "is_closed=1") + unittest.AssertNotExistsBean(t, commentBean) + unittest.AssertNotExistsBean(t, issueBean, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch)) - db.AssertExistsAndLoadBean(t, commentBean) - db.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") - models.CheckConsistencyFor(t, &models.Action{}) + unittest.AssertExistsAndLoadBean(t, commentBean) + unittest.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") + unittest.CheckConsistencyFor(t, &models.Action{}) } func TestUpdateIssuesCommit_AnotherRepo_FullAddress(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + user := unittest.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 @@ -229,7 +228,7 @@ func TestUpdateIssuesCommit_AnotherRepo_FullAddress(t *testing.T) { }, } - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) commentBean := &models.Comment{ Type: models.CommentTypeCommitRef, CommitSHA: "abcdef1", @@ -239,17 +238,17 @@ func TestUpdateIssuesCommit_AnotherRepo_FullAddress(t *testing.T) { issueBean := &models.Issue{RepoID: 1, Index: 1, ID: 1} - db.AssertNotExistsBean(t, commentBean) - db.AssertNotExistsBean(t, issueBean, "is_closed=1") + unittest.AssertNotExistsBean(t, commentBean) + unittest.AssertNotExistsBean(t, issueBean, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch)) - db.AssertExistsAndLoadBean(t, commentBean) - db.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") - models.CheckConsistencyFor(t, &models.Action{}) + unittest.AssertExistsAndLoadBean(t, commentBean) + unittest.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") + unittest.CheckConsistencyFor(t, &models.Action{}) } func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 10}).(*models.User) + user := unittest.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 @@ -272,7 +271,7 @@ func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) { }, } - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 6}).(*models.Repository) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 6}).(*models.Repository) commentBean := &models.Comment{ Type: models.CommentTypeCommitRef, CommitSHA: "abcdef3", @@ -288,12 +287,12 @@ func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) { issueBean := &models.Issue{RepoID: 3, Index: 1, ID: 6} - db.AssertNotExistsBean(t, commentBean) - db.AssertNotExistsBean(t, commentBean2) - db.AssertNotExistsBean(t, issueBean, "is_closed=1") + unittest.AssertNotExistsBean(t, commentBean) + unittest.AssertNotExistsBean(t, commentBean2) + unittest.AssertNotExistsBean(t, issueBean, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch)) - db.AssertNotExistsBean(t, commentBean) - db.AssertNotExistsBean(t, commentBean2) - db.AssertNotExistsBean(t, issueBean, "is_closed=1") - models.CheckConsistencyFor(t, &models.Action{}) + unittest.AssertNotExistsBean(t, commentBean) + unittest.AssertNotExistsBean(t, commentBean2) + unittest.AssertNotExistsBean(t, issueBean, "is_closed=1") + unittest.CheckConsistencyFor(t, &models.Action{}) } diff --git a/modules/repository/commits_test.go b/modules/repository/commits_test.go index c47d9cf48..0d6c2e4c5 100644 --- a/modules/repository/commits_test.go +++ b/modules/repository/commits_test.go @@ -11,7 +11,6 @@ import ( "time" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" "github.com/stretchr/testify/assert" @@ -49,7 +48,7 @@ func TestPushCommits_ToAPIPayloadCommits(t *testing.T) { } pushCommits.HeadCommit = &PushCommit{Sha1: "69554a6"} - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) payloadCommits, headCommit, err := pushCommits.ToAPIPayloadCommits(repo.RepoPath(), "/user2/repo16") assert.NoError(t, err) assert.Len(t, payloadCommits, 3) diff --git a/modules/repository/create_test.go b/modules/repository/create_test.go index d83301716..2e27ab0d7 100644 --- a/modules/repository/create_test.go +++ b/modules/repository/create_test.go @@ -9,7 +9,6 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/structs" @@ -20,7 +19,7 @@ func TestIncludesAllRepositoriesTeams(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) testTeamRepositories := func(teamID int64, repoIds []int64) { - team := db.AssertExistsAndLoadBean(t, &models.Team{ID: teamID}).(*models.Team) + team := unittest.AssertExistsAndLoadBean(t, &models.Team{ID: teamID}).(*models.Team) assert.NoError(t, team.GetRepositories(&models.SearchTeamOptions{}), "%s: GetRepositories", team.Name) assert.Len(t, team.Repos, team.NumRepos, "%s: len repo", team.Name) assert.Len(t, team.Repos, len(repoIds), "%s: repo count", team.Name) diff --git a/modules/repository/fork_test.go b/modules/repository/fork_test.go index bfb813adb..197d76b05 100644 --- a/modules/repository/fork_test.go +++ b/modules/repository/fork_test.go @@ -8,7 +8,6 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -17,8 +16,8 @@ func TestForkRepository(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) // user 13 has already forked repo10 - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 13}).(*models.User) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 10}).(*models.Repository) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 13}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 10}).(*models.Repository) fork, err := ForkRepository(user, user, models.ForkRepoOptions{ BaseRepo: repo, diff --git a/modules/test/context_tests.go b/modules/test/context_tests.go index 02723d8c3..eb1a54d86 100644 --- a/modules/test/context_tests.go +++ b/modules/test/context_tests.go @@ -14,7 +14,7 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/web/middleware" @@ -53,7 +53,7 @@ func MockContext(t *testing.T, path string) *context.Context { // LoadRepo load a repo into a test context. func LoadRepo(t *testing.T, ctx *context.Context, repoID int64) { ctx.Repo = &context.Repository{} - ctx.Repo.Repository = db.AssertExistsAndLoadBean(t, &models.Repository{ID: repoID}).(*models.Repository) + ctx.Repo.Repository = unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: repoID}).(*models.Repository) var err error ctx.Repo.Owner, err = models.GetUserByID(ctx.Repo.Repository.OwnerID) assert.NoError(t, err) @@ -78,7 +78,7 @@ func LoadRepoCommit(t *testing.T, ctx *context.Context) { // LoadUser load a user into a test context. func LoadUser(t *testing.T, ctx *context.Context, userID int64) { - ctx.User = db.AssertExistsAndLoadBean(t, &models.User{ID: userID}).(*models.User) + ctx.User = unittest.AssertExistsAndLoadBean(t, &models.User{ID: userID}).(*models.User) } // LoadGitRepo load a git repo into a test context. Requires that ctx.Repo has diff --git a/modules/unittestbridge/unittestbridge.go b/modules/unittestbridge/unittestbridge.go deleted file mode 100644 index 273cf5e70..000000000 --- a/modules/unittestbridge/unittestbridge.go +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright 2021 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package unittestbridge - -// Usage: generally, non-unit-test code shouldn't depend on unit test code. -// However, we have some code like models.CheckConsistencyFor, which need to do some unit test works. -// Now we can not decouple models.CheckConsistencyFor from unit test code easily (cycle-import reasons). -// So we introduce this `unit test bridge`: -// * When a release binary is built, no testing/assert framework would be compiled into the binary, and CheckConsistencyFor won't run unit test related code -// * When a unit test binary is built, the unit test code will init this bridge, then CheckConsistencyFor can run unit test related code -// -// Tester/Assert are intermediate interfaces, they should NOT be used in new code. -// One day, if CheckConsistencyFor is clean enough, we can remove these intermediate interfaces. - -// Tester is the same as TestingT in "stretchr/testify/assert" -// Tester can be used in non-unit-test code (ex: models.CheckConsistencyFor), it is used to isolate dependencies -type Tester interface { - Errorf(format string, args ...interface{}) -} - -// Asserter can be used in non-unit-test code (ex: models.CheckConsistencyFor), it is used to isolate dependencies -type Asserter interface { - Tester - NoError(err error, msgAndArgs ...interface{}) bool - EqualValues(expected, actual interface{}, msgAndArgs ...interface{}) bool - Equal(expected, actual interface{}, msgAndArgs ...interface{}) bool - True(value bool, msgAndArgs ...interface{}) bool - False(value bool, msgAndArgs ...interface{}) bool -} - -var newAsserterFunc func(t Tester) Asserter - -// NewAsserter returns a new asserter, only works in unit test -func NewAsserter(t Tester) Asserter { - if newAsserterFunc == nil { - panic("the newAsserterFunc is not set. you can only use assert in unit test.") - } - return newAsserterFunc(t) -} - -// SetNewAsserterFunc in unit test, the asserter must be set first -func SetNewAsserterFunc(f func(t Tester) Asserter) { - newAsserterFunc = f -} diff --git a/routers/api/v1/repo/hook_test.go b/routers/api/v1/repo/hook_test.go index ff0aaf293..07f1532f8 100644 --- a/routers/api/v1/repo/hook_test.go +++ b/routers/api/v1/repo/hook_test.go @@ -8,7 +8,6 @@ import ( "net/http" "testing" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/context" @@ -28,8 +27,8 @@ func TestTestHook(t *testing.T) { TestHook(&context.APIContext{Context: ctx, Org: nil}) assert.EqualValues(t, http.StatusNoContent, ctx.Resp.Status()) - db.AssertExistsAndLoadBean(t, &webhook.HookTask{ + unittest.AssertExistsAndLoadBean(t, &webhook.HookTask{ RepoID: 1, HookID: 1, - }, db.Cond("is_delivered=?", false)) + }, unittest.Cond("is_delivered=?", false)) } diff --git a/routers/api/v1/repo/repo_test.go b/routers/api/v1/repo/repo_test.go index b992b93fd..4d4093582 100644 --- a/routers/api/v1/repo/repo_test.go +++ b/routers/api/v1/repo/repo_test.go @@ -9,7 +9,6 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/context" api "code.gitea.io/gitea/modules/structs" @@ -61,9 +60,9 @@ func TestRepoEdit(t *testing.T) { Edit(apiCtx) assert.EqualValues(t, http.StatusOK, ctx.Resp.Status()) - db.AssertExistsAndLoadBean(t, &models.Repository{ + unittest.AssertExistsAndLoadBean(t, &models.Repository{ ID: 1, - }, db.Cond("name = ? AND is_archived = 1", *opts.Name)) + }, unittest.Cond("name = ? AND is_archived = 1", *opts.Name)) } func TestRepoEditNameChange(t *testing.T) { @@ -83,7 +82,7 @@ func TestRepoEditNameChange(t *testing.T) { Edit(apiCtx) assert.EqualValues(t, http.StatusOK, ctx.Resp.Status()) - db.AssertExistsAndLoadBean(t, &models.Repository{ + unittest.AssertExistsAndLoadBean(t, &models.Repository{ ID: 1, - }, db.Cond("name = ?", opts.Name)) + }, unittest.Cond("name = ?", opts.Name)) } diff --git a/routers/web/admin/users_test.go b/routers/web/admin/users_test.go index 62b35ea28..607ef2ea6 100644 --- a/routers/web/admin/users_test.go +++ b/routers/web/admin/users_test.go @@ -8,7 +8,6 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -24,7 +23,7 @@ func TestNewUserPost_MustChangePassword(t *testing.T) { unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "admin/users/new") - u := db.AssertExistsAndLoadBean(t, &models.User{ + u := unittest.AssertExistsAndLoadBean(t, &models.User{ IsAdmin: true, ID: 2, }).(*models.User) @@ -61,7 +60,7 @@ func TestNewUserPost_MustChangePasswordFalse(t *testing.T) { unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "admin/users/new") - u := db.AssertExistsAndLoadBean(t, &models.User{ + u := unittest.AssertExistsAndLoadBean(t, &models.User{ IsAdmin: true, ID: 2, }).(*models.User) @@ -98,7 +97,7 @@ func TestNewUserPost_InvalidEmail(t *testing.T) { unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "admin/users/new") - u := db.AssertExistsAndLoadBean(t, &models.User{ + u := unittest.AssertExistsAndLoadBean(t, &models.User{ IsAdmin: true, ID: 2, }).(*models.User) @@ -128,7 +127,7 @@ func TestNewUserPost_VisibilityDefaultPublic(t *testing.T) { unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "admin/users/new") - u := db.AssertExistsAndLoadBean(t, &models.User{ + u := unittest.AssertExistsAndLoadBean(t, &models.User{ IsAdmin: true, ID: 2, }).(*models.User) @@ -166,7 +165,7 @@ func TestNewUserPost_VisibilityPrivate(t *testing.T) { unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "admin/users/new") - u := db.AssertExistsAndLoadBean(t, &models.User{ + u := unittest.AssertExistsAndLoadBean(t, &models.User{ IsAdmin: true, ID: 2, }).(*models.User) diff --git a/routers/web/repo/issue_label_test.go b/routers/web/repo/issue_label_test.go index 98fa9eb96..baa34530f 100644 --- a/routers/web/repo/issue_label_test.go +++ b/routers/web/repo/issue_label_test.go @@ -10,7 +10,6 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/modules/web" @@ -38,7 +37,7 @@ func TestInitializeLabels(t *testing.T) { web.SetForm(ctx, &forms.InitializeLabelsForm{TemplateName: "Default"}) InitializeLabels(ctx) assert.EqualValues(t, http.StatusFound, ctx.Resp.Status()) - db.AssertExistsAndLoadBean(t, &models.Label{ + unittest.AssertExistsAndLoadBean(t, &models.Label{ RepoID: 2, Name: "enhancement", Color: "#84b6eb", @@ -84,7 +83,7 @@ func TestNewLabel(t *testing.T) { }) NewLabel(ctx) assert.EqualValues(t, http.StatusFound, ctx.Resp.Status()) - db.AssertExistsAndLoadBean(t, &models.Label{ + unittest.AssertExistsAndLoadBean(t, &models.Label{ Name: "newlabel", Color: "#abcdef", }) @@ -103,7 +102,7 @@ func TestUpdateLabel(t *testing.T) { }) UpdateLabel(ctx) assert.EqualValues(t, http.StatusFound, ctx.Resp.Status()) - db.AssertExistsAndLoadBean(t, &models.Label{ + unittest.AssertExistsAndLoadBean(t, &models.Label{ ID: 2, Name: "newnameforlabel", Color: "#abcdef", @@ -119,8 +118,8 @@ func TestDeleteLabel(t *testing.T) { ctx.Req.Form.Set("id", "2") DeleteLabel(ctx) assert.EqualValues(t, http.StatusOK, ctx.Resp.Status()) - db.AssertNotExistsBean(t, &models.Label{ID: 2}) - db.AssertNotExistsBean(t, &models.IssueLabel{LabelID: 2}) + unittest.AssertNotExistsBean(t, &models.Label{ID: 2}) + unittest.AssertNotExistsBean(t, &models.IssueLabel{LabelID: 2}) assert.Equal(t, ctx.Tr("repo.issues.label_deletion_success"), ctx.Flash.SuccessMsg) } @@ -133,9 +132,9 @@ func TestUpdateIssueLabel_Clear(t *testing.T) { ctx.Req.Form.Set("action", "clear") UpdateIssueLabel(ctx) assert.EqualValues(t, http.StatusOK, ctx.Resp.Status()) - db.AssertNotExistsBean(t, &models.IssueLabel{IssueID: 1}) - db.AssertNotExistsBean(t, &models.IssueLabel{IssueID: 3}) - models.CheckConsistencyFor(t, &models.Label{}) + unittest.AssertNotExistsBean(t, &models.IssueLabel{IssueID: 1}) + unittest.AssertNotExistsBean(t, &models.IssueLabel{IssueID: 3}) + unittest.CheckConsistencyFor(t, &models.Label{}) } func TestUpdateIssueLabel_Toggle(t *testing.T) { @@ -160,11 +159,11 @@ func TestUpdateIssueLabel_Toggle(t *testing.T) { UpdateIssueLabel(ctx) assert.EqualValues(t, http.StatusOK, ctx.Resp.Status()) for _, issueID := range testCase.IssueIDs { - db.AssertExistsIf(t, testCase.ExpectedAdd, &models.IssueLabel{ + unittest.AssertExistsIf(t, testCase.ExpectedAdd, &models.IssueLabel{ IssueID: issueID, LabelID: testCase.LabelID, }) } - models.CheckConsistencyFor(t, &models.Label{}) + unittest.CheckConsistencyFor(t, &models.Label{}) } } diff --git a/routers/web/repo/release_test.go b/routers/web/repo/release_test.go index ff54232da..33cf54cdc 100644 --- a/routers/web/repo/release_test.go +++ b/routers/web/repo/release_test.go @@ -8,7 +8,6 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/modules/web" @@ -53,14 +52,14 @@ func TestNewReleasePost(t *testing.T) { test.LoadGitRepo(t, ctx) web.SetForm(ctx, &testCase.Form) NewReleasePost(ctx) - db.AssertExistsAndLoadBean(t, &models.Release{ + unittest.AssertExistsAndLoadBean(t, &models.Release{ RepoID: 1, PublisherID: 2, TagName: testCase.Form.TagName, Target: testCase.Form.Target, Title: testCase.Form.Title, Note: testCase.Form.Content, - }, db.Cond("is_draft=?", len(testCase.Form.Draft) > 0)) + }, unittest.Cond("is_draft=?", len(testCase.Form.Draft) > 0)) ctx.Repo.GitRepo.Close() } } diff --git a/routers/web/repo/settings_test.go b/routers/web/repo/settings_test.go index 78426f5b7..3e8ae2b07 100644 --- a/routers/web/repo/settings_test.go +++ b/routers/web/repo/settings_test.go @@ -10,7 +10,6 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/setting" @@ -59,7 +58,7 @@ func TestAddReadOnlyDeployKey(t *testing.T) { DeployKeysPost(ctx) assert.EqualValues(t, http.StatusFound, ctx.Resp.Status()) - db.AssertExistsAndLoadBean(t, &models.DeployKey{ + unittest.AssertExistsAndLoadBean(t, &models.DeployKey{ Name: addKeyForm.Title, Content: addKeyForm.Content, Mode: models.AccessModeRead, @@ -89,7 +88,7 @@ func TestAddReadWriteOnlyDeployKey(t *testing.T) { DeployKeysPost(ctx) assert.EqualValues(t, http.StatusFound, ctx.Resp.Status()) - db.AssertExistsAndLoadBean(t, &models.DeployKey{ + unittest.AssertExistsAndLoadBean(t, &models.DeployKey{ Name: addKeyForm.Title, Content: addKeyForm.Content, Mode: models.AccessModeWrite, diff --git a/routers/web/user/oauth_test.go b/routers/web/user/oauth_test.go index e0c1bf4e7..dfdaa9a1e 100644 --- a/routers/web/user/oauth_test.go +++ b/routers/web/user/oauth_test.go @@ -8,7 +8,6 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/login" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/services/auth/source/oauth2" @@ -59,7 +58,7 @@ func TestNewAccessTokenResponse_OIDCToken(t *testing.T) { assert.Empty(t, oidcToken.Email) assert.False(t, oidcToken.EmailVerified) - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 5}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 5}).(*models.User) grants, err = login.GetOAuth2GrantsByUserID(user.ID) assert.NoError(t, err) assert.Len(t, grants, 1) diff --git a/services/attachment/attachment_test.go b/services/attachment/attachment_test.go index f44c4f9b5..5bb5db11e 100644 --- a/services/attachment/attachment_test.go +++ b/services/attachment/attachment_test.go @@ -10,7 +10,6 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" @@ -23,7 +22,7 @@ func TestMain(m *testing.M) { func TestUploadAttachment(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - user := db.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) fPath := "./attachment_test.go" f, err := os.Open(fPath) diff --git a/services/gitdiff/gitdiff_test.go b/services/gitdiff/gitdiff_test.go index 359e2e8c6..aefd396eb 100644 --- a/services/gitdiff/gitdiff_test.go +++ b/services/gitdiff/gitdiff_test.go @@ -13,7 +13,6 @@ import ( "testing" "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" @@ -496,8 +495,8 @@ func setupDefaultDiff() *Diff { func TestDiff_LoadComments(t *testing.T) { 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) + issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) diff := setupDefaultDiff() assert.NoError(t, diff.LoadComments(issue, user)) assert.Len(t, diff.Files[0].Sections[0].Lines[0].Comments, 2) diff --git a/services/issue/label_test.go b/services/issue/label_test.go index 758ef98b2..fa6ad613b 100644 --- a/services/issue/label_test.go +++ b/services/issue/label_test.go @@ -8,7 +8,6 @@ import ( "testing" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "github.com/stretchr/testify/assert" ) @@ -26,15 +25,15 @@ func TestIssue_AddLabels(t *testing.T) { } for _, test := range tests { assert.NoError(t, unittest.PrepareTestDatabase()) - issue := db.AssertExistsAndLoadBean(t, &models.Issue{ID: test.issueID}).(*models.Issue) + issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: test.issueID}).(*models.Issue) labels := make([]*models.Label, len(test.labelIDs)) for i, labelID := range test.labelIDs { - labels[i] = db.AssertExistsAndLoadBean(t, &models.Label{ID: labelID}).(*models.Label) + labels[i] = unittest.AssertExistsAndLoadBean(t, &models.Label{ID: labelID}).(*models.Label) } - doer := db.AssertExistsAndLoadBean(t, &models.User{ID: test.doerID}).(*models.User) + doer := unittest.AssertExistsAndLoadBean(t, &models.User{ID: test.doerID}).(*models.User) assert.NoError(t, AddLabels(issue, doer, labels)) for _, labelID := range test.labelIDs { - db.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: test.issueID, LabelID: labelID}) + unittest.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: test.issueID, LabelID: labelID}) } } } @@ -52,10 +51,10 @@ func TestIssue_AddLabel(t *testing.T) { } for _, test := range tests { 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) + issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: test.issueID}).(*models.Issue) + label := unittest.AssertExistsAndLoadBean(t, &models.Label{ID: test.labelID}).(*models.Label) + doer := unittest.AssertExistsAndLoadBean(t, &models.User{ID: test.doerID}).(*models.User) assert.NoError(t, AddLabel(issue, doer, label)) - db.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: test.issueID, LabelID: test.labelID}) + unittest.AssertExistsAndLoadBean(t, &models.IssueLabel{IssueID: test.issueID, LabelID: test.labelID}) } } diff --git a/services/mailer/mail_test.go b/services/mailer/mail_test.go index aaa18681b..94ff5a65a 100644 --- a/services/mailer/mail_test.go +++ b/services/mailer/mail_test.go @@ -11,7 +11,6 @@ import ( texttmpl "text/template" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/setting" @@ -50,11 +49,11 @@ func prepareMailerTest(t *testing.T) (doer *models.User, repo *models.Repository setting.MailService = &mailService setting.Domain = "localhost" - doer = db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo = db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1, Owner: doer}).(*models.Repository) - issue = db.AssertExistsAndLoadBean(t, &models.Issue{ID: 1, Repo: repo, Poster: doer}).(*models.Issue) + doer = unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo = unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1, Owner: doer}).(*models.Repository) + issue = unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 1, Repo: repo, Poster: doer}).(*models.Issue) assert.NoError(t, issue.LoadRepo()) - comment = db.AssertExistsAndLoadBean(t, &models.Comment{ID: 2, Issue: issue}).(*models.Comment) + comment = unittest.AssertExistsAndLoadBean(t, &models.Comment{ID: 2, Issue: issue}).(*models.Comment) return } @@ -145,8 +144,8 @@ func TestTemplateSelection(t *testing.T) { Content: "test body", Comment: comment}, recipients, false, "TestTemplateSelection") expect(t, msg, "issue/default/subject", "issue/default/body") - pull := db.AssertExistsAndLoadBean(t, &models.Issue{ID: 2, Repo: repo, Poster: doer}).(*models.Issue) - comment = db.AssertExistsAndLoadBean(t, &models.Comment{ID: 4, Issue: pull}).(*models.Comment) + pull := unittest.AssertExistsAndLoadBean(t, &models.Issue{ID: 2, Repo: repo, Poster: doer}).(*models.Issue) + comment = unittest.AssertExistsAndLoadBean(t, &models.Comment{ID: 4, Issue: pull}).(*models.Comment) msg = testComposeIssueCommentMessage(t, &mailCommentContext{Issue: pull, Doer: doer, ActionType: models.ActionCommentPull, Content: "test body", Comment: comment}, recipients, false, "TestTemplateSelection") expect(t, msg, "pull/comment/subject", "pull/comment/body") diff --git a/services/pull/check_test.go b/services/pull/check_test.go index bc3df0a4b..f0ec096ea 100644 --- a/services/pull/check_test.go +++ b/services/pull/check_test.go @@ -11,7 +11,6 @@ import ( "time" "code.gitea.io/gitea/models" - "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/queue" @@ -43,11 +42,11 @@ func TestPullRequest_AddToTaskQueue(t *testing.T) { prQueue = q.(queue.UniqueQueue) - pr := db.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest) + pr := unittest.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest) AddToTaskQueue(pr) assert.Eventually(t, func() bool { - pr = db.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest) + pr = unittest.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest) return pr.Status == models.PullRequestStatusChecking }, 1*time.Second, 100*time.Millisecond) @@ -72,7 +71,7 @@ func TestPullRequest_AddToTaskQueue(t *testing.T) { assert.False(t, has) assert.NoError(t, err) - pr = db.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest) + pr = unittest.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 2}).(*models.PullRequest) assert.Equal(t, models.PullRequestStatusChecking, pr.Status) for _, callback := range queueShutdown { diff --git a/services/release/release_test.go b/services/release/release_test.go index b855ec664..d720bf996 100644 --- a/services/release/release_test.go +++ b/services/release/release_test.go @@ -11,7 +11,6 @@ import ( "time" "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" @@ -26,8 +25,8 @@ func TestMain(m *testing.M) { func TestRelease_Create(t *testing.T) { 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) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) repoPath := models.RepoPath(user.Name, repo.Name) gitRepo, err := git.OpenRepository(repoPath) @@ -130,8 +129,8 @@ func TestRelease_Create(t *testing.T) { func TestRelease_Update(t *testing.T) { 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) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) repoPath := models.RepoPath(user.Name, repo.Name) gitRepo, err := git.OpenRepository(repoPath) @@ -272,8 +271,8 @@ func TestRelease_Update(t *testing.T) { func TestRelease_createTag(t *testing.T) { 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) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) repoPath := models.RepoPath(user.Name, repo.Name) gitRepo, err := git.OpenRepository(repoPath) @@ -354,8 +353,8 @@ func TestRelease_createTag(t *testing.T) { func TestCreateNewTag(t *testing.T) { 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) + user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) assert.NoError(t, CreateNewTag(user, repo, "master", "v2.0", "v2.0 is released \n\n BUGFIX: .... \n\n 123")) diff --git a/services/repository/transfer_test.go b/services/repository/transfer_test.go index 9ce60f30e..09c982921 100644 --- a/services/repository/transfer_test.go +++ b/services/repository/transfer_test.go @@ -9,7 +9,6 @@ import ( "testing" "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" @@ -31,12 +30,12 @@ func TestTransferOwnership(t *testing.T) { 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) - repo.Owner = db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + doer := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) + repo.Owner = unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) assert.NoError(t, TransferOwnership(doer, doer, repo, nil)) - transferredRepo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) + transferredRepo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) assert.EqualValues(t, 2, transferredRepo.OwnerID) exist, err := util.IsExist(models.RepoPath("user3", "repo3")) @@ -45,23 +44,23 @@ func TestTransferOwnership(t *testing.T) { exist, err = util.IsExist(models.RepoPath("user2", "repo3")) assert.NoError(t, err) assert.True(t, exist) - db.AssertExistsAndLoadBean(t, &models.Action{ + unittest.AssertExistsAndLoadBean(t, &models.Action{ OpType: models.ActionTransferRepo, ActUserID: 2, RepoID: 3, Content: "user3/repo3", }) - models.CheckConsistencyFor(t, &models.Repository{}, &models.User{}, &models.Team{}) + unittest.CheckConsistencyFor(t, &models.Repository{}, &models.User{}, &models.Team{}) } func TestStartRepositoryTransferSetPermission(t *testing.T) { 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) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) - repo.Owner = db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + doer := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) + recipient := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 5}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) + repo.Owner = unittest.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) hasAccess, err := models.HasAccess(recipient.ID, repo) assert.NoError(t, err) @@ -73,5 +72,5 @@ func TestStartRepositoryTransferSetPermission(t *testing.T) { assert.NoError(t, err) assert.True(t, hasAccess) - models.CheckConsistencyFor(t, &models.Repository{}, &models.User{}, &models.Team{}) + unittest.CheckConsistencyFor(t, &models.Repository{}, &models.User{}, &models.Team{}) } diff --git a/services/webhook/webhook_test.go b/services/webhook/webhook_test.go index 276d1e78c..0a649d36a 100644 --- a/services/webhook/webhook_test.go +++ b/services/webhook/webhook_test.go @@ -8,7 +8,6 @@ import ( "testing" "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" @@ -30,50 +29,50 @@ func TestWebhook_GetSlackHook(t *testing.T) { func TestPrepareWebhooks(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) hookTasks := []*webhook_model.HookTask{ {RepoID: repo.ID, HookID: 1, EventType: webhook_model.HookEventPush}, } for _, hookTask := range hookTasks { - db.AssertNotExistsBean(t, hookTask) + unittest.AssertNotExistsBean(t, hookTask) } assert.NoError(t, PrepareWebhooks(repo, webhook_model.HookEventPush, &api.PushPayload{Commits: []*api.PayloadCommit{{}}})) for _, hookTask := range hookTasks { - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) } } func TestPrepareWebhooksBranchFilterMatch(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) hookTasks := []*webhook_model.HookTask{ {RepoID: repo.ID, HookID: 4, EventType: webhook_model.HookEventPush}, } for _, hookTask := range hookTasks { - db.AssertNotExistsBean(t, hookTask) + unittest.AssertNotExistsBean(t, hookTask) } // this test also ensures that * doesn't handle / in any special way (like shell would) assert.NoError(t, PrepareWebhooks(repo, webhook_model.HookEventPush, &api.PushPayload{Ref: "refs/heads/feature/7791", Commits: []*api.PayloadCommit{{}}})) for _, hookTask := range hookTasks { - db.AssertExistsAndLoadBean(t, hookTask) + unittest.AssertExistsAndLoadBean(t, hookTask) } } func TestPrepareWebhooksBranchFilterNoMatch(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) hookTasks := []*webhook_model.HookTask{ {RepoID: repo.ID, HookID: 4, EventType: webhook_model.HookEventPush}, } for _, hookTask := range hookTasks { - db.AssertNotExistsBean(t, hookTask) + unittest.AssertNotExistsBean(t, hookTask) } assert.NoError(t, PrepareWebhooks(repo, webhook_model.HookEventPush, &api.PushPayload{Ref: "refs/heads/fix_weird_bug"})) for _, hookTask := range hookTasks { - db.AssertNotExistsBean(t, hookTask) + unittest.AssertNotExistsBean(t, hookTask) } } diff --git a/services/wiki/wiki_test.go b/services/wiki/wiki_test.go index 7ecd3bb04..ee548d231 100644 --- a/services/wiki/wiki_test.go +++ b/services/wiki/wiki_test.go @@ -10,7 +10,6 @@ import ( "testing" "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" @@ -113,11 +112,11 @@ func TestWikiNameToFilenameToName(t *testing.T) { func TestRepository_InitWiki(t *testing.T) { unittest.PrepareTestEnv(t) // repo1 already has a wiki - repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + repo1 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) assert.NoError(t, InitWiki(repo1)) // repo2 does not already have a wiki - repo2 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) + repo2 := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) assert.NoError(t, InitWiki(repo2)) assert.True(t, repo2.HasWiki()) } @@ -126,8 +125,8 @@ func TestRepository_AddWikiPage(t *testing.T) { 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) - doer := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + doer := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) for _, wikiName := range []string{ "Another page", "Here's a and a/slash", @@ -171,8 +170,8 @@ func TestRepository_EditWikiPage(t *testing.T) { const newWikiContent = "This is the new content" const commitMsg = "Commit message" - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - doer := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + doer := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) for _, newWikiName := range []string{ "Home", // same name as before "New home", @@ -201,8 +200,8 @@ func TestRepository_EditWikiPage(t *testing.T) { func TestRepository_DeleteWikiPage(t *testing.T) { unittest.PrepareTestEnv(t) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - doer := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + doer := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) assert.NoError(t, DeleteWikiPage(doer, repo, "Home")) // Now need to show that the page has been added: @@ -218,7 +217,7 @@ func TestRepository_DeleteWikiPage(t *testing.T) { func TestPrepareWikiFileName(t *testing.T) { unittest.PrepareTestEnv(t) - repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + repo := unittest.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) gitRepo, err := git.OpenRepository(repo.WikiPath()) defer gitRepo.Close() assert.NoError(t, err)