mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 16:40:24 +08:00 
			
		
		
		
	Refactor AssertExistsAndLoadBean to use generics (#20797)
* Refactor AssertExistsAndLoadBean to use generics * Fix tests Co-authored-by: zeripath <art27@cantab.net>
This commit is contained in:
		@@ -27,7 +27,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 := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: itarget.ID, RefIssueID: pr.ID, RefCommentID: 0}).(*issues_model.Comment)
 | 
			
		||||
	ref := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: itarget.ID, RefIssueID: pr.ID, RefCommentID: 0})
 | 
			
		||||
	assert.Equal(t, issues_model.CommentTypePullRef, ref.Type)
 | 
			
		||||
	assert.Equal(t, pr.RepoID, ref.RefRepoID)
 | 
			
		||||
	assert.True(t, ref.RefIsPull)
 | 
			
		||||
@@ -36,7 +36,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 = unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: itarget.ID, RefIssueID: pr.ID, RefCommentID: c.ID}).(*issues_model.Comment)
 | 
			
		||||
	ref = unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: itarget.ID, RefIssueID: pr.ID, RefCommentID: c.ID})
 | 
			
		||||
	assert.Equal(t, issues_model.CommentTypeCommentRef, ref.Type)
 | 
			
		||||
	assert.Equal(t, pr.RepoID, ref.RefRepoID)
 | 
			
		||||
	assert.True(t, ref.RefIsPull)
 | 
			
		||||
@@ -45,7 +45,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 = unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*issues_model.Comment)
 | 
			
		||||
	ref = unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0})
 | 
			
		||||
	assert.Equal(t, issues_model.CommentTypeIssueRef, ref.Type)
 | 
			
		||||
	assert.Equal(t, pr.RepoID, ref.RefRepoID)
 | 
			
		||||
	assert.False(t, ref.RefIsPull)
 | 
			
		||||
@@ -57,7 +57,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 = unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*issues_model.Comment)
 | 
			
		||||
	ref = unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0})
 | 
			
		||||
	assert.Equal(t, issues_model.CommentTypeIssueRef, ref.Type)
 | 
			
		||||
	assert.Equal(t, i.RepoID, ref.RefRepoID)
 | 
			
		||||
	assert.False(t, ref.RefIsPull)
 | 
			
		||||
@@ -78,15 +78,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 := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*issues_model.Comment)
 | 
			
		||||
	ref := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0})
 | 
			
		||||
	assert.Equal(t, issues_model.CommentTypeIssueRef, ref.Type)
 | 
			
		||||
	assert.Equal(t, references.XRefActionNone, ref.RefAction)
 | 
			
		||||
 | 
			
		||||
	d := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
 | 
			
		||||
	d := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
 | 
			
		||||
	i.Title = "title2, no mentions"
 | 
			
		||||
	assert.NoError(t, issues_model.ChangeIssueTitle(i, d, title))
 | 
			
		||||
 | 
			
		||||
	ref = unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*issues_model.Comment)
 | 
			
		||||
	ref = unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0})
 | 
			
		||||
	assert.Equal(t, issues_model.CommentTypeIssueRef, ref.Type)
 | 
			
		||||
	assert.Equal(t, references.XRefActionNeutered, ref.RefAction)
 | 
			
		||||
}
 | 
			
		||||
