Skip to content

Commit

Permalink
Make osmoutils store + module account tests not import osmosis (#3775) (
Browse files Browse the repository at this point in the history
#3789)

* Satisfy store_helper code

* Make osmoutils tests not integrated

(cherry picked from commit 793bb50)

Co-authored-by: Dev Ojha <[email protected]>
  • Loading branch information
mergify[bot] and ValarDragon authored Dec 19, 2022
1 parent 7dd1747 commit 502435e
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 40 deletions.
5 changes: 2 additions & 3 deletions osmoutils/module_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@ func (s *TestSuite) TestCreateModuleAccount() {
s.Run(name, func() {
s.SetupTest()
for _, priorAcc := range tc.priorAccounts {
s.App.AccountKeeper.SetAccount(s.Ctx, priorAcc)
s.accountKeeper.SetAccount(s.ctx, priorAcc)
}
err := osmoutils.CreateModuleAccount(s.Ctx, s.App.AccountKeeper, tc.moduleAccAddr)
err := osmoutils.CreateModuleAccount(s.ctx, s.accountKeeper, tc.moduleAccAddr)
osmoassert.ConditionalError(s.T(), tc.expErr, err)
})
}

}
45 changes: 45 additions & 0 deletions osmoutils/noapptest/cdc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package noapptest

// NOTE: This file is pulled from the SDK:
// https://github.com/cosmos/cosmos-sdk/blob/2eb51447494163bc9beef1b2cc8aa91c3691b2a7/types/module/testutil/codec.go#L23
import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/std"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
)

// TestEncodingConfig defines an encoding configuration that is used for testing
// purposes. Note, MakeTestEncodingConfig takes a series of AppModuleBasic types
// which should only contain the relevant module being tested and any potential
// dependencies.
type TestEncodingConfig struct {
InterfaceRegistry types.InterfaceRegistry
Codec codec.Codec
TxConfig client.TxConfig
Amino *codec.LegacyAmino
}

func MakeTestEncodingConfig(modules ...module.AppModuleBasic) TestEncodingConfig {
cdc := codec.NewLegacyAmino()
interfaceRegistry := types.NewInterfaceRegistry()
codec := codec.NewProtoCodec(interfaceRegistry)

encCfg := TestEncodingConfig{
InterfaceRegistry: interfaceRegistry,
Codec: codec,
TxConfig: tx.NewTxConfig(codec, tx.DefaultSignModes),
Amino: cdc,
}

mb := module.NewBasicManager(modules...)

std.RegisterLegacyAminoCodec(encCfg.Amino)
std.RegisterInterfaces(encCfg.InterfaceRegistry)
mb.RegisterLegacyAminoCodec(encCfg.Amino)
mb.RegisterInterfaces(encCfg.InterfaceRegistry)

return encCfg
}
32 changes: 32 additions & 0 deletions osmoutils/noapptest/ctx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package noapptest

import (
"time"

"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

dbm "github.com/tendermint/tm-db"
)

func CtxWithStoreKeys(keys []sdk.StoreKey, header tmproto.Header, isCheckTx bool) sdk.Context {
db := dbm.NewMemDB()
logger := log.NewNopLogger()
cms := store.NewCommitMultiStore(db, logger)
for _, key := range keys {
cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, nil)
}
err := cms.LoadLatestVersion()
if err != nil {
panic(err)
}
return sdk.NewContext(cms, header, isCheckTx, logger)
}

func DefaultCtxWithStoreKeys(storeKeys []sdk.StoreKey) sdk.Context {
header := tmproto.Header{Height: 1, ChainID: "osmoutils-test-1", Time: time.Now().UTC()}
deliverTx := false
return CtxWithStoreKeys(storeKeys, header, deliverTx)
}
90 changes: 53 additions & 37 deletions osmoutils/store_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,59 @@ import (
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/suite"

"github.com/osmosis-labs/osmosis/v13/app/apptesting"
"github.com/cosmos/cosmos-sdk/x/auth"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/params"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"

"github.com/osmosis-labs/osmosis/v13/app/apptesting/osmoassert"
"github.com/osmosis-labs/osmosis/v13/osmoutils"
"github.com/osmosis-labs/osmosis/v13/osmoutils/noapptest"
twaptypes "github.com/osmosis-labs/osmosis/v13/x/twap/types"
)

// We need to setup a test suite with account keeper
// and a custom store setup.
// unfortunately setting up account implies setting up params
type TestSuite struct {
apptesting.KeeperTestHelper
suite.Suite

ctx sdk.Context
store sdk.KVStore

authStoreKey sdk.StoreKey
accountKeeper authkeeper.AccountKeeperI
}

func (suite *TestSuite) SetupTest() {
suite.Setup()

// For the test suite, we manually wire a custom store "customStoreKey"
// Auth module (for module_account_test.go) which requires params module as well.
customStoreKey := sdk.NewKVStoreKey("osmoutil_store_test")
suite.authStoreKey = sdk.NewKVStoreKey(authtypes.StoreKey)
// setup ctx + stores
paramsKey := sdk.NewKVStoreKey(paramstypes.StoreKey)
paramsTKey := sdk.NewKVStoreKey(paramstypes.TStoreKey)
suite.ctx = noapptest.DefaultCtxWithStoreKeys(
[]sdk.StoreKey{customStoreKey, suite.authStoreKey, paramsKey, paramsTKey})
suite.store = suite.ctx.KVStore(customStoreKey)
// setup params (needed for auth)
encConfig := noapptest.MakeTestEncodingConfig(auth.AppModuleBasic{}, params.AppModuleBasic{})
paramsKeeper := paramskeeper.NewKeeper(encConfig.Codec, encConfig.Amino, paramsKey, paramsTKey)
paramsKeeper.Subspace(authtypes.ModuleName)

// setup auth
maccPerms := map[string][]string{
"fee_collector": nil,
"mint": {"minter"},
}
authsubspace, _ := paramsKeeper.GetSubspace(authtypes.ModuleName)
suite.accountKeeper = authkeeper.NewAccountKeeper(
encConfig.Codec,
suite.authStoreKey,
authsubspace,
authtypes.ProtoBaseAccount, maccPerms)
}

