From 4a9446165a55c57c4c0c90093d9e6d2c751759b6 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Mon, 6 Nov 2023 13:31:08 +0100 Subject: [PATCH] feat: add test suite for the cli tests (#3715) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 Co-authored-by: Jerónimo Albi --- changelog.md | 1 + .../client/cli/suite_test.go.plush | 54 +++++++++++++ .../files/testutil/network/network.go.plush | 1 - .../cli/query_{{typeName}}_test.go.plush | 75 ++++++++++--------- .../client/cli/tx_{{typeName}}_test.go.plush | 69 ++++++++--------- .../cli/query_{{typeName}}_test.go.plush | 72 +++++++++--------- .../client/cli/tx_{{typeName}}_test.go.plush | 56 +++++++------- .../cli/query_{{typeName}}_test.go.plush | 53 +++++++------ .../client/cli/tx_{{typeName}}_test.go.plush | 75 +++++++++---------- 9 files changed, 251 insertions(+), 205 deletions(-) create mode 100644 ignite/templates/module/create/files/base/x/{{moduleName}}/client/cli/suite_test.go.plush diff --git a/changelog.md b/changelog.md index 5581e423dc..bde13d11d5 100644 --- a/changelog.md +++ b/changelog.md @@ -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 diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/client/cli/suite_test.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/client/cli/suite_test.go.plush new file mode 100644 index 0000000000..135caf8045 --- /dev/null +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/client/cli/suite_test.go.plush @@ -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)) +} diff --git a/ignite/templates/testutil/files/testutil/network/network.go.plush b/ignite/templates/testutil/files/testutil/network/network.go.plush index 2536d92ca6..475de56957 100644 --- a/ignite/templates/testutil/files/testutil/network/network.go.plush +++ b/ignite/templates/testutil/files/testutil/network/network.go.plush @@ -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 } diff --git a/ignite/templates/typed/list/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush b/ignite/templates/typed/list/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush index c5a07cb0e8..bc13818958 100644 --- a/ignite/templates/typed/list/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush +++ b/ignite/templates/typed/list/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush @@ -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 %>{ @@ -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 @@ -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), @@ -103,14 +104,14 @@ 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), @@ -118,7 +119,7 @@ func TestList<%= TypeName.UpperCamel %>(t *testing.T) { ) } }) - 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 { @@ -126,7 +127,7 @@ func TestList<%= TypeName.UpperCamel %>(t *testing.T) { 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), @@ -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, diff --git a/ignite/templates/typed/list/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush b/ignite/templates/typed/list/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush index de69fdf6a6..608a4fc742 100644 --- a/ignite/templates/typed/list/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush +++ b/ignite/templates/typed/list/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush @@ -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 { @@ -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 { @@ -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 @@ -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 { @@ -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 @@ -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) diff --git a/ignite/templates/typed/map/files/tests/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush b/ignite/templates/typed/map/files/tests/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush index 725e8e740f..9a5499ffa2 100644 --- a/ignite/templates/typed/map/files/tests/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush +++ b/ignite/templates/typed/map/files/tests/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush @@ -12,7 +12,6 @@ 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" @@ -21,9 +20,8 @@ import ( // Prevent strconv unused error var _ = strconv.IntSize -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 %>{ @@ -33,19 +31,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 <%= for (i, index) in Indexes { %>id<%= index.Name.UpperCamel %> <%= index.DataType() %> @@ -70,7 +70,7 @@ func TestShow<%= TypeName.UpperCamel %>(t *testing.T) { }, } for _, tc := range tests { - t.Run(tc.desc, func(t *testing.T) { + s.T().Run(tc.desc, func(t *testing.T) { args := []string{ <%= for (i, index) in Indexes { %><%= index.ToString("tc.id" + index.Name.UpperCamel) %>, <% } %> @@ -81,24 +81,26 @@ func TestShow<%= TypeName.UpperCamel %>(t *testing.T) { 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), @@ -114,14 +116,14 @@ 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), @@ -129,7 +131,7 @@ func TestList<%= TypeName.UpperCamel %>(t *testing.T) { ) } }) - 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 { @@ -137,7 +139,7 @@ func TestList<%= TypeName.UpperCamel %>(t *testing.T) { 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), @@ -146,12 +148,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, diff --git a/ignite/templates/typed/map/files/tests/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush b/ignite/templates/typed/map/files/tests/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush index f08fda4241..479169b9c2 100644 --- a/ignite/templates/typed/map/files/tests/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush +++ b/ignite/templates/typed/map/files/tests/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush @@ -12,17 +12,18 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/stretchr/testify/require" - "<%= ModulePath %>/testutil/network" "<%= ModulePath %>/x/<%= ModuleName %>/client/cli" ) // Prevent strconv unused error var _ = strconv.IntSize -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 { @@ -41,14 +42,12 @@ 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()) - + s.T().Run(tc.desc, func(t *testing.T) { args := []string{ <%= for (i, index) in Indexes { %><%= index.ToString("tc.id" + index.Name.UpperCamel) %>, <% } %> @@ -69,17 +68,19 @@ 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{ <%= for (i, index) in Indexes { %>"0", @@ -88,7 +89,8 @@ func TestUpdate<%= TypeName.UpperCamel %>(t *testing.T) { args = append(args, 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 @@ -113,9 +115,7 @@ 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) { args := []string{ <%= for (i, index) in Indexes { %><%= index.ToString("tc.id" + index.Name.UpperCamel) %>, <% } %> @@ -136,18 +136,19 @@ 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{ <%= for (i, index) in Indexes { %>"0", @@ -156,7 +157,8 @@ func TestDelete<%= TypeName.UpperCamel %>(t *testing.T) { args = append(args, 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 @@ -181,9 +183,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) { args := []string{ <%= for (i, index) in Indexes { %><%= index.ToString("tc.id" + index.Name.UpperCamel) %>, <% } %> diff --git a/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush b/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush index c0deada34d..e106924ce4 100644 --- a/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush +++ b/ignite/templates/typed/singleton/files/component/x/{{moduleName}}/client/cli/query_{{typeName}}_test.go.plush @@ -9,32 +9,32 @@ import ( tmcli "github.com/cometbft/cometbft/libs/cli" "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) (*network.Network, types.<%= TypeName.UpperCamel %>) { - t.Helper() - cfg := network.DefaultConfig() +func (s *IntegrationTestSuite) networkWith<%= TypeName.UpperCamel %>Objects() (types.<%= TypeName.UpperCamel %>) { + s.T().Helper() state := types.GenesisState{<%= if (IsIBC) { %>PortId: types.PortID<% } %>} <%= TypeName.LowerCamel %> := &types.<%= TypeName.UpperCamel %>{} nullify.Fill(&<%= TypeName.LowerCamel %>) state.<%= TypeName.UpperCamel %> = <%= 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 %> + buf, err := s.cfg.Codec.MarshalJSON(&state) + s.Require().NoError(err) + s.cfg.GenesisState[types.ModuleName] = buf + return *state.<%= TypeName.UpperCamel %> } -func TestShow<%= TypeName.UpperCamel %>(t *testing.T) { - net, obj := networkWith<%= TypeName.UpperCamel %>Objects(t) - - ctx := net.Validators[0].ClientCtx - common := []string{ - fmt.Sprintf("--%s=json", tmcli.OutputFlag), - } +func (s *IntegrationTestSuite) TestShow<%= TypeName.UpperCamel %>() { + var ( + obj = s.networkWith<%= TypeName.UpperCamel %>Objects() + net = s.network() + ctx = net.Validators[0].ClientCtx + common = []string{ + fmt.Sprintf("--%s=json", tmcli.OutputFlag), + } + ) tests := []struct { desc string args []string @@ -48,24 +48,23 @@ func TestShow<%= TypeName.UpperCamel %>(t *testing.T) { }, } for _, tc := range tests { - t.Run(tc.desc, func(t *testing.T) { - var args []string - args = append(args, tc.args...) + s.T().Run(tc.desc, func(t *testing.T) { + args := append([]string{}, 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 %>), + ) }) } } diff --git a/ignite/templates/typed/singleton/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush b/ignite/templates/typed/singleton/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush index 00ac07ad94..6d63845b90 100644 --- a/ignite/templates/typed/singleton/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush +++ b/ignite/templates/typed/singleton/files/messages/x/{{moduleName}}/client/cli/tx_{{typeName}}_test.go.plush @@ -10,16 +10,16 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "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 { desc string @@ -33,16 +33,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()) - - var 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 { @@ -58,23 +55,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()), } - var 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 @@ -88,11 +87,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()) - - var 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.CmdUpdate<%= TypeName.UpperCamel %>(), args) if tc.err != nil { @@ -108,24 +104,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()), } - var 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 @@ -139,9 +136,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.args...)) if tc.err != nil { require.ErrorIs(t, err, tc.err)