feat(testutil): adding DefaultContextWithKeys test helper (backport #17216) (#17217)

Co-authored-by: Damian Nolan <damiannolan@gmail.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
This commit is contained in:
mergify[bot] 2023-07-31 12:47:50 +00:00 committed by GitHub
parent 75cad9d952
commit fac64cd5d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View File

@ -44,6 +44,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Improvements
* (testutil) [#17216](https://github.com/cosmos/cosmos-sdk/issues/17216) Add `DefaultContextWithKeys` to `testutil` package.
* (x/group, x/gov) [#17109](https://github.com/cosmos/cosmos-sdk/pull/17109) Let proposal summary be 40x longer than metadata limit.
* (version) [#17096](https://github.com/cosmos/cosmos-sdk/pull/17096) Improve `getSDKVersion()` to handle module replacements.

View File

@ -31,6 +31,36 @@ func DefaultContext(key, tkey storetypes.StoreKey) sdk.Context {
return ctx
}
// DefaultContextWithKeys creates a sdk.Context with a fresh MemDB, mounting the providing keys for usage in the multistore.
// This function is intended to be used for testing purposes only.
func DefaultContextWithKeys(
keys map[string]*storetypes.KVStoreKey,
transKeys map[string]*storetypes.TransientStoreKey,
memKeys map[string]*storetypes.MemoryStoreKey,
) sdk.Context {
db := dbm.NewMemDB()
cms := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
for _, key := range keys {
cms.MountStoreWithDB(key, storetypes.StoreTypeIAVL, db)
}
for _, tKey := range transKeys {
cms.MountStoreWithDB(tKey, storetypes.StoreTypeTransient, db)
}
for _, memkey := range memKeys {
cms.MountStoreWithDB(memkey, storetypes.StoreTypeMemory, db)
}
err := cms.LoadLatestVersion()
if err != nil {
panic(err)
}
return sdk.NewContext(cms, cmtproto.Header{}, false, log.NewNopLogger())
}
type TestContext struct {
Ctx sdk.Context
DB *dbm.MemDB