github.com/koko1123/flow-go-1@v0.29.6/engine/collection/rpc/engine_test.go (about)

     1  package rpc
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/onflow/flow/protobuf/go/flow/access"
    12  
    13  	rpcmock "github.com/koko1123/flow-go-1/engine/collection/rpc/mock"
    14  	"github.com/koko1123/flow-go-1/engine/common/rpc/convert"
    15  	"github.com/koko1123/flow-go-1/model/flow"
    16  	"github.com/koko1123/flow-go-1/utils/unittest"
    17  )
    18  
    19  func TestSubmitTransaction(t *testing.T) {
    20  	backend := new(rpcmock.Backend)
    21  
    22  	h := handler{
    23  		chainID: flow.Testnet,
    24  		backend: backend,
    25  	}
    26  
    27  	tx := unittest.TransactionBodyFixture()
    28  
    29  	t.Run("should submit transaction to engine", func(t *testing.T) {
    30  		backend.On("ProcessTransaction", &tx).Return(nil).Once()
    31  
    32  		res, err := h.SendTransaction(context.Background(), &access.SendTransactionRequest{
    33  			Transaction: convert.TransactionToMessage(tx),
    34  		})
    35  		require.NoError(t, err)
    36  
    37  		// should submit the transaction to the engine
    38  		backend.AssertCalled(t, "ProcessTransaction", &tx)
    39  
    40  		// should return the fingerprint of the submitted transaction
    41  		assert.Equal(t, tx.ID(), flow.HashToID(res.Id))
    42  	})
    43  
    44  	t.Run("should pass through error", func(t *testing.T) {
    45  		expected := errors.New("error")
    46  		backend.On("ProcessTransaction", &tx).Return(expected).Once()
    47  
    48  		res, err := h.SendTransaction(context.Background(), &access.SendTransactionRequest{
    49  			Transaction: convert.TransactionToMessage(tx),
    50  		})
    51  		if assert.Error(t, err) {
    52  			assert.Equal(t, expected, err)
    53  		}
    54  
    55  		// should submit the transaction to the engine
    56  		backend.AssertCalled(t, "ProcessTransaction", &tx)
    57  
    58  		// should only return the error
    59  		assert.Nil(t, res)
    60  	})
    61  }