@@ -206,3 +206,159 @@ func TestTransactionExistsByRef_Failure(t *testing.T) {
206
206
assert .Error (t , err )
207
207
assert .IsType (t , apierror.APIError {}, err )
208
208
}
209
+
210
+ func TestGetInflightTransactionsByParentID_Success (t * testing.T ) {
211
+ db , mock , err := sqlmock .New ()
212
+ assert .NoError (t , err )
213
+ defer db .Close ()
214
+
215
+ tracer := otel .Tracer ("transaction.database" )
216
+ ctx , span := tracer .Start (context .Background (), "TestGetInflightTransactionsByParentID" )
217
+ defer span .End ()
218
+
219
+ ds := Datasource {Conn : db }
220
+
221
+ // Create test data
222
+ parentID := "parent123"
223
+ metaData := map [string ]interface {}{"key" : "value" }
224
+ metaDataJSON , err := json .Marshal (metaData )
225
+ assert .NoError (t , err )
226
+
227
+ mockRows := sqlmock .NewRows ([]string {
228
+ "transaction_id" , "parent_transaction" , "source" , "reference" ,
229
+ "amount" , "precise_amount" , "precision" , "rate" , "currency" ,
230
+ "destination" , "description" , "status" , "created_at" ,
231
+ "meta_data" , "scheduled_for" , "hash" ,
232
+ }).AddRow (
233
+ "txn123" , parentID , "source1" , "ref123" ,
234
+ 1000 , 1000 , 2 , 1.0 , "USD" ,
235
+ "dest1" , "Test Transaction" , "INFLIGHT" , time .Now (),
236
+ metaDataJSON , time .Now (), "hash123" ,
237
+ )
238
+
239
+ // Expect the query with the correct WHERE clause
240
+ mock .ExpectQuery ("SELECT .+ FROM blnk.transactions WHERE \\ (transaction_id = \\ $1 OR parent_transaction = \\ $1\\ ) AND status = 'INFLIGHT'" ).
241
+ WithArgs (parentID , 10 , int64 (0 )). // Add all three expected arguments
242
+ WillReturnRows (mockRows )
243
+
244
+ transactions , err := ds .GetInflightTransactionsByParentID (ctx , parentID , 10 , 0 )
245
+ assert .NoError (t , err )
246
+ assert .Len (t , transactions , 1 )
247
+ assert .Equal (t , "txn123" , transactions [0 ].TransactionID )
248
+ assert .Equal (t , "INFLIGHT" , transactions [0 ].Status )
249
+ }
250
+
251
+ func TestGetInflightTransactionsByParentID_NoRows (t * testing.T ) {
252
+ db , mock , err := sqlmock .New ()
253
+ assert .NoError (t , err )
254
+ defer db .Close ()
255
+
256
+ tracer := otel .Tracer ("transaction.database" )
257
+ ctx , span := tracer .Start (context .Background (), "TestGetInflightTransactionsByParentID_NoRows" )
258
+ defer span .End ()
259
+
260
+ ds := Datasource {Conn : db }
261
+
262
+ parentID := "parent123"
263
+ mock .ExpectQuery ("SELECT .+ FROM blnk.transactions WHERE \\ (transaction_id = \\ $1 OR parent_transaction = \\ $1\\ ) AND status = 'INFLIGHT'" ).
264
+ WithArgs (parentID , 10 , int64 (0 )). // Add all three expected arguments
265
+ WillReturnRows (sqlmock .NewRows ([]string {}))
266
+
267
+ transactions , err := ds .GetInflightTransactionsByParentID (ctx , parentID , 10 , 0 )
268
+ assert .NoError (t , err )
269
+ assert .Empty (t , transactions )
270
+ }
271
+
272
+ func TestGetInflightTransactionsByParentID_Error (t * testing.T ) {
273
+ db , mock , err := sqlmock .New ()
274
+ assert .NoError (t , err )
275
+ defer db .Close ()
276
+
277
+ tracer := otel .Tracer ("transaction.database" )
278
+ ctx , span := tracer .Start (context .Background (), "TestGetInflightTransactionsByParentID_Error" )
279
+ defer span .End ()
280
+
281
+ ds := Datasource {Conn : db }
282
+
283
+ parentID := "parent123"
284
+ mock .ExpectQuery ("SELECT .+ FROM blnk.transactions WHERE \\ (transaction_id = \\ $1 OR parent_transaction = \\ $1\\ ) AND status = 'INFLIGHT'" ).
285
+ WithArgs (parentID , 10 , int64 (0 )). // Add all three expected arguments
286
+ WillReturnError (errors .New ("database error" ))
287
+
288
+ transactions , err := ds .GetInflightTransactionsByParentID (ctx , parentID , 10 , 0 )
289
+ assert .Error (t , err )
290
+ assert .Nil (t , transactions )
291
+ apiErr , ok := err .(apierror.APIError )
292
+ assert .True (t , ok )
293
+ assert .Equal (t , apierror .ErrInternalServer , apiErr .Code )
294
+ }
295
+
296
+ func TestGetTotalCommittedTransactions_Success (t * testing.T ) {
297
+ db , mock , err := sqlmock .New ()
298
+ assert .NoError (t , err )
299
+ defer db .Close ()
300
+
301
+ tracer := otel .Tracer ("transaction.database" )
302
+ ctx , span := tracer .Start (context .Background (), "TestGetTotalCommittedTransactions" )
303
+ defer span .End ()
304
+
305
+ ds := Datasource {Conn : db }
306
+
307
+ parentID := "parent123"
308
+ expectedTotal := int64 (2000 )
309
+
310
+ mock .ExpectQuery ("SELECT SUM\\ (precise_amount\\ ) AS total_amount FROM blnk.transactions WHERE parent_transaction = \\ $1 GROUP BY parent_transaction" ).
311
+ WithArgs (parentID ).
312
+ WillReturnRows (sqlmock .NewRows ([]string {"total_amount" }).AddRow (expectedTotal ))
313
+
314
+ total , err := ds .GetTotalCommittedTransactions (ctx , parentID )
315
+ assert .NoError (t , err )
316
+ assert .Equal (t , expectedTotal , total )
317
+ }
318
+
319
+ func TestGetTotalCommittedTransactions_NoRows (t * testing.T ) {
320
+ db , mock , err := sqlmock .New ()
321
+ assert .NoError (t , err )
322
+ defer db .Close ()
323
+
324
+ tracer := otel .Tracer ("transaction.database" )
325
+ ctx , span := tracer .Start (context .Background (), "TestGetTotalCommittedTransactions_NoRows" )
326
+ defer span .End ()
327
+
328
+ ds := Datasource {Conn : db }
329
+
330
+ parentID := "parent123"
331
+
332
+ mock .ExpectQuery ("SELECT SUM\\ (precise_amount\\ ) AS total_amount FROM blnk.transactions WHERE parent_transaction = \\ $1 GROUP BY parent_transaction" ).
333
+ WithArgs (parentID ).
334
+ WillReturnError (sql .ErrNoRows )
335
+
336
+ total , err := ds .GetTotalCommittedTransactions (ctx , parentID )
337
+ assert .NoError (t , err )
338
+ assert .Equal (t , int64 (0 ), total )
339
+ }
340
+
341
+ func TestGetTotalCommittedTransactions_Error (t * testing.T ) {
342
+ db , mock , err := sqlmock .New ()
343
+ assert .NoError (t , err )
344
+ defer db .Close ()
345
+
346
+ tracer := otel .Tracer ("transaction.database" )
347
+ ctx , span := tracer .Start (context .Background (), "TestGetTotalCommittedTransactions_Error" )
348
+ defer span .End ()
349
+
350
+ ds := Datasource {Conn : db }
351
+
352
+ parentID := "parent123"
353
+
354
+ mock .ExpectQuery ("SELECT SUM\\ (precise_amount\\ ) AS total_amount FROM blnk.transactions WHERE parent_transaction = \\ $1 GROUP BY parent_transaction" ).
355
+ WithArgs (parentID ).
356
+ WillReturnError (errors .New ("database error" ))
357
+
358
+ total , err := ds .GetTotalCommittedTransactions (ctx , parentID )
359
+ assert .Error (t , err )
360
+ assert .Equal (t , int64 (0 ), total )
361
+ apiErr , ok := err .(apierror.APIError )
362
+ assert .True (t , ok )
363
+ assert .Equal (t , apierror .ErrInternalServer , apiErr .Code )
364
+ }
0 commit comments