Skip to content

Commit

Permalink
fix: test issues
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmic-vagabond committed Sep 22, 2023
1 parent 6c1e6d6 commit d39d859
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 19 deletions.
3 changes: 2 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,9 +865,10 @@ func NewElysApp(
)
marginModule := marginmodule.NewAppModule(appCodec, app.MarginKeeper, app.AccountKeeper, app.BankKeeper)

app.ClockKeeper = clockmodulekeeper.NewKeeper(
app.ClockKeeper = *clockmodulekeeper.NewKeeper(
keys[clockmoduletypes.StoreKey],
appCodec,
app.GetSubspace(clockmoduletypes.ModuleName),
*app.ContractKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
Expand Down
57 changes: 57 additions & 0 deletions testutil/keeper/clock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package keeper

import (
"testing"

wasmmodulekeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
tmdb "github.com/cometbft/cometbft-db"
"github.com/cometbft/cometbft/libs/log"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
typesparams "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/elys-network/elys/x/clock/keeper"
"github.com/elys-network/elys/x/clock/types"
"github.com/stretchr/testify/require"
)

func ClockKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
storeKey := sdk.NewKVStoreKey(types.StoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)

db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db)
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil)
require.NoError(t, stateStore.LoadLatestVersion())

registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)

paramsSubspace := typesparams.NewSubspace(cdc,
types.Amino,
storeKey,
memStoreKey,
"ClockParams",
)

k := keeper.NewKeeper(
storeKey,
cdc,
paramsSubspace,
wasmmodulekeeper.PermissionedKeeper{},
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())

// Initialize params
k.SetParams(ctx, types.DefaultParams())

return k, ctx
}
15 changes: 6 additions & 9 deletions x/clock/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,18 @@ type GenesisTestSuite struct {

ctx sdk.Context

app *app.App
app *app.ElysApp
}

func TestGenesisTestSuite(t *testing.T) {
suite.Run(t, new(GenesisTestSuite))
}

func (suite *GenesisTestSuite) SetupTest() {
app := app.Setup(suite.T())
ctx := app.BaseApp.NewContext(false, tmproto.Header{
suite.app = app.InitElysTestApp(true)
suite.ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{
ChainID: "testing",
})

suite.app = app
suite.ctx = ctx
}

func (suite *GenesisTestSuite) TestClockInitGenesis() {
Expand Down Expand Up @@ -92,14 +89,14 @@ func (suite *GenesisTestSuite) TestClockInitGenesis() {

if tc.expPanic {
suite.Require().Panics(func() {
clock.InitGenesis(suite.ctx, suite.app.AppKeepers.ClockKeeper, tc.genesis)
clock.InitGenesis(suite.ctx, suite.app.ClockKeeper, tc.genesis)
})
} else {
suite.Require().NotPanics(func() {
clock.InitGenesis(suite.ctx, suite.app.AppKeepers.ClockKeeper, tc.genesis)
clock.InitGenesis(suite.ctx, suite.app.ClockKeeper, tc.genesis)
})

params := suite.app.AppKeepers.ClockKeeper.GetParams(suite.ctx)
params := suite.app.ClockKeeper.GetParams(suite.ctx)
suite.Require().Equal(tc.genesis.Params, params)
}
})
Expand Down
13 changes: 9 additions & 4 deletions x/clock/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package keeper

import (
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"

"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"

paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/elys-network/elys/x/clock/types"
)

Expand All @@ -23,10 +22,16 @@ type Keeper struct {
func NewKeeper(
key storetypes.StoreKey,
cdc codec.BinaryCodec,
ps paramtypes.Subspace,
contractKeeper wasmkeeper.PermissionedKeeper,
authority string,
) Keeper {
return Keeper{
) *Keeper {
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
ps = ps.WithKeyTable(types.ParamKeyTable())
}

return &Keeper{
cdc: cdc,
storeKey: key,
contractKeeper: contractKeeper,
Expand Down
3 changes: 1 addition & 2 deletions x/clock/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ type IntegrationTestSuite struct {

func (s *IntegrationTestSuite) SetupTest() {
isCheckTx := false
s.app = app.Setup(s.T())

s.app = app.InitElysTestApp(true)
s.ctx = s.app.BaseApp.NewContext(isCheckTx, tmproto.Header{
ChainID: "testing",
Height: 1,
Expand Down
2 changes: 1 addition & 1 deletion x/clock/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (s *IntegrationTestSuite) TestUpdateClockParams() {
params := types.DefaultParams()
params.ContractAddresses = tc.ContractAddresses

err := s.app.AppKeepers.ClockKeeper.SetParams(s.ctx, params)
err := s.app.ClockKeeper.SetParams(s.ctx, params)

if !tc.success {
s.Require().Error(err)
Expand Down
2 changes: 1 addition & 1 deletion x/clock/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (s *IntegrationTestSuite) TestClockQueryParams() {
tc := tc
s.Run(tc.desc, func() {
// Set the params to what is expected, then query and ensure the query is the same
err := s.app.AppKeepers.ClockKeeper.SetParams(s.ctx, tc.Expected)
err := s.app.ClockKeeper.SetParams(s.ctx, tc.Expected)
s.Require().NoError(err)

// Contracts check
Expand Down
4 changes: 3 additions & 1 deletion x/clock/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
)

var (
amino = codec.NewLegacyAmino()
amino = codec.NewLegacyAmino()
// retro compatibility purpose with the test interface
Amino = amino
AminoCdc = codec.NewAminoCodec(amino)
)

Expand Down
3 changes: 3 additions & 0 deletions x/clock/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ const (

// RouterKey to be used for message routing
RouterKey = ModuleName

// MemStoreKey defines the in-memory store key
MemStoreKey = "mem_clock"
)

0 comments on commit d39d859

Please sign in to comment.