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.

78 lines
2.8 KiB

  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. notf := AssertExistsAndLoadBean(t, &Notification{UserID: 1, IssueID: issue.ID}).(*Notification)
  14. assert.Equal(t, NotificationStatusUnread, notf.Status)
  15. notf = AssertExistsAndLoadBean(t, &Notification{UserID: 4, IssueID: issue.ID}).(*Notification)
  16. assert.Equal(t, NotificationStatusUnread, notf.Status)
  17. CheckConsistencyFor(t, &Issue{ID: issue.ID})
  18. }
  19. func TestNotificationsForUser(t *testing.T) {
  20. assert.NoError(t, PrepareTestDatabase())
  21. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  22. statuses := []NotificationStatus{NotificationStatusRead, NotificationStatusUnread}
  23. notfs, err := NotificationsForUser(user, statuses, 1, 10)
  24. assert.NoError(t, err)
  25. assert.Len(t, notfs, 1)
  26. assert.EqualValues(t, 2, notfs[0].ID)
  27. assert.EqualValues(t, user.ID, notfs[0].UserID)
  28. }
  29. func TestNotification_GetRepo(t *testing.T) {
  30. assert.NoError(t, PrepareTestDatabase())
  31. notf := AssertExistsAndLoadBean(t, &Notification{RepoID: 1}).(*Notification)
  32. repo, err := notf.GetRepo()
  33. assert.NoError(t, err)
  34. assert.Equal(t, repo, notf.Repository)
  35. assert.EqualValues(t, notf.RepoID, repo.ID)
  36. }
  37. func TestNotification_GetIssue(t *testing.T) {
  38. assert.NoError(t, PrepareTestDatabase())
  39. notf := AssertExistsAndLoadBean(t, &Notification{RepoID: 1}).(*Notification)
  40. issue, err := notf.GetIssue()
  41. assert.NoError(t, err)
  42. assert.Equal(t, issue, notf.Issue)
  43. assert.EqualValues(t, notf.IssueID, issue.ID)
  44. }
  45. func TestGetNotificationCount(t *testing.T) {
  46. assert.NoError(t, PrepareTestDatabase())
  47. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  48. cnt, err := GetNotificationCount(user, NotificationStatusUnread)
  49. assert.NoError(t, err)
  50. assert.EqualValues(t, 0, cnt)
  51. cnt, err = GetNotificationCount(user, NotificationStatusRead)
  52. assert.NoError(t, err)
  53. assert.EqualValues(t, 1, cnt)
  54. }
  55. func TestSetNotificationStatus(t *testing.T) {
  56. assert.NoError(t, PrepareTestDatabase())
  57. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  58. notf := AssertExistsAndLoadBean(t,
  59. &Notification{UserID: user.ID, Status: NotificationStatusRead}).(*Notification)
  60. assert.NoError(t, SetNotificationStatus(notf.ID, user, NotificationStatusPinned))
  61. AssertExistsAndLoadBean(t,
  62. &Notification{ID: notf.ID, Status: NotificationStatusPinned})
  63. assert.Error(t, SetNotificationStatus(1, user, NotificationStatusRead))
  64. assert.Error(t, SetNotificationStatus(NonexistentID, user, NotificationStatusRead))
  65. }