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"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-01-16 13:46:51 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-08-14 10:58:53 -07:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2020-01-16 13:46:51 -08:00
|
|
|
|
2020-03-02 07:04:49 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
2019-08-08 12:51:18 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetSetProposal(t *testing.T) {
|
2020-03-02 07:04:49 -08:00
|
|
|
app := simapp.Setup(false)
|
2020-08-14 10:58:53 -07:00
|
|
|
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
tp := TestProposal
|
2020-03-02 07:04:49 -08:00
|
|
|
proposal, err := app.GovKeeper.SubmitProposal(ctx, tp)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.NoError(t, err)
|
2020-08-14 01:55:54 -07:00
|
|
|
proposalID := proposal.ProposalId
|
2020-03-02 07:04:49 -08:00
|
|
|
app.GovKeeper.SetProposal(ctx, proposal)
|
2019-08-08 12:51:18 -07:00
|
|
|
|
2020-03-02 07:04:49 -08:00
|
|
|
gotProposal, ok := app.GovKeeper.GetProposal(ctx, proposalID)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.True(t, ok)
|
2020-03-02 14:21:00 -08:00
|
|
|
require.True(t, proposal.Equal(gotProposal))
|
2019-08-08 12:51:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestActivateVotingPeriod(t *testing.T) {
|
2020-03-02 07:07:23 -08:00
|
|
|
app := simapp.Setup(false)
|
2020-08-14 10:58:53 -07:00
|
|
|
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
tp := TestProposal
|
2020-03-02 07:07:23 -08:00
|
|
|
proposal, err := app.GovKeeper.SubmitProposal(ctx, tp)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.True(t, proposal.VotingStartTime.Equal(time.Time{}))
|
|
|
|
|
2020-03-02 07:07:23 -08:00
|
|
|
app.GovKeeper.ActivateVotingPeriod(ctx, proposal)
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
require.True(t, proposal.VotingStartTime.Equal(ctx.BlockHeader().Time))
|
|
|
|
|
2020-08-14 01:55:54 -07:00
|
|
|
proposal, ok := app.GovKeeper.GetProposal(ctx, proposal.ProposalId)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.True(t, ok)
|
|
|
|
|
2020-03-02 07:07:23 -08:00
|
|
|
activeIterator := app.GovKeeper.ActiveProposalQueueIterator(ctx, proposal.VotingEndTime)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.True(t, activeIterator.Valid())
|
|
|
|
|
|
|
|
proposalID := types.GetProposalIDFromBytes(activeIterator.Value())
|
2020-08-14 01:55:54 -07:00
|
|
|
require.Equal(t, proposalID, proposal.ProposalId)
|
2019-08-08 12:51:18 -07:00
|
|
|
activeIterator.Close()
|
|
|
|
}
|
|
|
|
|
2020-03-02 13:46:28 -08:00
|
|
|
type invalidProposalRoute struct{ types.TextProposal }
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
func (invalidProposalRoute) ProposalRoute() string { return "nonexistingroute" }
|
|
|
|
|
|
|
|
func TestSubmitProposal(t *testing.T) {
|
2020-03-02 07:10:19 -08:00
|
|
|
app := simapp.Setup(false)
|
2020-08-14 10:58:53 -07:00
|
|
|
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
content types.Content
|
2019-12-27 09:57:54 -08:00
|
|
|
expectedErr error
|
2019-08-08 12:51:18 -07:00
|
|
|
}{
|
2020-04-14 12:05:14 -07:00
|
|
|
{&types.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
|
2020-04-14 12:05:14 -07:00
|
|
|
{&types.TextProposal{Title: "", Description: "description"}, nil},
|
|
|
|
{&types.TextProposal{Title: strings.Repeat("1234567890", 100), Description: "description"}, nil},
|
|
|
|
{&types.TextProposal{Title: "title", Description: ""}, nil},
|
|
|
|
{&types.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 {
|
2020-03-02 07:10:19 -08:00
|
|
|
_, err := app.GovKeeper.SubmitProposal(ctx, tc.content)
|
2019-12-27 09:57:54 -08:00
|
|
|
require.True(t, 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
|
|
|
|
|
|
|
func TestGetProposalsFiltered(t *testing.T) {
|
|
|
|
proposalID := uint64(1)
|
2020-03-02 07:10:19 -08:00
|
|
|
app := simapp.Setup(false)
|
2020-08-14 10:58:53 -07:00
|
|
|
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
2020-03-02 07:10:19 -08:00
|
|
|
|
2019-10-18 09:46:21 -07:00
|
|
|
status := []types.ProposalStatus{types.StatusDepositPeriod, types.StatusVotingPeriod}
|
|
|
|
|
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++ {
|
2020-05-19 13:17:29 -07:00
|
|
|
p, err := types.NewProposal(TestProposal, proposalID, time.Now(), time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-10-18 09:46:21 -07:00
|
|
|
p.Status = s
|
|
|
|
|
|
|
|
if i%2 == 0 {
|
|
|
|
d := types.NewDeposit(proposalID, addr1, nil)
|
|
|
|
v := types.NewVote(proposalID, addr1, types.OptionYes)
|
2020-03-02 07:10:19 -08:00
|
|
|
app.GovKeeper.SetDeposit(ctx, d)
|
|
|
|
app.GovKeeper.SetVote(ctx, v)
|
2019-10-18 09:46:21 -07:00
|
|
|
}
|
|
|
|
|
2020-03-02 07:10:19 -08:00
|
|
|
app.GovKeeper.SetProposal(ctx, p)
|
2019-10-18 09:46:21 -07:00
|
|
|
proposalID++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
params types.QueryProposalsParams
|
|
|
|
expectedNumResults int
|
|
|
|
}{
|
|
|
|
{types.NewQueryProposalsParams(1, 50, types.StatusNil, nil, nil), 50},
|
|
|
|
{types.NewQueryProposalsParams(1, 50, types.StatusDepositPeriod, nil, nil), 50},
|
|
|
|
{types.NewQueryProposalsParams(1, 50, types.StatusVotingPeriod, nil, nil), 50},
|
|
|
|
{types.NewQueryProposalsParams(1, 25, types.StatusNil, nil, nil), 25},
|
|
|
|
{types.NewQueryProposalsParams(2, 25, types.StatusNil, nil, nil), 25},
|
|
|
|
{types.NewQueryProposalsParams(1, 50, types.StatusRejected, nil, nil), 0},
|
|
|
|
{types.NewQueryProposalsParams(1, 50, types.StatusNil, addr1, nil), 50},
|
|
|
|
{types.NewQueryProposalsParams(1, 50, types.StatusNil, nil, addr1), 50},
|
|
|
|
{types.NewQueryProposalsParams(1, 50, types.StatusNil, addr1, addr1), 50},
|
|
|
|
{types.NewQueryProposalsParams(1, 50, types.StatusDepositPeriod, addr1, addr1), 25},
|
|
|
|
{types.NewQueryProposalsParams(1, 50, types.StatusDepositPeriod, nil, nil), 50},
|
|
|
|
{types.NewQueryProposalsParams(1, 50, types.StatusVotingPeriod, nil, nil), 50},
|
|
|
|
}
|
|
|
|
|
2020-08-11 07:22:30 -07:00
|
|
|
for i, tc := range testCases {
|
|
|
|
t.Run(fmt.Sprintf("Test Case %d", i), func(t *testing.T) {
|
|
|
|
proposals := app.GovKeeper.GetProposalsFiltered(ctx, tc.params)
|
|
|
|
require.Len(t, proposals, tc.expectedNumResults)
|
|
|
|
|
|
|
|
for _, p := range proposals {
|
|
|
|
if types.ValidProposalStatus(tc.params.ProposalStatus) {
|
|
|
|
require.Equal(t, tc.params.ProposalStatus, p.Status)
|
|
|
|
}
|
2019-10-18 09:46:21 -07:00
|
|
|
}
|
2020-08-11 07:22:30 -07:00
|
|
|
})
|
2019-10-18 09:46:21 -07:00
|
|
|
}
|
|
|
|
}
|