0g-chain/x/savings/keeper/keeper_test.go

102 lines
3.0 KiB
Go
Raw Normal View History

package keeper_test
import (
"fmt"
"testing"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtime "github.com/tendermint/tendermint/types/time"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/kava-labs/kava/app"
"github.com/kava-labs/kava/x/savings/keeper"
"github.com/kava-labs/kava/x/savings/types"
)
// Test suite used for all keeper tests
type KeeperTestSuite struct {
suite.Suite
keeper keeper.Keeper
app app.TestApp
ctx sdk.Context
addrs []sdk.AccAddress
}
// The default state used by each test
func (suite *KeeperTestSuite) SetupTest() {
config := sdk.GetConfig()
app.SetBech32AddressPrefixes(config)
tApp := app.NewTestApp()
ctx := tApp.NewContext(true, tmproto.Header{Height: 1, Time: tmtime.Now()})
tApp.InitializeFromGenesisStates()
_, addrs := app.GeneratePrivKeyAddressPairs(1)
keeper := tApp.GetSavingsKeeper()
suite.app = tApp
suite.ctx = ctx
suite.keeper = keeper
suite.addrs = addrs
}
func (suite *KeeperTestSuite) TestGetSetDeleteDeposit() {
dep := types.NewDeposit(sdk.AccAddress("test"), sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))))
_, f := suite.keeper.GetDeposit(suite.ctx, sdk.AccAddress("test"))
suite.Require().False(f)
suite.keeper.SetDeposit(suite.ctx, dep)
testDeposit, f := suite.keeper.GetDeposit(suite.ctx, sdk.AccAddress("test"))
suite.Require().True(f)
suite.Require().Equal(dep, testDeposit)
suite.Require().NotPanics(func() { suite.keeper.DeleteDeposit(suite.ctx, dep) })
_, f = suite.keeper.GetDeposit(suite.ctx, sdk.AccAddress("test"))
suite.Require().False(f)
}
func (suite *KeeperTestSuite) TestIterateDeposits() {
for i := 0; i < 5; i++ {
dep := types.NewDeposit(sdk.AccAddress("test"+fmt.Sprint(i)), sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))))
suite.Require().NotPanics(func() { suite.keeper.SetDeposit(suite.ctx, dep) })
}
var deposits []types.Deposit
suite.keeper.IterateDeposits(suite.ctx, func(d types.Deposit) bool {
deposits = append(deposits, d)
return false
})
suite.Require().Equal(5, len(deposits))
}
func (suite *KeeperTestSuite) getAccountCoins(acc authtypes.AccountI) sdk.Coins {
bk := suite.app.GetBankKeeper()
return bk.GetAllBalances(suite.ctx, acc.GetAddress())
}
func (suite *KeeperTestSuite) getAccount(addr sdk.AccAddress) authtypes.AccountI {
ak := suite.app.GetAccountKeeper()
return ak.GetAccount(suite.ctx, addr)
}
func (suite *KeeperTestSuite) getAccountAtCtx(addr sdk.AccAddress, ctx sdk.Context) authtypes.AccountI {
ak := suite.app.GetAccountKeeper()
return ak.GetAccount(ctx, addr)
}
func (suite *KeeperTestSuite) getModuleAccount(name string) authtypes.ModuleAccountI {
ak := suite.app.GetAccountKeeper()
return ak.GetModuleAccount(suite.ctx, name)
}
func (suite *KeeperTestSuite) getModuleAccountAtCtx(name string, ctx sdk.Context) authtypes.ModuleAccountI {
ak := suite.app.GetAccountKeeper()
return ak.GetModuleAccount(ctx, name)
}
func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}