@@ -14,21 +14,26 @@ import (
14
14
"github.com/stretchr/testify/assert"
15
15
)
16
16
17
- func TestTransactinList (t * testing.T ) {
17
+ func testTxTable (t * testing.T ) ( string , db. DB , db. KVDB , * Table ) {
18
18
dir , ldb , kvdb := util .CreateTestDB ()
19
- defer util .CloseTestDB (dir , ldb )
20
19
opt := & Option {
21
20
Prefix : "prefix" ,
22
21
Name : "name" ,
23
22
Primary : "Hash" ,
24
23
Index : []string {"From" , "To" },
25
24
}
26
- cfg := types .NewChain33Config (types .GetDefaultCfgstring ())
27
25
table , err := NewTable (NewTransactionRow (), kvdb , opt )
28
26
assert .Nil (t , err )
27
+ return dir , ldb , kvdb , table
28
+ }
29
+
30
+ func TestTransactinList (t * testing.T ) {
31
+ dir , ldb , kvdb , table := testTxTable (t )
32
+ defer util .CloseTestDB (dir , ldb )
33
+ cfg := types .NewChain33Config (types .GetDefaultCfgstring ())
29
34
addr1 , priv := util .Genaddress ()
30
35
tx1 := util .CreateNoneTx (cfg , priv )
31
- err = table .Add (tx1 )
36
+ err : = table .Add (tx1 )
32
37
assert .Nil (t , err )
33
38
tx2 := util .CreateNoneTx (cfg , priv )
34
39
err = table .Add (tx2 )
@@ -448,3 +453,68 @@ func (tx *TransactionRow) Get(key string) ([]byte, error) {
448
453
}
449
454
return nil , types .ErrNotFound
450
455
}
456
+
457
+ func TestTableListPrimary (t * testing.T ) {
458
+
459
+ dir , ldb , _ , table := testTxTable (t )
460
+ defer util .CloseTestDB (dir , ldb )
461
+
462
+ cfg := types .NewChain33Config (types .GetDefaultCfgstring ())
463
+ addr1 , priv1 := util .Genaddress ()
464
+ _ , priv2 := util .Genaddress ()
465
+ tx1 := util .CreateNoneTx (cfg , priv1 )
466
+ assert .Nil (t , table .Add (tx1 ))
467
+ tx2 := util .CreateNoneTx (cfg , priv2 )
468
+ assert .Nil (t , table .Add (tx2 ))
469
+ kvs , err := table .Save ()
470
+ assert .Nil (t , err )
471
+ //save to database
472
+ util .SaveKVList (ldb , kvs )
473
+ hash := tx1 .Hash ()
474
+ if bytes .Compare (hash , tx2 .Hash ()) > 0 {
475
+ hash = tx2 .Hash ()
476
+ }
477
+
478
+ // List
479
+ rows , err := table .ListIndex ("Hash" , nil , nil , 10 , db .ListASC )
480
+ assert .Nil (t , err )
481
+ assert .Equal (t , 2 , len (rows ))
482
+ assert .Equal (t , hash , rows [0 ].Primary )
483
+
484
+ // List with prefix
485
+ rows , err = table .ListIndex ("Hash" , hash [:20 ], nil , 10 , db .ListASC )
486
+ assert .Equal (t , 1 , len (rows ))
487
+
488
+ // List with primary
489
+ rows , err = table .ListIndex ("Hash" , nil , hash , 10 , db .ListASC )
490
+ assert .Equal (t , 1 , len (rows ))
491
+ rows , err = table .ListIndex ("Hash" , nil , hash , 10 , db .ListDESC )
492
+ assert .Equal (t , types .ErrNotFound , err )
493
+
494
+ // List with primary and prefix
495
+ rows , err = table .ListIndex ("Hash" , hash [:20 ], hash , 10 , db .ListASC )
496
+ assert .Equal (t , types .ErrNotFound , err )
497
+ rows , err = table .ListIndex ("Hash" , hash [:20 ], hash , 10 , db .ListDESC )
498
+ assert .Equal (t , types .ErrNotFound , err )
499
+
500
+ // List index
501
+ rows , err = table .ListIndex ("From" , nil , nil , 10 , db .ListDESC )
502
+ assert .Nil (t , err )
503
+ assert .Equal (t , 2 , len (rows ))
504
+
505
+ // List with prefix
506
+ rows , err = table .ListIndex ("From" , []byte (addr1 [:20 ]), nil , 10 , db .ListDESC )
507
+ assert .Equal (t , 1 , len (rows ))
508
+ assert .Equal (t , tx1 .Hash (), rows [0 ].Primary )
509
+
510
+ // List with primary
511
+ rows , err = table .ListIndex ("From" , nil , hash , 10 , db .ListASC )
512
+ assert .Equal (t , 1 , len (rows ))
513
+ assert .NotEqual (t , hash , rows [0 ].Primary )
514
+ rows , err = table .ListIndex ("From" , nil , hash , 10 , db .ListDESC )
515
+ assert .Equal (t , types .ErrNotFound , err )
516
+
517
+ // List with primary and prefix
518
+ rows , err = table .ListIndex ("From" , []byte (addr1 )[:20 ], tx1 .Hash (), 10 , db .ListASC )
519
+ assert .Equal (t , types .ErrNotFound , err )
520
+ }
0 commit comments