Skip to content

Commit

Permalink
test: add test cases in ContractsByCode (#71)
Browse files Browse the repository at this point in the history
* test: add test cases in ContractsByCode

Signed-off-by: 170210 <j170210@icloud.com>

* chore: add this PR to CHANGELOG

Signed-off-by: 170210 <j170210@icloud.com>

* chore: add an explanation for block setting

Signed-off-by: 170210 <j170210@icloud.com>

---------

Signed-off-by: 170210 <j170210@icloud.com>
  • Loading branch information
170210 authored Aug 18, 2023
1 parent f504ce8 commit 40fa7c7
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* [\#74](https://github.com/Finschia/wasmd/pull/74) add event checking to TestInstantiateContract2
* [\#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

### Bug Fixes
* [\#62](https://github.com/Finschia/wasmd/pull/62) fill ContractHistory querier result's Updated field
Expand Down
121 changes: 121 additions & 0 deletions x/wasm/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,127 @@ func TestQueryRawContractState(t *testing.T) {
}
}

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

// store contract
contract := StoreHackatomExampleContract(t, ctx, keepers)

createInitMsg := func(ctx sdk.Context, keepers TestKeepers) sdk.AccAddress {
_, _, verifierAddr := keyPubAddr()
fundAccounts(t, ctx, keepers.AccountKeeper, keepers.BankKeeper, verifierAddr, contract.InitialAmount)

_, _, beneficiaryAddr := keyPubAddr()
initMsgBz := HackatomExampleInitMsg{
Verifier: verifierAddr,
Beneficiary: beneficiaryAddr,
}.GetBytes(t)
initialAmount := sdk.NewCoins(sdk.NewInt64Coin("denom", 100))

adminAddr := contract.CreatorAddr
label := "demo contract to query"
contractAddr, _, err := keepers.ContractKeeper.Instantiate(ctx, contract.CodeID, contract.CreatorAddr, adminAddr, initMsgBz, label, initialAmount)
fmt.Println(contract.CodeID)
require.NoError(t, err)
return contractAddr
}

// related link: https://github.com/CosmWasm/wasmd/issues/1559
// When not set with a block, the contract will return results based on the order of address rather than instantiation (timestamp).
// Since this function will test results for pagination, it is necessary to ensure that the results are returned in the order of instantiation.
// manage some realistic block settings
meter := sdk.NewGasMeter(1000000)
ctx = ctx.WithGasMeter(meter)
ctx = ctx.WithBlockGasMeter(meter)

// create 2 contracts with real block/gas setup
exampleContractAddr1 := createInitMsg(ctx, keepers).String()
exampleContractAddr2 := createInitMsg(ctx, keepers).String()

specs := map[string]struct {
req *types.QueryContractsByCodeRequest
expAddr []string
expPaginationTotal uint64
expErr error
}{
"with empty request": {
req: nil,
expErr: status.Error(codes.InvalidArgument, "empty request"),
},
"req.CodeId=0": {
req: &types.QueryContractsByCodeRequest{CodeId: 0},
expErr: sdkErrors.Wrap(types.ErrInvalid, "code id"),
},
"not exist codeID": {
req: &types.QueryContractsByCodeRequest{CodeId: 2},
expAddr: []string{},
expPaginationTotal: 0,
},
"query all": {
req: &types.QueryContractsByCodeRequest{
CodeId: 1,
},
expAddr: []string{exampleContractAddr1, exampleContractAddr2},
expPaginationTotal: 2,
},
"with pagination offset": {
req: &types.QueryContractsByCodeRequest{
CodeId: 1,
Pagination: &query.PageRequest{
Offset: 1,
},
},
expAddr: []string{exampleContractAddr2},
expPaginationTotal: 2,
},
"with invalid pagination key": {
req: &types.QueryContractsByCodeRequest{
CodeId: 1,
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: 1,
Pagination: &query.PageRequest{
Limit: 1,
},
},
expAddr: []string{exampleContractAddr1},
expPaginationTotal: 0,
},
"with pagination next key": {
req: &types.QueryContractsByCodeRequest{
CodeId: 1,
Pagination: &query.PageRequest{
Key: fromBase64("AAAAAAAS1ocAAAAAAASY0YcuhNIMvyvID4NhhfROlbQNuZ0fl0clmBPoWHtKYazH"),
},
},
expAddr: []string{exampleContractAddr2},
expPaginationTotal: 0,
},
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
got, err := q.ContractsByCode(sdk.WrapSDKContext(ctx), spec.req)

if spec.expErr != nil {
assert.NotNil(t, err)
assert.EqualError(t, err, spec.expErr.Error())
return
}
assert.EqualValues(t, spec.expPaginationTotal, got.Pagination.Total)
assert.NotNil(t, got)
assert.Equal(t, spec.expAddr, got.Contracts)
})
}
}
func TestQueryContractListByCodeOrdering(t *testing.T) {
ctx, keepers := CreateTestInput(t, false, AvailableCapabilities)
keeper := keepers.WasmKeeper
Expand Down

0 comments on commit 40fa7c7

Please sign in to comment.