@@ -3,6 +3,7 @@ package service
3
3
import (
4
4
"database/sql"
5
5
"errors"
6
+ "math"
6
7
"reflect"
7
8
"regexp"
8
9
"testing"
@@ -25,9 +26,13 @@ func (*mockMempoolQueryExecutorSuccess) ExecuteSelect(qe string, args ...interfa
25
26
defer db .Close ()
26
27
switch qe {
27
28
case "SELECT ID, FeePerByte, ArrivalTimestamp, TransactionBytes FROM mempool" :
28
- mock .ExpectQuery (regexp .QuoteMeta (qe )).WillReturnRows (sqlmock .NewRows ([]string {
29
- "ID" , "FeePerByte" , "ArrivalTimestamp" , "TransactionBytes" },
30
- ).AddRow (make ([]byte , 32 ), 1 , 1562893302 , []byte {}))
29
+ mockedRows := sqlmock .NewRows ([]string {"ID" , "FeePerByte" , "ArrivalTimestamp" , "TransactionBytes" })
30
+ mockedRows .AddRow (make ([]byte , 32 ), 1 , 1562893305 , []byte {5 , 5 , 5 , 5 , 5 })
31
+ mockedRows .AddRow (make ([]byte , 32 ), 10 , 1562893304 , []byte {2 , 2 , 2 , 2 , 2 })
32
+ mockedRows .AddRow (make ([]byte , 32 ), 1 , 1562893302 , []byte {1 , 1 , 1 , 1 , 1 })
33
+ mockedRows .AddRow (make ([]byte , 32 ), 100 , 1562893306 , []byte {4 , 4 , 4 , 4 , 4 })
34
+ mockedRows .AddRow (make ([]byte , 32 ), 5 , 1562893303 , []byte {4 , 4 , 4 , 4 , 4 })
35
+ mock .ExpectQuery (regexp .QuoteMeta (qe )).WillReturnRows (mockedRows )
31
36
case "SELECT ID, FeePerByte, ArrivalTimestamp, TransactionBytes FROM mempool WHERE id = :id" :
32
37
return nil , errors .New ("MempoolTransactionNotFound" )
33
38
}
@@ -155,11 +160,35 @@ func TestMempoolService_GetMempoolTransactions(t *testing.T) {
155
160
QueryExecutor : & mockMempoolQueryExecutorSuccess {},
156
161
},
157
162
want : []* model.MempoolTransaction {
163
+ {
164
+ ID : make ([]byte , 32 ),
165
+ FeePerByte : 1 ,
166
+ ArrivalTimestamp : 1562893305 ,
167
+ TransactionBytes : []byte {5 , 5 , 5 , 5 , 5 },
168
+ },
169
+ {
170
+ ID : make ([]byte , 32 ),
171
+ FeePerByte : 10 ,
172
+ ArrivalTimestamp : 1562893304 ,
173
+ TransactionBytes : []byte {2 , 2 , 2 , 2 , 2 },
174
+ },
158
175
{
159
176
ID : make ([]byte , 32 ),
160
177
FeePerByte : 1 ,
161
178
ArrivalTimestamp : 1562893302 ,
162
- TransactionBytes : []byte {},
179
+ TransactionBytes : []byte {1 , 1 , 1 , 1 , 1 },
180
+ },
181
+ {
182
+ ID : make ([]byte , 32 ),
183
+ FeePerByte : 100 ,
184
+ ArrivalTimestamp : 1562893306 ,
185
+ TransactionBytes : []byte {4 , 4 , 4 , 4 , 4 },
186
+ },
187
+ {
188
+ ID : make ([]byte , 32 ),
189
+ FeePerByte : 5 ,
190
+ ArrivalTimestamp : 1562893303 ,
191
+ TransactionBytes : []byte {4 , 4 , 4 , 4 , 4 },
163
192
},
164
193
},
165
194
wantErr : false ,
@@ -301,3 +330,77 @@ func TestMempoolService_RemoveMempoolTransaction(t *testing.T) {
301
330
})
302
331
}
303
332
}
333
+
334
+ func TestMempoolService_SelectTransactionsFromMempool (t * testing.T ) {
335
+ type fields struct {
336
+ Chaintype contract.ChainType
337
+ QueryExecutor query.ExecutorInterface
338
+ MempoolQuery query.MempoolQueryInterface
339
+ }
340
+ type args struct {
341
+ blockTimestamp int64
342
+ }
343
+ tests := []struct {
344
+ name string
345
+ fields fields
346
+ args args
347
+ want []* model.MempoolTransaction
348
+ wantErr bool
349
+ }{
350
+ {
351
+ name : "SelectTransactionsFromMempool:Success" ,
352
+ fields : fields {
353
+ Chaintype : & chaintype.MainChain {},
354
+ MempoolQuery : query .NewMempoolQuery (& chaintype.MainChain {}),
355
+ QueryExecutor : & mockMempoolQueryExecutorSuccess {},
356
+ },
357
+ args : args {
358
+ blockTimestamp : math .MaxInt64 ,
359
+ },
360
+ want : []* model.MempoolTransaction {
361
+ {
362
+ ID : make ([]byte , 32 ),
363
+ FeePerByte : 100 ,
364
+ ArrivalTimestamp : 1562893306 ,
365
+ TransactionBytes : []byte {4 , 4 , 4 , 4 , 4 },
366
+ },
367
+ {
368
+ ID : make ([]byte , 32 ),
369
+ FeePerByte : 10 ,
370
+ ArrivalTimestamp : 1562893304 ,
371
+ TransactionBytes : []byte {2 , 2 , 2 , 2 , 2 },
372
+ },
373
+ {
374
+ ID : make ([]byte , 32 ),
375
+ FeePerByte : 1 ,
376
+ ArrivalTimestamp : 1562893302 ,
377
+ TransactionBytes : []byte {1 , 1 , 1 , 1 , 1 },
378
+ },
379
+ {
380
+ ID : make ([]byte , 32 ),
381
+ FeePerByte : 1 ,
382
+ ArrivalTimestamp : 1562893305 ,
383
+ TransactionBytes : []byte {5 , 5 , 5 , 5 , 5 },
384
+ },
385
+ },
386
+ wantErr : false ,
387
+ },
388
+ }
389
+ for _ , tt := range tests {
390
+ t .Run (tt .name , func (t * testing.T ) {
391
+ mps := & MempoolService {
392
+ Chaintype : tt .fields .Chaintype ,
393
+ QueryExecutor : tt .fields .QueryExecutor ,
394
+ MempoolQuery : tt .fields .MempoolQuery ,
395
+ }
396
+ got , err := mps .SelectTransactionsFromMempool (tt .args .blockTimestamp )
397
+ if (err != nil ) != tt .wantErr {
398
+ t .Errorf ("MempoolService.SelectTransactionsFromMempool() error = %v, wantErr %v" , err , tt .wantErr )
399
+ return
400
+ }
401
+ if ! reflect .DeepEqual (got , tt .want ) {
402
+ t .Errorf ("MempoolService.SelectTransactionsFromMempool() = %v, want %v" , got , tt .want )
403
+ }
404
+ })
405
+ }
406
+ }
0 commit comments