github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/gov/keeper/proposal_test.go (about)

     1  package keeper
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    12  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    13  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/gov/types"
    14  )
    15  
    16  func TestGetSetProposal(t *testing.T) {
    17  	ctx, _, keeper, _, _ := createTestInput(t, false, 100)
    18  
    19  	tp := TestProposal
    20  	proposal, err := keeper.SubmitProposal(ctx, tp)
    21  	require.NoError(t, err)
    22  	proposalID := proposal.ProposalID
    23  	keeper.SetProposal(ctx, proposal)
    24  
    25  	gotProposal, ok := keeper.GetProposal(ctx, proposalID)
    26  	require.True(t, ok)
    27  	require.True(t, ProposalEqual(proposal, gotProposal))
    28  }
    29  
    30  func TestActivateVotingPeriod(t *testing.T) {
    31  	ctx, _, keeper, _, _ := createTestInput(t, false, 100)
    32  
    33  	tp := TestProposal
    34  	proposal, err := keeper.SubmitProposal(ctx, tp)
    35  	require.NoError(t, err)
    36  
    37  	require.True(t, proposal.VotingStartTime.Equal(time.Time{}))
    38  
    39  	keeper.activateVotingPeriod(ctx, proposal)
    40  
    41  	require.True(t, proposal.VotingStartTime.Equal(ctx.BlockHeader().Time))
    42  
    43  	proposal, ok := keeper.GetProposal(ctx, proposal.ProposalID)
    44  	require.True(t, ok)
    45  
    46  	activeIterator := keeper.ActiveProposalQueueIterator(ctx, proposal.VotingEndTime)
    47  	require.True(t, activeIterator.Valid())
    48  
    49  	proposalID := types.GetProposalIDFromBytes(activeIterator.Value())
    50  	require.Equal(t, proposalID, proposal.ProposalID)
    51  	activeIterator.Close()
    52  }
    53  
    54  type validProposal struct{}
    55  
    56  func (validProposal) GetTitle() string       { return "title" }
    57  func (validProposal) GetDescription() string { return "description" }
    58  func (validProposal) ProposalRoute() string  { return types.RouterKey }
    59  func (validProposal) ProposalType() string   { return types.ProposalTypeText }
    60  func (validProposal) String() string         { return "" }
    61  func (validProposal) ValidateBasic() error   { return nil }
    62  
    63  type invalidProposalTitle1 struct{ validProposal }
    64  
    65  func (invalidProposalTitle1) GetTitle() string { return "" }
    66  
    67  type invalidProposalTitle2 struct{ validProposal }
    68  
    69  func (invalidProposalTitle2) GetTitle() string { return strings.Repeat("1234567890", 100) }
    70  
    71  type invalidProposalDesc1 struct{ validProposal }
    72  
    73  func (invalidProposalDesc1) GetDescription() string { return "" }
    74  
    75  type invalidProposalDesc2 struct{ validProposal }
    76  
    77  func (invalidProposalDesc2) GetDescription() string { return strings.Repeat("1234567890", 1000) }
    78  
    79  type invalidProposalRoute struct{ validProposal }
    80  
    81  func (invalidProposalRoute) ProposalRoute() string { return "nonexistingroute" }
    82  
    83  type invalidProposalValidation struct{ validProposal }
    84  
    85  func (invalidProposalValidation) ValidateBasic() error {
    86  	return errors.New("invalid proposal")
    87  }
    88  
    89  func registerTestCodec(cdc *codec.Codec) {
    90  	cdc.RegisterConcrete(validProposal{}, "test/validproposal", nil)
    91  	cdc.RegisterConcrete(invalidProposalTitle1{}, "test/invalidproposalt1", nil)
    92  	cdc.RegisterConcrete(invalidProposalTitle2{}, "test/invalidproposalt2", nil)
    93  	cdc.RegisterConcrete(invalidProposalDesc1{}, "test/invalidproposald1", nil)
    94  	cdc.RegisterConcrete(invalidProposalDesc2{}, "test/invalidproposald2", nil)
    95  	cdc.RegisterConcrete(invalidProposalRoute{}, "test/invalidproposalr", nil)
    96  	cdc.RegisterConcrete(invalidProposalValidation{}, "test/invalidproposalv", nil)
    97  }
    98  
    99  func TestSubmitProposal(t *testing.T) {
   100  	ctx, _, keeper, _, _ := createTestInput(t, false, 100)
   101  
   102  	registerTestCodec(keeper.cdc)
   103  
   104  	testCases := []struct {
   105  		content     types.Content
   106  		expectedErr error
   107  	}{
   108  		{validProposal{}, nil},
   109  		// Keeper does not check the validity of title and description, no error
   110  		{invalidProposalTitle1{}, nil},
   111  		{invalidProposalTitle2{}, nil},
   112  		{invalidProposalDesc1{}, nil},
   113  		{invalidProposalDesc2{}, nil},
   114  		// error only when invalid route
   115  		{invalidProposalRoute{}, types.ErrNoProposalHandlerExists},
   116  		// Keeper does not call ValidateBasic, msg.ValidateBasic does
   117  		{invalidProposalValidation{}, nil},
   118  	}
   119  
   120  	for i, tc := range testCases {
   121  		_, err := keeper.SubmitProposal(ctx, tc.content)
   122  		require.True(t, errors.Is(tc.expectedErr, err), "tc #%d; got: %v, expected: %v", i, err, tc.expectedErr)
   123  	}
   124  }
   125  
   126  func TestGetProposalsFiltered(t *testing.T) {
   127  	proposalID := uint64(1)
   128  	ctx, _, keeper, _, _ := createTestInput(t, false, 100)
   129  	status := []types.ProposalStatus{types.StatusDepositPeriod, types.StatusVotingPeriod}
   130  
   131  	addr1 := sdk.AccAddress("foo")
   132  
   133  	for _, s := range status {
   134  		for i := 0; i < 50; i++ {
   135  			p := types.NewProposal(TestProposal, proposalID, time.Now(), time.Now())
   136  			p.Status = s
   137  
   138  			if i%2 == 0 {
   139  				d := types.NewDeposit(proposalID, addr1, nil)
   140  				v := types.NewVote(proposalID, addr1, types.OptionYes)
   141  				keeper.SetDeposit(ctx, d)
   142  				keeper.SetVote(ctx, v)
   143  			}
   144  
   145  			keeper.SetProposal(ctx, p)
   146  			proposalID++
   147  		}
   148  	}
   149  
   150  	testCases := []struct {
   151  		params             types.QueryProposalsParams
   152  		expectedNumResults int
   153  	}{
   154  		{types.NewQueryProposalsParams(1, 50, types.StatusNil, nil, nil), 50},
   155  		{types.NewQueryProposalsParams(1, 50, types.StatusDepositPeriod, nil, nil), 50},
   156  		{types.NewQueryProposalsParams(1, 50, types.StatusVotingPeriod, nil, nil), 50},
   157  		{types.NewQueryProposalsParams(1, 25, types.StatusNil, nil, nil), 25},
   158  		{types.NewQueryProposalsParams(2, 25, types.StatusNil, nil, nil), 25},
   159  		{types.NewQueryProposalsParams(1, 50, types.StatusRejected, nil, nil), 0},
   160  		{types.NewQueryProposalsParams(1, 50, types.StatusNil, addr1, nil), 50},
   161  		{types.NewQueryProposalsParams(1, 50, types.StatusNil, nil, addr1), 50},
   162  		{types.NewQueryProposalsParams(1, 50, types.StatusNil, addr1, addr1), 50},
   163  		{types.NewQueryProposalsParams(1, 50, types.StatusDepositPeriod, addr1, addr1), 25},
   164  		{types.NewQueryProposalsParams(1, 50, types.StatusDepositPeriod, nil, nil), 50},
   165  		{types.NewQueryProposalsParams(1, 50, types.StatusVotingPeriod, nil, nil), 50},
   166  	}
   167  
   168  	for _, tc := range testCases {
   169  		proposals := keeper.GetProposalsFiltered(ctx, tc.params)
   170  		require.Len(t, proposals, tc.expectedNumResults)
   171  
   172  		for _, p := range proposals {
   173  			if len(tc.params.ProposalStatus.String()) != 0 {
   174  				require.Equal(t, tc.params.ProposalStatus, p.Status)
   175  			}
   176  		}
   177  	}
   178  }