Skip to content

Commit

Permalink
#2730 bring query txs tests back
Browse files Browse the repository at this point in the history
  • Loading branch information
ackratos committed Jan 15, 2019
1 parent 449f199 commit 7e5c3b9
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 34 deletions.
51 changes: 27 additions & 24 deletions client/tx/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import (
)

const (
flagTags = "tags"
flagAny = "any"
flagPage = "page"
flagLimit = "limit"
flagTags = "tags"
flagAny = "any"
flagPage = "page"
flagLimit = "limit"
defaultPage = 1
defaultLimit = 30 // should be consistent with tendermint/tendermint/rpc/core/pipe.go:19
)

// default client command to search through tagged transactions
Expand Down Expand Up @@ -96,8 +98,8 @@ $ gaiacli query txs --tags '<tag1>:<value1>&<tag2>:<value2>' --page 1 --limit 30
cmd.Flags().Bool(client.FlagTrustNode, false, "Trust connected full node (don't verify proofs for responses)")
viper.BindPFlag(client.FlagTrustNode, cmd.Flags().Lookup(client.FlagTrustNode))
cmd.Flags().String(flagTags, "", "tag:value list of tags that must match")
cmd.Flags().Int32(flagPage, 1, "Query a specific page of paginated results")
cmd.Flags().Int32(flagLimit, 30, "Query number of transactions results per page returned")
cmd.Flags().Int32(flagPage, defaultPage, "Query a specific page of paginated results")
cmd.Flags().Int32(flagLimit, defaultLimit, "Query number of transactions results per page returned")
return cmd
}

Expand Down Expand Up @@ -208,7 +210,7 @@ func parseHTTPArgs(r *http.Request) (tags []string, page, limit int, err error)
var value string
value, err = url.QueryUnescape(values[0])
if err != nil {
return
return tags, page, limit, err
}

var tag string
Expand All @@ -222,26 +224,27 @@ func parseHTTPArgs(r *http.Request) (tags []string, page, limit int, err error)

pageStr := r.FormValue("page")
if pageStr == "" {
pageStr = "1"
}
page, err = strconv.Atoi(pageStr)
if err != nil {
return
} else if page <= 0 {
err = errors.New("page must greater than 0")
return
page = defaultPage
} else {
page, err = strconv.Atoi(pageStr)
if err != nil {
return tags, page, limit, err
} else if page <= 0 {
return tags, page, limit, errors.New("page must greater than 0")
}
}

limitStr := r.FormValue("limit")
if limitStr == "" {
limitStr = "30" // should be consistent with tendermint/tendermint/rpc/core/pipe.go:19
}
limit, err = strconv.Atoi(limitStr)
if err != nil {
return
} else if limit <= 0 {
err = errors.New("limit must greater than 0")
return
limit = defaultLimit
} else {
limit, err = strconv.Atoi(limitStr)
if err != nil {
return tags, page, limit, err
} else if limit <= 0 {
return tags, page, limit, errors.New("limit must greater than 0")
}
}
return

return tags, page, limit, nil
}
55 changes: 52 additions & 3 deletions cmd/gaia/cli_test/cli_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package clitest

