11package tests
22
33import (
4+ "context"
45 "testing"
6+ "time"
7+
8+ "github.com/stretchr/testify/require"
9+ "google.golang.org/protobuf/types/known/timestamppb"
10+
11+ activitypb "github.com/code-payments/flipchat-protobuf-api/generated/go/activity/v1"
512
613 "github.com/code-payments/flipchat-server/activity"
14+ "github.com/code-payments/flipchat-server/model"
15+ "github.com/code-payments/flipchat-server/protoutil"
716)
817
918func RunStoreTests (t * testing.T , s activity.Store , teardown func ()) {
@@ -16,4 +25,96 @@ func RunStoreTests(t *testing.T, s activity.Store, teardown func()) {
1625}
1726
1827func testActivityStore_HappyPath (t * testing.T , store activity.Store ) {
28+ userID := model .MustGenerateUserID ()
29+ var allExpected []* activitypb.Notification
30+
31+ t .Run ("Empty" , func (t * testing.T ) {
32+ actual , err := store .GetLatestNotifications (context .Background (), activitypb .ActivityFeedType_TRANSACTION_HISTORY , userID , 10 )
33+ require .NoError (t , err )
34+ require .Empty (t , actual )
35+ })
36+
37+ t .Run ("Welcome Bonus" , func (t * testing.T ) {
38+ expected , err := activity .NewWelcomeBonusNotificationBuilder (context .Background (), userID , 123 , time .Unix (1 , 0 ))()
39+ require .NoError (t , err )
40+ allExpected = append ([]* activitypb.Notification {expected }, allExpected ... )
41+
42+ actual , err := store .SaveNotification (context .Background (), activitypb .ActivityFeedType_TRANSACTION_HISTORY , userID , expected )
43+ require .NoError (t , err )
44+ require .NoError (t , protoutil .ProtoEqualError (expected , actual ))
45+
46+ allActual , err := store .GetLatestNotifications (context .Background (), activitypb .ActivityFeedType_TRANSACTION_HISTORY , userID , 1 )
47+ require .NoError (t , err )
48+ require .Len (t , allActual , 1 )
49+ require .NoError (t , protoutil .ProtoEqualError (expected , allActual [0 ]))
50+
51+ other , err := activity .NewWelcomeBonusNotificationBuilder (context .Background (), userID , 42 , time .Unix (123456789 , 0 ))()
52+ require .NoError (t , err )
53+
54+ actual , err = store .SaveNotification (context .Background (), activitypb .ActivityFeedType_TRANSACTION_HISTORY , userID , other )
55+ require .NoError (t , err )
56+ require .NoError (t , protoutil .ProtoEqualError (expected , actual ))
57+
58+ allActual , err = store .GetLatestNotifications (context .Background (), activitypb .ActivityFeedType_TRANSACTION_HISTORY , userID , 1 )
59+ require .NoError (t , err )
60+ require .Len (t , allActual , 1 )
61+ require .NoError (t , protoutil .ProtoEqualError (expected , allActual [0 ]))
62+ })
63+
64+ t .Run ("Weekly Bonus" , func (t * testing.T ) {
65+ expected , err := activity .NewWeeklyBonusNotificationBuilder (context .Background (), userID , 456 , time .Unix (2 , 0 ))()
66+ require .NoError (t , err )
67+ allExpected = append ([]* activitypb.Notification {expected }, allExpected ... )
68+
69+ actual , err := store .SaveNotification (context .Background (), activitypb .ActivityFeedType_TRANSACTION_HISTORY , userID , expected )
70+ require .NoError (t , err )
71+ require .NoError (t , protoutil .ProtoEqualError (expected , actual ))
72+
73+ allActual , err := store .GetLatestNotifications (context .Background (), activitypb .ActivityFeedType_TRANSACTION_HISTORY , userID , 1 )
74+ require .NoError (t , err )
75+ require .Len (t , allActual , 1 )
76+ require .NoError (t , protoutil .ProtoEqualError (expected , allActual [0 ]))
77+
78+ other , err := activity .NewWeeklyBonusNotificationBuilder (context .Background (), userID , 42 , time .Unix (3 , 0 ))()
79+ require .NoError (t , err )
80+
81+ actual , err = store .SaveNotification (context .Background (), activitypb .ActivityFeedType_TRANSACTION_HISTORY , userID , other )
82+ require .NoError (t , err )
83+ require .NoError (t , protoutil .ProtoEqualError (expected , actual ))
84+
85+ allActual , err = store .GetLatestNotifications (context .Background (), activitypb .ActivityFeedType_TRANSACTION_HISTORY , userID , 1 )
86+ require .NoError (t , err )
87+ require .Len (t , allActual , 1 )
88+ require .NoError (t , protoutil .ProtoEqualError (expected , allActual [0 ]))
89+ })
90+
91+ t .Run ("Get Latest Notifications" , func (t * testing.T ) {
92+ allActual , err := store .GetLatestNotifications (context .Background (), activitypb .ActivityFeedType_TRANSACTION_HISTORY , userID , 100 )
93+ require .NoError (t , err )
94+ require .NoError (t , protoutil .SliceEqualError (allExpected , allActual ))
95+
96+ allActual , err = store .GetLatestNotifications (context .Background (), activitypb .ActivityFeedType_TRANSACTION_HISTORY , userID , len (allExpected )/ 2 )
97+ require .NoError (t , err )
98+ require .NoError (t , protoutil .SliceEqualError (allExpected [:len (allExpected )/ 2 ], allActual ))
99+ })
100+
101+ t .Run ("Invalid Activity Feed" , func (t * testing.T ) {
102+ expected , err := activity .NewWelcomeBonusNotificationBuilder (context .Background (), userID , 123 , time .Unix (1 , 0 ))()
103+ require .NoError (t , err )
104+
105+ _ , err = store .SaveNotification (context .Background (), activitypb .ActivityFeedType_UNKNOWN , userID , expected )
106+ require .Equal (t , activity .ErrInvalidActivityFeedType , err )
107+
108+ _ , err = store .GetLatestNotifications (context .Background (), activitypb .ActivityFeedType_UNKNOWN , userID , 100 )
109+ require .Equal (t , activity .ErrInvalidActivityFeedType , err )
110+ })
111+
112+ t .Run ("Invalid Notification" , func (t * testing.T ) {
113+ _ , err := store .SaveNotification (context .Background (), activitypb .ActivityFeedType_TRANSACTION_HISTORY , userID , & activitypb.Notification {
114+ Id : & activitypb.NotificationId {Value : make ([]byte , 32 )},
115+ AdditionalMetadata : nil ,
116+ Ts : timestamppb .Now (),
117+ })
118+ require .Equal (t , activity .ErrInvalidNotificationType , err )
119+ })
19120}
0 commit comments