Skip to content

Commit

Permalink
add suite extension for the cli tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani authored and Pantani committed Oct 26, 2023
1 parent f643ccd commit 68af5e2
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 205 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cli_test

import (
"testing"
"time"

"github.com/stretchr/testify/suite"

"<%= modulePath %>/testutil/network"
)

type IntegrationTestSuite struct {
suite.Suite

network *network.Network
}

func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")
s.network = network.New(s.T(), network.DefaultConfig())
h, err := s.network.WaitForHeight(1)
s.Require().NoError(err, "stalled at height %d", h)
}

func (s *IntegrationTestSuite) TearDownTest() {
s.T().Log("tearing down test")
s.network.Config = network.DefaultConfig()
}

func (s *IntegrationTestSuite) TearDownSuite() {
s.T().Log("tearing down integration test suite")
s.network.Cleanup()
}

func (s *IntegrationTestSuite) TestNetwork_Liveness() {
h, err := s.network.WaitForHeightWithTimeout(10, time.Minute)
s.Require().NoError(err, "expected to reach 10 blocks; got %d", h)
}

func TestIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func New(t *testing.T, configs ...Config) *Network {
require.NoError(t, err)
_, err = net.WaitForHeight(1)
require.NoError(t, err)
t.Cleanup(net.Cleanup)
return net
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"<%= ModulePath %>/testutil/network"
"<%= ModulePath %>/testutil/nullify"
"<%= ModulePath %>/x/<%= ModuleName %>/client/cli"
"<%= ModulePath %>/x/<%= ModuleName %>/types"
)

