Categorygithub.com/valkey-io/valkey-go/mock
modulepackage
1.0.51
Repository: https://github.com/valkey-io/valkey-go.git
Documentation: pkg.go.dev

# 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)
		}
	})
}

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NewClient creates a new mock instance.
NewDedicatedClient creates a new mock instance.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
WithSlotCheck enables the command builder of Client to check if the command built across multiple slots and then panic.

# Structs

Client is a mock of Client interface.
ClientMockRecorder is the mock recorder for Client.
DedicatedClient is a mock of DedicatedClient interface.
DedicatedClientMockRecorder is the mock recorder for DedicatedClient.

# Type aliases

ClientOption is optional function parameter for NewClient.