Skip to content

Commit

Permalink
Refactor Fixture (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronc committed Jan 28, 2021
1 parent 04fdbcb commit c19d8b0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,4 @@ build/
.DS_Store

/protoc-gen-go-cosmos2/protoc-gen-go-cosmos2
/tools-stamp
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfc
github.com/cosmos/cosmos-sdk v0.40.0-rc5/go.mod h1:+oR3VJg5puOc6IDdw5MIKdhjXW+HgBzd5zTfGMjDmNQ=
github.com/cosmos/cosmos-sdk v0.40.0 h1:nMPdGr5lly9HOENghgr5IRx2QDieqAncHKCSsCdEJSU=
github.com/cosmos/cosmos-sdk v0.40.0/go.mod h1:MMEVnorMyci71WQopgvFmfz2SrDCdWgq5lBjpVclFrg=
github.com/cosmos/cosmos-sdk v0.41.0 h1:U614TXkI/+T8HY6V9krC0/mKaDm6qSs9EMFSIKuomCo=
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d h1:49RLWk1j44Xu4fjHb6JFYmeUnDORVwHNkDxaQ0ctCVU=
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d/go.mod h1:tSxLoYXyBmiFeKpvmq4dzayMdCjCnu8uqmCysIGBT2Y=
github.com/cosmos/go-bip39 v1.0.0 h1:pcomnQdrdH22njcAatO0yWojsUnCO3y2tNoV1cb6hHY=
Expand Down
9 changes: 4 additions & 5 deletions testutil/server/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ package server
import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"

sdk "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/grpc"
)

// FixtureFactory defines an interface for creating server test fixtures
type FixtureFactory interface {

// Setup runs necessary fixture setup and returns a fresh Fixture environment.
Setup() Fixture
Setup(setupHooks ...func(cdc *codec.ProtoCodec, app *baseapp.BaseApp)) Fixture
}

