1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-26 09:09:36 -05:00
forgejo/models/issue_reaction_test.go
zeripath 9302eba971
DBContext is just a Context (#17100)
* DBContext is just a Context

This PR removes some of the specialness from the DBContext and makes it context
This allows us to simplify the GetEngine code to wrap around any context in future
and means that we can change our loadRepo(e Engine) functions to simply take contexts.

Signed-off-by: Andrew Thornton <art27@cantab.net>

* fix unit tests

Signed-off-by: Andrew Thornton <art27@cantab.net>

* another place that needs to set the initial context

Signed-off-by: Andrew Thornton <art27@cantab.net>

* avoid race

Signed-off-by: Andrew Thornton <art27@cantab.net>

* change attachment error

Signed-off-by: Andrew Thornton <art27@cantab.net>
2021-09-23 23:45:36 +08:00

167 lines
5.4 KiB
Go

// 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/models/db"
"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, db.PrepareTestDatabase())
user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
issue1 := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
addReaction(t, user1, issue1, nil, "heart")
db.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID})
}
func TestIssueAddDuplicateReaction(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
issue1 := db.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.Equal(t, ErrReactionAlreadyExist{Reaction: "heart"}, err)
existingR := db.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID}).(*Reaction)
assert.Equal(t, existingR.ID, reaction.ID)
}
func TestIssueDeleteReaction(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
issue1 := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
addReaction(t, user1, issue1, nil, "heart")
err := DeleteIssueReaction(user1, issue1, "heart")
assert.NoError(t, err)
db.AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID})
}
func TestIssueReactionCount(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
setting.UI.ReactionMaxUserNum = 2
user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
user3 := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
user4 := db.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
ghost := NewGhostUser()
issue := db.AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue)
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")
err := issue.loadReactions(db.GetEngine(db.DefaultContext))
assert.NoError(t, err)
assert.Len(t, issue.Reactions, 7)
reactions := issue.Reactions.GroupByType()
assert.Len(t, reactions["heart"], 4)
assert.Equal(t, 2, reactions["heart"].GetMoreUserCount())
assert.Equal(t, user1.DisplayName()+", "+user2.DisplayName(), 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, db.PrepareTestDatabase())
user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
issue1 := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
comment1 := db.AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment)
addReaction(t, user1, issue1, comment1, "heart")
db.AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID, CommentID: comment1.ID})
}
func TestIssueCommentDeleteReaction(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
user3 := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
user4 := db.AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
issue1 := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: issue1.RepoID}).(*Repository)
comment1 := db.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")
err := comment1.LoadReactions(repo1)
assert.NoError(t, err)
assert.Len(t, comment1.Reactions, 4)
reactions := comment1.Reactions.GroupByType()
assert.Len(t, reactions["heart"], 3)
assert.Len(t, reactions["+1"], 1)
}
func TestIssueCommentReactionCount(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
issue1 := db.AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
comment1 := db.AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment)
addReaction(t, user1, issue1, comment1, "heart")
assert.NoError(t, DeleteCommentReaction(user1, issue1, comment1, "heart"))
db.AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID, CommentID: comment1.ID})
}