From 89b79e5d5236ddb36b36366c98eb180e800cf456 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Fri, 8 Jan 2021 17:43:47 +0100 Subject: [PATCH] Pull out some test helpers for genesis_test --- x/wasm/common_test.go | 35 +++++++++++++++++++++++++++++++++++ x/wasm/genesis_test.go | 16 +++++----------- 2 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 x/wasm/common_test.go diff --git a/x/wasm/common_test.go b/x/wasm/common_test.go new file mode 100644 index 0000000000..e7519e4cd9 --- /dev/null +++ b/x/wasm/common_test.go @@ -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 +} diff --git a/x/wasm/genesis_test.go b/x/wasm/genesis_test.go index e40d50b86d..42c621e5f4 100644 --- a/x/wasm/genesis_test.go +++ b/x/wasm/genesis_test.go @@ -22,7 +22,7 @@ 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() @@ -30,10 +30,7 @@ func TestInitGenesis(t *testing.T) { 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{ @@ -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(), @@ -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)