Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add test case to QueryInactiveContracts #82

Merged
merged 18 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* [\#78](https://github.com/Finschia/wasmd/pull/78) add the check for TestMigrateContract
* [\#69](https://github.com/Finschia/wasmd/pull/69) refactor: refactor test cases for Params
* [\#71](https://github.com/Finschia/wasmd/pull/71) add test cases in ContractsByCode
* [\#82](https://github.com/Finschia/wasmd/pull/82) add test case to QueryInactiveContracts


170210 marked this conversation as resolved.
Show resolved Hide resolved
### Bug Fixes
* [\#62](https://github.com/Finschia/wasmd/pull/62) fill ContractHistory querier result's Updated field
Expand Down
84 changes: 78 additions & 6 deletions x/wasmplus/keeper/querier_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package keeper

import (
"encoding/base64"
"fmt"
"github.com/Finschia/finschia-sdk/types/query"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
Expand All @@ -21,19 +24,80 @@ func TestQueryInactiveContracts(t *testing.T) {
example2 := InstantiateHackatomExampleContract(t, ctx, keepers)
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)

// Address order of contracts is ascending order of byte array whose address is decoded by bech32
expAddrs := CreateOrderedAddresses(example1.Contract.Bytes(), example2.Contract.Bytes())
170210 marked this conversation as resolved.
Show resolved Hide resolved

// set inactive
err := keeper.deactivateContract(ctx, example1.Contract)
require.NoError(t, err)
err = keeper.deactivateContract(ctx, example2.Contract)
require.NoError(t, err)

q := Querier(keeper)
rq := types.QueryInactiveContractsRequest{}
res, err := q.InactiveContracts(sdk.WrapSDKContext(ctx), &rq)
require.NoError(t, err)
expect := []string{example1.Contract.String(), example2.Contract.String()}
for _, exp := range expect {
assert.Contains(t, res.Addresses, exp)
specs := map[string]struct {
srcQuery *types.QueryInactiveContractsRequest
expAddrs []string
expPaginationTotal uint64
expErr error
}{
"req nil": {
srcQuery: nil,
expErr: status.Error(codes.InvalidArgument, "empty request"),
},
"query all": {
srcQuery: &types.QueryInactiveContractsRequest{},
expAddrs: expAddrs,
expPaginationTotal: 2,
},
"with pagination offset": {
srcQuery: &types.QueryInactiveContractsRequest{
Pagination: &query.PageRequest{
Offset: 1,
},
},
expAddrs: []string{expAddrs[1]},
expPaginationTotal: 2,
},
"with invalid pagination key": {
srcQuery: &types.QueryInactiveContractsRequest{
Pagination: &query.PageRequest{
Offset: 1,
Key: []byte("test"),
},
},
expErr: fmt.Errorf("invalid request, either offset or key is expected, got both"),
},
"with pagination limit": {
srcQuery: &types.QueryInactiveContractsRequest{
Pagination: &query.PageRequest{
Limit: 1,
},
},
expAddrs: []string{expAddrs[0]},
expPaginationTotal: 0,
},
"with pagination next key": {
srcQuery: &types.QueryInactiveContractsRequest{
Pagination: &query.PageRequest{
Key: fromBase64("reSl9YA6Q5g1xjY5Wo1kje5XsvyQ2Y3Bf6iHFZtpY4s="),
},
},
expAddrs: []string{expAddrs[1]},
expPaginationTotal: 0,
},
}

for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
got, err := q.InactiveContracts(sdk.WrapSDKContext(ctx), spec.srcQuery)
if spec.expErr != nil {
require.Equal(t, spec.expErr, err, "but got %+v", err)
return
}
require.NoError(t, err)
assert.Equal(t, spec.expAddrs, got.Addresses)
assert.EqualValues(t, spec.expPaginationTotal, got.Pagination.Total)
})
}
}

Expand Down Expand Up @@ -88,3 +152,11 @@ func TestQueryInactiveContract(t *testing.T) {
})
}
}

func fromBase64(s string) []byte {
r, err := base64.StdEncoding.DecodeString(s)
if err != nil {
panic(err)
}
return r
}
14 changes: 14 additions & 0 deletions x/wasmplus/keeper/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"os"
"sort"
"testing"
"time"

Expand Down Expand Up @@ -688,3 +689,16 @@ func keyPubAddr() (crypto.PrivKey, crypto.PubKey, sdk.AccAddress) {
addr := sdk.AccAddress(pub.Address())
return key, pub, addr
}

func CreateOrderedAddresses(addrs ...[]byte) []string {
Kynea0b marked this conversation as resolved.
Show resolved Hide resolved
sort.Slice(addrs, func(i, j int) bool {
return string(addrs[i]) < string(addrs[j])
})

expAddrs := make([]string, len(addrs))
for i, b := range addrs {
expAddrs[i] = sdk.AccAddress(b).String()
}

return expAddrs
}