Skip to content

Commit

Permalink
Merge pull request #1607 from 170210/modify_wasmd_test
Browse files Browse the repository at this point in the history
test: add test cases in ContractsByCode
  • Loading branch information
alpe authored Sep 7, 2023
2 parents 7bd6566 + 9e3904c commit cb887ee
Showing 1 changed file with 76 additions and 9 deletions.
85 changes: 76 additions & 9 deletions x/wasm/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func TestQueryRawContractState(t *testing.T) {
}
}

func TestQueryContractListByCodeOrdering(t *testing.T) {
func TestQueryContractsByCode(t *testing.T) {
ctx, keepers := CreateTestInput(t, false, AvailableCapabilities)
keeper := keepers.WasmKeeper

Expand Down Expand Up @@ -298,26 +298,93 @@ func TestQueryContractListByCodeOrdering(t *testing.T) {
return ctx
}

contractAddrs := make([]string, 0, 10)
// create 10 contracts with real block/gas setup
for i := 0; i < 10; i++ {
// 3 tx per block, so we ensure both comparisons work
if i%3 == 0 {
ctx = setBlock(ctx, h)
h++
}
_, _, err = keepers.ContractKeeper.Instantiate(ctx, codeID, creator, nil, initMsgBz, fmt.Sprintf("contract %d", i), topUp)
addr, _, err := keepers.ContractKeeper.Instantiate(ctx, codeID, creator, nil, initMsgBz, fmt.Sprintf("contract %d", i), topUp)
contractAddrs = append(contractAddrs, addr.String())
require.NoError(t, err)
}

// query and check the results are properly sorted
q := Querier(keeper)
res, err := q.ContractsByCode(sdk.WrapSDKContext(ctx), &types.QueryContractsByCodeRequest{CodeId: codeID})
require.NoError(t, err)

require.Equal(t, 10, len(res.Contracts))
specs := map[string]struct {
req *types.QueryContractsByCodeRequest
expAddr []string
expErr error
}{
"with empty request": {
req: nil,
expErr: status.Error(codes.InvalidArgument, "empty request"),
},
"req.CodeId=0": {
req: &types.QueryContractsByCodeRequest{CodeId: 0},
expErr: errorsmod.Wrap(types.ErrInvalid, "code id"),
},
"not exist codeID": {
req: &types.QueryContractsByCodeRequest{CodeId: codeID + 1},
expAddr: []string{},
},
"query all and check the results are properly sorted": {
req: &types.QueryContractsByCodeRequest{
CodeId: codeID,
},
expAddr: contractAddrs,
},
"with pagination offset": {
req: &types.QueryContractsByCodeRequest{
CodeId: codeID,
Pagination: &query.PageRequest{
Offset: 5,
},
},
expAddr: contractAddrs[5:10],
},
"with invalid pagination key": {
req: &types.QueryContractsByCodeRequest{
CodeId: codeID,
Pagination: &query.PageRequest{
Offset: 1,
Key: []byte("test"),
},
},
expErr: fmt.Errorf("invalid request, either offset or key is expected, got both"),
},
"with pagination limit": {
req: &types.QueryContractsByCodeRequest{
CodeId: codeID,
Pagination: &query.PageRequest{
Limit: 5,
},
},
expAddr: contractAddrs[0:5],
},
"with pagination next key": {
req: &types.QueryContractsByCodeRequest{
CodeId: codeID,
Pagination: &query.PageRequest{
Key: fromBase64("AAAAAAAAAAoAAAAAAAOc/4cuhNIMvyvID4NhhfROlbQNuZ0fl0clmBPoWHtKYazH"),
},
},
expAddr: contractAddrs[1:10],
},
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
got, err := q.ContractsByCode(sdk.WrapSDKContext(ctx), spec.req)

for _, contractAddr := range res.Contracts {
assert.NotEmpty(t, contractAddr)
if spec.expErr != nil {
assert.NotNil(t, err)
assert.EqualError(t, err, spec.expErr.Error())
return
}
assert.NotNil(t, got)
assert.Equal(t, spec.expAddr, got.Contracts)
})
}
}

Expand Down

0 comments on commit cb887ee

Please sign in to comment.