Skip to content

Commit

Permalink
feat(testutil): adding DefaultContextWithKeys test helper (#17216)
Browse files Browse the repository at this point in the history
(cherry picked from commit 382de33)
  • Loading branch information
damiannolan authored and mergify[bot] committed Jul 31, 2023
1 parent f41d678 commit f16bd2c
Showing 1 changed file with 30 additions and 0 deletions.
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

0 comments on commit f16bd2c

Please sign in to comment.