forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_test.go
187 lines (156 loc) · 4.67 KB
/
utils_test.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package utils
import (
"encoding/json"
"errors"
"io/ioutil"
"os"
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/libs/common"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)
var (
priv = ed25519.GenPrivKey()
addr = sdk.AccAddress(priv.PubKey().Address())
)
func TestParseQueryResponse(t *testing.T) {
cdc := makeCodec()
sdkResBytes := cdc.MustMarshalBinaryLengthPrefixed(sdk.Result{GasUsed: 10})
gas, err := parseQueryResponse(cdc, sdkResBytes)
assert.Equal(t, gas, uint64(10))
assert.Nil(t, err)
gas, err = parseQueryResponse(cdc, []byte("fuzzy"))
assert.Equal(t, gas, uint64(0))
assert.Error(t, err)
}
func TestCalculateGas(t *testing.T) {
cdc := makeCodec()
makeQueryFunc := func(gasUsed uint64, wantErr bool) func(string, common.HexBytes) ([]byte, error) {
return func(string, common.HexBytes) ([]byte, error) {
if wantErr {
return nil, errors.New("")
}
return cdc.MustMarshalBinaryLengthPrefixed(sdk.Result{GasUsed: gasUsed}), nil
}
}
type args struct {
queryFuncGasUsed uint64
queryFuncWantErr bool
adjustment float64
}
tests := []struct {
name string
args args
wantEstimate uint64
wantAdjusted uint64
wantErr bool
}{
{"error", args{0, true, 1.2}, 0, 0, true},
{"adjusted gas", args{10, false, 1.2}, 10, 12, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
queryFunc := makeQueryFunc(tt.args.queryFuncGasUsed, tt.args.queryFuncWantErr)
gotEstimate, gotAdjusted, err := CalculateGas(queryFunc, cdc, []byte(""), tt.args.adjustment)
assert.Equal(t, err != nil, tt.wantErr)
assert.Equal(t, gotEstimate, tt.wantEstimate)
assert.Equal(t, gotAdjusted, tt.wantAdjusted)
})
}
}
func TestDefaultTxEncoder(t *testing.T) {
cdc := makeCodec()
defaultEncoder := authtypes.DefaultTxEncoder(cdc)
encoder := GetTxEncoder(cdc)
compareEncoders(t, defaultEncoder, encoder)
}
func TestConfiguredTxEncoder(t *testing.T) {
cdc := makeCodec()
customEncoder := func(tx sdk.Tx) ([]byte, error) {
return json.Marshal(tx)
}
config := sdk.GetConfig()
config.SetTxEncoder(customEncoder)
encoder := GetTxEncoder(cdc)
compareEncoders(t, customEncoder, encoder)
}
func TestReadStdTxFromFile(t *testing.T) {
cdc := codec.New()
sdk.RegisterCodec(cdc)
// Build a test transaction
fee := authtypes.NewStdFee(50000, sdk.Coins{sdk.NewInt64Coin("atom", 150)})
stdTx := authtypes.NewStdTx([]sdk.Msg{}, fee, []authtypes.StdSignature{}, "foomemo", nil)
// Write it to the file
encodedTx, _ := cdc.MarshalJSON(stdTx)
jsonTxFile := writeToNewTempFile(t, string(encodedTx))
defer os.Remove(jsonTxFile.Name())
// Read it back
decodedTx, err := ReadStdTxFromFile(cdc, jsonTxFile.Name())
require.NoError(t, err)
require.Equal(t, decodedTx.Memo, "foomemo")
}
func TestValidateCmd(t *testing.T) {
// Setup root and subcommands
rootCmd := &cobra.Command{
Use: "root",
}
queryCmd := &cobra.Command{
Use: "query",
}
rootCmd.AddCommand(queryCmd)
// Command being tested
distCmd := &cobra.Command{
Use: "distr",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
}
queryCmd.AddCommand(distCmd)
commissionCmd := &cobra.Command{
Use: "commission",
}
distCmd.AddCommand(commissionCmd)
tests := []struct {
reason string
args []string
wantErr bool
}{
{"misspelled command", []string{"comission"}, true},
{"no command provided", []string{}, false},
{"help flag", []string{"comission", "--help"}, false},
{"shorthand help flag", []string{"comission", "-h"}, false},
}
for _, tt := range tests {
err := ValidateCmd(distCmd, tt.args)
assert.Equal(t, tt.wantErr, err != nil, tt.reason)
}
}
// aux functions
func compareEncoders(t *testing.T, expected sdk.TxEncoder, actual sdk.TxEncoder) {
msgs := []sdk.Msg{sdk.NewTestMsg(addr)}
tx := authtypes.NewStdTx(msgs, authtypes.StdFee{}, []authtypes.StdSignature{}, "", nil)
defaultEncoderBytes, err := expected(tx)
require.NoError(t, err)
encoderBytes, err := actual(tx)
require.NoError(t, err)
require.Equal(t, defaultEncoderBytes, encoderBytes)
}
func writeToNewTempFile(t *testing.T, data string) *os.File {
fp, err := ioutil.TempFile(os.TempDir(), "client_tx_test")
require.NoError(t, err)
_, err = fp.WriteString(data)
require.NoError(t, err)
return fp
}
func makeCodec() *codec.Codec {
var cdc = codec.New()
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
authtypes.RegisterCodec(cdc)
cdc.RegisterConcrete(sdk.TestMsg{}, "cosmos-sdk/Test", nil)
return cdc
}