Skip to content

Commit

Permalink
fix network tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani authored and Pantani committed Nov 8, 2023
1 parent 34b5a86 commit aeb2432
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,38 @@ package cli_test
import (
"testing"

"github.com/cosmos/gogoproto/proto"
"github.com/stretchr/testify/suite"

"<%= modulePath %>/testutil/network"
"<%= modulePath %>/x/<%= moduleName %>/types"
)

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) network(state proto.Message) *network.Network {
s.T().Helper()
cfg := network.DefaultConfig()
if state != nil {
buf, err := cfg.Codec.MarshalJSON(state)
s.Require().NoError(err)
cfg.GenesisState[types.ModuleName] = buf
}
return network.New(s.T(), cfg)
}

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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +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 (s *IntegrationTestSuite) networkWith<%= TypeName.UpperCamel %>Objects(n int) []types.<%= TypeName.UpperCamel %> {
func (s *IntegrationTestSuite) networkWith<%= TypeName.UpperCamel %>Objects(n int) (*network.Network, []types.<%= TypeName.UpperCamel %>) {
s.T().Helper()
state := types.GenesisState{<%= if (IsIBC) { %>PortId: types.PortID<% } %>}
for i := 0; i < n; i++ {
Expand All @@ -26,21 +27,18 @@ func (s *IntegrationTestSuite) networkWith<%= TypeName.UpperCamel %>Objects(n in
nullify.Fill(&<%= TypeName.LowerCamel %>)
state.<%= TypeName.UpperCamel %>List = append(state.<%= TypeName.UpperCamel %>List, <%= TypeName.LowerCamel %>)
}
buf, err := s.cfg.Codec.MarshalJSON(&state)
s.Require().NoError(err)
s.cfg.GenesisState[types.ModuleName] = buf
return state.<%= TypeName.UpperCamel %>List
return s.network(&state), state.<%= TypeName.UpperCamel %>List
}

func (s *IntegrationTestSuite) TestShow<%= TypeName.UpperCamel %>() {
var (
objs = s.networkWith<%= TypeName.UpperCamel %>Objects(2)
net = s.network()
ctx = net.Validators[0].ClientCtx
common = []string{
net, objs = s.networkWith<%= TypeName.UpperCamel %>Objects(2)
ctx = net.Validators[0].ClientCtx
common = []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
}
)
defer net.Cleanup()
tests := []struct {
desc string
id string
Expand Down Expand Up @@ -73,7 +71,7 @@ func (s *IntegrationTestSuite) TestShow<%= TypeName.UpperCamel %>() {
}
require.NoError(t, err)
var resp types.QueryGet<%= TypeName.UpperCamel %>Response
require.NoError(t, s.cfg.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.<%= TypeName.UpperCamel %>)
require.Equal(t,
nullify.Fill(&tc.obj),
Expand All @@ -85,10 +83,10 @@ func (s *IntegrationTestSuite) TestShow<%= TypeName.UpperCamel %>() {

func (s *IntegrationTestSuite) TestList<%= TypeName.UpperCamel %>() {
var (
objs = s.networkWith<%= TypeName.UpperCamel %>Objects(5)
net = s.network()
ctx = net.Validators[0].ClientCtx
net, objs = s.networkWith<%= TypeName.UpperCamel %>Objects(5)
ctx = net.Validators[0].ClientCtx
)
defer net.Cleanup()
request := func(next []byte, offset, limit uint64, total bool) []string {
args := []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
Expand All @@ -111,7 +109,7 @@ func (s *IntegrationTestSuite) TestList<%= TypeName.UpperCamel %>() {
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdList<%= TypeName.UpperCamel %>(), args)
require.NoError(t, err)
var resp types.QueryAll<%= TypeName.UpperCamel %>Response
require.NoError(t, s.cfg.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step)
require.Subset(t,
nullify.Fill(objs),
Expand All @@ -127,7 +125,7 @@ func (s *IntegrationTestSuite) TestList<%= TypeName.UpperCamel %>() {
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdList<%= TypeName.UpperCamel %>(), args)
require.NoError(t, err)
var resp types.QueryAll<%= TypeName.UpperCamel %>Response
require.NoError(t, s.cfg.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step)
require.Subset(t,
nullify.Fill(objs),
Expand All @@ -141,7 +139,7 @@ func (s *IntegrationTestSuite) TestList<%= TypeName.UpperCamel %>() {
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdList<%= TypeName.UpperCamel %>(), args)
require.NoError(t, err)
var resp types.QueryAll<%= TypeName.UpperCamel %>Response
require.NoError(t, s.cfg.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NoError(t, net.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 @@ -17,12 +17,12 @@ import (

func (s *IntegrationTestSuite) TestCreate<%= TypeName.UpperCamel %>() {
var (
net = s.network()
net = s.network(nil)
val = net.Validators[0]
ctx = val.ClientCtx
fields = []string{<%= for (field) in Fields { %> "<%= field.DefaultTestValue() %>", <% } %>}
)

fields := []string{<%= for (field) in Fields { %> "<%= field.DefaultTestValue() %>", <% } %>}
defer net.Cleanup()
tests := []struct {
desc string
args []string
Expand All @@ -35,7 +35,7 @@ func (s *IntegrationTestSuite) TestCreate<%= TypeName.UpperCamel %>() {
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(s.cfg.BondDenom, sdkmath.NewInt(10))).String()),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(net.Config.BondDenom, sdkmath.NewInt(10))).String()),
},
},
}
Expand All @@ -59,23 +59,23 @@ func (s *IntegrationTestSuite) TestCreate<%= TypeName.UpperCamel %>() {

func (s *IntegrationTestSuite) TestUpdate<%= TypeName.UpperCamel %>() {
var (
net = s.network()
net = s.network(nil)
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()),
}
args = append([]string{}, fields...)
)

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(s.cfg.BondDenom, sdkmath.NewInt(10))).String()),
}
args := append([]string{}, fields...)
args = append(args, common...)
args = append(args, common...)
defer net.Cleanup()
_, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdCreate<%= TypeName.UpperCamel %>(), args)
s.Require().NoError(err)
s.waitForNextBlock()
s.Require().NoError(net.WaitForNextBlock())

tests := []struct {
desc string
Expand Down Expand Up @@ -121,23 +121,23 @@ func (s *IntegrationTestSuite) TestUpdate<%= TypeName.UpperCamel %>() {

func (s *IntegrationTestSuite) TestDelete<%= TypeName.UpperCamel %>() {
var (
net = s.network()
net = s.network(nil)
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()),
}
args = append([]string{}, fields...)
)

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(s.cfg.BondDenom, sdkmath.NewInt(10))).String()),
}
args := append([]string{}, fields...)
args = append(args, common...)
args = append(args, common...)
defer net.Cleanup()
_, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdCreate<%= TypeName.UpperCamel %>(), args)
s.Require().NoError(err)
s.waitForNextBlock()
s.Require().NoError(net.WaitForNextBlock())

tests := []struct {
desc string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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"
Expand All @@ -20,7 +21,7 @@ import (
// Prevent strconv unused error
var _ = strconv.IntSize

func (s *IntegrationTestSuite) networkWith<%= TypeName.UpperCamel %>Objects(n int) []types.<%= TypeName.UpperCamel %> {
func (s *IntegrationTestSuite) networkWith<%= TypeName.UpperCamel %>Objects(n int) (*network.Network, []types.<%= TypeName.UpperCamel %>) {
s.T().Helper()
state := types.GenesisState{<%= if (IsIBC) { %>PortId: types.PortID<% } %>}
for i := 0; i < n; i++ {
Expand All @@ -31,21 +32,18 @@ func (s *IntegrationTestSuite) networkWith<%= TypeName.UpperCamel %>Objects(n in
nullify.Fill(&<%= TypeName.LowerCamel %>)
state.<%= TypeName.UpperCamel %>List = append(state.<%= TypeName.UpperCamel %>List, <%= TypeName.LowerCamel %>)
}
buf, err := s.cfg.Codec.MarshalJSON(&state)
s.Require().NoError(err)
s.cfg.GenesisState[types.ModuleName] = buf
return state.<%= TypeName.UpperCamel %>List
return s.network(&state), state.<%= TypeName.UpperCamel %>List
}

func (s *IntegrationTestSuite) TestShow<%= TypeName.UpperCamel %>() {
var (
objs = s.networkWith<%= TypeName.UpperCamel %>Objects(2)
net = s.network()
ctx = net.Validators[0].ClientCtx
common = []string{
net, objs = s.networkWith<%= TypeName.UpperCamel %>Objects(2)
ctx = net.Validators[0].ClientCtx
common = []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
}
)
defer net.Cleanup()
tests := []struct {
desc string
<%= for (i, index) in Indexes { %>id<%= index.Name.UpperCamel %> <%= index.DataType() %>
Expand Down Expand Up @@ -85,7 +83,7 @@ func (s *IntegrationTestSuite) TestShow<%= TypeName.UpperCamel %>() {
}
require.NoError(t, err)
var resp types.QueryGet<%= TypeName.UpperCamel %>Response
require.NoError(t, s.cfg.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.<%= TypeName.UpperCamel %>)
require.Equal(t,
nullify.Fill(&tc.obj),
Expand All @@ -97,10 +95,10 @@ func (s *IntegrationTestSuite) TestShow<%= TypeName.UpperCamel %>() {

func (s *IntegrationTestSuite) TestList<%= TypeName.UpperCamel %>() {
var (
objs = s.networkWith<%= TypeName.UpperCamel %>Objects(5)
net = s.network()
ctx = net.Validators[0].ClientCtx
net, objs = s.networkWith<%= TypeName.UpperCamel %>Objects(5)
ctx = net.Validators[0].ClientCtx
)
defer net.Cleanup()
request := func(next []byte, offset, limit uint64, total bool) []string {
args := []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
Expand All @@ -123,7 +121,7 @@ func (s *IntegrationTestSuite) TestList<%= TypeName.UpperCamel %>() {
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdList<%= TypeName.UpperCamel %>(), args)
require.NoError(t, err)
var resp types.QueryAll<%= TypeName.UpperCamel %>Response
require.NoError(t, s.cfg.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step)
require.Subset(t,
nullify.Fill(objs),
Expand All @@ -139,7 +137,7 @@ func (s *IntegrationTestSuite) TestList<%= TypeName.UpperCamel %>() {
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdList<%= TypeName.UpperCamel %>(), args)
require.NoError(t, err)
var resp types.QueryAll<%= TypeName.UpperCamel %>Response
require.NoError(t, s.cfg.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step)
require.Subset(t,
nullify.Fill(objs),
Expand All @@ -153,7 +151,7 @@ func (s *IntegrationTestSuite) TestList<%= TypeName.UpperCamel %>() {
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdList<%= TypeName.UpperCamel %>(), args)
require.NoError(t, err)
var resp types.QueryAll<%= TypeName.UpperCamel %>Response
require.NoError(t, s.cfg.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NoError(t, err)
require.Equal(t, len(objs), int(resp.Pagination.Total))
require.ElementsMatch(t,
Expand Down
Loading

0 comments on commit aeb2432

Please sign in to comment.