const (
Expand Down Expand Up @@ -52,15 +91,6 @@ func TestOsmoUtilsTestSuite(t *testing.T) {
suite.Run(t, new(TestSuite))
}

func (s *TestSuite) SetupStoreWithBasePrefix() {
_, ms := s.CreateTestContextWithMultiStore()
prefix := sdk.NewKVStoreKey(basePrefix)
ms.MountStoreWithDB(prefix, sdk.StoreTypeIAVL, nil)
err := ms.LoadLatestVersion()
s.Require().NoError(err)
s.store = ms.GetKVStore(prefix)
}

func mockParseValue(b []byte) (string, error) {
return string(b), nil
}
Expand Down Expand Up @@ -95,8 +125,7 @@ func (s *TestSuite) TestGatherAllKeysFromStore() {

for name, tc := range testcases {
s.Run(name, func() {
s.SetupStoreWithBasePrefix()

s.SetupTest()
for i, key := range tc.preSetKeys {
s.store.Set([]byte(key), []byte(fmt.Sprintf("%v", i)))
}
Expand Down Expand Up @@ -207,7 +236,7 @@ func (s *TestSuite) TestGatherValuesFromStore() {

for name, tc := range testcases {
s.Run(name, func() {
s.SetupStoreWithBasePrefix()
s.SetupTest()

for i, key := range tc.preSetKeys {
s.store.Set([]byte(key), []byte(fmt.Sprintf("%v", i)))
Expand Down Expand Up @@ -298,8 +327,7 @@ func (s *TestSuite) TestGatherValuesFromStorePrefix() {

for name, tc := range testcases {
s.Run(name, func() {
s.SetupStoreWithBasePrefix()

s.SetupTest()
for i, key := range tc.preSetKeys {
s.store.Set([]byte(key), []byte(fmt.Sprintf("%v", i)))
}
Expand Down Expand Up @@ -402,8 +430,7 @@ func (s *TestSuite) TestGetFirstValueAfterPrefixInclusive() {

for name, tc := range testcases {
s.Run(name, func() {
s.SetupStoreWithBasePrefix()

s.SetupTest()
for i, key := range tc.preSetKeys {
s.store.Set([]byte(key), []byte(fmt.Sprintf("%v", i)))
}
Expand Down Expand Up @@ -503,8 +530,7 @@ func (s *TestSuite) TestGatherValuesFromIterator() {

for name, tc := range testcases {
s.Run(name, func() {
s.SetupStoreWithBasePrefix()

s.SetupTest()
var iterator sdk.Iterator

for i, key := range tc.preSetKeys {
Expand Down Expand Up @@ -638,7 +664,7 @@ func (s *TestSuite) TestGetIterValuesWithStop() {

for name, tc := range testcases {
s.Run(name, func() {
s.SetupStoreWithBasePrefix()
s.SetupTest()

for i, key := range tc.preSetKeys {
s.store.Set([]byte(key), []byte(fmt.Sprintf("%v", i)))
Expand Down Expand Up @@ -706,8 +732,7 @@ func (s *TestSuite) TestGetValuesUntilDerivedStop() {

for name, tc := range testcases {
s.Run(name, func() {
s.SetupStoreWithBasePrefix()

s.SetupTest()
for i, key := range tc.preSetKeys {
s.store.Set([]byte(key), []byte(fmt.Sprintf("%v", i)))
}
Expand Down Expand Up @@ -792,8 +817,7 @@ func (s *TestSuite) TestMustGet() {

for name, tc := range tests {
s.Run(name, func() {
s.SetupStoreWithBasePrefix()

s.SetupTest()
// Setup
for key, value := range tc.preSetKeyValues {
osmoutils.MustSet(s.store, []byte(key), value)
Expand Down Expand Up @@ -879,8 +903,7 @@ func (s *TestSuite) TestGet() {

for name, tc := range tests {
s.Run(name, func() {
s.SetupStoreWithBasePrefix()

s.SetupTest()
// Setup
for key, value := range tc.preSetKeyValues {
osmoutils.MustSet(s.store, []byte(key), value)
Expand Down Expand Up @@ -946,9 +969,6 @@ func (s *TestSuite) TestMustSet() {

for name, tc := range tests {
s.Run(name, func() {
s.SetupStoreWithBasePrefix()

// Setup
osmoassert.ConditionalPanic(s.T(), tc.expectPanic, func() {
osmoutils.MustSet(s.store, []byte(tc.setKey), tc.setValue)
})
Expand Down Expand Up @@ -1005,8 +1025,7 @@ func (s *TestSuite) TestMustGetDec() {

for name, tc := range tests {
s.Run(name, func() {
s.SetupStoreWithBasePrefix()

s.SetupTest()
// Setup
for key, value := range tc.preSetKeyValues {
osmoutils.MustSetDec(s.store, []byte(key), value)
Expand All @@ -1032,9 +1051,6 @@ func (s *TestSuite) TestMustGetDec() {
// only panic if the proto argument is invalid.
// Therefore, we only test a success case here.
func (s *TestSuite) TestMustSetDec() {
// Setup.
s.SetupStoreWithBasePrefix()

originalDecValue := sdk.OneDec()

// System under test.
Expand Down

0 comments on commit 502435e

Please sign in to comment.