Skip to content

Commit

Permalink
refactor(test): add is not mandatory interface to action (#1719)
Browse files Browse the repository at this point in the history
* add is not mandatory interface to action

* add changelog

* Refactor AllBalancesEqual too

* make lint

---------

Co-authored-by: Unique Divine <[email protected]>
  • Loading branch information
jgimeno and Unique-Divine authored Dec 18, 2023
1 parent a0379dc commit 873c841
Show file tree
Hide file tree
Showing 26 changed files with 329 additions and 290 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1695](https://github.com/NibiruChain/nibiru/pull/1695) - fix(sudo): Make blank sudoers root invalid at genesis time.
* [#1710](https://github.com/NibiruChain/nibiru/pull/1710) - refactor(perp): Clean and organize module errors for x/perp
* [#1714](https://github.com/NibiruChain/nibiru/pull/1714) - ci(localnet.sh): Fix script, simplify, and test in CI.
* [#1719](https://github.com/NibiruChain/nibiru/pull/1719) - refactor(test): add is not mandatory interface to action

### Dependencies

Expand Down
16 changes: 8 additions & 8 deletions x/common/testutil/action/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ func FundAccount(account sdk.AccAddress, amount sdk.Coins) Action {
return &fundAccount{Account: account, Amount: amount}
}

func (c fundAccount) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
func (c fundAccount) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error) {
err := app.BankKeeper.MintCoins(ctx, inflationtypes.ModuleName, c.Amount)
if err != nil {
return ctx, err, true
return ctx, err
}

err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, inflationtypes.ModuleName, c.Account, c.Amount)
if err != nil {
return ctx, err, true
return ctx, err
}

return ctx, nil, true
return ctx, nil
}

type fundModule struct {
Expand All @@ -39,16 +39,16 @@ func FundModule(module string, amount sdk.Coins) Action {
return fundModule{Module: module, Amount: amount}
}

func (c fundModule) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
func (c fundModule) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error) {
err := app.BankKeeper.MintCoins(ctx, inflationtypes.ModuleName, c.Amount)
if err != nil {
return ctx, err, true
return ctx, err
}

err = app.BankKeeper.SendCoinsFromModuleToModule(ctx, inflationtypes.ModuleName, c.Module, c.Amount)
if err != nil {
return ctx, err, true
return ctx, err
}

return ctx, nil, true
return ctx, nil
}
28 changes: 14 additions & 14 deletions x/common/testutil/action/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ type increaseBlockNumberBy struct {
numBlocks int64
}

func (i increaseBlockNumberBy) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
func (i increaseBlockNumberBy) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error) {
app.EndBlocker(ctx, types.RequestEndBlock{Height: ctx.BlockHeight()})

ctx = ctx.WithBlockHeight(ctx.BlockHeight() + i.numBlocks)

return ctx, nil, true
return ctx, nil
}

// IncreaseBlockNumberBy increases the block number by the given number of blocks
Expand All @@ -31,10 +31,10 @@ type increaseBlockTimeBy struct {
seconds time.Duration
}

func (i increaseBlockTimeBy) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
func (i increaseBlockTimeBy) Do(_ *app.NibiruApp, ctx sdk.Context) (sdk.Context, error) {
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(time.Second * i.seconds))

return ctx, nil, true
return ctx, nil
}

