OSDN Git Service

add TestDeleteWalletTransactions
authorChengcheng Zhang <943420582@qq.com>
Thu, 18 Jul 2019 12:47:57 +0000 (20:47 +0800)
committerChengcheng Zhang <943420582@qq.com>
Thu, 18 Jul 2019 12:47:57 +0000 (20:47 +0800)
database/wallet_store_test.go

index da482f8..3c0f91e 100644 (file)
@@ -620,7 +620,7 @@ func TestDeleteTransactions(t *testing.T) {
                        },
                },
                {
-                       height: uint64(0),
+                       height: uint64(1000000),
                        txs: []*query.AnnotatedTx{
                                &query.AnnotatedTx{
                                        ID:          bc.NewHash([32]byte{0x01, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
@@ -732,3 +732,125 @@ func TestDeleteTransactions(t *testing.T) {
                os.RemoveAll("temp")
        }
 }
+
+func TestDeleteWalletTransactions(t *testing.T) {
+       cases := []struct {
+               height uint64
+               txs    []*query.AnnotatedTx
+       }{
+               {
+                       height: uint64(0),
+                       txs:    []*query.AnnotatedTx{},
+               },
+               {
+                       height: uint64(0),
+                       txs: []*query.AnnotatedTx{
+                               &query.AnnotatedTx{
+                                       ID:          bc.NewHash([32]byte{0x01, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                                       Timestamp:   uint64(9988000),
+                                       BlockHeight: uint64(0),
+                                       Position:    uint32(2333),
+                               },
+                       },
+               },
+               {
+                       height: uint64(1000000),
+                       txs: []*query.AnnotatedTx{
+                               &query.AnnotatedTx{
+                                       ID:          bc.NewHash([32]byte{0x01, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                                       Timestamp:   uint64(9988000),
+                                       BlockHeight: uint64(1000000),
+                                       Position:    uint32(0),
+                               },
+                               &query.AnnotatedTx{
+                                       ID:          bc.NewHash([32]byte{0x02, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                                       Timestamp:   uint64(9988000),
+                                       BlockHeight: uint64(1000000),
+                                       Position:    uint32(1),
+                               },
+                               &query.AnnotatedTx{
+                                       ID:          bc.NewHash([32]byte{0x03, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                                       Timestamp:   uint64(9988000),
+                                       BlockHeight: uint64(1000000),
+                                       Position:    uint32(2),
+                               },
+                               &query.AnnotatedTx{
+                                       ID:          bc.NewHash([32]byte{0x04, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                                       Timestamp:   uint64(9988000),
+                                       BlockHeight: uint64(1000000),
+                                       Position:    uint32(3),
+                               },
+                               &query.AnnotatedTx{
+                                       ID:          bc.NewHash([32]byte{0x05, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                                       Timestamp:   uint64(9988000),
+                                       BlockHeight: uint64(1000000),
+                                       Position:    uint32(4),
+                               },
+                               &query.AnnotatedTx{
+                                       ID:          bc.NewHash([32]byte{0x06, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                                       Timestamp:   uint64(9988000),
+                                       BlockHeight: uint64(1000000),
+                                       Position:    uint32(5),
+                               },
+                               &query.AnnotatedTx{
+                                       ID:          bc.NewHash([32]byte{0x07, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                                       Timestamp:   uint64(9988000),
+                                       BlockHeight: uint64(1000000),
+                                       Position:    uint32(6),
+                               },
+                               &query.AnnotatedTx{
+                                       ID:          bc.NewHash([32]byte{0x08, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                                       Timestamp:   uint64(9988000),
+                                       BlockHeight: uint64(1000000),
+                                       Position:    uint32(7),
+                               },
+                               &query.AnnotatedTx{
+                                       ID:          bc.NewHash([32]byte{0x09, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                                       Timestamp:   uint64(9988000),
+                                       BlockHeight: uint64(1000000),
+                                       Position:    uint32(8),
+                               },
+                               &query.AnnotatedTx{
+                                       ID:          bc.NewHash([32]byte{0x0a, 0x01, 0x51, 0x31, 0x71, 0x30, 0xd4, 0x3b, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                                       Timestamp:   uint64(9988000),
+                                       BlockHeight: uint64(1000000),
+                                       Position:    uint32(9),
+                               },
+                       },
+               },
+       }
+
+       for i, c := range cases {
+               testDB := dbm.NewDB("testdb", "leveldb", "temp")
+               walletStore := NewWalletStore(testDB)
+               ws := walletStore.InitBatch()
+               // store txs by given height
+               for _, tx := range c.txs {
+                       if err := ws.SetTransaction(c.height, tx); err != nil {
+                               t.Fatal(err)
+                       }
+               }
+
+               if err := ws.CommitBatch(); err != nil {
+                       t.Fatal(err)
+               }
+
+               // delete all txs
+               ws = walletStore.InitBatch()
+               ws.DeleteWalletTransactions()
+               if err := ws.CommitBatch(); err != nil {
+                       t.Fatal(err)
+               }
+
+               // get tx by txID, it should return err
+               for _, tx := range c.txs {
+                       _, err := ws.GetTransaction(tx.ID.String())
+                       if err == nil {
+                               t.Errorf("case: %v: it should return some err.", i)
+                       }
+               }
+
+               testDB.Close()
+               os.RemoveAll("temp")
+       }
+}