Skip to content

Commit

Permalink
Pull out some test helpers for genesis_test
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Jan 8, 2021
1 parent 202c195 commit 89b79e5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
35 changes: 35 additions & 0 deletions x/wasm/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package wasm

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"strconv"
"testing"
)

// ensure store code returns the expected response
func assertStoreCodeResponse(t *testing.T, data []byte, expected int64) {
var pStoreResp MsgStoreCodeResponse
require.NoError(t, pStoreResp.Unmarshal(data))
// TODO: change this when it we store int natively
require.Equal(t, pStoreResp.CodeID, strconv.FormatInt(expected, 10))
}

// ensure execution returns the expected data
func assertExecuteResponse(t *testing.T, data []byte, expected []byte) {
var pExecResp MsgExecuteContractResponse
require.NoError(t, pExecResp.Unmarshal(data))
require.Equal(t, pExecResp.Data, expected)
}

// ensures this returns a valid bech32 address and returns it
func parseInitResponse(t *testing.T, data []byte) string {
var pInstResp MsgInstantiateContractResponse
require.NoError(t, pInstResp.Unmarshal(data))
require.NotEmpty(t, pInstResp.Address)
addr := pInstResp.Address
// ensure this is a valid sdk address
_, err := sdk.AccAddressFromBech32(addr)
require.NoError(t, err)
return addr
}
16 changes: 5 additions & 11 deletions x/wasm/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@ func TestInitGenesis(t *testing.T) {
msg := MsgStoreCode{
Sender: creator.String(),
WASMByteCode: testContract,
Source: "https://github.com/CosmWasm/wasmd/blob/master/x/wasm/testdata/escrow.wasm",
Source: "https://github.com/CosmWasm/wasmd/blob/master/x/wasm/testdata/hackatom.wasm",
Builder: "confio/cosmwasm-opt:0.7.0",
}
err := msg.ValidateBasic()
require.NoError(t, err)

res, err := h(data.ctx, &msg)
require.NoError(t, err)

var pStoreResp MsgStoreCodeResponse
require.NoError(t, pStoreResp.Unmarshal(res.Data))
require.Equal(t, pStoreResp.CodeID, "1")
assertStoreCodeResponse(t, res.Data, 1)

_, _, bob := keyPubAddr()
initMsg := initMsg{
Expand All @@ -51,9 +48,7 @@ func TestInitGenesis(t *testing.T) {
}
res, err = h(data.ctx, &initCmd)
require.NoError(t, err)
var pInstResp MsgInstantiateContractResponse
require.NoError(t, pInstResp.Unmarshal(res.Data))
contractBech32Addr := pInstResp.Address
contractBech32Addr := parseInitResponse(t, res.Data)

execCmd := MsgExecuteContract{
Sender: fred.String(),
Expand All @@ -63,9 +58,8 @@ func TestInitGenesis(t *testing.T) {
}
res, err = h(data.ctx, &execCmd)
require.NoError(t, err)
var pExecResp MsgExecuteContractResponse
require.NoError(t, pExecResp.Unmarshal(res.Data))
require.NotEmpty(t, pExecResp.Data)
// from https://github.com/CosmWasm/cosmwasm/blob/master/contracts/hackatom/src/contract.rs#L167
assertExecuteResponse(t, res.Data, []byte{0xf0, 0x0b, 0xaa})

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

0 comments on commit 89b79e5

Please sign in to comment.