Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-nguy committed Nov 22, 2021
1 parent 60f0fc3 commit 0287876
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
32 changes: 26 additions & 6 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"testing"
"time"

servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/crypto-org-chain/cronos/x/cronos"

"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
Expand Down Expand Up @@ -58,19 +61,36 @@ var DefaultConsensusParams = &abci.ConsensusParams{
},
}

func setup(withGenesis bool, invCheckPeriod uint) (*App, GenesisState) {
// ExperimentalAppOptions is a stub implementing AppOptions
type ExperimentalAppOptions struct{}

// Get implements AppOptions
func (ao ExperimentalAppOptions) Get(o string) interface{} {
if o == cronos.ExperimentalFlag {
return true
}
return nil
}

func setup(withGenesis bool, invCheckPeriod uint, experimental bool) (*App, GenesisState) {
db := dbm.NewMemDB()
encCdc := MakeEncodingConfig()
app := New(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, invCheckPeriod, encCdc, EmptyAppOptions{})
var appOption servertypes.AppOptions
if experimental {
appOption = ExperimentalAppOptions{}
} else {
appOption = EmptyAppOptions{}
}
app := New(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, invCheckPeriod, encCdc, appOption)
if withGenesis {
return app, NewDefaultGenesisState(encCdc.Marshaler)
}
return app, GenesisState{}
}

// Setup initializes a new App. A Nop logger is set in App.
func Setup(isCheckTx bool, cronosAdmin string) *App {
app, genesisState := setup(!isCheckTx, 5)
func Setup(isCheckTx bool, cronosAdmin string, experimental bool) *App {
app, genesisState := setup(!isCheckTx, 5, experimental)

// set cronos_admin for test
cronosGen := cronostypes.DefaultGenesis()
Expand Down Expand Up @@ -105,7 +125,7 @@ func Setup(isCheckTx bool, cronosAdmin string) *App {
// of one consensus engine unit (10^6) in the default token of the simapp from first genesis
// account. A Nop logger is set in App.
func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *App {
app, genesisState := setup(true, 5)
app, genesisState := setup(true, 5, true)
// set genesis accounts
authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)
genesisState[authtypes.ModuleName] = app.AppCodec().MustMarshalJSON(authGenesis)
Expand Down Expand Up @@ -185,7 +205,7 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs
// SetupWithGenesisAccounts initializes a new App with the provided genesis
// accounts and possible balances.
func SetupWithGenesisAccounts(genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *App {
app, genesisState := setup(true, 0)
app, genesisState := setup(true, 0, true)
authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)
genesisState[authtypes.ModuleName] = app.AppCodec().MustMarshalJSON(authGenesis)

Expand Down
2 changes: 1 addition & 1 deletion x/cronos/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (suite *CronosTestSuite) SetupTest() {
privKey, err := ethsecp256k1.GenerateKey()
suite.Require().NoError(err)
suite.address = sdk.AccAddress(privKey.PubKey().Address())
suite.app = app.Setup(false, suite.address.String())
suite.app = app.Setup(false, suite.address.String(), true)
suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{Height: 1, ChainID: app.TestAppChainID, Time: time.Now().UTC()})
suite.handler = cronos.NewHandler(suite.app.CronosKeeper)

Expand Down
2 changes: 1 addition & 1 deletion x/cronos/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (suite *KeeperTestSuite) DoSetupTest(t require.TestingT) {
require.NoError(t, err)
consAddress := sdk.ConsAddress(priv.PubKey().Address())

suite.app = app.Setup(checkTx, sdk.AccAddress(suite.address.Bytes()).String())
suite.app = app.Setup(checkTx, sdk.AccAddress(suite.address.Bytes()).String(), true)
suite.ctx = suite.app.NewContext(checkTx, tmproto.Header{
Height: 1,
ChainID: app.TestAppChainID,
Expand Down

0 comments on commit 0287876

Please sign in to comment.