本站源代码
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.

102 lines
3.9KB

  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. "github.com/stretchr/testify/assert"
  8. )
  9. func TestCreateOrUpdateIssueNotifications(t *testing.T) {
  10. assert.NoError(t, PrepareTestDatabase())
  11. issue := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue)
  12. assert.NoError(t, CreateOrUpdateIssueNotifications(issue, 2))
  13. // User 9 is inactive, thus notifications for user 1 and 4 are created
  14. notf := AssertExistsAndLoadBean(t, &Notification{UserID: 1, IssueID: issue.ID}).(*Notification)
  15. assert.Equal(t, NotificationStatusUnread, notf.Status)
  16. CheckConsistencyFor(t, &Issue{ID: issue.ID})
  17. notf = AssertExistsAndLoadBean(t, &Notification{UserID: 4, IssueID: issue.ID}).(*Notification)
  18. assert.Equal(t, NotificationStatusUnread, notf.Status)
  19. }
  20. func TestNotificationsForUser(t *testing.T) {
  21. assert.NoError(t, PrepareTestDatabase())
  22. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  23. statuses := []NotificationStatus{NotificationStatusRead, NotificationStatusUnread}
  24. notfs, err := NotificationsForUser(user, statuses, 1, 10)
  25. assert.NoError(t, err)
  26. if assert.Len(t, notfs, 2) {
  27. assert.EqualValues(t, 2, notfs[0].ID)
  28. assert.EqualValues(t, user.ID, notfs[0].UserID)
  29. assert.EqualValues(t, 4, notfs[1].ID)
  30. assert.EqualValues(t, user.ID, notfs[1].UserID)
  31. }
  32. }
  33. func TestNotification_GetRepo(t *testing.T) {
  34. assert.NoError(t, PrepareTestDatabase())
  35. notf := AssertExistsAndLoadBean(t, &Notification{RepoID: 1}).(*Notification)
  36. repo, err := notf.GetRepo()
  37. assert.NoError(t, err)
  38. assert.Equal(t, repo, notf.Repository)
  39. assert.EqualValues(t, notf.RepoID, repo.ID)
  40. }
  41. func TestNotification_GetIssue(t *testing.T) {
  42. assert.NoError(t, PrepareTestDatabase())
  43. notf := AssertExistsAndLoadBean(t, &Notification{RepoID: 1}).(*Notification)
  44. issue, err := notf.GetIssue()
  45. assert.NoError(t, err)
  46. assert.Equal(t, issue, notf.Issue)
  47. assert.EqualValues(t, notf.IssueID, issue.ID)
  48. }
  49. func TestGetNotificationCount(t *testing.T) {
  50. assert.NoError(t, PrepareTestDatabase())
  51. user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  52. cnt, err := GetNotificationCount(user, NotificationStatusRead)
  53. assert.NoError(t, err)
  54. assert.EqualValues(t, 0, cnt)
  55. cnt, err = GetNotificationCount(user, NotificationStatusUnread)
  56. assert.NoError(t, err)
  57. assert.EqualValues(t, 1, cnt)
  58. }
  59. func TestSetNotificationStatus(t *testing.T) {
  60. assert.NoError(t, PrepareTestDatabase())
  61. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  62. notf := AssertExistsAndLoadBean(t,
  63. &Notification{UserID: user.ID, Status: NotificationStatusRead}).(*Notification)
  64. assert.NoError(t, SetNotificationStatus(notf.ID, user, NotificationStatusPinned))
  65. AssertExistsAndLoadBean(t,
  66. &Notification{ID: notf.ID, Status: NotificationStatusPinned})
  67. assert.Error(t, SetNotificationStatus(1, user, NotificationStatusRead))
  68. assert.Error(t, SetNotificationStatus(NonexistentID, user, NotificationStatusRead))
  69. }
  70. func TestUpdateNotificationStatuses(t *testing.T) {
  71. assert.NoError(t, PrepareTestDatabase())
  72. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  73. notfUnread := AssertExistsAndLoadBean(t,
  74. &Notification{UserID: user.ID, Status: NotificationStatusUnread}).(*Notification)
  75. notfRead := AssertExistsAndLoadBean(t,
  76. &Notification{UserID: user.ID, Status: NotificationStatusRead}).(*Notification)
  77. notfPinned := AssertExistsAndLoadBean(t,
  78. &Notification{UserID: user.ID, Status: NotificationStatusPinned}).(*Notification)
  79. assert.NoError(t, UpdateNotificationStatuses(user, NotificationStatusUnread, NotificationStatusRead))
  80. AssertExistsAndLoadBean(t,
  81. &Notification{ID: notfUnread.ID, Status: NotificationStatusRead})
  82. AssertExistsAndLoadBean(t,
  83. &Notification{ID: notfRead.ID, Status: NotificationStatusRead})
  84. AssertExistsAndLoadBean(t,
  85. &Notification{ID: notfPinned.ID, Status: NotificationStatusPinned})
  86. }
上海开阖软件有限公司 沪ICP备12045867号-1