From ce35a593879f8e5c4fd92d357f387854c902e4cf Mon Sep 17 00:00:00 2001 From: Lucas Bertrand Date: Wed, 8 Jun 2022 19:41:31 +0200 Subject: [PATCH] test: implement query test suite (#832) * initiliaze test suite * launch tests * launch state * initialize query test suites * campaign tests * claim * monitoringc * participation * profile * reward * goimport * imports * fix campaign bug * remove campaign summary cli Co-authored-by: Alex Johnson Co-authored-by: Giuseppe Natale <12249307+giunatale@users.noreply.github.com> --- testutil/networksuite/networksuite.go | 295 ++++++++++++++++++ .../client/cli/query_campaign_chains_test.go | 29 +- .../client/cli/query_campaign_summary_test.go | 175 ----------- x/campaign/client/cli/query_campaign_test.go | 72 ++--- .../client/cli/query_mainnet_account_test.go | 75 ++--- .../cli/query_mainnet_vesting_account_test.go | 153 --------- x/campaign/client/cli/query_test.go | 19 ++ .../client/cli/query_airdrop_supply_test.go | 28 +- x/claim/client/cli/query_claim_record_test.go | 54 +--- x/claim/client/cli/query_mission_test.go | 52 +-- x/claim/client/cli/query_test.go | 19 ++ x/launch/client/cli/query_chain_test.go | 68 ++-- .../client/cli/query_genesis_account_test.go | 72 ++--- .../cli/query_genesis_validator_test.go | 71 ++--- x/launch/client/cli/query_request_test.go | 71 ++--- x/launch/client/cli/query_test.go | 19 ++ .../client/cli/query_vesting_account_test.go | 71 ++--- .../query_launch_id_from_channel_id_test.go | 48 +-- .../cli/query_monitoring_history_test.go | 30 +- .../cli/query_provider_client_id_test.go | 48 +-- x/monitoringc/client/cli/query_test.go | 19 ++ .../cli/query_verified_client_ids_test.go | 30 +- .../query_auction_used_allocations_test.go | 73 +---- .../cli/query_available_allocations_test.go | 70 ----- x/participation/client/cli/query_test.go | 19 ++ .../cli/query_total_allocations_test.go | 91 ------ .../client/cli/query_used_allocations_test.go | 50 +-- .../cli/query_coordinator_by_address_test.go | 27 +- .../client/cli/query_coordinator_test.go | 44 +-- x/profile/client/cli/query_test.go | 19 ++ x/profile/client/cli/query_validator_test.go | 46 +-- x/reward/client/cli/query_reward_pool_test.go | 48 +-- x/reward/client/cli/query_test.go | 19 ++ 33 files changed, 756 insertions(+), 1268 deletions(-) create mode 100644 testutil/networksuite/networksuite.go delete mode 100644 x/campaign/client/cli/query_campaign_summary_test.go delete mode 100644 x/campaign/client/cli/query_mainnet_vesting_account_test.go create mode 100644 x/campaign/client/cli/query_test.go create mode 100644 x/claim/client/cli/query_test.go create mode 100644 x/launch/client/cli/query_test.go create mode 100644 x/monitoringc/client/cli/query_test.go delete mode 100644 x/participation/client/cli/query_available_allocations_test.go create mode 100644 x/participation/client/cli/query_test.go delete mode 100644 x/participation/client/cli/query_total_allocations_test.go create mode 100644 x/profile/client/cli/query_test.go create mode 100644 x/reward/client/cli/query_test.go diff --git a/testutil/networksuite/networksuite.go b/testutil/networksuite/networksuite.go new file mode 100644 index 000000000..384b30094 --- /dev/null +++ b/testutil/networksuite/networksuite.go @@ -0,0 +1,295 @@ +// Package networksuite provides base test suite for tests that need a local network instance +package networksuite + +import ( + "math/rand" + "strconv" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/gogo/protobuf/proto" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + + "github.com/tendermint/spn/testutil/network" + "github.com/tendermint/spn/testutil/nullify" + "github.com/tendermint/spn/testutil/sample" + campaign "github.com/tendermint/spn/x/campaign/types" + claim "github.com/tendermint/spn/x/claim/types" + launch "github.com/tendermint/spn/x/launch/types" + monitoringc "github.com/tendermint/spn/x/monitoringc/types" + participation "github.com/tendermint/spn/x/participation/types" + profile "github.com/tendermint/spn/x/profile/types" + reward "github.com/tendermint/spn/x/reward/types" +) + +// NetworkTestSuite is a test suite for query tests that initializes a network instance +type NetworkTestSuite struct { + suite.Suite + Network *network.Network + LaunchState launch.GenesisState + CampaignState campaign.GenesisState + ClaimState claim.GenesisState + MonitoringcState monitoringc.GenesisState + ParticipationState participation.GenesisState + ProfileState profile.GenesisState + RewardState reward.GenesisState +} + +// SetupSuite setups the local network with a genesis state +func (nts *NetworkTestSuite) SetupSuite() { + r := sample.Rand() + cfg := network.DefaultConfig() + + updateConfigGenesisState := func(moduleName string, moduleState proto.Message) { + buf, err := cfg.Codec.MarshalJSON(moduleState) + require.NoError(nts.T(), err) + cfg.GenesisState[moduleName] = buf + } + + // initialize launch + require.NoError(nts.T(), cfg.Codec.UnmarshalJSON(cfg.GenesisState[launch.ModuleName], &nts.LaunchState)) + nts.LaunchState = populateLaunch(r, nts.LaunchState) + updateConfigGenesisState(launch.ModuleName, &nts.LaunchState) + + // initialize campaign + require.NoError(nts.T(), cfg.Codec.UnmarshalJSON(cfg.GenesisState[campaign.ModuleName], &nts.CampaignState)) + nts.CampaignState = populateCampaign(r, nts.CampaignState) + updateConfigGenesisState(campaign.ModuleName, &nts.CampaignState) + + // initialize claim + require.NoError(nts.T(), cfg.Codec.UnmarshalJSON(cfg.GenesisState[claim.ModuleName], &nts.ClaimState)) + nts.ClaimState = populateClaim(r, nts.ClaimState) + updateConfigGenesisState(claim.ModuleName, &nts.ClaimState) + + // initialize monitoring consumer + require.NoError(nts.T(), cfg.Codec.UnmarshalJSON(cfg.GenesisState[monitoringc.ModuleName], &nts.MonitoringcState)) + nts.MonitoringcState = populateMonitoringc(nts.MonitoringcState) + updateConfigGenesisState(monitoringc.ModuleName, &nts.MonitoringcState) + + // initialize participation + require.NoError(nts.T(), cfg.Codec.UnmarshalJSON(cfg.GenesisState[participation.ModuleName], &nts.ParticipationState)) + nts.ParticipationState = populateParticipation(r, nts.ParticipationState) + updateConfigGenesisState(participation.ModuleName, &nts.ParticipationState) + + // initialize profile + require.NoError(nts.T(), cfg.Codec.UnmarshalJSON(cfg.GenesisState[profile.ModuleName], &nts.ProfileState)) + nts.ProfileState = populateProfile(r, nts.ProfileState) + updateConfigGenesisState(profile.ModuleName, &nts.ProfileState) + + // initialize reward + require.NoError(nts.T(), cfg.Codec.UnmarshalJSON(cfg.GenesisState[reward.ModuleName], &nts.RewardState)) + nts.RewardState = populateReward(nts.RewardState) + updateConfigGenesisState(reward.ModuleName, &nts.RewardState) + + nts.Network = network.New(nts.T(), cfg) +} + +func populateLaunch(r *rand.Rand, launchState launch.GenesisState) launch.GenesisState { + // add chains + for i := 0; i < 5; i++ { + chain := sample.Chain(r, uint64(i), uint64(i)) + launchState.ChainList = append( + launchState.ChainList, + chain, + ) + } + + // add genesis accounts + for i := 0; i < 5; i++ { + launchState.GenesisAccountList = append( + launchState.GenesisAccountList, + sample.GenesisAccount(r, 0, sample.Address(r)), + ) + } + + // add vesting accounts + for i := 0; i < 5; i++ { + launchState.VestingAccountList = append( + launchState.VestingAccountList, + sample.VestingAccount(r, 0, sample.Address(r)), + ) + } + + // add genesis validators + for i := 0; i < 5; i++ { + launchState.GenesisValidatorList = append( + launchState.GenesisValidatorList, + sample.GenesisValidator(r, uint64(0), sample.Address(r)), + ) + } + + // add request + for i := 0; i < 5; i++ { + request := sample.Request(r, 0, sample.Address(r)) + request.RequestID = uint64(i) + launchState.RequestList = append( + launchState.RequestList, + request, + ) + } + + return launchState +} + +func populateCampaign(r *rand.Rand, campaignState campaign.GenesisState) campaign.GenesisState { + // add campaigns + for i := 0; i < 5; i++ { + camp := campaign.Campaign{ + CampaignID: uint64(i), + } + nullify.Fill(&camp) + campaignState.CampaignList = append(campaignState.CampaignList, camp) + } + + // add campaign chains + for i := 0; i < 5; i++ { + campaignState.CampaignChainsList = append(campaignState.CampaignChainsList, campaign.CampaignChains{ + CampaignID: uint64(i), + Chains: []uint64{uint64(i)}, + }) + } + + // add mainnet accounts + campaignID := uint64(5) + for i := 0; i < 5; i++ { + campaignState.MainnetAccountList = append( + campaignState.MainnetAccountList, + sample.MainnetAccount(r, campaignID, sample.Address(r)), + ) + } + + return campaignState +} + +func populateClaim(r *rand.Rand, claimState claim.GenesisState) claim.GenesisState { + claimState.AirdropSupply = sample.Coin(r) + + // add claim records + for i := 0; i < 5; i++ { + claimRecord := claim.ClaimRecord{ + Address: sample.Address(r), + Claimable: sdk.NewInt(r.Int63()), + } + nullify.Fill(&claimRecord) + claimState.ClaimRecords = append(claimState.ClaimRecords, claimRecord) + } + + // add missions + for i := 0; i < 5; i++ { + mission := claim.Mission{ + MissionID: uint64(i), + Weight: sdk.NewDec(r.Int63()), + } + nullify.Fill(&mission) + claimState.Missions = append(claimState.Missions, mission) + } + + return claimState +} + +func populateMonitoringc(monitoringcState monitoringc.GenesisState) monitoringc.GenesisState { + // add launch ID from channel ID + for i := 0; i < 5; i++ { + launchIDFromChannelID := monitoringc.LaunchIDFromChannelID{ + ChannelID: strconv.Itoa(i), + } + nullify.Fill(&launchIDFromChannelID) + monitoringcState.LaunchIDFromChannelIDList = append( + monitoringcState.LaunchIDFromChannelIDList, + launchIDFromChannelID, + ) + } + + // add monitoring history + for i := 0; i < 5; i++ { + monitoringHistory := monitoringc.MonitoringHistory{ + LaunchID: uint64(i), + } + nullify.Fill(&monitoringHistory) + monitoringcState.MonitoringHistoryList = append(monitoringcState.MonitoringHistoryList, monitoringHistory) + } + + // add provider client ID + for i := 0; i < 5; i++ { + providerClientID := monitoringc.ProviderClientID{ + LaunchID: uint64(i), + } + nullify.Fill(&providerClientID) + monitoringcState.ProviderClientIDList = append(monitoringcState.ProviderClientIDList, providerClientID) + } + + // add verified client IDs + for i := 0; i < 5; i++ { + verifiedClientID := monitoringc.VerifiedClientID{ + LaunchID: uint64(i), + } + nullify.Fill(&verifiedClientID) + monitoringcState.VerifiedClientIDList = append(monitoringcState.VerifiedClientIDList, verifiedClientID) + } + + return monitoringcState +} + +func populateParticipation(r *rand.Rand, participationState participation.GenesisState) participation.GenesisState { + // add used allocations + for i := 0; i < 5; i++ { + usedAllocations := participation.UsedAllocations{ + Address: sample.Address(r), + } + nullify.Fill(&usedAllocations) + participationState.UsedAllocationsList = append(participationState.UsedAllocationsList, usedAllocations) + } + + // add auction used allocations + address := sample.Address(r) + for i := 0; i < 5; i++ { + auctionUsedAllocations := participation.AuctionUsedAllocations{ + Address: address, + AuctionID: uint64(i), + } + nullify.Fill(&auctionUsedAllocations) + participationState.AuctionUsedAllocationsList = append(participationState.AuctionUsedAllocationsList, auctionUsedAllocations) + } + + return participationState +} + +func populateProfile(r *rand.Rand, profileState profile.GenesisState) profile.GenesisState { + // add coordinators + for i := 0; i < 5; i++ { + profileState.CoordinatorList = append( + profileState.CoordinatorList, + profile.Coordinator{CoordinatorID: uint64(i)}, + ) + } + + // add coordinator by address + for i := 0; i < 5; i++ { + profileState.CoordinatorByAddressList = append( + profileState.CoordinatorByAddressList, + profile.CoordinatorByAddress{Address: sample.Address(r)}, + ) + } + + // add validator + for i := 0; i < 5; i++ { + profileState.ValidatorList = append(profileState.ValidatorList, profile.Validator{ + Address: sample.Address(r), + }) + } + + return profileState +} + +func populateReward(rewardState reward.GenesisState) reward.GenesisState { + // add reward pool + for i := 0; i < 5; i++ { + rewardPool := reward.RewardPool{ + LaunchID: uint64(i), + } + nullify.Fill(&rewardPool) + rewardState.RewardPoolList = append(rewardState.RewardPoolList, rewardPool) + } + + return rewardState +} diff --git a/x/campaign/client/cli/query_campaign_chains_test.go b/x/campaign/client/cli/query_campaign_chains_test.go index d28e6e3b1..0ae1939b2 100644 --- a/x/campaign/client/cli/query_campaign_chains_test.go +++ b/x/campaign/client/cli/query_campaign_chains_test.go @@ -11,33 +11,14 @@ import ( "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" ) -func networkWithCampaignChainsObjects(t *testing.T, n int) (*network.Network, []types.CampaignChains) { - t.Helper() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) +func (suite *QueryTestSuite) TestShowCampaignChains() { + ctx := suite.Network.Validators[0].ClientCtx + objs := suite.CampaignState.CampaignChainsList - for i := 0; i < n; i++ { - state.CampaignChainsList = append(state.CampaignChainsList, types.CampaignChains{ - CampaignID: uint64(i), - Chains: []uint64{uint64(i)}, - }) - } - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.CampaignChainsList -} - -func TestShowCampaignChains(t *testing.T) { - net, objs := networkWithCampaignChainsObjects(t, 2) - - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -64,7 +45,7 @@ func TestShowCampaignChains(t *testing.T) { err: status.Error(codes.NotFound, "not found"), }, } { - t.Run(tc.desc, func(t *testing.T) { + suite.T().Run(tc.desc, func(t *testing.T) { args := []string{ strconv.Itoa(int(tc.idCampaignID)), } @@ -77,7 +58,7 @@ func TestShowCampaignChains(t *testing.T) { } else { require.NoError(t, err) var resp types.QueryGetCampaignChainsResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.CampaignChains) require.Equal(t, tc.obj, resp.CampaignChains) } diff --git a/x/campaign/client/cli/query_campaign_summary_test.go b/x/campaign/client/cli/query_campaign_summary_test.go deleted file mode 100644 index d353d251a..000000000 --- a/x/campaign/client/cli/query_campaign_summary_test.go +++ /dev/null @@ -1,175 +0,0 @@ -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/testutil/nullify" - "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()), - SpecialAllocations: types.EmptySpecialAllocations(), - } - 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, nullify.Fill(objs), nullify.Fill(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, nullify.Fill(objs), nullify.Fill(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, nullify.Fill(objs), nullify.Fill(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"), - }, - } { - 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)) - - } - }) - } -} diff --git a/x/campaign/client/cli/query_campaign_test.go b/x/campaign/client/cli/query_campaign_test.go index 1a9e6b3d7..851dc9b5c 100644 --- a/x/campaign/client/cli/query_campaign_test.go +++ b/x/campaign/client/cli/query_campaign_test.go @@ -2,42 +2,23 @@ package cli_test import ( "fmt" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" "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" - - "github.com/tendermint/spn/testutil/network" "github.com/tendermint/spn/testutil/nullify" - "github.com/tendermint/spn/testutil/sample" "github.com/tendermint/spn/x/campaign/client/cli" "github.com/tendermint/spn/x/campaign/types" + tmcli "github.com/tendermint/tendermint/libs/cli" ) -func networkWithCampaignObjects(t *testing.T, n int) (*network.Network, []types.Campaign) { - t.Helper() - r := sample.Rand() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) - - for i := 0; i < n; i++ { - state.CampaignList = append(state.CampaignList, sample.Campaign(r, uint64(i))) - } - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.CampaignList -} - -func TestShowCampaign(t *testing.T) { - net, objs := networkWithCampaignObjects(t, 2) +func (suite *QueryTestSuite) TestShowCampaign() { + ctx := suite.Network.Validators[0].ClientCtx + campaigns := suite.CampaignState.CampaignList - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -50,9 +31,9 @@ func TestShowCampaign(t *testing.T) { }{ { desc: "found", - id: fmt.Sprintf("%d", objs[0].CampaignID), + id: fmt.Sprintf("%d", campaigns[0].CampaignID), args: common, - obj: objs[0], + obj: campaigns[0], }, { desc: "not found", @@ -61,7 +42,7 @@ func TestShowCampaign(t *testing.T) { err: status.Error(codes.NotFound, "not found"), }, } { - t.Run(tc.desc, func(t *testing.T) { + suite.T().Run(tc.desc, func(t *testing.T) { args := []string{tc.id} args = append(args, tc.args...) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowCampaign(), args) @@ -72,17 +53,18 @@ func TestShowCampaign(t *testing.T) { } else { require.NoError(t, err) var resp types.QueryGetCampaignResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + tc.obj.Metadata = []uint8(nil) require.Equal(t, nullify.Fill(tc.obj), nullify.Fill(resp.Campaign)) } }) } } -func TestListCampaign(t *testing.T) { - net, objs := networkWithCampaignObjects(t, 5) +func (suite *QueryTestSuite) TestListCampaign() { + ctx := suite.Network.Validators[0].ClientCtx + campaigns := suite.CampaignState.CampaignList - ctx := net.Validators[0].ClientCtx request := func(next []byte, offset, limit uint64, total bool) []string { args := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), @@ -98,40 +80,40 @@ func TestListCampaign(t *testing.T) { } return args } - t.Run("ByOffset", func(t *testing.T) { + suite.T().Run("ByOffset", func(t *testing.T) { step := 2 - for i := 0; i < len(objs); i += step { + for i := 0; i < len(campaigns); i += step { args := request(nil, uint64(i), uint64(step), false) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListCampaign(), args) require.NoError(t, err) var resp types.QueryAllCampaignResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.Campaign), step) - require.Subset(t, nullify.Fill(objs), nullify.Fill(resp.Campaign)) + require.Subset(t, nullify.Fill(campaigns), nullify.Fill(resp.Campaign)) } }) - t.Run("ByKey", func(t *testing.T) { + suite.T().Run("ByKey", func(t *testing.T) { step := 2 var next []byte - for i := 0; i < len(objs); i += step { + for i := 0; i < len(campaigns); i += step { args := request(next, 0, uint64(step), false) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListCampaign(), args) require.NoError(t, err) var resp types.QueryAllCampaignResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.Campaign), step) - require.Subset(t, nullify.Fill(objs), nullify.Fill(resp.Campaign)) + require.Subset(t, nullify.Fill(campaigns), nullify.Fill(resp.Campaign)) next = resp.Pagination.NextKey } }) - t.Run("Total", func(t *testing.T) { - args := request(nil, 0, uint64(len(objs)), true) + suite.T().Run("Total", func(t *testing.T) { + args := request(nil, 0, uint64(len(campaigns)), true) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListCampaign(), args) require.NoError(t, err) var resp types.QueryAllCampaignResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.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.Campaign)) + require.Equal(t, len(campaigns), int(resp.Pagination.Total)) + require.ElementsMatch(t, nullify.Fill(campaigns), nullify.Fill(resp.Campaign)) }) } diff --git a/x/campaign/client/cli/query_mainnet_account_test.go b/x/campaign/client/cli/query_mainnet_account_test.go index 333e9b04c..87ea4c40e 100644 --- a/x/campaign/client/cli/query_mainnet_account_test.go +++ b/x/campaign/client/cli/query_mainnet_account_test.go @@ -12,39 +12,14 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "github.com/tendermint/spn/testutil/network" - "github.com/tendermint/spn/testutil/sample" "github.com/tendermint/spn/x/campaign/client/cli" "github.com/tendermint/spn/x/campaign/types" ) -func networkWithMainnetAccountObjects(t *testing.T, n int) (*network.Network, []types.MainnetAccount) { - t.Helper() - r := sample.Rand() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) +func (suite *QueryTestSuite) TestShowMainnetAccount() { + ctx := suite.Network.Validators[0].ClientCtx + accs := suite.CampaignState.MainnetAccountList - campaignID := uint64(5) - for i := 0; i < n; i++ { - state.MainnetAccountList = append( - state.MainnetAccountList, - sample.MainnetAccount(r, - campaignID, - sample.Address(r), - ), - ) - } - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.MainnetAccountList -} - -func TestShowMainnetAccount(t *testing.T) { - net, objs := networkWithMainnetAccountObjects(t, 2) - - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -59,11 +34,11 @@ func TestShowMainnetAccount(t *testing.T) { }{ { desc: "found", - idCampaignID: objs[0].CampaignID, - idAddress: objs[0].Address, + idCampaignID: accs[0].CampaignID, + idAddress: accs[0].Address, args: common, - obj: objs[0], + obj: accs[0], }, { desc: "not found", @@ -74,7 +49,7 @@ func TestShowMainnetAccount(t *testing.T) { err: status.Error(codes.NotFound, "not found"), }, } { - t.Run(tc.desc, func(t *testing.T) { + suite.T().Run(tc.desc, func(t *testing.T) { args := []string{ strconv.Itoa(int(tc.idCampaignID)), tc.idAddress, @@ -88,7 +63,7 @@ func TestShowMainnetAccount(t *testing.T) { } else { require.NoError(t, err) var resp types.QueryGetMainnetAccountResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.MainnetAccount) require.Equal(t, tc.obj, resp.MainnetAccount) } @@ -96,11 +71,11 @@ func TestShowMainnetAccount(t *testing.T) { } } -func TestListMainnetAccount(t *testing.T) { - net, objs := networkWithMainnetAccountObjects(t, 5) +func (suite *QueryTestSuite) TestListMainnetAccount() { + ctx := suite.Network.Validators[0].ClientCtx + accs := suite.CampaignState.MainnetAccountList - campaignID := objs[0].CampaignID - ctx := net.Validators[0].ClientCtx + campaignID := accs[0].CampaignID request := func(campaignID uint64, next []byte, offset, limit uint64, total bool) []string { args := []string{ strconv.FormatUint(campaignID, 10), @@ -117,40 +92,40 @@ func TestListMainnetAccount(t *testing.T) { } return args } - t.Run("ByOffset", func(t *testing.T) { + suite.T().Run("ByOffset", func(t *testing.T) { step := 2 - for i := 0; i < len(objs); i += step { + for i := 0; i < len(accs); i += step { args := request(campaignID, nil, uint64(i), uint64(step), false) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListMainnetAccount(), args) require.NoError(t, err) var resp types.QueryAllMainnetAccountResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.MainnetAccount), step) - require.Subset(t, objs, resp.MainnetAccount) + require.Subset(t, accs, resp.MainnetAccount) } }) - t.Run("ByKey", func(t *testing.T) { + suite.T().Run("ByKey", func(t *testing.T) { step := 2 var next []byte - for i := 0; i < len(objs); i += step { + for i := 0; i < len(accs); i += step { args := request(campaignID, next, 0, uint64(step), false) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListMainnetAccount(), args) require.NoError(t, err) var resp types.QueryAllMainnetAccountResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.MainnetAccount), step) - require.Subset(t, objs, resp.MainnetAccount) + require.Subset(t, accs, resp.MainnetAccount) next = resp.Pagination.NextKey } }) - t.Run("Total", func(t *testing.T) { - args := request(campaignID, nil, 0, uint64(len(objs)), true) + suite.T().Run("Total", func(t *testing.T) { + args := request(campaignID, nil, 0, uint64(len(accs)), true) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListMainnetAccount(), args) require.NoError(t, err) var resp types.QueryAllMainnetAccountResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NoError(t, err) - require.Equal(t, len(objs), int(resp.Pagination.Total)) - require.ElementsMatch(t, objs, resp.MainnetAccount) + require.Equal(t, len(accs), int(resp.Pagination.Total)) + require.ElementsMatch(t, accs, resp.MainnetAccount) }) } diff --git a/x/campaign/client/cli/query_mainnet_vesting_account_test.go b/x/campaign/client/cli/query_mainnet_vesting_account_test.go deleted file mode 100644 index 74575412b..000000000 --- a/x/campaign/client/cli/query_mainnet_vesting_account_test.go +++ /dev/null @@ -1,153 +0,0 @@ -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" - - "github.com/tendermint/spn/testutil/network" - "github.com/tendermint/spn/testutil/sample" - "github.com/tendermint/spn/x/campaign/client/cli" - "github.com/tendermint/spn/x/campaign/types" -) - -func networkWithMainnetVestingAccountObjects(t *testing.T, n int) (*network.Network, []types.MainnetVestingAccount) { - t.Helper() - r := sample.Rand() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) - - campaignID := uint64(5) - for i := 0; i < n; i++ { - state.MainnetVestingAccountList = append(state.MainnetVestingAccountList, sample.MainnetVestingAccount(r, - campaignID, - sample.Address(r), - )) - } - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.MainnetVestingAccountList -} - -func TestShowMainnetVestingAccount(t *testing.T) { - net, objs := networkWithMainnetVestingAccountObjects(t, 2) - - ctx := net.Validators[0].ClientCtx - common := []string{ - fmt.Sprintf("--%s=json", tmcli.OutputFlag), - } - for _, tc := range []struct { - desc string - idCampaignID uint64 - idAddress string - - args []string - err error - obj types.MainnetVestingAccount - }{ - { - desc: "found", - idCampaignID: objs[0].CampaignID, - idAddress: objs[0].Address, - - args: common, - obj: objs[0], - }, - { - desc: "not found", - idCampaignID: 100000, - idAddress: strconv.Itoa(100000), - - args: common, - err: status.Error(codes.NotFound, "not found"), - }, - } { - t.Run(tc.desc, func(t *testing.T) { - args := []string{ - strconv.Itoa(int(tc.idCampaignID)), - tc.idAddress, - } - args = append(args, tc.args...) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowMainnetVestingAccount(), 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.QueryGetMainnetVestingAccountResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.NotNil(t, resp.MainnetVestingAccount) - require.Equal(t, tc.obj, resp.MainnetVestingAccount) - } - }) - } -} - -func TestListMainnetVestingAccount(t *testing.T) { - net, objs := networkWithMainnetVestingAccountObjects(t, 5) - - campaignID := objs[0].CampaignID - ctx := net.Validators[0].ClientCtx - request := func(campaignID uint64, next []byte, offset, limit uint64, total bool) []string { - args := []string{ - strconv.FormatUint(campaignID, 10), - 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(campaignID, nil, uint64(i), uint64(step), false) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListMainnetVestingAccount(), args) - require.NoError(t, err) - var resp types.QueryAllMainnetVestingAccountResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.LessOrEqual(t, len(resp.MainnetVestingAccount), step) - require.Subset(t, objs, resp.MainnetVestingAccount) - } - }) - t.Run("ByKey", func(t *testing.T) { - step := 2 - var next []byte - for i := 0; i < len(objs); i += step { - args := request(campaignID, next, 0, uint64(step), false) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListMainnetVestingAccount(), args) - require.NoError(t, err) - var resp types.QueryAllMainnetVestingAccountResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.LessOrEqual(t, len(resp.MainnetVestingAccount), step) - require.Subset(t, objs, resp.MainnetVestingAccount) - next = resp.Pagination.NextKey - } - }) - t.Run("Total", func(t *testing.T) { - args := request(campaignID, nil, 0, uint64(len(objs)), true) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListMainnetVestingAccount(), args) - require.NoError(t, err) - var resp types.QueryAllMainnetVestingAccountResponse - 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.MainnetVestingAccount) - }) -} diff --git a/x/campaign/client/cli/query_test.go b/x/campaign/client/cli/query_test.go new file mode 100644 index 000000000..d7b546208 --- /dev/null +++ b/x/campaign/client/cli/query_test.go @@ -0,0 +1,19 @@ +package cli_test + +import ( + "testing" + + "github.com/stretchr/testify/suite" + + "github.com/tendermint/spn/testutil/networksuite" +) + +// QueryTestSuite is a test suite for query tests +type QueryTestSuite struct { + networksuite.NetworkTestSuite +} + +// TestQueryTestSuite runs test of the query suite +func TestQueryTestSuite(t *testing.T) { + suite.Run(t, new(QueryTestSuite)) +} diff --git a/x/claim/client/cli/query_airdrop_supply_test.go b/x/claim/client/cli/query_airdrop_supply_test.go index 60d8a24d7..2c82b4c45 100644 --- a/x/claim/client/cli/query_airdrop_supply_test.go +++ b/x/claim/client/cli/query_airdrop_supply_test.go @@ -10,31 +10,15 @@ import ( tmcli "github.com/tendermint/tendermint/libs/cli" "google.golang.org/grpc/status" - "github.com/tendermint/spn/testutil/network" "github.com/tendermint/spn/testutil/nullify" - "github.com/tendermint/spn/testutil/sample" "github.com/tendermint/spn/x/claim/client/cli" "github.com/tendermint/spn/x/claim/types" ) -func networkWithAirdropSupplyObjects(t *testing.T) (*network.Network, sdk.Coin) { - t.Helper() - r := sample.Rand() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) +func (suite *QueryTestSuite) TestShowAirdropSupply() { + ctx := suite.Network.Validators[0].ClientCtx + airdropSupply := suite.ClaimState.AirdropSupply - state.AirdropSupply = sample.Coin(r) - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.AirdropSupply -} - -func TestShowAirdropSupply(t *testing.T) { - net, obj := networkWithAirdropSupplyObjects(t) - - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -47,10 +31,10 @@ func TestShowAirdropSupply(t *testing.T) { { desc: "get", args: common, - obj: obj, + obj: airdropSupply, }, } { - t.Run(tc.desc, func(t *testing.T) { + suite.T().Run(tc.desc, func(t *testing.T) { var args []string args = append(args, tc.args...) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowAirdropSupply(), args) @@ -61,7 +45,7 @@ func TestShowAirdropSupply(t *testing.T) { } else { require.NoError(t, err) var resp types.QueryGetAirdropSupplyResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.AirdropSupply) require.Equal(t, nullify.Fill(&tc.obj), diff --git a/x/claim/client/cli/query_claim_record_test.go b/x/claim/client/cli/query_claim_record_test.go index 0d560c82c..353489565 100644 --- a/x/claim/client/cli/query_claim_record_test.go +++ b/x/claim/client/cli/query_claim_record_test.go @@ -6,45 +6,21 @@ import ( "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/testutil/nullify" "github.com/tendermint/spn/testutil/sample" "github.com/tendermint/spn/x/claim/client/cli" "github.com/tendermint/spn/x/claim/types" ) -func networkWithClaimRecordObjects(t *testing.T, n int) (*network.Network, []types.ClaimRecord) { - t.Helper() - r := sample.Rand() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) +func (suite *QueryTestSuite) TestShowClaimRecord() { + ctx := suite.Network.Validators[0].ClientCtx + objs := suite.ClaimState.ClaimRecords - for i := 0; i < n; i++ { - claimRecord := types.ClaimRecord{ - Address: sample.Address(r), - Claimable: sdk.NewInt(r.Int63()), - } - nullify.Fill(&claimRecord) - state.ClaimRecords = append(state.ClaimRecords, claimRecord) - } - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.ClaimRecords -} - -func TestShowClaimRecord(t *testing.T) { - net, objs := networkWithClaimRecordObjects(t, 2) - r := sample.Rand() - - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -65,13 +41,13 @@ func TestShowClaimRecord(t *testing.T) { }, { desc: "not found", - address: sample.Address(r), + address: sample.Address(sample.Rand()), args: common, err: status.Error(codes.NotFound, "not found"), }, } { - t.Run(tc.desc, func(t *testing.T) { + suite.T().Run(tc.desc, func(t *testing.T) { args := []string{ tc.address, } @@ -84,7 +60,7 @@ func TestShowClaimRecord(t *testing.T) { } else { require.NoError(t, err) var resp types.QueryGetClaimRecordResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.ClaimRecord) require.Equal(t, nullify.Fill(&tc.obj), @@ -95,10 +71,10 @@ func TestShowClaimRecord(t *testing.T) { } } -func TestListClaimRecord(t *testing.T) { - net, objs := networkWithClaimRecordObjects(t, 5) +func (suite *QueryTestSuite) TestListClaimRecord() { + ctx := suite.Network.Validators[0].ClientCtx + objs := suite.ClaimState.ClaimRecords - ctx := net.Validators[0].ClientCtx request := func(next []byte, offset, limit uint64, total bool) []string { args := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), @@ -114,14 +90,14 @@ func TestListClaimRecord(t *testing.T) { } return args } - t.Run("ByOffset", func(t *testing.T) { + suite.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.CmdListClaimRecord(), args) require.NoError(t, err) var resp types.QueryAllClaimRecordResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.ClaimRecord), step) require.Subset(t, nullify.Fill(objs), @@ -129,7 +105,7 @@ func TestListClaimRecord(t *testing.T) { ) } }) - t.Run("ByKey", func(t *testing.T) { + suite.T().Run("ByKey", func(t *testing.T) { step := 2 var next []byte for i := 0; i < len(objs); i += step { @@ -137,7 +113,7 @@ func TestListClaimRecord(t *testing.T) { out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListClaimRecord(), args) require.NoError(t, err) var resp types.QueryAllClaimRecordResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.ClaimRecord), step) require.Subset(t, nullify.Fill(objs), @@ -146,12 +122,12 @@ func TestListClaimRecord(t *testing.T) { next = resp.Pagination.NextKey } }) - t.Run("Total", func(t *testing.T) { + suite.T().Run("Total", func(t *testing.T) { args := request(nil, 0, uint64(len(objs)), true) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListClaimRecord(), args) require.NoError(t, err) var resp types.QueryAllClaimRecordResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NoError(t, err) require.Equal(t, len(objs), int(resp.Pagination.Total)) require.ElementsMatch(t, diff --git a/x/claim/client/cli/query_mission_test.go b/x/claim/client/cli/query_mission_test.go index 9b765c014..041238426 100644 --- a/x/claim/client/cli/query_mission_test.go +++ b/x/claim/client/cli/query_mission_test.go @@ -6,44 +6,20 @@ import ( "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/testutil/nullify" - "github.com/tendermint/spn/testutil/sample" "github.com/tendermint/spn/x/claim/client/cli" "github.com/tendermint/spn/x/claim/types" ) -func networkWithMissionObjects(t *testing.T, n int) (*network.Network, []types.Mission) { - t.Helper() - r := sample.Rand() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) +func (suite *QueryTestSuite) TestShowMission() { + ctx := suite.Network.Validators[0].ClientCtx + objs := suite.ClaimState.Missions - for i := 0; i < n; i++ { - mission := types.Mission{ - MissionID: uint64(i), - Weight: sdk.NewDec(r.Int63()), - } - nullify.Fill(&mission) - state.Missions = append(state.Missions, mission) - } - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.Missions -} - -func TestShowMission(t *testing.T) { - net, objs := networkWithMissionObjects(t, 2) - - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -67,7 +43,7 @@ func TestShowMission(t *testing.T) { err: status.Error(codes.NotFound, "not found"), }, } { - t.Run(tc.desc, func(t *testing.T) { + suite.T().Run(tc.desc, func(t *testing.T) { args := []string{tc.id} args = append(args, tc.args...) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowMission(), args) @@ -78,7 +54,7 @@ func TestShowMission(t *testing.T) { } else { require.NoError(t, err) var resp types.QueryGetMissionResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.Mission) require.Equal(t, nullify.Fill(&tc.obj), @@ -89,10 +65,10 @@ func TestShowMission(t *testing.T) { } } -func TestListMission(t *testing.T) { - net, objs := networkWithMissionObjects(t, 5) +func (suite *QueryTestSuite) TestListMission() { + ctx := suite.Network.Validators[0].ClientCtx + objs := suite.ClaimState.Missions - ctx := net.Validators[0].ClientCtx request := func(next []byte, offset, limit uint64, total bool) []string { args := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), @@ -108,14 +84,14 @@ func TestListMission(t *testing.T) { } return args } - t.Run("ByOffset", func(t *testing.T) { + suite.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.CmdListMission(), args) require.NoError(t, err) var resp types.QueryAllMissionResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.Mission), step) require.Subset(t, nullify.Fill(objs), @@ -123,7 +99,7 @@ func TestListMission(t *testing.T) { ) } }) - t.Run("ByKey", func(t *testing.T) { + suite.T().Run("ByKey", func(t *testing.T) { step := 2 var next []byte for i := 0; i < len(objs); i += step { @@ -131,7 +107,7 @@ func TestListMission(t *testing.T) { out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListMission(), args) require.NoError(t, err) var resp types.QueryAllMissionResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.Mission), step) require.Subset(t, nullify.Fill(objs), @@ -140,12 +116,12 @@ func TestListMission(t *testing.T) { next = resp.Pagination.NextKey } }) - t.Run("Total", func(t *testing.T) { + suite.T().Run("Total", func(t *testing.T) { args := request(nil, 0, uint64(len(objs)), true) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListMission(), args) require.NoError(t, err) var resp types.QueryAllMissionResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NoError(t, err) require.Equal(t, len(objs), int(resp.Pagination.Total)) require.ElementsMatch(t, diff --git a/x/claim/client/cli/query_test.go b/x/claim/client/cli/query_test.go new file mode 100644 index 000000000..d7b546208 --- /dev/null +++ b/x/claim/client/cli/query_test.go @@ -0,0 +1,19 @@ +package cli_test + +import ( + "testing" + + "github.com/stretchr/testify/suite" + + "github.com/tendermint/spn/testutil/networksuite" +) + +// QueryTestSuite is a test suite for query tests +type QueryTestSuite struct { + networksuite.NetworkTestSuite +} + +// TestQueryTestSuite runs test of the query suite +func TestQueryTestSuite(t *testing.T) { + suite.Run(t, new(QueryTestSuite)) +} diff --git a/x/launch/client/cli/query_chain_test.go b/x/launch/client/cli/query_chain_test.go index 971e43e71..081ed50e2 100644 --- a/x/launch/client/cli/query_chain_test.go +++ b/x/launch/client/cli/query_chain_test.go @@ -11,36 +11,14 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "github.com/tendermint/spn/testutil/network" - "github.com/tendermint/spn/testutil/sample" "github.com/tendermint/spn/x/launch/client/cli" "github.com/tendermint/spn/x/launch/types" ) -func networkWithFooObjects(t *testing.T, n int) (*network.Network, []types.Chain) { - t.Helper() - r := sample.Rand() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) +func (suite *QueryTestSuite) TestShowChain() { + ctx := suite.Network.Validators[0].ClientCtx + chains := suite.LaunchState.ChainList - for i := 0; i < n; i++ { - chain := sample.Chain(r, uint64(i), uint64(i)) - state.ChainList = append( - state.ChainList, - chain, - ) - } - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.ChainList -} - -func TestShowChain(t *testing.T) { - net, objs := networkWithFooObjects(t, 2) - - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -53,9 +31,9 @@ func TestShowChain(t *testing.T) { }{ { desc: "found", - id: fmt.Sprintf("%d", objs[0].LaunchID), + id: fmt.Sprintf("%d", chains[0].LaunchID), args: common, - obj: objs[0], + obj: chains[0], }, { desc: "not found", @@ -64,7 +42,7 @@ func TestShowChain(t *testing.T) { err: status.Error(codes.NotFound, "not found"), }, } { - t.Run(tc.desc, func(t *testing.T) { + suite.T().Run(tc.desc, func(t *testing.T) { args := []string{tc.id} args = append(args, tc.args...) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowChain(), args) @@ -75,7 +53,7 @@ func TestShowChain(t *testing.T) { } else { require.NoError(t, err) var resp types.QueryGetChainResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.Chain) require.Equal(t, tc.obj, resp.Chain) } @@ -83,10 +61,10 @@ func TestShowChain(t *testing.T) { } } -func TestListFoo(t *testing.T) { - net, objs := networkWithFooObjects(t, 5) +func (suite *QueryTestSuite) TestListFoo() { + ctx := suite.Network.Validators[0].ClientCtx + chains := suite.LaunchState.ChainList - ctx := net.Validators[0].ClientCtx request := func(next []byte, offset, limit uint64, total bool) []string { args := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), @@ -102,40 +80,40 @@ func TestListFoo(t *testing.T) { } return args } - t.Run("ByOffset", func(t *testing.T) { + suite.T().Run("ByOffset", func(t *testing.T) { step := 2 - for i := 0; i < len(objs); i += step { + for i := 0; i < len(chains); i += step { args := request(nil, uint64(i), uint64(step), false) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListChain(), args) require.NoError(t, err) var resp types.QueryAllChainResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.Chain), step) - require.Subset(t, objs, resp.Chain) + require.Subset(t, chains, resp.Chain) } }) - t.Run("ByKey", func(t *testing.T) { + suite.T().Run("ByKey", func(t *testing.T) { step := 2 var next []byte - for i := 0; i < len(objs); i += step { + for i := 0; i < len(chains); i += step { args := request(next, 0, uint64(step), false) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListChain(), args) require.NoError(t, err) var resp types.QueryAllChainResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.Chain), step) - require.Subset(t, objs, resp.Chain) + require.Subset(t, chains, resp.Chain) next = resp.Pagination.NextKey } }) - t.Run("Total", func(t *testing.T) { - args := request(nil, 0, uint64(len(objs)), true) + suite.T().Run("Total", func(t *testing.T) { + args := request(nil, 0, uint64(len(chains)), true) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListChain(), args) require.NoError(t, err) var resp types.QueryAllChainResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NoError(t, err) - require.Equal(t, len(objs), int(resp.Pagination.Total)) - require.ElementsMatch(t, objs, resp.Chain) + require.Equal(t, len(chains), int(resp.Pagination.Total)) + require.ElementsMatch(t, chains, resp.Chain) }) } diff --git a/x/launch/client/cli/query_genesis_account_test.go b/x/launch/client/cli/query_genesis_account_test.go index fcb9e41dc..07307ee29 100644 --- a/x/launch/client/cli/query_genesis_account_test.go +++ b/x/launch/client/cli/query_genesis_account_test.go @@ -12,36 +12,14 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "github.com/tendermint/spn/testutil/network" - "github.com/tendermint/spn/testutil/sample" "github.com/tendermint/spn/x/launch/client/cli" "github.com/tendermint/spn/x/launch/types" ) -func networkWithGenesisAccountObjects(t *testing.T, n int) (*network.Network, []types.GenesisAccount) { - t.Helper() - r := sample.Rand() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) +func (suite *QueryTestSuite) TestShowGenesisAccount() { + ctx := suite.Network.Validators[0].ClientCtx + accs := suite.LaunchState.GenesisAccountList - for i := 0; i < n; i++ { - state.GenesisAccountList = append( - state.GenesisAccountList, - sample.GenesisAccount(r, 0, strconv.Itoa(i)), - ) - } - state.ChainList = append(state.ChainList, sample.Chain(r, 0, sample.Uint64(r))) - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.GenesisAccountList -} - -func TestShowGenesisAccount(t *testing.T) { - net, objs := networkWithGenesisAccountObjects(t, 2) - - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -56,11 +34,11 @@ func TestShowGenesisAccount(t *testing.T) { }{ { desc: "found", - idLaunchID: strconv.Itoa(int(objs[0].LaunchID)), - idAddress: objs[0].Address, + idLaunchID: strconv.Itoa(int(accs[0].LaunchID)), + idAddress: accs[0].Address, args: common, - obj: objs[0], + obj: accs[0], }, { desc: "not found", @@ -71,7 +49,7 @@ func TestShowGenesisAccount(t *testing.T) { err: status.Error(codes.NotFound, "not found"), }, } { - t.Run(tc.desc, func(t *testing.T) { + suite.T().Run(tc.desc, func(t *testing.T) { args := []string{ tc.idLaunchID, tc.idAddress, @@ -85,7 +63,7 @@ func TestShowGenesisAccount(t *testing.T) { } else { require.NoError(t, err) var resp types.QueryGetGenesisAccountResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.GenesisAccount) require.Equal(t, tc.obj, resp.GenesisAccount) } @@ -93,11 +71,11 @@ func TestShowGenesisAccount(t *testing.T) { } } -func TestListGenesisAccount(t *testing.T) { - net, objs := networkWithGenesisAccountObjects(t, 5) +func (suite *QueryTestSuite) TestListGenesisAccount() { + ctx := suite.Network.Validators[0].ClientCtx + accs := suite.LaunchState.GenesisAccountList - chainID := objs[0].LaunchID - ctx := net.Validators[0].ClientCtx + chainID := accs[0].LaunchID request := func(chainID uint64, next []byte, offset, limit uint64, total bool) []string { args := []string{ strconv.Itoa(int(chainID)), @@ -114,40 +92,40 @@ func TestListGenesisAccount(t *testing.T) { } return args } - t.Run("ByOffset", func(t *testing.T) { + suite.T().Run("ByOffset", func(t *testing.T) { step := 2 - for i := 0; i < len(objs); i += step { + for i := 0; i < len(accs); i += step { args := request(chainID, nil, uint64(i), uint64(step), false) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListGenesisAccount(), args) require.NoError(t, err) var resp types.QueryAllGenesisAccountResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.GenesisAccount), step) - require.Subset(t, objs, resp.GenesisAccount) + require.Subset(t, accs, resp.GenesisAccount) } }) - t.Run("ByKey", func(t *testing.T) { + suite.T().Run("ByKey", func(t *testing.T) { step := 2 var next []byte - for i := 0; i < len(objs); i += step { + for i := 0; i < len(accs); i += step { args := request(chainID, next, 0, uint64(step), false) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListGenesisAccount(), args) require.NoError(t, err) var resp types.QueryAllGenesisAccountResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.GenesisAccount), step) - require.Subset(t, objs, resp.GenesisAccount) + require.Subset(t, accs, resp.GenesisAccount) next = resp.Pagination.NextKey } }) - t.Run("Total", func(t *testing.T) { - args := request(chainID, nil, 0, uint64(len(objs)), true) + suite.T().Run("Total", func(t *testing.T) { + args := request(chainID, nil, 0, uint64(len(accs)), true) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListGenesisAccount(), args) require.NoError(t, err) var resp types.QueryAllGenesisAccountResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NoError(t, err) - require.Equal(t, len(objs), int(resp.Pagination.Total)) - require.ElementsMatch(t, objs, resp.GenesisAccount) + require.Equal(t, len(accs), int(resp.Pagination.Total)) + require.ElementsMatch(t, accs, resp.GenesisAccount) }) } diff --git a/x/launch/client/cli/query_genesis_validator_test.go b/x/launch/client/cli/query_genesis_validator_test.go index 19010ea55..768eb8ad9 100644 --- a/x/launch/client/cli/query_genesis_validator_test.go +++ b/x/launch/client/cli/query_genesis_validator_test.go @@ -12,35 +12,14 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "github.com/tendermint/spn/testutil/network" - "github.com/tendermint/spn/testutil/sample" "github.com/tendermint/spn/x/launch/client/cli" "github.com/tendermint/spn/x/launch/types" ) -func networkWithGenesisValidatorObjects(t *testing.T, n int) (*network.Network, []types.GenesisValidator) { - t.Helper() - r := sample.Rand() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) +func (suite *QueryTestSuite) TestShowGenesisValidator() { + ctx := suite.Network.Validators[0].ClientCtx + accs := suite.LaunchState.GenesisValidatorList - for i := 0; i < n; i++ { - state.GenesisValidatorList = append( - state.GenesisValidatorList, - sample.GenesisValidator(r, uint64(0), strconv.Itoa(i)), - ) - } - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.GenesisValidatorList -} - -func TestShowGenesisValidator(t *testing.T) { - net, objs := networkWithGenesisValidatorObjects(t, 2) - - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -55,11 +34,11 @@ func TestShowGenesisValidator(t *testing.T) { }{ { desc: "found", - idChainID: strconv.Itoa(int(objs[0].LaunchID)), - idAddress: objs[0].Address, + idChainID: strconv.Itoa(int(accs[0].LaunchID)), + idAddress: accs[0].Address, args: common, - obj: objs[0], + obj: accs[0], }, { desc: "not found", @@ -70,7 +49,7 @@ func TestShowGenesisValidator(t *testing.T) { err: status.Error(codes.NotFound, "not found"), }, } { - t.Run(tc.desc, func(t *testing.T) { + suite.T().Run(tc.desc, func(t *testing.T) { args := []string{ tc.idChainID, tc.idAddress, @@ -84,7 +63,7 @@ func TestShowGenesisValidator(t *testing.T) { } else { require.NoError(t, err) var resp types.QueryGetGenesisValidatorResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.GenesisValidator) require.Equal(t, tc.obj, resp.GenesisValidator) } @@ -92,11 +71,11 @@ func TestShowGenesisValidator(t *testing.T) { } } -func TestListGenesisValidator(t *testing.T) { - net, objs := networkWithGenesisValidatorObjects(t, 5) +func (suite *QueryTestSuite) TestListGenesisValidator() { + ctx := suite.Network.Validators[0].ClientCtx + accs := suite.LaunchState.GenesisValidatorList - chainID := objs[0].LaunchID - ctx := net.Validators[0].ClientCtx + chainID := accs[0].LaunchID request := func(chainID uint64, next []byte, offset, limit uint64, total bool) []string { args := []string{ strconv.Itoa(int(chainID)), @@ -113,40 +92,40 @@ func TestListGenesisValidator(t *testing.T) { } return args } - t.Run("ByOffset", func(t *testing.T) { + suite.T().Run("ByOffset", func(t *testing.T) { step := 2 - for i := 0; i < len(objs); i += step { + for i := 0; i < len(accs); i += step { args := request(chainID, nil, uint64(i), uint64(step), false) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListGenesisValidator(), args) require.NoError(t, err) var resp types.QueryAllGenesisValidatorResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.GenesisValidator), step) - require.Subset(t, objs, resp.GenesisValidator) + require.Subset(t, accs, resp.GenesisValidator) } }) - t.Run("ByKey", func(t *testing.T) { + suite.T().Run("ByKey", func(t *testing.T) { step := 2 var next []byte - for i := 0; i < len(objs); i += step { + for i := 0; i < len(accs); i += step { args := request(chainID, next, 0, uint64(step), false) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListGenesisValidator(), args) require.NoError(t, err) var resp types.QueryAllGenesisValidatorResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.GenesisValidator), step) - require.Subset(t, objs, resp.GenesisValidator) + require.Subset(t, accs, resp.GenesisValidator) next = resp.Pagination.NextKey } }) - t.Run("Total", func(t *testing.T) { - args := request(chainID, nil, 0, uint64(len(objs)), true) + suite.T().Run("Total", func(t *testing.T) { + args := request(chainID, nil, 0, uint64(len(accs)), true) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListGenesisValidator(), args) require.NoError(t, err) var resp types.QueryAllGenesisValidatorResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NoError(t, err) - require.Equal(t, len(objs), int(resp.Pagination.Total)) - require.ElementsMatch(t, objs, resp.GenesisValidator) + require.Equal(t, len(accs), int(resp.Pagination.Total)) + require.ElementsMatch(t, accs, resp.GenesisValidator) }) } diff --git a/x/launch/client/cli/query_request_test.go b/x/launch/client/cli/query_request_test.go index cebff4908..2b1053fe0 100644 --- a/x/launch/client/cli/query_request_test.go +++ b/x/launch/client/cli/query_request_test.go @@ -12,37 +12,14 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "github.com/tendermint/spn/testutil/network" - "github.com/tendermint/spn/testutil/sample" "github.com/tendermint/spn/x/launch/client/cli" "github.com/tendermint/spn/x/launch/types" ) -func networkWithRequestObjects(t *testing.T, n int) (*network.Network, []types.Request) { - t.Helper() - r := sample.Rand() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) +func (suite *QueryTestSuite) TestShowRequest() { + ctx := suite.Network.Validators[0].ClientCtx + requests := suite.LaunchState.RequestList - for i := 0; i < n; i++ { - request := sample.Request(r, 0, sample.Address(r)) - request.RequestID = uint64(i) - state.RequestList = append( - state.RequestList, - request, - ) - } - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.RequestList -} - -func TestShowRequest(t *testing.T) { - net, objs := networkWithRequestObjects(t, 2) - - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -57,11 +34,11 @@ func TestShowRequest(t *testing.T) { }{ { desc: "found", - idLaunchID: strconv.Itoa(int(objs[0].LaunchID)), - idRequestID: objs[0].RequestID, + idLaunchID: strconv.Itoa(int(requests[0].LaunchID)), + idRequestID: requests[0].RequestID, args: common, - obj: objs[0], + obj: requests[0], }, { desc: "not found", @@ -72,7 +49,7 @@ func TestShowRequest(t *testing.T) { err: status.Error(codes.NotFound, "not found"), }, } { - t.Run(tc.desc, func(t *testing.T) { + suite.T().Run(tc.desc, func(t *testing.T) { args := []string{ tc.idLaunchID, strconv.Itoa(int(tc.idRequestID)), @@ -86,7 +63,7 @@ func TestShowRequest(t *testing.T) { } else { require.NoError(t, err) var resp types.QueryGetRequestResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.Request) require.Equal(t, tc.obj, resp.Request) } @@ -94,10 +71,10 @@ func TestShowRequest(t *testing.T) { } } -func TestListRequest(t *testing.T) { - net, objs := networkWithRequestObjects(t, 5) +func (suite *QueryTestSuite) TestListRequest() { + ctx := suite.Network.Validators[0].ClientCtx + requests := suite.LaunchState.RequestList - ctx := net.Validators[0].ClientCtx request := func(launchID string, next []byte, offset, limit uint64, total bool) []string { args := []string{ launchID, @@ -114,40 +91,40 @@ func TestListRequest(t *testing.T) { } return args } - t.Run("ByOffset", func(t *testing.T) { + suite.T().Run("ByOffset", func(t *testing.T) { step := 2 - for i := 0; i < len(objs); i += step { + for i := 0; i < len(requests); i += step { args := request("0", nil, uint64(i), uint64(step), false) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListRequest(), args) require.NoError(t, err) var resp types.QueryAllRequestResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.Request), step) - require.Subset(t, objs, resp.Request) + require.Subset(t, requests, resp.Request) } }) - t.Run("ByKey", func(t *testing.T) { + suite.T().Run("ByKey", func(t *testing.T) { step := 2 var next []byte - for i := 0; i < len(objs); i += step { + for i := 0; i < len(requests); i += step { args := request("0", next, 0, uint64(step), false) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListRequest(), args) require.NoError(t, err) var resp types.QueryAllRequestResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.Request), step) - require.Subset(t, objs, resp.Request) + require.Subset(t, requests, resp.Request) next = resp.Pagination.NextKey } }) - t.Run("Total", func(t *testing.T) { - args := request("0", nil, 0, uint64(len(objs)), true) + suite.T().Run("Total", func(t *testing.T) { + args := request("0", nil, 0, uint64(len(requests)), true) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListRequest(), args) require.NoError(t, err) var resp types.QueryAllRequestResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NoError(t, err) - require.Equal(t, len(objs), int(resp.Pagination.Total)) - require.ElementsMatch(t, objs, resp.Request) + require.Equal(t, len(requests), int(resp.Pagination.Total)) + require.ElementsMatch(t, requests, resp.Request) }) } diff --git a/x/launch/client/cli/query_test.go b/x/launch/client/cli/query_test.go new file mode 100644 index 000000000..d7b546208 --- /dev/null +++ b/x/launch/client/cli/query_test.go @@ -0,0 +1,19 @@ +package cli_test + +import ( + "testing" + + "github.com/stretchr/testify/suite" + + "github.com/tendermint/spn/testutil/networksuite" +) + +// QueryTestSuite is a test suite for query tests +type QueryTestSuite struct { + networksuite.NetworkTestSuite +} + +// TestQueryTestSuite runs test of the query suite +func TestQueryTestSuite(t *testing.T) { + suite.Run(t, new(QueryTestSuite)) +} diff --git a/x/launch/client/cli/query_vesting_account_test.go b/x/launch/client/cli/query_vesting_account_test.go index 4ff73865f..c9f9c4c5f 100644 --- a/x/launch/client/cli/query_vesting_account_test.go +++ b/x/launch/client/cli/query_vesting_account_test.go @@ -12,35 +12,14 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "github.com/tendermint/spn/testutil/network" - "github.com/tendermint/spn/testutil/sample" "github.com/tendermint/spn/x/launch/client/cli" "github.com/tendermint/spn/x/launch/types" ) -func networkWithVestingAccountObjects(t *testing.T, n int) (*network.Network, []types.VestingAccount) { - t.Helper() - r := sample.Rand() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) +func (suite *QueryTestSuite) TestShowVestingAccount() { + ctx := suite.Network.Validators[0].ClientCtx + accs := suite.LaunchState.VestingAccountList - for i := 0; i < n; i++ { - state.VestingAccountList = append( - state.VestingAccountList, - sample.VestingAccount(r, 0, strconv.Itoa(i)), - ) - } - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.VestingAccountList -} - -func TestShowVestingAccount(t *testing.T) { - net, objs := networkWithVestingAccountObjects(t, 2) - - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -55,11 +34,11 @@ func TestShowVestingAccount(t *testing.T) { }{ { desc: "found", - idChainID: strconv.Itoa(int(objs[0].LaunchID)), - idAddress: objs[0].Address, + idChainID: strconv.Itoa(int(accs[0].LaunchID)), + idAddress: accs[0].Address, args: common, - obj: objs[0], + obj: accs[0], }, { desc: "not found", @@ -70,7 +49,7 @@ func TestShowVestingAccount(t *testing.T) { err: status.Error(codes.NotFound, "not found"), }, } { - t.Run(tc.desc, func(t *testing.T) { + suite.T().Run(tc.desc, func(t *testing.T) { args := []string{ tc.idChainID, tc.idAddress, @@ -84,7 +63,7 @@ func TestShowVestingAccount(t *testing.T) { } else { require.NoError(t, err) var resp types.QueryGetVestingAccountResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.VestingAccount) require.Equal(t, tc.obj, resp.VestingAccount) } @@ -92,11 +71,11 @@ func TestShowVestingAccount(t *testing.T) { } } -func TestListVestingAccount(t *testing.T) { - net, objs := networkWithVestingAccountObjects(t, 5) +func (suite *QueryTestSuite) TestListVestingAccount() { + ctx := suite.Network.Validators[0].ClientCtx + accs := suite.LaunchState.VestingAccountList - chainID := strconv.Itoa(int(objs[0].LaunchID)) - ctx := net.Validators[0].ClientCtx + chainID := strconv.Itoa(int(accs[0].LaunchID)) request := func(chainID string, next []byte, offset, limit uint64, total bool) []string { args := []string{ chainID, @@ -113,40 +92,40 @@ func TestListVestingAccount(t *testing.T) { } return args } - t.Run("ByOffset", func(t *testing.T) { + suite.T().Run("ByOffset", func(t *testing.T) { step := 2 - for i := 0; i < len(objs); i += step { + for i := 0; i < len(accs); i += step { args := request(chainID, nil, uint64(i), uint64(step), false) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListVestingAccount(), args) require.NoError(t, err) var resp types.QueryAllVestingAccountResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.VestingAccount), step) - require.Subset(t, objs, resp.VestingAccount) + require.Subset(t, accs, resp.VestingAccount) } }) - t.Run("ByKey", func(t *testing.T) { + suite.T().Run("ByKey", func(t *testing.T) { step := 2 var next []byte - for i := 0; i < len(objs); i += step { + for i := 0; i < len(accs); i += step { args := request(chainID, next, 0, uint64(step), false) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListVestingAccount(), args) require.NoError(t, err) var resp types.QueryAllVestingAccountResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.VestingAccount), step) - require.Subset(t, objs, resp.VestingAccount) + require.Subset(t, accs, resp.VestingAccount) next = resp.Pagination.NextKey } }) - t.Run("Total", func(t *testing.T) { - args := request(chainID, nil, 0, uint64(len(objs)), true) + suite.T().Run("Total", func(t *testing.T) { + args := request(chainID, nil, 0, uint64(len(accs)), true) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListVestingAccount(), args) require.NoError(t, err) var resp types.QueryAllVestingAccountResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NoError(t, err) - require.Equal(t, len(objs), int(resp.Pagination.Total)) - require.ElementsMatch(t, objs, resp.VestingAccount) + require.Equal(t, len(accs), int(resp.Pagination.Total)) + require.ElementsMatch(t, accs, resp.VestingAccount) }) } diff --git a/x/monitoringc/client/cli/query_launch_id_from_channel_id_test.go b/x/monitoringc/client/cli/query_launch_id_from_channel_id_test.go index 794ac0ac1..17688609c 100644 --- a/x/monitoringc/client/cli/query_launch_id_from_channel_id_test.go +++ b/x/monitoringc/client/cli/query_launch_id_from_channel_id_test.go @@ -12,35 +12,15 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "github.com/tendermint/spn/testutil/network" "github.com/tendermint/spn/testutil/nullify" "github.com/tendermint/spn/x/monitoringc/client/cli" "github.com/tendermint/spn/x/monitoringc/types" ) -func networkWithLaunchIDFromChannelIDObjects(t *testing.T, n int) (*network.Network, []types.LaunchIDFromChannelID) { - t.Helper() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) +func (suite *QueryTestSuite) TestShowLaunchIDFromChannelID() { + ctx := suite.Network.Validators[0].ClientCtx + objs := suite.MonitoringcState.LaunchIDFromChannelIDList - for i := 0; i < n; i++ { - launchIDFromChannelID := types.LaunchIDFromChannelID{ - ChannelID: strconv.Itoa(i), - } - nullify.Fill(&launchIDFromChannelID) - state.LaunchIDFromChannelIDList = append(state.LaunchIDFromChannelIDList, launchIDFromChannelID) - } - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.LaunchIDFromChannelIDList -} - -func TestShowLaunchIDFromChannelID(t *testing.T) { - net, objs := networkWithLaunchIDFromChannelIDObjects(t, 2) - - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -67,7 +47,7 @@ func TestShowLaunchIDFromChannelID(t *testing.T) { err: status.Error(codes.NotFound, "not found"), }, } { - t.Run(tc.desc, func(t *testing.T) { + suite.T().Run(tc.desc, func(t *testing.T) { args := []string{ tc.idChannelID, } @@ -80,7 +60,7 @@ func TestShowLaunchIDFromChannelID(t *testing.T) { } else { require.NoError(t, err) var resp types.QueryGetLaunchIDFromChannelIDResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.LaunchIDFromChannelID) require.Equal(t, nullify.Fill(&tc.obj), @@ -91,10 +71,10 @@ func TestShowLaunchIDFromChannelID(t *testing.T) { } } -func TestListLaunchIDFromChannelID(t *testing.T) { - net, objs := networkWithLaunchIDFromChannelIDObjects(t, 5) +func (suite *QueryTestSuite) TestListLaunchIDFromChannelID() { + ctx := suite.Network.Validators[0].ClientCtx + objs := suite.MonitoringcState.LaunchIDFromChannelIDList - ctx := net.Validators[0].ClientCtx request := func(next []byte, offset, limit uint64, total bool) []string { args := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), @@ -110,14 +90,14 @@ func TestListLaunchIDFromChannelID(t *testing.T) { } return args } - t.Run("by offset", func(t *testing.T) { + suite.T().Run("by offset", 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.CmdListLaunchIDFromChannelID(), args) require.NoError(t, err) var resp types.QueryAllLaunchIDFromChannelIDResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.LaunchIDFromChannelID), step) require.Subset(t, nullify.Fill(objs), @@ -125,7 +105,7 @@ func TestListLaunchIDFromChannelID(t *testing.T) { ) } }) - t.Run("by key", func(t *testing.T) { + suite.T().Run("by key", func(t *testing.T) { step := 2 var next []byte for i := 0; i < len(objs); i += step { @@ -133,7 +113,7 @@ func TestListLaunchIDFromChannelID(t *testing.T) { out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListLaunchIDFromChannelID(), args) require.NoError(t, err) var resp types.QueryAllLaunchIDFromChannelIDResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.LaunchIDFromChannelID), step) require.Subset(t, nullify.Fill(objs), @@ -142,12 +122,12 @@ func TestListLaunchIDFromChannelID(t *testing.T) { next = resp.Pagination.NextKey } }) - t.Run("total", func(t *testing.T) { + suite.T().Run("total", func(t *testing.T) { args := request(nil, 0, uint64(len(objs)), true) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListLaunchIDFromChannelID(), args) require.NoError(t, err) var resp types.QueryAllLaunchIDFromChannelIDResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NoError(t, err) require.Equal(t, len(objs), int(resp.Pagination.Total)) require.ElementsMatch(t, diff --git a/x/monitoringc/client/cli/query_monitoring_history_test.go b/x/monitoringc/client/cli/query_monitoring_history_test.go index 054d54bbd..7e314b15b 100644 --- a/x/monitoringc/client/cli/query_monitoring_history_test.go +++ b/x/monitoringc/client/cli/query_monitoring_history_test.go @@ -11,35 +11,15 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "github.com/tendermint/spn/testutil/network" "github.com/tendermint/spn/testutil/nullify" "github.com/tendermint/spn/x/monitoringc/client/cli" "github.com/tendermint/spn/x/monitoringc/types" ) -func networkWithMonitoringHistoryObjects(t *testing.T, n int) (*network.Network, []types.MonitoringHistory) { - t.Helper() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) +func (suite *QueryTestSuite) TestShowMonitoringHistory() { + ctx := suite.Network.Validators[0].ClientCtx + objs := suite.MonitoringcState.MonitoringHistoryList - for i := 0; i < n; i++ { - monitoringHistory := types.MonitoringHistory{ - LaunchID: uint64(i), - } - nullify.Fill(&monitoringHistory) - state.MonitoringHistoryList = append(state.MonitoringHistoryList, monitoringHistory) - } - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.MonitoringHistoryList -} - -func TestShowMonitoringHistory(t *testing.T) { - net, objs := networkWithMonitoringHistoryObjects(t, 2) - - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -66,7 +46,7 @@ func TestShowMonitoringHistory(t *testing.T) { err: status.Error(codes.NotFound, "not found"), }, } { - t.Run(tc.desc, func(t *testing.T) { + suite.T().Run(tc.desc, func(t *testing.T) { args := []string{ strconv.Itoa(int(tc.idLaunchID)), } @@ -79,7 +59,7 @@ func TestShowMonitoringHistory(t *testing.T) { } else { require.NoError(t, err) var resp types.QueryGetMonitoringHistoryResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.MonitoringHistory) require.Equal(t, nullify.Fill(&tc.obj), diff --git a/x/monitoringc/client/cli/query_provider_client_id_test.go b/x/monitoringc/client/cli/query_provider_client_id_test.go index f8f584338..b26c89441 100644 --- a/x/monitoringc/client/cli/query_provider_client_id_test.go +++ b/x/monitoringc/client/cli/query_provider_client_id_test.go @@ -12,35 +12,15 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "github.com/tendermint/spn/testutil/network" "github.com/tendermint/spn/testutil/nullify" "github.com/tendermint/spn/x/monitoringc/client/cli" "github.com/tendermint/spn/x/monitoringc/types" ) -func networkWithProviderClientIDObjects(t *testing.T, n int) (*network.Network, []types.ProviderClientID) { - t.Helper() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) +func (suite *QueryTestSuite) TestShowProviderClientID() { + ctx := suite.Network.Validators[0].ClientCtx + objs := suite.MonitoringcState.ProviderClientIDList - for i := 0; i < n; i++ { - providerClientID := types.ProviderClientID{ - LaunchID: uint64(i), - } - nullify.Fill(&providerClientID) - state.ProviderClientIDList = append(state.ProviderClientIDList, providerClientID) - } - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.ProviderClientIDList -} - -func TestShowProviderClientID(t *testing.T) { - net, objs := networkWithProviderClientIDObjects(t, 2) - - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -67,7 +47,7 @@ func TestShowProviderClientID(t *testing.T) { err: status.Error(codes.NotFound, "not found"), }, } { - t.Run(tc.desc, func(t *testing.T) { + suite.T().Run(tc.desc, func(t *testing.T) { args := []string{ strconv.Itoa(int(tc.idLaunchID)), } @@ -80,7 +60,7 @@ func TestShowProviderClientID(t *testing.T) { } else { require.NoError(t, err) var resp types.QueryGetProviderClientIDResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.ProviderClientID) require.Equal(t, nullify.Fill(&tc.obj), @@ -91,10 +71,10 @@ func TestShowProviderClientID(t *testing.T) { } } -func TestListProviderClientID(t *testing.T) { - net, objs := networkWithProviderClientIDObjects(t, 5) +func (suite *QueryTestSuite) TestListProviderClientID() { + ctx := suite.Network.Validators[0].ClientCtx + objs := suite.MonitoringcState.ProviderClientIDList - ctx := net.Validators[0].ClientCtx request := func(next []byte, offset, limit uint64, total bool) []string { args := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), @@ -110,14 +90,14 @@ func TestListProviderClientID(t *testing.T) { } return args } - t.Run("by offset", func(t *testing.T) { + suite.T().Run("by offset", 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.CmdListProviderClientID(), args) require.NoError(t, err) var resp types.QueryAllProviderClientIDResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.ProviderClientID), step) require.Subset(t, nullify.Fill(objs), @@ -125,7 +105,7 @@ func TestListProviderClientID(t *testing.T) { ) } }) - t.Run("by key", func(t *testing.T) { + suite.T().Run("by key", func(t *testing.T) { step := 2 var next []byte for i := 0; i < len(objs); i += step { @@ -133,7 +113,7 @@ func TestListProviderClientID(t *testing.T) { out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListProviderClientID(), args) require.NoError(t, err) var resp types.QueryAllProviderClientIDResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.ProviderClientID), step) require.Subset(t, nullify.Fill(objs), @@ -142,12 +122,12 @@ func TestListProviderClientID(t *testing.T) { next = resp.Pagination.NextKey } }) - t.Run("total", func(t *testing.T) { + suite.T().Run("total", func(t *testing.T) { args := request(nil, 0, uint64(len(objs)), true) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListProviderClientID(), args) require.NoError(t, err) var resp types.QueryAllProviderClientIDResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NoError(t, err) require.Equal(t, len(objs), int(resp.Pagination.Total)) require.ElementsMatch(t, diff --git a/x/monitoringc/client/cli/query_test.go b/x/monitoringc/client/cli/query_test.go new file mode 100644 index 000000000..d7b546208 --- /dev/null +++ b/x/monitoringc/client/cli/query_test.go @@ -0,0 +1,19 @@ +package cli_test + +import ( + "testing" + + "github.com/stretchr/testify/suite" + + "github.com/tendermint/spn/testutil/networksuite" +) + +// QueryTestSuite is a test suite for query tests +type QueryTestSuite struct { + networksuite.NetworkTestSuite +} + +// TestQueryTestSuite runs test of the query suite +func TestQueryTestSuite(t *testing.T) { + suite.Run(t, new(QueryTestSuite)) +} diff --git a/x/monitoringc/client/cli/query_verified_client_ids_test.go b/x/monitoringc/client/cli/query_verified_client_ids_test.go index 121737f2d..eaf1e55b7 100644 --- a/x/monitoringc/client/cli/query_verified_client_ids_test.go +++ b/x/monitoringc/client/cli/query_verified_client_ids_test.go @@ -11,35 +11,15 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "github.com/tendermint/spn/testutil/network" "github.com/tendermint/spn/testutil/nullify" "github.com/tendermint/spn/x/monitoringc/client/cli" "github.com/tendermint/spn/x/monitoringc/types" ) -func networkWithRewardPoolObjects(t *testing.T, n int) (*network.Network, []types.VerifiedClientID) { - t.Helper() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) +func (suite *QueryTestSuite) TestShowVerifiedClientIds() { + ctx := suite.Network.Validators[0].ClientCtx + objs := suite.MonitoringcState.VerifiedClientIDList - for i := 0; i < n; i++ { - rewardPool := types.VerifiedClientID{ - LaunchID: uint64(i), - } - nullify.Fill(&rewardPool) - state.VerifiedClientIDList = append(state.VerifiedClientIDList, rewardPool) - } - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.VerifiedClientIDList -} - -func TestShowRewardPool(t *testing.T) { - net, objs := networkWithRewardPoolObjects(t, 2) - - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -66,7 +46,7 @@ func TestShowRewardPool(t *testing.T) { err: status.Error(codes.NotFound, "not found"), }, } { - t.Run(tc.desc, func(t *testing.T) { + suite.T().Run(tc.desc, func(t *testing.T) { args := []string{ strconv.Itoa(int(tc.idLaunchID)), } @@ -79,7 +59,7 @@ func TestShowRewardPool(t *testing.T) { } else { require.NoError(t, err) var resp types.QueryGetVerifiedClientIdsResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.ClientIds) require.Equal(t, nullify.Fill(&tc.obj.ClientIDs), diff --git a/x/participation/client/cli/query_auction_used_allocations_test.go b/x/participation/client/cli/query_auction_used_allocations_test.go index 35f8c2e07..ce13e93bf 100644 --- a/x/participation/client/cli/query_auction_used_allocations_test.go +++ b/x/participation/client/cli/query_auction_used_allocations_test.go @@ -12,59 +12,15 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "github.com/tendermint/spn/testutil/network" "github.com/tendermint/spn/testutil/nullify" - "github.com/tendermint/spn/testutil/sample" "github.com/tendermint/spn/x/participation/client/cli" "github.com/tendermint/spn/x/participation/types" ) -func networkWithAuctionUsedAllocationsObjects(t *testing.T, n int) (*network.Network, []types.AuctionUsedAllocations) { - t.Helper() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) +func (suite *QueryTestSuite) TestShowAuctionUsedAllocations() { + ctx := suite.Network.Validators[0].ClientCtx + objs := suite.ParticipationState.AuctionUsedAllocationsList - for i := 0; i < n; i++ { - auctionUsedAllocations := types.AuctionUsedAllocations{ - Address: strconv.Itoa(i), - AuctionID: uint64(i), - } - nullify.Fill(&auctionUsedAllocations) - state.AuctionUsedAllocationsList = append(state.AuctionUsedAllocationsList, auctionUsedAllocations) - } - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.AuctionUsedAllocationsList -} - -func networkWithAuctionUsedAllocationsObjectsWithSameAddress(t *testing.T, n int) (*network.Network, []types.AuctionUsedAllocations) { - t.Helper() - r := sample.Rand() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) - - address := sample.Address(r) - for i := 0; i < n; i++ { - auctionUsedAllocations := types.AuctionUsedAllocations{ - Address: address, - AuctionID: uint64(i), - } - nullify.Fill(&auctionUsedAllocations) - state.AuctionUsedAllocationsList = append(state.AuctionUsedAllocationsList, auctionUsedAllocations) - } - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.AuctionUsedAllocationsList -} - -func TestShowAuctionUsedAllocations(t *testing.T) { - net, objs := networkWithAuctionUsedAllocationsObjects(t, 2) - - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -91,7 +47,7 @@ func TestShowAuctionUsedAllocations(t *testing.T) { err: status.Error(codes.NotFound, "not found"), }, } { - t.Run(tc.desc, func(t *testing.T) { + suite.T().Run(tc.desc, func(t *testing.T) { args := []string{ tc.idAddress, strconv.FormatUint(tc.idAuctionID, 10), @@ -105,7 +61,7 @@ func TestShowAuctionUsedAllocations(t *testing.T) { } else { require.NoError(t, err) var resp types.QueryGetAuctionUsedAllocationsResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.AuctionUsedAllocations) require.Equal(t, nullify.Fill(&tc.obj), @@ -116,11 +72,12 @@ func TestShowAuctionUsedAllocations(t *testing.T) { } } -func TestListAuctionUsedAllocations(t *testing.T) { - net, objs := networkWithAuctionUsedAllocationsObjectsWithSameAddress(t, 5) +func (suite *QueryTestSuite) TestListAuctionUsedAllocations() { + ctx := suite.Network.Validators[0].ClientCtx + objs := suite.ParticipationState.AuctionUsedAllocationsList + address := objs[0].Address - ctx := net.Validators[0].ClientCtx request := func(addr string, next []byte, offset, limit uint64, total bool) []string { args := []string{ addr, @@ -137,14 +94,14 @@ func TestListAuctionUsedAllocations(t *testing.T) { } return args } - t.Run("ByOffset", func(t *testing.T) { + suite.T().Run("ByOffset", func(t *testing.T) { step := 2 for i := 0; i < len(objs); i += step { args := request(address, nil, uint64(i), uint64(step), false) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListAuctionUsedAllocations(), args) require.NoError(t, err) var resp types.QueryAllAuctionUsedAllocationsResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.AuctionUsedAllocations), step) require.Subset(t, nullify.Fill(objs), @@ -152,7 +109,7 @@ func TestListAuctionUsedAllocations(t *testing.T) { ) } }) - t.Run("ByKey", func(t *testing.T) { + suite.T().Run("ByKey", func(t *testing.T) { step := 2 var next []byte for i := 0; i < len(objs); i += step { @@ -160,7 +117,7 @@ func TestListAuctionUsedAllocations(t *testing.T) { out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListAuctionUsedAllocations(), args) require.NoError(t, err) var resp types.QueryAllAuctionUsedAllocationsResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.AuctionUsedAllocations), step) require.Subset(t, nullify.Fill(objs), @@ -169,12 +126,12 @@ func TestListAuctionUsedAllocations(t *testing.T) { next = resp.Pagination.NextKey } }) - t.Run("Total", func(t *testing.T) { + suite.T().Run("Total", func(t *testing.T) { args := request(address, nil, 0, uint64(len(objs)), true) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListAuctionUsedAllocations(), args) require.NoError(t, err) var resp types.QueryAllAuctionUsedAllocationsResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NoError(t, err) require.Equal(t, len(objs), int(resp.Pagination.Total)) require.ElementsMatch(t, diff --git a/x/participation/client/cli/query_available_allocations_test.go b/x/participation/client/cli/query_available_allocations_test.go deleted file mode 100644 index 80908db52..000000000 --- a/x/participation/client/cli/query_available_allocations_test.go +++ /dev/null @@ -1,70 +0,0 @@ -package cli_test - -import ( - "fmt" - "strconv" - "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/codes" - "google.golang.org/grpc/status" - - "github.com/tendermint/spn/testutil/nullify" - "github.com/tendermint/spn/x/participation/client/cli" - "github.com/tendermint/spn/x/participation/types" -) - -func TestShowAvailableAllocations(t *testing.T) { - net, totalAlloc := networkWithDelegations(t) - - ctx := net.Validators[0].ClientCtx - - delAddr := net.Validators[0].Address.String() - - common := []string{ - fmt.Sprintf("--%s=json", tmcli.OutputFlag), - } - for _, tc := range []struct { - desc string - delAddress string - args []string - err error - alloc uint64 - }{ - { - desc: "found", - delAddress: delAddr, - args: common, - alloc: totalAlloc, - }, - { - desc: "not found", - delAddress: strconv.Itoa(100000), - args: common, - err: status.Error(codes.InvalidArgument, "not found"), - }, - } { - t.Run(tc.desc, func(t *testing.T) { - args := []string{ - tc.delAddress, - } - args = append(args, tc.args...) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowAvailableAllocations(), 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.QueryGetAvailableAllocationsResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.Equal(t, - nullify.Fill(&tc.alloc), - nullify.Fill(&resp.AvailableAllocations), - ) - } - }) - } -} diff --git a/x/participation/client/cli/query_test.go b/x/participation/client/cli/query_test.go new file mode 100644 index 000000000..d7b546208 --- /dev/null +++ b/x/participation/client/cli/query_test.go @@ -0,0 +1,19 @@ +package cli_test + +import ( + "testing" + + "github.com/stretchr/testify/suite" + + "github.com/tendermint/spn/testutil/networksuite" +) + +// QueryTestSuite is a test suite for query tests +type QueryTestSuite struct { + networksuite.NetworkTestSuite +} + +// TestQueryTestSuite runs test of the query suite +func TestQueryTestSuite(t *testing.T) { + suite.Run(t, new(QueryTestSuite)) +} diff --git a/x/participation/client/cli/query_total_allocations_test.go b/x/participation/client/cli/query_total_allocations_test.go deleted file mode 100644 index c01f58b42..000000000 --- a/x/participation/client/cli/query_total_allocations_test.go +++ /dev/null @@ -1,91 +0,0 @@ -package cli_test - -import ( - "fmt" - "math/rand" - "strconv" - "testing" - - 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/testutil/nullify" - "github.com/tendermint/spn/x/participation/client/cli" - "github.com/tendermint/spn/x/participation/types" -) - -func networkWithDelegations(t *testing.T) (*network.Network, uint64) { - t.Helper() - cfg := network.DefaultConfig() - - // validator amount delegated to self - totalShares := cfg.BondedTokens.ToDec() - - allocationPrice := types.AllocationPrice{Bonded: sdk.NewInt(rand.Int63n(100))} - participationState := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &participationState)) - participationState.Params.AllocationPrice = allocationPrice - buf, err := cfg.Codec.MarshalJSON(&participationState) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - - return network.New(t, cfg), uint64(totalShares.Quo(allocationPrice.Bonded.ToDec()).TruncateInt64()) -} - -func TestShowTotalAllocations(t *testing.T) { - net, totalAlloc := networkWithDelegations(t) - - ctx := net.Validators[0].ClientCtx - - delAddr := net.Validators[0].Address.String() - - common := []string{ - fmt.Sprintf("--%s=json", tmcli.OutputFlag), - } - for _, tc := range []struct { - desc string - delAddress string - args []string - err error - alloc uint64 - }{ - { - desc: "found", - delAddress: delAddr, - args: common, - alloc: totalAlloc, - }, - { - desc: "not found", - delAddress: strconv.Itoa(100000), - args: common, - err: status.Error(codes.NotFound, "not found"), - }, - } { - t.Run(tc.desc, func(t *testing.T) { - args := []string{ - tc.delAddress, - } - args = append(args, tc.args...) - out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowTotalAllocations(), 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.QueryGetTotalAllocationsResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) - require.Equal(t, - nullify.Fill(&tc.alloc), - nullify.Fill(&resp.TotalAllocations), - ) - } - }) - } -} diff --git a/x/participation/client/cli/query_used_allocations_test.go b/x/participation/client/cli/query_used_allocations_test.go index 7a579125e..4d96b9a9a 100644 --- a/x/participation/client/cli/query_used_allocations_test.go +++ b/x/participation/client/cli/query_used_allocations_test.go @@ -12,37 +12,15 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "github.com/tendermint/spn/testutil/network" "github.com/tendermint/spn/testutil/nullify" - "github.com/tendermint/spn/testutil/sample" "github.com/tendermint/spn/x/participation/client/cli" "github.com/tendermint/spn/x/participation/types" ) -func networkWithUsedAllocationsObjects(t *testing.T, n int) (*network.Network, []types.UsedAllocations) { - t.Helper() - r := sample.Rand() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) +func (suite *QueryTestSuite) TestShowUsedAllocations() { + ctx := suite.Network.Validators[0].ClientCtx + objs := suite.ParticipationState.UsedAllocationsList - for i := 0; i < n; i++ { - usedAllocations := types.UsedAllocations{ - Address: sample.Address(r), - } - nullify.Fill(&usedAllocations) - state.UsedAllocationsList = append(state.UsedAllocationsList, usedAllocations) - } - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.UsedAllocationsList -} - -func TestShowUsedAllocations(t *testing.T) { - net, objs := networkWithUsedAllocationsObjects(t, 2) - - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -66,7 +44,7 @@ func TestShowUsedAllocations(t *testing.T) { err: status.Error(codes.NotFound, "not found"), }, } { - t.Run(tc.desc, func(t *testing.T) { + suite.T().Run(tc.desc, func(t *testing.T) { args := []string{ tc.idAddress, } @@ -79,7 +57,7 @@ func TestShowUsedAllocations(t *testing.T) { } else { require.NoError(t, err) var resp types.QueryGetUsedAllocationsResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.UsedAllocations) require.Equal(t, nullify.Fill(&tc.obj), @@ -90,10 +68,10 @@ func TestShowUsedAllocations(t *testing.T) { } } -func TestListUsedAllocations(t *testing.T) { - net, objs := networkWithUsedAllocationsObjects(t, 5) +func (suite *QueryTestSuite) TestListUsedAllocations() { + ctx := suite.Network.Validators[0].ClientCtx + objs := suite.ParticipationState.UsedAllocationsList - ctx := net.Validators[0].ClientCtx request := func(next []byte, offset, limit uint64, total bool) []string { args := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), @@ -109,14 +87,14 @@ func TestListUsedAllocations(t *testing.T) { } return args } - t.Run("ByOffset", func(t *testing.T) { + suite.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.CmdListUsedAllocations(), args) require.NoError(t, err) var resp types.QueryAllUsedAllocationsResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.UsedAllocations), step) require.Subset(t, nullify.Fill(objs), @@ -124,7 +102,7 @@ func TestListUsedAllocations(t *testing.T) { ) } }) - t.Run("ByKey", func(t *testing.T) { + suite.T().Run("ByKey", func(t *testing.T) { step := 2 var next []byte for i := 0; i < len(objs); i += step { @@ -132,7 +110,7 @@ func TestListUsedAllocations(t *testing.T) { out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListUsedAllocations(), args) require.NoError(t, err) var resp types.QueryAllUsedAllocationsResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.UsedAllocations), step) require.Subset(t, nullify.Fill(objs), @@ -141,12 +119,12 @@ func TestListUsedAllocations(t *testing.T) { next = resp.Pagination.NextKey } }) - t.Run("Total", func(t *testing.T) { + suite.T().Run("Total", func(t *testing.T) { args := request(nil, 0, uint64(len(objs)), true) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListUsedAllocations(), args) require.NoError(t, err) var resp types.QueryAllUsedAllocationsResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NoError(t, err) require.Equal(t, len(objs), int(resp.Pagination.Total)) require.ElementsMatch(t, diff --git a/x/profile/client/cli/query_coordinator_by_address_test.go b/x/profile/client/cli/query_coordinator_by_address_test.go index 0e66c688a..5f2d5da56 100644 --- a/x/profile/client/cli/query_coordinator_by_address_test.go +++ b/x/profile/client/cli/query_coordinator_by_address_test.go @@ -2,7 +2,6 @@ package cli_test import ( "fmt" - "strconv" "testing" clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" @@ -11,30 +10,14 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "github.com/tendermint/spn/testutil/network" "github.com/tendermint/spn/x/profile/client/cli" "github.com/tendermint/spn/x/profile/types" ) -func networkWithCoordinatorByAddressObjects(t *testing.T, n int) (*network.Network, []types.CoordinatorByAddress) { - t.Helper() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) +func (suite *QueryTestSuite) TestShowCoordinatorByAddress() { + ctx := suite.Network.Validators[0].ClientCtx + objs := suite.ProfileState.CoordinatorByAddressList - for i := 0; i < n; i++ { - state.CoordinatorByAddressList = append(state.CoordinatorByAddressList, types.CoordinatorByAddress{Address: "cosmos" + strconv.Itoa(i)}) - } - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.CoordinatorByAddressList -} - -func TestShowCoordinatorByAddress(t *testing.T) { - net, objs := networkWithCoordinatorByAddressObjects(t, 2) - - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -58,7 +41,7 @@ func TestShowCoordinatorByAddress(t *testing.T) { err: status.Error(codes.NotFound, "not found"), }, } { - t.Run(tc.desc, func(t *testing.T) { + suite.T().Run(tc.desc, func(t *testing.T) { args := []string{tc.id} args = append(args, tc.args...) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowCoordinatorByAddress(), args) @@ -69,7 +52,7 @@ func TestShowCoordinatorByAddress(t *testing.T) { } else { require.NoError(t, err) var resp types.QueryGetCoordinatorByAddressResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.CoordinatorByAddress) require.Equal(t, tc.obj, resp.CoordinatorByAddress) } diff --git a/x/profile/client/cli/query_coordinator_test.go b/x/profile/client/cli/query_coordinator_test.go index 6c2ba9c4d..6509292d3 100644 --- a/x/profile/client/cli/query_coordinator_test.go +++ b/x/profile/client/cli/query_coordinator_test.go @@ -11,30 +11,14 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "github.com/tendermint/spn/testutil/network" "github.com/tendermint/spn/x/profile/client/cli" "github.com/tendermint/spn/x/profile/types" ) -func networkWithCoordinatorObjects(t *testing.T, n int) (*network.Network, []types.Coordinator) { - t.Helper() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) +func (suite *QueryTestSuite) TestShowCoordinator() { + ctx := suite.Network.Validators[0].ClientCtx + objs := suite.ProfileState.CoordinatorList - for i := 0; i < n; i++ { - state.CoordinatorList = append(state.CoordinatorList, types.Coordinator{CoordinatorID: uint64(i)}) - } - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.CoordinatorList -} - -func TestShowCoordinator(t *testing.T) { - net, objs := networkWithCoordinatorObjects(t, 2) - - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -58,7 +42,7 @@ func TestShowCoordinator(t *testing.T) { err: status.Error(codes.NotFound, "not found"), }, } { - t.Run(tc.desc, func(t *testing.T) { + suite.T().Run(tc.desc, func(t *testing.T) { args := []string{tc.id} args = append(args, tc.args...) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowCoordinator(), args) @@ -69,7 +53,7 @@ func TestShowCoordinator(t *testing.T) { } else { require.NoError(t, err) var resp types.QueryGetCoordinatorResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.Coordinator) require.Equal(t, tc.obj, resp.Coordinator) } @@ -77,10 +61,10 @@ func TestShowCoordinator(t *testing.T) { } } -func TestListCoordinator(t *testing.T) { - net, objs := networkWithCoordinatorObjects(t, 5) +func (suite *QueryTestSuite) TestListCoordinator() { + ctx := suite.Network.Validators[0].ClientCtx + objs := suite.ProfileState.CoordinatorList - ctx := net.Validators[0].ClientCtx request := func(next []byte, offset, limit uint64, total bool) []string { args := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), @@ -96,19 +80,19 @@ func TestListCoordinator(t *testing.T) { } return args } - t.Run("ByOffset", func(t *testing.T) { + suite.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.CmdListCoordinator(), args) require.NoError(t, err) var resp types.QueryAllCoordinatorResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.Coordinator), step) require.Subset(t, objs, resp.Coordinator) } }) - t.Run("ByKey", func(t *testing.T) { + suite.T().Run("ByKey", func(t *testing.T) { step := 2 var next []byte for i := 0; i < len(objs); i += step { @@ -116,18 +100,18 @@ func TestListCoordinator(t *testing.T) { out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListCoordinator(), args) require.NoError(t, err) var resp types.QueryAllCoordinatorResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.Coordinator), step) require.Subset(t, objs, resp.Coordinator) next = resp.Pagination.NextKey } }) - t.Run("Total", func(t *testing.T) { + suite.T().Run("Total", func(t *testing.T) { args := request(nil, 0, uint64(len(objs)), true) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListCoordinator(), args) require.NoError(t, err) var resp types.QueryAllCoordinatorResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NoError(t, err) require.Equal(t, len(objs), int(resp.Pagination.Total)) require.ElementsMatch(t, objs, resp.Coordinator) diff --git a/x/profile/client/cli/query_test.go b/x/profile/client/cli/query_test.go new file mode 100644 index 000000000..d7b546208 --- /dev/null +++ b/x/profile/client/cli/query_test.go @@ -0,0 +1,19 @@ +package cli_test + +import ( + "testing" + + "github.com/stretchr/testify/suite" + + "github.com/tendermint/spn/testutil/networksuite" +) + +// QueryTestSuite is a test suite for query tests +type QueryTestSuite struct { + networksuite.NetworkTestSuite +} + +// TestQueryTestSuite runs test of the query suite +func TestQueryTestSuite(t *testing.T) { + suite.Run(t, new(QueryTestSuite)) +} diff --git a/x/profile/client/cli/query_validator_test.go b/x/profile/client/cli/query_validator_test.go index 3c5c7c24f..207198575 100644 --- a/x/profile/client/cli/query_validator_test.go +++ b/x/profile/client/cli/query_validator_test.go @@ -14,32 +14,14 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "github.com/tendermint/spn/testutil/network" "github.com/tendermint/spn/x/profile/client/cli" "github.com/tendermint/spn/x/profile/types" ) -func networkWithValidatorObjects(t *testing.T, n int) (*network.Network, []types.Validator) { - t.Helper() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) +func (suite *QueryTestSuite) TestShowValidator() { + ctx := suite.Network.Validators[0].ClientCtx + objs := suite.ProfileState.ValidatorList - for i := 0; i < n; i++ { - state.ValidatorList = append(state.ValidatorList, types.Validator{ - Address: strconv.Itoa(i), - }) - } - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.ValidatorList -} - -func TestShowValidator(t *testing.T) { - net, objs := networkWithValidatorObjects(t, 2) - - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -66,7 +48,7 @@ func TestShowValidator(t *testing.T) { err: status.Error(codes.NotFound, "not found"), }, } { - t.Run(tc.desc, func(t *testing.T) { + suite.T().Run(tc.desc, func(t *testing.T) { args := []string{ tc.idAddress, } @@ -79,7 +61,7 @@ func TestShowValidator(t *testing.T) { } else { require.NoError(t, err) var resp types.QueryGetValidatorResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.Validator) require.Equal(t, nullify.Fill(&tc.obj), nullify.Fill(&resp.Validator)) } @@ -87,10 +69,10 @@ func TestShowValidator(t *testing.T) { } } -func TestListValidator(t *testing.T) { - net, objs := networkWithValidatorObjects(t, 5) +func (suite *QueryTestSuite) TestListValidator() { + ctx := suite.Network.Validators[0].ClientCtx + objs := suite.ProfileState.ValidatorList - ctx := net.Validators[0].ClientCtx request := func(next []byte, offset, limit uint64, total bool) []string { args := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), @@ -106,19 +88,19 @@ func TestListValidator(t *testing.T) { } return args } - t.Run("ByOffset", func(t *testing.T) { + suite.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.CmdListValidator(), args) require.NoError(t, err) var resp types.QueryAllValidatorResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.Validator), step) require.Subset(t, nullify.Fill(objs), nullify.Fill(resp.Validator)) } }) - t.Run("ByKey", func(t *testing.T) { + suite.T().Run("ByKey", func(t *testing.T) { step := 2 var next []byte for i := 0; i < len(objs); i += step { @@ -126,18 +108,18 @@ func TestListValidator(t *testing.T) { out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListValidator(), args) require.NoError(t, err) var resp types.QueryAllValidatorResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.Validator), step) require.Subset(t, nullify.Fill(objs), nullify.Fill(resp.Validator)) next = resp.Pagination.NextKey } }) - t.Run("Total", func(t *testing.T) { + suite.T().Run("Total", func(t *testing.T) { args := request(nil, 0, uint64(len(objs)), true) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListValidator(), args) require.NoError(t, err) var resp types.QueryAllValidatorResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.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.Validator)) diff --git a/x/reward/client/cli/query_reward_pool_test.go b/x/reward/client/cli/query_reward_pool_test.go index 043c97b86..27f80909d 100644 --- a/x/reward/client/cli/query_reward_pool_test.go +++ b/x/reward/client/cli/query_reward_pool_test.go @@ -12,35 +12,15 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "github.com/tendermint/spn/testutil/network" "github.com/tendermint/spn/testutil/nullify" "github.com/tendermint/spn/x/reward/client/cli" "github.com/tendermint/spn/x/reward/types" ) -func networkWithRewardPoolObjects(t *testing.T, n int) (*network.Network, []types.RewardPool) { - t.Helper() - cfg := network.DefaultConfig() - state := types.GenesisState{} - require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state)) +func (suite *QueryTestSuite) TestShowRewardPool() { + ctx := suite.Network.Validators[0].ClientCtx + objs := suite.RewardState.RewardPoolList - for i := 0; i < n; i++ { - rewardPool := types.RewardPool{ - LaunchID: uint64(i), - } - nullify.Fill(&rewardPool) - state.RewardPoolList = append(state.RewardPoolList, rewardPool) - } - buf, err := cfg.Codec.MarshalJSON(&state) - require.NoError(t, err) - cfg.GenesisState[types.ModuleName] = buf - return network.New(t, cfg), state.RewardPoolList -} - -func TestShowRewardPool(t *testing.T) { - net, objs := networkWithRewardPoolObjects(t, 2) - - ctx := net.Validators[0].ClientCtx common := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), } @@ -67,7 +47,7 @@ func TestShowRewardPool(t *testing.T) { err: status.Error(codes.NotFound, "not found"), }, } { - t.Run(tc.desc, func(t *testing.T) { + suite.T().Run(tc.desc, func(t *testing.T) { args := []string{ strconv.Itoa(int(tc.idLaunchID)), } @@ -80,7 +60,7 @@ func TestShowRewardPool(t *testing.T) { } else { require.NoError(t, err) var resp types.QueryGetRewardPoolResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NotNil(t, resp.RewardPool) require.Equal(t, nullify.Fill(&tc.obj), @@ -91,10 +71,10 @@ func TestShowRewardPool(t *testing.T) { } } -func TestListRewardPool(t *testing.T) { - net, objs := networkWithRewardPoolObjects(t, 5) +func (suite *QueryTestSuite) TestListRewardPool() { + ctx := suite.Network.Validators[0].ClientCtx + objs := suite.RewardState.RewardPoolList - ctx := net.Validators[0].ClientCtx request := func(next []byte, offset, limit uint64, total bool) []string { args := []string{ fmt.Sprintf("--%s=json", tmcli.OutputFlag), @@ -110,14 +90,14 @@ func TestListRewardPool(t *testing.T) { } return args } - t.Run("ByOffset", func(t *testing.T) { + suite.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.CmdListRewardPool(), args) require.NoError(t, err) var resp types.QueryAllRewardPoolResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.RewardPool), step) require.Subset(t, nullify.Fill(objs), @@ -125,7 +105,7 @@ func TestListRewardPool(t *testing.T) { ) } }) - t.Run("ByKey", func(t *testing.T) { + suite.T().Run("ByKey", func(t *testing.T) { step := 2 var next []byte for i := 0; i < len(objs); i += step { @@ -133,7 +113,7 @@ func TestListRewardPool(t *testing.T) { out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListRewardPool(), args) require.NoError(t, err) var resp types.QueryAllRewardPoolResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.LessOrEqual(t, len(resp.RewardPool), step) require.Subset(t, nullify.Fill(objs), @@ -142,12 +122,12 @@ func TestListRewardPool(t *testing.T) { next = resp.Pagination.NextKey } }) - t.Run("Total", func(t *testing.T) { + suite.T().Run("Total", func(t *testing.T) { args := request(nil, 0, uint64(len(objs)), true) out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListRewardPool(), args) require.NoError(t, err) var resp types.QueryAllRewardPoolResponse - require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) + require.NoError(t, suite.Network.Config.Codec.UnmarshalJSON(out.Bytes(), &resp)) require.NoError(t, err) require.Equal(t, len(objs), int(resp.Pagination.Total)) require.ElementsMatch(t, diff --git a/x/reward/client/cli/query_test.go b/x/reward/client/cli/query_test.go new file mode 100644 index 000000000..d7b546208 --- /dev/null +++ b/x/reward/client/cli/query_test.go @@ -0,0 +1,19 @@ +package cli_test + +import ( + "testing" + + "github.com/stretchr/testify/suite" + + "github.com/tendermint/spn/testutil/networksuite" +) + +// QueryTestSuite is a test suite for query tests +type QueryTestSuite struct { + networksuite.NetworkTestSuite +} + +// TestQueryTestSuite runs test of the query suite +func TestQueryTestSuite(t *testing.T) { + suite.Run(t, new(QueryTestSuite)) +}