@@ -1065,3 +1065,82 @@ func TestMarkNotificationsAsRead(t *testing.T) {
10651065 })
10661066 }
10671067}
1068+
1069+ func TestGetNotificationUnreadCount (t * testing.T ) {
1070+ t .Parallel ()
1071+ mockController := gomock .NewController (t )
1072+ defer mockController .Finish ()
1073+
1074+ newBoundRow := func (createdAt int64 ) * mock.MockRow {
1075+ row := mock .NewMockRow (mockController )
1076+ row .EXPECT ().Scan (gomock .Any ()).DoAndReturn (func (args ... interface {}) error {
1077+ * args [0 ].(* int64 ) = createdAt
1078+ return nil
1079+ })
1080+ return row
1081+ }
1082+
1083+ patterns := []struct {
1084+ desc string
1085+ setup func (* notificationStorage )
1086+ expected int64
1087+ expectedErr error
1088+ }{
1089+ {
1090+ desc : "Error: account bound query" ,
1091+ setup : func (s * notificationStorage ) {
1092+ row := mock .NewMockRow (mockController )
1093+ row .EXPECT ().Scan (gomock .Any ()).Return (errors .New ("error" ))
1094+ s .qe .(* mock.MockQueryExecer ).EXPECT ().QueryRowContext (
1095+ gomock .Any (), selectEarliestAccountCreatedAtSQL , "viewer@example.com" ,
1096+ ).Return (row )
1097+ },
1098+ expected : 0 ,
1099+ expectedErr : errors .New ("error" ),
1100+ },
1101+ {
1102+ desc : "Error: count query" ,
1103+ setup : func (s * notificationStorage ) {
1104+ s .qe .(* mock.MockQueryExecer ).EXPECT ().QueryRowContext (
1105+ gomock .Any (), selectEarliestAccountCreatedAtSQL , "viewer@example.com" ,
1106+ ).Return (newBoundRow (5 ))
1107+ row := mock .NewMockRow (mockController )
1108+ row .EXPECT ().Scan (gomock .Any ()).Return (errors .New ("error" ))
1109+ s .qe .(* mock.MockQueryExecer ).EXPECT ().QueryRowContext (
1110+ gomock .Any (), gomock .Any (), gomock .Any (),
1111+ ).Return (row )
1112+ },
1113+ expected : 0 ,
1114+ expectedErr : errors .New ("error" ),
1115+ },
1116+ {
1117+ desc : "Success" ,
1118+ setup : func (s * notificationStorage ) {
1119+ s .qe .(* mock.MockQueryExecer ).EXPECT ().QueryRowContext (
1120+ gomock .Any (), selectEarliestAccountCreatedAtSQL , "viewer@example.com" ,
1121+ ).Return (newBoundRow (5 ))
1122+ row := mock .NewMockRow (mockController )
1123+ row .EXPECT ().Scan (gomock .Any ()).DoAndReturn (func (args ... interface {}) error {
1124+ * args [0 ].(* int64 ) = int64 (3 )
1125+ return nil
1126+ })
1127+ s .qe .(* mock.MockQueryExecer ).EXPECT ().QueryRowContext (
1128+ gomock .Any (), gomock .Any (), gomock .Any (),
1129+ ).Return (row )
1130+ },
1131+ expected : 3 ,
1132+ expectedErr : nil ,
1133+ },
1134+ }
1135+ for _ , p := range patterns {
1136+ t .Run (p .desc , func (t * testing.T ) {
1137+ storage := & notificationStorage {qe : mock .NewMockQueryExecer (mockController )}
1138+ if p .setup != nil {
1139+ p .setup (storage )
1140+ }
1141+ count , err := storage .GetNotificationUnreadCount (context .Background (), "viewer@example.com" )
1142+ assert .Equal (t , p .expectedErr , err )
1143+ assert .Equal (t , p .expected , count )
1144+ })
1145+ }
1146+ }
0 commit comments