Skip to content

Commit

Permalink
test: add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaru Wang committed Oct 2, 2023
1 parent 6bf0f39 commit dc7371b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
3 changes: 1 addition & 2 deletions x/ccv/provider/keeper/gov_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ func (gh GovHooks) AfterProposalVotingPeriodEnded(ctx sdk.Context, proposalID ui
}

if consAdditionProp.ProposalType() == types.ProposalTypeConsumerAddition {
gh.k.DeleteChainsInProposal(ctx, consAdditionProp.ChainId, proposalID)
gh.k.DeleteProposedConsumerChainInStore(ctx, proposalID)
}

}
}

Expand Down
4 changes: 2 additions & 2 deletions x/ccv/provider/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ func (k Keeper) GetProposedConsumerChain(ctx sdk.Context, proposalID uint64) str
return string(store.Get(types.ProposedConsumerChainKey(proposalID)))
}

// DeleteChainsInProposal deletes the consumer chainID from store
// DeleteProposedConsumerChainInStore deletes the consumer chainID from store
// which is in gov consumerAddition proposal
func (k Keeper) DeleteChainsInProposal(ctx sdk.Context, chainID string, proposalID uint64) {
func (k Keeper) DeleteProposedConsumerChainInStore(ctx sdk.Context, proposalID uint64) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.ProposedConsumerChainKey(proposalID))
}
Expand Down
75 changes: 75 additions & 0 deletions x/ccv/provider/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ func TestSetProposedConsumerChains(t *testing.T) {
{chainID: "1", proposalID: 1},
{chainID: "some other ID", proposalID: 12},
{chainID: "some other other chain ID", proposalID: 123},
{chainID: "", proposalID: 1234},
}

for _, test := range tests {
Expand All @@ -553,3 +554,77 @@ func TestSetProposedConsumerChains(t *testing.T) {
require.Equal(t, cID, test.chainID)
}
}

func TestDeleteProposedConsumerChainInStore(t *testing.T) {
providerKeeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()

tests := []struct {
chainID string
proposalID uint64
deleteProposalID uint64
ok bool
}{
{chainID: "1", proposalID: 1, deleteProposalID: 1, ok: true},
{chainID: "", proposalID: 12, deleteProposalID: 12, ok: true},
{chainID: "1", proposalID: 0, deleteProposalID: 1, ok: false},
}
for _, test := range tests {
providerKeeper.SetProposedConsumerChain(ctx, test.chainID, test.proposalID)
providerKeeper.DeleteProposedConsumerChainInStore(ctx, test.deleteProposalID)
cID := providerKeeper.GetProposedConsumerChain(ctx, test.proposalID)
if test.ok {
require.Equal(t, cID, "")
} else {
require.Equal(t, cID, test.chainID)
}
}
}

func TestGetAllProposedConsumerChainIDs(t *testing.T) {
providerKeeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()
tests := [][]types.ProposedChain{
[]types.ProposedChain{},

Check failure on line 588 in x/ccv/provider/keeper/keeper_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
[]types.ProposedChain{
{
ChainID: "1",

Check failure on line 591 in x/ccv/provider/keeper/keeper_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(cosmossdk.io) -s prefix(github.com/cosmos/cosmos-sdk) -s prefix(github.com/cometbft/cometbft) -s prefix(github.com/cosmos/interchain-security) --custom-order (gci)
ProposalID: 1,
},
},
[]types.ProposedChain{

Check failure on line 595 in x/ccv/provider/keeper/keeper_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
{
ChainID: "1",

Check failure on line 597 in x/ccv/provider/keeper/keeper_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(cosmossdk.io) -s prefix(github.com/cosmos/cosmos-sdk) -s prefix(github.com/cometbft/cometbft) -s prefix(github.com/cosmos/interchain-security) --custom-order (gci)
ProposalID: 1,
},
{
ChainID: "2",

Check failure on line 601 in x/ccv/provider/keeper/keeper_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(cosmossdk.io) -s prefix(github.com/cosmos/cosmos-sdk) -s prefix(github.com/cometbft/cometbft) -s prefix(github.com/cosmos/interchain-security) --custom-order (gci)
ProposalID: 2,
},
{
ChainID: "",

Check failure on line 605 in x/ccv/provider/keeper/keeper_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(cosmossdk.io) -s prefix(github.com/cosmos/cosmos-sdk) -s prefix(github.com/cometbft/cometbft) -s prefix(github.com/cosmos/interchain-security) --custom-order (gci)
ProposalID: 3,
},
},
}

for _, test := range tests {
for _, tc := range test {
providerKeeper.SetProposedConsumerChain(ctx, tc.ChainID, tc.ProposalID)
}

chains := providerKeeper.GetAllProposedConsumerChainIDs(ctx)

sort.Slice(chains, func(i, j int) bool {
return chains[i].ProposalID < chains[j].ProposalID
})
sort.Slice(test, func(i, j int) bool {
return test[i].ProposalID < test[j].ProposalID
})
require.Equal(t, chains, test)

for _, tc := range test {
providerKeeper.DeleteProposedConsumerChainInStore(ctx, tc.ProposalID)
}
}
}

0 comments on commit dc7371b

Please sign in to comment.