2020-04-05 09:20:50 +03:00
|
|
|
// 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.
|
|
|
|
|
2018-08-06 07:43:22 +03:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-01-20 02:26:57 +03:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-12 17:36:47 +03:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 12:49:20 +03:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-17 15:34:35 +03:00
|
|
|
|
2018-08-06 07:43:22 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetReviewByID(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-05-20 17:08:52 +03:00
|
|
|
review, err := GetReviewByID(db.DefaultContext, 1)
|
2018-08-06 07:43:22 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "Demo Review", review.Content)
|
|
|
|
assert.Equal(t, ReviewTypeApprove, review.Type)
|
|
|
|
|
2022-05-20 17:08:52 +03:00
|
|
|
_, err = GetReviewByID(db.DefaultContext, 23892)
|
2018-08-06 07:43:22 +03:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrReviewNotExist(err), "IsErrReviewNotExist")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReview_LoadAttributes(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-16 11:53:21 +03:00
|
|
|
review := unittest.AssertExistsAndLoadBean(t, &Review{ID: 1}).(*Review)
|
2022-01-20 02:26:57 +03:00
|
|
|
assert.NoError(t, review.LoadAttributes(db.DefaultContext))
|
2018-08-06 07:43:22 +03:00
|
|
|
assert.NotNil(t, review.Issue)
|
|
|
|
assert.NotNil(t, review.Reviewer)
|
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
invalidReview1 := unittest.AssertExistsAndLoadBean(t, &Review{ID: 2}).(*Review)
|
2022-01-20 02:26:57 +03:00
|
|
|
assert.Error(t, invalidReview1.LoadAttributes(db.DefaultContext))
|
2018-08-06 07:43:22 +03:00
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
invalidReview2 := unittest.AssertExistsAndLoadBean(t, &Review{ID: 3}).(*Review)
|
2022-01-20 02:26:57 +03:00
|
|
|
assert.Error(t, invalidReview2.LoadAttributes(db.DefaultContext))
|
2018-08-06 07:43:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReview_LoadCodeComments(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2018-08-06 07:43:22 +03:00
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
review := unittest.AssertExistsAndLoadBean(t, &Review{ID: 4}).(*Review)
|
2022-01-20 02:26:57 +03:00
|
|
|
assert.NoError(t, review.LoadAttributes(db.DefaultContext))
|
|
|
|
assert.NoError(t, review.LoadCodeComments(db.DefaultContext))
|
2018-08-06 07:43:22 +03:00
|
|
|
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) {
|
2020-04-03 08:12:42 +03:00
|
|
|
assert.Equal(t, "check", ReviewTypeApprove.Icon())
|
2020-07-17 18:15:12 +03:00
|
|
|
assert.Equal(t, "diff", ReviewTypeReject.Icon())
|
2018-08-06 07:43:22 +03:00
|
|
|
assert.Equal(t, "comment", ReviewTypeComment.Icon())
|
|
|
|
assert.Equal(t, "comment", ReviewTypeUnknown.Icon())
|
2020-07-17 18:15:12 +03:00
|
|
|
assert.Equal(t, "dot-fill", ReviewTypeRequest.Icon())
|
2020-04-06 19:33:34 +03:00
|
|
|
assert.Equal(t, "comment", ReviewType(6).Icon())
|
2018-08-06 07:43:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindReviews(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-05-20 17:08:52 +03:00
|
|
|
reviews, err := FindReviews(db.DefaultContext, FindReviewOptions{
|
2018-08-06 07:43:22 +03:00
|
|
|
Type: ReviewTypeApprove,
|
|
|
|
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) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-16 11:53:21 +03:00
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue)
|
2021-11-24 12:49:20 +03:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
2018-08-06 07:43:22 +03:00
|
|
|
|
2022-05-20 17:08:52 +03:00
|
|
|
review, err := GetCurrentReview(db.DefaultContext, user, issue)
|
2018-08-06 07:43:22 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, review)
|
|
|
|
assert.Equal(t, ReviewTypePending, review.Type)
|
|
|
|
assert.Equal(t, "Pending Review", review.Content)
|
|
|
|
|
2021-11-24 12:49:20 +03:00
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 7}).(*user_model.User)
|
2022-05-20 17:08:52 +03:00
|
|
|
review2, err := GetCurrentReview(db.DefaultContext, user2, issue)
|
2018-08-06 07:43:22 +03:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrReviewNotExist(err))
|
|
|
|
assert.Nil(t, review2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateReview(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2018-08-06 07:43:22 +03:00
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue)
|
2021-11-24 12:49:20 +03:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
|
2018-08-06 07:43:22 +03:00
|
|
|
|
2022-05-20 17:08:52 +03:00
|
|
|
review, err := CreateReview(db.DefaultContext, CreateReviewOptions{
|
2018-08-06 07:43:22 +03:00
|
|
|
Content: "New Review",
|
|
|
|
Type: ReviewTypePending,
|
|
|
|
Issue: issue,
|
|
|
|
Reviewer: user,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "New Review", review.Content)
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &Review{Content: "New Review"})
|
2018-08-06 07:43:22 +03:00
|
|
|
}
|
|
|
|
|
2019-12-04 04:08:56 +03:00
|
|
|
func TestGetReviewersByIssueID(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2018-11-22 16:17:36 +03:00
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 3}).(*Issue)
|
2021-11-24 12:49:20 +03:00
|
|
|
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)
|
2018-11-22 16:17:36 +03:00
|
|
|
|
2019-12-04 04:08:56 +03:00
|
|
|
expectedReviews := []*Review{}
|
|
|
|
expectedReviews = append(expectedReviews,
|
|
|
|
&Review{
|
|
|
|
Reviewer: user3,
|
|
|
|
Type: ReviewTypeReject,
|
|
|
|
UpdatedUnix: 946684812,
|
2018-11-22 16:17:36 +03:00
|
|
|
},
|
2019-12-04 04:08:56 +03:00
|
|
|
&Review{
|
|
|
|
Reviewer: user4,
|
|
|
|
Type: ReviewTypeApprove,
|
|
|
|
UpdatedUnix: 946684813,
|
|
|
|
},
|
|
|
|
&Review{
|
|
|
|
Reviewer: user2,
|
|
|
|
Type: ReviewTypeReject,
|
|
|
|
UpdatedUnix: 946684814,
|
2018-11-22 16:17:36 +03:00
|
|
|
})
|
|
|
|
|
2019-12-04 04:08:56 +03:00
|
|
|
allReviews, err := GetReviewersByIssueID(issue.ID)
|
2020-10-12 22:55:13 +03:00
|
|
|
for _, reviewer := range allReviews {
|
|
|
|
assert.NoError(t, reviewer.LoadReviewer())
|
|
|
|
}
|
2018-11-22 16:17:36 +03:00
|
|
|
assert.NoError(t, err)
|
2020-05-02 03:20:51 +03:00
|
|
|
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)
|
|
|
|
}
|
2019-12-04 04:08:56 +03:00
|
|
|
}
|
2018-11-22 16:17:36 +03:00
|
|
|
}
|
2021-02-11 20:32:25 +03:00
|
|
|
|
|
|
|
func TestDismissReview(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-04-15 13:03:11 +03:00
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
rejectReviewExample := unittest.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review)
|
|
|
|
requestReviewExample := unittest.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review)
|
|
|
|
approveReviewExample := unittest.AssertExistsAndLoadBean(t, &Review{ID: 8}).(*Review)
|
2021-04-15 13:03:11 +03:00
|
|
|
assert.False(t, rejectReviewExample.Dismissed)
|
|
|
|
assert.False(t, requestReviewExample.Dismissed)
|
|
|
|
assert.False(t, approveReviewExample.Dismissed)
|
|
|
|
|
|
|
|
assert.NoError(t, DismissReview(rejectReviewExample, true))
|
2021-11-16 11:53:21 +03:00
|
|
|
rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review)
|
|
|
|
requestReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review)
|
2021-04-15 13:03:11 +03:00
|
|
|
assert.True(t, rejectReviewExample.Dismissed)
|
|
|
|
assert.False(t, requestReviewExample.Dismissed)
|
|
|
|
|
|
|
|
assert.NoError(t, DismissReview(requestReviewExample, true))
|
2021-11-16 11:53:21 +03:00
|
|
|
rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review)
|
|
|
|
requestReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review)
|
2021-04-15 13:03:11 +03:00
|
|
|
assert.True(t, rejectReviewExample.Dismissed)
|
|
|
|
assert.False(t, requestReviewExample.Dismissed)
|
|
|
|
assert.False(t, approveReviewExample.Dismissed)
|
|
|
|
|
|
|
|
assert.NoError(t, DismissReview(requestReviewExample, true))
|
2021-11-16 11:53:21 +03:00
|
|
|
rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review)
|
|
|
|
requestReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review)
|
2021-04-15 13:03:11 +03:00
|
|
|
assert.True(t, rejectReviewExample.Dismissed)
|
|
|
|
assert.False(t, requestReviewExample.Dismissed)
|
|
|
|
assert.False(t, approveReviewExample.Dismissed)
|
|
|
|
|
|
|
|
assert.NoError(t, DismissReview(requestReviewExample, false))
|
2021-11-16 11:53:21 +03:00
|
|
|
rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review)
|
|
|
|
requestReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review)
|
2021-04-15 13:03:11 +03:00
|
|
|
assert.True(t, rejectReviewExample.Dismissed)
|
|
|
|
assert.False(t, requestReviewExample.Dismissed)
|
|
|
|
assert.False(t, approveReviewExample.Dismissed)
|
|
|
|
|
|
|
|
assert.NoError(t, DismissReview(requestReviewExample, false))
|
2021-11-16 11:53:21 +03:00
|
|
|
rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review)
|
|
|
|
requestReviewExample = unittest.AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review)
|
2021-04-15 13:03:11 +03:00
|
|
|
assert.True(t, rejectReviewExample.Dismissed)
|
|
|
|
assert.False(t, requestReviewExample.Dismissed)
|
|
|
|
assert.False(t, approveReviewExample.Dismissed)
|
|
|
|
|
|
|
|
assert.NoError(t, DismissReview(rejectReviewExample, false))
|
|
|
|
assert.False(t, rejectReviewExample.Dismissed)
|
|
|
|
assert.False(t, requestReviewExample.Dismissed)
|
|
|
|
assert.False(t, approveReviewExample.Dismissed)
|
|
|
|
|
|
|
|
assert.NoError(t, DismissReview(approveReviewExample, true))
|
|
|
|
assert.False(t, rejectReviewExample.Dismissed)
|
|
|
|
assert.False(t, requestReviewExample.Dismissed)
|
|
|
|
assert.True(t, approveReviewExample.Dismissed)
|
2021-02-11 20:32:25 +03:00
|
|
|
}
|