cosmos-sdk/x/gov/keeper/proposal_test.go
Amaury 1715693ca2
chore: Bump gov and group to v1 (#11334)
## Description

Closes: #11331



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2022-03-11 15:58:01 +00:00

155 lines
5.6 KiB
Go

package keeper_test
import (
"errors"
"fmt"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov/types"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
)
func (suite *KeeperTestSuite) TestGetSetProposal() {
tp := TestProposal
proposal, err := suite.app.GovKeeper.SubmitProposal(suite.ctx, tp, "")
suite.Require().NoError(err)
proposalID := proposal.Id
suite.app.GovKeeper.SetProposal(suite.ctx, proposal)
gotProposal, ok := suite.app.GovKeeper.GetProposal(suite.ctx, proposalID)
suite.Require().True(ok)
suite.Require().Equal(proposal, gotProposal)
}
func (suite *KeeperTestSuite) TestActivateVotingPeriod() {
tp := TestProposal
proposal, err := suite.app.GovKeeper.SubmitProposal(suite.ctx, tp, "")
suite.Require().NoError(err)
suite.Require().Nil(proposal.VotingStartTime)
suite.app.GovKeeper.ActivateVotingPeriod(suite.ctx, proposal)
proposal, ok := suite.app.GovKeeper.GetProposal(suite.ctx, proposal.Id)
suite.Require().True(ok)
suite.Require().True(proposal.VotingStartTime.Equal(suite.ctx.BlockHeader().Time))
activeIterator := suite.app.GovKeeper.ActiveProposalQueueIterator(suite.ctx, *proposal.VotingEndTime)
suite.Require().True(activeIterator.Valid())
proposalID := types.GetProposalIDFromBytes(activeIterator.Value())
suite.Require().Equal(proposalID, proposal.Id)
activeIterator.Close()
}
type invalidProposalRoute struct{ v1beta1.TextProposal }
func (invalidProposalRoute) ProposalRoute() string { return "nonexistingroute" }
func (suite *KeeperTestSuite) TestSubmitProposal() {
govAcct := suite.app.GovKeeper.GetGovernanceAccount(suite.ctx).GetAddress().String()
_, _, randomAddr := testdata.KeyTestPubAddr()
tp := v1beta1.TextProposal{Title: "title", Description: "description"}
testCases := []struct {
content v1beta1.Content
authority string
metadata string
expectedErr error
}{
{&tp, govAcct, "", nil},
// Keeper does not check the validity of title and description, no error
{&v1beta1.TextProposal{Title: "", Description: "description"}, govAcct, "", nil},
{&v1beta1.TextProposal{Title: strings.Repeat("1234567890", 100), Description: "description"}, govAcct, "", nil},
{&v1beta1.TextProposal{Title: "title", Description: ""}, govAcct, "", nil},
{&v1beta1.TextProposal{Title: "title", Description: strings.Repeat("1234567890", 1000)}, govAcct, "", nil},
// error when metadata is too long (>10000)
{&tp, govAcct, strings.Repeat("a", 100001), types.ErrMetadataTooLong},
// error when signer is not gov acct
{&tp, randomAddr.String(), "", types.ErrInvalidSigner},
// error only when invalid route
{&invalidProposalRoute{}, govAcct, "", types.ErrNoProposalHandlerExists},
}
for i, tc := range testCases {
prop, err := v1.NewLegacyContent(tc.content, tc.authority)
suite.Require().NoError(err)
_, err = suite.app.GovKeeper.SubmitProposal(suite.ctx, []sdk.Msg{prop}, tc.metadata)
suite.Require().True(errors.Is(tc.expectedErr, err), "tc #%d; got: %v, expected: %v", i, err, tc.expectedErr)
}
}
func (suite *KeeperTestSuite) TestGetProposalsFiltered() {
proposalID := uint64(1)
status := []v1.ProposalStatus{v1.StatusDepositPeriod, v1.StatusVotingPeriod}
addr1 := sdk.AccAddress("foo_________________")
for _, s := range status {
for i := 0; i < 50; i++ {
p, err := v1.NewProposal(TestProposal, proposalID, "", time.Now(), time.Now())
suite.Require().NoError(err)
p.Status = s
if i%2 == 0 {
d := v1.NewDeposit(proposalID, addr1, nil)
v := v1.NewVote(proposalID, addr1, v1.NewNonSplitVoteOption(v1.OptionYes), "")
suite.app.GovKeeper.SetDeposit(suite.ctx, d)
suite.app.GovKeeper.SetVote(suite.ctx, v)
}
suite.app.GovKeeper.SetProposal(suite.ctx, p)
proposalID++
}
}
testCases := []struct {
params v1.QueryProposalsParams
expectedNumResults int
}{
{v1.NewQueryProposalsParams(1, 50, v1.StatusNil, nil, nil), 50},
{v1.NewQueryProposalsParams(1, 50, v1.StatusDepositPeriod, nil, nil), 50},
{v1.NewQueryProposalsParams(1, 50, v1.StatusVotingPeriod, nil, nil), 50},
{v1.NewQueryProposalsParams(1, 25, v1.StatusNil, nil, nil), 25},
{v1.NewQueryProposalsParams(2, 25, v1.StatusNil, nil, nil), 25},
{v1.NewQueryProposalsParams(1, 50, v1.StatusRejected, nil, nil), 0},
{v1.NewQueryProposalsParams(1, 50, v1.StatusNil, addr1, nil), 50},
{v1.NewQueryProposalsParams(1, 50, v1.StatusNil, nil, addr1), 50},
{v1.NewQueryProposalsParams(1, 50, v1.StatusNil, addr1, addr1), 50},
{v1.NewQueryProposalsParams(1, 50, v1.StatusDepositPeriod, addr1, addr1), 25},
{v1.NewQueryProposalsParams(1, 50, v1.StatusDepositPeriod, nil, nil), 50},
{v1.NewQueryProposalsParams(1, 50, v1.StatusVotingPeriod, nil, nil), 50},
}
for i, tc := range testCases {
suite.Run(fmt.Sprintf("Test Case %d", i), func() {
proposals := suite.app.GovKeeper.GetProposalsFiltered(suite.ctx, tc.params)
suite.Require().Len(proposals, tc.expectedNumResults)
for _, p := range proposals {
if v1.ValidProposalStatus(tc.params.ProposalStatus) {
suite.Require().Equal(tc.params.ProposalStatus, p.Status)
}
}
})
}
}
func TestMigrateProposalMessages(t *testing.T) {
content := v1beta1.NewTextProposal("Test", "description")
contentMsg, err := v1.NewLegacyContent(content, sdk.AccAddress("test1").String())
require.NoError(t, err)
content, err = v1.LegacyContentFromMessage(contentMsg)
require.NoError(t, err)
require.Equal(t, "Test", content.GetTitle())
require.Equal(t, "description", content.GetDescription())
}