OSDN Git Service

add TestListControlPrograms
authorChengcheng Zhang <943420582@qq.com>
Wed, 17 Jul 2019 06:31:19 +0000 (14:31 +0800)
committerChengcheng Zhang <943420582@qq.com>
Wed, 17 Jul 2019 06:31:33 +0000 (14:31 +0800)
database/account_store_test.go

index a482a56..a610ff3 100644 (file)
@@ -1,7 +1,9 @@
 package database
 
 import (
+       "bytes"
        "os"
+       "sort"
        "testing"
 
        "github.com/vapor/common"
@@ -714,6 +716,9 @@ func TestListAccounts(t *testing.T) {
                        t.Fatal(err)
                }
 
+               sort.Sort(SortAccounts(gotAccounts))
+               sort.Sort(SortAccounts(c.want))
+
                if !testutil.DeepEqual(gotAccounts, c.want) {
                        t.Errorf("case %v: list accounts, got: %v, want: %v.", i, gotAccounts, c.want)
                }
@@ -722,3 +727,115 @@ func TestListAccounts(t *testing.T) {
                os.RemoveAll("temp")
        }
 }
+
+type SortAccounts []*acc.Account
+
+func (s SortAccounts) Len() int { return len(s) }
+func (s SortAccounts) Less(i, j int) bool {
+       return bytes.Compare([]byte(s[i].ID), []byte(s[j].ID)) < 0
+}
+func (s SortAccounts) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
+
+func TestListControlPrograms(t *testing.T) {
+       cases := []struct {
+               hashs    []bc.Hash
+               programs []*acc.CtrlProgram
+       }{
+               {
+                       hashs:    []bc.Hash{},
+                       programs: []*acc.CtrlProgram{},
+               },
+               {
+                       hashs: []bc.Hash{
+                               bc.NewHash([32]byte{}),
+                       },
+                       programs: []*acc.CtrlProgram{
+                               &acc.CtrlProgram{},
+                       },
+               },
+               {
+                       hashs: []bc.Hash{
+                               bc.NewHash([32]byte{}),
+                               bc.NewHash([32]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                               bc.NewHash([32]byte{0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                               bc.NewHash([32]byte{0x02, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                               bc.NewHash([32]byte{0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                               bc.NewHash([32]byte{0x04, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                               bc.NewHash([32]byte{0x05, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                               bc.NewHash([32]byte{0x06, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                               bc.NewHash([32]byte{0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                               bc.NewHash([32]byte{0x08, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3d, 0xe3, 0xdd, 0x80, 0x67, 0x29, 0x9a, 0x5e, 0x09, 0xf9, 0xfb, 0x2b, 0xad, 0x5f, 0x92, 0xc8, 0x69, 0xd1, 0x42, 0x39, 0x74, 0x9a, 0xd1, 0x1c}),
+                       },
+                       programs: []*acc.CtrlProgram{
+                               &acc.CtrlProgram{
+                                       AccountID: "account0",
+                               },
+                               &acc.CtrlProgram{
+                                       AccountID: "account1",
+                               },
+                               &acc.CtrlProgram{
+                                       AccountID: "account2",
+                               },
+                               &acc.CtrlProgram{
+                                       AccountID: "account3",
+                               },
+                               &acc.CtrlProgram{
+                                       AccountID: "account4",
+                               },
+                               &acc.CtrlProgram{
+                                       AccountID: "account5",
+                               },
+                               &acc.CtrlProgram{
+                                       AccountID: "account6",
+                               },
+                               &acc.CtrlProgram{
+                                       AccountID: "account7",
+                               },
+                               &acc.CtrlProgram{
+                                       AccountID: "account8",
+                               },
+                               &acc.CtrlProgram{
+                                       AccountID: "account9",
+                               },
+                       },
+               },
+       }
+
+       for i, c := range cases {
+               testDB := dbm.NewDB("testdb", "leveldb", "temp")
+               accountStore := NewAccountStore(testDB)
+               as := accountStore.InitBatch()
+               for j := 0; j < len(c.hashs); j++ {
+                       if err := as.SetControlProgram(c.hashs[j], c.programs[j]); err != nil {
+                               t.Fatal(err)
+                       }
+               }
+
+               if err := as.CommitBatch(); err != nil {
+                       t.Fatal(err)
+               }
+
+               gotPrograms, err := as.ListControlPrograms()
+               if err != nil {
+                       t.Fatal(err)
+               }
+
+               sort.Sort(SortPrograms(gotPrograms))
+               sort.Sort(SortPrograms(c.programs))
+
+               if !testutil.DeepEqual(gotPrograms, c.programs) {
+                       t.Errorf("case %v: list control programs, got: %v, want: %v.", i, gotPrograms, c.programs)
+               }
+
+               testDB.Close()
+               os.RemoveAll("temp")
+       }
+}
+
+type SortPrograms []*acc.CtrlProgram
+
+func (s SortPrograms) Len() int { return len(s) }
+func (s SortPrograms) Less(i, j int) bool {
+       return bytes.Compare([]byte(s[i].AccountID), []byte(s[j].AccountID)) < 0
+}
+func (s SortPrograms) Swap(i, j int) { s[i], s[j] = s[j], s[i] }