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

248 lines
7.2 KiB
Go
Raw Normal View History

2020-03-13 15:11:31 +00:00
package keeper_test
import (
"strings"
"testing"
2020-03-27 18:34:03 +00:00
"time"
2020-03-13 15:11:31 +00:00
"github.com/stretchr/testify/suite"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/kava-labs/kava/app"
2020-03-27 18:34:03 +00:00
"github.com/kava-labs/kava/x/committee"
2020-03-13 15:11:31 +00:00
"github.com/kava-labs/kava/x/committee/keeper"
"github.com/kava-labs/kava/x/committee/types"
)
const (
custom = "custom"
)
2020-03-27 18:34:03 +00:00
var testTime time.Time = time.Date(1998, time.January, 1, 0, 0, 0, 0, time.UTC)
func NewCommitteeGenesisState(cdc *codec.Codec, gs committee.GenesisState) app.GenesisState {
return app.GenesisState{committee.ModuleName: cdc.MustMarshalJSON(gs)}
}
2020-03-13 15:11:31 +00:00
type QuerierTestSuite struct {
suite.Suite
keeper keeper.Keeper
app app.TestApp
ctx sdk.Context
cdc *codec.Codec
querier sdk.Querier
2020-03-27 18:34:03 +00:00
addresses []sdk.AccAddress
testGenesis types.GenesisState
votes map[uint64]([]types.Vote)
2020-03-13 15:11:31 +00:00
}
func (suite *QuerierTestSuite) SetupTest() {
suite.app = app.NewTestApp()
suite.keeper = suite.app.GetCommitteeKeeper()
suite.ctx = suite.app.NewContext(true, abci.Header{})
suite.cdc = suite.app.Codec()
suite.querier = keeper.NewQuerier(suite.keeper)
_, suite.addresses = app.GeneratePrivKeyAddressPairs(5)
2020-03-27 18:34:03 +00:00
suite.testGenesis = types.NewGenesisState(
3,
[]types.Committee{
{
ID: 1,
Description: "This committee is for testing.",
Members: suite.addresses[:3],
Permissions: []types.Permission{types.GodPermission{}},
VoteThreshold: d("0.667"),
MaxProposalDuration: time.Hour * 24 * 7,
},
{
ID: 2,
Members: suite.addresses[2:],
Permissions: nil,
VoteThreshold: d("0.667"),
MaxProposalDuration: time.Hour * 24 * 7,
},
},
[]types.Proposal{
{ID: 1, CommitteeID: 1, PubProposal: gov.NewTextProposal("A Title", "A description of this proposal."), Deadline: testTime.Add(7 * 24 * time.Hour)},
{ID: 2, CommitteeID: 1, PubProposal: gov.NewTextProposal("Another Title", "A description of this other proposal."), Deadline: testTime.Add(21 * 24 * time.Hour)},
},
[]types.Vote{
{ProposalID: 1, Voter: suite.addresses[0]},
{ProposalID: 1, Voter: suite.addresses[1]},
{ProposalID: 2, Voter: suite.addresses[2]},
},
)
suite.app.InitializeFromGenesisStates(
NewCommitteeGenesisState(suite.cdc, suite.testGenesis),
)
2020-04-24 22:15:51 +00:00
suite.votes = getProposalVoteMap(suite.keeper, suite.ctx)
2020-03-13 15:11:31 +00:00
}
func (suite *QuerierTestSuite) TestQueryCommittees() {
ctx := suite.ctx.WithIsCheckTx(false)
// Set up request query
query := abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryCommittees}, "/"),
}
// Execute query and check the []byte result
bz, err := suite.querier(ctx, []string{types.QueryCommittees}, query)
suite.NoError(err)
suite.NotNil(bz)
// Unmarshal the bytes
var committees []types.Committee
suite.NoError(suite.cdc.UnmarshalJSON(bz, &committees))
// Check
2020-03-27 18:34:03 +00:00
suite.Equal(suite.testGenesis.Committees, committees)
2020-03-13 15:11:31 +00:00
}
func (suite *QuerierTestSuite) TestQueryCommittee() {
ctx := suite.ctx.WithIsCheckTx(false) // ?
// Set up request query
query := abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryCommittee}, "/"),
2020-03-27 18:34:03 +00:00
Data: suite.cdc.MustMarshalJSON(types.NewQueryCommitteeParams(suite.testGenesis.Committees[0].ID)),
2020-03-13 15:11:31 +00:00
}
// Execute query and check the []byte result
bz, err := suite.querier(ctx, []string{types.QueryCommittee}, query)
suite.NoError(err)
suite.NotNil(bz)
// Unmarshal the bytes
var committee types.Committee
suite.NoError(suite.cdc.UnmarshalJSON(bz, &committee))
// Check
2020-03-27 18:34:03 +00:00
suite.Equal(suite.testGenesis.Committees[0], committee)
2020-03-13 15:11:31 +00:00
}
func (suite *QuerierTestSuite) TestQueryProposals() {
ctx := suite.ctx.WithIsCheckTx(false)
// Set up request query
2020-03-27 18:34:03 +00:00
comID := suite.testGenesis.Proposals[0].CommitteeID
2020-03-13 15:11:31 +00:00
query := abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryProposals}, "/"),
Data: suite.cdc.MustMarshalJSON(types.NewQueryCommitteeParams(comID)),
}
// Execute query and check the []byte result
bz, err := suite.querier(ctx, []string{types.QueryProposals}, query)
suite.NoError(err)
suite.NotNil(bz)
// Unmarshal the bytes
var proposals []types.Proposal
suite.NoError(suite.cdc.UnmarshalJSON(bz, &proposals))
// Check
expectedProposals := []types.Proposal{}
2020-03-27 18:34:03 +00:00
for _, p := range suite.testGenesis.Proposals {
2020-03-13 15:11:31 +00:00
if p.CommitteeID == comID {
expectedProposals = append(expectedProposals, p)
}
}
suite.Equal(expectedProposals, proposals)
}
func (suite *QuerierTestSuite) TestQueryProposal() {
ctx := suite.ctx.WithIsCheckTx(false) // ?
// Set up request query
query := abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryProposal}, "/"),
2020-03-27 18:34:03 +00:00
Data: suite.cdc.MustMarshalJSON(types.NewQueryProposalParams(suite.testGenesis.Proposals[0].ID)),
2020-03-13 15:11:31 +00:00
}
// Execute query and check the []byte result
bz, err := suite.querier(ctx, []string{types.QueryProposal}, query)
suite.NoError(err)
suite.NotNil(bz)
// Unmarshal the bytes
var proposal types.Proposal
suite.NoError(suite.cdc.UnmarshalJSON(bz, &proposal))
// Check
2020-03-27 18:34:03 +00:00
suite.Equal(suite.testGenesis.Proposals[0], proposal)
2020-03-13 15:11:31 +00:00
}
func (suite *QuerierTestSuite) TestQueryVotes() {
ctx := suite.ctx.WithIsCheckTx(false)
// Set up request query
2020-03-27 18:34:03 +00:00
propID := suite.testGenesis.Proposals[0].ID
2020-03-13 15:11:31 +00:00
query := abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryVotes}, "/"),
Data: suite.cdc.MustMarshalJSON(types.NewQueryProposalParams(propID)),
}
// Execute query and check the []byte result
bz, err := suite.querier(ctx, []string{types.QueryVotes}, query)
suite.NoError(err)
suite.NotNil(bz)
// Unmarshal the bytes
var votes []types.Vote
suite.NoError(suite.cdc.UnmarshalJSON(bz, &votes))
// Check
suite.Equal(suite.votes[propID], votes)
}
func (suite *QuerierTestSuite) TestQueryVote() {
ctx := suite.ctx.WithIsCheckTx(false) // ?
// Set up request query
2020-03-27 18:34:03 +00:00
propID := suite.testGenesis.Proposals[0].ID
2020-03-13 15:11:31 +00:00
query := abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryVote}, "/"),
Data: suite.cdc.MustMarshalJSON(types.NewQueryVoteParams(propID, suite.votes[propID][0].Voter)),
}
// Execute query and check the []byte result
bz, err := suite.querier(ctx, []string{types.QueryVote}, query)
suite.NoError(err)
suite.NotNil(bz)
// Unmarshal the bytes
var vote types.Vote
suite.NoError(suite.cdc.UnmarshalJSON(bz, &vote))
// Check
suite.Equal(suite.votes[propID][0], vote)
}
func (suite *QuerierTestSuite) TestQueryTally() {
ctx := suite.ctx.WithIsCheckTx(false) // ?
// Set up request query
2020-03-27 18:34:03 +00:00
propID := suite.testGenesis.Proposals[0].ID
2020-03-13 15:11:31 +00:00
query := abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryTally}, "/"),
Data: suite.cdc.MustMarshalJSON(types.NewQueryProposalParams(propID)),
}
// Execute query and check the []byte result
bz, err := suite.querier(ctx, []string{types.QueryTally}, query)
suite.NoError(err)
suite.NotNil(bz)
// Unmarshal the bytes
2020-03-26 20:17:49 +00:00
var tally int64
2020-03-13 15:11:31 +00:00
suite.NoError(suite.cdc.UnmarshalJSON(bz, &tally))
// Check
2020-03-26 20:17:49 +00:00
suite.Equal(int64(len(suite.votes[propID])), tally)
2020-03-13 15:11:31 +00:00
}
func TestQuerierTestSuite(t *testing.T) {
suite.Run(t, new(QuerierTestSuite))
}