|
|
|
@ -1,7 +1,8 @@ |
|
|
|
|
// Copyright 2017 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.
|
|
|
|
|
package models |
|
|
|
|
|
|
|
|
|
package issues |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"testing" |
|
|
|
@ -15,13 +16,13 @@ import ( |
|
|
|
|
"github.com/stretchr/testify/assert" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func addReaction(t *testing.T, doer *user_model.User, issue *Issue, comment *Comment, content string) { |
|
|
|
|
func addReaction(t *testing.T, doerID, issueID, commentID int64, content string) { |
|
|
|
|
var reaction *Reaction |
|
|
|
|
var err error |
|
|
|
|
if comment == nil { |
|
|
|
|
reaction, err = CreateIssueReaction(doer, issue, content) |
|
|
|
|
if commentID == 0 { |
|
|
|
|
reaction, err = CreateIssueReaction(doerID, issueID, content) |
|
|
|
|
} else { |
|
|
|
|
reaction, err = CreateCommentReaction(doer, issue, comment, content) |
|
|
|
|
reaction, err = CreateCommentReaction(doerID, issueID, commentID, content) |
|
|
|
|
} |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.NotNil(t, reaction) |
|
|
|
@ -32,11 +33,11 @@ func TestIssueAddReaction(t *testing.T) { |
|
|
|
|
|
|
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User) |
|
|
|
|
|
|
|
|
|
issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) |
|
|
|
|
var issue1ID int64 = 1 |
|
|
|
|
|
|
|
|
|
addReaction(t, user1, issue1, nil, "heart") |
|
|
|
|
addReaction(t, user1.ID, issue1ID, 0, "heart") |
|
|
|
|
|
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID}) |
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1ID}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestIssueAddDuplicateReaction(t *testing.T) { |
|
|
|
@ -44,19 +45,19 @@ func TestIssueAddDuplicateReaction(t *testing.T) { |
|
|
|
|
|
|
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User) |
|
|
|
|
|
|
|
|
|
issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) |
|
|
|
|
var issue1ID int64 = 1 |
|
|
|
|
|
|
|
|
|
addReaction(t, user1, issue1, nil, "heart") |
|
|
|
|
addReaction(t, user1.ID, issue1ID, 0, "heart") |
|
|
|
|
|
|
|
|
|
reaction, err := CreateReaction(&ReactionOptions{ |
|
|
|
|
Doer: user1, |
|
|
|
|
Issue: issue1, |
|
|
|
|
DoerID: user1.ID, |
|
|
|
|
IssueID: issue1ID, |
|
|
|
|
Type: "heart", |
|
|
|
|
}) |
|
|
|
|
assert.Error(t, err) |
|
|
|
|
assert.Equal(t, ErrReactionAlreadyExist{Reaction: "heart"}, err) |
|
|
|
|
|
|
|
|
|
existingR := unittest.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID}).(*Reaction) |
|
|
|
|
existingR := unittest.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1ID}).(*Reaction) |
|
|
|
|
assert.Equal(t, existingR.ID, reaction.ID) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -65,14 +66,14 @@ func TestIssueDeleteReaction(t *testing.T) { |
|
|
|
|
|
|
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User) |
|
|
|
|
|
|
|
|
|
issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) |
|
|
|
|
var issue1ID int64 = 1 |
|
|
|
|
|
|
|
|
|
addReaction(t, user1, issue1, nil, "heart") |
|
|
|
|
addReaction(t, user1.ID, issue1ID, 0, "heart") |
|
|
|
|
|
|
|
|
|
err := DeleteIssueReaction(user1, issue1, "heart") |
|
|
|
|
err := DeleteIssueReaction(user1.ID, issue1ID, "heart") |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
|
|
|
|
|
unittest.AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID}) |
|
|
|
|
unittest.AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1ID}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestIssueReactionCount(t *testing.T) { |
|
|
|
@ -86,22 +87,26 @@ func TestIssueReactionCount(t *testing.T) { |
|
|
|
|
user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}).(*user_model.User) |
|
|
|
|
ghost := user_model.NewGhostUser() |
|
|
|
|
|
|
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue) |
|
|
|
|
var issueID int64 = 2 |
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) |
|
|
|
|
|
|
|
|
|
addReaction(t, user1, issue, nil, "heart") |
|
|
|
|
addReaction(t, user2, issue, nil, "heart") |
|
|
|
|
addReaction(t, user3, issue, nil, "heart") |
|
|
|
|
addReaction(t, user3, issue, nil, "+1") |
|
|
|
|
addReaction(t, user4, issue, nil, "+1") |
|
|
|
|
addReaction(t, user4, issue, nil, "heart") |
|
|
|
|
addReaction(t, ghost, issue, nil, "-1") |
|
|
|
|
addReaction(t, user1.ID, issueID, 0, "heart") |
|
|
|
|
addReaction(t, user2.ID, issueID, 0, "heart") |
|
|
|
|
addReaction(t, user3.ID, issueID, 0, "heart") |
|
|
|
|
addReaction(t, user3.ID, issueID, 0, "+1") |
|
|
|
|
addReaction(t, user4.ID, issueID, 0, "+1") |
|
|
|
|
addReaction(t, user4.ID, issueID, 0, "heart") |
|
|
|
|
addReaction(t, ghost.ID, issueID, 0, "-1") |
|
|
|
|
|
|
|
|
|
err := issue.loadReactions(db.DefaultContext) |
|
|
|
|
reactionsList, _, err := FindReactions(db.DefaultContext, FindReactionsOptions{ |
|
|
|
|
IssueID: issueID, |
|
|
|
|
}) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.Len(t, reactionsList, 7) |
|
|
|
|
_, err = reactionsList.LoadUsers(db.DefaultContext, repo) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
|
|
|
|
|
assert.Len(t, issue.Reactions, 7) |
|
|
|
|
|
|
|
|
|
reactions := issue.Reactions.GroupByType() |
|
|
|
|
reactions := reactionsList.GroupByType() |
|
|
|
|
assert.Len(t, reactions["heart"], 4) |
|
|
|
|
assert.Equal(t, 2, reactions["heart"].GetMoreUserCount()) |
|
|
|
|
assert.Equal(t, user1.DisplayName()+", "+user2.DisplayName(), reactions["heart"].GetFirstUsers()) |
|
|
|
@ -118,13 +123,12 @@ func TestIssueCommentAddReaction(t *testing.T) { |
|
|
|
|
|
|
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User) |
|
|
|
|
|
|
|
|
|
issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) |
|
|
|
|
var issue1ID int64 = 1 |
|
|
|
|
var comment1ID int64 = 1 |
|
|
|
|
|
|
|
|
|
comment1 := unittest.AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment) |
|
|
|
|
addReaction(t, user1.ID, issue1ID, comment1ID, "heart") |
|
|
|
|
|
|
|
|
|
addReaction(t, user1, issue1, comment1, "heart") |
|
|
|
|
|
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID, CommentID: comment1.ID}) |
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1ID, CommentID: comment1ID}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestIssueCommentDeleteReaction(t *testing.T) { |
|
|
|
@ -135,21 +139,22 @@ func TestIssueCommentDeleteReaction(t *testing.T) { |
|
|
|
|
user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}).(*user_model.User) |
|
|
|
|
user4 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4}).(*user_model.User) |
|
|
|
|
|
|
|
|
|
issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) |
|
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue1.RepoID}).(*repo_model.Repository) |
|
|
|
|
|
|
|
|
|
comment1 := unittest.AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment) |
|
|
|
|
var issue1ID int64 = 1 |
|
|
|
|
var comment1ID int64 = 1 |
|
|
|
|
|
|
|
|
|
addReaction(t, user1, issue1, comment1, "heart") |
|
|
|
|
addReaction(t, user2, issue1, comment1, "heart") |
|
|
|
|
addReaction(t, user3, issue1, comment1, "heart") |
|
|
|
|
addReaction(t, user4, issue1, comment1, "+1") |
|
|
|
|
addReaction(t, user1.ID, issue1ID, comment1ID, "heart") |
|
|
|
|
addReaction(t, user2.ID, issue1ID, comment1ID, "heart") |
|
|
|
|
addReaction(t, user3.ID, issue1ID, comment1ID, "heart") |
|
|
|
|
addReaction(t, user4.ID, issue1ID, comment1ID, "+1") |
|
|
|
|
|
|
|
|
|
err := comment1.LoadReactions(repo1) |
|
|
|
|
reactionsList, _, err := FindReactions(db.DefaultContext, FindReactionsOptions{ |
|
|
|
|
IssueID: issue1ID, |
|
|
|
|
CommentID: comment1ID, |
|
|
|
|
}) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
assert.Len(t, comment1.Reactions, 4) |
|
|
|
|
assert.Len(t, reactionsList, 4) |
|
|
|
|
|
|
|
|
|
reactions := comment1.Reactions.GroupByType() |
|
|
|
|
reactions := reactionsList.GroupByType() |
|
|
|
|
assert.Len(t, reactions["heart"], 3) |
|
|
|
|
assert.Len(t, reactions["+1"], 1) |
|
|
|
|
} |
|
|
|
@ -159,12 +164,11 @@ func TestIssueCommentReactionCount(t *testing.T) { |
|
|
|
|
|
|
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User) |
|
|
|
|
|
|
|
|
|
issue1 := unittest.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) |
|
|
|
|
|
|
|
|
|
comment1 := unittest.AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment) |
|
|
|
|
var issue1ID int64 = 1 |
|
|
|
|
var comment1ID int64 = 1 |
|
|
|
|
|
|
|
|
|
addReaction(t, user1, issue1, comment1, "heart") |
|
|
|
|
assert.NoError(t, DeleteCommentReaction(user1, issue1, comment1, "heart")) |
|
|
|
|
addReaction(t, user1.ID, issue1ID, comment1ID, "heart") |
|
|
|
|
assert.NoError(t, DeleteCommentReaction(user1.ID, issue1ID, comment1ID, "heart")) |
|
|
|
|
|
|
|
|
|
unittest.AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID, CommentID: comment1.ID}) |
|
|
|
|
unittest.AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1ID, CommentID: comment1ID}) |
|
|
|
|
} |