github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/gov/client/utils/query_test.go (about) 1 package utils 2 3 import ( 4 codectypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec/types" 5 "testing" 6 7 "github.com/fibonacci-chain/fbc/libs/tendermint/rpc/client/mock" 8 ctypes "github.com/fibonacci-chain/fbc/libs/tendermint/rpc/core/types" 9 tmtypes "github.com/fibonacci-chain/fbc/libs/tendermint/types" 10 "github.com/stretchr/testify/require" 11 12 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client" 13 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context" 14 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec" 15 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 16 authtypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/types" 17 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/gov/types" 18 ) 19 20 type TxSearchMock struct { 21 mock.Client 22 txs []tmtypes.Tx 23 } 24 25 func (mock TxSearchMock) TxSearch(query string, prove bool, page, perPage int, orderBy string) (*ctypes.ResultTxSearch, error) { 26 start, end := client.Paginate(len(mock.txs), page, perPage, 100) 27 if start < 0 || end < 0 { 28 // nil result with nil error crashes utils.QueryTxsByEvents 29 return &ctypes.ResultTxSearch{}, nil 30 } 31 txs := mock.txs[start:end] 32 rst := &ctypes.ResultTxSearch{Txs: make([]*ctypes.ResultTx, len(txs)), TotalCount: len(txs)} 33 for i := range txs { 34 rst.Txs[i] = &ctypes.ResultTx{Tx: txs[i]} 35 } 36 return rst, nil 37 } 38 39 func (mock TxSearchMock) Block(height *int64) (*ctypes.ResultBlock, error) { 40 // any non nil Block needs to be returned. used to get time value 41 return &ctypes.ResultBlock{Block: &tmtypes.Block{}}, nil 42 } 43 44 func newTestCodec() *codec.CodecProxy { 45 cdc := codec.New() 46 sdk.RegisterCodec(cdc) 47 types.RegisterCodec(cdc) 48 authtypes.RegisterCodec(cdc) 49 reg := codectypes.NewInterfaceRegistry() 50 px := codec.NewCodecProxy(codec.NewProtoCodec(reg), cdc) 51 return px 52 } 53 54 func TestGetPaginatedVotes(t *testing.T) { 55 type testCase struct { 56 description string 57 page, limit int 58 txs []authtypes.StdTx 59 votes []types.Vote 60 } 61 acc1 := make(sdk.AccAddress, 20) 62 acc1[0] = 1 63 acc2 := make(sdk.AccAddress, 20) 64 acc2[0] = 2 65 acc1Msgs := []sdk.Msg{ 66 types.NewMsgVote(acc1, 0, types.OptionYes), 67 types.NewMsgVote(acc1, 0, types.OptionYes), 68 } 69 acc2Msgs := []sdk.Msg{ 70 types.NewMsgVote(acc2, 0, types.OptionYes), 71 types.NewMsgVote(acc2, 0, types.OptionYes), 72 } 73 for _, tc := range []testCase{ 74 { 75 description: "1MsgPerTxAll", 76 page: 1, 77 limit: 2, 78 txs: []authtypes.StdTx{ 79 {Msgs: acc1Msgs[:1]}, 80 {Msgs: acc2Msgs[:1]}, 81 }, 82 votes: []types.Vote{ 83 types.NewVote(0, acc1, types.OptionYes), 84 types.NewVote(0, acc2, types.OptionYes)}, 85 }, 86 87 { 88 description: "2MsgPerTx1Chunk", 89 page: 1, 90 limit: 2, 91 txs: []authtypes.StdTx{ 92 {Msgs: acc1Msgs}, 93 {Msgs: acc2Msgs}, 94 }, 95 votes: []types.Vote{ 96 types.NewVote(0, acc1, types.OptionYes), 97 types.NewVote(0, acc1, types.OptionYes)}, 98 }, 99 { 100 description: "2MsgPerTx2Chunk", 101 page: 2, 102 limit: 2, 103 txs: []authtypes.StdTx{ 104 {Msgs: acc1Msgs}, 105 {Msgs: acc2Msgs}, 106 }, 107 votes: []types.Vote{ 108 types.NewVote(0, acc2, types.OptionYes), 109 types.NewVote(0, acc2, types.OptionYes)}, 110 }, 111 { 112 description: "IncompleteSearchTx", 113 page: 1, 114 limit: 2, 115 txs: []authtypes.StdTx{ 116 {Msgs: acc1Msgs[:1]}, 117 }, 118 votes: []types.Vote{types.NewVote(0, acc1, types.OptionYes)}, 119 }, 120 { 121 description: "InvalidPage", 122 page: -1, 123 txs: []authtypes.StdTx{ 124 {Msgs: acc1Msgs[:1]}, 125 }, 126 }, 127 { 128 description: "OutOfBounds", 129 page: 2, 130 limit: 10, 131 txs: []authtypes.StdTx{ 132 {Msgs: acc1Msgs[:1]}, 133 }, 134 }, 135 } { 136 tc := tc 137 t.Run(tc.description, func(t *testing.T) { 138 var ( 139 marshalled = make([]tmtypes.Tx, len(tc.txs)) 140 cdc = newTestCodec() 141 ) 142 for i := range tc.txs { 143 tx, err := cdc.GetCdc().MarshalBinaryLengthPrefixed(&tc.txs[i]) 144 require.NoError(t, err) 145 marshalled[i] = tmtypes.Tx(tx) 146 } 147 client := TxSearchMock{txs: marshalled} 148 ctx := context.CLIContext{}.WithProxy(cdc).WithTrustNode(true).WithClient(client) 149 150 params := types.NewQueryProposalVotesParams(0, tc.page, tc.limit) 151 votesData, err := QueryVotesByTxQuery(ctx, params) 152 require.NoError(t, err) 153 votes := []types.Vote{} 154 require.NoError(t, ctx.Codec.UnmarshalJSON(votesData, &votes)) 155 require.Equal(t, len(tc.votes), len(votes)) 156 for i := range votes { 157 require.Equal(t, tc.votes[i], votes[i]) 158 } 159 }) 160 } 161 }