From cdd33aa04b5ebf1cdbb4c1479bd97ae39f18c5d1 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 21 Feb 2018 16:28:18 +0100 Subject: [PATCH] mock app tests now work --- mock/app_test.go | 38 ++++++++++++++++++++++++++++++++++++++ mock/tx.go | 12 ++++++++++++ 2 files changed, 50 insertions(+) diff --git a/mock/app_test.go b/mock/app_test.go index 782362827..f21abd594 100644 --- a/mock/app_test.go +++ b/mock/app_test.go @@ -33,4 +33,42 @@ func TestInitApp(t *testing.T) { qres := app.Query(query) require.Equal(t, uint32(0), qres.Code, qres.Log) assert.Equal(t, []byte("bar"), qres.Value) + +} + +// TextDeliverTx ensures we can write a tx +func TestDeliverTx(t *testing.T) { + // set up an app + app, closer, err := SetupApp() + // closer may need to be run, even when error in later stage + if closer != nil { + defer closer() + } + require.NoError(t, err) + + key := "my-special-key" + value := "top-secret-data!!" + tx := NewTx(key, value) + txBytes := tx.GetSignBytes() + + header := abci.Header{ + AppHash: []byte("apphash"), + Height: 1, + } + app.BeginBlock(abci.RequestBeginBlock{Header: header}) + dres := app.DeliverTx(txBytes) + require.Equal(t, uint32(0), dres.Code, dres.Log) + app.EndBlock(abci.RequestEndBlock{}) + cres := app.Commit() + require.NotEmpty(t, cres.Data) + + // make sure we can query these values + query := abci.RequestQuery{ + Path: "/main/key", + Data: []byte(key), + } + qres := app.Query(query) + require.Equal(t, uint32(0), qres.Code, qres.Log) + assert.Equal(t, []byte(value), qres.Value) + } diff --git a/mock/tx.go b/mock/tx.go index 077c090e6..970549d2c 100644 --- a/mock/tx.go +++ b/mock/tx.go @@ -2,6 +2,7 @@ package mock import ( "bytes" + "fmt" sdk "github.com/cosmos/cosmos-sdk/types" crypto "github.com/tendermint/go-crypto" @@ -14,6 +15,17 @@ type kvstoreTx struct { bytes []byte } +var _ sdk.Tx = kvstoreTx{} + +func NewTx(key, value string) kvstoreTx { + bytes := fmt.Sprintf("%s=%s", key, value) + return kvstoreTx{ + key: []byte(key), + value: []byte(value), + bytes: []byte(bytes), + } +} + func (tx kvstoreTx) Get(key interface{}) (value interface{}) { switch k := key.(type) { case string: