You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.5 KiB

  1. package mock_test
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/pkg/errors"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "github.com/tendermint/abci/example/dummy"
  9. abci "github.com/tendermint/abci/types"
  10. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  11. "github.com/tendermint/tendermint/types"
  12. "github.com/tendermint/tendermint/rpc/client/mock"
  13. )
  14. func TestABCIMock(t *testing.T) {
  15. assert, require := assert.New(t), require.New(t)
  16. key, value := []byte("foo"), []byte("bar")
  17. height := uint64(10)
  18. goodTx := types.Tx{0x01, 0xff}
  19. badTx := types.Tx{0x12, 0x21}
  20. m := mock.ABCIMock{
  21. Info: mock.Call{Error: errors.New("foobar")},
  22. Query: mock.Call{Response: abci.ResponseQuery{
  23. Key: key,
  24. Value: value,
  25. Height: height,
  26. }},
  27. // Broadcast commit depends on call
  28. BroadcastCommit: mock.Call{
  29. Args: goodTx,
  30. Response: &ctypes.ResultBroadcastTxCommit{
  31. CheckTx: &abci.ResponseCheckTx{Data: []byte("stand")},
  32. DeliverTx: &abci.ResponseDeliverTx{Data: []byte("deliver")},
  33. },
  34. Error: errors.New("bad tx"),
  35. },
  36. Broadcast: mock.Call{Error: errors.New("must commit")},
  37. }
  38. // now, let's try to make some calls
  39. _, err := m.ABCIInfo()
  40. require.NotNil(err)
  41. assert.Equal("foobar", err.Error())
  42. // query always returns the response
  43. query, err := m.ABCIQuery("/", nil, false)
  44. require.Nil(err)
  45. require.NotNil(query)
  46. assert.Equal(key, query.Response.GetKey())
  47. assert.Equal(value, query.Response.GetValue())
  48. assert.Equal(height, query.Response.GetHeight())
  49. // non-commit calls always return errors
  50. _, err = m.BroadcastTxSync(goodTx)
  51. require.NotNil(err)
  52. assert.Equal("must commit", err.Error())
  53. _, err = m.BroadcastTxAsync(goodTx)
  54. require.NotNil(err)
  55. assert.Equal("must commit", err.Error())
  56. // commit depends on the input
  57. _, err = m.BroadcastTxCommit(badTx)
  58. require.NotNil(err)
  59. assert.Equal("bad tx", err.Error())
  60. bres, err := m.BroadcastTxCommit(goodTx)
  61. require.Nil(err, "%+v", err)
  62. assert.EqualValues(0, bres.CheckTx.Code)
  63. assert.EqualValues("stand", bres.CheckTx.Data)
  64. assert.EqualValues("deliver", bres.DeliverTx.Data)
  65. }
  66. func TestABCIRecorder(t *testing.T) {
  67. assert, require := assert.New(t), require.New(t)
  68. m := mock.ABCIMock{
  69. Info: mock.Call{Response: abci.ResponseInfo{
  70. Data: "data",
  71. Version: "v0.9.9",
  72. }},
  73. Query: mock.Call{Error: errors.New("query")},
  74. Broadcast: mock.Call{Error: errors.New("broadcast")},
  75. BroadcastCommit: mock.Call{Error: errors.New("broadcast_commit")},
  76. }
  77. r := mock.NewABCIRecorder(m)
  78. require.Equal(0, len(r.Calls))
  79. r.ABCIInfo()
  80. r.ABCIQuery("path", []byte("data"), true)
  81. require.Equal(2, len(r.Calls))
  82. info := r.Calls[0]
  83. assert.Equal("abci_info", info.Name)
  84. assert.Nil(info.Error)
  85. assert.Nil(info.Args)
  86. require.NotNil(info.Response)
  87. ir, ok := info.Response.(*ctypes.ResultABCIInfo)
  88. require.True(ok)
  89. assert.Equal("data", ir.Response.Data)
  90. assert.Equal("v0.9.9", ir.Response.Version)
  91. query := r.Calls[1]
  92. assert.Equal("abci_query", query.Name)
  93. assert.Nil(query.Response)
  94. require.NotNil(query.Error)
  95. assert.Equal("query", query.Error.Error())
  96. require.NotNil(query.Args)
  97. qa, ok := query.Args.(mock.QueryArgs)
  98. require.True(ok)
  99. assert.Equal("path", qa.Path)
  100. assert.EqualValues("data", qa.Data)
  101. assert.True(qa.Prove)
  102. // now add some broadcasts
  103. txs := []types.Tx{{1}, {2}, {3}}
  104. r.BroadcastTxCommit(txs[0])
  105. r.BroadcastTxSync(txs[1])
  106. r.BroadcastTxAsync(txs[2])
  107. require.Equal(5, len(r.Calls))
  108. bc := r.Calls[2]
  109. assert.Equal("broadcast_tx_commit", bc.Name)
  110. assert.Nil(bc.Response)
  111. require.NotNil(bc.Error)
  112. assert.EqualValues(bc.Args, txs[0])
  113. bs := r.Calls[3]
  114. assert.Equal("broadcast_tx_sync", bs.Name)
  115. assert.Nil(bs.Response)
  116. require.NotNil(bs.Error)
  117. assert.EqualValues(bs.Args, txs[1])
  118. ba := r.Calls[4]
  119. assert.Equal("broadcast_tx_async", ba.Name)
  120. assert.Nil(ba.Response)
  121. require.NotNil(ba.Error)
  122. assert.EqualValues(ba.Args, txs[2])
  123. }
  124. func TestABCIApp(t *testing.T) {
  125. assert, require := assert.New(t), require.New(t)
  126. app := dummy.NewDummyApplication()
  127. m := mock.ABCIApp{app}
  128. // get some info
  129. info, err := m.ABCIInfo()
  130. require.Nil(err)
  131. assert.Equal(`{"size":0}`, info.Response.GetData())
  132. // add a key
  133. key, value := "foo", "bar"
  134. tx := fmt.Sprintf("%s=%s", key, value)
  135. res, err := m.BroadcastTxCommit(types.Tx(tx))
  136. require.Nil(err)
  137. assert.True(res.CheckTx.Code.IsOK())
  138. require.NotNil(res.DeliverTx)
  139. assert.True(res.DeliverTx.Code.IsOK())
  140. // check the key
  141. qres, err := m.ABCIQuery("/key", []byte(key), false)
  142. require.Nil(err)
  143. assert.EqualValues(value, qres.Response.Value)
  144. }