Skip to content

Commit

Permalink
test(logic): increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Oct 4, 2024
1 parent 8e03276 commit bfe7d11
Show file tree
Hide file tree
Showing 6 changed files with 487 additions and 103 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,9 @@ mock: ## Generate all the mocks (for tests)
@mockgen -destination x/logic/testutil/gas_mocks.go -package testutil cosmossdk.io/store/types GasMeter
@mockgen -destination x/logic/testutil/fs_mocks.go -package testutil io/fs FS
@mockgen -destination x/logic/testutil/read_file_fs_mocks.go -package testutil io/fs ReadFileFS
@mockgen -source "$$(go list -f '{{.Dir}}' github.com/cosmos/cosmos-sdk/codec/types)/interface_registry.go" \
-package testutil \
-destination x/logic/testutil/interface_registry_mocks.go

## Release:
.PHONY: release-assets
Expand Down
124 changes: 22 additions & 102 deletions x/logic/predicate/bank_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package predicate
import (
"context"
"fmt"
"strconv"
"strings"
"testing"

Expand All @@ -26,10 +25,8 @@ import (
"cosmossdk.io/x/evidence"

codecaddress "github.com/cosmos/cosmos-sdk/codec/address"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
query "github.com/cosmos/cosmos-sdk/types/query"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bank "github.com/cosmos/cosmos-sdk/x/bank/types"

Expand Down Expand Up @@ -489,53 +486,7 @@ func TestBank(t *testing.T) {

return authtypes.NewBaseAccountWithAddress(accAddr)
})
authQueryServiceKeeper.
EXPECT().
Accounts(gomock.Any(), gomock.Any()).
AnyTimes().
DoAndReturn(func(_ context.Context, req *authtypes.QueryAccountsRequest) (*authtypes.QueryAccountsResponse, error) {
start := 0
limit := 5
toCursor := func(idx int) []byte { return []byte(fmt.Sprintf("%d", idx)) }
fromCursor := func(k []byte) int {
idx, err := strconv.Atoi(string(k))
c.So(err, ShouldBeNil)

return idx
}

if req.Pagination != nil {
if req.Pagination.Key != nil {
start = fromCursor(req.Pagination.Key)
}
if req.Pagination.Limit != 0 {
limit = int(req.Pagination.GetLimit())
}
}
accounts := lo.Map(
lo.Slice(addresses, start, start+limit),
func(acc string, _ int) *codectypes.Any {
addr, err := sdk.AccAddressFromBech32(acc)
c.So(err, ShouldBeNil)

accI := authtypes.ProtoBaseAccount()
err = accI.SetAddress(addr)
c.So(err, ShouldBeNil)

anyV, err := codectypes.NewAnyWithValue(accI)
c.So(err, ShouldBeNil)

return anyV
})

return &authtypes.QueryAccountsResponse{
Accounts: accounts,
Pagination: &query.PageResponse{
NextKey: toCursor(start + 1),
Total: 0,
},
}, nil
})
testutil.MockAuthQueryServiceWithAddresses(authQueryServiceKeeper, addresses)
for _, balance := range tc.balances {
bankKeeper.
EXPECT().
Expand Down Expand Up @@ -750,7 +701,22 @@ func TestAccount(t *testing.T) {
Variables: []string{},
Results: []types.Result{
{
Error: "error(resource_error(resource_context),account/1)",
Error: "error(resource_error(resource_context(authKeeper)),account/1)",
Substitutions: nil,
},
},
},
},
{
ctx: context.WithValue(context.Background(), types.AuthKeeperContextKey, testutil.NewMockAccountKeeper(ctrl)),
addresses: []string{},
query: `account(dont_care).`,
wantAnswer: &types.Answer{
HasMore: false,
Variables: []string{},
Results: []types.Result{
{
Error: "error(resource_error(resource_context(authQueryService)),account/1)",
Substitutions: nil,
},
},
Expand Down Expand Up @@ -812,57 +778,11 @@ func TestAccount(t *testing.T) {
return authtypes.NewBaseAccountWithAddress(accAddr)
})

authQueryServiceKeeper.
EXPECT().
Accounts(gomock.Any(), gomock.Any()).
AnyTimes().
DoAndReturn(func(_ context.Context, req *authtypes.QueryAccountsRequest) (*authtypes.QueryAccountsResponse, error) {
if tc.authQueryServiceKeeperError {
return nil, status.Error(codes.PermissionDenied, "not allowed")
}

start := 0
limit := 5
toCursor := func(idx int) []byte { return []byte(fmt.Sprintf("%d", idx)) }
fromCursor := func(k []byte) int {
idx, err := strconv.Atoi(string(k))
c.So(err, ShouldBeNil)

return idx
}

if req.Pagination != nil {
if req.Pagination.Key != nil {
start = fromCursor(req.Pagination.Key)
}
if req.Pagination.Limit != 0 {
limit = int(req.Pagination.GetLimit())
}
}
accounts := lo.Map(
lo.Slice(tc.addresses, start, start+limit),
func(acc string, _ int) *codectypes.Any {
addr, err := sdk.AccAddressFromBech32(acc)
c.So(err, ShouldBeNil)

accI := authtypes.ProtoBaseAccount()
err = accI.SetAddress(addr)
c.So(err, ShouldBeNil)

anyV, err := codectypes.NewAnyWithValue(accI)
c.So(err, ShouldBeNil)

return anyV
})

return &authtypes.QueryAccountsResponse{
Accounts: accounts,
Pagination: &query.PageResponse{
NextKey: toCursor(start + 1),
Total: 0,
},
}, nil
})
if tc.authQueryServiceKeeperError {
testutil.MockAuthQueryServiceWithError(authQueryServiceKeeper, status.Error(codes.PermissionDenied, "not allowed"))
} else {
testutil.MockAuthQueryServiceWithAddresses(authQueryServiceKeeper, tc.addresses)
}

Convey("and a vm with the account predicate registered", func() {
interpreter := testutil.NewLightInterpreterMust(ctx)
Expand Down
2 changes: 1 addition & 1 deletion x/logic/predicate/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func Accounts(ctx context.Context, authQueryService types.AuthQueryService) func

key = res.Pagination.NextKey

interfaceRegistry := ctx.Value(types.InterfaceRegistryContextKey).(cdctypes.InterfaceRegistry)
interfaceRegistry := ctx.Value(types.InterfaceRegistryContextKey).(cdctypes.AnyUnpacker)
var account sdk.AccountI
if err := interfaceRegistry.UnpackAny(res.Accounts[0], &account); err != nil {
return lo.Tuple2[sdk.AccountI, error]{A: nil, B: err}, true
Expand Down
93 changes: 93 additions & 0 deletions x/logic/predicate/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package predicate

import (
"errors"
"testing"

dbm "github.com/cosmos/cosmos-db"
"github.com/golang/mock/gomock"

. "github.com/smartystreets/goconvey/convey"

tmproto "github.com/cometbft/cometbft/proto/tendermint/types"

"cosmossdk.io/log"
"cosmossdk.io/store"
"cosmossdk.io/store/metrics"
"cosmossdk.io/x/evidence"

sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"

"github.com/axone-protocol/axoned/v10/x/logic/testutil"
"github.com/axone-protocol/axoned/v10/x/logic/types"
)

func TestAccounts(t *testing.T) {
Convey("Given a mocked context and authQueryService", t, func() {
ctrl := gomock.NewController(t)
sdk.GetConfig().SetBech32PrefixForAccount("axone", "axonepub")
addresses := []string{
"axone1ffd5wx65l407yvm478cxzlgygw07h79sw4jwpa",
"axone1wze8mn5nsgl9qrgazq6a92fvh7m5e6ps372aep",
}
authQueryServiceKeeper := testutil.NewMockAuthQueryService(ctrl)
testutil.MockAuthQueryServiceWithAddresses(authQueryServiceKeeper, addresses)
encCfg := moduletestutil.MakeTestEncodingConfig(evidence.AppModuleBasic{})

db := dbm.NewMemDB()
stateStore := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())

Convey("Given a mocked context", func() {
ctx := sdk.
NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger()).
WithValue(types.AuthQueryServiceContextKey, authQueryServiceKeeper).
WithValue(types.InterfaceRegistryContextKey, encCfg.InterfaceRegistry)

Convey("When Accounts is called", func() {
next := Accounts(ctx, authQueryServiceKeeper)

Convey("Then next() returns all the addresses", func() {
for i := 0; i < 2; i++ {
result, ok := next()
So(ok, ShouldBeTrue)
So(result.A.GetAddress().String(), ShouldEqual, addresses[i])
So(result.B, ShouldBeNil)
}
for i := 0; i < 2; i++ {
result, ok := next()
So(ok, ShouldBeFalse)
So(result.A, ShouldBeNil)
So(result.B, ShouldBeNil)
}
})
})
})

Convey("Given a badly mocked context", func() {
interfaceRegistry := testutil.NewMockInterfaceRegistry(ctrl)

ctx := sdk.
NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger()).
WithValue(types.AuthQueryServiceContextKey, authQueryServiceKeeper).
WithValue(types.InterfaceRegistryContextKey, interfaceRegistry)

interfaceRegistry.
EXPECT().
UnpackAny(gomock.Any(), gomock.Any()).
AnyTimes().
Return(errors.New("I was told not to unpack anything"))

Convey("When Accounts is called", func() {
next := Accounts(ctx, authQueryServiceKeeper)

Convey("Then next() returns an error", func() {
result, ok := next()
So(ok, ShouldBeTrue)
So(result.A, ShouldBeNil)
So(result.B, ShouldBeError, errors.New("I was told not to unpack anything"))
})
})
})
})
}
Loading

0 comments on commit bfe7d11

Please sign in to comment.