github.com/Finschia/finschia-sdk@v0.48.1/server/mock/app_test.go (about)

     1  package mock
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  	abci "github.com/tendermint/tendermint/abci/types"
     8  	tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
     9  
    10  	ocabci "github.com/Finschia/ostracon/abci/types"
    11  	"github.com/Finschia/ostracon/types"
    12  )
    13  
    14  // TestInitApp makes sure we can initialize this thing without an error
    15  func TestInitApp(t *testing.T) {
    16  	// set up an app
    17  	app, closer, err := SetupApp()
    18  
    19  	// closer may need to be run, even when error in later stage
    20  	if closer != nil {
    21  		defer closer()
    22  	}
    23  	require.NoError(t, err)
    24  
    25  	// initialize it future-way
    26  	appState, err := AppGenState(nil, types.GenesisDoc{}, nil)
    27  	require.NoError(t, err)
    28  
    29  	// TODO test validators in the init chain?
    30  	req := abci.RequestInitChain{
    31  		AppStateBytes: appState,
    32  	}
    33  	app.InitChain(req)
    34  	app.Commit()
    35  
    36  	// make sure we can query these values
    37  	query := abci.RequestQuery{
    38  		Path: "/store/main/key",
    39  		Data: []byte("foo"),
    40  	}
    41  	qres := app.Query(query)
    42  	require.Equal(t, uint32(0), qres.Code, qres.Log)
    43  	require.Equal(t, []byte("bar"), qres.Value)
    44  }
    45  
    46  // TextDeliverTx ensures we can write a tx
    47  func TestDeliverTx(t *testing.T) {
    48  	// set up an app
    49  	app, closer, err := SetupApp()
    50  	// closer may need to be run, even when error in later stage
    51  	if closer != nil {
    52  		defer closer()
    53  	}
    54  	require.NoError(t, err)
    55  
    56  	key := "my-special-key"
    57  	value := "top-secret-data!!"
    58  	tx := NewTx(key, value)
    59  	txBytes := tx.GetSignBytes()
    60  
    61  	header := tmproto.Header{
    62  		AppHash: []byte("apphash"),
    63  		Height:  1,
    64  	}
    65  	app.BeginBlock(ocabci.RequestBeginBlock{Header: header})
    66  	dres := app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
    67  	require.Equal(t, uint32(0), dres.Code, dres.Log)
    68  	app.EndBlock(abci.RequestEndBlock{})
    69  	cres := app.Commit()
    70  	require.NotEmpty(t, cres.Data)
    71  
    72  	// make sure we can query these values
    73  	query := abci.RequestQuery{
    74  		Path: "/store/main/key",
    75  		Data: []byte(key),
    76  	}
    77  	qres := app.Query(query)
    78  	require.Equal(t, uint32(0), qres.Code, qres.Log)
    79  	require.Equal(t, []byte(value), qres.Value)
    80  }