Skip to content

Commit

Permalink
refactor(testutil): deduplicate simapp setup (#1197)
Browse files Browse the repository at this point in the history
* refactor(testutil): deduplicate simapp setup

* chore: update changelog
  • Loading branch information
k-yang authored Feb 15, 2023
1 parent 8d464cf commit 8563989
Show file tree
Hide file tree
Showing 56 changed files with 275 additions and 646 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Improvements

* [#1197](https://github.com/NibiruChain/nibiru/pull/1197) - refactor(testutil): clean up `x/common/testutil` test setup code
* [#1193](https://github.com/NibiruChain/nibiru/pull/1193) - refactor(oracle): clean up `x/oracle/keeper` tests
* [#1192](https://github.com/NibiruChain/nibiru/pull/1192) - feat: chaosnet docker-compose
* [#1191](https://github.com/NibiruChain/nibiru/pull/1191) - fix(oracle): default whitelisted pairs
Expand Down
5 changes: 2 additions & 3 deletions app/ante.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package app

import (
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmTypes "github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
ibcante "github.com/cosmos/ibc-go/v3/modules/core/ante"
ibckeeper "github.com/cosmos/ibc-go/v3/modules/core/keeper"

feeante "github.com/NibiruChain/nibiru/app/antedecorators/fee"

wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmTypes "github.com/CosmWasm/wasmd/x/wasm/types"
)

type AnteHandlerOptions struct {
Expand Down
15 changes: 2 additions & 13 deletions app/antedecorators/fee/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ import (
"github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
xauthsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/NibiruChain/nibiru/app"
"github.com/NibiruChain/nibiru/simapp"
"github.com/NibiruChain/nibiru/x/common/testutil/testapp"
)

// AnteTestSuite is a test suite to be used with ante handler tests.
Expand All @@ -30,18 +28,9 @@ type AnteTestSuite struct {
txBuilder client.TxBuilder
}

// returns context and app with params set on account keeper
func createTestApp(isCheckTx bool) (*app.NibiruApp, sdk.Context) {
app := simapp.NewTestNibiruApp(true)
ctx := app.BaseApp.NewContext(isCheckTx, tmproto.Header{})
app.AccountKeeper.SetParams(ctx, authtypes.DefaultParams())

return app, ctx
}

// SetupTest setups a new test, with new app, context, and anteHandler.
func (suite *AnteTestSuite) SetupTest(isCheckTx bool) {
suite.app, suite.ctx = createTestApp(isCheckTx)
suite.app, suite.ctx = testapp.NewNibiruTestAppAndContext(false)
suite.ctx = suite.ctx.WithBlockHeight(1)

// Set up TxConfig.
Expand Down
4 changes: 2 additions & 2 deletions app/ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"github.com/stretchr/testify/suite"

"github.com/NibiruChain/nibiru/app"
"github.com/NibiruChain/nibiru/simapp"
"github.com/NibiruChain/nibiru/x/common/testutil"
"github.com/NibiruChain/nibiru/x/common/testutil/testapp"
)

// init changes the value of 'DefaultTestingAppInit' to use custom initialization.
Expand All @@ -35,7 +35,7 @@ func SetupNibiruTestingApp() (
defaultGenesis map[string]json.RawMessage,
) {
// create testing app
nibiruApp, _ := simapp.NewTestNibiruAppAndContext(true)
nibiruApp, _ := testapp.NewNibiruTestAppAndContext(true)

// Create genesis state
encCdc := app.MakeTestEncodingConfig()
Expand Down
21 changes: 13 additions & 8 deletions simapp/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"testing"

"github.com/NibiruChain/nibiru/app"
"github.com/NibiruChain/nibiru/x/common/testutil/testapp"
sdkSimapp "github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/simapp/helpers"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
Expand Down Expand Up @@ -35,23 +37,24 @@ func TestFullAppSimulation(tb *testing.T) {
}
}()

nibiru := NewTestNibiruApp( /*shouldUseDefaultGenesis*/ true)
encoding := app.MakeTestEncodingConfig()
app := testapp.NewNibiruTestApp(app.NewDefaultGenesisState(encoding.Marshaler))

// Run randomized simulation:
_, simParams, simErr := simulation.SimulateFromSeed(
/* tb */ tb,
/* w */ os.Stdout,
/* app */ nibiru.BaseApp,
/* appStateFn */ AppStateFn(nibiru.AppCodec(), nibiru.SimulationManager()),
/* app */ app.BaseApp,
/* appStateFn */ AppStateFn(app.AppCodec(), app.SimulationManager()),
/* randAccFn */ simulationtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
/* ops */ sdkSimapp.SimulationOperations(nibiru, nibiru.AppCodec(), config), // Run all registered operations
/* blockedAddrs */ nibiru.ModuleAccountAddrs(),
/* ops */ sdkSimapp.SimulationOperations(app, app.AppCodec(), config), // Run all registered operations
/* blockedAddrs */ app.ModuleAccountAddrs(),
/* config */ config,
/* cdc */ nibiru.AppCodec(),
/* cdc */ app.AppCodec(),
)

// export state and simParams before the simulation error is checked
if err = sdkSimapp.CheckExportSimulation(nibiru, config, simParams); err != nil {
if err = sdkSimapp.CheckExportSimulation(app, config, simParams); err != nil {
tb.Fatal(err)
}

Expand All @@ -69,6 +72,8 @@ func TestAppStateDeterminism(t *testing.T) {
t.Skip("skipping application simulation")
}

encoding := app.MakeTestEncodingConfig()

config := sdkSimapp.NewConfigFromFlags()
config.InitialBlockHeight = 1
config.ExportParamsPath = ""
Expand All @@ -85,7 +90,7 @@ func TestAppStateDeterminism(t *testing.T) {

for j := 0; j < numTimesToRunPerSeed; j++ {
db := dbm.NewMemDB()
app := NewTestNibiruApp( /*shouldUseDefaultGenesis*/ true)
app := testapp.NewNibiruTestApp(app.NewDefaultGenesisState(encoding.Marshaler))

fmt.Printf(
"running non-determinism simulation; seed %d: %d/%d, attempt: %d/%d\n",
Expand Down
132 changes: 0 additions & 132 deletions simapp/testapp.go

This file was deleted.

7 changes: 3 additions & 4 deletions x/common/testutil/cli/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (
"testing"
"time"

"github.com/NibiruChain/nibiru/simapp"
"github.com/NibiruChain/nibiru/x/common/denoms"
"github.com/NibiruChain/nibiru/x/common/testutil/testapp"

"github.com/NibiruChain/nibiru/app"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -43,8 +44,6 @@ import (
"github.com/tendermint/tendermint/node"
tmclient "github.com/tendermint/tendermint/rpc/client"
"google.golang.org/grpc"

"github.com/NibiruChain/nibiru/app"
)

// package-wide network lock to only allow one test network at a time
Expand Down Expand Up @@ -137,7 +136,7 @@ func BuildNetworkConfig(appGenesis app.GenesisState) Config {
InterfaceRegistry: encCfg.InterfaceRegistry,
AccountRetriever: authtypes.AccountRetriever{},
AppConstructor: func(val Validator) servertypes.Application {
return simapp.NewTestNibiruAppWithGenesis(appGenesis)
return testapp.NewNibiruTestApp(appGenesis)
},
GenesisState: appGenesis,
TimeoutCommit: time.Second / 2,
Expand Down
32 changes: 32 additions & 0 deletions x/common/testutil/genesis/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package genesis

import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

"github.com/NibiruChain/nibiru/app"
"github.com/NibiruChain/nibiru/x/common/denoms"
)

/*
NewTestGenesisState returns 'NewGenesisState' using the default
genesis as input. The blockchain genesis state is represented as a map from module
identifier strings to raw json messages.
*/
func NewTestGenesisState() app.GenesisState {
encodingConfig := app.MakeTestEncodingConfig()
codec := encodingConfig.Marshaler
genState := app.NewDefaultGenesisState(codec)

// Set short voting period to allow fast gov proposals in tests
var govGenState govtypes.GenesisState
codec.MustUnmarshalJSON(genState[govtypes.ModuleName], &govGenState)
govGenState.VotingParams.VotingPeriod = time.Second * 20
govGenState.DepositParams.MinDeposit = sdk.NewCoins(sdk.NewInt64Coin(denoms.NIBI, 1_000_000)) // min deposit of 1 NIBI
genState[govtypes.ModuleName] = codec.MustMarshalJSON(&govGenState)

return genState
}
52 changes: 0 additions & 52 deletions x/common/testutil/testapp/config.go

This file was deleted.

Loading

0 comments on commit 8563989

Please sign in to comment.