refactor(mint): CLI tests using Tendermint Mock (#13059)
* wip: mint CLI tests using Tendermint Mock * try fix test * remove query test * add cli tests Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
This commit is contained in:
co-authored by
Aleksandr Bezobchuk
parent
0eacc88096
commit
3e3b225a5e
@@ -1,7 +1,7 @@
|
||||
//go:build e2e
|
||||
// +build e2e
|
||||
|
||||
package testutil
|
||||
package mint
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@@ -10,11 +10,10 @@ import (
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/simapp"
|
||||
"github.com/cosmos/cosmos-sdk/testutil/network"
|
||||
clienttestutil "github.com/cosmos/cosmos-sdk/x/mint/client/testutil"
|
||||
)
|
||||
|
||||
func TestIntegrationTestSuite(t *testing.T) {
|
||||
cfg := network.DefaultConfig(simapp.NewTestNetworkFixture)
|
||||
cfg.NumValidators = 1
|
||||
suite.Run(t, clienttestutil.NewIntegrationTestSuite(cfg))
|
||||
suite.Run(t, NewIntegrationTestSuite(cfg))
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package mint
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"cosmossdk.io/math"
|
||||
"github.com/cosmos/cosmos-sdk/testutil"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
|
||||
|
||||
"github.com/gogo/protobuf/proto"
|
||||
|
||||
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
|
||||
)
|
||||
|
||||
func (s *IntegrationTestSuite) TestQueryGRPC() {
|
||||
val := s.network.Validators[0]
|
||||
baseURL := val.APIAddress
|
||||
testCases := []struct {
|
||||
name string
|
||||
url string
|
||||
headers map[string]string
|
||||
respType proto.Message
|
||||
expected proto.Message
|
||||
}{
|
||||
{
|
||||
"gRPC request params",
|
||||
fmt.Sprintf("%s/cosmos/mint/v1beta1/params", baseURL),
|
||||
map[string]string{},
|
||||
&minttypes.QueryParamsResponse{},
|
||||
&minttypes.QueryParamsResponse{
|
||||
Params: minttypes.NewParams("stake", sdk.NewDecWithPrec(13, 2), sdk.NewDecWithPrec(100, 2),
|
||||
math.LegacyNewDec(1), sdk.NewDecWithPrec(67, 2), (60 * 60 * 8766 / 5)),
|
||||
},
|
||||
},
|
||||
{
|
||||
"gRPC request inflation",
|
||||
fmt.Sprintf("%s/cosmos/mint/v1beta1/inflation", baseURL),
|
||||
map[string]string{},
|
||||
&minttypes.QueryInflationResponse{},
|
||||
&minttypes.QueryInflationResponse{
|
||||
Inflation: math.LegacyNewDec(1),
|
||||
},
|
||||
},
|
||||
{
|
||||
"gRPC request annual provisions",
|
||||
fmt.Sprintf("%s/cosmos/mint/v1beta1/annual_provisions", baseURL),
|
||||
map[string]string{
|
||||
grpctypes.GRPCBlockHeightHeader: "1",
|
||||
},
|
||||
&minttypes.QueryAnnualProvisionsResponse{},
|
||||
&minttypes.QueryAnnualProvisionsResponse{
|
||||
AnnualProvisions: math.LegacyNewDec(500000000),
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers)
|
||||
s.Run(tc.name, func() {
|
||||
s.Require().NoError(err)
|
||||
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
|
||||
s.Require().Equal(tc.expected.String(), tc.respType.String())
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
package mint
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
tmcli "github.com/tendermint/tendermint/libs/cli"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
|
||||
"github.com/cosmos/cosmos-sdk/testutil/network"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/mint/client/cli"
|
||||
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
|
||||
)
|
||||
|
||||
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")
|
||||
|
||||
genesisState := s.cfg.GenesisState
|
||||
|
||||
var mintData minttypes.GenesisState
|
||||
s.Require().NoError(s.cfg.Codec.UnmarshalJSON(genesisState[minttypes.ModuleName], &mintData))
|
||||
|
||||
inflation := sdk.MustNewDecFromStr("1.0")
|
||||
mintData.Minter.Inflation = inflation
|
||||
mintData.Params.InflationMin = inflation
|
||||
mintData.Params.InflationMax = inflation
|
||||
|
||||
mintDataBz, err := s.cfg.Codec.MarshalJSON(&mintData)
|
||||
s.Require().NoError(err)
|
||||
genesisState[minttypes.ModuleName] = mintDataBz
|
||||
s.cfg.GenesisState = genesisState
|
||||
|
||||
s.network, err = network.New(s.T(), s.T().TempDir(), s.cfg)
|
||||
s.Require().NoError(err)
|
||||
|
||||
_, err = s.network.WaitForHeight(1)
|
||||
s.Require().NoError(err)
|
||||
}
|
||||
|
||||
func (s *IntegrationTestSuite) TearDownSuite() {
|
||||
s.T().Log("tearing down integration test suite")
|
||||
s.network.Cleanup()
|
||||
}
|
||||
|
||||
func (s *IntegrationTestSuite) TestGetCmdQueryParams() {
|
||||
val := s.network.Validators[0]
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
args []string
|
||||
expectedOutput string
|
||||
}{
|
||||
{
|
||||
"json output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
|
||||
`{"mint_denom":"stake","inflation_rate_change":"0.130000000000000000","inflation_max":"1.000000000000000000","inflation_min":"1.000000000000000000","goal_bonded":"0.670000000000000000","blocks_per_year":"6311520"}`,
|
||||
},
|
||||
{
|
||||
"text output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", tmcli.OutputFlag)},
|
||||
`blocks_per_year: "6311520"
|
||||
goal_bonded: "0.670000000000000000"
|
||||
inflation_max: "1.000000000000000000"
|
||||
inflation_min: "1.000000000000000000"
|
||||
inflation_rate_change: "0.130000000000000000"
|
||||
mint_denom: stake`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
|
||||
s.Run(tc.name, func() {
|
||||
cmd := cli.GetCmdQueryParams()
|
||||
clientCtx := val.ClientCtx
|
||||
|
||||
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *IntegrationTestSuite) TestGetCmdQueryInflation() {
|
||||
val := s.network.Validators[0]
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
args []string
|
||||
expectedOutput string
|
||||
}{
|
||||
{
|
||||
"json output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
|
||||
`1.000000000000000000`,
|
||||
},
|
||||
{
|
||||
"text output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", tmcli.OutputFlag)},
|
||||
`1.000000000000000000`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
|
||||
s.Run(tc.name, func() {
|
||||
cmd := cli.GetCmdQueryInflation()
|
||||
clientCtx := val.ClientCtx
|
||||
|
||||
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *IntegrationTestSuite) TestGetCmdQueryAnnualProvisions() {
|
||||
val := s.network.Validators[0]
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
args []string
|
||||
expectedOutput string
|
||||
}{
|
||||
{
|
||||
"json output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
|
||||
`500000000.000000000000000000`,
|
||||
},
|
||||
{
|
||||
"text output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", tmcli.OutputFlag)},
|
||||
`500000000.000000000000000000`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
|
||||
s.Run(tc.name, func() {
|
||||
cmd := cli.GetCmdQueryAnnualProvisions()
|
||||
clientCtx := val.ClientCtx
|
||||
|
||||
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user