2020-03-02 10:50:39 -08:00
|
|
|
package keeper_test
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-08-14 10:58:53 -07:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2019-08-08 12:51:18 -07:00
|
|
|
|
2020-03-02 12:07:51 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-08-08 12:51:18 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestVotes(t *testing.T) {
|
2020-03-02 10:50:39 -08:00
|
|
|
app := simapp.Setup(false)
|
2020-08-14 10:58:53 -07:00
|
|
|
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
2020-03-02 10:50:39 -08:00
|
|
|
|
|
|
|
addrs := simapp.AddTestAddrsIncremental(app, ctx, 5, sdk.NewInt(30000000))
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
tp := TestProposal
|
2020-03-02 10:50:39 -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
|
2019-08-08 12:51:18 -07:00
|
|
|
|
2019-09-16 10:11:17 -07:00
|
|
|
var invalidOption types.VoteOption = 0x10
|
2019-08-08 12:51:18 -07:00
|
|
|
|
2020-03-02 10:50:39 -08:00
|
|
|
require.Error(t, app.GovKeeper.AddVote(ctx, proposalID, addrs[0], types.OptionYes), "proposal not on voting period")
|
|
|
|
require.Error(t, app.GovKeeper.AddVote(ctx, 10, addrs[0], types.OptionYes), "invalid proposal ID")
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
proposal.Status = types.StatusVotingPeriod
|
2020-03-02 10:50:39 -08:00
|
|
|
app.GovKeeper.SetProposal(ctx, proposal)
|
2019-08-08 12:51:18 -07:00
|
|
|
|
2020-03-02 10:50:39 -08:00
|
|
|
require.Error(t, app.GovKeeper.AddVote(ctx, proposalID, addrs[0], invalidOption), "invalid option")
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
// Test first vote
|
2020-03-02 10:50:39 -08:00
|
|
|
require.NoError(t, app.GovKeeper.AddVote(ctx, proposalID, addrs[0], types.OptionAbstain))
|
|
|
|
vote, found := app.GovKeeper.GetVote(ctx, proposalID, addrs[0])
|
2019-08-08 12:51:18 -07:00
|
|
|
require.True(t, found)
|
2020-09-25 03:25:37 -07:00
|
|
|
require.Equal(t, addrs[0].String(), vote.Voter)
|
2020-08-14 01:55:54 -07:00
|
|
|
require.Equal(t, proposalID, vote.ProposalId)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.Equal(t, types.OptionAbstain, vote.Option)
|
|
|
|
|
|
|
|
// Test change of vote
|
2020-03-02 10:50:39 -08:00
|
|
|
require.NoError(t, app.GovKeeper.AddVote(ctx, proposalID, addrs[0], types.OptionYes))
|
|
|
|
vote, found = app.GovKeeper.GetVote(ctx, proposalID, addrs[0])
|
2019-08-08 12:51:18 -07:00
|
|
|
require.True(t, found)
|
2020-09-25 03:25:37 -07:00
|
|
|
require.Equal(t, addrs[0].String(), vote.Voter)
|
2020-08-14 01:55:54 -07:00
|
|
|
require.Equal(t, proposalID, vote.ProposalId)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.Equal(t, types.OptionYes, vote.Option)
|
|
|
|
|
|
|
|
// Test second vote
|
2020-03-02 10:50:39 -08:00
|
|
|
require.NoError(t, app.GovKeeper.AddVote(ctx, proposalID, addrs[1], types.OptionNoWithVeto))
|
|
|
|
vote, found = app.GovKeeper.GetVote(ctx, proposalID, addrs[1])
|
2019-08-08 12:51:18 -07:00
|
|
|
require.True(t, found)
|
2020-09-25 03:25:37 -07:00
|
|
|
require.Equal(t, addrs[1].String(), vote.Voter)
|
2020-08-14 01:55:54 -07:00
|
|
|
require.Equal(t, proposalID, vote.ProposalId)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.Equal(t, types.OptionNoWithVeto, vote.Option)
|
|
|
|
|
|
|
|
// Test vote iterator
|
|
|
|
// NOTE order of deposits is determined by the addresses
|
2020-03-02 10:50:39 -08:00
|
|
|
votes := app.GovKeeper.GetAllVotes(ctx)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.Len(t, votes, 2)
|
2020-03-02 10:50:39 -08:00
|
|
|
require.Equal(t, votes, app.GovKeeper.GetVotes(ctx, proposalID))
|
2020-09-25 03:25:37 -07:00
|
|
|
require.Equal(t, addrs[0].String(), votes[0].Voter)
|
2020-08-14 01:55:54 -07:00
|
|
|
require.Equal(t, proposalID, votes[0].ProposalId)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.Equal(t, types.OptionYes, votes[0].Option)
|
2020-09-25 03:25:37 -07:00
|
|
|
require.Equal(t, addrs[1].String(), votes[1].Voter)
|
2020-08-14 01:55:54 -07:00
|
|
|
require.Equal(t, proposalID, votes[1].ProposalId)
|
2019-08-08 12:51:18 -07:00
|
|
|
require.Equal(t, types.OptionNoWithVeto, votes[1].Option)
|
|
|
|
}
|