import (
"errors"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -354,7 +355,7 @@ func TestGaiaCLISubmitProposal(t *testing.T) {
tests.WaitForNextNBlocksTM(1, f.Port)

// Ensure transaction tags can be queried
txs := f.QueryTxs("action:submit_proposal", fmt.Sprintf("proposer:%s", fooAddr))
txs := f.QueryTxs(1, 50, "action:submit_proposal", fmt.Sprintf("proposer:%s", fooAddr))
require.Len(t, txs, 1)

// Ensure deposit was deducted
Expand Down Expand Up @@ -397,7 +398,7 @@ func TestGaiaCLISubmitProposal(t *testing.T) {
require.Equal(t, int64(15), deposit.Amount.AmountOf(denom).Int64())

// Ensure tags are set on the transaction
txs = f.QueryTxs("action:deposit", fmt.Sprintf("depositor:%s", fooAddr))
txs = f.QueryTxs(1, 50, "action:deposit", fmt.Sprintf("depositor:%s", fooAddr))
require.Len(t, txs, 1)

// Ensure account has expected amount of funds
Expand Down Expand Up @@ -434,7 +435,7 @@ func TestGaiaCLISubmitProposal(t *testing.T) {
require.Equal(t, gov.OptionYes, votes[0].Option)

// Ensure tags are applied to voting transaction properly
txs = f.QueryTxs("action:vote", fmt.Sprintf("voter:%s", fooAddr))
txs = f.QueryTxs(1, 50, "action:vote", fmt.Sprintf("voter:%s", fooAddr))
require.Len(t, txs, 1)

// Ensure no proposals in deposit period
Expand All @@ -456,6 +457,54 @@ func TestGaiaCLISubmitProposal(t *testing.T) {
f.Cleanup()
}

func TestGaiaCLIQueryTxPagination(t *testing.T) {
t.Parallel()
f := InitFixtures(t)

// start gaiad server
proc := f.GDStart()
defer proc.Stop(false)

fooAddr := f.KeyAddress(keyFoo)
barAddr := f.KeyAddress(keyBar)

for i := 1; i <= 30; i++ {
success := executeWrite(t, fmt.Sprintf(
"gaiacli tx send %s --amount=%dfootoken --to=%s --from=foo",
f.Flags(), i, barAddr), app.DefaultKeyPass)
require.True(t, success)
tests.WaitForNextNBlocksTM(1, f.Port)
}

// perPage = 15, 2 pages
txsPage1 := f.QueryTxs(1, 15, fmt.Sprintf("sender:%s", fooAddr))
require.Len(t, txsPage1, 15)
txsPage2 := f.QueryTxs(2, 15, fmt.Sprintf("sender:%s", fooAddr))
require.Len(t, txsPage2, 15)
require.NotEqual(t, txsPage1, txsPage2)
txsPage3 := f.QueryTxs(3, 15, fmt.Sprintf("sender:%s", fooAddr))
require.Len(t, txsPage3, 15)
require.Equal(t, txsPage2, txsPage3)

// perPage = 16, 2 pages
txsPage1 = f.QueryTxs(1, 16, fmt.Sprintf("sender:%s", fooAddr))
require.Len(t, txsPage1, 16)
txsPage2 = f.QueryTxs(2, 16, fmt.Sprintf("sender:%s", fooAddr))
require.Len(t, txsPage2, 14)
require.NotEqual(t, txsPage1, txsPage2)

// perPage = 50
txsPageFull := f.QueryTxs(1, 50, fmt.Sprintf("sender:%s", fooAddr))
require.Len(t, txsPageFull, 30)
require.Equal(t, txsPageFull, append(txsPage1, txsPage2...))

// perPage = 0
f.QueryTxsInvalid(errors.New("ERROR: page must greater than 0"), 0, 50, fmt.Sprintf("sender:%s", fooAddr))

// limit = 0
f.QueryTxsInvalid(errors.New("ERROR: limit must greater than 0"), 1, 0, fmt.Sprintf("sender:%s", fooAddr))
}

func TestGaiaCLIValidateSignatures(t *testing.T) {
t.Parallel()
f := InitFixtures(t)
Expand Down
11 changes: 9 additions & 2 deletions cmd/gaia/cli_test/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ func (f *Fixtures) QueryAccount(address sdk.AccAddress, flags ...string) auth.Ba
// gaiacli query txs

// QueryTxs is gaiacli query txs
func (f *Fixtures) QueryTxs(tags ...string) []tx.Info {
cmd := fmt.Sprintf("gaiacli query txs --tags='%s' %v", queryTags(tags), f.Flags())
func (f *Fixtures) QueryTxs(page, limit int, tags ...string) []tx.Info {
cmd := fmt.Sprintf("gaiacli query txs --page=%d --limit=%d --tags='%s' %v", page, limit, queryTags(tags), f.Flags())
out, _ := tests.ExecuteT(f.T, cmd, "")
var txs []tx.Info
cdc := app.MakeCodec()
Expand All @@ -305,6 +305,13 @@ func (f *Fixtures) QueryTxs(tags ...string) []tx.Info {
return txs
}

// QueryTxsInvalid query txs with wrong parameters and compare expected error
func (f *Fixtures) QueryTxsInvalid(expectedErr error, page, limit int, tags ...string) {
cmd := fmt.Sprintf("gaiacli query txs --page=%d --limit=%d --tags='%s' %v", page, limit, queryTags(tags), f.Flags())
_, err := tests.ExecuteT(f.T, cmd, "")
require.EqualError(f.T, expectedErr, err)
}

//___________________________________________________________________________________
// gaiacli query staking

Expand Down
15 changes: 10 additions & 5 deletions x/gov/client/utils/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
"github.com/cosmos/cosmos-sdk/x/gov/tags"
)

const (
defaultPage = 1
defaultLimit = 30 // should be consistent with tendermint/tendermint/rpc/core/pipe.go:19
)

// Proposer contains metadata of a governance proposal used for querying a
// proposer.
type Proposer struct {
Expand All @@ -33,7 +38,7 @@ func QueryDepositsByTxQuery(

// NOTE: SearchTxs is used to facilitate the txs query which does not currently
// support configurable pagination.
infos, err := tx.SearchTxs(cliCtx, cdc, tags, 1, 30)
infos, err := tx.SearchTxs(cliCtx, cdc, tags, defaultPage, defaultLimit)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -78,7 +83,7 @@ func QueryVotesByTxQuery(

// NOTE: SearchTxs is used to facilitate the txs query which does not currently
// support configurable pagination.
infos, err := tx.SearchTxs(cliCtx, cdc, tags, 1, 30)
infos, err := tx.SearchTxs(cliCtx, cdc, tags, defaultPage, defaultLimit)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -119,7 +124,7 @@ func QueryVoteByTxQuery(

// NOTE: SearchTxs is used to facilitate the txs query which does not currently
// support configurable pagination.
infos, err := tx.SearchTxs(cliCtx, cdc, tags, 1, 30)
infos, err := tx.SearchTxs(cliCtx, cdc, tags, defaultPage, defaultLimit)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -162,7 +167,7 @@ func QueryDepositByTxQuery(

// NOTE: SearchTxs is used to facilitate the txs query which does not currently
// support configurable pagination.
infos, err := tx.SearchTxs(cliCtx, cdc, tags, 1, 30)
infos, err := tx.SearchTxs(cliCtx, cdc, tags, defaultPage, defaultLimit)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -204,7 +209,7 @@ func QueryProposerByTxQuery(

// NOTE: SearchTxs is used to facilitate the txs query which does not currently
// support configurable pagination.
infos, err := tx.SearchTxs(cliCtx, cdc, tags, 1, 30)
infos, err := tx.SearchTxs(cliCtx, cdc, tags, defaultPage, defaultLimit)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 7e5c3b9

Please sign in to comment.