本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
5.1KB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "testing"
  7. "code.gitea.io/gitea/modules/setting"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func addReaction(t *testing.T, doer *User, issue *Issue, comment *Comment, content string) {
  11. var reaction *Reaction
  12. var err error
  13. if comment == nil {
  14. reaction, err = CreateIssueReaction(doer, issue, content)
  15. } else {
  16. reaction, err = CreateCommentReaction(doer, issue, comment, content)
  17. }
  18. assert.NoError(t, err)
  19. assert.NotNil(t, reaction)
  20. }
  21. func TestIssueAddReaction(t *testing.T) {
  22. assert.NoError(t, PrepareTestDatabase())
  23. user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  24. issue1 := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
  25. addReaction(t, user1, issue1, nil, "heart")
  26. AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID})
  27. }
  28. func TestIssueAddDuplicateReaction(t *testing.T) {
  29. assert.NoError(t, PrepareTestDatabase())
  30. user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  31. issue1 := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
  32. addReaction(t, user1, issue1, nil, "heart")
  33. reaction, err := CreateReaction(&ReactionOptions{
  34. Doer: user1,
  35. Issue: issue1,
  36. Type: "heart",
  37. })
  38. assert.Error(t, err)
  39. assert.Nil(t, reaction)
  40. AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID})
  41. }
  42. func TestIssueDeleteReaction(t *testing.T) {
  43. assert.NoError(t, PrepareTestDatabase())
  44. user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  45. issue1 := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
  46. addReaction(t, user1, issue1, nil, "heart")
  47. err := DeleteIssueReaction(user1, issue1, "heart")
  48. assert.NoError(t, err)
  49. AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID})
  50. }
  51. func TestIssueReactionCount(t *testing.T) {
  52. assert.NoError(t, PrepareTestDatabase())
  53. setting.UI.ReactionMaxUserNum = 2
  54. user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  55. user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  56. user3 := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  57. user4 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
  58. ghost := NewGhostUser()
  59. issue1 := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
  60. addReaction(t, user1, issue1, nil, "heart")
  61. addReaction(t, user2, issue1, nil, "heart")
  62. addReaction(t, user3, issue1, nil, "heart")
  63. addReaction(t, user3, issue1, nil, "+1")
  64. addReaction(t, user4, issue1, nil, "+1")
  65. addReaction(t, user4, issue1, nil, "heart")
  66. addReaction(t, ghost, issue1, nil, "-1")
  67. err := issue1.loadReactions(x)
  68. assert.NoError(t, err)
  69. assert.Len(t, issue1.Reactions, 7)
  70. reactions := issue1.Reactions.GroupByType()
  71. assert.Len(t, reactions["heart"], 4)
  72. assert.Equal(t, 2, reactions["heart"].GetMoreUserCount())
  73. assert.Equal(t, user1.DisplayName()+", "+user2.DisplayName(), reactions["heart"].GetFirstUsers())
  74. assert.True(t, reactions["heart"].HasUser(1))
  75. assert.False(t, reactions["heart"].HasUser(5))
  76. assert.False(t, reactions["heart"].HasUser(0))
  77. assert.Len(t, reactions["+1"], 2)
  78. assert.Equal(t, 0, reactions["+1"].GetMoreUserCount())
  79. assert.Len(t, reactions["-1"], 1)
  80. }
  81. func TestIssueCommentAddReaction(t *testing.T) {
  82. assert.NoError(t, PrepareTestDatabase())
  83. user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  84. issue1 := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
  85. comment1 := AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment)
  86. addReaction(t, user1, issue1, comment1, "heart")
  87. AssertExistsAndLoadBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID, CommentID: comment1.ID})
  88. }
  89. func TestIssueCommentDeleteReaction(t *testing.T) {
  90. assert.NoError(t, PrepareTestDatabase())
  91. user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  92. user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  93. user3 := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  94. user4 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
  95. ghost := NewGhostUser()
  96. issue1 := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
  97. comment1 := AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment)
  98. addReaction(t, user1, issue1, comment1, "heart")
  99. addReaction(t, user2, issue1, comment1, "heart")
  100. addReaction(t, user3, issue1, comment1, "heart")
  101. addReaction(t, user4, issue1, comment1, "+1")
  102. addReaction(t, ghost, issue1, comment1, "heart")
  103. err := comment1.LoadReactions()
  104. assert.NoError(t, err)
  105. assert.Len(t, comment1.Reactions, 5)
  106. reactions := comment1.Reactions.GroupByType()
  107. assert.Len(t, reactions["heart"], 4)
  108. assert.Len(t, reactions["+1"], 1)
  109. }
  110. func TestIssueCommentReactionCount(t *testing.T) {
  111. assert.NoError(t, PrepareTestDatabase())
  112. user1 := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  113. issue1 := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
  114. comment1 := AssertExistsAndLoadBean(t, &Comment{ID: 1}).(*Comment)
  115. addReaction(t, user1, issue1, comment1, "heart")
  116. DeleteCommentReaction(user1, issue1, comment1, "heart")
  117. AssertNotExistsBean(t, &Reaction{Type: "heart", UserID: user1.ID, IssueID: issue1.ID, CommentID: comment1.ID})
  118. }
上海开阖软件有限公司 沪ICP备12045867号-1