OSDN Git Service

add TestDeleteTransactions
authorChengcheng Zhang <943420582@qq.com>
Thu, 18 Jul 2019 11:25:06 +0000 (19:25 +0800)
committerChengcheng Zhang <943420582@qq.com>
Thu, 18 Jul 2019 11:25:06 +0000 (19:25 +0800)
database/account_store.go
database/wallet_store_test.go

index 3a00566..d050f49 100644 (file)
@@ -314,7 +314,7 @@ func (store *AccountStore) ListControlPrograms() ([]*acc.CtrlProgram, error) {
        return cps, nil
 }
 
-// ListUTXOs list all utxos includling 
+// ListUTXOs list all utxos
 func (store *AccountStore) ListUTXOs() ([]*acc.UTXO, error) {
        utxoIter := store.db.IteratorPrefix(UTXOPrefix)
        defer utxoIter.Release()
index f1a2753..fc2ce0a 100644 (file)
@@ -598,3 +598,69 @@ func TestGetAsset(t *testing.T) {
                os.RemoveAll("temp")
        }
 }
+
+func TestDeleteTransactions(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}),
+                               },
+                       },
+               },
+       }
+
+       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)
+               }
+
+               // get tx by txID
+               for j := 0; j < len(c.txs); j++ {
+                       gotTx, err := ws.GetTransaction(c.txs[j].ID.String())
+                       if err != nil {
+                               t.Fatal(err)
+                       }
+
+                       if !testutil.DeepEqual(gotTx, c.txs[j]) {
+                               t.Errorf("case %v: got transaction, got: %v, want: %v.", i, gotTx, c.txs[j])
+                       }
+               }
+
+               // delete all txs by given height
+               ws = walletStore.InitBatch()
+               ws.DeleteTransactions(c.height)
+               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")
+       }
+}