Skip to content

Commit

Permalink
update genesis test paths and remove keeper test utility (2/n)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Nov 22, 2024
1 parent 9f686ea commit 70b48e0
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 121 deletions.
2 changes: 0 additions & 2 deletions ignite/cmd/scaffold_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ This command does the following:
* Creates a directory with module's protocol buffer files in "proto/"
* Creates a directory with module's boilerplate Go code in "x/"
* Imports the newly created module by modifying "app/app.go"
* Creates a file in "testutil/keeper/" that contains logic to create a keeper
for testing purposes
This command will proceed with module scaffolding even if "app/app.go" doesn't
have the required default placeholders. If the placeholders are missing, you
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"

keepertest "<%= ModulePath %>/testutil/keeper"
"<%= ModulePath %>/x/<%= moduleName %>/keeper"
"<%= ModulePath %>/x/<%= moduleName %>/types"
)

func TestMsgServerSend<%= packetName.UpperCamel %>(t *testing.T) {
k, ctx, addressCodec := keepertest.<%= title(moduleName) %>Keeper(t)
<%= MsgSigner.LowerCamel %>, err := addressCodec.BytesToString([]byte("signerAddr__________________"))
f := initFixture(t)
srv := keeper.NewMsgServerImpl(f.keeper)
<%= MsgSigner.LowerCamel %>, err := f.addressCodec.BytesToString([]byte("signerAddr__________________"))
require.NoError(t, err)
srv := keeper.NewMsgServerImpl(k)

tests := []struct {
name string
Expand Down Expand Up @@ -73,7 +72,7 @@ func TestMsgServerSend<%= packetName.UpperCamel %>(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err = srv.Send<%= packetName.UpperCamel %>(ctx, &tt.msg)
_, err = srv.Send<%= packetName.UpperCamel %>(f.ctx, &tt.msg)
if tt.err != nil {
require.ErrorContains(t, err, tt.err.Error())
return
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package <%= moduleName %>_test
package keeper_test

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package keeper
package keeper_test

import (
"testing"

"cosmossdk.io/core/address"
"cosmossdk.io/log"
"cosmossdk.io/store"
"cosmossdk.io/store/metrics"
storetypes "cosmossdk.io/store/types"
dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/cosmos-sdk/codec"
codectestutil "github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -22,33 +20,38 @@ import (
"github.com/stretchr/testify/require"

"<%= modulePath %>/x/<%= moduleName %>/keeper"
module "<%= modulePath %>/x/<%= moduleName %>/module"
"<%= modulePath %>/x/<%= moduleName %>/types"
)

func <%= title(moduleName) %>Keeper(t testing.TB) (keeper.Keeper, sdk.Context, address.Codec) {
type fixture struct {
ctx context.Context
keeper keeper.Keeper
addressCodec address.Codec
}

func initFixture(t *testing.T) *fixture {
t.Helper()

encCfg := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, module.AppModule{})
addressCodec := addresscodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix())
storeKey := storetypes.NewKVStoreKey(types.StoreKey)

db := dbm.NewMemDB()
stateStore := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
require.NoError(t, stateStore.LoadLatestVersion())
env := runtime.NewEnvironment(runtime.NewKVStoreService(storeKey), log.NewTestLogger(t))
ctx := testutil.DefaultContextWithDB(t, storeKey, storetypes.NewTransientStoreKey("transient_test")).Ctx

registry := codectypes.NewInterfaceRegistry()
appCodec := codec.NewProtoCodec(registry)
capabilityKeeper := capabilitykeeper.NewKeeper(appCodec, storeKey, nil)
authority := authtypes.NewModuleAddress(govtypes.ModuleName)
addressCodec := addresscodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix())

capabilityKeeper := capabilitykeeper.NewKeeper(encCfg.Codec, storeKey, nil)
scopedKeeper := capabilityKeeper.ScopeToModule(ibcexported.ModuleName)
portKeeper := portkeeper.NewKeeper(scopedKeeper)
scopeModule := capabilityKeeper.ScopeToModule(types.ModuleName)

k := keeper.NewKeeper(
appCodec,
env,
encCfg.Codec,
addressCodec,
runtime.NewKVStoreService(storeKey),
log.NewNopLogger(),
authority.String(),
authority,
func() *ibckeeper.Keeper {
return &ibckeeper.Keeper{
PortKeeper: &portKeeper,
Expand All @@ -60,12 +63,14 @@ func <%= title(moduleName) %>Keeper(t testing.TB) (keeper.Keeper, sdk.Context, a
nil,<% } %>
)

ctx := sdk.NewContext(stateStore, false, log.NewNopLogger())

// Initialize params
if err := k.Params.Set(ctx, types.DefaultParams()); err != nil {
t.Fatalf("failed to set params: %v", err)
}

return k, ctx, addressCodec

return &fixture{
ctx: ctx,
keeper: k,
addressCodec: addressCodec,
}
}
2 changes: 1 addition & 1 deletion ignite/templates/module/create/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewIBC(replacer placeholder.Replacer, opts *CreateOptions) (*genny.Generato

func genesisModify(replacer placeholder.Replacer, opts *CreateOptions) genny.RunFn {
return func(r *genny.Runner) error {
path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "module/genesis.go")
path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "keeper/genesis.go")
f, err := r.Disk.Find(path)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"<%= ModulePath %>/x/<%= ModuleName %>/keeper"
"<%= ModulePath %>/testutil/nullify"
"<%= ModulePath %>/x/<%= ModuleName %>/keeper"
keepertest "<%= ModulePath %>/testutil/keeper"
)

func createN<%= TypeName.UpperCamel %>(keeper keeper.Keeper, ctx context.Context, n int) []types.<%= TypeName.UpperCamel %> {
Expand All @@ -28,9 +27,9 @@ func createN<%= TypeName.UpperCamel %>(keeper keeper.Keeper, ctx context.Context
}

func Test<%= TypeName.UpperCamel %>QuerySingle(t *testing.T) {
k, ctx, _ := keepertest.<%= title(ModuleName) %>Keeper(t)
qs := keeper.NewQueryServerImpl(k)
msgs := createN<%= TypeName.UpperCamel %>(k, ctx, 2)
f := initFixture(t)
qs := keeper.NewQueryServerImpl(f.keeper)
msgs := createN<%= TypeName.UpperCamel %>(f.keeper, f.ctx, 2)
tests := []struct {
desc string
request *types.QueryGet<%= TypeName.UpperCamel %>Request
Expand Down Expand Up @@ -59,7 +58,7 @@ func Test<%= TypeName.UpperCamel %>QuerySingle(t *testing.T) {
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
response, err := qs.Get<%= TypeName.UpperCamel %>(ctx, tc.request)
response, err := qs.Get<%= TypeName.UpperCamel %>(f.ctx, tc.request)
if tc.err != nil {
require.ErrorIs(t, err, tc.err)
} else {
Expand All @@ -74,9 +73,9 @@ func Test<%= TypeName.UpperCamel %>QuerySingle(t *testing.T) {
}

func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) {
k, ctx, _ := keepertest.<%= title(ModuleName) %>Keeper(t)
qs := keeper.NewQueryServerImpl(k)
msgs := createN<%= TypeName.UpperCamel %>(k, ctx, 5)
f := initFixture(t)
qs := keeper.NewQueryServerImpl(f.keeper)
msgs := createN<%= TypeName.UpperCamel %>(f.keeper, f.ctx, 5)

request := func(next []byte, offset, limit uint64, total bool) *types.QueryAll<%= TypeName.UpperCamel %>Request {
return &types.QueryAll<%= TypeName.UpperCamel %>Request{
Expand All @@ -91,7 +90,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) {
t.Run("ByOffset", func(t *testing.T) {
step := 2
for i := 0; i < len(msgs); i += step {
resp, err := qs.List<%= TypeName.UpperCamel %>(ctx, request(nil, uint64(i), uint64(step), false))
resp, err := qs.List<%= TypeName.UpperCamel %>(f.ctx, request(nil, uint64(i), uint64(step), false))
require.NoError(t, err)
require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step)
require.Subset(t,
Expand All @@ -104,7 +103,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) {
step := 2
var next []byte
for i := 0; i < len(msgs); i += step {
resp, err := qs.List<%= TypeName.UpperCamel %>(ctx, request(next, 0, uint64(step), false))
resp, err := qs.List<%= TypeName.UpperCamel %>(f.ctx, request(next, 0, uint64(step), false))
require.NoError(t, err)
require.LessOrEqual(t, len(resp.<%= TypeName.UpperCamel %>), step)
require.Subset(t,
Expand All @@ -115,7 +114,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) {
}
})
t.Run("Total", func(t *testing.T) {
resp, err := qs.List<%= TypeName.UpperCamel %>(ctx, request(nil, 0, 0, true))
resp, err := qs.List<%= TypeName.UpperCamel %>(f.ctx, request(nil, 0, 0, true))
require.NoError(t, err)
require.Equal(t, len(msgs), int(resp.Pagination.Total))
require.ElementsMatch(t,
Expand All @@ -124,7 +123,7 @@ func Test<%= TypeName.UpperCamel %>QueryPaginated(t *testing.T) {
)
})
t.Run("InvalidRequest", func(t *testing.T) {
_, err := qs.List<%= TypeName.UpperCamel %>(ctx, nil)
_, err := qs.List<%= TypeName.UpperCamel %>(f.ctx, nil)
require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "invalid request"))
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,35 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/stretchr/testify/require"

keepertest "<%= ModulePath %>/testutil/keeper"
"<%= ModulePath %>/x/<%= ModuleName %>/keeper"
"<%= ModulePath %>/x/<%= ModuleName %>/types"
)

func Test<%= TypeName.UpperCamel %>MsgServerCreate(t *testing.T) {
k, ctx, addressCodec := keepertest.<%= title(ModuleName) %>Keeper(t)
srv := keeper.NewMsgServerImpl(k)
f := initFixture(t)
srv := keeper.NewMsgServerImpl(f.keeper)

<%= MsgSigner.LowerCamel %>, err := addressCodec.BytesToString([]byte("signerAddr__________________"))
<%= MsgSigner.LowerCamel %>, err := f.addressCodec.BytesToString([]byte("signerAddr__________________"))
require.NoError(t, err)

for i := 0; i < 5; i++ {
resp, err := srv.Create<%= TypeName.UpperCamel %>(ctx, &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>})
resp, err := srv.Create<%= TypeName.UpperCamel %>(f.ctx, &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>})
require.NoError(t, err)
require.Equal(t, i, int(resp.Id))
}
}

func Test<%= TypeName.UpperCamel %>MsgServerUpdate(t *testing.T) {
k, ctx, addressCodec := keepertest.<%= title(ModuleName) %>Keeper(t)
srv := keeper.NewMsgServerImpl(k)
f := initFixture(t)
srv := keeper.NewMsgServerImpl(f.keeper)

<%= MsgSigner.LowerCamel %>, err := addressCodec.BytesToString([]byte("signerAddr__________________"))
<%= MsgSigner.LowerCamel %>, err := f.addressCodec.BytesToString([]byte("signerAddr__________________"))
require.NoError(t, err)

unauthorizedAddr, err := addressCodec.BytesToString([]byte("unauthorizedAddr___________"))
unauthorizedAddr, err := f.addressCodec.BytesToString([]byte("unauthorizedAddr___________"))
require.NoError(t, err)

_, err = srv.Create<%= TypeName.UpperCamel %>(ctx, &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>})
_, err = srv.Create<%= TypeName.UpperCamel %>(f.ctx, &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>})
require.NoError(t, err)

tests := []struct {
Expand Down Expand Up @@ -66,7 +65,7 @@ func Test<%= TypeName.UpperCamel %>MsgServerUpdate(t *testing.T) {
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
_, err = srv.Update<%= TypeName.UpperCamel %>(ctx, tc.request)
_, err = srv.Update<%= TypeName.UpperCamel %>(f.ctx, tc.request)
if tc.err != nil {
require.ErrorIs(t, err, tc.err)
} else {
Expand All @@ -77,16 +76,16 @@ func Test<%= TypeName.UpperCamel %>MsgServerUpdate(t *testing.T) {
}

func Test<%= TypeName.UpperCamel %>MsgServerDelete(t *testing.T) {
k, ctx, addressCodec := keepertest.<%= title(ModuleName) %>Keeper(t)
srv := keeper.NewMsgServerImpl(k)
f := initFixture(t)
srv := keeper.NewMsgServerImpl(f.keeper)

<%= MsgSigner.LowerCamel %>, err := addressCodec.BytesToString([]byte("signerAddr__________________"))
<%= MsgSigner.LowerCamel %>, err := f.addressCodec.BytesToString([]byte("signerAddr__________________"))
require.NoError(t, err)

unauthorizedAddr, err := addressCodec.BytesToString([]byte("unauthorizedAddr___________"))
unauthorizedAddr, err := f.addressCodec.BytesToString([]byte("unauthorizedAddr___________"))
require.NoError(t, err)

_, err = srv.Create<%= TypeName.UpperCamel %>(ctx, &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>})
_, err = srv.Create<%= TypeName.UpperCamel %>(f.ctx, &types.MsgCreate<%= TypeName.UpperCamel %>{<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>})
require.NoError(t, err)

tests := []struct {
Expand Down Expand Up @@ -116,7 +115,7 @@ func Test<%= TypeName.UpperCamel %>MsgServerDelete(t *testing.T) {
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
_, err = srv.Delete<%= TypeName.UpperCamel %>(ctx, tc.request)
_, err = srv.Delete<%= TypeName.UpperCamel %>(f.ctx, tc.request)
if tc.err != nil {
require.ErrorIs(t, err, tc.err)
} else {
Expand Down
4 changes: 2 additions & 2 deletions ignite/templates/typed/list/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ for _, elem := range gs.%[3]vList {

func genesisModuleModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn {
return func(r *genny.Runner) error {
path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "module/genesis.go")
path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "keeper/genesis.go")
f, err := r.Disk.Find(path)
if err != nil {
return err
Expand Down Expand Up @@ -172,7 +172,7 @@ if err != nil {

func genesisTestsModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn {
return func(r *genny.Runner) error {
path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "module/genesis_test.go")
path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "keeper/genesis_test.go")
f, err := r.Disk.Find(path)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 70b48e0

Please sign in to comment.