github.com/blend/go-sdk@v1.20220411.3/redis/mock_client.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package redis
     9  
    10  import "context"
    11  
    12  // MockClientFunc is a function that implements Client.
    13  type MockClientFunc func(context.Context, interface{}, string, ...string) error
    14  
    15  // Do implements Client.Do.
    16  func (mcf MockClientFunc) Do(ctx context.Context, out interface{}, op string, args ...string) error {
    17  	return mcf(ctx, out, op, args...)
    18  }
    19  
    20  // Close is a no-op.
    21  func (mcf MockClientFunc) Close() error { return nil }
    22  
    23  // NewMockClient returns a new mock client with a given capacity.
    24  func NewMockClient(capacity int) *MockClient {
    25  	return &MockClient{
    26  		Ops: make(chan MockClientOp, capacity),
    27  	}
    28  }
    29  
    30  // Assert `MockClient` implements client.
    31  var (
    32  	_ Client = (*MockClient)(nil)
    33  )
    34  
    35  // MockClient is a mocked client.
    36  type MockClient struct {
    37  	Ops chan MockClientOp
    38  }
    39  
    40  // Do applies a command.
    41  func (mc *MockClient) Do(_ context.Context, out interface{}, op string, args ...string) error {
    42  	mc.Ops <- MockClientOp{Out: out, Op: op, Args: args}
    43  	return nil
    44  }
    45  
    46  // Close closes the mock client.
    47  func (mc *MockClient) Close() error { return nil }
    48  
    49  // MockClientOp is a mocked client op.
    50  type MockClientOp struct {
    51  	Out  interface{}
    52  	Op   string
    53  	Args []string
    54  }