Skip to content

Commit

Permalink
test: add test cases for empty request in each function (#65)
Browse files Browse the repository at this point in the history
* test: add empty request test in ContractHistory

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

* test: add empty request test in AllContractState

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

* test: add empty request test in ContractInfo

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

* test: add empty request test in RawContractState

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

* test: add empty request test in SmartContractState

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

* test: add empty request test in Codes

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

* chore: add this PR to CHANGELOG

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

* fixup: fix for comment

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

---------

Signed-off-by: 170210 <j170210@icloud.com>
  • Loading branch information
170210 authored Aug 10, 2023
1 parent 52a6659 commit 1f60af6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

### Improvements
* [\#63](https://github.com/Finschia/wasmd/pull/63) add event checking to TestStoreCode
* [\#65](https://github.com/Finschia/wasmd/pull/65) add test cases for empty request in each function

### Bug Fixes
* [\#52](https://github.com/Finschia/wasmd/pull/52) fix cli_test error of wasmplus and add cli_test ci
Expand Down
89 changes: 63 additions & 26 deletions x/wasm/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestQueryAllContractState(t *testing.T) {
srcQuery *types.QueryAllContractStateRequest
expModelContains []types.Model
expModelContainsNot []types.Model
expErr *sdkErrors.Error
expErr error
}{
"query all": {
srcQuery: &types.QueryAllContractStateRequest{Address: contractAddr.String()},
Expand Down Expand Up @@ -95,12 +95,16 @@ func TestQueryAllContractState(t *testing.T) {
{Key: []byte{0x0, 0x1}, Value: []byte(`{"count":8}`)},
},
},
"with empty request": {
srcQuery: nil,
expErr: status.Error(codes.InvalidArgument, "empty request"),
},
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
got, err := q.AllContractState(sdk.WrapSDKContext(ctx), spec.srcQuery)
require.True(t, spec.expErr.Is(err), err)
if spec.expErr != nil {
require.True(t, errors.Is(err, spec.expErr), "but got %+v", err)
return
}
for _, exp := range spec.expModelContains {
Expand Down Expand Up @@ -143,12 +147,16 @@ func TestQuerySmartContractState(t *testing.T) {
srcQuery: &types.QuerySmartContractStateRequest{Address: RandomBech32AccountAddress(t), QueryData: []byte(`{"verifier":{}}`)},
expErr: types.ErrNotFound,
},
"with empty request": {
srcQuery: nil,
expErr: status.Error(codes.InvalidArgument, "empty request"),
},
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
got, err := q.SmartContractState(sdk.WrapSDKContext(ctx), spec.srcQuery)
require.True(t, errors.Is(err, spec.expErr), "but got %+v", err)
if spec.expErr != nil {
require.True(t, errors.Is(err, spec.expErr), "but got %+v", err)
return
}
assert.JSONEq(t, string(got.Data), spec.expResp)
Expand Down Expand Up @@ -217,7 +225,7 @@ func TestQueryRawContractState(t *testing.T) {
specs := map[string]struct {
srcQuery *types.QueryRawContractStateRequest
expData []byte
expErr *sdkErrors.Error
expErr error
}{
"query raw key": {
srcQuery: &types.QueryRawContractStateRequest{Address: contractAddr, QueryData: []byte("foo")},
Expand All @@ -243,12 +251,16 @@ func TestQueryRawContractState(t *testing.T) {
srcQuery: &types.QueryRawContractStateRequest{Address: RandomBech32AccountAddress(t), QueryData: []byte("foo")},
expErr: types.ErrNotFound,
},
"with empty request": {
srcQuery: nil,
expErr: status.Error(codes.InvalidArgument, "empty request"),
},
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
got, err := q.RawContractState(sdk.WrapSDKContext(ctx), spec.srcQuery)
require.True(t, spec.expErr.Is(err), err)
if spec.expErr != nil {
require.True(t, errors.Is(err, spec.expErr), "but got %+v", err)
return
}
assert.Equal(t, spec.expData, got.Data)
Expand Down Expand Up @@ -323,8 +335,9 @@ func TestQueryContractHistory(t *testing.T) {

specs := map[string]struct {
srcHistory []types.ContractCodeHistoryEntry
req types.QueryContractHistoryRequest
req *types.QueryContractHistoryRequest
expContent []types.ContractCodeHistoryEntry
expErr error
}{
"response with internal fields cleared": {
srcHistory: []types.ContractCodeHistoryEntry{{
Expand All @@ -333,7 +346,7 @@ func TestQueryContractHistory(t *testing.T) {
Updated: types.NewAbsoluteTxPosition(ctx),
Msg: []byte(`"init message"`),
}},
req: types.QueryContractHistoryRequest{Address: myContractBech32Addr},
req: &types.QueryContractHistoryRequest{Address: myContractBech32Addr},
expContent: []types.ContractCodeHistoryEntry{{
Operation: types.ContractCodeHistoryOperationTypeGenesis,
CodeID: firstCodeID,
Expand All @@ -357,7 +370,7 @@ func TestQueryContractHistory(t *testing.T) {
Updated: types.NewAbsoluteTxPosition(ctx),
Msg: []byte(`"migrate message 2"`),
}},
req: types.QueryContractHistoryRequest{Address: myContractBech32Addr},
req: &types.QueryContractHistoryRequest{Address: myContractBech32Addr},
expContent: []types.ContractCodeHistoryEntry{{
Operation: types.ContractCodeHistoryOperationTypeInit,
CodeID: firstCodeID,
Expand All @@ -384,7 +397,7 @@ func TestQueryContractHistory(t *testing.T) {
Updated: types.NewAbsoluteTxPosition(ctx),
Msg: []byte(`"migrate message 1"`),
}},
req: types.QueryContractHistoryRequest{
req: &types.QueryContractHistoryRequest{
Address: myContractBech32Addr,
Pagination: &query.PageRequest{
Offset: 1,
Expand All @@ -408,7 +421,7 @@ func TestQueryContractHistory(t *testing.T) {
Updated: types.NewAbsoluteTxPosition(ctx),
Msg: []byte(`"migrate message 1"`),
}},
req: types.QueryContractHistoryRequest{
req: &types.QueryContractHistoryRequest{
Address: myContractBech32Addr,
Pagination: &query.PageRequest{
Limit: 1,
Expand All @@ -421,14 +434,18 @@ func TestQueryContractHistory(t *testing.T) {
}},
},
"unknown contract address": {
req: types.QueryContractHistoryRequest{Address: otherBech32Addr},
req: &types.QueryContractHistoryRequest{Address: otherBech32Addr},
srcHistory: []types.ContractCodeHistoryEntry{{
Operation: types.ContractCodeHistoryOperationTypeGenesis,
CodeID: firstCodeID,
Updated: types.NewAbsoluteTxPosition(ctx),
Msg: []byte(`"init message"`),
}},
expContent: nil,
expErr: types.ErrEmpty,
},
"with empty request": {
req: nil,
expErr: status.Error(codes.InvalidArgument, "empty request"),
},
}
for msg, spec := range specs {
Expand All @@ -440,11 +457,15 @@ func TestQueryContractHistory(t *testing.T) {

// when
q := Querier(keeper)
got, err := q.ContractHistory(sdk.WrapSDKContext(xCtx), &spec.req)
got, err := q.ContractHistory(sdk.WrapSDKContext(xCtx), spec.req)

// then
if spec.expContent == nil {
require.Error(t, types.ErrEmpty)
if spec.expErr != nil {
if err != nil {
assert.Equal(t, spec.expErr, err)
} else {
require.Error(t, spec.expErr)
}
return
}
require.NoError(t, err)
Expand Down Expand Up @@ -515,21 +536,26 @@ func TestQueryCodeList(t *testing.T) {

specs := map[string]struct {
storedCodeIDs []uint64
req types.QueryCodesRequest
req *types.QueryCodesRequest
expCodeIDs []uint64
expErr error
}{
"none": {},
"none": {
req: &types.QueryCodesRequest{},
},
"no gaps": {
storedCodeIDs: []uint64{1, 2, 3},
req: &types.QueryCodesRequest{},
expCodeIDs: []uint64{1, 2, 3},
},
"with gaps": {
storedCodeIDs: []uint64{2, 4, 6},
req: &types.QueryCodesRequest{},
expCodeIDs: []uint64{2, 4, 6},
},
"with pagination offset": {
storedCodeIDs: []uint64{1, 2, 3},
req: types.QueryCodesRequest{
req: &types.QueryCodesRequest{
Pagination: &query.PageRequest{
Offset: 1,
},
Expand All @@ -538,7 +564,7 @@ func TestQueryCodeList(t *testing.T) {
},
"with pagination limit": {
storedCodeIDs: []uint64{1, 2, 3},
req: types.QueryCodesRequest{
req: &types.QueryCodesRequest{
Pagination: &query.PageRequest{
Limit: 2,
},
Expand All @@ -547,13 +573,17 @@ func TestQueryCodeList(t *testing.T) {
},
"with pagination next key": {
storedCodeIDs: []uint64{1, 2, 3},
req: types.QueryCodesRequest{
req: &types.QueryCodesRequest{
Pagination: &query.PageRequest{
Key: fromBase64("AAAAAAAAAAI="),
},
},
expCodeIDs: []uint64{2, 3},
},
"with empty request": {
req: nil,
expErr: status.Error(codes.InvalidArgument, "empty request"),
},
}

for msg, spec := range specs {
Expand All @@ -568,9 +598,12 @@ func TestQueryCodeList(t *testing.T) {
}
// when
q := Querier(keeper)
got, err := q.Codes(sdk.WrapSDKContext(xCtx), &spec.req)

got, err := q.Codes(sdk.WrapSDKContext(xCtx), spec.req)
// then
if spec.expErr != nil {
require.True(t, errors.Is(err, spec.expErr), "but got %+v", err)
return
}
require.NoError(t, err)
require.NotNil(t, got.CodeInfos)
require.Len(t, got.CodeInfos, len(spec.expCodeIDs))
Expand Down Expand Up @@ -607,7 +640,7 @@ func TestQueryContractInfo(t *testing.T) {
src *types.QueryContractInfoRequest
stored types.ContractInfo
expRsp *types.QueryContractInfoResponse
expErr bool
expErr error
}{
"found": {
src: &types.QueryContractInfoRequest{Address: contractAddr.String()},
Expand All @@ -632,7 +665,11 @@ func TestQueryContractInfo(t *testing.T) {
"not found": {
src: &types.QueryContractInfoRequest{Address: RandomBech32AccountAddress(t)},
stored: types.ContractInfoFixture(),
expErr: true,
expErr: types.ErrNotFound,
},
"with empty request": {
src: nil,
expErr: status.Error(codes.InvalidArgument, "empty request"),
},
}
for name, spec := range specs {
Expand All @@ -641,8 +678,8 @@ func TestQueryContractInfo(t *testing.T) {
k.storeContractInfo(xCtx, contractAddr, &spec.stored)
// when
gotRsp, gotErr := querier.ContractInfo(sdk.WrapSDKContext(xCtx), spec.src)
if spec.expErr {
require.Error(t, gotErr)
if spec.expErr != nil {
require.True(t, errors.Is(gotErr, spec.expErr), "but got %+v", gotErr)
return
}
require.NoError(t, gotErr)
Expand Down

0 comments on commit 1f60af6

Please sign in to comment.