// Fixture defines an interface for interacting with app services in tests
Expand All @@ -44,6 +45,4 @@ type Fixture interface {

// Teardown performs any teardown actions for the fixture.
Teardown()

BankKeeper() bankkeeper.BaseKeeper
}
63 changes: 13 additions & 50 deletions types/module/server/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ import (
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/bank"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
Expand Down Expand Up @@ -52,65 +46,38 @@ func makeTestAddresses(count int) []sdk.AccAddress {
return addrs
}

func (ff fixtureFactory) Setup() server.Fixture {
func (ff fixtureFactory) Setup(setupHooks ...func(cdc *codec.ProtoCodec, app *baseapp.BaseApp)) server.Fixture {
registry := types.NewInterfaceRegistry()
baseApp := baseapp.NewBaseApp("test", log.NewNopLogger(), dbm.NewMemDB(), nil)
baseApp.MsgServiceRouter().SetInterfaceRegistry(registry)
baseApp.GRPCQueryRouter().SetInterfaceRegistry(registry)

cdc := codec.NewProtoCodec(registry)

for _, hook := range setupHooks {
hook(cdc, baseApp)
}

mm := NewManager(baseApp, cdc)
err := mm.RegisterModules(ff.modules)
require.NoError(ff.t, err)
err = mm.CompleteInitialization()
require.NoError(ff.t, err)

// Setting up bank keeper to use with group module tests
// TODO: remove once #225 addressed
banktypes.RegisterInterfaces(registry)
authtypes.RegisterInterfaces(registry)

paramsKey := sdk.NewKVStoreKey(paramstypes.StoreKey)
authKey := sdk.NewKVStoreKey(authtypes.StoreKey)
bankKey := sdk.NewKVStoreKey(banktypes.StoreKey)
tkey := sdk.NewTransientStoreKey(paramstypes.TStoreKey)
amino := codec.NewLegacyAmino()

authSubspace := paramstypes.NewSubspace(mm.cdc, amino, paramsKey, tkey, authtypes.ModuleName)
bankSubspace := paramstypes.NewSubspace(mm.cdc, amino, paramsKey, tkey, banktypes.ModuleName)

accountKeeper := authkeeper.NewAccountKeeper(
mm.cdc, authKey, authSubspace, authtypes.ProtoBaseAccount, map[string][]string{},
)
bankKeeper := bankkeeper.NewBaseKeeper(
mm.cdc, bankKey, accountKeeper, bankSubspace, map[string]bool{},
)

baseApp.Router().AddRoute(sdk.NewRoute(banktypes.ModuleName, bank.NewHandler(bankKeeper)))
baseApp.MountStore(tkey, sdk.StoreTypeTransient)
baseApp.MountStore(paramsKey, sdk.StoreTypeIAVL)
baseApp.MountStore(authKey, sdk.StoreTypeIAVL)
baseApp.MountStore(bankKey, sdk.StoreTypeIAVL)

err = baseApp.LoadLatestVersion()
require.NoError(ff.t, err)

return fixture{
baseApp: baseApp,
router: mm.router,
t: ff.t,
signers: ff.signers,
bankKeeper: bankKeeper,
baseApp: baseApp,
router: mm.router,
t: ff.t,
signers: ff.signers,
}
}

type fixture struct {
baseApp *baseapp.BaseApp
router *router
t *testing.T
signers []sdk.AccAddress
bankKeeper bankkeeper.BaseKeeper
baseApp *baseapp.BaseApp
router *router
t *testing.T
signers []sdk.AccAddress
}

func (f fixture) Context() context.Context {
Expand All @@ -129,10 +96,6 @@ func (f fixture) Signers() []sdk.AccAddress {
return f.signers
}

func (f fixture) BankKeeper() bankkeeper.BaseKeeper {
return f.bankKeeper
}

func (f fixture) Teardown() {}

type testKey struct {
Expand Down
35 changes: 33 additions & 2 deletions x/group/server/testsuite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
"sort"
"time"

"github.com/cosmos/cosmos-sdk/codec"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"

gogotypes "github.com/gogo/protobuf/types"
"github.com/stretchr/testify/suite"

Expand Down Expand Up @@ -54,9 +59,35 @@ func NewIntegrationTestSuite(
}

func (s *IntegrationTestSuite) SetupSuite() {
s.fixture = s.fixtureFactory.Setup()
s.fixture = s.fixtureFactory.Setup(func(cdc *codec.ProtoCodec, baseApp *baseapp.BaseApp) {
// Setting up bank keeper
banktypes.RegisterInterfaces(cdc.InterfaceRegistry())
authtypes.RegisterInterfaces(cdc.InterfaceRegistry())

paramsKey := sdk.NewKVStoreKey(paramstypes.StoreKey)
authKey := sdk.NewKVStoreKey(authtypes.StoreKey)
bankKey := sdk.NewKVStoreKey(banktypes.StoreKey)
tkey := sdk.NewTransientStoreKey(paramstypes.TStoreKey)
amino := codec.NewLegacyAmino()

authSubspace := paramstypes.NewSubspace(cdc, amino, paramsKey, tkey, authtypes.ModuleName)
bankSubspace := paramstypes.NewSubspace(cdc, amino, paramsKey, tkey, banktypes.ModuleName)

accountKeeper := authkeeper.NewAccountKeeper(
cdc, authKey, authSubspace, authtypes.ProtoBaseAccount, map[string][]string{},
)
s.bankKeeper = bankkeeper.NewBaseKeeper(
cdc, bankKey, accountKeeper, bankSubspace, map[string]bool{},
)

baseApp.Router().AddRoute(sdk.NewRoute(banktypes.ModuleName, bank.NewHandler(s.bankKeeper)))
baseApp.MountStore(tkey, sdk.StoreTypeTransient)
baseApp.MountStore(paramsKey, sdk.StoreTypeIAVL)
baseApp.MountStore(authKey, sdk.StoreTypeIAVL)
baseApp.MountStore(bankKey, sdk.StoreTypeIAVL)
})

s.ctx = s.fixture.Context()
s.bankKeeper = s.fixture.BankKeeper()

s.blockTime = time.Now().UTC()

Expand Down

0 comments on commit c19d8b0

Please sign in to comment.