-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
app_test.go
86 lines (72 loc) · 2.13 KB
/
app_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
package mock
import (
"math/rand"
"testing"
"time"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tendermint/tendermint/types"
)
// TestInitApp makes sure we can initialize this thing without an error
func TestInitApp(t *testing.T) {
// set up an app
app, closer, err := SetupApp()
// closer may need to be run, even when error in later stage
if closer != nil {
defer closer()
}
require.NoError(t, err)
// initialize it future-way
appState, err := AppGenState(nil, types.GenesisDoc{}, nil)
require.NoError(t, err)
// TODO test validators in the init chain?
req := abci.RequestInitChain{
AppStateBytes: appState,
}
app.InitChain(req)
app.Commit()
// make sure we can query these values
query := abci.RequestQuery{
Path: "/store/main/key",
Data: []byte("foo"),
}
qres := app.Query(query)
require.Equal(t, uint32(0), qres.Code, qres.Log)
require.Equal(t, []byte("bar"), qres.Value)
}
// TextDeliverTx ensures we can write a tx
func TestDeliverTx(t *testing.T) {
// set up an app
app, closer, err := SetupApp()
// closer may need to be run, even when error in later stage
if closer != nil {
defer closer()
}
require.NoError(t, err)
key := "my-special-key"
value := "top-secret-data!!"
r := rand.New(rand.NewSource(time.Now().UnixNano()))
randomAccounts := simtypes.RandomAccounts(r, 1)
tx := NewTx(key, value, randomAccounts[0].Address)
txBytes := tx.GetSignBytes()
header := tmproto.Header{
AppHash: []byte("apphash"),
Height: 1,
}
app.BeginBlock(abci.RequestBeginBlock{Header: header})
dres := app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
require.Equal(t, uint32(0), dres.Code, dres.Log)
app.EndBlock(abci.RequestEndBlock{})
cres := app.Commit()
require.NotEmpty(t, cres.Data)
// make sure we can query these values
query := abci.RequestQuery{
Path: "/store/main/key",
Data: []byte(key),
}
qres := app.Query(query)
require.Equal(t, uint32(0), qres.Code, qres.Log)
require.Equal(t, []byte(value), qres.Value)
}