2020-03-02 07:04:49 -08:00
|
|
|
package keeper_test
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
import (
|
2019-12-27 09:57:54 -08:00
|
|
|
"errors"
|
2020-08-11 07:22:30 -07:00
|
|
|
"fmt"
|
2019-08-08 12:51:18 -07:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types"
|
2021-12-13 10:48:44 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
|
2019-08-08 12:51:18 -07:00
|
|
|
)
|
|
|
|
|
2021-04-30 01:41:25 -07:00
|
|
|
func (suite *KeeperTestSuite) TestGetSetProposal() {
|
2019-08-08 12:51:18 -07:00
|
|
|
tp := TestProposal
|
2021-04-30 01:41:25 -07:00
|
|
|
proposal, err := suite.app.GovKeeper.SubmitProposal(suite.ctx, tp)
|
|
|
|
suite.Require().NoError(err)
|
2020-08-14 01:55:54 -07:00
|
|
|
proposalID := proposal.ProposalId
|
2021-04-30 01:41:25 -07:00
|
|
|
suite.app.GovKeeper.SetProposal(suite.ctx, proposal)
|
2019-08-08 12:51:18 -07:00
|
|
|
|
2021-04-30 01:41:25 -07:00
|
|
|
gotProposal, ok := suite.app.GovKeeper.GetProposal(suite.ctx, proposalID)
|
|
|
|
suite.Require().True(ok)
|
|
|
|
suite.Require().True(proposal.Equal(gotProposal))
|
2019-08-08 12:51:18 -07:00
|
|
|
}
|
|
|
|
|
2021-04-30 01:41:25 -07:00
|
|
|
func (suite *KeeperTestSuite) TestActivateVotingPeriod() {
|
2019-08-08 12:51:18 -07:00
|
|
|
tp := TestProposal
|
2021-04-30 01:41:25 -07:00
|
|
|
proposal, err := suite.app.GovKeeper.SubmitProposal(suite.ctx, tp)
|
|
|
|
suite.Require().NoError(err)
|
2019-08-08 12:51:18 -07:00
|
|
|
|
2021-04-30 01:41:25 -07:00
|
|
|
suite.Require().True(proposal.VotingStartTime.Equal(time.Time{}))
|
2019-08-08 12:51:18 -07:00
|
|
|
|
2021-04-30 01:41:25 -07:00
|
|
|
suite.app.GovKeeper.ActivateVotingPeriod(suite.ctx, proposal)
|
2019-08-08 12:51:18 -07:00
|
|
|
|
2021-04-30 01:41:25 -07:00
|
|
|
suite.Require().True(proposal.VotingStartTime.Equal(suite.ctx.BlockHeader().Time))
|
2019-08-08 12:51:18 -07:00
|
|
|
|
2021-04-30 01:41:25 -07:00
|
|
|
proposal, ok := suite.app.GovKeeper.GetProposal(suite.ctx, proposal.ProposalId)
|
|
|
|
suite.Require().True(ok)
|
2019-08-08 12:51:18 -07:00
|
|
|
|
2021-04-30 01:41:25 -07:00
|
|
|
activeIterator := suite.app.GovKeeper.ActiveProposalQueueIterator(suite.ctx, proposal.VotingEndTime)
|
|
|
|
suite.Require().True(activeIterator.Valid())
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
proposalID := types.GetProposalIDFromBytes(activeIterator.Value())
|
2021-04-30 01:41:25 -07:00
|
|
|
suite.Require().Equal(proposalID, proposal.ProposalId)
|
2019-08-08 12:51:18 -07:00
|
|
|
activeIterator.Close()
|
|
|
|
}
|
|
|
|
|
2021-12-13 10:48:44 -08:00
|
|
|
type invalidProposalRoute struct{ v1beta1.TextProposal }
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
func (invalidProposalRoute) ProposalRoute() string { return "nonexistingroute" }
|
|
|
|
|
2021-04-30 01:41:25 -07:00
|
|
|
func (suite *KeeperTestSuite) TestSubmitProposal() {
|
2019-08-08 12:51:18 -07:00
|
|
|
testCases := []struct {
|
2021-12-13 10:48:44 -08:00
|
|
|
content v1beta1.Content
|
2019-12-27 09:57:54 -08:00
|
|
|
expectedErr error
|
2019-08-08 12:51:18 -07:00
|
|
|
}{
|
2021-12-13 10:48:44 -08:00
|
|
|
{&v1beta1.TextProposal{Title: "title", Description: "description"}, nil},
|
2019-08-08 12:51:18 -07:00
|
|
|
// Keeper does not check the validity of title and description, no error
|
2021-12-13 10:48:44 -08:00
|
|
|
{&v1beta1.TextProposal{Title: "", Description: "description"}, nil},
|
|
|
|
{&v1beta1.TextProposal{Title: strings.Repeat("1234567890", 100), Description: "description"}, nil},
|
|
|
|
{&v1beta1.TextProposal{Title: "title", Description: ""}, nil},
|
|
|
|
{&v1beta1.TextProposal{Title: "title", Description: strings.Repeat("1234567890", 1000)}, nil},
|
2019-08-08 12:51:18 -07:00
|
|
|
// error only when invalid route
|
2020-04-14 12:05:14 -07:00
|
|
|
{&invalidProposalRoute{}, types.ErrNoProposalHandlerExists},
|
2019-08-08 12:51:18 -07:00
|
|
|
}
|
|
|
|
|
2019-12-27 09:57:54 -08:00
|
|
|
for i, tc := range testCases {
|
2021-04-30 01:41:25 -07:00
|
|
|
_, err := suite.app.GovKeeper.SubmitProposal(suite.ctx, tc.content)
|
|
|
|
suite.Require().True(errors.Is(tc.expectedErr, err), "tc #%d; got: %v, expected: %v", i, err, tc.expectedErr)
|
2019-08-08 12:51:18 -07:00
|
|
|
}
|
|
|
|
}
|
2019-10-18 09:46:21 -07:00
|
|
|
|
2021-04-30 01:41:25 -07:00
|
|
|
func (suite *KeeperTestSuite) TestGetProposalsFiltered() {
|
2019-10-18 09:46:21 -07:00
|
|
|
proposalID := uint64(1)
|
2021-12-13 10:48:44 -08:00
|
|
|
status := []v1beta1.ProposalStatus{v1beta1.StatusDepositPeriod, v1beta1.StatusVotingPeriod}
|
2019-10-18 09:46:21 -07:00
|
|
|
|
2020-09-25 03:25:37 -07:00
|
|
|
addr1 := sdk.AccAddress("foo_________________")
|
2019-10-18 09:46:21 -07:00
|
|
|
|
|
|
|
for _, s := range status {
|
|
|
|
for i := 0; i < 50; i++ {
|
2021-12-13 10:48:44 -08:00
|
|
|
p, err := v1beta1.NewProposal(TestProposal, proposalID, time.Now(), time.Now())
|
2021-04-30 01:41:25 -07:00
|
|
|
suite.Require().NoError(err)
|
2020-05-19 13:17:29 -07:00
|
|
|
|
2019-10-18 09:46:21 -07:00
|
|
|
p.Status = s
|
|
|
|
|
|
|
|
if i%2 == 0 {
|
2021-12-13 10:48:44 -08:00
|
|
|
d := v1beta1.NewDeposit(proposalID, addr1, nil)
|
|
|
|
v := v1beta1.NewVote(proposalID, addr1, v1beta1.NewNonSplitVoteOption(v1beta1.OptionYes))
|
2021-04-30 01:41:25 -07:00
|
|
|
suite.app.GovKeeper.SetDeposit(suite.ctx, d)
|
|
|
|
suite.app.GovKeeper.SetVote(suite.ctx, v)
|
2019-10-18 09:46:21 -07:00
|
|
|
}
|
|
|
|
|
2021-04-30 01:41:25 -07:00
|
|
|
suite.app.GovKeeper.SetProposal(suite.ctx, p)
|
2019-10-18 09:46:21 -07:00
|
|
|
proposalID++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
2021-12-13 10:48:44 -08:00
|
|
|
params v1beta1.QueryProposalsParams
|
2019-10-18 09:46:21 -07:00
|
|
|
expectedNumResults int
|
|
|
|
}{
|
2021-12-13 10:48:44 -08:00
|
|
|
{v1beta1.NewQueryProposalsParams(1, 50, v1beta1.StatusNil, nil, nil), 50},
|
|
|
|
{v1beta1.NewQueryProposalsParams(1, 50, v1beta1.StatusDepositPeriod, nil, nil), 50},
|
|
|
|
{v1beta1.NewQueryProposalsParams(1, 50, v1beta1.StatusVotingPeriod, nil, nil), 50},
|
|
|
|
{v1beta1.NewQueryProposalsParams(1, 25, v1beta1.StatusNil, nil, nil), 25},
|
|
|
|
{v1beta1.NewQueryProposalsParams(2, 25, v1beta1.StatusNil, nil, nil), 25},
|
|
|
|
{v1beta1.NewQueryProposalsParams(1, 50, v1beta1.StatusRejected, nil, nil), 0},
|
|
|
|
{v1beta1.NewQueryProposalsParams(1, 50, v1beta1.StatusNil, addr1, nil), 50},
|
|
|
|
{v1beta1.NewQueryProposalsParams(1, 50, v1beta1.StatusNil, nil, addr1), 50},
|
|
|
|
{v1beta1.NewQueryProposalsParams(1, 50, v1beta1.StatusNil, addr1, addr1), 50},
|
|
|
|
{v1beta1.NewQueryProposalsParams(1, 50, v1beta1.StatusDepositPeriod, addr1, addr1), 25},
|
|
|
|
{v1beta1.NewQueryProposalsParams(1, 50, v1beta1.StatusDepositPeriod, nil, nil), 50},
|
|
|
|
{v1beta1.NewQueryProposalsParams(1, 50, v1beta1.StatusVotingPeriod, nil, nil), 50},
|
2019-10-18 09:46:21 -07:00
|
|
|
}
|
|
|
|
|
2020-08-11 07:22:30 -07:00
|
|
|
for i, tc := range testCases {
|
2021-04-30 01:41:25 -07:00
|
|
|
suite.Run(fmt.Sprintf("Test Case %d", i), func() {
|
|
|
|
proposals := suite.app.GovKeeper.GetProposalsFiltered(suite.ctx, tc.params)
|
|
|
|
suite.Require().Len(proposals, tc.expectedNumResults)
|
2020-08-11 07:22:30 -07:00
|
|
|
|
|
|
|
for _, p := range proposals {
|
2021-12-13 10:48:44 -08:00
|
|
|
if v1beta1.ValidProposalStatus(tc.params.ProposalStatus) {
|
2021-04-30 01:41:25 -07:00
|
|
|
suite.Require().Equal(tc.params.ProposalStatus, p.Status)
|
2020-08-11 07:22:30 -07:00
|
|
|
}
|
2019-10-18 09:46:21 -07:00
|
|
|
}
|
2020-08-11 07:22:30 -07:00
|
|
|
})
|
2019-10-18 09:46:21 -07:00
|
|
|
}
|
|
|
|
}
|