Skip to content

Commit

Permalink
Add tests for wrap store loader.
Browse files Browse the repository at this point in the history
  • Loading branch information
Taztingo committed Jan 5, 2024
1 parent b4be10d commit 2ae72bf
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 2 deletions.
Empty file added app/.db/000001.log
Empty file.
1 change: 1 addition & 0 deletions app/.db/CURRENT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MANIFEST-000000
Empty file added app/.db/LOCK
Empty file.
6 changes: 6 additions & 0 deletions app/.db/LOG
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
=============== Jan 5, 2024 (EST) ===============
10:42:43.970756 log@legend F·NumFile S·FileSize N·Entry C·BadEntry B·BadBlock Ke·KeyError D·DroppedEntry L·Level Q·SeqNum T·TimeElapsed
10:42:44.004968 db@open opening
10:42:44.005399 version@stat F·[] S·0B[] Sc·[]
10:42:44.010304 db@janitor F·2 G·0
10:42:44.010345 db@open done T·5.349541ms
Binary file added app/.db/MANIFEST-000000
Binary file not shown.
10 changes: 9 additions & 1 deletion app/store_loader.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"errors"
"fmt"
"strconv"
"time"
Expand All @@ -13,12 +14,19 @@ import (
dbm "github.com/tendermint/tm-db"
)

type StoreLoaderWrapper func(sdk.CommitMultiStore, baseapp.StoreLoader) error

// WrapStoreLoader creates a new StoreLoader by wrapping an existing one.
func WrapStoreLoader(wrapper func(sdk.CommitMultiStore, baseapp.StoreLoader) error, storeLoader baseapp.StoreLoader) baseapp.StoreLoader {
func WrapStoreLoader(wrapper StoreLoaderWrapper, storeLoader baseapp.StoreLoader) baseapp.StoreLoader {
return func(ms sdk.CommitMultiStore) error {
if storeLoader == nil {
storeLoader = baseapp.DefaultStoreLoader
}

if wrapper == nil {
return errors.New("wrapper must not be nil")
}

return wrapper(ms, storeLoader)
}
}
Expand Down
73 changes: 72 additions & 1 deletion app/store_loader_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,82 @@
package app

import "testing"
import (
"testing"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/store/rootmulti"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
dbm "github.com/tendermint/tm-db"
)

func TestWrapStoreLoader(t *testing.T) {
var flag bool
tests := []struct {
name string
storeLoader baseapp.StoreLoader
wrapper StoreLoaderWrapper
err string
}{
{
name: "nil store loader is set with valid value",
storeLoader: nil,
wrapper: createMockStoreWrapper(&flag),
},
{
name: "nil wrapper is handled",
storeLoader: createMockStoreLoader(),
wrapper: nil,
err: "wrapper must not be nil",
},
{
name: "contents of wrapper are called",
storeLoader: createMockStoreLoader(),
wrapper: createMockFlipWrapper(&flag),
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
storeLoader := WrapStoreLoader(tc.wrapper, tc.storeLoader)
db := dbm.GoLevelDB{}
ms := rootmulti.NewStore(&db, nil)
assert.NotNil(t, ms, "should create a new multistore for testing")
flag = false

err := storeLoader(ms)
if len(tc.err) > 0 {
assert.EqualError(t, err, tc.err, "should have correct error")
assert.False(t, flag, "wrapper should not be executed")
} else {
assert.NoError(t, err, "should not return an error on success")
assert.True(t, flag, "wrapper should execute and have correct logic")
}

})
}
}

func TestPruningWrapper(t *testing.T) {

}

func createMockStoreLoader() baseapp.StoreLoader {
return func(ms sdk.CommitMultiStore) error {
return nil
}
}

func createMockFlipWrapper(flag *bool) StoreLoaderWrapper {
return func(cms sdk.CommitMultiStore, sl baseapp.StoreLoader) error {
*flag = !(*flag)
return nil
}
}

func createMockStoreWrapper(flag *bool) StoreLoaderWrapper {
return func(cms sdk.CommitMultiStore, sl baseapp.StoreLoader) error {
*flag = sl != nil
return nil
}
}

0 comments on commit 2ae72bf

Please sign in to comment.