github.com/vipernet-xyz/tm@v0.34.24/rpc/client/mock/client.go (about)

     1  package mock
     2  
     3  /*
     4  package mock returns a Client implementation that
     5  accepts various (mock) implementations of the various methods.
     6  
     7  This implementation is useful for using in tests, when you don't
     8  need a real server, but want a high-level of control about
     9  the server response you want to mock (eg. error handling),
    10  or if you just want to record the calls to verify in your tests.
    11  
    12  For real clients, you probably want the "http" package.  If you
    13  want to directly call a tendermint node in process, you can use the
    14  "local" package.
    15  */
    16  
    17  import (
    18  	"context"
    19  	"reflect"
    20  
    21  	"github.com/vipernet-xyz/tm/libs/bytes"
    22  	"github.com/vipernet-xyz/tm/libs/service"
    23  	"github.com/vipernet-xyz/tm/rpc/client"
    24  	"github.com/vipernet-xyz/tm/rpc/core"
    25  	ctypes "github.com/vipernet-xyz/tm/rpc/core/types"
    26  	rpctypes "github.com/vipernet-xyz/tm/rpc/jsonrpc/types"
    27  	"github.com/vipernet-xyz/tm/types"
    28  )
    29  
    30  // Client wraps arbitrary implementations of the various interfaces.
    31  //
    32  // We provide a few choices to mock out each one in this package.
    33  // Nothing hidden here, so no New function, just construct it from
    34  // some parts, and swap them out them during the tests.
    35  type Client struct {
    36  	client.ABCIClient
    37  	client.SignClient
    38  	client.HistoryClient
    39  	client.StatusClient
    40  	client.EventsClient
    41  	client.EvidenceClient
    42  	client.MempoolClient
    43  	service.Service
    44  }
    45  
    46  var _ client.Client = Client{}
    47  
    48  // Call is used by recorders to save a call and response.
    49  // It can also be used to configure mock responses.
    50  type Call struct {
    51  	Name     string
    52  	Args     interface{}
    53  	Response interface{}
    54  	Error    error
    55  }
    56  
    57  // GetResponse will generate the apporiate response for us, when
    58  // using the Call struct to configure a Mock handler.
    59  //
    60  // When configuring a response, if only one of Response or Error is
    61  // set then that will always be returned. If both are set, then
    62  // we return Response if the Args match the set args, Error otherwise.
    63  func (c Call) GetResponse(args interface{}) (interface{}, error) {
    64  	// handle the case with no response
    65  	if c.Response == nil {
    66  		if c.Error == nil {
    67  			panic("Misconfigured call, you must set either Response or Error")
    68  		}
    69  		return nil, c.Error
    70  	}
    71  	// response without error
    72  	if c.Error == nil {
    73  		return c.Response, nil
    74  	}
    75  	// have both, we must check args....
    76  	if reflect.DeepEqual(args, c.Args) {
    77  		return c.Response, nil
    78  	}
    79  	return nil, c.Error
    80  }
    81  
    82  func (c Client) Status(ctx context.Context) (*ctypes.ResultStatus, error) {
    83  	return core.Status(&rpctypes.Context{})
    84  }
    85  
    86  func (c Client) ABCIInfo(ctx context.Context) (*ctypes.ResultABCIInfo, error) {
    87  	return core.ABCIInfo(&rpctypes.Context{})
    88  }
    89  
    90  func (c Client) ABCIQuery(ctx context.Context, path string, data bytes.HexBytes) (*ctypes.ResultABCIQuery, error) {
    91  	return c.ABCIQueryWithOptions(ctx, path, data, client.DefaultABCIQueryOptions)
    92  }
    93  
    94  func (c Client) ABCIQueryWithOptions(
    95  	ctx context.Context,
    96  	path string,
    97  	data bytes.HexBytes,
    98  	opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
    99  	return core.ABCIQuery(&rpctypes.Context{}, path, data, opts.Height, opts.Prove)
   100  }
   101  
   102  func (c Client) BroadcastTxCommit(ctx context.Context, tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
   103  	return core.BroadcastTxCommit(&rpctypes.Context{}, tx)
   104  }
   105  
   106  func (c Client) BroadcastTxAsync(ctx context.Context, tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
   107  	return core.BroadcastTxAsync(&rpctypes.Context{}, tx)
   108  }
   109  
   110  func (c Client) BroadcastTxSync(ctx context.Context, tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
   111  	return core.BroadcastTxSync(&rpctypes.Context{}, tx)
   112  }
   113  
   114  func (c Client) CheckTx(ctx context.Context, tx types.Tx) (*ctypes.ResultCheckTx, error) {
   115  	return core.CheckTx(&rpctypes.Context{}, tx)
   116  }
   117  
   118  func (c Client) NetInfo(ctx context.Context) (*ctypes.ResultNetInfo, error) {
   119  	return core.NetInfo(&rpctypes.Context{})
   120  }
   121  
   122  func (c Client) ConsensusState(ctx context.Context) (*ctypes.ResultConsensusState, error) {
   123  	return core.ConsensusState(&rpctypes.Context{})
   124  }
   125  
   126  func (c Client) DumpConsensusState(ctx context.Context) (*ctypes.ResultDumpConsensusState, error) {
   127  	return core.DumpConsensusState(&rpctypes.Context{})
   128  }
   129  
   130  func (c Client) ConsensusParams(ctx context.Context, height *int64) (*ctypes.ResultConsensusParams, error) {
   131  	return core.ConsensusParams(&rpctypes.Context{}, height)
   132  }
   133  
   134  func (c Client) Health(ctx context.Context) (*ctypes.ResultHealth, error) {
   135  	return core.Health(&rpctypes.Context{})
   136  }
   137  
   138  func (c Client) DialSeeds(ctx context.Context, seeds []string) (*ctypes.ResultDialSeeds, error) {
   139  	return core.UnsafeDialSeeds(&rpctypes.Context{}, seeds)
   140  }
   141  
   142  func (c Client) DialPeers(
   143  	ctx context.Context,
   144  	peers []string,
   145  	persistent,
   146  	unconditional,
   147  	private bool,
   148  ) (*ctypes.ResultDialPeers, error) {
   149  	return core.UnsafeDialPeers(&rpctypes.Context{}, peers, persistent, unconditional, private)
   150  }
   151  
   152  func (c Client) BlockchainInfo(ctx context.Context, minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) {
   153  	return core.BlockchainInfo(&rpctypes.Context{}, minHeight, maxHeight)
   154  }
   155  
   156  func (c Client) Genesis(ctx context.Context) (*ctypes.ResultGenesis, error) {
   157  	return core.Genesis(&rpctypes.Context{})
   158  }
   159  
   160  func (c Client) Block(ctx context.Context, height *int64) (*ctypes.ResultBlock, error) {
   161  	return core.Block(&rpctypes.Context{}, height)
   162  }
   163  
   164  func (c Client) BlockByHash(ctx context.Context, hash []byte) (*ctypes.ResultBlock, error) {
   165  	return core.BlockByHash(&rpctypes.Context{}, hash)
   166  }
   167  
   168  func (c Client) Commit(ctx context.Context, height *int64) (*ctypes.ResultCommit, error) {
   169  	return core.Commit(&rpctypes.Context{}, height)
   170  }
   171  
   172  func (c Client) Validators(ctx context.Context, height *int64, page, perPage *int) (*ctypes.ResultValidators, error) {
   173  	return core.Validators(&rpctypes.Context{}, height, page, perPage)
   174  }
   175  
   176  func (c Client) BroadcastEvidence(ctx context.Context, ev types.Evidence) (*ctypes.ResultBroadcastEvidence, error) {
   177  	return core.BroadcastEvidence(&rpctypes.Context{}, ev)
   178  }