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"
2022-01-21 03:14:00 -08:00
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta2"
2019-08-08 12:51:18 -07:00
)
func TestVotes ( t * testing . T ) {
2021-08-16 17:52:06 -07:00
app := simapp . Setup ( t , 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
2022-03-01 00:35:37 -08:00
proposal , err := app . GovKeeper . SubmitProposal ( ctx , tp , "" )
2019-08-08 12:51:18 -07:00
require . NoError ( t , err )
2022-02-14 05:48:17 -08:00
proposalID := proposal . Id
2022-03-03 06:38:26 -08:00
metadata := "metadata"
2019-08-08 12:51:18 -07:00
2022-01-21 03:14:00 -08:00
var invalidOption v1beta2 . VoteOption = 0x10
2019-08-08 12:51:18 -07:00
2022-03-03 06:38:26 -08:00
require . Error ( t , app . GovKeeper . AddVote ( ctx , proposalID , addrs [ 0 ] , v1beta2 . NewNonSplitVoteOption ( v1beta2 . OptionYes ) , metadata ) , "proposal not on voting period" )
require . Error ( t , app . GovKeeper . AddVote ( ctx , 10 , addrs [ 0 ] , v1beta2 . NewNonSplitVoteOption ( v1beta2 . OptionYes ) , "" ) , "invalid proposal ID" )
2019-08-08 12:51:18 -07:00
2022-01-21 03:14:00 -08:00
proposal . Status = v1beta2 . StatusVotingPeriod
2020-03-02 10:50:39 -08:00
app . GovKeeper . SetProposal ( ctx , proposal )
2019-08-08 12:51:18 -07:00
2022-03-03 06:38:26 -08:00
require . Error ( t , app . GovKeeper . AddVote ( ctx , proposalID , addrs [ 0 ] , v1beta2 . NewNonSplitVoteOption ( invalidOption ) , "" ) , "invalid option" )
2019-08-08 12:51:18 -07:00
// Test first vote
2022-03-03 06:38:26 -08:00
require . NoError ( t , app . GovKeeper . AddVote ( ctx , proposalID , addrs [ 0 ] , v1beta2 . NewNonSplitVoteOption ( v1beta2 . OptionAbstain ) , metadata ) )
2020-03-02 10:50:39 -08:00
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 )
2020-10-30 06:14:39 -07:00
require . True ( t , len ( vote . Options ) == 1 )
2022-01-21 03:14:00 -08:00
require . Equal ( t , v1beta2 . OptionAbstain , vote . Options [ 0 ] . Option )
2019-08-08 12:51:18 -07:00
// Test change of vote
2022-03-03 06:38:26 -08:00
require . NoError ( t , app . GovKeeper . AddVote ( ctx , proposalID , addrs [ 0 ] , v1beta2 . NewNonSplitVoteOption ( v1beta2 . OptionYes ) , "" ) )
2020-03-02 10:50:39 -08:00
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 )
2020-10-30 06:14:39 -07:00
require . True ( t , len ( vote . Options ) == 1 )
2022-01-21 03:14:00 -08:00
require . Equal ( t , v1beta2 . OptionYes , vote . Options [ 0 ] . Option )
2019-08-08 12:51:18 -07:00
// Test second vote
2022-01-21 03:14:00 -08:00
require . NoError ( t , app . GovKeeper . AddVote ( ctx , proposalID , addrs [ 1 ] , v1beta2 . WeightedVoteOptions {
v1beta2 . NewWeightedVoteOption ( v1beta2 . OptionYes , sdk . NewDecWithPrec ( 60 , 2 ) ) ,
v1beta2 . NewWeightedVoteOption ( v1beta2 . OptionNo , sdk . NewDecWithPrec ( 30 , 2 ) ) ,
v1beta2 . NewWeightedVoteOption ( v1beta2 . OptionAbstain , sdk . NewDecWithPrec ( 5 , 2 ) ) ,
v1beta2 . NewWeightedVoteOption ( v1beta2 . OptionNoWithVeto , sdk . NewDecWithPrec ( 5 , 2 ) ) ,
2022-03-03 06:38:26 -08:00
} , "" ) )
2020-03-02 10:50:39 -08:00
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 )
2020-10-30 06:14:39 -07:00
require . True ( t , len ( vote . Options ) == 4 )
2022-01-21 03:14:00 -08:00
require . Equal ( t , v1beta2 . OptionYes , vote . Options [ 0 ] . Option )
require . Equal ( t , v1beta2 . OptionNo , vote . Options [ 1 ] . Option )
require . Equal ( t , v1beta2 . OptionAbstain , vote . Options [ 2 ] . Option )
require . Equal ( t , v1beta2 . OptionNoWithVeto , vote . Options [ 3 ] . Option )
require . Equal ( t , vote . Options [ 0 ] . Weight , sdk . NewDecWithPrec ( 60 , 2 ) . String ( ) )
require . Equal ( t , vote . Options [ 1 ] . Weight , sdk . NewDecWithPrec ( 30 , 2 ) . String ( ) )
require . Equal ( t , vote . Options [ 2 ] . Weight , sdk . NewDecWithPrec ( 5 , 2 ) . String ( ) )
require . Equal ( t , vote . Options [ 3 ] . Weight , sdk . NewDecWithPrec ( 5 , 2 ) . String ( ) )
2019-08-08 12:51:18 -07:00
// 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 )
2020-10-30 06:14:39 -07:00
require . True ( t , len ( votes [ 0 ] . Options ) == 1 )
2022-01-21 03:14:00 -08:00
require . Equal ( t , v1beta2 . OptionYes , votes [ 0 ] . Options [ 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 )
2020-10-30 06:14:39 -07:00
require . True ( t , len ( votes [ 1 ] . Options ) == 4 )
2022-01-21 03:14:00 -08:00
require . Equal ( t , votes [ 1 ] . Options [ 0 ] . Weight , sdk . NewDecWithPrec ( 60 , 2 ) . String ( ) )
require . Equal ( t , votes [ 1 ] . Options [ 1 ] . Weight , sdk . NewDecWithPrec ( 30 , 2 ) . String ( ) )
require . Equal ( t , votes [ 1 ] . Options [ 2 ] . Weight , sdk . NewDecWithPrec ( 5 , 2 ) . String ( ) )
require . Equal ( t , votes [ 1 ] . Options [ 3 ] . Weight , sdk . NewDecWithPrec ( 5 , 2 ) . String ( ) )
2019-08-08 12:51:18 -07:00
}