0g-chain/x/committee/keeper/keeper_test.go

113 lines
2.8 KiB
Go
Raw Normal View History

package keeper_test
import (
"testing"
2020-03-27 18:34:03 +00:00
"time"
2020-03-10 23:16:22 +00:00
"github.com/stretchr/testify/suite"
sdk "github.com/cosmos/cosmos-sdk/types"
2020-03-27 18:34:03 +00:00
"github.com/cosmos/cosmos-sdk/x/gov"
2020-03-10 23:16:22 +00:00
abci "github.com/tendermint/tendermint/abci/types"
"github.com/kava-labs/kava/app"
"github.com/kava-labs/kava/x/committee/keeper"
2020-03-11 19:27:36 +00:00
"github.com/kava-labs/kava/x/committee/types"
)
type KeeperTestSuite struct {
suite.Suite
keeper keeper.Keeper
app app.TestApp
ctx sdk.Context
2020-03-10 23:16:22 +00:00
addresses []sdk.AccAddress
}
func (suite *KeeperTestSuite) SetupTest() {
2020-03-10 23:16:22 +00:00
suite.app = app.NewTestApp()
suite.keeper = suite.app.GetCommitteeKeeper()
2020-03-10 23:16:22 +00:00
suite.ctx = suite.app.NewContext(true, abci.Header{})
2020-03-11 19:27:36 +00:00
_, suite.addresses = app.GeneratePrivKeyAddressPairs(5)
}
2020-03-10 23:28:25 +00:00
func (suite *KeeperTestSuite) TestGetSetDeleteCommittee() {
2020-03-11 00:58:42 +00:00
// setup test
2020-03-10 23:16:22 +00:00
com := types.Committee{
2020-04-24 23:22:56 +00:00
ID: 12,
Description: "This committee is for testing.",
Members: suite.addresses,
Permissions: []types.Permission{types.GodPermission{}},
VoteThreshold: d("0.667"),
ProposalDuration: time.Hour * 24 * 7,
2020-03-10 23:16:22 +00:00
}
// write and read from store
suite.keeper.SetCommittee(suite.ctx, com)
readCommittee, found := suite.keeper.GetCommittee(suite.ctx, com.ID)
// check before and after match
suite.True(found)
suite.Equal(com, readCommittee)
2020-03-10 23:28:25 +00:00
// delete from store
suite.keeper.DeleteCommittee(suite.ctx, com.ID)
// check does not exist
_, found = suite.keeper.GetCommittee(suite.ctx, com.ID)
suite.False(found)
2020-03-10 23:16:22 +00:00
}
func (suite *KeeperTestSuite) TestGetSetDeleteProposal() {
2020-03-10 23:16:22 +00:00
// test setup
prop := types.Proposal{
2020-03-27 18:34:03 +00:00
ID: 12,
CommitteeID: 0,
PubProposal: gov.NewTextProposal("A Title", "A description of this proposal."),
Deadline: time.Date(1998, time.January, 1, 0, 0, 0, 0, time.UTC),
2020-03-10 23:16:22 +00:00
}
// write and read from store
suite.keeper.SetProposal(suite.ctx, prop)
readProposal, found := suite.keeper.GetProposal(suite.ctx, prop.ID)
// check before and after match
suite.True(found)
suite.Equal(prop, readProposal)
2020-03-10 23:28:25 +00:00
// delete from store
suite.keeper.DeleteProposal(suite.ctx, prop.ID)
// check does not exist
_, found = suite.keeper.GetProposal(suite.ctx, prop.ID)
suite.False(found)
}
func (suite *KeeperTestSuite) TestGetSetDeleteVote() {
2020-03-10 23:16:22 +00:00
// test setup
vote := types.Vote{
ProposalID: 12,
Voter: suite.addresses[0],
}
// write and read from store
suite.keeper.SetVote(suite.ctx, vote)
readVote, found := suite.keeper.GetVote(suite.ctx, vote.ProposalID, vote.Voter)
// check before and after match
suite.True(found)
suite.Equal(vote, readVote)
2020-03-10 23:28:25 +00:00
// delete from store
suite.keeper.DeleteVote(suite.ctx, vote.ProposalID, vote.Voter)
// check does not exist
_, found = suite.keeper.GetVote(suite.ctx, vote.ProposalID, vote.Voter)
suite.False(found)
2020-03-10 23:16:22 +00:00
}
func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
2020-03-10 23:16:22 +00:00
}