mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-01 13:44:06 +01:00
da50be7360
Currently 'userxx' and 'orgxx' are both used as username in test files when the user type is org, which is confusing. This PR replaces all 'userxx' with 'orgxx' when the user type is org(`user.type==1`). Some non-trivial changes 1. Rename `user3` dir to `org3` in `tests/git-repositories-meta` 2. Change `end` in `issue reference` because 'org3' is one char shorter than 'user3' ![ksnip_20230913-112819](https://github.com/go-gitea/gitea/assets/70063547/442988c5-4cf4-49b8-aa01-4dd6bf0ca954) 3. Change the search result number of `user/repo2` because `user3/repo21` can't be searched now ![ksnip_20230913-112931](https://github.com/go-gitea/gitea/assets/70063547/d9ebeba4-479f-4110-9a85-825efbc981fd) 4. Change the first org name getting from API because the result is ordered by alphabet asc and now `org 17` is before `org25` ![JW8U7NIO(J$H _YCRB36H)T](https://github.com/go-gitea/gitea/assets/70063547/f55a685c-cf24-40e5-a87f-3a2327319548) ![)KFD411O4I8RB5ZOH7E0 Z3](https://github.com/go-gitea/gitea/assets/70063547/a0dc3299-249c-46f6-91cb-d15d4ee88dd5) Other modifications are just find all and replace all. Unit tests with SQLite are all passed. --------- Co-authored-by: caicandong <1290147055@qq.com>
260 lines
10 KiB
Go
260 lines
10 KiB
Go
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package issues_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
|
"code.gitea.io/gitea/models/unittest"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestGetReviewByID(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
review, err := issues_model.GetReviewByID(db.DefaultContext, 1)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "Demo Review", review.Content)
|
|
assert.Equal(t, issues_model.ReviewTypeApprove, review.Type)
|
|
|
|
_, err = issues_model.GetReviewByID(db.DefaultContext, 23892)
|
|
assert.Error(t, err)
|
|
assert.True(t, issues_model.IsErrReviewNotExist(err), "IsErrReviewNotExist")
|
|
}
|
|
|
|
func TestReview_LoadAttributes(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
review := unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 1})
|
|
assert.NoError(t, review.LoadAttributes(db.DefaultContext))
|
|
assert.NotNil(t, review.Issue)
|
|
assert.NotNil(t, review.Reviewer)
|
|
|
|
invalidReview1 := unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 2})
|
|
assert.Error(t, invalidReview1.LoadAttributes(db.DefaultContext))
|
|
|
|
invalidReview2 := unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 3})
|
|
assert.Error(t, invalidReview2.LoadAttributes(db.DefaultContext))
|
|
}
|
|
|
|
func TestReview_LoadCodeComments(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
review := unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 4})
|
|
assert.NoError(t, review.LoadAttributes(db.DefaultContext))
|
|
assert.NoError(t, review.LoadCodeComments(db.DefaultContext))
|
|
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) {
|
|
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())
|
|
}
|
|
|
|
func TestFindReviews(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
reviews, err := issues_model.FindReviews(db.DefaultContext, issues_model.FindReviewOptions{
|
|
Type: issues_model.ReviewTypeApprove,
|
|
IssueID: 2,
|
|
ReviewerID: 1,
|
|
})
|
|
assert.NoError(t, err)
|
|
assert.Len(t, reviews, 1)
|
|
assert.Equal(t, "Demo Review", reviews[0].Content)
|
|
}
|
|
|
|
func TestFindLatestReviews(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
reviews, err := issues_model.FindLatestReviews(db.DefaultContext, issues_model.FindReviewOptions{
|
|
Type: issues_model.ReviewTypeApprove,
|
|
IssueID: 11,
|
|
})
|
|
assert.NoError(t, err)
|
|
assert.Len(t, reviews, 2)
|
|
assert.Equal(t, "duplicate review from user5 (latest)", reviews[0].Content)
|
|
assert.Equal(t, "singular review from org6 and final review for this pr", reviews[1].Content)
|
|
}
|
|
|
|
func TestGetCurrentReview(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
|
|
|
review, err := issues_model.GetCurrentReview(db.DefaultContext, user, issue)
|
|
assert.NoError(t, err)
|
|
assert.NotNil(t, review)
|
|
assert.Equal(t, issues_model.ReviewTypePending, review.Type)
|
|
assert.Equal(t, "Pending Review", review.Content)
|
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 7})
|
|
review2, err := issues_model.GetCurrentReview(db.DefaultContext, user2, issue)
|
|
assert.Error(t, err)
|
|
assert.True(t, issues_model.IsErrReviewNotExist(err))
|
|
assert.Nil(t, review2)
|
|
}
|
|
|
|
func TestCreateReview(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
|
|
|
review, err := issues_model.CreateReview(db.DefaultContext, issues_model.CreateReviewOptions{
|
|
Content: "New Review",
|
|
Type: issues_model.ReviewTypePending,
|
|
Issue: issue,
|
|
Reviewer: user,
|
|
})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "New Review", review.Content)
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.Review{Content: "New Review"})
|
|
}
|
|
|
|
func TestGetReviewersByIssueID(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 3})
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
|
|
user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
|
|
|
|
expectedReviews := []*issues_model.Review{}
|
|
expectedReviews = append(expectedReviews,
|
|
&issues_model.Review{
|
|
Reviewer: org3,
|
|
Type: issues_model.ReviewTypeReject,
|
|
UpdatedUnix: 946684812,
|
|
},
|
|
&issues_model.Review{
|
|
Reviewer: user4,
|
|
Type: issues_model.ReviewTypeApprove,
|
|
UpdatedUnix: 946684813,
|
|
},
|
|
&issues_model.Review{
|
|
Reviewer: user2,
|
|
Type: issues_model.ReviewTypeReject,
|
|
UpdatedUnix: 946684814,
|
|
})
|
|
|
|
allReviews, err := issues_model.GetReviewsByIssueID(issue.ID)
|
|
assert.NoError(t, err)
|
|
for _, review := range allReviews {
|
|
assert.NoError(t, review.LoadReviewer(db.DefaultContext))
|
|
}
|
|
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)
|
|
}
|
|
}
|
|
|
|
allReviews, err = issues_model.GetReviewsByIssueID(issue.ID)
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, allReviews.LoadReviewers(db.DefaultContext))
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDismissReview(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
rejectReviewExample := unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 9})
|
|
requestReviewExample := unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 11})
|
|
approveReviewExample := unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 8})
|
|
assert.False(t, rejectReviewExample.Dismissed)
|
|
assert.False(t, requestReviewExample.Dismissed)
|
|
assert.False(t, approveReviewExample.Dismissed)
|
|
|
|
assert.NoError(t, issues_model.DismissReview(db.DefaultContext, rejectReviewExample, true))
|
|
rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 9})
|
|
requestReviewExample = unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 11})
|
|
assert.True(t, rejectReviewExample.Dismissed)
|
|
assert.False(t, requestReviewExample.Dismissed)
|
|
|
|
assert.NoError(t, issues_model.DismissReview(db.DefaultContext, requestReviewExample, true))
|
|
rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 9})
|
|
requestReviewExample = unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 11})
|
|
assert.True(t, rejectReviewExample.Dismissed)
|
|
assert.False(t, requestReviewExample.Dismissed)
|
|
assert.False(t, approveReviewExample.Dismissed)
|
|
|
|
assert.NoError(t, issues_model.DismissReview(db.DefaultContext, requestReviewExample, true))
|
|
rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 9})
|
|
requestReviewExample = unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 11})
|
|
assert.True(t, rejectReviewExample.Dismissed)
|
|
assert.False(t, requestReviewExample.Dismissed)
|
|
assert.False(t, approveReviewExample.Dismissed)
|
|
|
|
assert.NoError(t, issues_model.DismissReview(db.DefaultContext, requestReviewExample, false))
|
|
rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 9})
|
|
requestReviewExample = unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 11})
|
|
assert.True(t, rejectReviewExample.Dismissed)
|
|
assert.False(t, requestReviewExample.Dismissed)
|
|
assert.False(t, approveReviewExample.Dismissed)
|
|
|
|
assert.NoError(t, issues_model.DismissReview(db.DefaultContext, requestReviewExample, false))
|
|
rejectReviewExample = unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 9})
|
|
requestReviewExample = unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: 11})
|
|
assert.True(t, rejectReviewExample.Dismissed)
|
|
assert.False(t, requestReviewExample.Dismissed)
|
|
assert.False(t, approveReviewExample.Dismissed)
|
|
|
|
assert.NoError(t, issues_model.DismissReview(db.DefaultContext, rejectReviewExample, false))
|
|
assert.False(t, rejectReviewExample.Dismissed)
|
|
assert.False(t, requestReviewExample.Dismissed)
|
|
assert.False(t, approveReviewExample.Dismissed)
|
|
|
|
assert.NoError(t, issues_model.DismissReview(db.DefaultContext, approveReviewExample, true))
|
|
assert.False(t, rejectReviewExample.Dismissed)
|
|
assert.False(t, requestReviewExample.Dismissed)
|
|
assert.True(t, approveReviewExample.Dismissed)
|
|
}
|
|
|
|
func TestDeleteReview(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
|
|
|
review1, err := issues_model.CreateReview(db.DefaultContext, issues_model.CreateReviewOptions{
|
|
Content: "Official rejection",
|
|
Type: issues_model.ReviewTypeReject,
|
|
Official: false,
|
|
Issue: issue,
|
|
Reviewer: user,
|
|
})
|
|
assert.NoError(t, err)
|
|
|
|
review2, err := issues_model.CreateReview(db.DefaultContext, issues_model.CreateReviewOptions{
|
|
Content: "Official approval",
|
|
Type: issues_model.ReviewTypeApprove,
|
|
Official: true,
|
|
Issue: issue,
|
|
Reviewer: user,
|
|
})
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, issues_model.DeleteReview(review2))
|
|
|
|
_, err = issues_model.GetReviewByID(db.DefaultContext, review2.ID)
|
|
assert.Error(t, err)
|
|
assert.True(t, issues_model.IsErrReviewNotExist(err), "IsErrReviewNotExist")
|
|
|
|
review1, err = issues_model.GetReviewByID(db.DefaultContext, review1.ID)
|
|
assert.NoError(t, err)
|
|
assert.True(t, review1.Official)
|
|
}
|