OSDN Git Service

fix test failure (#302)
[bytom/bytom.git] / blockchain / account / accounts_test.go
1 package account
2
3 import (
4         "context"
5         "io/ioutil"
6         "os"
7         "testing"
8
9         dbm "github.com/tendermint/tmlibs/db"
10
11         "github.com/bytom/blockchain/txdb"
12         "github.com/bytom/crypto/ed25519/chainkd"
13         "github.com/bytom/errors"
14         "github.com/bytom/protocol"
15         "github.com/bytom/protocol/bc"
16         "github.com/bytom/testutil"
17 )
18
19 func TestCreateAccount(t *testing.T) {
20         m := mockAccountManager(t)
21         ctx := context.Background()
22
23         account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias", nil)
24         if err != nil {
25                 testutil.FatalErr(t, err)
26         }
27
28         found, err := m.findByID(ctx, account.ID)
29         if err != nil {
30                 t.Errorf("unexpected error %v", err)
31         }
32         if !testutil.DeepEqual(account, found) {
33                 t.Errorf("expected account %s to be recorded as %s", account, found)
34         }
35 }
36
37 func TestCreateAccountReusedAlias(t *testing.T) {
38         m := mockAccountManager(t)
39         ctx := context.Background()
40         m.createTestAccount(ctx, t, "test-alias", nil)
41
42         _, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias", nil)
43         if errors.Root(err) != ErrDuplicateAlias {
44                 t.Errorf("expected %s when reusing an alias, got %v", ErrDuplicateAlias, err)
45         }
46 }
47
48 func TestDeleteAccount(t *testing.T) {
49         m := mockAccountManager(t)
50         ctx := context.Background()
51
52         account1, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias1", nil)
53         if err != nil {
54                 testutil.FatalErr(t, err)
55         }
56
57         account2, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias2", nil)
58         if err != nil {
59                 testutil.FatalErr(t, err)
60         }
61
62         cases := []struct {
63                 AccountInfo string `json:"account_info"`
64         }{
65                 {AccountInfo: account1.Alias},
66                 {AccountInfo: account2.ID},
67         }
68
69         if err = m.DeleteAccount(cases[0]); err != nil {
70                 testutil.FatalErr(t, err)
71         }
72
73         found, err := m.findByID(ctx, account1.ID)
74         if err != nil {
75                 t.Errorf("expected account %v should be deleted", found)
76         }
77
78         if err = m.DeleteAccount(cases[1]); err != nil {
79                 testutil.FatalErr(t, err)
80         }
81
82         found, err = m.findByID(ctx, account2.ID)
83         if err != nil {
84                 t.Errorf("expected account %v should be deleted", found)
85         }
86 }
87
88 func TestFindByID(t *testing.T) {
89         m := mockAccountManager(t)
90         ctx := context.Background()
91         account := m.createTestAccount(ctx, t, "", nil)
92
93         found, err := m.findByID(ctx, account.ID)
94         if err != nil {
95                 testutil.FatalErr(t, err)
96         }
97
98         if !testutil.DeepEqual(account, found) {
99                 t.Errorf("expected found account to be %v, instead found %v", account, found)
100         }
101 }
102
103 func TestFindByAlias(t *testing.T) {
104         m := mockAccountManager(t)
105         ctx := context.Background()
106         account := m.createTestAccount(ctx, t, "some-alias", nil)
107
108         found, err := m.FindByAlias(ctx, "some-alias")
109         if err != nil {
110                 testutil.FatalErr(t, err)
111         }
112
113         if !testutil.DeepEqual(account, found) {
114                 t.Errorf("expected found account to be %v, instead found %v", account, found)
115         }
116 }
117
118 func mockAccountManager(t *testing.T) *Manager {
119         dirPath, err := ioutil.TempDir(".", "")
120         if err != nil {
121                 t.Fatal(err)
122         }
123         defer os.RemoveAll(dirPath)
124
125         testDB := dbm.NewDB("testdb", "leveldb", "temp")
126         defer os.RemoveAll("temp")
127
128         store := txdb.NewStore(testDB)
129         txPool := protocol.NewTxPool()
130         chain, err := protocol.NewChain(bc.Hash{}, store, txPool)
131         if err != nil {
132                 t.Fatal(err)
133         }
134
135         return NewManager(testDB, chain)
136 }
137
138 func (m *Manager) createTestAccount(ctx context.Context, t testing.TB, alias string, tags map[string]interface{}) *Account {
139         account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, alias, tags)
140         if err != nil {
141                 testutil.FatalErr(t, err)
142         }
143
144         return account
145
146 }