OSDN Git Service

add TestDeleteStandardUTXO
authorChengcheng Zhang <943420582@qq.com>
Tue, 16 Jul 2019 07:59:24 +0000 (15:59 +0800)
committerChengcheng Zhang <943420582@qq.com>
Tue, 16 Jul 2019 07:59:24 +0000 (15:59 +0800)
database/account_store_test.go

index 44b3c02..c6c276f 100644 (file)
@@ -6,6 +6,7 @@ import (
 
        acc "github.com/vapor/account"
        dbm "github.com/vapor/database/leveldb"
+       "github.com/vapor/protocol/bc"
 )
 
 func TestDeleteAccount(t *testing.T) {
@@ -106,8 +107,8 @@ func TestDeleteAccount(t *testing.T) {
                },
        }
 
+       accountStore := NewAccountStore(testDB)
        for i, c := range cases {
-               accountStore := NewAccountStore(testDB)
                as := accountStore.InitBatch()
                // store mock accounts
                for _, a := range c.accounts {
@@ -131,50 +132,118 @@ func TestDeleteAccount(t *testing.T) {
                }
 
                for _, a := range c.want {
-                       if _, err := as.GetAccountByID(a.ID); err == acc.ErrFindAccount {
+                       if _, err := as.GetAccountByID(a.ID); err != nil {
                                t.Errorf("case %v: cann't find account, err: %v", i, err)
                        }
 
-                       if _, err := as.GetAccountByAlias(a.Alias); err == acc.ErrFindAccount {
+                       if _, err := as.GetAccountByAlias(a.Alias); err != nil {
                                t.Errorf("case %v: cann't find account, err: %v", i, err)
                        }
                }
        }
 }
 
-// func TestDeleteStandardUTXO(t *testing.T) {
-//     testDB := dbm.NewDB("testdb", "leveldb", "temp")
-//     defer func() {
-//             testDB.Close()
-//             os.RemoveAll("temp")
-//     }()
+func TestDeleteStandardUTXO(t *testing.T) {
+       testDB := dbm.NewDB("testdb", "leveldb", "temp")
+       defer func() {
+               testDB.Close()
+               os.RemoveAll("temp")
+       }()
 
-//     cases := []struct {
-//             utxos      []*acc.UTXO
-//             deleteUTXO *acc.UTXO
-//             want       []*acc.UTXO
-//     }{
-//             {
-//                     utxos:      []*acc.UTXO{},
-//                     deleteUTXO: &acc.UTXO{},
-//                     want:       []*acc.UTXO{},
-//             },
-//             {
-//                     utxos: []*acc.UTXO{
-//                             &acc.UTXO{
-//                                     OutputID: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 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}),
-//                             },
-//                     },
-//                     deleteUTXO: &acc.UTXO{
-//                             OutputID: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 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}),
-//                     },
-//                     want: []*acc.UTXO{},
-//             },
-//     }
+       cases := []struct {
+               utxos      []*acc.UTXO
+               deleteUTXO *acc.UTXO
+               want       []*acc.UTXO
+       }{
+               {
+                       utxos:      []*acc.UTXO{},
+                       deleteUTXO: &acc.UTXO{},
+                       want:       []*acc.UTXO{},
+               },
+               {
+                       utxos: []*acc.UTXO{
+                               &acc.UTXO{
+                                       OutputID: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 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}),
+                               },
+                       },
+                       deleteUTXO: &acc.UTXO{
+                               OutputID: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 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}),
+                       },
+                       want: []*acc.UTXO{},
+               },
+               {
+                       utxos: []*acc.UTXO{
+                               &acc.UTXO{
+                                       OutputID: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 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}),
+                               },
+                       },
+                       deleteUTXO: &acc.UTXO{},
+                       want: []*acc.UTXO{
+                               &acc.UTXO{
+                                       OutputID: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 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}),
+                               },
+                       },
+               },
+               {
+                       utxos: []*acc.UTXO{},
+                       deleteUTXO: &acc.UTXO{
+                               OutputID: bc.NewHash([32]byte{0x3e, 0x94, 0x5d, 0x35, 0x70, 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}),
+                       },
+                       want: []*acc.UTXO{},
+               },
+               {
+                       utxos: []*acc.UTXO{
+                               &acc.UTXO{
+                                       OutputID: bc.NewHash([32]byte{0x0e, 0x04, 0x50, 0x35, 0x70, 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}),
+                               },
+                               &acc.UTXO{
+                                       OutputID: bc.NewHash([32]byte{0x00, 0x01, 0x02, 0x35, 0x70, 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}),
+                               },
+                               &acc.UTXO{
+                                       OutputID: 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}),
+                               },
+                               &acc.UTXO{
+                                       OutputID: bc.NewHash([32]byte{0x01, 0x01, 0x02, 0x39, 0x70, 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}),
+                               },
+                       },
+                       deleteUTXO: &acc.UTXO{
+                               OutputID: bc.NewHash([32]byte{0x01, 0x01, 0x02, 0x39, 0x70, 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}),
+                       },
+                       want: []*acc.UTXO{
+                               &acc.UTXO{
+                                       OutputID: bc.NewHash([32]byte{0x0e, 0x04, 0x50, 0x35, 0x70, 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}),
+                               },
+                               &acc.UTXO{
+                                       OutputID: bc.NewHash([32]byte{0x00, 0x01, 0x02, 0x35, 0x70, 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}),
+                               },
+                               &acc.UTXO{
+                                       OutputID: 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}),
+                               },
+                       },
+               },
+       }
+
+       accountStore := NewAccountStore(testDB)
+       for _, c := range cases {
+               as := accountStore.InitBatch()
+               // store mock utxos
+               for _, utxo := range c.utxos {
+                       if err := as.SetStandardUTXO(utxo.OutputID, utxo); err != nil {
+                               t.Fatal(err)
+                       }
+               }
 
-//     accountStore := NewAccountStore(testDB)
+               // delete utxo
+               as.DeleteStandardUTXO(c.deleteUTXO.OutputID)
+               if err := as.CommitBatch(); err != nil {
+                       t.Fatal(err)
+               }
 
-//     for i, c := range cases {
-//             accountStore.
-//     }
-// }
+               // get utxo by outputID
+               for _, utxo := range c.want {
+                       if _, err := as.GetUTXO(utxo.OutputID); err != nil {
+                               t.Fatal(err)
+                       }
+               }
+       }
+}