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

test(campaign): add tests for CampaignSummary #766

Merged
merged 4 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 2 additions & 5 deletions x/campaign/client/cli/query_campaign_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ func CmdListCampaignSummary() *cobra.Command {
Short: "List information summarizing all campaigns",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) (err error) {

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

Expand All @@ -41,6 +37,7 @@ func CmdListCampaignSummary() *cobra.Command {
},
}

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

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

import (
"fmt"
"strconv"
"testing"

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

"github.com/tendermint/spn/testutil/network"
"github.com/tendermint/spn/x/campaign/client/cli"
"github.com/tendermint/spn/x/campaign/types"
launchtypes "github.com/tendermint/spn/x/launch/types"
)

func networkWithCampaignSummariesObjects(t *testing.T, n int) (*network.Network, []types.CampaignSummary) {
t.Helper()
cfg := network.DefaultConfig()
campaignState := types.GenesisState{}
require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &campaignState))
chainState := launchtypes.GenesisState{}
require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[launchtypes.ModuleName], &chainState))
objs := make([]types.CampaignSummary, 0)

for i := 0; i < n; i++ {
campaign := types.Campaign{
CampaignID: uint64(i),
TotalSupply: sdk.NewCoins(),
AllocatedShares: types.Shares(sdk.NewCoins()),
}
campaignState.CampaignChainsList = append(campaignState.CampaignChainsList, types.CampaignChains{
CampaignID: uint64(i),
Chains: []uint64{uint64(i)},
})
campaignState.CampaignList = append(campaignState.CampaignList, campaign)
chainState.ChainList = append(chainState.ChainList, launchtypes.Chain{
LaunchID: uint64(i),
HasCampaign: true,
CampaignID: uint64(i),
})
chainState.ChainCounter += 1

objs = append(objs, types.CampaignSummary{
Campaign: campaign,
HasMostRecentChain: true,
MostRecentChain: types.MostRecentChain{
LaunchID: uint64(i),
},
Rewards: sdk.NewCoins(),
PreviousRewards: sdk.NewCoins(),
})
}
buf, err := cfg.Codec.MarshalJSON(&campaignState)
require.NoError(t, err)
cfg.GenesisState[types.ModuleName] = buf
buf, err = cfg.Codec.MarshalJSON(&chainState)
require.NoError(t, err)
cfg.GenesisState[launchtypes.ModuleName] = buf
return network.New(t, cfg), objs
}

func TestListCampaignSummary(t *testing.T) {
net, objs := networkWithCampaignSummariesObjects(t, 1)

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.CmdListCampaignSummary(), args)
require.NoError(t, err)
var resp types.QueryCampaignSummariesResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.LessOrEqual(t, len(resp.CampaignSummaries), step)
require.Subset(t, objs, resp.CampaignSummaries)
}
})
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.CmdListCampaignSummary(), args)
require.NoError(t, err)
var resp types.QueryCampaignSummariesResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.LessOrEqual(t, len(resp.CampaignSummaries), step)
require.Subset(t, objs, resp.CampaignSummaries)
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.CmdListCampaignSummary(), args)
require.NoError(t, err)
var resp types.QueryCampaignSummariesResponse
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, objs, resp.CampaignSummaries)
})
}

func TestShowCampaignSummary(t *testing.T) {
net, objs := networkWithCampaignSummariesObjects(t, 2)

ctx := net.Validators[0].ClientCtx
common := []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
}
for _, tc := range []struct {
desc string
idCampaignID uint64

args []string
err error
obj types.CampaignSummary
}{
{
desc: "found",
idCampaignID: objs[0].Campaign.CampaignID,

args: common,
obj: objs[0],
},
{
desc: "not found",
idCampaignID: 100000,

args: common,
err: status.Error(codes.NotFound, "not found"),
},
} {
tc := tc
t.Run(tc.desc, func(t *testing.T) {
args := []string{
strconv.Itoa(int(tc.idCampaignID)),
}
args = append(args, tc.args...)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowCampaignSummary(), 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.QueryCampaignSummaryResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))

}
})
}
}
10 changes: 3 additions & 7 deletions x/campaign/keeper/grpc_campaign_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package keeper

import (
"context"
giunatale marked this conversation as resolved.
Show resolved Hide resolved
"fmt"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -23,7 +20,7 @@ func (k Keeper) CampaignSummary(goCtx context.Context, req *types.QueryCampaignS

campaign, found := k.GetCampaign(ctx, req.CampaignID)
if !found {
return nil, sdkerrors.ErrKeyNotFound
return nil, status.Error(codes.NotFound, "not found")
}
campaignSummary, err := k.GetCampaignSummary(ctx, campaign)

Expand Down Expand Up @@ -69,13 +66,12 @@ func (k Keeper) CampaignSummaries(goCtx context.Context, req *types.QueryCampaig
}

// GetCampaignSummary returns the campaign with summary attached to it like most recent chain and rewards attached to it
// TODO: add tests https://github.com/tendermint/spn/issues/650
func (k Keeper) GetCampaignSummary(ctx sdk.Context, campaign types.Campaign) (cs types.CampaignSummary, err error) {
cs.Campaign = campaign

campaignChains, found := k.GetCampaignChains(ctx, campaign.CampaignID)
if !found {
return cs, status.Error(codes.NotFound, fmt.Sprintf("chain list not found for existing campaign %d", campaign.CampaignID))
return cs, status.Errorf(codes.NotFound, "chain list not found for existing campaign %d", campaign.CampaignID)
}

// retrieve information about most recent chain
Expand All @@ -87,7 +83,7 @@ func (k Keeper) GetCampaignSummary(ctx sdk.Context, campaign types.Campaign) (cs

chain, found := k.launchKeeper.GetChain(ctx, mostRecentLaunchID)
if !found {
return cs, status.Error(codes.NotFound, fmt.Sprintf("chain not found for campaign chain %d", mostRecentLaunchID))
return cs, status.Errorf(codes.NotFound, "chain not found for campaign chain %d", mostRecentLaunchID)
}

cs.MostRecentChain = types.MostRecentChain{
Expand Down
Loading