0g-chain/x/committee/proposal_handler_test.go

242 lines
7.3 KiB
Go
Raw Normal View History

2020-03-21 18:06:58 +00:00
package committee_test
import (
"testing"
"time"
2020-04-30 14:23:41 +00:00
"github.com/stretchr/testify/suite"
2020-03-21 18:06:58 +00:00
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
2020-04-30 14:23:41 +00:00
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
2020-03-21 18:06:58 +00:00
"github.com/kava-labs/kava/app"
"github.com/kava-labs/kava/x/committee"
"github.com/kava-labs/kava/x/committee/keeper"
"github.com/kava-labs/kava/x/committee/testutil"
2020-03-21 18:06:58 +00:00
"github.com/kava-labs/kava/x/committee/types"
)
var testTime time.Time = time.Date(1998, time.January, 1, 0, 0, 0, 0, time.UTC)
func NewCommitteeGenState(cdc codec.Codec, gs *types.GenesisState) app.GenesisState {
return app.GenesisState{types.ModuleName: cdc.MustMarshalJSON(gs)}
2020-03-21 18:06:58 +00:00
}
type ProposalHandlerTestSuite struct {
suite.Suite
keeper keeper.Keeper
2020-03-21 18:06:58 +00:00
app app.TestApp
ctx sdk.Context
addresses []sdk.AccAddress
testGenesis *types.GenesisState
2020-03-21 18:06:58 +00:00
}
func (suite *ProposalHandlerTestSuite) SetupTest() {
_, suite.addresses = app.GeneratePrivKeyAddressPairs(5)
suite.testGenesis = types.NewGenesisState(
2020-03-21 18:06:58 +00:00
2,
[]types.Committee{
types.MustNewMemberCommittee(
1,
"This committee is for testing.",
suite.addresses[:3],
[]types.Permission{&types.GodPermission{}},
testutil.D("0.667"),
time.Hour*24*7,
types.TALLY_OPTION_FIRST_PAST_THE_POST,
),
types.MustNewMemberCommittee(
2,
"member committee",
suite.addresses[2:],
nil,
testutil.D("0.667"),
time.Hour*24*7,
types.TALLY_OPTION_FIRST_PAST_THE_POST,
),
2020-03-21 18:06:58 +00:00
},
types.Proposals{
types.MustNewProposal(
govtypes.NewTextProposal("A Title", "A description of this proposal."), 1, 1, testTime.Add(7*24*time.Hour),
),
2020-03-21 18:06:58 +00:00
},
[]types.Vote{
{ProposalID: 1, Voter: suite.addresses[0], VoteType: types.VOTE_TYPE_YES},
2020-03-21 18:06:58 +00:00
},
)
}
func (suite *ProposalHandlerTestSuite) TestProposalHandler_ChangeCommittee() {
testCases := []struct {
name string
proposal types.CommitteeChangeProposal
2020-03-21 18:06:58 +00:00
expectPass bool
}{
{
name: "add new",
proposal: types.MustNewCommitteeChangeProposal(
2020-03-21 18:06:58 +00:00
"A Title",
"A proposal description.",
types.MustNewMemberCommittee(
34,
"member committee",
suite.addresses[:1],
[]types.Permission{},
testutil.D("1"),
time.Hour*24,
types.TALLY_OPTION_DEADLINE,
),
2020-03-21 18:06:58 +00:00
),
expectPass: true,
},
{
name: "update",
proposal: types.MustNewCommitteeChangeProposal(
2020-03-21 18:06:58 +00:00
"A Title",
"A proposal description.",
types.MustNewMemberCommittee(
suite.testGenesis.GetCommittees()[0].GetID(),
"member committee",
suite.addresses, // add new members
suite.testGenesis.GetCommittees()[0].GetPermissions(),
suite.testGenesis.GetCommittees()[0].GetVoteThreshold(),
suite.testGenesis.GetCommittees()[0].GetProposalDuration(),
types.TALLY_OPTION_FIRST_PAST_THE_POST,
),
2020-03-21 18:06:58 +00:00
),
expectPass: true,
},
{
name: "invalid title",
proposal: types.MustNewCommitteeChangeProposal(
2020-04-30 14:13:31 +00:00
"A Title That Is Much Too Long And Really Quite Unreasonable Given That It Is Trying To Fulfill The Roll Of An Acceptable Governance Proposal Title That Should Succinctly Communicate The Goal And Contents Of The Proposed Proposal To All Parties Involved",
2020-03-21 18:06:58 +00:00
"A proposal description.",
suite.testGenesis.GetCommittees()[0],
2020-03-21 18:06:58 +00:00
),
expectPass: false,
},
{
name: "invalid committee",
proposal: types.MustNewCommitteeChangeProposal(
2020-03-21 18:06:58 +00:00
"A Title",
"A proposal description.",
types.MustNewMemberCommittee(
suite.testGenesis.GetCommittees()[0].GetID(),
"member committee",
append(suite.addresses, suite.addresses[0]), // duplicate address
suite.testGenesis.GetCommittees()[0].GetPermissions(),
suite.testGenesis.GetCommittees()[0].GetVoteThreshold(),
suite.testGenesis.GetCommittees()[0].GetProposalDuration(),
types.TALLY_OPTION_DEADLINE,
),
2020-03-21 18:06:58 +00:00
),
expectPass: false,
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
// Setup
suite.app = app.NewTestApp()
suite.keeper = suite.app.GetCommitteeKeeper()
suite.app = suite.app.InitializeFromGenesisStates(
NewCommitteeGenState(suite.app.AppCodec(), suite.testGenesis),
2020-03-21 18:06:58 +00:00
)
suite.ctx = suite.app.NewContext(true, tmproto.Header{Height: 1, Time: testTime})
2020-03-21 18:06:58 +00:00
handler := committee.NewProposalHandler(suite.keeper)
oldProposals := suite.keeper.GetProposalsByCommittee(suite.ctx, tc.proposal.GetNewCommittee().GetID())
2020-03-21 18:06:58 +00:00
// Run
err := handler(suite.ctx, &tc.proposal)
2020-03-21 18:06:58 +00:00
// Check
if tc.expectPass {
suite.NoError(err)
2020-04-27 14:04:47 +00:00
// check committee is accurate
actualCom, found := suite.keeper.GetCommittee(suite.ctx, tc.proposal.GetNewCommittee().GetID())
2020-03-21 18:06:58 +00:00
suite.True(found)
testutil.AssertProtoMessageJSON(suite.T(), suite.app.AppCodec(), tc.proposal.GetNewCommittee(), actualCom)
2020-03-21 18:06:58 +00:00
// check proposals and votes for this committee have been removed
suite.Empty(suite.keeper.GetProposalsByCommittee(suite.ctx, tc.proposal.GetNewCommittee().GetID()))
2020-04-27 14:04:47 +00:00
for _, p := range oldProposals {
suite.Empty(suite.keeper.GetVotesByProposal(suite.ctx, p.ID))
2020-03-21 18:06:58 +00:00
}
} else {
suite.Error(err)
testutil.AssertProtoMessageJSON(suite.T(), suite.app.AppCodec(), suite.testGenesis, committee.ExportGenesis(suite.ctx, suite.keeper))
2020-03-21 18:06:58 +00:00
}
})
}
}
func (suite *ProposalHandlerTestSuite) TestProposalHandler_DeleteCommittee() {
testCases := []struct {
name string
proposal types.CommitteeDeleteProposal
2020-03-21 18:06:58 +00:00
expectPass bool
}{
{
name: "normal",
proposal: types.NewCommitteeDeleteProposal(
2020-03-21 18:06:58 +00:00
"A Title",
"A proposal description.",
suite.testGenesis.GetCommittees()[0].GetID(),
2020-03-21 18:06:58 +00:00
),
expectPass: true,
},
{
name: "invalid title",
proposal: types.NewCommitteeDeleteProposal(
2020-04-30 14:13:31 +00:00
"A Title That Is Much Too Long And Really Quite Unreasonable Given That It Is Trying To Fulfill The Roll Of An Acceptable Governance Proposal Title That Should Succinctly Communicate The Goal And Contents Of The Proposed Proposal To All Parties Involved",
2020-03-21 18:06:58 +00:00
"A proposal description.",
suite.testGenesis.GetCommittees()[1].GetID(),
2020-03-21 18:06:58 +00:00
),
expectPass: false,
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
// Setup
suite.app = app.NewTestApp()
suite.keeper = suite.app.GetCommitteeKeeper()
suite.app = suite.app.InitializeFromGenesisStates(
NewCommitteeGenState(suite.app.AppCodec(), suite.testGenesis),
2020-03-21 18:06:58 +00:00
)
suite.ctx = suite.app.NewContext(true, tmproto.Header{Height: 1, Time: testTime})
2020-03-21 18:06:58 +00:00
handler := committee.NewProposalHandler(suite.keeper)
2020-04-27 14:04:47 +00:00
oldProposals := suite.keeper.GetProposalsByCommittee(suite.ctx, tc.proposal.CommitteeID)
2020-03-21 18:06:58 +00:00
// Run
err := handler(suite.ctx, &tc.proposal)
2020-03-21 18:06:58 +00:00
// Check
if tc.expectPass {
suite.NoError(err)
2020-04-27 14:04:47 +00:00
// check committee has been removed
2020-03-21 18:06:58 +00:00
_, found := suite.keeper.GetCommittee(suite.ctx, tc.proposal.CommitteeID)
suite.False(found)
// check proposals and votes for this committee have been removed
2020-04-27 14:04:47 +00:00
suite.Empty(suite.keeper.GetProposalsByCommittee(suite.ctx, tc.proposal.CommitteeID))
for _, p := range oldProposals {
suite.Empty(suite.keeper.GetVotesByProposal(suite.ctx, p.ID))
2020-03-21 18:06:58 +00:00
}
} else {
suite.Error(err)
testutil.AssertProtoMessageJSON(suite.T(), suite.app.AppCodec(), suite.testGenesis, committee.ExportGenesis(suite.ctx, suite.keeper))
2020-03-21 18:06:58 +00:00
}
})
}
}
func TestProposalHandlerTestSuite(t *testing.T) {
suite.Run(t, new(ProposalHandlerTestSuite))
}