scaffold base auction & nextAuction types

This commit is contained in:
2022-08-25 18:49:35 -07:00
parent c829ab63fc
commit 6ff5b7fb7f
37 changed files with 4747 additions and 38 deletions

View File

@@ -25,6 +25,9 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
}
cmd.AddCommand(CmdQueryParams())
cmd.AddCommand(CmdShowNextAuction())
cmd.AddCommand(CmdListAuction())
cmd.AddCommand(CmdShowAuction())
// this line is used by starport scaffolding # 1
return cmd

View File

@@ -0,0 +1,73 @@
package cli
import (
"context"
"cosmos-test/x/cosmostest/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"
)
func CmdListAuction() *cobra.Command {
cmd := &cobra.Command{
Use: "list-auction",
Short: "list all auction",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
params := &types.QueryAllAuctionRequest{
Pagination: pageReq,
}
res, err := queryClient.AuctionAll(context.Background(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddPaginationFlagsToCmd(cmd, cmd.Use)
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
func CmdShowAuction() *cobra.Command {
cmd := &cobra.Command{
Use: "show-auction [index]",
Short: "shows a auction",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx := client.GetClientContextFromCmd(cmd)
queryClient := types.NewQueryClient(clientCtx)
argIndex := args[0]
params := &types.QueryGetAuctionRequest{
Index: argIndex,
}
res, err := queryClient.Auction(context.Background(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

View File

@@ -0,0 +1,161 @@
package cli_test
import (
"fmt"
"strconv"
"testing"
"github.com/cosmos/cosmos-sdk/client/flags"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/stretchr/testify/require"
tmcli "github.com/tendermint/tendermint/libs/cli"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"cosmos-test/testutil/network"
"cosmos-test/testutil/nullify"
"cosmos-test/x/cosmostest/client/cli"
"cosmos-test/x/cosmostest/types"
)
// Prevent strconv unused error
var _ = strconv.IntSize
func networkWithAuctionObjects(t *testing.T, n int) (*network.Network, []types.Auction) {
t.Helper()
cfg := network.DefaultConfig()
state := types.GenesisState{}
require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state))
for i := 0; i < n; i++ {
auction := types.Auction{
Index: strconv.Itoa(i),
}
nullify.Fill(&auction)
state.AuctionList = append(state.AuctionList, auction)
}
buf, err := cfg.Codec.MarshalJSON(&state)
require.NoError(t, err)
cfg.GenesisState[types.ModuleName] = buf
return network.New(t, cfg), state.AuctionList
}
func TestShowAuction(t *testing.T) {
net, objs := networkWithAuctionObjects(t, 2)
ctx := net.Validators[0].ClientCtx
common := []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
}
for _, tc := range []struct {
desc string
idIndex string
args []string
err error
obj types.Auction
}{
{
desc: "found",
idIndex: objs[0].Index,
args: common,
obj: objs[0],
},
{
desc: "not found",
idIndex: strconv.Itoa(100000),
args: common,
err: status.Error(codes.NotFound, "not found"),
},
} {
t.Run(tc.desc, func(t *testing.T) {
args := []string{
tc.idIndex,
}
args = append(args, tc.args...)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowAuction(), 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.QueryGetAuctionResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.Auction)
require.Equal(t,
nullify.Fill(&tc.obj),
nullify.Fill(&resp.Auction),
)
}
})
}
}
func TestListAuction(t *testing.T) {
net, objs := networkWithAuctionObjects(t, 5)
ctx := net.Validators[0].ClientCtx
request := func(next []byte, offset, limit uint64, total bool) []string {
args := []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
}
if next == nil {
args = append(args, fmt.Sprintf("--%s=%d", flags.FlagOffset, offset))
} else {
args = append(args, fmt.Sprintf("--%s=%s", flags.FlagPageKey, next))
}
args = append(args, fmt.Sprintf("--%s=%d", flags.FlagLimit, limit))
if total {
args = append(args, fmt.Sprintf("--%s", flags.FlagCountTotal))
}
return args
}
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.CmdListAuction(), args)
require.NoError(t, err)
var resp types.QueryAllAuctionResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.LessOrEqual(t, len(resp.Auction), step)
require.Subset(t,
nullify.Fill(objs),
nullify.Fill(resp.Auction),
)
}
})
t.Run("ByKey", func(t *testing.T) {
step := 2
var next []byte
for i := 0; i < len(objs); i += step {
args := request(next, 0, uint64(step), false)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListAuction(), args)
require.NoError(t, err)
var resp types.QueryAllAuctionResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.LessOrEqual(t, len(resp.Auction), step)
require.Subset(t,
nullify.Fill(objs),
nullify.Fill(resp.Auction),
)
next = resp.Pagination.NextKey
}
})
t.Run("Total", func(t *testing.T) {
args := request(nil, 0, uint64(len(objs)), true)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListAuction(), args)
require.NoError(t, err)
var resp types.QueryAllAuctionResponse
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,
nullify.Fill(objs),
nullify.Fill(resp.Auction),
)
})
}

View File

@@ -0,0 +1,36 @@
package cli
import (
"context"
"cosmos-test/x/cosmostest/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"
)
func CmdShowNextAuction() *cobra.Command {
cmd := &cobra.Command{
Use: "show-next-auction",
Short: "shows nextAuction",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
queryClient := types.NewQueryClient(clientCtx)
params := &types.QueryGetNextAuctionRequest{}
res, err := queryClient.NextAuction(context.Background(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

View File

@@ -0,0 +1,72 @@
package cli_test
import (
"fmt"
"testing"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/stretchr/testify/require"
tmcli "github.com/tendermint/tendermint/libs/cli"
"google.golang.org/grpc/status"
"cosmos-test/testutil/network"
"cosmos-test/testutil/nullify"
"cosmos-test/x/cosmostest/client/cli"
"cosmos-test/x/cosmostest/types"
)
func networkWithNextAuctionObjects(t *testing.T) (*network.Network, types.NextAuction) {
t.Helper()
cfg := network.DefaultConfig()
state := types.GenesisState{}
require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state))
nextAuction := &types.NextAuction{}
nullify.Fill(&nextAuction)
state.NextAuction = nextAuction
buf, err := cfg.Codec.MarshalJSON(&state)
require.NoError(t, err)
cfg.GenesisState[types.ModuleName] = buf
return network.New(t, cfg), *state.NextAuction
}
func TestShowNextAuction(t *testing.T) {
net, obj := networkWithNextAuctionObjects(t)
ctx := net.Validators[0].ClientCtx
common := []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
}
for _, tc := range []struct {
desc string
args []string
err error
obj types.NextAuction
}{
{
desc: "get",
args: common,
obj: obj,
},
} {
t.Run(tc.desc, func(t *testing.T) {
var args []string
args = append(args, tc.args...)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowNextAuction(), 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.QueryGetNextAuctionResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.NextAuction)
require.Equal(t,
nullify.Fill(&tc.obj),
nullify.Fill(&resp.NextAuction),
)
}
})
}
}