-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
test_helpers.go
83 lines (68 loc) · 2.14 KB
/
test_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package client
import (
"fmt"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
var (
_ AccountRetriever = TestAccountRetriever{}
_ Account = TestAccount{}
)
// TestAccount represents a client Account that can be used in unit tests
type TestAccount struct {
Address sdk.AccAddress
Num uint64
Seq uint64
}
// GetAddress implements client Account.GetAddress
func (t TestAccount) GetAddress() sdk.AccAddress {
return t.Address
}
// GetPubKey implements client Account.GetPubKey
func (t TestAccount) GetPubKey() cryptotypes.PubKey {
return nil
}
// GetAccountNumber implements client Account.GetAccountNumber
func (t TestAccount) GetAccountNumber() uint64 {
return t.Num
}
// GetSequence implements client Account.GetSequence
func (t TestAccount) GetSequence() uint64 {
return t.Seq
}
// TestAccountRetriever is an AccountRetriever that can be used in unit tests
type TestAccountRetriever struct {
Accounts map[string]TestAccount
}
// GetAccount implements AccountRetriever.GetAccount
func (t TestAccountRetriever) GetAccount(_ Context, addr sdk.AccAddress) (Account, error) {
acc, ok := t.Accounts[addr.String()]
if !ok {
return nil, fmt.Errorf("account %s not found", addr)
}
return acc, nil
}
// GetAccountWithHeight implements AccountRetriever.GetAccountWithHeight
func (t TestAccountRetriever) GetAccountWithHeight(clientCtx Context, addr sdk.AccAddress) (Account, int64, error) {
acc, err := t.GetAccount(clientCtx, addr)
if err != nil {
return nil, 0, err
}
return acc, 0, nil
}
// EnsureExists implements AccountRetriever.EnsureExists
func (t TestAccountRetriever) EnsureExists(_ Context, addr sdk.AccAddress) error {
_, ok := t.Accounts[addr.String()]
if !ok {
return fmt.Errorf("account %s not found", addr)
}
return nil
}
// GetAccountNumberSequence implements AccountRetriever.GetAccountNumberSequence
func (t TestAccountRetriever) GetAccountNumberSequence(_ Context, addr sdk.AccAddress) (accNum uint64, accSeq uint64, err error) {
acc, ok := t.Accounts[addr.String()]
if !ok {
return 0, 0, fmt.Errorf("account %s not found", addr)
}
return acc.Num, acc.Seq, nil
}