-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
suite.go
146 lines (120 loc) · 3.85 KB
/
suite.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
package testutil
import (
"fmt"
"strings"
"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
tmcli "github.com/tendermint/tendermint/libs/cli"
dbm "github.com/tendermint/tm-db"
"cosmossdk.io/depinject"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/runtime"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
pruningtypes "github.com/cosmos/cosmos-sdk/store/pruning/types"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/params/client/cli"
"github.com/cosmos/cosmos-sdk/x/params/keeper"
"github.com/cosmos/cosmos-sdk/x/params/testutil"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)
// mySubspace is a x/params subspace created for the purpose of this
// test suite.
const (
mySubspace = "foo"
)
// myParams defines some params in the `mySubspace` subspace.
type myParams struct{}
func (p *myParams) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair([]byte("bar"), 1234, func(value interface{}) error { return nil }),
}
}
type IntegrationTestSuite struct {
suite.Suite
cfg network.Config
network *network.Network
}
func NewIntegrationTestSuite(cfg network.Config) *IntegrationTestSuite {
return &IntegrationTestSuite{cfg: cfg}
}
func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")
// Create a new AppConstructor for this test suite, where we manually
// add a subspace and `myParams` to the x/params store.
s.cfg.AppConstructor = func(val moduletestutil.Validator) servertypes.Application {
var (
appBuilder *runtime.AppBuilder
paramsKeeper keeper.Keeper
)
if err := depinject.Inject(testutil.AppConfig, &appBuilder, ¶msKeeper); err != nil {
panic(err)
}
// Add this test's `myParams` to the x/params store.
paramSet := myParams{}
subspace := paramsKeeper.Subspace(mySubspace).WithKeyTable(paramtypes.NewKeyTable().RegisterParamSet(¶mSet))
app := appBuilder.Build(
val.GetCtx().Logger,
dbm.NewMemDB(),
nil,
baseapp.SetPruning(pruningtypes.NewPruningOptionsFromString(val.GetAppConfig().Pruning)),
baseapp.SetMinGasPrices(val.GetAppConfig().MinGasPrices),
)
s.Require().NoError(app.Load(false))
// Make sure not to forget to persist `myParams` into the actual store,
// this is done in InitChain.
app.SetInitChainer(func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
subspace.SetParamSet(ctx, ¶mSet)
return app.InitChainer(ctx, req)
})
s.Require().NoError(app.LoadLatestVersion())
return app
}
var err error
s.network, err = network.New(s.T(), s.T().TempDir(), s.cfg)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
}
func (s *IntegrationTestSuite) TearDownSuite() {
s.T().Log("tearing down integration test suite")
s.network.Cleanup()
}
func (s *IntegrationTestSuite) TestNewQuerySubspaceParamsCmd() {
val := s.network.Validators[0]
testCases := []struct {
name string
args []string
expectedOutput string
}{
{
"json output",
[]string{
"foo", "bar",
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
`{"subspace":"foo","key":"bar","value":"\"1234\""}`,
},
{
"text output",
[]string{
"foo", "bar",
fmt.Sprintf("--%s=text", tmcli.OutputFlag),
},
`key: bar
subspace: foo
value: '"1234"'`,
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
cmd := cli.NewQuerySubspaceParamsCmd()
clientCtx := val.ClientCtx
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
s.Require().NoError(err)
s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
})
}
}