From 710e64e82572f5f6f341cff9ff559e58abb2dea8 Mon Sep 17 00:00:00 2001 From: 170210 Date: Tue, 21 May 2024 17:36:44 +0900 Subject: [PATCH 1/8] test: add cli_test for query Signed-off-by: 170210 --- x/fswap/client/testutil/cli_test.go | 18 ++ x/fswap/client/testutil/query.go | 245 ++++++++++++++++++++++++++++ x/fswap/client/testutil/suite.go | 91 +++++++++++ 3 files changed, 354 insertions(+) create mode 100644 x/fswap/client/testutil/cli_test.go create mode 100644 x/fswap/client/testutil/query.go create mode 100644 x/fswap/client/testutil/suite.go diff --git a/x/fswap/client/testutil/cli_test.go b/x/fswap/client/testutil/cli_test.go new file mode 100644 index 0000000000..15c377bbb9 --- /dev/null +++ b/x/fswap/client/testutil/cli_test.go @@ -0,0 +1,18 @@ +//go:build norace +// +build norace + +package testutil + +import ( + "testing" + + "github.com/stretchr/testify/suite" + + "github.com/Finschia/finschia-sdk/testutil/network" +) + +func TestIntegrationTestSuite(t *testing.T) { + cfg := network.DefaultConfig() + cfg.NumValidators = 1 + suite.Run(t, NewIntegrationTestSuite(cfg)) +} diff --git a/x/fswap/client/testutil/query.go b/x/fswap/client/testutil/query.go new file mode 100644 index 0000000000..78bb57b6af --- /dev/null +++ b/x/fswap/client/testutil/query.go @@ -0,0 +1,245 @@ +package testutil + +import ( + clitestutil "github.com/Finschia/finschia-sdk/testutil/cli" + sdk "github.com/Finschia/finschia-sdk/types" + "github.com/Finschia/finschia-sdk/types/query" + "github.com/Finschia/finschia-sdk/x/fswap/client/cli" + fswaptypes "github.com/Finschia/finschia-sdk/x/fswap/types" + "github.com/gogo/protobuf/proto" +) + +func (s *IntegrationTestSuite) TestCmdQuerySwapped() { + val := s.network.Validators[0] + clientCtx := val.ClientCtx + // avoid printing as yaml from CLI command + clientCtx.OutputFormat = "JSON" + + fromDenom := s.cfg.BondDenom + toDenom := s.toDenom.Base + + testCases := []struct { + name string + args []string + expectErr bool + expected proto.Message + }{ + { + "valid query", + []string{fromDenom, toDenom}, + false, + &fswaptypes.QuerySwappedResponse{ + FromCoinAmount: sdk.NewCoin(fromDenom, sdk.ZeroInt()), + ToCoinAmount: sdk.NewCoin(toDenom, sdk.ZeroInt()), + }, + }, + { + "wrong number of args", + []string{fromDenom, toDenom, "extra"}, + true, + nil, + }, + { + "invalid fromDenom", + []string{"", toDenom}, + true, + nil, + }, + { + "invalid toDenom", + []string{fromDenom, ""}, + true, + nil, + }, + } + + for _, tc := range testCases { + tc := tc + + s.Run(tc.name, func() { + cmd := cli.CmdQuerySwapped() + out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) + + if tc.expectErr { + s.Require().Error(err) + } else { + s.Require().NoError(err) + var actual fswaptypes.QuerySwappedResponse + s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual)) + s.Require().Equal(tc.expected, &actual) + } + }) + } +} + +func (s *IntegrationTestSuite) TestCmdQueryTotalSwappableAmount() { + val := s.network.Validators[0] + clientCtx := val.ClientCtx + // avoid printing as yaml from CLI command + clientCtx.OutputFormat = "JSON" + + fromDenom := s.cfg.BondDenom + toDenom := s.toDenom.Base + + testCases := []struct { + name string + args []string + expectErr bool + expected proto.Message + }{ + { + "valid query", + []string{fromDenom, toDenom}, + false, + &fswaptypes.QueryTotalSwappableToCoinAmountResponse{ + SwappableAmount: sdk.NewCoin(toDenom, s.dummySwap.AmountCapForToDenom), + }, + }, + { + "wrong number of args", + []string{fromDenom, toDenom, "extra"}, + true, + nil, + }, + { + "invalid fromDenom", + []string{"", toDenom}, + true, + nil, + }, + { + "invalid toDenom", + []string{fromDenom, ""}, + true, + nil, + }, + } + + for _, tc := range testCases { + tc := tc + + s.Run(tc.name, func() { + cmd := cli.CmdQueryTotalSwappableAmount() + out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) + + if tc.expectErr { + s.Require().Error(err) + } else { + s.Require().NoError(err) + var actual fswaptypes.QueryTotalSwappableToCoinAmountResponse + s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual)) + s.Require().Equal(tc.expected, &actual) + } + }) + } +} + +func (s *IntegrationTestSuite) TestCmdQuerySwap() { + val := s.network.Validators[0] + clientCtx := val.ClientCtx + // avoid printing as yaml from CLI command + clientCtx.OutputFormat = "JSON" + + fromDenom := s.cfg.BondDenom + toDenom := s.toDenom.Base + + testCases := []struct { + name string + args []string + expectErr bool + expected proto.Message + }{ + { + "valid query", + []string{fromDenom, toDenom}, + false, + &fswaptypes.QuerySwapResponse{ + Swap: s.dummySwap, + }, + }, + { + "wrong number of args", + []string{fromDenom, toDenom, "extra"}, + true, + nil, + }, + { + "invalid fromDenom", + []string{"", toDenom}, + true, + nil, + }, + { + "invalid toDenom", + []string{fromDenom, ""}, + true, + nil, + }, + } + + for _, tc := range testCases { + tc := tc + + s.Run(tc.name, func() { + cmd := cli.CmdQuerySwap() + out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) + + if tc.expectErr { + s.Require().Error(err) + } else { + s.Require().NoError(err) + var actual fswaptypes.QuerySwapResponse + s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual)) + s.Require().Equal(tc.expected, &actual) + } + }) + } +} + +func (s *IntegrationTestSuite) TestCmdQuerySwaps() { + val := s.network.Validators[0] + clientCtx := val.ClientCtx + // avoid printing as yaml from CLI command + clientCtx.OutputFormat = "JSON" + + testCases := []struct { + name string + args []string + expectErr bool + expected proto.Message + }{ + { + "valid query (default pagination)", + []string{}, + false, + &fswaptypes.QuerySwapsResponse{ + Swaps: []fswaptypes.Swap{s.dummySwap}, + Pagination: &query.PageResponse{Total: 1}, + }, + }, + { + "invalid query", + []string{"extra"}, + true, + nil, + }, + } + + for _, tc := range testCases { + tc := tc + + s.Run(tc.name, func() { + cmd := cli.CmdQuerySwaps() + out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) + + if tc.expectErr { + s.Require().Error(err) + } else { + s.Require().NoError(err) + var actual fswaptypes.QuerySwapsResponse + s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual)) + s.Require().Equal(tc.expected, &actual) + } + }) + } +} diff --git a/x/fswap/client/testutil/suite.go b/x/fswap/client/testutil/suite.go new file mode 100644 index 0000000000..89472572c1 --- /dev/null +++ b/x/fswap/client/testutil/suite.go @@ -0,0 +1,91 @@ +package testutil + +import ( + "github.com/Finschia/finschia-sdk/testutil/network" + sdk "github.com/Finschia/finschia-sdk/types" + banktypes "github.com/Finschia/finschia-sdk/x/bank/types" + fswaptypes "github.com/Finschia/finschia-sdk/x/fswap/types" + "github.com/stretchr/testify/suite" +) + +type IntegrationTestSuite struct { + suite.Suite + + cfg network.Config + network *network.Network + + toDenom banktypes.Metadata + dummySwap fswaptypes.Swap +} + +func NewIntegrationTestSuite(cfg network.Config) *IntegrationTestSuite { + return &IntegrationTestSuite{cfg: cfg} +} + +func (s *IntegrationTestSuite) SetupSuite() { + s.T().Log("setting up integration test suite") + + genesisState := s.cfg.GenesisState + var bankGenesis banktypes.GenesisState + s.Require().NoError(s.cfg.Codec.UnmarshalJSON(genesisState[banktypes.ModuleName], &bankGenesis)) + s.toDenom = banktypes.Metadata{ + Name: "Dummy Coin", + Symbol: "Dummy", + Description: "The native token of Dummy chain", + DenomUnits: []*banktypes.DenomUnit{ + { + Denom: "dummy", + Exponent: 0, + Aliases: []string{}, + }, + }, + Base: "dummy", + Display: "Dummy", + } + bankGenesis.DenomMetadata = []banktypes.Metadata{s.toDenom} + bankDataBz, err := s.cfg.Codec.MarshalJSON(&bankGenesis) + s.Require().NoError(err) + genesisState[banktypes.ModuleName] = bankDataBz + + bondDenom := s.cfg.BondDenom + toDenom := s.toDenom.Base + + var fswapData fswaptypes.GenesisState + s.Require().NoError(s.cfg.Codec.UnmarshalJSON(genesisState[fswaptypes.ModuleName], &fswapData)) + fswapData.SwapStats.SwapCount = 1 + s.dummySwap = fswaptypes.Swap{ + FromDenom: bondDenom, + ToDenom: toDenom, + AmountCapForToDenom: sdk.NewInt(12340000000000), + SwapRate: sdk.MustNewDecFromStr("1234"), + } + + fswapData.Swaps = []fswaptypes.Swap{s.dummySwap} + fswapData.Swappeds = []fswaptypes.Swapped{ + { + FromCoinAmount: sdk.Coin{ + Denom: bondDenom, + Amount: sdk.ZeroInt(), + }, + ToCoinAmount: sdk.Coin{ + Denom: toDenom, + Amount: sdk.ZeroInt(), + }, + }, + } + + fswapDataBz, err := s.cfg.Codec.MarshalJSON(&fswapData) + s.Require().NoError(err) + genesisState[fswaptypes.ModuleName] = fswapDataBz + s.cfg.GenesisState = genesisState + + s.network = network.New(s.T(), s.cfg) + + _, err = s.network.WaitForHeight(1) + s.Require().NoError(err) +} + +func (s *IntegrationTestSuite) TearDownSuite() { + s.T().Log("tearing down integration test suite") + s.network.Cleanup() +} From 1c891b65b6bd8e4da21d2605c94bce4ee18770c9 Mon Sep 17 00:00:00 2001 From: 170210 Date: Wed, 22 May 2024 12:55:09 +0900 Subject: [PATCH 2/8] test: add cli_test for tx Signed-off-by: 170210 --- x/fswap/client/cli/tx.go | 9 +- x/fswap/client/testutil/suite.go | 4 +- x/fswap/client/testutil/tx.go | 378 +++++++++++++++++++++++++++++++ 3 files changed, 386 insertions(+), 5 deletions(-) create mode 100644 x/fswap/client/testutil/tx.go diff --git a/x/fswap/client/cli/tx.go b/x/fswap/client/cli/tx.go index 61cd359d75..b92f326b99 100644 --- a/x/fswap/client/cli/tx.go +++ b/x/fswap/client/cli/tx.go @@ -22,6 +22,10 @@ const ( FlagSwapRate = "swap-rate" ) +type ToDenomMeta struct { + Metadata bank.Metadata `json:"metadata"` +} + // GetTxCmd returns the transaction commands for this module func GetTxCmd() *cobra.Command { cmd := &cobra.Command{ @@ -232,10 +236,7 @@ func validateGenerateOnly(cmd *cobra.Command) error { } func parseToDenomMetadata(jsonDenomMetadata string) (bank.Metadata, error) { - type toDenomMeta struct { - Metadata bank.Metadata `json:"metadata"` - } - denomMeta := toDenomMeta{} + denomMeta := ToDenomMeta{} if err := json.Unmarshal([]byte(jsonDenomMetadata), &denomMeta); err != nil { return bank.Metadata{}, err } diff --git a/x/fswap/client/testutil/suite.go b/x/fswap/client/testutil/suite.go index 89472572c1..9f59d9063a 100644 --- a/x/fswap/client/testutil/suite.go +++ b/x/fswap/client/testutil/suite.go @@ -14,6 +14,7 @@ type IntegrationTestSuite struct { cfg network.Config network *network.Network + authority sdk.AccAddress toDenom banktypes.Metadata dummySwap fswaptypes.Swap } @@ -40,7 +41,7 @@ func (s *IntegrationTestSuite) SetupSuite() { }, }, Base: "dummy", - Display: "Dummy", + Display: "dummy", } bankGenesis.DenomMetadata = []banktypes.Metadata{s.toDenom} bankDataBz, err := s.cfg.Codec.MarshalJSON(&bankGenesis) @@ -80,6 +81,7 @@ func (s *IntegrationTestSuite) SetupSuite() { s.cfg.GenesisState = genesisState s.network = network.New(s.T(), s.cfg) + s.authority = fswaptypes.DefaultAuthority() _, err = s.network.WaitForHeight(1) s.Require().NoError(err) diff --git a/x/fswap/client/testutil/tx.go b/x/fswap/client/testutil/tx.go new file mode 100644 index 0000000000..dd22158f2a --- /dev/null +++ b/x/fswap/client/testutil/tx.go @@ -0,0 +1,378 @@ +package testutil + +import ( + "encoding/json" + "fmt" + + "github.com/Finschia/finschia-sdk/client/flags" + clitestutil "github.com/Finschia/finschia-sdk/testutil/cli" + sdk "github.com/Finschia/finschia-sdk/types" + sdkerrors "github.com/Finschia/finschia-sdk/types/errors" + "github.com/Finschia/finschia-sdk/x/fswap/client/cli" + "github.com/gogo/protobuf/proto" +) + +func (s *IntegrationTestSuite) TestCmdTxMsgSwap() { + val := s.network.Validators[0] + clientCtx := val.ClientCtx + // avoid printing as yaml from CLI command + clientCtx.OutputFormat = "JSON" + + testCases := []struct { + name string + args []string + expectErr bool + expectedCode uint32 + respType proto.Message + }{ + { + "valid transaction", + []string{ + val.Address.String(), + sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)).String(), + s.toDenom.Base, + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), + }, + false, + 0, + &sdk.TxResponse{}, + }, + { + "invalid request (wrong number of args)", + []string{ + val.Address.String(), + sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)).String(), + s.toDenom.Base, + "extra", + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(1))).String()), + }, + true, + 0, + nil, + }, + { + "invalid request (invalid from address)", + []string{ + "invalidAddress", + sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)).String(), + s.toDenom.Base, + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(1))).String()), + }, + true, + 0, + nil, + }, + { + "invalid request (invalid from coin amount)", + []string{ + val.Address.String(), + "", + s.toDenom.Base, + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(1))).String()), + }, + true, + 0, + nil, + }, + { + "not enough fees", + []string{ + val.Address.String(), + sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)).String(), + s.toDenom.Base, + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(1))).String()), + }, + false, + sdkerrors.ErrInsufficientFee.ABCICode(), + &sdk.TxResponse{}, + }, + { + "not enough gas", + []string{ + val.Address.String(), + sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)).String(), + s.toDenom.Base, + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), + "--gas=10", + }, + false, + sdkerrors.ErrOutOfGas.ABCICode(), + &sdk.TxResponse{}, + }, + } + + for _, tc := range testCases { + tc := tc + + s.Run(tc.name, func() { + cmd := cli.CmdTxMsgSwap() + bz, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) + + if tc.expectErr { + s.Require().Error(err) + } else { + s.Require().NoError(err) + + s.Require().NoError(clientCtx.Codec.UnmarshalJSON(bz.Bytes(), tc.respType), bz.String()) + txResp := tc.respType.(*sdk.TxResponse) + s.Require().Equal(tc.expectedCode, txResp.Code) + } + }) + } +} + +func (s *IntegrationTestSuite) TestCmdTxMsgSwapAll() { + val := s.network.Validators[0] + clientCtx := val.ClientCtx + // avoid printing as yaml from CLI command + clientCtx.OutputFormat = "JSON" + + testCases := []struct { + name string + args []string + expectErr bool + expectedCode uint32 + respType proto.Message + }{ + { + "valid transaction", + []string{ + val.Address.String(), + s.cfg.BondDenom, + s.toDenom.Base, + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), + }, + false, + 0, + &sdk.TxResponse{}, + }, + { + "invalid request (wrong number of args)", + []string{ + val.Address.String(), + s.cfg.BondDenom, + s.toDenom.Base, + "extra", + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(1))).String()), + }, + true, + 0, + nil, + }, + { + "invalid request (invalid from address)", + []string{ + "invalidAddress", + s.cfg.BondDenom, + s.toDenom.Base, + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(1))).String()), + }, + true, + 0, + nil, + }, + { + "not enough fees", + []string{ + val.Address.String(), + s.cfg.BondDenom, + s.toDenom.Base, + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(1))).String()), + }, + false, + sdkerrors.ErrInsufficientFee.ABCICode(), + &sdk.TxResponse{}, + }, + { + "not enough gas", + []string{ + val.Address.String(), + s.cfg.BondDenom, + s.toDenom.Base, + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), + "--gas=10", + }, + false, + sdkerrors.ErrOutOfGas.ABCICode(), + &sdk.TxResponse{}, + }, + } + + for _, tc := range testCases { + tc := tc + + s.Run(tc.name, func() { + cmd := cli.CmdTxMsgSwapAll() + bz, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) + + if tc.expectErr { + s.Require().Error(err) + } else { + s.Require().NoError(err) + + s.Require().NoError(clientCtx.Codec.UnmarshalJSON(bz.Bytes(), tc.respType), bz.String()) + txResp := tc.respType.(*sdk.TxResponse) + s.Require().Equal(tc.expectedCode, txResp.Code) + } + }) + } +} + +func (s *IntegrationTestSuite) TestMsgSetSwap() { + val := s.network.Validators[0] + clientCtx := val.ClientCtx + // avoid printing as yaml from CLI command + clientCtx.OutputFormat = "JSON" + + denomMeta := cli.ToDenomMeta{ + Metadata: s.toDenom, + } + jsonBytes, err := json.Marshal(denomMeta) + s.Require().NoError(err) + denomMetaString := string(jsonBytes) + + commonArgs := []string{ + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), + } + + testCases := []struct { + name string + //modify func(args []string) []string + args []string + expectErr bool + }{ + { + "valid transaction (generateOnly)", + []string{ + s.authority.String(), + denomMetaString, + fmt.Sprintf("--%s=true", flags.FlagGenerateOnly), + fmt.Sprintf("--%s=%s", cli.FlagFromDenom, s.dummySwap.FromDenom), + fmt.Sprintf("--%s=%s", cli.FlagToDenom, s.dummySwap.ToDenom), + fmt.Sprintf("--%s=%s", cli.FlagAmountCapForToDenom, s.dummySwap.AmountCapForToDenom), + fmt.Sprintf("--%s=%s", cli.FlagSwapRate, s.dummySwap.SwapRate), + }, + false, + }, + { + "invalid transaction (without generateOnly)", + []string{ + s.authority.String(), + denomMetaString, + fmt.Sprintf("--%s=false", flags.FlagGenerateOnly), + fmt.Sprintf("--%s=%s", cli.FlagFromDenom, s.dummySwap.FromDenom), + fmt.Sprintf("--%s=%s", cli.FlagToDenom, s.dummySwap.ToDenom), + fmt.Sprintf("--%s=%s", cli.FlagAmountCapForToDenom, s.dummySwap.AmountCapForToDenom), + fmt.Sprintf("--%s=%s", cli.FlagSwapRate, s.dummySwap.SwapRate), + }, + true, + }, + { + "extra args", + []string{ + s.authority.String(), + denomMetaString, + "extra", + fmt.Sprintf("--%s=true", flags.FlagGenerateOnly), + fmt.Sprintf("--%s=%s", cli.FlagFromDenom, s.dummySwap.FromDenom), + fmt.Sprintf("--%s=%s", cli.FlagToDenom, s.dummySwap.ToDenom), + fmt.Sprintf("--%s=%s", cli.FlagAmountCapForToDenom, s.dummySwap.AmountCapForToDenom), + fmt.Sprintf("--%s=%s", cli.FlagSwapRate, s.dummySwap.SwapRate), + }, + true, + }, + { + "invalid authority", + []string{ + "invalid authority", + denomMetaString, + fmt.Sprintf("--%s=true", flags.FlagGenerateOnly), + fmt.Sprintf("--%s=%s", cli.FlagFromDenom, s.dummySwap.FromDenom), + fmt.Sprintf("--%s=%s", cli.FlagToDenom, s.dummySwap.ToDenom), + fmt.Sprintf("--%s=%s", cli.FlagAmountCapForToDenom, s.dummySwap.AmountCapForToDenom), + fmt.Sprintf("--%s=%s", cli.FlagSwapRate, s.dummySwap.SwapRate), + }, + true, + }, + { + "invalid json", + []string{ + s.authority.String(), + "invalid json", + fmt.Sprintf("--%s=true", flags.FlagGenerateOnly), + fmt.Sprintf("--%s=%s", cli.FlagFromDenom, s.dummySwap.FromDenom), + fmt.Sprintf("--%s=%s", cli.FlagToDenom, s.dummySwap.ToDenom), + fmt.Sprintf("--%s=%s", cli.FlagAmountCapForToDenom, s.dummySwap.AmountCapForToDenom), + fmt.Sprintf("--%s=%s", cli.FlagSwapRate, s.dummySwap.SwapRate), + }, + true, + }, + { + "invalid amountCapForToDenom", + []string{ + s.authority.String(), + denomMetaString, + fmt.Sprintf("--%s=true", flags.FlagGenerateOnly), + fmt.Sprintf("--%s=%s", cli.FlagFromDenom, s.dummySwap.FromDenom), + fmt.Sprintf("--%s=%s", cli.FlagToDenom, s.dummySwap.ToDenom), + fmt.Sprintf("--%s=%s", cli.FlagAmountCapForToDenom, "123.456"), + fmt.Sprintf("--%s=%s", cli.FlagSwapRate, s.dummySwap.SwapRate), + }, + true, + }, + { + "invalid swapRate", + []string{ + s.authority.String(), + denomMetaString, + fmt.Sprintf("--%s=true", flags.FlagGenerateOnly), + fmt.Sprintf("--%s=%s", cli.FlagFromDenom, s.dummySwap.FromDenom), + fmt.Sprintf("--%s=%s", cli.FlagToDenom, s.dummySwap.ToDenom), + fmt.Sprintf("--%s=%s", cli.FlagAmountCapForToDenom, s.dummySwap.AmountCapForToDenom), + fmt.Sprintf("--%s=%s", cli.FlagSwapRate, "abc.123"), + }, + true, + }, + } + + for _, tc := range testCases { + tc := tc + + s.Run(tc.name, func() { + cmd := cli.CmdMsgSetSwap() + bz, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, append(tc.args, commonArgs...)) + + if tc.expectErr { + s.Require().Error(err) + } else { + s.Require().NoError(err) + _, err := s.cfg.TxConfig.TxJSONDecoder()(bz.Bytes()) + s.Require().NoError(err) + } + }) + } +} From ca7e6cd5e4504205ff7a7599b1d66cf1bd177191 Mon Sep 17 00:00:00 2001 From: 170210 Date: Wed, 22 May 2024 13:11:54 +0900 Subject: [PATCH 3/8] chore: lint fix Signed-off-by: 170210 --- x/fswap/client/testutil/query.go | 3 ++- x/fswap/client/testutil/suite.go | 3 ++- x/fswap/client/testutil/tx.go | 5 +++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/x/fswap/client/testutil/query.go b/x/fswap/client/testutil/query.go index 78bb57b6af..0c7e847ced 100644 --- a/x/fswap/client/testutil/query.go +++ b/x/fswap/client/testutil/query.go @@ -1,12 +1,13 @@ package testutil import ( + "github.com/gogo/protobuf/proto" + clitestutil "github.com/Finschia/finschia-sdk/testutil/cli" sdk "github.com/Finschia/finschia-sdk/types" "github.com/Finschia/finschia-sdk/types/query" "github.com/Finschia/finschia-sdk/x/fswap/client/cli" fswaptypes "github.com/Finschia/finschia-sdk/x/fswap/types" - "github.com/gogo/protobuf/proto" ) func (s *IntegrationTestSuite) TestCmdQuerySwapped() { diff --git a/x/fswap/client/testutil/suite.go b/x/fswap/client/testutil/suite.go index 9f59d9063a..8614267093 100644 --- a/x/fswap/client/testutil/suite.go +++ b/x/fswap/client/testutil/suite.go @@ -1,11 +1,12 @@ package testutil import ( + "github.com/stretchr/testify/suite" + "github.com/Finschia/finschia-sdk/testutil/network" sdk "github.com/Finschia/finschia-sdk/types" banktypes "github.com/Finschia/finschia-sdk/x/bank/types" fswaptypes "github.com/Finschia/finschia-sdk/x/fswap/types" - "github.com/stretchr/testify/suite" ) type IntegrationTestSuite struct { diff --git a/x/fswap/client/testutil/tx.go b/x/fswap/client/testutil/tx.go index dd22158f2a..c3633c7538 100644 --- a/x/fswap/client/testutil/tx.go +++ b/x/fswap/client/testutil/tx.go @@ -4,12 +4,13 @@ import ( "encoding/json" "fmt" + "github.com/gogo/protobuf/proto" + "github.com/Finschia/finschia-sdk/client/flags" clitestutil "github.com/Finschia/finschia-sdk/testutil/cli" sdk "github.com/Finschia/finschia-sdk/types" sdkerrors "github.com/Finschia/finschia-sdk/types/errors" "github.com/Finschia/finschia-sdk/x/fswap/client/cli" - "github.com/gogo/protobuf/proto" ) func (s *IntegrationTestSuite) TestCmdTxMsgSwap() { @@ -261,7 +262,7 @@ func (s *IntegrationTestSuite) TestMsgSetSwap() { testCases := []struct { name string - //modify func(args []string) []string + args []string expectErr bool }{ From df5fa94523cb0d9662a90b63969854a5e79dfc2c Mon Sep 17 00:00:00 2001 From: 170210 Date: Wed, 22 May 2024 13:14:05 +0900 Subject: [PATCH 4/8] chore: update CHANGLOG.md Signed-off-by: 170210 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2003eab02e..96e62f1a76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/fswap) [\#1372](https://github.com/Finschia/finschia-sdk/pull/1372) support message based proposals * (x/fswap) [\#1387](https://github.com/Finschia/finschia-sdk/pull/1387) add new Swap query to get a single swap * (x/fswap) [\#1382](https://github.com/Finschia/finschia-sdk/pull/1382) add validation & unit tests in fswap module +* (x/fswap) [\#1391](https://github.com/Finschia/finschia-sdk/pull/1391) add cli_test for fswap module ### Bug Fixes * chore(deps) [\#1141](https://github.com/Finschia/finschia-sdk/pull/1141) Bump github.com/cosmos/ledger-cosmos-go from 0.12.2 to 0.13.2 to fix ledger signing issue From 330174bcad7f68f2ee4fe2b193a02e3640c05ead Mon Sep 17 00:00:00 2001 From: 170210 Date: Wed, 22 May 2024 13:29:43 +0900 Subject: [PATCH 5/8] chore: lint fix Signed-off-by: 170210 --- x/fswap/client/testutil/query.go | 8 ++++---- x/fswap/client/testutil/suite.go | 2 ++ x/fswap/client/testutil/tx.go | 6 +++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/x/fswap/client/testutil/query.go b/x/fswap/client/testutil/query.go index 0c7e847ced..4e4004f8b5 100644 --- a/x/fswap/client/testutil/query.go +++ b/x/fswap/client/testutil/query.go @@ -14,7 +14,7 @@ func (s *IntegrationTestSuite) TestCmdQuerySwapped() { val := s.network.Validators[0] clientCtx := val.ClientCtx // avoid printing as yaml from CLI command - clientCtx.OutputFormat = "JSON" + clientCtx.OutputFormat = jsonOutputFormat fromDenom := s.cfg.BondDenom toDenom := s.toDenom.Base @@ -77,7 +77,7 @@ func (s *IntegrationTestSuite) TestCmdQueryTotalSwappableAmount() { val := s.network.Validators[0] clientCtx := val.ClientCtx // avoid printing as yaml from CLI command - clientCtx.OutputFormat = "JSON" + clientCtx.OutputFormat = jsonOutputFormat fromDenom := s.cfg.BondDenom toDenom := s.toDenom.Base @@ -139,7 +139,7 @@ func (s *IntegrationTestSuite) TestCmdQuerySwap() { val := s.network.Validators[0] clientCtx := val.ClientCtx // avoid printing as yaml from CLI command - clientCtx.OutputFormat = "JSON" + clientCtx.OutputFormat = jsonOutputFormat fromDenom := s.cfg.BondDenom toDenom := s.toDenom.Base @@ -201,7 +201,7 @@ func (s *IntegrationTestSuite) TestCmdQuerySwaps() { val := s.network.Validators[0] clientCtx := val.ClientCtx // avoid printing as yaml from CLI command - clientCtx.OutputFormat = "JSON" + clientCtx.OutputFormat = jsonOutputFormat testCases := []struct { name string diff --git a/x/fswap/client/testutil/suite.go b/x/fswap/client/testutil/suite.go index 8614267093..e3f3aab2c0 100644 --- a/x/fswap/client/testutil/suite.go +++ b/x/fswap/client/testutil/suite.go @@ -9,6 +9,8 @@ import ( fswaptypes "github.com/Finschia/finschia-sdk/x/fswap/types" ) +const jsonOutputFormat string = "JSON" + type IntegrationTestSuite struct { suite.Suite diff --git a/x/fswap/client/testutil/tx.go b/x/fswap/client/testutil/tx.go index c3633c7538..8b24bdb345 100644 --- a/x/fswap/client/testutil/tx.go +++ b/x/fswap/client/testutil/tx.go @@ -17,7 +17,7 @@ func (s *IntegrationTestSuite) TestCmdTxMsgSwap() { val := s.network.Validators[0] clientCtx := val.ClientCtx // avoid printing as yaml from CLI command - clientCtx.OutputFormat = "JSON" + clientCtx.OutputFormat = jsonOutputFormat testCases := []struct { name string @@ -138,7 +138,7 @@ func (s *IntegrationTestSuite) TestCmdTxMsgSwapAll() { val := s.network.Validators[0] clientCtx := val.ClientCtx // avoid printing as yaml from CLI command - clientCtx.OutputFormat = "JSON" + clientCtx.OutputFormat = jsonOutputFormat testCases := []struct { name string @@ -245,7 +245,7 @@ func (s *IntegrationTestSuite) TestMsgSetSwap() { val := s.network.Validators[0] clientCtx := val.ClientCtx // avoid printing as yaml from CLI command - clientCtx.OutputFormat = "JSON" + clientCtx.OutputFormat = jsonOutputFormat denomMeta := cli.ToDenomMeta{ Metadata: s.toDenom, From 4a4938c958c2059f6cd2d9328359f3204d888658 Mon Sep 17 00:00:00 2001 From: 170210 Date: Wed, 22 May 2024 17:40:41 +0900 Subject: [PATCH 6/8] test: add grpc query test Signed-off-by: 170210 --- x/fswap/client/testutil/grpc.go | 226 ++++++++++++++++++++++++++++++++ 1 file changed, 226 insertions(+) create mode 100644 x/fswap/client/testutil/grpc.go diff --git a/x/fswap/client/testutil/grpc.go b/x/fswap/client/testutil/grpc.go new file mode 100644 index 0000000000..bcd4856078 --- /dev/null +++ b/x/fswap/client/testutil/grpc.go @@ -0,0 +1,226 @@ +package testutil + +import ( + "fmt" + + "github.com/gogo/protobuf/proto" + + "github.com/Finschia/finschia-sdk/testutil" + "github.com/Finschia/finschia-sdk/testutil/rest" + sdk "github.com/Finschia/finschia-sdk/types" + grpctypes "github.com/Finschia/finschia-sdk/types/grpc" + "github.com/Finschia/finschia-sdk/types/query" + fswaptypes "github.com/Finschia/finschia-sdk/x/fswap/types" +) + +func (s *IntegrationTestSuite) TestGRPCQuerySwap() { + val := s.network.Validators[0] + baseURL := val.APIAddress + + testCases := []struct { + name string + url string + expectedErr bool + expected proto.Message + }{ + { + "test query swap with valid query string", + fmt.Sprintf("%s/lbm/fswap/v1/swap?fromDenom=stake&toDenom=dummy", baseURL), + false, + &fswaptypes.QuerySwapResponse{ + Swap: s.dummySwap, + }, + }, + { + "test query swap with not existed swap pairs", + fmt.Sprintf("%s/lbm/fswap/v1/swap?fromDenom=fake&toDenom=dummy", baseURL), + true, + &fswaptypes.QuerySwapResponse{}, + }, + { + "test query swap with nil toDenom", + fmt.Sprintf("%s/lbm/fswap/v1/swap?fromDenom=fake", baseURL), + true, + &fswaptypes.QuerySwapResponse{}, + }, + { + "test query swap with nil fromDenom", + fmt.Sprintf("%s/lbm/fswap/v1/swap?toDenom=fake", baseURL), + true, + &fswaptypes.QuerySwapResponse{}, + }, + } + + for _, tc := range testCases { + tc := tc + s.Run(tc.name, func() { + resp, err := rest.GetRequest(tc.url) + s.Require().NoError(err) + var valRes fswaptypes.QuerySwapResponse + err = val.ClientCtx.Codec.UnmarshalJSON(resp, &valRes) + + if tc.expectedErr { + s.Require().Error(err) + } else { + s.Require().NoError(err) + s.Require().Equal(tc.expected.String(), valRes.String()) + } + }) + } +} + +func (s *IntegrationTestSuite) TestGRPCQuerySwaps() { + val := s.network.Validators[0] + baseURL := val.APIAddress + + testCases := []struct { + name string + url string + expectedErr bool + expected proto.Message + }{ + { + "test query swaps", + fmt.Sprintf("%s/lbm/fswap/v1/swaps", baseURL), + false, + &fswaptypes.QuerySwapsResponse{ + Swaps: []fswaptypes.Swap{s.dummySwap}, + Pagination: &query.PageResponse{Total: 1}, + }, + }, + } + + for _, tc := range testCases { + tc := tc + s.Run(tc.name, func() { + resp, err := rest.GetRequest(tc.url) + s.Require().NoError(err) + var valRes fswaptypes.QuerySwapsResponse + err = val.ClientCtx.Codec.UnmarshalJSON(resp, &valRes) + + if tc.expectedErr { + s.Require().Error(err) + } else { + s.Require().NoError(err) + s.Require().Equal(tc.expected.String(), valRes.String()) + } + }) + } +} + +func (s *IntegrationTestSuite) TestGRPCQuerySwapped() { + val := s.network.Validators[0] + baseURL := val.APIAddress + + testCases := []struct { + name string + url string + expectedErr bool + expected proto.Message + }{ + { + "test query swapped with valid query string", + fmt.Sprintf("%s/lbm/fswap/v1/swapped?fromDenom=stake&toDenom=dummy", baseURL), + false, + &fswaptypes.QuerySwappedResponse{ + FromCoinAmount: sdk.NewCoin("stake", sdk.ZeroInt()), + ToCoinAmount: sdk.NewCoin("dummy", sdk.ZeroInt()), + }, + }, + { + "test query swapped with not existed swap pairs", + fmt.Sprintf("%s/lbm/fswap/v1/swapped?fromDenom=fake&toDenom=dummy", baseURL), + true, + &fswaptypes.QuerySwappedResponse{}, + }, + { + "test query swapped with nil toDenom", + fmt.Sprintf("%s/lbm/fswap/v1/swapped?fromDenom=fake", baseURL), + true, + &fswaptypes.QuerySwappedResponse{}, + }, + { + "test query swapped with nil fromDenom", + fmt.Sprintf("%s/lbm/fswap/v1/swapped?toDenom=fake", baseURL), + true, + &fswaptypes.QuerySwappedResponse{}, + }, + } + + for _, tc := range testCases { + tc := tc + s.Run(tc.name, func() { + resp, err := testutil.GetRequestWithHeaders(tc.url, map[string]string{ + grpctypes.GRPCBlockHeightHeader: "1", + }) + s.Require().NoError(err) + var valRes fswaptypes.QuerySwappedResponse + err = val.ClientCtx.Codec.UnmarshalJSON(resp, &valRes) + + if tc.expectedErr { + s.Require().Error(err) + } else { + s.Require().NoError(err) + s.Require().Equal(tc.expected.String(), valRes.String()) + } + }) + } +} + +func (s *IntegrationTestSuite) TestGRPCQueryTotalSwappableAmount() { + val := s.network.Validators[0] + baseURL := val.APIAddress + + testCases := []struct { + name string + url string + expectedErr bool + expected proto.Message + }{ + { + "test query total_swappable_to_coin_amount with valid query string", + fmt.Sprintf("%s/lbm/fswap/v1/total_swappable_to_coin_amount?fromDenom=stake&toDenom=dummy", baseURL), + false, + &fswaptypes.QueryTotalSwappableToCoinAmountResponse{ + SwappableAmount: sdk.NewCoin("dummy", s.dummySwap.AmountCapForToDenom), + }, + }, + { + "test query total_swappable_to_coin_amount with not existed swap pairs", + fmt.Sprintf("%s/lbm/fswap/v1/total_swappable_to_coin_amount?fromDenom=fake&toDenom=dummy", baseURL), + true, + &fswaptypes.QueryTotalSwappableToCoinAmountResponse{}, + }, + { + "test query total_swappable_to_coin_amount with nil toDenom", + fmt.Sprintf("%s/lbm/fswap/v1/total_swappable_to_coin_amount?fromDenom=fake", baseURL), + true, + &fswaptypes.QueryTotalSwappableToCoinAmountResponse{}, + }, + { + "test query total_swappable_to_coin_amount with nil fromDenom", + fmt.Sprintf("%s/lbm/fswap/v1/total_swappable_to_coin_amount?toDenom=fake", baseURL), + true, + &fswaptypes.QueryTotalSwappableToCoinAmountResponse{}, + }, + } + + for _, tc := range testCases { + tc := tc + s.Run(tc.name, func() { + resp, err := testutil.GetRequestWithHeaders(tc.url, map[string]string{ + grpctypes.GRPCBlockHeightHeader: "1", + }) + s.Require().NoError(err) + var valRes fswaptypes.QueryTotalSwappableToCoinAmountResponse + err = val.ClientCtx.Codec.UnmarshalJSON(resp, &valRes) + + if tc.expectedErr { + s.Require().Error(err) + } else { + s.Require().NoError(err) + s.Require().Equal(tc.expected.String(), valRes.String()) + } + }) + } +} From 12b1df198fa4e98fd0aa4b29ccc233093336b41e Mon Sep 17 00:00:00 2001 From: 170210 Date: Fri, 24 May 2024 13:49:20 +0900 Subject: [PATCH 7/8] fix: change fromDenom & toDenom Signed-off-by: 170210 --- x/fswap/client/testutil/grpc.go | 36 ++++++++++++++++----------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/x/fswap/client/testutil/grpc.go b/x/fswap/client/testutil/grpc.go index bcd4856078..74e81b200a 100644 --- a/x/fswap/client/testutil/grpc.go +++ b/x/fswap/client/testutil/grpc.go @@ -25,7 +25,7 @@ func (s *IntegrationTestSuite) TestGRPCQuerySwap() { }{ { "test query swap with valid query string", - fmt.Sprintf("%s/lbm/fswap/v1/swap?fromDenom=stake&toDenom=dummy", baseURL), + fmt.Sprintf("%s/lbm/fswap/v1/swap?from_denom=stake&to_denom=dummy", baseURL), false, &fswaptypes.QuerySwapResponse{ Swap: s.dummySwap, @@ -33,19 +33,19 @@ func (s *IntegrationTestSuite) TestGRPCQuerySwap() { }, { "test query swap with not existed swap pairs", - fmt.Sprintf("%s/lbm/fswap/v1/swap?fromDenom=fake&toDenom=dummy", baseURL), + fmt.Sprintf("%s/lbm/fswap/v1/swap?from_denom=fake&to_denom=dummy", baseURL), true, &fswaptypes.QuerySwapResponse{}, }, { - "test query swap with nil toDenom", - fmt.Sprintf("%s/lbm/fswap/v1/swap?fromDenom=fake", baseURL), + "test query swap with nil to_denom", + fmt.Sprintf("%s/lbm/fswap/v1/swap?from_denom=stake", baseURL), true, &fswaptypes.QuerySwapResponse{}, }, { - "test query swap with nil fromDenom", - fmt.Sprintf("%s/lbm/fswap/v1/swap?toDenom=fake", baseURL), + "test query swap with nil from_denom", + fmt.Sprintf("%s/lbm/fswap/v1/swap?to_denom=dummy", baseURL), true, &fswaptypes.QuerySwapResponse{}, }, @@ -120,7 +120,7 @@ func (s *IntegrationTestSuite) TestGRPCQuerySwapped() { }{ { "test query swapped with valid query string", - fmt.Sprintf("%s/lbm/fswap/v1/swapped?fromDenom=stake&toDenom=dummy", baseURL), + fmt.Sprintf("%s/lbm/fswap/v1/swapped?from_denom=stake&to_denom=dummy", baseURL), false, &fswaptypes.QuerySwappedResponse{ FromCoinAmount: sdk.NewCoin("stake", sdk.ZeroInt()), @@ -129,19 +129,19 @@ func (s *IntegrationTestSuite) TestGRPCQuerySwapped() { }, { "test query swapped with not existed swap pairs", - fmt.Sprintf("%s/lbm/fswap/v1/swapped?fromDenom=fake&toDenom=dummy", baseURL), + fmt.Sprintf("%s/lbm/fswap/v1/swapped?from_denom=fake&to_denom=dummy", baseURL), true, &fswaptypes.QuerySwappedResponse{}, }, { - "test query swapped with nil toDenom", - fmt.Sprintf("%s/lbm/fswap/v1/swapped?fromDenom=fake", baseURL), + "test query swapped with nil to_denom", + fmt.Sprintf("%s/lbm/fswap/v1/swapped?from_denom=stake", baseURL), true, &fswaptypes.QuerySwappedResponse{}, }, { - "test query swapped with nil fromDenom", - fmt.Sprintf("%s/lbm/fswap/v1/swapped?toDenom=fake", baseURL), + "test query swapped with nil from_denom", + fmt.Sprintf("%s/lbm/fswap/v1/swapped?to_denom=dummy", baseURL), true, &fswaptypes.QuerySwappedResponse{}, }, @@ -179,7 +179,7 @@ func (s *IntegrationTestSuite) TestGRPCQueryTotalSwappableAmount() { }{ { "test query total_swappable_to_coin_amount with valid query string", - fmt.Sprintf("%s/lbm/fswap/v1/total_swappable_to_coin_amount?fromDenom=stake&toDenom=dummy", baseURL), + fmt.Sprintf("%s/lbm/fswap/v1/total_swappable_to_coin_amount?from_denom=stake&to_denom=dummy", baseURL), false, &fswaptypes.QueryTotalSwappableToCoinAmountResponse{ SwappableAmount: sdk.NewCoin("dummy", s.dummySwap.AmountCapForToDenom), @@ -187,19 +187,19 @@ func (s *IntegrationTestSuite) TestGRPCQueryTotalSwappableAmount() { }, { "test query total_swappable_to_coin_amount with not existed swap pairs", - fmt.Sprintf("%s/lbm/fswap/v1/total_swappable_to_coin_amount?fromDenom=fake&toDenom=dummy", baseURL), + fmt.Sprintf("%s/lbm/fswap/v1/total_swappable_to_coin_amount?from_denom=fake&to_denom=dummy", baseURL), true, &fswaptypes.QueryTotalSwappableToCoinAmountResponse{}, }, { - "test query total_swappable_to_coin_amount with nil toDenom", - fmt.Sprintf("%s/lbm/fswap/v1/total_swappable_to_coin_amount?fromDenom=fake", baseURL), + "test query total_swappable_to_coin_amount with nil to_denom", + fmt.Sprintf("%s/lbm/fswap/v1/total_swappable_to_coin_amount?from_denom=stake", baseURL), true, &fswaptypes.QueryTotalSwappableToCoinAmountResponse{}, }, { - "test query total_swappable_to_coin_amount with nil fromDenom", - fmt.Sprintf("%s/lbm/fswap/v1/total_swappable_to_coin_amount?toDenom=fake", baseURL), + "test query total_swappable_to_coin_amount with nil from_denom", + fmt.Sprintf("%s/lbm/fswap/v1/total_swappable_to_coin_amount?to_denom=dummy", baseURL), true, &fswaptypes.QueryTotalSwappableToCoinAmountResponse{}, }, From e4a51f9ed79cdabd1e03dfb2e001d43e566a26df Mon Sep 17 00:00:00 2001 From: 170210 Date: Fri, 24 May 2024 14:03:55 +0900 Subject: [PATCH 8/8] fix: fix for comment Signed-off-by: 170210 --- x/fswap/client/cli/tx.go | 9 ++- x/fswap/client/testutil/tx.go | 130 +++++++--------------------------- 2 files changed, 28 insertions(+), 111 deletions(-) diff --git a/x/fswap/client/cli/tx.go b/x/fswap/client/cli/tx.go index b92f326b99..61cd359d75 100644 --- a/x/fswap/client/cli/tx.go +++ b/x/fswap/client/cli/tx.go @@ -22,10 +22,6 @@ const ( FlagSwapRate = "swap-rate" ) -type ToDenomMeta struct { - Metadata bank.Metadata `json:"metadata"` -} - // GetTxCmd returns the transaction commands for this module func GetTxCmd() *cobra.Command { cmd := &cobra.Command{ @@ -236,7 +232,10 @@ func validateGenerateOnly(cmd *cobra.Command) error { } func parseToDenomMetadata(jsonDenomMetadata string) (bank.Metadata, error) { - denomMeta := ToDenomMeta{} + type toDenomMeta struct { + Metadata bank.Metadata `json:"metadata"` + } + denomMeta := toDenomMeta{} if err := json.Unmarshal([]byte(jsonDenomMetadata), &denomMeta); err != nil { return bank.Metadata{}, err } diff --git a/x/fswap/client/testutil/tx.go b/x/fswap/client/testutil/tx.go index 8b24bdb345..31fd363446 100644 --- a/x/fswap/client/testutil/tx.go +++ b/x/fswap/client/testutil/tx.go @@ -9,7 +9,7 @@ import ( "github.com/Finschia/finschia-sdk/client/flags" clitestutil "github.com/Finschia/finschia-sdk/testutil/cli" sdk "github.com/Finschia/finschia-sdk/types" - sdkerrors "github.com/Finschia/finschia-sdk/types/errors" + banktypes "github.com/Finschia/finschia-sdk/x/bank/types" "github.com/Finschia/finschia-sdk/x/fswap/client/cli" ) @@ -18,13 +18,17 @@ func (s *IntegrationTestSuite) TestCmdTxMsgSwap() { clientCtx := val.ClientCtx // avoid printing as yaml from CLI command clientCtx.OutputFormat = jsonOutputFormat + commonArgs := []string{ + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), + } testCases := []struct { - name string - args []string - expectErr bool - expectedCode uint32 - respType proto.Message + name string + args []string + expectErr bool + respType proto.Message }{ { "valid transaction", @@ -32,12 +36,8 @@ func (s *IntegrationTestSuite) TestCmdTxMsgSwap() { val.Address.String(), sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)).String(), s.toDenom.Base, - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), }, false, - 0, &sdk.TxResponse{}, }, { @@ -47,12 +47,8 @@ func (s *IntegrationTestSuite) TestCmdTxMsgSwap() { sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)).String(), s.toDenom.Base, "extra", - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(1))).String()), }, true, - 0, nil, }, { @@ -61,12 +57,8 @@ func (s *IntegrationTestSuite) TestCmdTxMsgSwap() { "invalidAddress", sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)).String(), s.toDenom.Base, - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(1))).String()), }, true, - 0, nil, }, { @@ -75,43 +67,10 @@ func (s *IntegrationTestSuite) TestCmdTxMsgSwap() { val.Address.String(), "", s.toDenom.Base, - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(1))).String()), }, true, - 0, nil, }, - { - "not enough fees", - []string{ - val.Address.String(), - sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)).String(), - s.toDenom.Base, - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(1))).String()), - }, - false, - sdkerrors.ErrInsufficientFee.ABCICode(), - &sdk.TxResponse{}, - }, - { - "not enough gas", - []string{ - val.Address.String(), - sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)).String(), - s.toDenom.Base, - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), - "--gas=10", - }, - false, - sdkerrors.ErrOutOfGas.ABCICode(), - &sdk.TxResponse{}, - }, } for _, tc := range testCases { @@ -119,16 +78,13 @@ func (s *IntegrationTestSuite) TestCmdTxMsgSwap() { s.Run(tc.name, func() { cmd := cli.CmdTxMsgSwap() - bz, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) + bz, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, append(tc.args, commonArgs...)) if tc.expectErr { s.Require().Error(err) } else { s.Require().NoError(err) - s.Require().NoError(clientCtx.Codec.UnmarshalJSON(bz.Bytes(), tc.respType), bz.String()) - txResp := tc.respType.(*sdk.TxResponse) - s.Require().Equal(tc.expectedCode, txResp.Code) } }) } @@ -139,13 +95,17 @@ func (s *IntegrationTestSuite) TestCmdTxMsgSwapAll() { clientCtx := val.ClientCtx // avoid printing as yaml from CLI command clientCtx.OutputFormat = jsonOutputFormat + commonArgs := []string{ + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), + } testCases := []struct { - name string - args []string - expectErr bool - expectedCode uint32 - respType proto.Message + name string + args []string + expectErr bool + respType proto.Message }{ { "valid transaction", @@ -153,12 +113,8 @@ func (s *IntegrationTestSuite) TestCmdTxMsgSwapAll() { val.Address.String(), s.cfg.BondDenom, s.toDenom.Base, - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), }, false, - 0, &sdk.TxResponse{}, }, { @@ -168,12 +124,8 @@ func (s *IntegrationTestSuite) TestCmdTxMsgSwapAll() { s.cfg.BondDenom, s.toDenom.Base, "extra", - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(1))).String()), }, true, - 0, nil, }, { @@ -182,43 +134,10 @@ func (s *IntegrationTestSuite) TestCmdTxMsgSwapAll() { "invalidAddress", s.cfg.BondDenom, s.toDenom.Base, - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(1))).String()), }, true, - 0, nil, }, - { - "not enough fees", - []string{ - val.Address.String(), - s.cfg.BondDenom, - s.toDenom.Base, - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(1))).String()), - }, - false, - sdkerrors.ErrInsufficientFee.ABCICode(), - &sdk.TxResponse{}, - }, - { - "not enough gas", - []string{ - val.Address.String(), - s.cfg.BondDenom, - s.toDenom.Base, - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), - "--gas=10", - }, - false, - sdkerrors.ErrOutOfGas.ABCICode(), - &sdk.TxResponse{}, - }, } for _, tc := range testCases { @@ -226,16 +145,13 @@ func (s *IntegrationTestSuite) TestCmdTxMsgSwapAll() { s.Run(tc.name, func() { cmd := cli.CmdTxMsgSwapAll() - bz, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) + bz, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, append(tc.args, commonArgs...)) if tc.expectErr { s.Require().Error(err) } else { s.Require().NoError(err) - s.Require().NoError(clientCtx.Codec.UnmarshalJSON(bz.Bytes(), tc.respType), bz.String()) - txResp := tc.respType.(*sdk.TxResponse) - s.Require().Equal(tc.expectedCode, txResp.Code) } }) } @@ -247,7 +163,9 @@ func (s *IntegrationTestSuite) TestMsgSetSwap() { // avoid printing as yaml from CLI command clientCtx.OutputFormat = jsonOutputFormat - denomMeta := cli.ToDenomMeta{ + denomMeta := struct { + Metadata banktypes.Metadata `json:"metadata"` + }{ Metadata: s.toDenom, } jsonBytes, err := json.Marshal(denomMeta)