func IncreaseBlockTimeBy(seconds time.Duration) Action {
Expand All @@ -45,8 +45,8 @@ type setBlockTime struct {
blockTime time.Time
}

func (s setBlockTime) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
return ctx.WithBlockTime(s.blockTime), nil, true
func (s setBlockTime) Do(_ *app.NibiruApp, ctx sdk.Context) (sdk.Context, error) {
return ctx.WithBlockTime(s.blockTime), nil
}

// SetBlockTime sets the block time to the given value
Expand All @@ -58,8 +58,8 @@ type setBlockNumber struct {
blockNumber int64
}

func (s setBlockNumber) Do(_ *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
return ctx.WithBlockHeight(s.blockNumber), nil, true
func (s setBlockNumber) Do(_ *app.NibiruApp, ctx sdk.Context) (sdk.Context, error) {
return ctx.WithBlockHeight(s.blockNumber), nil
}

// SetBlockNumber sets the block number to the given value
Expand All @@ -69,7 +69,7 @@ func SetBlockNumber(blockNumber int64) Action {

type moveToNextBlock struct{}

func (m moveToNextBlock) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
func (m moveToNextBlock) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error) {
app.EndBlock(types.RequestEndBlock{})
app.Commit()

Expand All @@ -85,7 +85,7 @@ func (m moveToNextBlock) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, e
return app.NewContext(
false,
newHeader,
).WithBlockTime(newHeader.Time), nil, true
).WithBlockTime(newHeader.Time), nil
}

func MoveToNextBlock() Action {
Expand All @@ -96,7 +96,7 @@ type moveToNextBlockWithDuration struct {
blockDuration time.Duration
}

func (m moveToNextBlockWithDuration) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
func (m moveToNextBlockWithDuration) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error) {
app.EndBlock(types.RequestEndBlock{Height: ctx.BlockHeight()})
app.Commit()

Expand All @@ -112,7 +112,7 @@ func (m moveToNextBlockWithDuration) Do(app *app.NibiruApp, ctx sdk.Context) (sd
return app.NewContext(
false,
newHeader,
).WithBlockTime(newHeader.Time), nil, true
).WithBlockTime(newHeader.Time), nil
}

func MoveToNextBlockWithDuration(blockDuration time.Duration) Action {
Expand All @@ -125,7 +125,7 @@ type moveToNextBlockWithTime struct {
blockTime time.Time
}

func (m moveToNextBlockWithTime) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
func (m moveToNextBlockWithTime) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error) {
app.EndBlock(types.RequestEndBlock{Height: ctx.BlockHeight()})
app.Commit()

Expand All @@ -141,7 +141,7 @@ func (m moveToNextBlockWithTime) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Co
return app.NewContext(
false,
newHeader,
).WithBlockTime(newHeader.Time), nil, true
).WithBlockTime(newHeader.Time), nil
}

