cosmos-sdk/docs/examples/basecoin/app/app_test.go
gamarin2 addcfbf5cb Documentation Structure Change and Cleanup (#2808)
* Update docs/sdk/clients.md
* organize ADR directory like tendermint
* docs: move spec-proposals into spec/
* remove lotion, moved to website repo
* move getting-started to cosmos-hub, and voyager to website
* docs: move lite/ into clients/lite/
* move introduction/ content to website repo
* move resources/ content to website repo
* mv sdk/clients.md to clients/clients.md
* mv validators to cosmos-hub/validators
* move deprecated sdk/ content to _attic
* sdk/modules.md is duplicate with modules/README.md
* consolidate remianing sdk/ files into a single sdk.md
* move examples/ to docs/examples/
* mv docs/cosmos-hub to docs/gaia
* Add keys/accounts section to localnet docs
2018-11-14 11:44:17 -08:00

82 lines
2.5 KiB
Go

package app
import (
"os"
"testing"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/docs/examples/basecoin/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/ed25519"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
)
func setGenesis(baseApp *BasecoinApp, accounts ...*types.AppAccount) (types.GenesisState, error) {
genAccts := make([]*types.GenesisAccount, len(accounts))
for i, appAct := range accounts {
genAccts[i] = types.NewGenesisAccount(appAct)
}
genesisState := types.GenesisState{Accounts: genAccts}
stateBytes, err := codec.MarshalJSONIndent(baseApp.cdc, genesisState)
if err != nil {
return types.GenesisState{}, err
}
// initialize and commit the chain
baseApp.InitChain(abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{}, AppStateBytes: stateBytes,
})
baseApp.Commit()
return genesisState, nil
}
func TestGenesis(t *testing.T) {
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "sdk/app")
db := dbm.NewMemDB()
baseApp := NewBasecoinApp(logger, db)
// construct a pubkey and an address for the test account
pubkey := ed25519.GenPrivKey().PubKey()
addr := sdk.AccAddress(pubkey.Address())
// construct some test coins
coins, err := sdk.ParseCoins("77foocoin,99barcoin")
require.Nil(t, err)
// create an auth.BaseAccount for the given test account and set it's coins
baseAcct := auth.NewBaseAccountWithAddress(addr)
err = baseAcct.SetCoins(coins)
require.Nil(t, err)
// create a new test AppAccount with the given auth.BaseAccount
appAcct := types.NewAppAccount("foobar", baseAcct)
genState, err := setGenesis(baseApp, appAcct)
require.Nil(t, err)
// create a context for the BaseApp
ctx := baseApp.BaseApp.NewContext(true, abci.Header{})
res := baseApp.accountKeeper.GetAccount(ctx, baseAcct.Address)
require.Equal(t, appAcct, res)
// reload app and ensure the account is still there
baseApp = NewBasecoinApp(logger, db)
stateBytes, err := codec.MarshalJSONIndent(baseApp.cdc, genState)
require.Nil(t, err)
// initialize the chain with the expected genesis state
baseApp.InitChain(abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{}, AppStateBytes: stateBytes,
})
ctx = baseApp.BaseApp.NewContext(true, abci.Header{})
res = baseApp.accountKeeper.GetAccount(ctx, baseAcct.Address)
require.Equal(t, appAcct, res)
}