github.com/Finschia/finschia-sdk@v0.48.1/x/upgrade/client/testutil/suite.go (about) 1 package testutil 2 3 import ( 4 "fmt" 5 6 "github.com/stretchr/testify/suite" 7 tmproto "github.com/tendermint/tendermint/proto/tendermint/types" 8 9 "github.com/Finschia/finschia-sdk/simapp" 10 clitestutil "github.com/Finschia/finschia-sdk/testutil/cli" 11 "github.com/Finschia/finschia-sdk/testutil/network" 12 sdk "github.com/Finschia/finschia-sdk/types" 13 "github.com/Finschia/finschia-sdk/x/upgrade/client/cli" 14 "github.com/Finschia/finschia-sdk/x/upgrade/types" 15 ) 16 17 func NewIntegrationTestSuite(cfg network.Config) *IntegrationTestSuite { 18 return &IntegrationTestSuite{cfg: cfg} 19 } 20 21 type IntegrationTestSuite struct { 22 suite.Suite 23 24 app *simapp.SimApp 25 cfg network.Config 26 network *network.Network 27 ctx sdk.Context 28 } 29 30 func (s *IntegrationTestSuite) SetupSuite() { 31 s.T().Log("setting up integration test suite") 32 app := simapp.Setup(false) 33 s.app = app 34 s.ctx = app.BaseApp.NewContext(false, tmproto.Header{}) 35 36 cfg := network.DefaultConfig() 37 cfg.NumValidators = 1 38 39 s.cfg = cfg 40 s.network = network.New(s.T(), cfg) 41 } 42 43 func (s *IntegrationTestSuite) TearDownSuite() { 44 s.T().Log("tearing down integration test suite") 45 s.network.Cleanup() 46 } 47 48 func (s *IntegrationTestSuite) TestModuleVersionsCLI() { 49 testCases := []struct { 50 msg string 51 req types.QueryModuleVersionsRequest 52 single bool 53 expPass bool 54 }{ 55 { 56 msg: "test full query", 57 req: types.QueryModuleVersionsRequest{ModuleName: ""}, 58 single: false, 59 expPass: true, 60 }, 61 { 62 msg: "test single module", 63 req: types.QueryModuleVersionsRequest{ModuleName: "bank"}, 64 single: true, 65 expPass: true, 66 }, 67 { 68 msg: "test non-existent module", 69 req: types.QueryModuleVersionsRequest{ModuleName: "abcdefg"}, 70 single: true, 71 expPass: false, 72 }, 73 } 74 75 val := s.network.Validators[0] 76 clientCtx := val.ClientCtx 77 // avoid printing as yaml from CLI command 78 clientCtx.OutputFormat = "JSON" 79 80 vm := s.app.UpgradeKeeper.GetModuleVersionMap(s.ctx) 81 mv := s.app.UpgradeKeeper.GetModuleVersions(s.ctx) 82 s.Require().NotEmpty(vm) 83 84 for _, stc := range testCases { 85 tc := stc 86 s.Run(fmt.Sprintf("Case %s", tc.msg), func() { 87 expect := mv 88 if tc.expPass { 89 if tc.single { 90 expect = []*types.ModuleVersion{{Name: tc.req.ModuleName, Version: vm[tc.req.ModuleName]}} 91 } 92 // setup expected response 93 pm := types.QueryModuleVersionsResponse{ 94 ModuleVersions: expect, 95 } 96 jsonVM, _ := clientCtx.Codec.MarshalJSON(&pm) 97 expectedRes := string(jsonVM) 98 // append new line to match behaviour of PrintProto 99 expectedRes += "\n" 100 101 // get actual module versions list response from cli 102 cmd := cli.GetModuleVersionsCmd() 103 outVM, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, []string{tc.req.ModuleName}) 104 s.Require().NoError(err) 105 106 s.Require().Equal(expectedRes, outVM.String()) 107 } else { 108 cmd := cli.GetModuleVersionsCmd() 109 _, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, []string{tc.req.ModuleName}) 110 s.Require().Error(err) 111 } 112 }) 113 } 114 }