You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
204 lines
8.6 KiB
204 lines
8.6 KiB
5 years ago
|
// Copyright 2020 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.
|
||
|
|
||
3 years ago
|
package issues_test
|
||
6 years ago
|
|
||
|
import (
|
||
|
"testing"
|
||
|
|
||
3 years ago
|
"code.gitea.io/gitea/models/db"
|
||
3 years ago
|
issues_model "code.gitea.io/gitea/models/issues"
|
||
3 years ago
|
"code.gitea.io/gitea/models/unittest"
|
||
3 years ago
|
user_model "code.gitea.io/gitea/models/user"
|
||
3 years ago
|
|
||
6 years ago
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func TestGetReviewByID(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
3 years ago
|
review, err := issues_model.GetReviewByID(db.DefaultContext, 1)
|
||
6 years ago
|
assert.NoError(t, err)
|
||
|
assert.Equal(t, "Demo Review", review.Content)
|
||
3 years ago
|
assert.Equal(t, issues_model.ReviewTypeApprove, review.Type)
|
||
6 years ago
|
|
||
3 years ago
|
_, err = issues_model.GetReviewByID(db.DefaultContext, 23892)
|
||
6 years ago
|
assert.Error(t, err)
|
||
3 years ago
|
assert.True(t, issues_model.IsErrReviewNotExist(err), "IsErrReviewNotExist")
|
||
6 years ago
|
}
|
||
|
|
||
|
func TestReview_LoadAttributes(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
3 years ago
|
review := unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 1}).(*issues_model.Review)
|
||
3 years ago
|
assert.NoError(t, review.LoadAttributes(db.DefaultContext))
|
||
6 years ago
|
assert.NotNil(t, review.Issue)
|
||
|
assert.NotNil(t, review.Reviewer)
|
||
|
|
||
3 years ago
|
invalidReview1 := unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 2}).(*issues_model.Review)
|
||
3 years ago
|
assert.Error(t, invalidReview1.LoadAttributes(db.DefaultContext))
|
||
6 years ago
|
|
||
3 years ago
|
invalidReview2 := unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 3}).(*issues_model.Review)
|
||
3 years ago
|
assert.Error(t, invalidReview2.LoadAttributes(db.DefaultContext))
|
||
6 years ago
|
}
|
||
|
|
||
|
func TestReview_LoadCodeComments(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
6 years ago
|
|
||
3 years ago
|
review := unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 4}).(*issues_model.Review)
|
||
3 years ago
|
assert.NoError(t, review.LoadAttributes(db.DefaultContext))
|
||
|
assert.NoError(t, review.LoadCodeComments(db.DefaultContext))
|
||
6 years ago
|
assert.Len(t, review.CodeComments, 1)
|
||
|
assert.Equal(t, int64(4), review.CodeComments["README.md"][int64(4)][0].Line)
|
||
|
}
|
||
|
|
||
|
func TestReviewType_Icon(t *testing.T) {
|
||
3 years ago
|
assert.Equal(t, "check", issues_model.ReviewTypeApprove.Icon())
|
||
|
assert.Equal(t, "diff", issues_model.ReviewTypeReject.Icon())
|
||
|
assert.Equal(t, "comment", issues_model.ReviewTypeComment.Icon())
|
||
|
assert.Equal(t, "comment", issues_model.ReviewTypeUnknown.Icon())
|
||
|
assert.Equal(t, "dot-fill", issues_model.ReviewTypeRequest.Icon())
|
||
|
assert.Equal(t, "comment", issues_model.ReviewType(6).Icon())
|
||
6 years ago
|
}
|
||
|
|
||
|
func TestFindReviews(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
3 years ago
|
reviews, err := issues_model.FindReviews(db.DefaultContext, issues_model.FindReviewOptions{
|
||
|
Type: issues_model.ReviewTypeApprove,
|
||
6 years ago
|
IssueID: 2,
|
||
|
ReviewerID: 1,
|
||
|
})
|
||
|
assert.NoError(t, err)
|
||
|
assert.Len(t, reviews, 1)
|
||
|
assert.Equal(t, "Demo Review", reviews[0].Content)
|
||
|
}
|
||
|
|
||
|
func TestGetCurrentReview(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
3 years ago
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2}).(*issues_model.Issue)
|
||
3 years ago
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
||
6 years ago
|
|
||
3 years ago
|
review, err := issues_model.GetCurrentReview(db.DefaultContext, user, issue)
|
||
6 years ago
|
assert.NoError(t, err)
|
||
|
assert.NotNil(t, review)
|
||
3 years ago
|
assert.Equal(t, issues_model.ReviewTypePending, review.Type)
|
||
6 years ago
|
assert.Equal(t, "Pending Review", review.Content)
|
||
|
|
||
3 years ago
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 7}).(*user_model.User)
|
||
3 years ago
|
review2, err := issues_model.GetCurrentReview(db.DefaultContext, user2, issue)
|
||
6 years ago
|
assert.Error(t, err)
|
||
3 years ago
|
assert.True(t, issues_model.IsErrReviewNotExist(err))
|
||
6 years ago
|
assert.Nil(t, review2)
|
||
|
}
|
||
|
|
||
|
func TestCreateReview(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
6 years ago
|
|
||
3 years ago
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2}).(*issues_model.Issue)
|
||
3 years ago
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
||
6 years ago
|
|
||
3 years ago
|
review, err := issues_model.CreateReview(db.DefaultContext, issues_model.CreateReviewOptions{
|
||
6 years ago
|
Content: "New Review",
|
||
3 years ago
|
Type: issues_model.ReviewTypePending,
|
||
6 years ago
|
Issue: issue,
|
||
|
Reviewer: user,
|
||
|
})
|
||
|
assert.NoError(t, err)
|
||
|
assert.Equal(t, "New Review", review.Content)
|
||
3 years ago
|
unittest.AssertExistsAndLoadBean(t, &issues_model.Review{Content: "New Review"})
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
func TestGetReviewersByIssueID(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
6 years ago
|
|
||
3 years ago
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 3}).(*issues_model.Issue)
|
||
3 years ago
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
|
||
|
user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}).(*user_model.User)
|
||
|
user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}).(*user_model.User)
|
||
6 years ago
|
|
||
3 years ago
|
expectedReviews := []*issues_model.Review{}
|
||
5 years ago
|
expectedReviews = append(expectedReviews,
|
||
3 years ago
|
&issues_model.Review{
|
||
5 years ago
|
Reviewer: user3,
|
||
3 years ago
|
Type: issues_model.ReviewTypeReject,
|
||
5 years ago
|
UpdatedUnix: 946684812,
|
||
6 years ago
|
},
|
||
3 years ago
|
&issues_model.Review{
|
||
5 years ago
|
Reviewer: user4,
|
||
3 years ago
|
Type: issues_model.ReviewTypeApprove,
|
||
5 years ago
|
UpdatedUnix: 946684813,
|
||
|
},
|
||
3 years ago
|
&issues_model.Review{
|
||
5 years ago
|
Reviewer: user2,
|
||
3 years ago
|
Type: issues_model.ReviewTypeReject,
|
||
5 years ago
|
UpdatedUnix: 946684814,
|
||
6 years ago
|
})
|
||
|
|
||
3 years ago
|
allReviews, err := issues_model.GetReviewersByIssueID(issue.ID)
|
||
4 years ago
|
for _, reviewer := range allReviews {
|
||
|
assert.NoError(t, reviewer.LoadReviewer())
|
||
|
}
|
||
6 years ago
|
assert.NoError(t, err)
|
||
5 years ago
|
if assert.Len(t, allReviews, 3) {
|
||
|
for i, review := range allReviews {
|
||
|
assert.Equal(t, expectedReviews[i].Reviewer, review.Reviewer)
|
||
|
assert.Equal(t, expectedReviews[i].Type, review.Type)
|
||
|
assert.Equal(t, expectedReviews[i].UpdatedUnix, review.UpdatedUnix)
|
||
|
}
|
||
5 years ago
|
}
|
||
6 years ago
|
}
|
||
4 years ago
|
|
||
|
func TestDismissReview(t *testing.T) {
|
||
3 years ago
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
||
4 years ago
|
|
||
3 years ago
|
rejectReviewExample := unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 9}).(*issues_model.Review)
|
||
|
requestReviewExample := unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 11}).(*issues_model.Review)
|
||
|
approveReviewExample := unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 8}).(*issues_model.Review)
|
||
4 years ago
|
assert.False(t, rejectReviewExample.Dismissed)
|
||
|
assert.False(t, requestReviewExample.Dismissed)
|
||
|
assert.False(t, approveReviewExample.Dismissed)
|
||
|
|
||
3 years ago
|
assert.NoError(t, issues_model.DismissReview(rejectReviewExample, true))
|
||
|
rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 9}).(*issues_model.Review)
|
||
|
requestReviewExample = unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 11}).(*issues_model.Review)
|
||
4 years ago
|
assert.True(t, rejectReviewExample.Dismissed)
|
||
|
assert.False(t, requestReviewExample.Dismissed)
|
||
|
|
||
3 years ago
|
assert.NoError(t, issues_model.DismissReview(requestReviewExample, true))
|
||
|
rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 9}).(*issues_model.Review)
|
||
|
requestReviewExample = unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 11}).(*issues_model.Review)
|
||
4 years ago
|
assert.True(t, rejectReviewExample.Dismissed)
|
||
|
assert.False(t, requestReviewExample.Dismissed)
|
||
|
assert.False(t, approveReviewExample.Dismissed)
|
||
|
|
||
3 years ago
|
assert.NoError(t, issues_model.DismissReview(requestReviewExample, true))
|
||
|
rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 9}).(*issues_model.Review)
|
||
|
requestReviewExample = unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 11}).(*issues_model.Review)
|
||
4 years ago
|
assert.True(t, rejectReviewExample.Dismissed)
|
||
|
assert.False(t, requestReviewExample.Dismissed)
|
||
|
assert.False(t, approveReviewExample.Dismissed)
|
||
|
|
||
3 years ago
|
assert.NoError(t, issues_model.DismissReview(requestReviewExample, false))
|
||
|
rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 9}).(*issues_model.Review)
|
||
|
requestReviewExample = unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 11}).(*issues_model.Review)
|
||
4 years ago
|
assert.True(t, rejectReviewExample.Dismissed)
|
||
|
assert.False(t, requestReviewExample.Dismissed)
|
||
|
assert.False(t, approveReviewExample.Dismissed)
|
||
|
|
||
3 years ago
|
assert.NoError(t, issues_model.DismissReview(requestReviewExample, false))
|
||
|
rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 9}).(*issues_model.Review)
|
||
|
requestReviewExample = unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 11}).(*issues_model.Review)
|
||
4 years ago
|
assert.True(t, rejectReviewExample.Dismissed)
|
||
|
assert.False(t, requestReviewExample.Dismissed)
|
||
|
assert.False(t, approveReviewExample.Dismissed)
|
||
|
|
||
3 years ago
|
assert.NoError(t, issues_model.DismissReview(rejectReviewExample, false))
|
||
4 years ago
|
assert.False(t, rejectReviewExample.Dismissed)
|
||
|
assert.False(t, requestReviewExample.Dismissed)
|
||
|
assert.False(t, approveReviewExample.Dismissed)
|
||
|
|
||
3 years ago
|
assert.NoError(t, issues_model.DismissReview(approveReviewExample, true))
|
||
4 years ago
|
assert.False(t, rejectReviewExample.Dismissed)
|
||
|
assert.False(t, requestReviewExample.Dismissed)
|
||
|
assert.True(t, approveReviewExample.Dismissed)
|
||
4 years ago
|
}
|