rename test helper func

This commit is contained in:
rhuairahrighairigh 2019-12-07 00:12:07 +00:00
parent 83969f3702
commit 2152bb4ed2
7 changed files with 22 additions and 24 deletions

View File

@ -44,7 +44,6 @@ import (
// So TestApp.InitializeFromGenesisStates() will call InitGenesis with the default genesis state. // So TestApp.InitializeFromGenesisStates() will call InitGenesis with the default genesis state.
// and TestApp.InitializeFromGenesisStates(authState, cdpState) will do the same but overwrite the auth and cdp sections of the default genesis state // and TestApp.InitializeFromGenesisStates(authState, cdpState) will do the same but overwrite the auth and cdp sections of the default genesis state
// Creating the genesis states can be combersome, but helper methods can make it easier such as NewAuthGenStateFromAccounts below. // Creating the genesis states can be combersome, but helper methods can make it easier such as NewAuthGenStateFromAccounts below.
//
type TestApp struct { type TestApp struct {
App App
} }
@ -71,19 +70,6 @@ func (tApp TestApp) GetCDPKeeper() cdp.Keeper { return tApp.cdpKee
func (tApp TestApp) GetLiquidatorKeeper() liquidator.Keeper { return tApp.liquidatorKeeper } func (tApp TestApp) GetLiquidatorKeeper() liquidator.Keeper { return tApp.liquidatorKeeper }
func (tApp TestApp) GetPriceFeedKeeper() pricefeed.Keeper { return tApp.pricefeedKeeper } func (tApp TestApp) GetPriceFeedKeeper() pricefeed.Keeper { return tApp.pricefeedKeeper }
// Create a new auth genesis state from some addresses and coins. The state is returned marshalled into a map.
// TODO make this not a TestApp method.
func (tApp TestApp) NewAuthGenStateFromAccounts(addresses []sdk.AccAddress, coins []sdk.Coins) GenesisState {
// Create GenAccounts
accounts := authexported.GenesisAccounts{}
for i := range addresses {
accounts = append(accounts, auth.NewBaseAccount(addresses[i], coins[i], nil, 0, 0))
}
// Create the auth genesis state
authGenesis := auth.NewGenesisState(auth.DefaultParams(), accounts)
return GenesisState{auth.ModuleName: auth.ModuleCdc.MustMarshalJSON(authGenesis)}
}
// This calls InitChain on the app using the default genesis state, overwitten with any passed in genesis states // This calls InitChain on the app using the default genesis state, overwitten with any passed in genesis states
func (tApp TestApp) InitializeFromGenesisStates(genesisStates ...GenesisState) TestApp { func (tApp TestApp) InitializeFromGenesisStates(genesisStates ...GenesisState) TestApp {
// Create a default genesis state and overwrite with provided values // Create a default genesis state and overwrite with provided values
@ -115,6 +101,18 @@ func (tApp TestApp) CheckBalance(t *testing.T, ctx sdk.Context, owner sdk.AccAdd
require.Equal(t, expectedCoins, actualCoins) require.Equal(t, expectedCoins, actualCoins)
} }
// Create a new auth genesis state from some addresses and coins. The state is returned marshalled into a map.
func NewAuthGenState(addresses []sdk.AccAddress, coins []sdk.Coins) GenesisState {
// Create GenAccounts
accounts := authexported.GenesisAccounts{}
for i := range addresses {
accounts = append(accounts, auth.NewBaseAccount(addresses[i], coins[i], nil, 0, 0))
}
// Create the auth genesis state
authGenesis := auth.NewGenesisState(auth.DefaultParams(), accounts)
return GenesisState{auth.ModuleName: auth.ModuleCdc.MustMarshalJSON(authGenesis)}
}
// GeneratePrivKeyAddressPairsFromRand generates (deterministically) a total of n private keys and addresses. // GeneratePrivKeyAddressPairsFromRand generates (deterministically) a total of n private keys and addresses.
// TODO only generate secp256 keys? // TODO only generate secp256 keys?
func GeneratePrivKeyAddressPairs(n int) (keys []crypto.PrivKey, addrs []sdk.AccAddress) { func GeneratePrivKeyAddressPairs(n int) (keys []crypto.PrivKey, addrs []sdk.AccAddress) {

View File

@ -17,7 +17,7 @@ func TestKeeper_EndBlocker(t *testing.T) {
seller := addrs[0] seller := addrs[0]
tApp := app.NewTestApp() tApp := app.NewTestApp()
authGenState := tApp.NewAuthGenStateFromAccounts(addrs, []sdk.Coins{cs(c("token1", 100), c("token2", 100))}) authGenState := app.NewAuthGenState(addrs, []sdk.Coins{cs(c("token1", 100), c("token2", 100))})
tApp.InitializeFromGenesisStates(authGenState) tApp.InitializeFromGenesisStates(authGenState)
ctx := tApp.NewContext(true, abci.Header{}) ctx := tApp.NewContext(true, abci.Header{})

View File

@ -19,7 +19,7 @@ func TestKeeper_ForwardAuction(t *testing.T) {
buyer := addrs[1] buyer := addrs[1]
tApp := app.NewTestApp() tApp := app.NewTestApp()
authGenState := tApp.NewAuthGenStateFromAccounts(addrs, []sdk.Coins{cs(c("token1", 100), c("token2", 100)), cs(c("token1", 100), c("token2", 100))}) authGenState := app.NewAuthGenState(addrs, []sdk.Coins{cs(c("token1", 100), c("token2", 100)), cs(c("token1", 100), c("token2", 100))})
tApp.InitializeFromGenesisStates(authGenState) tApp.InitializeFromGenesisStates(authGenState)
ctx := tApp.NewContext(false, abci.Header{}) ctx := tApp.NewContext(false, abci.Header{})
@ -52,7 +52,7 @@ func TestKeeper_ReverseAuction(t *testing.T) {
buyer := addrs[1] buyer := addrs[1]
tApp := app.NewTestApp() tApp := app.NewTestApp()
authGenState := tApp.NewAuthGenStateFromAccounts(addrs, []sdk.Coins{cs(c("token1", 100), c("token2", 100)), cs(c("token1", 100), c("token2", 100))}) authGenState := app.NewAuthGenState(addrs, []sdk.Coins{cs(c("token1", 100), c("token2", 100)), cs(c("token1", 100), c("token2", 100))})
tApp.InitializeFromGenesisStates(authGenState) tApp.InitializeFromGenesisStates(authGenState)
ctx := tApp.NewContext(false, abci.Header{}) ctx := tApp.NewContext(false, abci.Header{})
@ -86,7 +86,7 @@ func TestKeeper_ForwardReverseAuction(t *testing.T) {
recipient := addrs[2] recipient := addrs[2]
tApp := app.NewTestApp() tApp := app.NewTestApp()
authGenState := tApp.NewAuthGenStateFromAccounts(addrs, []sdk.Coins{cs(c("token1", 100), c("token2", 100)), cs(c("token1", 100), c("token2", 100)), cs(c("token1", 100), c("token2", 100))}) authGenState := app.NewAuthGenState(addrs, []sdk.Coins{cs(c("token1", 100), c("token2", 100)), cs(c("token1", 100), c("token2", 100)), cs(c("token1", 100), c("token2", 100))})
tApp.InitializeFromGenesisStates(authGenState) tApp.InitializeFromGenesisStates(authGenState)
ctx := tApp.NewContext(false, abci.Header{}) ctx := tApp.NewContext(false, abci.Header{})

View File

@ -20,7 +20,7 @@ func TestApp_CreateModifyDeleteCDP(t *testing.T) {
testAddr := addrs[0] testAddr := addrs[0]
testPrivKey := privKeys[0] testPrivKey := privKeys[0]
tApp.InitializeFromGenesisStates( tApp.InitializeFromGenesisStates(
tApp.NewAuthGenStateFromAccounts(addrs, []sdk.Coins{cs(c("xrp", 100))}), app.NewAuthGenState(addrs, []sdk.Coins{cs(c("xrp", 100))}),
) )
ctx := tApp.NewContext(false, abci.Header{}) ctx := tApp.NewContext(false, abci.Header{})
// check balance // check balance

View File

@ -35,7 +35,7 @@ func TestKeeper_AddSubtractGetCoins(t *testing.T) {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
// setup app with an account // setup app with an account
tApp := app.NewTestApp() tApp := app.NewTestApp()
authGenState := tApp.NewAuthGenStateFromAccounts([]sdk.AccAddress{normalAddr}, []sdk.Coins{cs(c("usdx", 100), c("kava", 100))}) authGenState := app.NewAuthGenState([]sdk.AccAddress{normalAddr}, []sdk.Coins{cs(c("usdx", 100), c("kava", 100))})
tApp.InitializeFromGenesisStates(authGenState) tApp.InitializeFromGenesisStates(authGenState)
// create a new context and setup the liquidator account // create a new context and setup the liquidator account

View File

@ -97,7 +97,7 @@ func TestKeeper_ModifyCDP(t *testing.T) {
// setup keeper // setup keeper
tApp := app.NewTestApp() tApp := app.NewTestApp()
// initialize cdp owner account with coins // initialize cdp owner account with coins
authGen := tApp.NewAuthGenStateFromAccounts([]sdk.AccAddress{ownerAddr}, []sdk.Coins{tc.priorState.OwnerCoins}) authGen := app.NewAuthGenState([]sdk.AccAddress{ownerAddr}, []sdk.Coins{tc.priorState.OwnerCoins})
tApp.InitializeFromGenesisStates(authGen) tApp.InitializeFromGenesisStates(authGen)
// create a context for db access // create a context for db access
ctx := tApp.NewContext(false, abci.Header{}) ctx := tApp.NewContext(false, abci.Header{})
@ -172,7 +172,7 @@ func TestKeeper_PartialSeizeCDP(t *testing.T) {
testAddr := addrs[0] testAddr := addrs[0]
tApp := app.NewTestApp() tApp := app.NewTestApp()
authGenState := tApp.NewAuthGenStateFromAccounts(addrs, []sdk.Coins{cs(c(collateral, 100))}) authGenState := app.NewAuthGenState(addrs, []sdk.Coins{cs(c(collateral, 100))})
tApp.InitializeFromGenesisStates(authGenState) tApp.InitializeFromGenesisStates(authGenState)
ctx := tApp.NewContext(false, abci.Header{}) ctx := tApp.NewContext(false, abci.Header{})

View File

@ -19,7 +19,7 @@ func TestKeeper_SeizeAndStartCollateralAuction(t *testing.T) {
tApp := app.NewTestApp() tApp := app.NewTestApp()
tApp.InitializeFromGenesisStates( tApp.InitializeFromGenesisStates(
tApp.NewAuthGenStateFromAccounts(addrs, []sdk.Coins{cs(c("btc", 100))}), app.NewAuthGenState(addrs, []sdk.Coins{cs(c("btc", 100))}),
NewPFGenState("btc", sdk.MustNewDecFromStr("8000.00")), NewPFGenState("btc", sdk.MustNewDecFromStr("8000.00")),
NewCDPGenState(), NewCDPGenState(),
NewLiquidatorGenState(), NewLiquidatorGenState(),
@ -80,7 +80,7 @@ func TestKeeper_partialSeizeCDP(t *testing.T) {
tApp := app.NewTestApp() tApp := app.NewTestApp()
tApp.InitializeFromGenesisStates( tApp.InitializeFromGenesisStates(
tApp.NewAuthGenStateFromAccounts(addrs, []sdk.Coins{cs(c("btc", 100))}), app.NewAuthGenState(addrs, []sdk.Coins{cs(c("btc", 100))}),
NewPFGenState("btc", sdk.MustNewDecFromStr("8000.00")), NewPFGenState("btc", sdk.MustNewDecFromStr("8000.00")),
NewCDPGenState(), NewCDPGenState(),
NewLiquidatorGenState(), NewLiquidatorGenState(),