func MoveToNextBlockWithTime(blockTime time.Time) Action {
Expand Down
47 changes: 29 additions & 18 deletions x/common/testutil/action/testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// `Action` is a type of operation or task that can be performed in the
// Action is a type of operation or task that can be performed in the
// Nibiru application.
type Action interface {
// `Do` is a specific implementation of the `Action`. When `Do` is called,
// Do is a specific implementation of the `Action`. When `Do` is called,
// the action is performed and some feedback is provided about the action's
// success. `Do` can mutate the app.
//
Expand All @@ -25,12 +25,17 @@ type Action interface {
// - err: The error if one was raised.
// - isMandatory: Whether an error should have been raised.
Do(app *app.NibiruApp, ctx sdk.Context) (
outCtx sdk.Context, err error, isMandatory bool,
outCtx sdk.Context, err error,
)
}

func ActionResp(ctx sdk.Context, respErr error) (outCtx sdk.Context, err error, isMandatory bool) {
return ctx, respErr, false
// IsNotMandatory is a marker interface for actions that are not mandatory, and it does not stop the test when there is an error.
type IsNotMandatory interface {
IsNotMandatory()
}

func ActionResp(ctx sdk.Context, respErr error) (outCtx sdk.Context, err error) {
return ctx, respErr
}

type TestCases []TestCase
Expand Down Expand Up @@ -67,32 +72,38 @@ func (tc TestCase) Run(t *testing.T) {
t.Run(tc.Name, func(t *testing.T) {
app, ctx := testapp.NewNibiruTestAppAndContextAtTime(time.UnixMilli(0))
var err error
var isMandatory bool
var isNotMandatory bool

for _, action := range tc.given {
ctx, err, isMandatory = action.Do(app, ctx)
if isMandatory {
require.NoError(t, err, "failed to execute given action: %s", tc.Name)
} else {
_, isNotMandatory = action.(IsNotMandatory)

ctx, err = action.Do(app, ctx)
if isNotMandatory {
assert.NoError(t, err, "failed to execute given action: %s", tc.Name)
} else {
require.NoError(t, err, "failed to execute given action: %s", tc.Name)
}
}

for _, action := range tc.when {
ctx, err, isMandatory = action.Do(app, ctx)
if isMandatory {
require.NoError(t, err, "failed to execute when action: %s", tc.Name)
} else {
_, isNotMandatory = action.(IsNotMandatory)

ctx, err = action.Do(app, ctx)
if isNotMandatory {
assert.NoError(t, err, "failed to execute when action: %s", tc.Name)
} else {
require.NoError(t, err, "failed to execute when action: %s", tc.Name)
}
}

for _, action := range tc.then {
ctx, err, isMandatory = action.Do(app, ctx)
if isMandatory {
require.NoError(t, err, "failed to execute then action: %s", tc.Name)
} else {
_, isNotMandatory = action.(IsNotMandatory)

ctx, err = action.Do(app, ctx)
if isNotMandatory {
assert.NoError(t, err, "failed to execute then action: %s", tc.Name)
} else {
require.NoError(t, err, "failed to execute then action: %s", tc.Name)
}
}
})
Expand Down
30 changes: 18 additions & 12 deletions x/common/testutil/assertion/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package assertion
import (
"fmt"

"github.com/NibiruChain/nibiru/x/common/testutil/action"

sdkmath "cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"

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

func AllBalancesEqual(account sdk.AccAddress, amount sdk.Coins) *allBalancesEqual {
func AllBalancesEqual(account sdk.AccAddress, amount sdk.Coins) action.Action {
return &allBalancesEqual{Account: account, Amount: amount}
}

Expand All @@ -19,21 +21,21 @@ type allBalancesEqual struct {
Amount sdk.Coins
}

func (b allBalancesEqual) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
func (b allBalancesEqual) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error) {
coins := app.BankKeeper.GetAllBalances(ctx, b.Account)
if !coins.IsEqual(b.Amount) {
return ctx, fmt.Errorf(
"account %s balance not equal, expected %s, got %s",
b.Account.String(),
b.Amount.String(),
coins.String(),
), false
)
}

return ctx, nil, false
return ctx, nil
}

func BalanceEqual(account sdk.AccAddress, denom string, amount sdkmath.Int) *balanceEqual {
func BalanceEqual(account sdk.AccAddress, denom string, amount sdkmath.Int) action.Action {
return &balanceEqual{Account: account, Denom: denom, Amount: amount}
}

Expand All @@ -43,21 +45,23 @@ type balanceEqual struct {
Amount sdkmath.Int
}

func (b balanceEqual) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
func (b balanceEqual) IsNotMandatory() {}

func (b balanceEqual) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error) {
coin := app.BankKeeper.GetBalance(ctx, b.Account, b.Denom)
if !coin.Amount.Equal(b.Amount) {
return ctx, fmt.Errorf(
"account %s balance not equal, expected %s, got %s",
b.Account.String(),
b.Amount.String(),
coin.String(),
), false
)
}

return ctx, nil, false
return ctx, nil
}

func ModuleBalanceEqual(moduleName string, denom string, amount sdkmath.Int) *moduleBalanceEqual {
func ModuleBalanceEqual(moduleName string, denom string, amount sdkmath.Int) action.Action {
return &moduleBalanceEqual{ModuleName: moduleName, Denom: denom, Amount: amount}
}

Expand All @@ -67,16 +71,18 @@ type moduleBalanceEqual struct {
Amount sdkmath.Int
}

func (b moduleBalanceEqual) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
func (b moduleBalanceEqual) IsNotMandatory() {}

func (b moduleBalanceEqual) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error) {
coin := app.BankKeeper.GetBalance(ctx, app.AccountKeeper.GetModuleAddress(b.ModuleName), b.Denom)
if !coin.Amount.Equal(b.Amount) {
return ctx, fmt.Errorf(
"module %s balance not equal, expected %s, got %s",
b.ModuleName,
b.Amount.String(),
coin.String(),
), false
)
}

return ctx, nil, false
return ctx, nil
}
6 changes: 3 additions & 3 deletions x/common/testutil/assertion/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ type gasConsumedShouldBe struct {
gasConsumed uint64
}

func (g gasConsumedShouldBe) Do(_ *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
func (g gasConsumedShouldBe) Do(_ *app.NibiruApp, ctx sdk.Context) (sdk.Context, error) {
gasUsed := ctx.GasMeter().GasConsumed()
if g.gasConsumed != gasUsed {
return ctx, fmt.Errorf("gas consumed should be %d, but got %d", g.gasConsumed, gasUsed), true
return ctx, fmt.Errorf("gas consumed should be %d, but got %d", g.gasConsumed, gasUsed)
}

return ctx, nil, true
return ctx, nil
}

func GasConsumedShouldBe(gasConsumed uint64) action.Action {
Expand Down
8 changes: 5 additions & 3 deletions x/epochs/integration/action/epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ type startEpoch struct {
epochIdentifier string
}

func (s startEpoch) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
func (s startEpoch) IsNotMandatory() {}

func (s startEpoch) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error) {
epochInfo, err := app.EpochsKeeper.GetEpochInfo(ctx, s.epochIdentifier)
if err != nil {
return ctx, err, false
return ctx, err
}
epochInfo.EpochCountingStarted = true
epochInfo.CurrentEpoch = 1
Expand All @@ -24,7 +26,7 @@ func (s startEpoch) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error,

app.EpochsKeeper.Epochs.Insert(ctx, epochInfo.Identifier, epochInfo)

return ctx, nil, false
return ctx, nil
}

func StartEpoch(epochIdentifier string) action.Action {
Expand Down
Loading

0 comments on commit 873c841

Please sign in to comment.