Skip to content

Commit

Permalink
QueryTG4Members.* integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolacy committed Dec 10, 2021
1 parent 08245b7 commit 1595ae4
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 4 deletions.
16 changes: 12 additions & 4 deletions x/poe/contract/tg4.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package contract

import (
"encoding/json"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"sort"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -35,8 +37,8 @@ type ListMembersQuery struct {
}

type ListMembersByWeightQuery struct {
StartAfter []byte `json:"start_after,omitempty"`
Limit int `json:"limit,omitempty"`
StartAfter *TG4Member `json:"start_after,omitempty"`
Limit int `json:"limit,omitempty"`
}

type MemberQuery struct {
Expand All @@ -63,10 +65,16 @@ type TG4TotalWeightResponse struct {
}

func QueryTG4MembersByWeight(ctx sdk.Context, k types.SmartQuerier, tg4Addr sdk.AccAddress, pagination *types.Paginator) ([]TG4Member, error) {
var startAfter []byte
var sa TG4Member
var startAfter *TG4Member
var limit int
if pagination != nil {
startAfter = pagination.StartAfter
if pagination.StartAfter != nil {
if err := json.Unmarshal(pagination.StartAfter, &sa); err != nil {
return nil, sdkerrors.Wrap(err, "query tg4 members by weight")
}
startAfter = &sa
}
limit = int(pagination.Limit)
}
query := TG4Query{ListMembersByWeight: &ListMembersByWeightQuery{StartAfter: startAfter, Limit: limit}}
Expand Down
130 changes: 130 additions & 0 deletions x/poe/contract/tgrade_validator_voting_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package contract_test
import (
_ "embed"
"encoding/json"
"sort"
"testing"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -129,5 +130,134 @@ func TestValidatorsGovProposal(t *testing.T) {
spec.assertExp(t, ctx)
})
}
}

func TestQueryTG4Members(t *testing.T) {
// setup contracts and seed some data
ctx, example, vals := setupPoEContracts(t)
require.Len(t, vals, 3)

contractAddr, err := example.PoEKeeper.GetPoEContractAddress(ctx, types.PoEContractTypeDistribution)
require.NoError(t, err)
// ensure members set
// FIXME: Get members from test setup
expMembers, err := contract.QueryTG4Members(ctx, example.TWasmKeeper, contractAddr, nil)
require.NoError(t, err)
require.Len(t, expMembers, 3)

specs := map[string]struct {
pagination *types.Paginator
expVal []contract.TG4Member
expEmpty bool
expError bool
}{
"query no pagination": {
pagination: nil,
expVal: expMembers,
},
"query offset 0, limit 2": {
pagination: &types.Paginator{Limit: 2},
expVal: expMembers[:2],
},
"query offset 2, limit 2": {
pagination: &types.Paginator{StartAfter: []byte(expMembers[1].Addr), Limit: 2},
expVal: expMembers[2:],
},
"query offset 3, limit 2": {
pagination: &types.Paginator{StartAfter: []byte(expMembers[2].Addr), Limit: 2},
expEmpty: true,
},
"query offset invalid addr, limit 2": {
pagination: &types.Paginator{StartAfter: []byte("invalid"), Limit: 2},
expError: true,
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
// when
gotMembers, err := contract.QueryTG4Members(ctx, example.TWasmKeeper, contractAddr, spec.pagination)

// then
if spec.expError {
require.Error(t, err)
} else {
require.NoError(t, err)
if spec.expEmpty {
assert.Equal(t, 0, len(gotMembers))
} else {
assert.Equal(t, spec.expVal, gotMembers)
}
}
})
}
}

func buildStartAfter(t *testing.T, m contract.TG4Member) []byte {
startAfter, err := json.Marshal(m)
require.NoError(t, err)
return startAfter
}

func TestQueryTG4MembersByWeight(t *testing.T) {
// setup contracts and seed some data
ctx, example, vals := setupPoEContracts(t)
require.Len(t, vals, 3)

contractAddr, err := example.PoEKeeper.GetPoEContractAddress(ctx, types.PoEContractTypeDistribution)
require.NoError(t, err)
// ensure members set
// FIXME: Get members from test setup
expMembers, err := contract.QueryTG4Members(ctx, example.TWasmKeeper, contractAddr, nil)
require.NoError(t, err)
require.Len(t, expMembers, 3)

sort.Slice(expMembers, func(i, j int) bool {
return expMembers[i].Weight > expMembers[j].Weight
})

specs := map[string]struct {
pagination *types.Paginator
expVal []contract.TG4Member
expEmpty bool
expError bool
}{
"query no pagination": {
pagination: nil,
expVal: expMembers,
},
"query offset 0, limit 2": {
pagination: &types.Paginator{Limit: 2},
expVal: expMembers[:2],
},
"query offset 2, limit 2": {
pagination: &types.Paginator{StartAfter: buildStartAfter(t, expMembers[1]), Limit: 2},
expVal: expMembers[2:],
},
"query offset 3, limit 2": {
pagination: &types.Paginator{StartAfter: buildStartAfter(t, expMembers[2]), Limit: 2},
expEmpty: true,
},
"query offset invalid addr, limit 2": {
pagination: &types.Paginator{StartAfter: []byte("invalid"), Limit: 2},
expError: true,
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
// when
gotMembers, err := contract.QueryTG4MembersByWeight(ctx, example.TWasmKeeper, contractAddr, spec.pagination)

// then
if spec.expError {
require.Error(t, err)
} else {
require.NoError(t, err)
if spec.expEmpty {
assert.Equal(t, 0, len(gotMembers))
} else {
assert.Equal(t, spec.expVal, gotMembers)
}
}
})
}
}

0 comments on commit 1595ae4

Please sign in to comment.