0g-chain/x/cdp/keeper/bank_test.go

60 lines
2.1 KiB
Go
Raw Normal View History

2019-12-03 14:35:27 +00:00
package keeper_test
2019-11-25 19:46:02 +00:00
import (
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
2019-12-03 14:35:27 +00:00
"github.com/kava-labs/kava/app"
keep "github.com/kava-labs/kava/x/cdp/keeper"
2019-11-25 19:46:02 +00:00
)
// Test the bank functionality of the CDP keeper
func TestKeeper_AddSubtractGetCoins(t *testing.T) {
2019-12-03 14:35:27 +00:00
_, addrs := app.GeneratePrivKeyAddressPairs(1)
2019-11-25 19:46:02 +00:00
normalAddr := addrs[0]
tests := []struct {
name string
address sdk.AccAddress
shouldAdd bool
amount sdk.Coins
expectedCoins sdk.Coins
}{
{"addNormalAddress", normalAddr, true, cs(c("usdx", 53)), cs(c("usdx", 153), c("kava", 100))},
{"subNormalAddress", normalAddr, false, cs(c("usdx", 53)), cs(c("usdx", 47), c("kava", 100))},
2019-12-03 14:35:27 +00:00
{"addLiquidatorStable", keep.LiquidatorAccountAddress, true, cs(c("usdx", 53)), cs(c("usdx", 153))},
{"subLiquidatorStable", keep.LiquidatorAccountAddress, false, cs(c("usdx", 53)), cs(c("usdx", 47))},
{"addLiquidatorGov", keep.LiquidatorAccountAddress, true, cs(c("kava", 53)), cs(c("usdx", 100))}, // no change to balance
{"subLiquidatorGov", keep.LiquidatorAccountAddress, false, cs(c("kava", 53)), cs(c("usdx", 100))}, // no change to balance
2019-11-25 19:46:02 +00:00
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
2019-12-03 14:35:27 +00:00
// setup app with an account
tApp := app.NewTestApp()
authGenState := tApp.NewAuthGenStateFromAccounts([]sdk.AccAddress{normalAddr}, []sdk.Coins{cs(c("usdx", 100), c("kava", 100))})
tApp.InitializeFromGenesisStates(authGenState)
2019-11-25 19:46:02 +00:00
// create a new context and setup the liquidator account
2019-12-03 14:35:27 +00:00
ctx := tApp.NewContext(false, abci.Header{})
keeper := tApp.GetCDPKeeper()
keeper.SetLiquidatorModuleAccount(ctx, keep.LiquidatorModuleAccount{cs(c("usdx", 100))}) // set gov coin "balance" to zero
2019-11-25 19:46:02 +00:00
// perform the test action
var err sdk.Error
if tc.shouldAdd {
_, err = keeper.AddCoins(ctx, tc.address, tc.amount)
} else {
_, err = keeper.SubtractCoins(ctx, tc.address, tc.amount)
}
// check balances are as expected
require.NoError(t, err)
require.Equal(t, tc.expectedCoins, keeper.GetCoins(ctx, tc.address))
})
}
}