|
| 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 | + |
| 5 | +package models |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestCreateOrUpdateIssueNotifications(t *testing.T) { |
| 14 | + assert.NoError(t, PrepareTestDatabase()) |
| 15 | + issue := AssertExistsAndLoadBean(t, &Issue{ID: 1}).(*Issue) |
| 16 | + |
| 17 | + assert.NoError(t, CreateOrUpdateIssueNotifications(issue, 2)) |
| 18 | + |
| 19 | + notf := AssertExistsAndLoadBean(t, &Notification{UserID: 1, IssueID: issue.ID}).(*Notification) |
| 20 | + assert.Equal(t, NotificationStatusUnread, notf.Status) |
| 21 | + notf = AssertExistsAndLoadBean(t, &Notification{UserID: 4, IssueID: issue.ID}).(*Notification) |
| 22 | + assert.Equal(t, NotificationStatusUnread, notf.Status) |
| 23 | +} |
| 24 | + |
| 25 | +func TestNotificationsForUser(t *testing.T) { |
| 26 | + assert.NoError(t, PrepareTestDatabase()) |
| 27 | + user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) |
| 28 | + statuses := []NotificationStatus{NotificationStatusRead, NotificationStatusUnread} |
| 29 | + notfs, err := NotificationsForUser(user, statuses, 1, 10) |
| 30 | + assert.NoError(t, err) |
| 31 | + assert.Len(t, notfs, 1) |
| 32 | + assert.EqualValues(t, 2, notfs[0].ID) |
| 33 | + assert.EqualValues(t, user.ID, notfs[0].UserID) |
| 34 | +} |
| 35 | + |
| 36 | +func TestNotification_GetRepo(t *testing.T) { |
| 37 | + assert.NoError(t, PrepareTestDatabase()) |
| 38 | + notf := AssertExistsAndLoadBean(t, &Notification{RepoID: 1}).(*Notification) |
| 39 | + repo, err := notf.GetRepo() |
| 40 | + assert.NoError(t, err) |
| 41 | + assert.Equal(t, repo, notf.Repository) |
| 42 | + assert.EqualValues(t, notf.RepoID, repo.ID) |
| 43 | +} |
| 44 | + |
| 45 | +func TestNotification_GetIssue(t *testing.T) { |
| 46 | + assert.NoError(t, PrepareTestDatabase()) |
| 47 | + notf := AssertExistsAndLoadBean(t, &Notification{RepoID: 1}).(*Notification) |
| 48 | + issue, err := notf.GetIssue() |
| 49 | + assert.NoError(t, err) |
| 50 | + assert.Equal(t, issue, notf.Issue) |
| 51 | + assert.EqualValues(t, notf.IssueID, issue.ID) |
| 52 | +} |
| 53 | + |
| 54 | +func TestGetNotificationCount(t *testing.T) { |
| 55 | + assert.NoError(t, PrepareTestDatabase()) |
| 56 | + user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) |
| 57 | + cnt, err := GetNotificationCount(user, NotificationStatusUnread) |
| 58 | + assert.NoError(t, err) |
| 59 | + assert.EqualValues(t, 0, cnt) |
| 60 | + |
| 61 | + cnt, err = GetNotificationCount(user, NotificationStatusRead) |
| 62 | + assert.NoError(t, err) |
| 63 | + assert.EqualValues(t, 1, cnt) |
| 64 | +} |
| 65 | + |
| 66 | +func TestSetNotificationStatus(t *testing.T) { |
| 67 | + assert.NoError(t, PrepareTestDatabase()) |
| 68 | + user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) |
| 69 | + notf := AssertExistsAndLoadBean(t, |
| 70 | + &Notification{UserID: user.ID, Status: NotificationStatusRead}).(*Notification) |
| 71 | + assert.NoError(t, SetNotificationStatus(notf.ID, user, NotificationStatusPinned)) |
| 72 | + AssertExistsAndLoadBean(t, |
| 73 | + &Notification{ID: notf.ID, Status: NotificationStatusPinned}) |
| 74 | + |
| 75 | + assert.Error(t, SetNotificationStatus(1, user, NotificationStatusRead)) |
| 76 | + assert.Error(t, SetNotificationStatus(NonexistentID, user, NotificationStatusRead)) |
| 77 | +} |
0 commit comments