Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Application] Scaffolded the Application type (without CRUD messages) and nothing else #47

Merged
merged 3 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions proto/pocket/application/application.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";
package pocket.application;

option go_package = "pocket/x/application/types";

message Application {
string address = 1;

}

6 changes: 5 additions & 1 deletion proto/pocket/application/genesis.proto
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
syntax = "proto3";

package pocket.application;

import "gogoproto/gogo.proto";
import "pocket/application/params.proto";
import "pocket/application/application.proto";

option go_package = "pocket/x/application/types";

// GenesisState defines the application module's genesis state.
message GenesisState {
Params params = 1 [(gogoproto.nullable) = false];
Params params = 1 [(gogoproto.nullable) = false];
repeated Application applicationList = 2 [(gogoproto.nullable) = false];
}

38 changes: 35 additions & 3 deletions proto/pocket/application/query.proto
Original file line number Diff line number Diff line change
@@ -1,26 +1,58 @@
syntax = "proto3";

package pocket.application;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "pocket/application/params.proto";
import "pocket/application/application.proto";

option go_package = "pocket/x/application/types";

// Query defines the gRPC querier service.
service Query {

// Parameters queries the parameters of the module.
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
rpc Params (QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/pocket/application/params";

}

// Queries a list of Application items.
rpc Application (QueryGetApplicationRequest) returns (QueryGetApplicationResponse) {
option (google.api.http).get = "/pocket/application/application/{address}";

}
rpc ApplicationAll (QueryAllApplicationRequest) returns (QueryAllApplicationResponse) {
option (google.api.http).get = "/pocket/application/application";

}
}

// QueryParamsRequest is request type for the Query/Params RPC method.
message QueryParamsRequest {}

// QueryParamsResponse is response type for the Query/Params RPC method.
message QueryParamsResponse {

// params holds all the parameters of this module.
Params params = 1 [(gogoproto.nullable) = false];
}
}

message QueryGetApplicationRequest {
string address = 1;
}

message QueryGetApplicationResponse {
Application application = 1 [(gogoproto.nullable) = false];
}

message QueryAllApplicationRequest {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

message QueryAllApplicationResponse {
repeated Application application = 1 [(gogoproto.nullable) = false];
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

2 changes: 2 additions & 0 deletions x/application/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
}

cmd.AddCommand(CmdQueryParams())
cmd.AddCommand(CmdListApplication())
cmd.AddCommand(CmdShowApplication())
// this line is used by starport scaffolding # 1

return cmd
Expand Down
78 changes: 78 additions & 0 deletions x/application/client/cli/query_application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package cli

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"

"pocket/x/application/types"
)

func CmdListApplication() *cobra.Command {
cmd := &cobra.Command{
Use: "list-application",
Short: "list all application",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryAllApplicationRequest{
Pagination: pageReq,
}

res, err := queryClient.ApplicationAll(cmd.Context(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddPaginationFlagsToCmd(cmd, cmd.Use)
flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func CmdShowApplication() *cobra.Command {
cmd := &cobra.Command{
Use: "show-application [address]",
Short: "shows a application",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

argAddress := args[0]

params := &types.QueryGetApplicationRequest{
Address: argAddress,
}

res, err := queryClient.Application(cmd.Context(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
160 changes: 160 additions & 0 deletions x/application/client/cli/query_application_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package cli_test

import (
"fmt"
"strconv"
"testing"

tmcli "github.com/cometbft/cometbft/libs/cli"
"github.com/cosmos/cosmos-sdk/client/flags"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"pocket/testutil/network"
"pocket/testutil/nullify"
"pocket/x/application/client/cli"
"pocket/x/application/types"
)

// Prevent strconv unused error
var _ = strconv.IntSize

func networkWithApplicationObjects(t *testing.T, n int) (*network.Network, []types.Application) {
t.Helper()
cfg := network.DefaultConfig()
state := types.GenesisState{}
for i := 0; i < n; i++ {
application := types.Application{
Address: strconv.Itoa(i),
}
nullify.Fill(&application)
state.ApplicationList = append(state.ApplicationList, application)
}
buf, err := cfg.Codec.MarshalJSON(&state)
require.NoError(t, err)
cfg.GenesisState[types.ModuleName] = buf
return network.New(t, cfg), state.ApplicationList
}

func TestShowApplication(t *testing.T) {
net, objs := networkWithApplicationObjects(t, 2)

ctx := net.Validators[0].ClientCtx
common := []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
}
tests := []struct {
desc string
idAddress string

args []string
err error
obj types.Application
}{
{
desc: "found",
idAddress: objs[0].Address,

args: common,
obj: objs[0],
},
{
desc: "not found",
idAddress: strconv.Itoa(100000),

args: common,
err: status.Error(codes.NotFound, "not found"),
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
args := []string{
tc.idAddress,
}
args = append(args, tc.args...)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowApplication(), 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.QueryGetApplicationResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.Application)
require.Equal(t,
nullify.Fill(&tc.obj),
nullify.Fill(&resp.Application),
)
}
})
}
}

func TestListApplication(t *testing.T) {
net, objs := networkWithApplicationObjects(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.CmdListApplication(), args)
require.NoError(t, err)
var resp types.QueryAllApplicationResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.LessOrEqual(t, len(resp.Application), step)
require.Subset(t,
nullify.Fill(objs),
nullify.Fill(resp.Application),
)
}
})
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.CmdListApplication(), args)
require.NoError(t, err)
var resp types.QueryAllApplicationResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.LessOrEqual(t, len(resp.Application), step)
require.Subset(t,
nullify.Fill(objs),
nullify.Fill(resp.Application),
)
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.CmdListApplication(), args)
require.NoError(t, err)
var resp types.QueryAllApplicationResponse
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.Application),
)
})
}
5 changes: 5 additions & 0 deletions x/application/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (

// InitGenesis initializes the module's state from a provided genesis state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {
// Set all the application
for _, elem := range genState.ApplicationList {
k.SetApplication(ctx, elem)
}
// this line is used by starport scaffolding # genesis/module/init
k.SetParams(ctx, genState.Params)
}
Expand All @@ -17,6 +21,7 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
genesis := types.DefaultGenesis()
genesis.Params = k.GetParams(ctx)

genesis.ApplicationList = k.GetAllApplication(ctx)
// this line is used by starport scaffolding # genesis/module/export

return genesis
Expand Down
9 changes: 9 additions & 0 deletions x/application/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ func TestGenesis(t *testing.T) {
genesisState := types.GenesisState{
Params: types.DefaultParams(),

ApplicationList: []types.Application{
{
Address: "0",
},
{
Address: "1",
},
},
// this line is used by starport scaffolding # genesis/test/state
}

Expand All @@ -25,5 +33,6 @@ func TestGenesis(t *testing.T) {
nullify.Fill(&genesisState)
nullify.Fill(got)

require.ElementsMatch(t, genesisState.ApplicationList, got.ApplicationList)
// this line is used by starport scaffolding # genesis/test/assert
}
Loading