Add tests for bond module gRPC requests
All checks were successful
Integration Tests / test-integration (pull_request) Successful in 2m2s

This commit is contained in:
Prathamesh Musale 2024-03-02 11:56:29 +05:30
parent cc6f2070b5
commit 19da88254e
4 changed files with 228 additions and 3 deletions

View File

@ -22,3 +22,162 @@ func (ets *E2ETestSuite) TestGRPCGetParams() {
sr.NoError(err)
sr.Equal(params.GetParams().MaxBondAmount, bondtypes.DefaultParams().MaxBondAmount)
}
func (ets *E2ETestSuite) TestGRPCGetBonds() {
val := ets.network.Validators[0]
sr := ets.Require()
reqURL := fmt.Sprintf("%s/cerc/bond/v1/bonds", val.APIAddress)
testCases := []struct {
name string
url string
expErr bool
errorMsg string
preRun func() string
}{
{
"invalid request with headers",
reqURL + "asdasdas",
true,
"",
func() string { return "" },
},
{
"valid request",
reqURL,
false,
"",
func() string { return ets.createBond() },
},
}
for _, tc := range testCases {
ets.Run(tc.name, func() {
tc.preRun()
resp, _ := testutil.GetRequest(tc.url)
if tc.expErr {
sr.Contains(string(resp), tc.errorMsg)
} else {
var response bondtypes.QueryGetBondsResponse
err := val.ClientCtx.Codec.UnmarshalJSON(resp, &response)
sr.NoError(err)
sr.NotZero(len(response.GetBonds()))
}
})
}
}
func (ets *E2ETestSuite) TestGRPCGetBondsByOwner() {
val := ets.network.Validators[0]
sr := ets.Require()
reqURL := val.APIAddress + "/cerc/bond/v1/by-owner/%s"
testCases := []struct {
name string
url string
expErr bool
preRun func() string
}{
{
"empty list",
fmt.Sprintf(reqURL, "asdasd"),
true,
func() string { return "" },
},
{
"valid request",
fmt.Sprintf(reqURL, ets.accountAddress),
false,
func() string { return ets.createBond() },
},
}
for _, tc := range testCases {
ets.Run(tc.name, func() {
tc.preRun()
resp, err := testutil.GetRequest(tc.url)
ets.Require().NoError(err)
var bonds bondtypes.QueryGetBondsByOwnerResponse
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &bonds)
sr.NoError(err)
if tc.expErr {
sr.Empty(bonds.GetBonds())
} else {
bondsList := bonds.GetBonds()
sr.NotZero(len(bondsList))
sr.Equal(ets.accountAddress, bondsList[0].GetOwner())
}
})
}
}
func (ets *E2ETestSuite) TestGRPCGetBondByID() {
val := ets.network.Validators[0]
sr := ets.Require()
reqURL := val.APIAddress + "/cerc/bond/v1/bonds/%s"
testCases := []struct {
name string
url string
expErr bool
preRun func() string
}{
{
"invalid request",
fmt.Sprintf(reqURL, "asdadad"),
true,
func() string { return "" },
},
{
"valid request",
reqURL,
false,
func() string { return ets.createBond() },
},
}
for _, tc := range testCases {
ets.Run(tc.name, func() {
var bondID string
if !tc.expErr {
bondID = tc.preRun()
tc.url = fmt.Sprintf(reqURL, bondID)
}
resp, err := testutil.GetRequest(tc.url)
ets.Require().NoError(err)
var bonds bondtypes.QueryGetBondByIdResponse
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &bonds)
if tc.expErr {
sr.Empty(bonds.GetBond().GetId())
} else {
sr.NoError(err)
sr.NotZero(bonds.GetBond().GetId())
sr.Equal(bonds.GetBond().GetId(), bondID)
}
})
}
}
func (ets *E2ETestSuite) TestGRPCGetBondModuleBalance() {
val := ets.network.Validators[0]
sr := ets.Require()
reqURL := fmt.Sprintf("%s/cerc/bond/v1/balance", val.APIAddress)
// creating the bond
ets.createBond()
ets.Run("valid request", func() {
resp, err := testutil.GetRequest(reqURL)
sr.NoError(err)
var response bondtypes.QueryGetBondModuleBalanceResponse
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &response)
sr.NoError(err)
sr.False(response.GetBalance().IsZero())
})
}

View File

@ -14,6 +14,7 @@ import (
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
bondtypes "git.vdb.to/cerc-io/laconic2d/x/bond"
"git.vdb.to/cerc-io/laconic2d/x/bond/client/cli"
)
@ -81,7 +82,7 @@ func (ets *E2ETestSuite) createAccountWithBalance(accountName string, accountAdd
sr.NoError(err)
}
func (ets *E2ETestSuite) createBond() {
func (ets *E2ETestSuite) createBond() string {
val := ets.network.Validators[0]
sr := ets.Require()
createBondCmd := cli.NewCreateBondCmd()
@ -103,4 +104,20 @@ func (ets *E2ETestSuite) createBond() {
// wait for tx to take effect
err = ets.network.WaitForNextBlock()
sr.NoError(err)
// getting the bonds list and returning the bond-id
clientCtx := val.ClientCtx
cmd := cli.GetQueryBondLists()
args = []string{
fmt.Sprintf("--%s=json", flags.FlagOutput),
}
out, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
sr.NoError(err)
var queryResponse bondtypes.QueryGetBondsResponse
err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &queryResponse)
sr.NoError(err)
// extract bond id from bonds list
bond := queryResponse.GetBonds()[0]
return bond.GetId()
}

View File

@ -0,0 +1,48 @@
package cli
import (
"fmt"
"strings"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/version"
"github.com/spf13/cobra"
bondtypes "git.vdb.to/cerc-io/laconic2d/x/bond"
)
// GetQueryBondLists implements the bond lists query command.
func GetQueryBondLists() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List bonds.",
Long: strings.TrimSpace(
fmt.Sprintf(`Get bond list .
Example:
$ %s query %s list
`,
version.AppName, bondtypes.ModuleName,
),
),
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := bondtypes.NewQueryClient(clientCtx)
res, err := queryClient.Bonds(cmd.Context(), &bondtypes.QueryGetBondsRequest{})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

View File

@ -26,7 +26,8 @@ func (ms msgServer) CreateBond(c context.Context, msg *bond.MsgCreateBond) (*bon
if err != nil {
return nil, err
}
_, err = ms.k.CreateBond(ctx, signerAddress, msg.Coins)
resp, err := ms.k.CreateBond(ctx, signerAddress, msg.Coins)
if err != nil {
return nil, err
}
@ -44,7 +45,7 @@ func (ms msgServer) CreateBond(c context.Context, msg *bond.MsgCreateBond) (*bon
),
})
return &bond.MsgCreateBondResponse{}, nil
return &bond.MsgCreateBondResponse{Id: resp.Id}, nil
}
// RefillBond implements bond.MsgServer.