aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorLauris BH <lauris@nix.lv>2017-12-05 22:57:01 +0200
committerGitHub <noreply@github.com>2017-12-05 22:57:01 +0200
commitf1d8dda5b4031ccada00e43b64113f65601b906e (patch)
tree14642ec0ae715a7a97333124d014eabb326accb3 /models
parent3b89ee5501335e263a73fe61abebf812a00def35 (diff)
downloadgitea-f1d8dda5b4031ccada00e43b64113f65601b906e.tar.gz
gitea-f1d8dda5b4031ccada00e43b64113f65601b906e.zip
Tests for reactions (#3083)
* Unit tests for reactions * Fix import order Signed-off-by: Lauris Bukšis-Haberkorns <lauris@nix.lv>
Diffstat (limited to 'models')
-rw-r--r--models/issue_reaction.go4
-rw-r--r--models/issue_reaction_test.go166
2 files changed, 168 insertions, 2 deletions
diff --git a/models/issue_reaction.go b/models/issue_reaction.go
index 358e0701b3..3ecf536420 100644
--- a/models/issue_reaction.go
+++ b/models/issue_reaction.go
@@ -9,10 +9,10 @@ import (
"fmt"
"time"
+ "code.gitea.io/gitea/modules/setting"
+
"github.com/go-xorm/builder"
"github.com/go-xorm/xorm"
-
- "code.gitea.io/gitea/modules/setting"
)
// Reaction represents a reactions on issues and comments.
diff --git a/models/issue_reaction_test.go b/models/issue_reaction_test.go
new file mode 100644
index 0000000000..8363e534d5
--- /dev/null
+++ b/models/issue_reaction_test.go
@@ -0,0 +1,166 @@
+// 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
+
+import (
+ "testing"
+
+ "code.gitea.io/gitea/modules/setting"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func addReaction(t *testing.T, doer *User, issue *Issue, comment *Comment, content string) {
+ var reaction *Reaction
+ var err error
+ if comment == nil {
+ reaction, err = CreateIssueReaction(doer, issue, content)
+ } else {
+ reaction, err = CreateCommentReaction(doer, issue, comment, content)
+ }
+ assert.NoError(t, err)
+ assert.NotNil(t, reaction)
+}
+
+func TestIssueAddReaction(t *testing.T) {
+ assert.NoError(t, PrepareTestDatabase())
+
+ user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
+
+ issue1 := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
+
+ addReaction(t, user1, issue1, nil, "heart")
+
+ AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID})
+}
+
+func TestIssueAddDuplicateReaction(t *testing.T) {
+ assert.NoError(t, PrepareTestDatabase())
+
+ user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
+
+ issue1 := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
+
+ addReaction(t, user1, issue1, nil, "heart")
+
+ reaction, err := CreateReaction(&ReactionOptions{
+ Doer: user1,
+ Issue: issue1,
+ Type: "heart",
+ })
+ assert.Error(t, err)
+ assert.Nil(t, reaction)
+
+ AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID})
+}
+
+func TestIssueDeleteReaction(t *testing.T) {
+ assert.NoError(t, PrepareTestDatabase())
+
+ user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
+
+ issue1 := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
+
+ addReaction(t, user1, issue1, nil, "heart")
+
+ err := DeleteIssueReaction(user1, issue1, "heart")
+ assert.NoError(t, err)
+
+ AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID})
+}
+
+func TestIssueReactionCount(t *testing.T) {
+ assert.NoError(t, PrepareTestDatabase())
+
+ setting.UI.ReactionMaxUserNum = 2
+
+ user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
+ user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
+ user3 := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
+ user4 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
+ ghost := NewGhostUser()
+
+ issue1 := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
+
+ addReaction(t, user1, issue1, nil, "heart")
+ addReaction(t, user2, issue1, nil, "heart")
+ addReaction(t, user3, issue1, nil, "heart")
+ addReaction(t, user3, issue1, nil, "+1")
+ addReaction(t, user4, issue1, nil, "+1")
+ addReaction(t, user4, issue1, nil, "heart")
+ addReaction(t, ghost, issue1, nil, "-1")
+
+ err := issue1.loadReactions(x)
+ assert.NoError(t, err)
+
+ assert.Len(t, issue1.Reactions, 7)
+
+ reactions := issue1.Reactions.GroupByType()
+ assert.Len(t, reactions["heart"], 4)
+ assert.Equal(t, 2, reactions["heart"].GetMoreUserCount())
+ assert.Equal(t, "User One, User Two", reactions["heart"].GetFirstUsers())
+ assert.True(t, reactions["heart"].HasUser(1))
+ assert.False(t, reactions["heart"].HasUser(5))
+ assert.False(t, reactions["heart"].HasUser(0))
+ assert.Len(t, reactions["+1"], 2)
+ assert.Equal(t, 0, reactions["+1"].GetMoreUserCount())
+ assert.Len(t, reactions["-1"], 1)
+}
+
+func TestIssueCommentAddReaction(t *testing.T) {
+ assert.NoError(t, PrepareTestDatabase())
+
+ user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
+
+ issue1 := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
+
+ comment1 := AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment)
+
+ addReaction(t, user1, issue1, comment1, "heart")
+
+ AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID, CommentID: comment1.ID})
+}
+
+func TestIssueCommentDeleteReaction(t *testing.T) {
+ assert.NoError(t, PrepareTestDatabase())
+
+ user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
+ user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
+ user3 := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
+ user4 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
+ ghost := NewGhostUser()
+
+ issue1 := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
+
+ comment1 := AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment)
+
+ 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, ghost, issue1, comment1, "heart")
+
+ err := comment1.LoadReactions()
+ assert.NoError(t, err)
+ assert.Len(t, comment1.Reactions, 5)
+
+ reactions := comment1.Reactions.GroupByType()
+ assert.Len(t, reactions["heart"], 4)
+ assert.Len(t, reactions["+1"], 1)
+}
+
+func TestIssueCommentReactionCount(t *testing.T) {
+ assert.NoError(t, PrepareTestDatabase())
+
+ user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
+
+ issue1 := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
+
+ comment1 := AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment)
+
+ addReaction(t, user1, issue1, comment1, "heart")
+ DeleteCommentReaction(user1, issue1, comment1, "heart")
+
+ AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID, CommentID: comment1.ID})
+}