mock

package module
v0.0.1-beta.3 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

README

valkey mock

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

Therefore, valkey 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 valkey responses.

Examples

Mock client.Do
package main

import (
	"context"
	"testing"

	"go.uber.org/mock/gomock"
	"github.com/valkey-io/valkey-go/mock"
)

func TestWithValkey(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.ValkeyString("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([]valkey.ValkeyResult{
		mock.Result(mock.ValkeyNil()),
		mock.Result(mock.ValkeyNil()),
	})
	for _, resp := range client.DoMulti(ctx, client.B().Get().Key("c").Build(), client.B().Get().Key("d").Build()) {
		if err := resp.Error(); !valkey.IsValkeyNil(err) {
			t.Fatalf("unexpected err %v", err)
		}
	}
}
Mock client.Receive
package main

import (
	"context"
	"testing"

	"go.uber.org/mock/gomock"
	"github.com/valkey-io/valkey-go/mock"
)

func TestWithValkeyReceive(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 valkey.PubSubMessage)) {
		fn(valkey.PubSubMessage{Message: "msg"})
	})

	client.Receive(ctx, client.B().Subscribe().Channel("ch").Build(), func(msg valkey.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) valkey.ValkeyResult

func Match

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

func MatchFn

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

func MultiValkeyResultStreamError

func MultiValkeyResultStreamError(err error) valkey.ValkeyResultStream

func ValkeyArray

func ValkeyArray(values ...valkey.ValkeyMessage) valkey.ValkeyMessage

func ValkeyBlobString

func ValkeyBlobString(v string) valkey.ValkeyMessage

func ValkeyBool

func ValkeyBool(v bool) valkey.ValkeyMessage

func ValkeyError

func ValkeyError(v string) valkey.ValkeyMessage

func ValkeyFloat64

func ValkeyFloat64(v float64) valkey.ValkeyMessage

func ValkeyInt64

func ValkeyInt64(v int64) valkey.ValkeyMessage

func ValkeyNil

func ValkeyNil() valkey.ValkeyMessage

func ValkeyResultStream

func ValkeyResultStream(ms ...valkey.ValkeyMessage) valkey.ValkeyResultStream

func ValkeyResultStreamError

func ValkeyResultStreamError(err error) valkey.ValkeyResultStream

func ValkeyString

func ValkeyString(v string) valkey.ValkeyMessage

Types

type Client

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

Client mocks the 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() valkey.Builder

B mocks base method.

func (*Client) Close

func (m *Client) Close()

Close mocks base method.

func (*Client) Dedicate

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

Dedicate mocks base method.

func (*Client) Dedicated

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

Dedicated mocks base method.

func (*Client) Do

Do mocks the base method.

func (*Client) DoCache

func (m *Client) DoCache(arg0 context.Context, arg1 valkey.Cacheable, arg2 time.Duration) valkey.ValkeyResult

DoCache mocks base method.

func (*Client) DoMulti

func (m *Client) DoMulti(arg0 context.Context, arg1 ...valkey.Completed) []valkey.ValkeyResult

DoMulti mocks base method.

func (*Client) DoMultiCache

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

DoMultiCache mocks the base method.

func (*Client) DoMultiStream

func (m *Client) DoMultiStream(arg0 context.Context, arg1 ...valkey.Completed) valkey.MultiValkeyResultStream

DoMultiStream mocks base method.

func (*Client) DoStream

DoStream mocks base method.

func (*Client) EXPECT

func (m *Client) EXPECT() *ClientMockRecorder

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

func (*Client) Mode

func (m *Client) Mode() valkey.ClientMode

Mode mocks base method.

func (*Client) Nodes

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

Nodes mocks the base method.

func (*Client) Receive

func (m *Client) Receive(arg0 context.Context, arg1 valkey.Completed, arg2 func(valkey.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) DoMultiStream

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

DoMultiStream indicates an expected call of DoMultiStream.

func (*ClientMockRecorder) DoStream

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

DoStream indicates an expected call of DoStream.

func (*ClientMockRecorder) Mode

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

Mode indicates an expected call of Mode.

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 an 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 mocks the DedicatedClient interface.

func NewDedicatedClient

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

NewDedicatedClient creates a new mock instance.

func (*DedicatedClient) B

B mocks base method.

func (*DedicatedClient) Close

func (m *DedicatedClient) Close()

Close mocks base method.

func (*DedicatedClient) Do

Do mocks the base method.

func (*DedicatedClient) DoMulti

func (m *DedicatedClient) DoMulti(arg0 context.Context, arg1 ...valkey.Completed) []valkey.ValkeyResult

DoMulti mocks base method.

func (*DedicatedClient) EXPECT

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

func (*DedicatedClient) Receive

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

Receive mocks base method.

func (*DedicatedClient) SetOnInvalidations

func (m *DedicatedClient) SetOnInvalidations(arg0 func([]valkey.ValkeyMessage)) <-chan error

SetOnInvalidations mocks base method.

func (*DedicatedClient) SetPubSubHooks

func (m *DedicatedClient) SetPubSubHooks(arg0 valkey.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) SetOnInvalidations

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

SetOnInvalidations indicates an expected call of SetOnInvalidations.

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