github.com/cosmos/cosmos-sdk@v0.50.10/x/group/keeper/tally_test.go (about)

     1  package keeper_test
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	sdk "github.com/cosmos/cosmos-sdk/types"
     8  	banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
     9  	"github.com/cosmos/cosmos-sdk/x/group"
    10  )
    11  
    12  func (s *TestSuite) TestTally() {
    13  	addrs := s.addrs
    14  	addr2 := addrs[1]
    15  
    16  	msgSend1 := &banktypes.MsgSend{
    17  		FromAddress: s.groupPolicyAddr.String(),
    18  		ToAddress:   addr2.String(),
    19  		Amount:      sdk.Coins{sdk.NewInt64Coin("test", 100)},
    20  	}
    21  	proposers := []string{addr2.String()}
    22  
    23  	specs := map[string]struct {
    24  		srcBlockTime   time.Time
    25  		setupProposal  func(ctx context.Context) uint64
    26  		expErr         bool
    27  		expTallyResult group.TallyResult
    28  	}{
    29  		"invalid proposal id": {
    30  			setupProposal: func(ctx context.Context) uint64 {
    31  				return 123
    32  			},
    33  			expErr: true,
    34  		},
    35  		"proposal with no votes": {
    36  			setupProposal: func(ctx context.Context) uint64 {
    37  				msgs := []sdk.Msg{msgSend1}
    38  				return submitProposal(ctx, s, msgs, proposers)
    39  			},
    40  			expTallyResult: group.DefaultTallyResult(),
    41  		},
    42  		"withdrawn proposal": {
    43  			setupProposal: func(ctx context.Context) uint64 {
    44  				msgs := []sdk.Msg{msgSend1}
    45  				proposalID := submitProposal(ctx, s, msgs, proposers)
    46  				_, err := s.groupKeeper.WithdrawProposal(ctx, &group.MsgWithdrawProposal{
    47  					ProposalId: proposalID,
    48  					Address:    proposers[0],
    49  				})
    50  				s.Require().NoError(err)
    51  
    52  				return proposalID
    53  			},
    54  			expErr: true,
    55  		},
    56  		"proposal with some votes": {
    57  			setupProposal: func(ctx context.Context) uint64 {
    58  				msgs := []sdk.Msg{msgSend1}
    59  				return submitProposalAndVote(ctx, s, msgs, proposers, group.VOTE_OPTION_YES)
    60  			},
    61  			expTallyResult: group.TallyResult{
    62  				YesCount:        "2",
    63  				NoCount:         "0",
    64  				NoWithVetoCount: "0",
    65  				AbstainCount:    "0",
    66  			},
    67  		},
    68  	}
    69  
    70  	for msg, spec := range specs {
    71  		spec := spec
    72  		s.Run(msg, func() {
    73  			sdkCtx, _ := s.sdkCtx.CacheContext()
    74  			pID := spec.setupProposal(sdkCtx)
    75  			req := &group.QueryTallyResultRequest{
    76  				ProposalId: pID,
    77  			}
    78  
    79  			res, err := s.groupKeeper.TallyResult(sdkCtx, req)
    80  			if spec.expErr {
    81  				s.Require().Error(err)
    82  			} else {
    83  				s.Require().NoError(err)
    84  				s.Require().Equal(res.Tally, spec.expTallyResult)
    85  			}
    86  		})
    87  	}
    88  }