Skip to content

Commit

Permalink
feat: add test suite for the cli tests (#3715)
Browse files Browse the repository at this point in the history
* add suite extension for the cli tests

* fix test lock for network

* fix missing vars in template

* add a flag to check if the network mutex is locked

* reset the config for each test in the cli pkg

* fix test suite parameter for CLI test

* wait for the next block to execute the tests

* update changelog

---------

Co-authored-by: Pantani <Pantani>
Co-authored-by: Jerónimo Albi <[email protected]>
  • Loading branch information
Pantani and jeronimoalbi committed Nov 6, 2023
1 parent 897b1cc commit 4a94461
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 205 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [#3614](https://github.com/ignite/cli/pull/3614) feat: use DefaultBaseappOptions for app.New method
- [#3536](https://github.com/ignite/cli/pull/3536) Change app.go to v2 and add AppWiring feature
- [#3670](https://github.com/ignite/cli/pull/3670) Remove nodetime binaries
- [#3715](https://github.com/ignite/cli/pull/3715) Add test suite for the cli tests

### Changes

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cli_test

import (
"testing"

"github.com/stretchr/testify/suite"

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

type IntegrationTestSuite struct {
suite.Suite

locked bool
net *network.Network
cfg network.Config
}

func (s *IntegrationTestSuite) network() *network.Network {
s.net = network.New(s.T(), s.cfg)
s.locked = true
return s.net
}

func (s *IntegrationTestSuite) waitForNextBlock() {
s.T().Log("wait for next block")
s.Require().NoError(s.net.WaitForNextBlock())
}

func (s *IntegrationTestSuite) SetupTest() {
s.T().Log("setting up test")
s.cfg = network.DefaultConfig()
}

func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")
s.cfg = network.DefaultConfig()
}

func (s *IntegrationTestSuite) TearDownTest() {
s.T().Log("tearing down test")
if s.net != nil && s.locked {
s.net.Cleanup()
s.locked = false
}
}

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

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,21 @@ 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.cfg.Codec.MarshalJSON(&state)
s.Require().NoError(err)
s.cfg.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 %>() {
var (
objs = s.networkWith<%= TypeName.UpperCamel %>Objects(2)
net = s.network()
ctx = net.Validators[0].ClientCtx
common = []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
}
)
tests := []struct {
desc string
id string
Expand All @@ -62,32 +62,33 @@ 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.cfg.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 %>() {
var (
objs = s.networkWith<%= TypeName.UpperCamel %>Objects(5)
net = s.network()
ctx = net.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 +104,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.cfg.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.cfg.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step)
require.Subset(t,
nullify.Fill(objs),
Expand All @@ -135,12 +136,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.cfg.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,15 @@ 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 (
net = s.network()
val = net.Validators[0]
ctx = val.ClientCtx
)

fields := []string{<%= for (field) in Fields { %> "<%= field.DefaultTestValue() %>", <% } %>}
tests := []struct {
Expand All @@ -34,16 +35,13 @@ 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.cfg.BondDenom, sdkmath.NewInt(10))).String()),
},
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
require.NoError(t, net.WaitForNextBlock())

args := []string{}
args = append(args, fields...)
s.T().Run(tc.desc, func(t *testing.T) {
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 @@ -59,24 +57,25 @@ func TestCreate<%= TypeName.UpperCamel %>(t *testing.T) {
}
}

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 (
net = s.network()
val = net.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.cfg.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)
s.waitForNextBlock()

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

args := []string{tc.id}
args = append(args, fields...)
s.T().Run(tc.desc, func(t *testing.T) {
args := append([]string{tc.id}, fields...)
args = append(args, tc.args...)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdUpdate<%= TypeName.UpperCamel %>(), args)
if tc.err != nil {
Expand All @@ -123,24 +119,25 @@ func TestUpdate<%= TypeName.UpperCamel %>(t *testing.T) {
}
}

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 (
net = s.network()
val = net.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.cfg.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)
s.waitForNextBlock()

tests := []struct {
desc string
Expand All @@ -167,9 +164,7 @@ 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) {
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdDelete<%= TypeName.UpperCamel %>(), append([]string{tc.id}, tc.args...))
if tc.err != nil {
require.ErrorIs(t, err, tc.err)
Expand Down
Loading

0 comments on commit 4a94461

Please sign in to comment.