Skip to content

Commit 2eb15f4

Browse files
ethantkoeniglunny
authored andcommitted
Unit tests and remove unused functions in models/notification (#796)
* Unit tests and remove unused functions in models/notification * Read -> Unread
1 parent 77ab60d commit 2eb15f4

File tree

5 files changed

+108
-11
lines changed

5 files changed

+108
-11
lines changed

models/fixtures/notification.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-
2+
id: 1
3+
user_id: 1
4+
repo_id: 1
5+
status: 1 # unread
6+
source: 1 # issue
7+
updated_by: 2
8+
issue_id: 1
9+
created_unix: 946684800
10+
updated_unix: 946684800
11+
12+
-
13+
id: 2
14+
user_id: 2
15+
repo_id: 1
16+
status: 2 # read
17+
source: 1 # issue
18+
updated_by: 1
19+
issue_id: 2
20+
created_unix: 946684800
21+
updated_unix: 946684800

models/fixtures/watch.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-
2+
id: 1
3+
user_id: 1
4+
repo_id: 1
5+
6+
-
7+
id: 2
8+
user_id: 4
9+
repo_id: 1

models/notification.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,6 @@ func (n *Notification) GetIssue() (*Issue, error) {
227227
return n.Issue, err
228228
}
229229

230-
// GetNotificationReadCount returns the notification read count for user
231-
func GetNotificationReadCount(user *User) (int64, error) {
232-
return GetNotificationCount(user, NotificationStatusRead)
233-
}
234-
235-
// GetNotificationUnreadCount returns the notification unread count for user
236-
func GetNotificationUnreadCount(user *User) (int64, error) {
237-
return GetNotificationCount(user, NotificationStatusUnread)
238-
}
239-
240230
// GetNotificationCount returns the notification count for user
241231
func GetNotificationCount(user *User, status NotificationStatus) (int64, error) {
242232
return getNotificationCount(x, user, status)

models/notification_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
}

routers/user/notification.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func GetNotificationCount(c *context.Context) {
2828
return
2929
}
3030

31-
count, err := models.GetNotificationUnreadCount(c.User)
31+
count, err := models.GetNotificationCount(c.User, models.NotificationStatusUnread)
3232
if err != nil {
3333
c.Handle(500, "GetNotificationCount", err)
3434
return

0 commit comments

Comments
 (0)