Skip to content

Commit

Permalink
Merge pull request #23 from sikkatech/genesis_tests
Browse files Browse the repository at this point in the history
Add genesis tests
  • Loading branch information
ethanfrey authored Jan 14, 2020
2 parents d365da2 + 9d39f3a commit eb78f8c
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
93 changes: 93 additions & 0 deletions x/wasm/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package wasm

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
)

func TestInitGenesis(t *testing.T) {
data, cleanup := setupTest(t)
defer cleanup()

deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
topUp := sdk.NewCoins(sdk.NewInt64Coin("denom", 5000))
creator := createFakeFundedAccount(data.ctx, data.acctKeeper, deposit.Add(deposit))
fred := createFakeFundedAccount(data.ctx, data.acctKeeper, topUp)

h := data.module.NewHandler()
q := data.module.NewQuerierHandler()

msg := MsgStoreCode{
Sender: creator,
WASMByteCode: testContract,
}
res := h(data.ctx, msg)
require.True(t, res.IsOK())
require.Equal(t, res.Data, []byte("1"))

_, _, bob := keyPubAddr()
initMsg := initMsg{
Verifier: fred.String(),
Beneficiary: bob.String(),
}
initMsgBz, err := json.Marshal(initMsg)
require.NoError(t, err)

initCmd := MsgInstantiateContract{
Sender: creator,
Code: 1,
InitMsg: initMsgBz,
InitFunds: deposit,
}
res = h(data.ctx, initCmd)
require.True(t, res.IsOK())
contractAddr := sdk.AccAddress(res.Data)

execCmd := MsgExecuteContract{
Sender: fred,
Contract: contractAddr,
Msg: []byte("{}"),
SentFunds: topUp,
}
res = h(data.ctx, execCmd)
require.True(t, res.IsOK())

// ensure all contract state is as after init
assertCodeList(t, q, data.ctx, 1)
assertCodeBytes(t, q, data.ctx, 1, testContract)

assertContractList(t, q, data.ctx, []string{contractAddr.String()})
assertContractInfo(t, q, data.ctx, contractAddr, 1, creator)
assertContractState(t, q, data.ctx, contractAddr, state{
Verifier: fred.String(),
Beneficiary: bob.String(),
Funder: creator.String(),
})

// export into genstate
genState := ExportGenesis(data.ctx, data.keeper)

// create new app to import genstate into
newData, newCleanup := setupTest(t)
defer newCleanup()
q2 := newData.module.NewQuerierHandler()

// initialize new app with genstate
InitGenesis(newData.ctx, newData.keeper, genState)

// run same checks again on newdata, to make sure it was reinitialized correctly
assertCodeList(t, q2, newData.ctx, 1)
assertCodeBytes(t, q2, newData.ctx, 1, testContract)

assertContractList(t, q2, newData.ctx, []string{contractAddr.String()})
assertContractInfo(t, q2, newData.ctx, contractAddr, 1, creator)
assertContractState(t, q2, newData.ctx, contractAddr, state{
Verifier: fred.String(),
Beneficiary: bob.String(),
Funder: creator.String(),
})
}
2 changes: 2 additions & 0 deletions x/wasm/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type testData struct {
module module.AppModule
ctx sdk.Context
acctKeeper auth.AccountKeeper
keeper Keeper
}

// returns a cleanup function, which must be defered on
Expand All @@ -35,6 +36,7 @@ func setupTest(t *testing.T) (testData, func()) {
module: NewAppModule(keeper),
ctx: ctx,
acctKeeper: acctKeeper,
keeper: keeper,
}
cleanup := func() { os.RemoveAll(tempDir) }
return data, cleanup
Expand Down

0 comments on commit eb78f8c

Please sign in to comment.