Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
30 changes: 30 additions & 0 deletions testutil/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading