mock

package
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

README

rueidis mock

Due to the design of the command builder, it is impossible for users to mock rueidis.Client for testing.

Therefore, rueidis provides an implemented one, based on the gomock, with some helpers to make user writing tests more easily, including command matcher mock.Match, mock.MatchFn and mock.Result for faking redis responses.

Examples

Mock client.Do
package main

import (
	"context"
	"testing"

	"github.com/golang/mock/gomock"
	"github.com/rueian/rueidis/mock"
)

func TestWithRueidis(t *testing.T) {
	ctrl := gomock.NewController(t)
	defer ctrl.Finish()

	ctx := context.Background()
	client := mock.NewClient(ctrl)

	client.EXPECT().Do(ctx, mock.Match("GET", "key")).Return(mock.Result(mock.RedisString("val")))
	if v, _ := client.Do(ctx, client.B().Get().Key("key").Build()).ToString(); v != "val" {
		t.Fatalf("unexpected val %v", v)
	}
	client.EXPECT().DoMulti(ctx, mock.Match("GET", "c"), mock.Match("GET", "d")).Return([]rueidis.RedisResult{
		mock.Result(mock.RedisNil()),
		mock.Result(mock.RedisNil()),
	})
	for _, resp := range client.DoMulti(ctx, client.B().Get().Key("c").Build(), client.B().Get().Key("d").Build()) {
		if err := resp.Error(); !rueidis.IsRedisNil(err) {
			t.Fatalf("unexpected err %v", err)
		}
	}
}
Mock client.Receive
package main

import (
	"context"
	"testing"

	"github.com/golang/mock/gomock"
	"github.com/rueian/rueidis/mock"
)

func TestWithRueidisReceive(t *testing.T) {
	ctrl := gomock.NewController(t)
	defer ctrl.Finish()

	ctx := context.Background()
	client := mock.NewClient(ctrl)

	client.EXPECT().Receive(ctx, mock.Match("SUBSCRIBE", "ch"), gomock.Any()).Do(func(_, _ any, fn func(message rueidis.PubSubMessage)) {
		fn(rueidis.PubSubMessage{Message: "msg"})
	})

	client.Receive(ctx, client.B().Subscribe().Channel("ch").Build(), func(msg rueidis.PubSubMessage) {
		if msg.Message != "msg" {
			t.Fatalf("unexpected val %v", msg.Message)
		}
	})
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorResult

func ErrorResult(err error) rueidis.RedisResult

func Match

func Match(cmd ...string) gomock.Matcher

func MatchFn

func MatchFn(fn func(cmd []string) bool, description ...string) gomock.Matcher

func RedisArray

func RedisArray(values ...rueidis.RedisMessage) rueidis.RedisMessage

func RedisBool

func RedisBool(v bool) rueidis.RedisMessage

func RedisError

func RedisError(v string) rueidis.RedisMessage

func RedisFloat64

func RedisFloat64(v float64) rueidis.RedisMessage

func RedisInt64

func RedisInt64(v int64) rueidis.RedisMessage

func RedisNil

func RedisNil() rueidis.RedisMessage

func RedisString

func RedisString(v string) rueidis.RedisMessage

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a mock of Client interface.

func NewClient

func NewClient(ctrl *gomock.Controller, options ...ClientOption) *Client

NewClient creates a new mock instance.

func (*Client) B

func (m *Client) B() cmds.Builder

B mocks base method.

func (*Client) Close

func (m *Client) Close()

Close mocks base method.

func (*Client) Dedicate

func (m *Client) Dedicate() (rueidis.DedicatedClient, func())

Dedicate mocks base method.

func (*Client) Dedicated

func (m *Client) Dedicated(arg0 func(rueidis.DedicatedClient) error) error

Dedicated mocks base method.

func (*Client) Do

Do mocks base method.

func (*Client) DoCache

func (m *Client) DoCache(arg0 context.Context, arg1 cmds.Cacheable, arg2 time.Duration) rueidis.RedisResult

DoCache mocks base method.

func (*Client) DoMulti

func (m *Client) DoMulti(arg0 context.Context, arg1 ...cmds.Completed) []rueidis.RedisResult

DoMulti mocks base method.

func (*Client) DoMultiCache

func (m *Client) DoMultiCache(arg0 context.Context, arg1 ...rueidis.CacheableTTL) []rueidis.RedisResult

DoMultiCache mocks base method.

func (*Client) EXPECT

func (m *Client) EXPECT() *ClientMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*Client) Nodes

func (m *Client) Nodes() map[string]rueidis.Client

Nodes mocks base method.

func (*Client) Receive

func (m *Client) Receive(arg0 context.Context, arg1 cmds.Completed, arg2 func(rueidis.PubSubMessage)) error

Receive mocks base method.

type ClientMockRecorder

type ClientMockRecorder struct {
	// contains filtered or unexported fields
}

ClientMockRecorder is the mock recorder for Client.

func (*ClientMockRecorder) Close

func (mr *ClientMockRecorder) Close() *gomock.Call

Close indicates an expected call of Close.

func (*ClientMockRecorder) Dedicate

func (mr *ClientMockRecorder) Dedicate() *gomock.Call

Dedicate indicates an expected call of Dedicate.

func (*ClientMockRecorder) Dedicated

func (mr *ClientMockRecorder) Dedicated(arg0 any) *gomock.Call

Dedicated indicates an expected call of Dedicated.

func (*ClientMockRecorder) Do

func (mr *ClientMockRecorder) Do(arg0, arg1 any) *gomock.Call

Do indicates an expected call of Do.

func (*ClientMockRecorder) DoCache

func (mr *ClientMockRecorder) DoCache(arg0, arg1, arg2 any) *gomock.Call

DoCache indicates an expected call of DoCache.

func (*ClientMockRecorder) DoMulti

func (mr *ClientMockRecorder) DoMulti(arg0 any, arg1 ...any) *gomock.Call

DoMulti indicates an expected call of DoMulti.

func (*ClientMockRecorder) DoMultiCache

func (mr *ClientMockRecorder) DoMultiCache(arg0 any, arg1 ...any) *gomock.Call

DoMultiCache indicates an expected call of DoMultiCache.

func (*ClientMockRecorder) Nodes

func (mr *ClientMockRecorder) Nodes() *gomock.Call

Nodes indicates an expected call of Nodes.

func (*ClientMockRecorder) Receive

func (mr *ClientMockRecorder) Receive(arg0, arg1, arg2 any) *gomock.Call

Receive indicates an expected call of Receive.

type ClientOption

type ClientOption func(c any)

ClientOption is optional function parameter for NewClient

func WithSlotCheck

func WithSlotCheck() ClientOption

WithSlotCheck enables the command builder of Client to check if the command built across multiple slots and then panic

type DedicatedClient

type DedicatedClient struct {
	// contains filtered or unexported fields
}

DedicatedClient is a mock of DedicatedClient interface.

func NewDedicatedClient

func NewDedicatedClient(ctrl *gomock.Controller, options ...ClientOption) *DedicatedClient

NewDedicatedClient creates a new mock instance.

func (*DedicatedClient) B

func (m *DedicatedClient) B() cmds.Builder

B mocks base method.

func (*DedicatedClient) Close

func (m *DedicatedClient) Close()

Close mocks base method.

func (*DedicatedClient) Do

Do mocks base method.

func (*DedicatedClient) DoMulti

func (m *DedicatedClient) DoMulti(arg0 context.Context, arg1 ...cmds.Completed) []rueidis.RedisResult

DoMulti mocks base method.

func (*DedicatedClient) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

func (*DedicatedClient) Receive

func (m *DedicatedClient) Receive(arg0 context.Context, arg1 cmds.Completed, arg2 func(rueidis.PubSubMessage)) error

Receive mocks base method.

func (*DedicatedClient) SetPubSubHooks

func (m *DedicatedClient) SetPubSubHooks(arg0 rueidis.PubSubHooks) <-chan error

SetPubSubHooks mocks base method.

type DedicatedClientMockRecorder

type DedicatedClientMockRecorder struct {
	// contains filtered or unexported fields
}

DedicatedClientMockRecorder is the mock recorder for DedicatedClient.

func (*DedicatedClientMockRecorder) Close

Close indicates an expected call of Close.

func (*DedicatedClientMockRecorder) Do

func (mr *DedicatedClientMockRecorder) Do(arg0, arg1 any) *gomock.Call

Do indicates an expected call of Do.

func (*DedicatedClientMockRecorder) DoMulti

func (mr *DedicatedClientMockRecorder) DoMulti(arg0 any, arg1 ...any) *gomock.Call

DoMulti indicates an expected call of DoMulti.

func (*DedicatedClientMockRecorder) Receive

func (mr *DedicatedClientMockRecorder) Receive(arg0, arg1, arg2 any) *gomock.Call

Receive indicates an expected call of Receive.

func (*DedicatedClientMockRecorder) SetPubSubHooks

func (mr *DedicatedClientMockRecorder) SetPubSubHooks(arg0 any) *gomock.Call

SetPubSubHooks indicates an expected call of SetPubSubHooks.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL