diff --git a/osmoutils/module_account_test.go b/osmoutils/module_account_test.go index 5cff725bcfb..1166f0b0a2b 100644 --- a/osmoutils/module_account_test.go +++ b/osmoutils/module_account_test.go @@ -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) }) } - } diff --git a/osmoutils/noapptest/cdc.go b/osmoutils/noapptest/cdc.go new file mode 100644 index 00000000000..8f9a25905b1 --- /dev/null +++ b/osmoutils/noapptest/cdc.go @@ -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 +} diff --git a/osmoutils/noapptest/ctx.go b/osmoutils/noapptest/ctx.go new file mode 100644 index 00000000000..834cf6797df --- /dev/null +++ b/osmoutils/noapptest/ctx.go @@ -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) +} diff --git a/osmoutils/store_helper_test.go b/osmoutils/store_helper_test.go index 33359ba7cdd..f059576767a 100644 --- a/osmoutils/store_helper_test.go +++ b/osmoutils/store_helper_test.go @@ -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 ( @@ -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 } @@ -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))) } @@ -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))) @@ -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))) } @@ -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))) } @@ -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 { @@ -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))) @@ -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))) } @@ -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) @@ -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) @@ -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) }) @@ -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) @@ -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.