func networkWith<%= TypeName.UpperCamel %>Objects(t *testing.T, n int) (*network.Network, []types.<%= TypeName.UpperCamel %>) {
t.Helper()
cfg := network.DefaultConfig()
func (s *IntegrationTestSuite) networkWith<%= TypeName.UpperCamel %>Objects(n int) []types.<%= TypeName.UpperCamel %> {
s.T().Helper()
state := types.GenesisState{<%= if (IsIBC) { %>PortId: types.PortID<% } %>}
for i := 0; i < n; i++ {
<%= TypeName.LowerCamel %> := types.<%= TypeName.UpperCamel %>{
Expand All @@ -28,19 +26,20 @@ func networkWith<%= TypeName.UpperCamel %>Objects(t *testing.T, n int) (*network
nullify.Fill(&<%= TypeName.LowerCamel %>)
state.<%= TypeName.UpperCamel %>List = append(state.<%= TypeName.UpperCamel %>List, <%= TypeName.LowerCamel %>)
}
buf, err := cfg.Codec.MarshalJSON(&state)
require.NoError(t, err)
cfg.GenesisState[types.ModuleName] = buf
return network.New(t, cfg), state.<%= TypeName.UpperCamel %>List
buf, err := s.network.Config.Codec.MarshalJSON(&state)
s.Require().NoError(err)
s.network.Config.GenesisState[types.ModuleName] = buf
return state.<%= TypeName.UpperCamel %>List
}

func TestShow<%= TypeName.UpperCamel %>(t *testing.T) {
net, objs := networkWith<%= TypeName.UpperCamel %>Objects(t, 2)

ctx := net.Validators[0].ClientCtx
common := []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
}
func (s *IntegrationTestSuite) TestShow<%= TypeName.UpperCamel %>(t *testing.T) {
var (
objs = s.networkWith<%= TypeName.UpperCamel %>Objects(2)
ctx = s.network.Validators[0].ClientCtx
common = []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
}
)
tests := []struct {
desc string
id string
Expand All @@ -62,32 +61,32 @@ func TestShow<%= TypeName.UpperCamel %>(t *testing.T) {
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
args := []string{tc.id}
args = append(args, tc.args...)
s.T().Run(tc.desc, func(t *testing.T) {
args := append([]string{tc.id}, tc.args...)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShow<%= TypeName.UpperCamel %>(), args)
if tc.err != nil {
stat, ok := status.FromError(tc.err)
require.True(t, ok)
require.ErrorIs(t, stat.Err(), tc.err)
} else {
require.NoError(t, err)
var resp types.QueryGet<%= TypeName.UpperCamel %>Response
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.<%= TypeName.UpperCamel %>)
require.Equal(t,
nullify.Fill(&tc.obj),
nullify.Fill(&resp.<%= TypeName.UpperCamel %>),
)
return
}
require.NoError(t, err)
var resp types.QueryGet<%= TypeName.UpperCamel %>Response
require.NoError(t, s.network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.<%= TypeName.UpperCamel %>)
require.Equal(t,
nullify.Fill(&tc.obj),
nullify.Fill(&resp.<%= TypeName.UpperCamel %>),
)
})
}
}

func TestList<%= TypeName.UpperCamel %>(t *testing.T) {
net, objs := networkWith<%= TypeName.UpperCamel %>Objects(t, 5)

ctx := net.Validators[0].ClientCtx
func (s *IntegrationTestSuite) TestList<%= TypeName.UpperCamel %>(t *testing.T) {
var (
objs = s.networkWith<%= TypeName.UpperCamel %>Objects(5)
ctx = s.network.Validators[0].ClientCtx
)
request := func(next []byte, offset, limit uint64, total bool) []string {
args := []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
Expand All @@ -103,30 +102,30 @@ func TestList<%= TypeName.UpperCamel %>(t *testing.T) {
}
return args
}
t.Run("ByOffset", func(t *testing.T) {
s.T().Run("ByOffset", func(t *testing.T) {
step := 2
for i := 0; i < len(objs); i += step {
args := request(nil, uint64(i), uint64(step), false)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdList<%= TypeName.UpperCamel %>(), args)
require.NoError(t, err)
var resp types.QueryAll<%= TypeName.UpperCamel %>Response
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NoError(t, s.network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step)
require.Subset(t,
nullify.Fill(objs),
nullify.Fill(resp.<%= TypeName.UpperCamel %>),
)
}
})
t.Run("ByKey", func(t *testing.T) {
s.T().Run("ByKey", func(t *testing.T) {
step := 2
var next []byte
for i := 0; i < len(objs); i += step {
args := request(next, 0, uint64(step), false)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdList<%= TypeName.UpperCamel %>(), args)
require.NoError(t, err)
var resp types.QueryAll<%= TypeName.UpperCamel %>Response
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NoError(t, s.network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step)
require.Subset(t,
nullify.Fill(objs),
Expand All @@ -135,12 +134,12 @@ func TestList<%= TypeName.UpperCamel %>(t *testing.T) {
next = resp.Pagination.NextKey
}
})
t.Run("Total", func(t *testing.T) {
s.T().Run("Total", func(t *testing.T) {
args := request(nil, 0, uint64(len(objs)), true)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdList<%= TypeName.UpperCamel %>(), args)
require.NoError(t, err)
var resp types.QueryAll<%= TypeName.UpperCamel %>Response
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NoError(t, s.network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NoError(t, err)
require.Equal(t, len(objs), int(resp.Pagination.Total))
require.ElementsMatch(t,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/stretchr/testify/require"

"<%= ModulePath %>/testutil/network"
"<%= ModulePath %>/x/<%= ModuleName %>/client/cli"
)

func TestCreate<%= TypeName.UpperCamel %>(t *testing.T) {
net := network.New(t)
val := net.Validators[0]
ctx := val.ClientCtx
func (s *IntegrationTestSuite) TestCreate<%= TypeName.UpperCamel %>() {
var (
val = s.network.Validators[0]
ctx = val.ClientCtx
)

fields := []string{<%= for (field) in Fields { %> "<%= field.DefaultTestValue() %>", <% } %>}
tests := []struct {
Expand All @@ -34,16 +34,15 @@ func TestCreate<%= TypeName.UpperCamel %>(t *testing.T) {
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(net.Config.BondDenom, sdkmath.NewInt(10))).String()),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.network.Config.BondDenom, sdkmath.NewInt(10))).String()),
},
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
require.NoError(t, net.WaitForNextBlock())
s.T().Run(tc.desc, func(t *testing.T) {
require.NoError(t, s.network.WaitForNextBlock())

args := []string{}
args = append(args, fields...)
args := append([]string{}, fields...)
args = append(args, tc.args...)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdCreate<%= TypeName.UpperCamel %>(), args)
if tc.err != nil {
Expand All @@ -54,29 +53,28 @@ func TestCreate<%= TypeName.UpperCamel %>(t *testing.T) {

var resp sdk.TxResponse
require.NoError(t, ctx.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NoError(t, clitestutil.CheckTxCode(net, ctx, resp.TxHash, tc.code))
require.NoError(t, clitestutil.CheckTxCode(s.network, ctx, resp.TxHash, tc.code))
})
}
}

func TestUpdate<%= TypeName.UpperCamel %>(t *testing.T) {
net := network.New(t)

val := net.Validators[0]
ctx := val.ClientCtx
func (s *IntegrationTestSuite) TestUpdate<%= TypeName.UpperCamel %>() {
var (
val = s.network.Validators[0]
ctx = val.ClientCtx
)

fields := []string{<%= for (field) in Fields { %> "<%= field.DefaultTestValue() %>", <% } %>}
common := []string{
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(net.Config.BondDenom, sdkmath.NewInt(10))).String()),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.network.Config.BondDenom, sdkmath.NewInt(10))).String()),
}
args := []string{}
args = append(args, fields...)
args := append([]string{}, fields...)
args = append(args, common...)
_, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdCreate<%= TypeName.UpperCamel %>(), args)
require.NoError(t, err)
s.Require().NoError(err)

tests := []struct {
desc string
Expand All @@ -103,11 +101,10 @@ func TestUpdate<%= TypeName.UpperCamel %>(t *testing.T) {
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
require.NoError(t, net.WaitForNextBlock())
s.T().Run(tc.desc, func(t *testing.T) {
require.NoError(t, s.network.WaitForNextBlock())

args := []string{tc.id}
args = append(args, fields...)
args := append([]string{}, fields...)
args = append(args, tc.args...)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdUpdate<%= TypeName.UpperCamel %>(), args)
if tc.err != nil {
Expand All @@ -118,29 +115,28 @@ func TestUpdate<%= TypeName.UpperCamel %>(t *testing.T) {

var resp sdk.TxResponse
require.NoError(t, ctx.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NoError(t, clitestutil.CheckTxCode(net, ctx, resp.TxHash, tc.code))
require.NoError(t, clitestutil.CheckTxCode(s.network, ctx, resp.TxHash, tc.code))
})
}
}

func TestDelete<%= TypeName.UpperCamel %>(t *testing.T) {
net := network.New(t)

val := net.Validators[0]
ctx := val.ClientCtx
func (s *IntegrationTestSuite) TestDelete<%= TypeName.UpperCamel %>() {
var (
val = s.network.Validators[0]
ctx = val.ClientCtx
)

fields := []string{<%= for (field) in Fields { %> "<%= field.DefaultTestValue() %>", <% } %>}
common := []string{
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(net.Config.BondDenom, sdkmath.NewInt(10))).String()),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.network.Config.BondDenom, sdkmath.NewInt(10))).String()),
}
args := []string{}
args = append(args, fields...)
args := append([]string{}, fields...)
args = append(args, common...)
_, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdCreate<%= TypeName.UpperCamel %>(), args)
require.NoError(t, err)
s.Require().NoError(err)

tests := []struct {
desc string
Expand All @@ -167,8 +163,8 @@ func TestDelete<%= TypeName.UpperCamel %>(t *testing.T) {
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
require.NoError(t, net.WaitForNextBlock())
s.T().Run(tc.desc, func(t *testing.T) {
require.NoError(t, s.network.WaitForNextBlock())

out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdDelete<%= TypeName.UpperCamel %>(), append([]string{tc.id}, tc.args...))
if tc.err != nil {
Expand All @@ -179,7 +175,7 @@ func TestDelete<%= TypeName.UpperCamel %>(t *testing.T) {

var resp sdk.TxResponse
require.NoError(t, ctx.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NoError(t, clitestutil.CheckTxCode(net, ctx, resp.TxHash, tc.code))
require.NoError(t, clitestutil.CheckTxCode(s.network, ctx, resp.TxHash, tc.code))
})
}
}
Loading

0 comments on commit 68af5e2

Please sign in to comment.