Skip to content

Commit

Permalink
test(campaign): add tests for CampaignSummary (#766)
Browse files Browse the repository at this point in the history
* add test for campaign summaries

* Update x/campaign/keeper/grpc_campaign_summary.go

Co-authored-by: Lucas Bertrand <[email protected]>

* format fix

Co-authored-by: Lucas Bertrand <[email protected]>
  • Loading branch information
giunatale and lumtis authored May 6, 2022
1 parent 67ab845 commit ff51a17
Show file tree
Hide file tree
Showing 4 changed files with 400 additions and 11 deletions.
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))

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

import (
"context"
"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 +21,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 +67,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 +84,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

0 comments on commit ff51a17

Please sign in to comment.