@@ -14,6 +14,7 @@ import (
1414
1515type notificationTestEnv struct {
1616 svc service.NotificationService
17+ commentSvc * service.CommentService
1718 notificationRepository repositories.NotificationRepository
1819 userRepository repositories.UserRepository
1920 postRepository repositories.PostRepository
@@ -28,59 +29,22 @@ func setupNotificationService(t *testing.T) notificationTestEnv {
2829 postRepository := repositories .NewDBPostRepository (testDb .Queries )
2930 notificationRepository := repositories .NewDBNotificationRepository (testDb .Queries )
3031 userRepository := repositories .NewDBUserRepository (testDb .Queries )
32+ likeRepository := repositories .NewDBLikeRepository (testDb .Queries )
33+ bucketRepository := & fakeBucketRepository {}
3134
32- notificationService := service .NewNotificationService (notificationRepository , postRepository , commentRepository , userRepository , repositories .NewS3BucketRepository (nil ))
35+ notificationService := service .NewNotificationService (notificationRepository , postRepository , commentRepository , userRepository , bucketRepository )
36+ commentService := service .NewCommentService (commentRepository , postRepository , notificationRepository , userRepository , likeRepository , bucketRepository )
3337
3438 return notificationTestEnv {
3539 svc : * notificationService ,
40+ commentSvc : commentService ,
3641 notificationRepository : notificationRepository ,
3742 userRepository : userRepository ,
3843 postRepository : postRepository ,
3944 commentRepository : commentRepository ,
4045 }
4146}
4247
43- func TestGetNotificationsByUserId (t * testing.T ) {
44- env := setupNotificationService (t )
45- user := testutil .CreateTestUser (t , env .userRepository , "testUser" )
46-
47- notifications , err := env .svc .GetNotificationsByUserId (t .Context (), user , 0 , 10 )
48- require .NoError (t , err )
49- assert .NotNil (t , notifications )
50- assert .Len (t , notifications , 0 )
51-
52- require .NoError (t , env .notificationRepository .InsertNotification (t .Context (), user .UserID , nil , nil , nil , "Test notification 1" , models .NotificationTypeLike , nil ))
53- require .NoError (t , env .notificationRepository .InsertNotification (t .Context (), user .UserID , nil , nil , nil , "Test notification 2" , models .NotificationTypeLike , nil ))
54- require .NoError (t , env .notificationRepository .InsertNotification (t .Context (), user .UserID , nil , nil , nil , "Test notification 3" , models .NotificationTypeLike , nil ))
55-
56- notifications , err = env .svc .GetNotificationsByUserId (t .Context (), user , 0 , 10 )
57- require .NoError (t , err )
58- assert .NotNil (t , notifications )
59- assert .Len (t , notifications , 3 )
60-
61- notifications , err = env .svc .GetNotificationsByUserId (t .Context (), user , 0 , 2 )
62- require .NoError (t , err )
63- assert .NotNil (t , notifications )
64- assert .Len (t , notifications , 2 )
65-
66- notifications , err = env .svc .GetNotificationsByUserId (t .Context (), user , 2 , 2 )
67- require .NoError (t , err )
68- assert .NotNil (t , notifications )
69- assert .Len (t , notifications , 1 )
70-
71- notifications , err = env .svc .GetNotificationsByUserId (t .Context (), user , 5 , 2 )
72- require .NoError (t , err )
73- assert .NotNil (t , notifications )
74- assert .Len (t , notifications , 0 )
75-
76- anotherUser := testutil .CreateTestUser (t , env .userRepository , "anotherUser" )
77-
78- notifications , err = env .svc .GetNotificationsByUserId (t .Context (), anotherUser , 0 , 10 )
79- require .NoError (t , err )
80- assert .NotNil (t , notifications )
81- assert .Len (t , notifications , 0 )
82- }
83-
8448func TestMarkNotificationAsReadById (t * testing.T ) {
8549 env := setupNotificationService (t )
8650 user := testutil .CreateTestUser (t , env .userRepository , "testUser" )
@@ -99,10 +63,10 @@ func TestMarkNotificationAsReadById(t *testing.T) {
9963 err = env .svc .MarkNotificationAsReadById (t .Context (), user , notificationId )
10064 require .NoError (t , err )
10165
102- notifications , err := env .svc .GetNotificationsByUserId (t .Context (), user , 0 , 10 )
66+ notifications , err := env .svc .GetReadNotificationsByUserIdWithTimeOffset (t .Context (), user , time . Now (). UTC () , 10 , nil )
10367 require .NoError (t , err )
10468 assert .NotNil (t , notifications )
105- assert .Len (t , notifications , 1 )
69+ require .Len (t , notifications , 1 )
10670 assert .True (t , notifications [0 ].Viewed )
10771}
10872
@@ -121,7 +85,7 @@ func TestMarkAllNotificationsAsReadForUserId(t *testing.T) {
12185 err = env .svc .MarkAllNotificationsAsReadForUserId (t .Context (), user )
12286 require .NoError (t , err )
12387
124- notifications , err := env .svc .GetNotificationsByUserId (t .Context (), user , 0 , 10 )
88+ notifications , err := env .svc .GetReadNotificationsByUserIdWithTimeOffset (t .Context (), user , time . Now (). UTC () , 10 , nil )
12589 require .NoError (t , err )
12690 assert .NotNil (t , notifications )
12791 assert .Len (t , notifications , 3 )
@@ -165,36 +129,6 @@ func TestUserHasUnreadNotifications(t *testing.T) {
165129 assert .False (t , hasUnread )
166130}
167131
168- func TestMultipleUsersNotifications (t * testing.T ) {
169- env := setupNotificationService (t )
170-
171- user1 := testutil .CreateTestUser (t , env .userRepository , "user1" )
172- user2 := testutil .CreateTestUser (t , env .userRepository , "user2" )
173-
174- require .NoError (t , env .notificationRepository .InsertNotification (t .Context (), user1 .UserID , nil , nil , nil , "User 1 notification 1" , models .NotificationTypeLike , nil ))
175- require .NoError (t , env .notificationRepository .InsertNotification (t .Context (), user1 .UserID , nil , nil , nil , "User 1 notification 2" , models .NotificationTypeLike , nil ))
176- require .NoError (t , env .notificationRepository .InsertNotification (t .Context (), user2 .UserID , nil , nil , nil , "User 2 notification 1" , models .NotificationTypeLike , nil ))
177-
178- notificationsUser1 , err := env .svc .GetNotificationsByUserId (t .Context (), user1 , 0 , 10 )
179- require .NoError (t , err )
180- assert .Len (t , notificationsUser1 , 2 )
181-
182- notificationsUser2 , err := env .svc .GetNotificationsByUserId (t .Context (), user2 , 0 , 10 )
183- require .NoError (t , err )
184- assert .Len (t , notificationsUser2 , 1 )
185-
186- err = env .svc .MarkAllNotificationsAsReadForUserId (t .Context (), user1 )
187- require .NoError (t , err )
188-
189- hasUnreadUser1 , err := env .svc .UserHasUnreadNotifications (t .Context (), user1 )
190- require .NoError (t , err )
191- assert .False (t , hasUnreadUser1 )
192-
193- hasUnreadUser2 , err := env .svc .UserHasUnreadNotifications (t .Context (), user2 )
194- require .NoError (t , err )
195- assert .True (t , hasUnreadUser2 )
196- }
197-
198132func TestErrorWhenMarkingNonExistentNotification (t * testing.T ) {
199133 env := setupNotificationService (t )
200134 user := testutil .CreateTestUser (t , env .userRepository , "testUser" )
@@ -295,3 +229,43 @@ func TestGetComments_DoesNotFailLinkingToDeletedUsers(t *testing.T) {
295229 assert .NoError (t , err )
296230 assert .Len (t , notifications , 1 )
297231}
232+
233+ func TestCommentNotification_ImagePopulatedFromComment (t * testing.T ) {
234+ env := setupNotificationService (t )
235+
236+ postOwner := testutil .CreateTestUser (t , env .userRepository , "postOwner" )
237+ commenter := testutil .CreateTestUser (t , env .userRepository , "commenter" )
238+
239+ visibility := models .VisibilityPublic
240+ post , err := env .postRepository .InsertPost (t .Context (), postOwner .UserID , "test post" , nil , nil , & visibility )
241+ require .NoError (t , err )
242+
243+ imageKey := "images/test-image.jpg"
244+ imageKeyMap := map [int ]models.ImageData {
245+ 0 : {S3Key : imageKey , Width : 640 , Height : 480 },
246+ }
247+ _ , err = env .commentSvc .AddCommentToPost (t .Context (), commenter , post .PostID , "test comment with image" , imageKeyMap )
248+ require .NoError (t , err )
249+
250+ notifications , err := env .svc .GetUnreadNotificationsByUserIdWithTimeOffset (t .Context (), postOwner , time .Now ().UTC (), 10 , nil )
251+ require .NoError (t , err )
252+ require .Len (t , notifications , 1 )
253+ require .NotNil (t , notifications [0 ].ImageBlob , "imageBlob should be set from the comment image" )
254+ assert .Equal (t , imageKey , * notifications [0 ].ImageBlob )
255+ assert .Equal (t , 640 , * notifications [0 ].ImageWidth )
256+ assert .Equal (t , 480 , * notifications [0 ].ImageHeight )
257+
258+ unread , err := env .svc .GetUnreadNotificationsByUserIdWithTimeOffset (t .Context (), postOwner , time .Now ().UTC (), 10 , nil )
259+ require .NoError (t , err )
260+ require .Len (t , unread , 1 )
261+ require .NotNil (t , unread [0 ].ImageBlob )
262+ assert .Equal (t , imageKey , * unread [0 ].ImageBlob )
263+
264+ require .NoError (t , env .svc .MarkAllNotificationsAsReadForUserId (t .Context (), postOwner ))
265+
266+ read , err := env .svc .GetReadNotificationsByUserIdWithTimeOffset (t .Context (), postOwner , time .Now ().UTC (), 10 , nil )
267+ require .NoError (t , err )
268+ require .Len (t , read , 1 )
269+ require .NotNil (t , read [0 ].ImageBlob )
270+ assert .Equal (t , imageKey , * read [0 ].ImageBlob )
271+ }
0 commit comments