update cdp tests

This commit is contained in:
rhuairahrighairigh 2019-12-03 14:35:40 +00:00
parent ce2b2e2213
commit 2d73e62773
3 changed files with 49 additions and 75 deletions

View File

@ -1,30 +1,37 @@
package cdp package cdp_test
import ( import (
"testing" "testing"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/mock"
abci "github.com/tendermint/tendermint/abci/types" abci "github.com/tendermint/tendermint/abci/types"
"github.com/kava-labs/kava/app"
"github.com/kava-labs/kava/x/cdp"
"github.com/kava-labs/kava/x/pricefeed" "github.com/kava-labs/kava/x/pricefeed"
) )
func TestApp_CreateModifyDeleteCDP(t *testing.T) { func TestApp_CreateModifyDeleteCDP(t *testing.T) {
// Setup // Setup
mapp, keeper, pfKeeper := setUpMockAppWithoutGenesis() tApp := app.NewTestApp()
genAccs, addrs, _, privKeys := mock.CreateGenAccounts(1, cs(c("xrp", 100))) privKeys, addrs := app.GeneratePrivKeyAddressPairs(1)
testAddr := addrs[0] testAddr := addrs[0]
testPrivKey := privKeys[0] testPrivKey := privKeys[0]
mock.SetGenesis(mapp, genAccs) tApp.InitializeFromGenesisStates(
mock.CheckBalance(t, mapp, testAddr, cs(c("xrp", 100))) tApp.NewAuthGenStateFromAccounts(addrs, []sdk.Coins{cs(c("xrp", 100))}),
// setup pricefeed, TODO can this be shortened a bit? )
header := abci.Header{Height: mapp.LastBlockHeight() + 1} // check balance
mapp.BeginBlock(abci.RequestBeginBlock{Header: header}) ctx := tApp.NewContext(false, abci.Header{})
ctx := mapp.BaseApp.NewContext(false, header) require.Equal(t, cs(c("xrp", 100)), tApp.GetAccountKeeper().GetAccount(ctx, testAddr).GetCoins())
params := CdpParams{
// setup cdp keeper
keeper := tApp.GetCDPKeeper()
params := cdp.CdpParams{
GlobalDebtLimit: sdk.NewInt(100000), GlobalDebtLimit: sdk.NewInt(100000),
CollateralParams: []CollateralParams{ CollateralParams: []cdp.CollateralParams{
{ {
Denom: "xrp", Denom: "xrp",
LiquidationRatio: sdk.MustNewDecFromStr("1.5"), LiquidationRatio: sdk.MustNewDecFromStr("1.5"),
@ -35,6 +42,8 @@ func TestApp_CreateModifyDeleteCDP(t *testing.T) {
} }
keeper.SetParams(ctx, params) keeper.SetParams(ctx, params)
keeper.SetGlobalDebt(ctx, sdk.NewInt(0)) keeper.SetGlobalDebt(ctx, sdk.NewInt(0))
// setup pricefeed
pfKeeper := tApp.GetPriceFeedKeeper()
ap := pricefeed.AssetParams{ ap := pricefeed.AssetParams{
Assets: []pricefeed.Asset{pricefeed.Asset{AssetCode: "xrp", Description: ""}}, Assets: []pricefeed.Asset{pricefeed.Asset{AssetCode: "xrp", Description: ""}},
} }
@ -44,24 +53,30 @@ func TestApp_CreateModifyDeleteCDP(t *testing.T) {
sdk.MustNewDecFromStr("1.00"), sdk.MustNewDecFromStr("1.00"),
sdk.NewInt(10)) sdk.NewInt(10))
pfKeeper.SetCurrentPrices(ctx) pfKeeper.SetCurrentPrices(ctx)
mapp.EndBlock(abci.RequestEndBlock{}) tApp.EndBlock(abci.RequestEndBlock{})
mapp.Commit() tApp.Commit()
// Create CDP // Create CDP
msgs := []sdk.Msg{NewMsgCreateOrModifyCDP(testAddr, "xrp", i(10), i(5))} msgs := []sdk.Msg{cdp.NewMsgCreateOrModifyCDP(testAddr, "xrp", i(10), i(5))}
mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, abci.Header{Height: mapp.LastBlockHeight() + 1}, msgs, []uint64{0}, []uint64{0}, true, true, testPrivKey) simapp.SignCheckDeliver(t, tApp.Codec(), tApp.BaseApp, abci.Header{Height: tApp.LastBlockHeight() + 1}, msgs, []uint64{0}, []uint64{0}, true, true, testPrivKey)
mock.CheckBalance(t, mapp, testAddr, cs(c("usdx", 5), c("xrp", 90))) // check balance
ctx = tApp.NewContext(true, abci.Header{})
require.Equal(t, cs(c("usdx", 5), c("xrp", 90)), tApp.GetAccountKeeper().GetAccount(ctx, testAddr).GetCoins())
// Modify CDP // Modify CDP
msgs = []sdk.Msg{NewMsgCreateOrModifyCDP(testAddr, "xrp", i(40), i(5))} msgs = []sdk.Msg{cdp.NewMsgCreateOrModifyCDP(testAddr, "xrp", i(40), i(5))}
mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, abci.Header{Height: mapp.LastBlockHeight() + 1}, msgs, []uint64{0}, []uint64{1}, true, true, testPrivKey) simapp.SignCheckDeliver(t, tApp.Codec(), tApp.BaseApp, abci.Header{Height: tApp.LastBlockHeight() + 1}, msgs, []uint64{0}, []uint64{1}, true, true, testPrivKey)
mock.CheckBalance(t, mapp, testAddr, cs(c("usdx", 10), c("xrp", 50))) // check balance
ctx = tApp.NewContext(true, abci.Header{})
require.Equal(t, cs(c("usdx", 10), c("xrp", 50)), tApp.GetAccountKeeper().GetAccount(ctx, testAddr).GetCoins())
// Delete CDP // Delete CDP
msgs = []sdk.Msg{NewMsgCreateOrModifyCDP(testAddr, "xrp", i(-50), i(-10))} msgs = []sdk.Msg{cdp.NewMsgCreateOrModifyCDP(testAddr, "xrp", i(-50), i(-10))}
mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, abci.Header{Height: mapp.LastBlockHeight() + 1}, msgs, []uint64{0}, []uint64{2}, true, true, testPrivKey) simapp.SignCheckDeliver(t, tApp.Codec(), tApp.BaseApp, abci.Header{Height: tApp.LastBlockHeight() + 1}, msgs, []uint64{0}, []uint64{2}, true, true, testPrivKey)
mock.CheckBalance(t, mapp, testAddr, cs(c("xrp", 100))) // check balance
ctx = tApp.NewContext(true, abci.Header{})
require.Equal(t, cs(c("xrp", 100)), tApp.GetAccountKeeper().GetAccount(ctx, testAddr).GetCoins())
} }

11
x/cdp/integration_test.go Normal file
View File

@ -0,0 +1,11 @@
package cdp_test
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Avoid cluttering test cases with long function name
func i(in int64) sdk.Int { return sdk.NewInt(in) }
func d(str string) sdk.Dec { return sdk.MustNewDecFromStr(str) }
func c(denom string, amount int64) sdk.Coin { return sdk.NewInt64Coin(denom, amount) }
func cs(coins ...sdk.Coin) sdk.Coins { return sdk.NewCoins(coins...) }

View File

@ -1,52 +0,0 @@
package cdp
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/mock"
"github.com/kava-labs/kava/x/pricefeed"
)
// Mock app is an ABCI app with an in memory database.
// This function creates an app, setting up the keepers, routes, begin and end blockers.
// But leaves it to the tests to call InitChain (done by calling mock.SetGenesis)
// The app works by submitting ABCI messages.
// - InitChain sets up the app db from genesis.
// - BeginBlock starts the delivery of a new block
// - DeliverTx delivers a tx
// - EndBlock signals the end of a block
// - Commit ?
func setUpMockAppWithoutGenesis() (*mock.App, Keeper, PricefeedKeeper) {
// Create uninitialized mock app
mapp := mock.NewApp()
// Register codecs
RegisterCodec(mapp.Cdc)
// Create keepers
keyCDP := sdk.NewKVStoreKey("cdp")
keyPriceFeed := sdk.NewKVStoreKey(pricefeed.StoreKey)
pk := mapp.ParamsKeeper
priceFeedKeeper := pricefeed.NewKeeper(keyPriceFeed, mapp.Cdc, pk.Subspace(pricefeed.DefaultParamspace).WithKeyTable(pricefeed.ParamKeyTable()), pricefeed.DefaultCodespace)
blacklistedAddrs := make(map[string]bool)
bankKeeper := bank.NewBaseKeeper(mapp.AccountKeeper, pk.Subspace(bank.DefaultParamspace), bank.DefaultCodespace, blacklistedAddrs)
cdpKeeper := NewKeeper(mapp.Cdc, keyCDP, pk.Subspace(DefaultParamspace), priceFeedKeeper, bankKeeper)
// Register routes
mapp.Router().AddRoute("cdp", NewHandler(cdpKeeper))
// Mount and load the stores
err := mapp.CompleteSetup(keyPriceFeed, keyCDP)
if err != nil {
panic("mock app setup failed")
}
return mapp, cdpKeeper, priceFeedKeeper
}
// Avoid cluttering test cases with long function name
func i(in int64) sdk.Int { return sdk.NewInt(in) }
func d(str string) sdk.Dec { return sdk.MustNewDecFromStr(str) }
func c(denom string, amount int64) sdk.Coin { return sdk.NewInt64Coin(denom, amount) }
func cs(coins ...sdk.Coin) sdk.Coins { return sdk.NewCoins(coins...) }