Repository: go-gitea/gitea
File: models/issues/review_test.go (Go)

Code before:
	issue := pull.Issue
	assert.NoError(t, issue.LoadRepo(t.Context()))
	reviewer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
	_, err := issues_model.CreateReview(t.Context(), issues_model.CreateReviewOptions{
		Issue:    issue,
		Reviewer: reviewer,
		Type:     issues_model.ReviewTypeReject,
	})

	assert.NoError(t, err)
	pull.HasMerged = false
	assert.NoError(t, pull.UpdateCols(t.Context(), "has_merged"))
	issue.IsClosed = true
	_, err = issues_model.AddReviewRequest(t.Context(), issue, reviewer, &user_model.User{}, false)
	assert.Error(t, err)
	assert.True(t, issues_model.IsErrReviewRequestOnClosedPR(err))

	pull.HasMerged = true
	assert.NoError(t, pull.UpdateCols(t.Context(), "has_merged"))
	issue.IsClosed = false
	_, err = issues_model.AddReviewRequest(t.Context(), issue, reviewer, &user_model.User{}, false)
	assert.Error(t, err)
	assert.True(t, issues_model.IsErrReviewRequestOnClosedPR(err))
}

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

	pull := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
	assert.NoError(t, pull.LoadIssue(t.Context()))
	issue := pull.Issue
	assert.NoError(t, issue.LoadRepo(t.Context()))

	reviewer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 7})
	doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})

	// Test: Add review request with isCodeOwners=true
	comment, err := issues_model.AddReviewRequest(t.Context(), issue, reviewer, doer, true)
	assert.NoError(t, err)
	assert.NotNil(t, comment)

	// Assert: Comment created with correct type
	assert.Equal(t, issues_model.CommentTypeReviewRequest, comment.Type)
	assert.Equal(t, reviewer.ID, comment.AssigneeID)
	assert.Equal(t, doer.ID, comment.PosterID)

	// Assert: Review created
	assert.NotNil(t, comment.Review)
	assert.Equal(t, issues_model.ReviewTypeRequest, comment.Review.Type)
	assert.Equal(t, reviewer.ID, comment.Review.ReviewerID)


Review comment (suggestion):
These hundreds lines of tests were added for the copied & pasted `AddCodeOwnersReviewRequest` function

Now, the existing `AddReviewRequest` is reused, only one line is added, I think all the new tests should be removed. Only need to add a new case for `AddReviewRequest(..., true)` into the existing tests.

Code after:
	assert.NoError(t, issue.LoadRepo(t.Context()))
	reviewer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
	_, err := issues_model.CreateReview(t.Context(), issues_model.CreateReviewOptions{
		Issue:    issue,
		Reviewer: reviewer,
		Type:     issues_model.ReviewTypeReject,
	})

	assert.NoError(t, err)
	pull.HasMerged = false
	assert.NoError(t, pull.UpdateCols(t.Context(), "has_merged"))
	issue.IsClosed = true
	_, err = issues_model.AddReviewRequest(t.Context(), issue, reviewer, &user_model.User{}, false)
	assert.Error(t, err)
	assert.True(t, issues_model.IsErrReviewRequestOnClosedPR(err))

	pull.HasMerged = true
	assert.NoError(t, pull.UpdateCols(t.Context(), "has_merged"))
	issue.IsClosed = false
	_, err = issues_model.AddReviewRequest(t.Context(), issue, reviewer, &user_model.User{}, false)
	assert.Error(t, err)
	assert.True(t, issues_model.IsErrReviewRequestOnClosedPR(err))

	// Test CODEOWNERS review request stores metadata correctly
	pull2 := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
	assert.NoError(t, pull2.LoadIssue(t.Context()))
	issue2 := pull2.Issue
	assert.NoError(t, issue2.LoadRepo(t.Context()))
	reviewer2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 7})
	doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})

	comment, err := issues_model.AddReviewRequest(t.Context(), issue2, reviewer2, doer, true)
	assert.NoError(t, err)
	assert.NotNil(t, comment)
	assert.NotNil(t, comment.CommentMetaData)
	assert.Equal(t, issues_model.SpecialDoerNameCodeOwners, comment.CommentMetaData.SpecialDoerName)
}