@@ -25,12 +25,12 @@ func TestTransactionStore(t *testing.T) {
2525 transactionStore := redis .NewTransactionStore (mockRedisClient )
2626
2727 // Mock data
28- userID := primitive .NewObjectID (). Hex ()
28+ userID := primitive .NewObjectID ()
2929
3030 mockTransactions := []entities.Transaction {
3131 {
3232 ID : primitive .NewObjectID (),
33- UserID : primitive . NewObjectID () ,
33+ UserID : userID ,
3434 Amount : 100 ,
3535 Description : "Groceries" ,
3636 Date : time .Now (),
@@ -41,7 +41,7 @@ func TestTransactionStore(t *testing.T) {
4141 },
4242 {
4343 ID : primitive .NewObjectID (),
44- UserID : primitive . NewObjectID () ,
44+ UserID : userID ,
4545 Amount : 200 ,
4646 Description : "Rent" ,
4747 Date : time .Now (),
@@ -56,12 +56,12 @@ func TestTransactionStore(t *testing.T) {
5656 mockData , _ := json .Marshal (mockTransactions )
5757
5858 // Mock the Get method to return the serialized JSON data
59- mockRedisClient .EXPECT ().Get (gomock .Any (), fmt .Sprintf ("user:%s:transactions" , userID ), gomock .Any ()).DoAndReturn (
59+ mockRedisClient .EXPECT ().Get (gomock .Any (), fmt .Sprintf ("user:%s:transactions" , userID . Hex () ), gomock .Any ()).DoAndReturn (
6060 func (_ context.Context , _ string , dest interface {}) error {
6161 return json .Unmarshal (mockData , dest )
6262 },
6363 )
64- transactions , err := transactionStore .GetByUserId (context .Background (), userID )
64+ transactions , err := transactionStore .GetByUserId (context .Background (), userID . Hex () )
6565 require .NoError (t , err )
6666 assert .NotNil (t , transactions )
6767 })
@@ -70,24 +70,24 @@ func TestTransactionStore(t *testing.T) {
7070 mockRedisClient := redis .NewMockRedisClient (ctrl )
7171 transactionStore := redis .NewTransactionStore (mockRedisClient )
7272
73- userID := primitive .NewObjectID (). Hex ()
73+ userID := primitive .NewObjectID ()
7474 mockRedisClient .EXPECT ().Get (gomock .Any (), gomock .Any (), gomock .Any ()).Return (goredis .Nil )
7575
76- transactions , err := transactionStore .GetByUserId (context .Background (), userID )
76+ transactions , err := transactionStore .GetByUserId (context .Background (), userID . Hex () )
7777 require .Error (t , err )
7878 assert .Nil (t , transactions )
7979 })
8080
81- t .Run ("SetByUserIdSuccess " , func (t * testing.T ) {
81+ t .Run ("SetMultipleByUserIdWithExistingTransactions " , func (t * testing.T ) {
8282 mockRedisClient := redis .NewMockRedisClient (ctrl )
8383 transactionStore := redis .NewTransactionStore (mockRedisClient )
8484
85- // mock data
86- userID := primitive .NewObjectID (). Hex ()
87- mockTransactions := []entities.Transaction {
85+ // Mock data
86+ userID := primitive .NewObjectID ()
87+ existingTransactions := []entities.Transaction {
8888 {
8989 ID : primitive .NewObjectID (),
90- UserID : primitive . NewObjectID () ,
90+ UserID : userID ,
9191 Amount : 100 ,
9292 Description : "Groceries" ,
9393 Date : time .Now (),
@@ -96,9 +96,11 @@ func TestTransactionStore(t *testing.T) {
9696 CreatedAt : time .Now (),
9797 UpdatedAt : time .Now (),
9898 },
99+ }
100+ newTransactions := []entities.Transaction {
99101 {
100102 ID : primitive .NewObjectID (),
101- UserID : primitive . NewObjectID () ,
103+ UserID : userID ,
102104 Amount : 200 ,
103105 Description : "Rent" ,
104106 Date : time .Now (),
@@ -107,21 +109,58 @@ func TestTransactionStore(t *testing.T) {
107109 CreatedAt : time .Now (),
108110 UpdatedAt : time .Now (),
109111 },
112+ {
113+ ID : primitive .NewObjectID (),
114+ UserID : userID ,
115+ Amount : 50 ,
116+ Description : "Utilities" ,
117+ Date : time .Now (),
118+ Category : "Bills" ,
119+ Type : "expense" ,
120+ CreatedAt : time .Now (),
121+ UpdatedAt : time .Now (),
122+ },
110123 }
111- mockRedisClient .EXPECT ().Set (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil )
112124
113- err := transactionStore .SetByUserId (context .Background (), userID , mockTransactions )
125+ // Serialize existing transactions to JSON
126+ mockData , _ := json .Marshal (existingTransactions )
127+
128+ // Mock the Get method to return the existing transactions
129+ mockRedisClient .EXPECT ().Get (
130+ gomock .Any (),
131+ fmt .Sprintf ("user:%s:transactions" , userID .Hex ()),
132+ gomock .Any (),
133+ ).DoAndReturn (
134+ func (_ context.Context , _ string , dest interface {}) error {
135+ return json .Unmarshal (mockData , dest )
136+ },
137+ )
138+
139+ // Mock the Set method to save the updated transactions
140+ mockRedisClient .EXPECT ().Set (
141+ gomock .Any (),
142+ fmt .Sprintf ("user:%s:transactions" , userID .Hex ()),
143+ gomock .Any (),
144+ gomock .Any (),
145+ ).DoAndReturn (
146+ func (_ context.Context , _ string , value interface {}, _ time.Duration ) error {
147+ return nil
148+ },
149+ )
150+
151+ // Call SetMultipleByUserId
152+ err := transactionStore .SetMultipleByUserId (context .Background (), userID .Hex (), newTransactions )
114153 require .NoError (t , err )
115154 })
116155
117156 t .Run ("DeleteTransactionSuccess" , func (t * testing.T ) {
118157 mockRedisClient := redis .NewMockRedisClient (ctrl )
119158 transactionStore := redis .NewTransactionStore (mockRedisClient )
120159
121- userID := primitive .NewObjectID (). Hex ()
122- mockRedisClient .EXPECT ().Delete (gomock .Any (), fmt .Sprintf ("user:%s:transactions" , userID )).Return (nil )
160+ userID := primitive .NewObjectID ()
161+ mockRedisClient .EXPECT ().Delete (gomock .Any (), fmt .Sprintf ("user:%s:transactions" , userID . Hex () )).Return (nil )
123162
124- err := transactionStore .DeleteByUserId (context .Background (), userID )
163+ err := transactionStore .DeleteByUserId (context .Background (), userID . Hex () )
125164 require .NoError (t , err )
126165 })
127166}
0 commit comments