@@ -94,7 +94,7 @@ func TestXRef_NeuterCrossReferences(t *testing.T) {
 | 
			
		||||
func TestXRef_ResolveCrossReferences(t *testing.T) {
 | 
			
		||||
	assert.NoError(t, unittest.PrepareTestDatabase())
 | 
			
		||||
 | 
			
		||||
	d := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
 | 
			
		||||
	d := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
 | 
			
		||||
 | 
			
		||||
	i1 := testCreateIssue(t, 1, 2, "title1", "content1", false)
 | 
			
		||||
	i2 := testCreateIssue(t, 1, 2, "title2", "content2", false)
 | 
			
		||||
@@ -103,10 +103,10 @@ func TestXRef_ResolveCrossReferences(t *testing.T) {
 | 
			
		||||
	assert.NoError(t, err)
 | 
			
		||||
 | 
			
		||||
	pr := testCreatePR(t, 1, 2, "titlepr", fmt.Sprintf("closes #%d", i1.Index))
 | 
			
		||||
	rp := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: i1.ID, RefIssueID: pr.Issue.ID, RefCommentID: 0}).(*issues_model.Comment)
 | 
			
		||||
	rp := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: i1.ID, RefIssueID: pr.Issue.ID, RefCommentID: 0})
 | 
			
		||||
 | 
			
		||||
	c1 := testCreateComment(t, 1, 2, pr.Issue.ID, fmt.Sprintf("closes #%d", i2.Index))
 | 
			
		||||
	r1 := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: i2.ID, RefIssueID: pr.Issue.ID, RefCommentID: c1.ID}).(*issues_model.Comment)
 | 
			
		||||
	r1 := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: i2.ID, RefIssueID: pr.Issue.ID, RefCommentID: c1.ID})
 | 
			
		||||
 | 
			
		||||
	// Must be ignored
 | 
			
		||||
	c2 := testCreateComment(t, 1, 2, pr.Issue.ID, fmt.Sprintf("mentions #%d", i2.Index))
 | 
			
		||||
@@ -117,7 +117,7 @@ func TestXRef_ResolveCrossReferences(t *testing.T) {
 | 
			
		||||
	unittest.AssertExistsAndLoadBean(t, &issues_model.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 := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: i3.ID, RefIssueID: pr.Issue.ID, RefCommentID: c4.ID}).(*issues_model.Comment)
 | 
			
		||||
	r4 := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: i3.ID, RefIssueID: pr.Issue.ID, RefCommentID: c4.ID})
 | 
			
		||||
 | 
			
		||||
	refs, err := pr.ResolveCrossReferences(db.DefaultContext)
 | 
			
		||||
	assert.NoError(t, err)
 | 
			
		||||
@@ -128,8 +128,8 @@ func TestXRef_ResolveCrossReferences(t *testing.T) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func testCreateIssue(t *testing.T, repo, doer int64, title, content string, ispull bool) *issues_model.Issue {
 | 
			
		||||
	r := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repo}).(*repo_model.Repository)
 | 
			
		||||
	d := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: doer}).(*user_model.User)
 | 
			
		||||
	r := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repo})
 | 
			
		||||
	d := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: doer})
 | 
			
		||||
 | 
			
		||||
	idx, err := db.GetNextResourceIndex("issue_index", r.ID)
 | 
			
		||||
	assert.NoError(t, err)
 | 
			
		||||
@@ -159,8 +159,8 @@ func testCreateIssue(t *testing.T, repo, doer int64, title, content string, ispu
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func testCreatePR(t *testing.T, repo, doer int64, title, content string) *issues_model.PullRequest {
 | 
			
		||||
	r := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repo}).(*repo_model.Repository)
 | 
			
		||||
	d := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: doer}).(*user_model.User)
 | 
			
		||||
	r := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repo})
 | 
			
		||||
	d := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: doer})
 | 
			
		||||
	i := &issues_model.Issue{RepoID: r.ID, PosterID: d.ID, Poster: d, Title: title, Content: content, IsPull: true}
 | 
			
		||||
	pr := &issues_model.PullRequest{HeadRepoID: repo, BaseRepoID: repo, HeadBranch: "head", BaseBranch: "base", Status: issues_model.PullRequestStatusMergeable}
 | 
			
		||||
	assert.NoError(t, issues_model.NewPullRequest(db.DefaultContext, r, i, nil, nil, pr))
 | 
			
		||||
@@ -169,8 +169,8 @@ func testCreatePR(t *testing.T, repo, doer int64, title, content string) *issues
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func testCreateComment(t *testing.T, repo, doer, issue int64, content string) *issues_model.Comment {
 | 
			
		||||
	d := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: doer}).(*user_model.User)
 | 
			
		||||
	i := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: issue}).(*issues_model.Issue)
 | 
			
		||||
	d := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: doer})
 | 
			
		||||
	i := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: issue})
 | 
			
		||||
	c := &issues_model.Comment{Type: issues_model.CommentTypeComment, PosterID: doer, Poster: d, IssueID: issue, Issue: i, Content: content}
 | 
			
		||||
 | 
			
		||||
	ctx, committer, err := db.TxContext()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user