redismock

package module
v9.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2025 License: BSD-2-Clause Imports: 10 Imported by: 0

README

Redis client Mock

Provide mock test for redis query, Compatible with github.com/redis/go-redis/v9

Install

Confirm that you are using redis.Client the version is github.com/redis/go-redis/v9

go get github.com/AVOXI/redismock/v9

Quick Start

RedisClient

var ctx = context.TODO()

func NewsInfoForCache(redisDB *redis.Client, newsID int) (info string, err error) {
	cacheKey := fmt.Sprintf("news_redis_cache_%d", newsID)
	info, err = redisDB.Get(ctx, cacheKey).Result()
	if err == redis.Nil {
		// info, err = call api()
		info = "test"
		err = redisDB.Set(ctx, cacheKey, info, 30 * time.Minute).Err()
	}
	return
}

func TestNewsInfoForCache(t *testing.T) {
	db, mock := redismock.NewClientMock()

	newsID := 123456789
	key := fmt.Sprintf("news_redis_cache_%d", newsID)

	// mock ignoring `call api()`

	mock.ExpectGet(key).RedisNil()
	mock.Regexp().ExpectSet(key, `[a-z]+`, 30 * time.Minute).SetErr(errors.New("FAIL"))

	_, err := NewsInfoForCache(db, newsID)
	if err == nil || err.Error() != "FAIL" {
		t.Error("wrong error")
	}

	if err := mock.ExpectationsWereMet(); err != nil {
		t.Error(err)
	}
}

RedisCluster

clusterClient, clusterMock := redismock.NewClusterMock()

Unsupported Command

RedisClient:

  • Subscribe / PSubscribe

RedisCluster

  • Subscribe / PSubscribe
  • Watch

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClientMock

type ClientMock interface {
	// contains filtered or unexported methods
}

func NewClientMock

func NewClientMock() (*redis.Client, ClientMock)

type ClusterClientMock

type ClusterClientMock interface {
	// contains filtered or unexported methods
}

func NewClusterMock

func NewClusterMock() (*redis.ClusterClient, ClusterClientMock)

type CustomMatch

type CustomMatch func(expected, actual []interface{}) error

type ExpectedBool

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

func (*ExpectedBool) RedisNil

func (base *ExpectedBool) RedisNil()

func (*ExpectedBool) SetErr

func (base *ExpectedBool) SetErr(err error)

func (*ExpectedBool) SetVal

func (cmd *ExpectedBool) SetVal(val bool)

type ExpectedBoolSlice

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

func (*ExpectedBoolSlice) RedisNil

func (base *ExpectedBoolSlice) RedisNil()

func (*ExpectedBoolSlice) SetErr

func (base *ExpectedBoolSlice) SetErr(err error)

func (*ExpectedBoolSlice) SetVal

func (cmd *ExpectedBoolSlice) SetVal(val []bool)
type ExpectedClusterLinks struct {
	// contains filtered or unexported fields
}

func (*ExpectedClusterLinks) RedisNil

func (base *ExpectedClusterLinks) RedisNil()

func (*ExpectedClusterLinks) SetErr

func (base *ExpectedClusterLinks) SetErr(err error)

func (*ExpectedClusterLinks) SetVal

func (cmd *ExpectedClusterLinks) SetVal(val []redis.ClusterLink)

type ExpectedClusterShards

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

func (*ExpectedClusterShards) RedisNil

func (base *ExpectedClusterShards) RedisNil()

func (*ExpectedClusterShards) SetErr

func (base *ExpectedClusterShards) SetErr(err error)

func (*ExpectedClusterShards) SetVal

func (cmd *ExpectedClusterShards) SetVal(val []redis.ClusterShard)

type ExpectedClusterSlots

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

func (*ExpectedClusterSlots) RedisNil

func (base *ExpectedClusterSlots) RedisNil()

func (*ExpectedClusterSlots) SetErr

func (base *ExpectedClusterSlots) SetErr(err error)

func (*ExpectedClusterSlots) SetVal

func (cmd *ExpectedClusterSlots) SetVal(val []redis.ClusterSlot)

type ExpectedCmd

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

func (*ExpectedCmd) RedisNil

func (base *ExpectedCmd) RedisNil()

func (*ExpectedCmd) SetErr

func (base *ExpectedCmd) SetErr(err error)

func (*ExpectedCmd) SetVal

func (cmd *ExpectedCmd) SetVal(val interface{})

type ExpectedCommandsInfo

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

func (*ExpectedCommandsInfo) RedisNil

func (base *ExpectedCommandsInfo) RedisNil()

func (*ExpectedCommandsInfo) SetErr

func (base *ExpectedCommandsInfo) SetErr(err error)

func (*ExpectedCommandsInfo) SetVal

func (cmd *ExpectedCommandsInfo) SetVal(val []*redis.CommandInfo)

type ExpectedDuration

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

func (*ExpectedDuration) RedisNil

func (base *ExpectedDuration) RedisNil()

func (*ExpectedDuration) SetErr

func (base *ExpectedDuration) SetErr(err error)

func (*ExpectedDuration) SetVal

func (cmd *ExpectedDuration) SetVal(val time.Duration)

type ExpectedError

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

func (*ExpectedError) RedisNil

func (base *ExpectedError) RedisNil()

func (*ExpectedError) SetErr

func (base *ExpectedError) SetErr(err error)

type ExpectedFloat

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

func (*ExpectedFloat) RedisNil

func (base *ExpectedFloat) RedisNil()

func (*ExpectedFloat) SetErr

func (base *ExpectedFloat) SetErr(err error)

func (*ExpectedFloat) SetVal

func (cmd *ExpectedFloat) SetVal(val float64)

type ExpectedFloatSlice

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

func (*ExpectedFloatSlice) RedisNil

func (base *ExpectedFloatSlice) RedisNil()

func (*ExpectedFloatSlice) SetErr

func (base *ExpectedFloatSlice) SetErr(err error)

func (*ExpectedFloatSlice) SetVal

func (cmd *ExpectedFloatSlice) SetVal(val []float64)

type ExpectedFunctionList

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

func (*ExpectedFunctionList) RedisNil

func (base *ExpectedFunctionList) RedisNil()

func (*ExpectedFunctionList) SetErr

func (base *ExpectedFunctionList) SetErr(err error)

func (*ExpectedFunctionList) SetVal

func (cmd *ExpectedFunctionList) SetVal(val []redis.Library)

type ExpectedGeoLocation

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

func (*ExpectedGeoLocation) RedisNil

func (base *ExpectedGeoLocation) RedisNil()

func (*ExpectedGeoLocation) SetErr

func (base *ExpectedGeoLocation) SetErr(err error)

func (*ExpectedGeoLocation) SetVal

func (cmd *ExpectedGeoLocation) SetVal(val []redis.GeoLocation)

type ExpectedGeoPos

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

func (*ExpectedGeoPos) RedisNil

func (base *ExpectedGeoPos) RedisNil()

func (*ExpectedGeoPos) SetErr

func (base *ExpectedGeoPos) SetErr(err error)

func (*ExpectedGeoPos) SetVal

func (cmd *ExpectedGeoPos) SetVal(val []*redis.GeoPos)

type ExpectedGeoSearchLocation

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

func (*ExpectedGeoSearchLocation) RedisNil

func (base *ExpectedGeoSearchLocation) RedisNil()

func (*ExpectedGeoSearchLocation) SetErr

func (base *ExpectedGeoSearchLocation) SetErr(err error)

func (*ExpectedGeoSearchLocation) SetVal

func (cmd *ExpectedGeoSearchLocation) SetVal(val []redis.GeoLocation)

type ExpectedInt

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

func (*ExpectedInt) RedisNil

func (base *ExpectedInt) RedisNil()

func (*ExpectedInt) SetErr

func (base *ExpectedInt) SetErr(err error)

func (*ExpectedInt) SetVal

func (cmd *ExpectedInt) SetVal(val int64)

type ExpectedIntSlice

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

func (*ExpectedIntSlice) RedisNil

func (base *ExpectedIntSlice) RedisNil()

func (*ExpectedIntSlice) SetErr

func (base *ExpectedIntSlice) SetErr(err error)

func (*ExpectedIntSlice) SetVal

func (cmd *ExpectedIntSlice) SetVal(val []int64)

type ExpectedKeyFlags

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

func (*ExpectedKeyFlags) RedisNil

func (base *ExpectedKeyFlags) RedisNil()

func (*ExpectedKeyFlags) SetErr

func (base *ExpectedKeyFlags) SetErr(err error)

func (*ExpectedKeyFlags) SetVal

func (cmd *ExpectedKeyFlags) SetVal(val []redis.KeyFlags)

type ExpectedKeyValueSlice

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

func (*ExpectedKeyValueSlice) RedisNil

func (base *ExpectedKeyValueSlice) RedisNil()

func (*ExpectedKeyValueSlice) SetErr

func (base *ExpectedKeyValueSlice) SetErr(err error)

func (*ExpectedKeyValueSlice) SetVal

func (cmd *ExpectedKeyValueSlice) SetVal(val []redis.KeyValue)

type ExpectedKeyValues

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

func (*ExpectedKeyValues) RedisNil

func (base *ExpectedKeyValues) RedisNil()

func (*ExpectedKeyValues) SetErr

func (base *ExpectedKeyValues) SetErr(err error)

func (*ExpectedKeyValues) SetVal

func (cmd *ExpectedKeyValues) SetVal(key string, val []string)

type ExpectedLCS

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

func (*ExpectedLCS) RedisNil

func (base *ExpectedLCS) RedisNil()

func (*ExpectedLCS) SetErr

func (base *ExpectedLCS) SetErr(err error)

func (*ExpectedLCS) SetVal

func (cmd *ExpectedLCS) SetVal(val *redis.LCSMatch)

type ExpectedMapStringInt

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

func (*ExpectedMapStringInt) RedisNil

func (base *ExpectedMapStringInt) RedisNil()

func (*ExpectedMapStringInt) SetErr

func (base *ExpectedMapStringInt) SetErr(err error)

func (*ExpectedMapStringInt) SetVal

func (cmd *ExpectedMapStringInt) SetVal(val map[string]int64)

type ExpectedMapStringInterface

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

func (*ExpectedMapStringInterface) RedisNil

func (base *ExpectedMapStringInterface) RedisNil()

func (*ExpectedMapStringInterface) SetErr

func (base *ExpectedMapStringInterface) SetErr(err error)

func (*ExpectedMapStringInterface) SetVal

func (cmd *ExpectedMapStringInterface) SetVal(val map[string]interface{})

type ExpectedMapStringSliceInterface

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

func (*ExpectedMapStringSliceInterface) RedisNil

func (base *ExpectedMapStringSliceInterface) RedisNil()

func (*ExpectedMapStringSliceInterface) SetErr

func (base *ExpectedMapStringSliceInterface) SetErr(err error)

func (*ExpectedMapStringSliceInterface) SetVal

func (cmd *ExpectedMapStringSliceInterface) SetVal(val map[string][]interface{})

type ExpectedMapStringString

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

func (*ExpectedMapStringString) RedisNil

func (base *ExpectedMapStringString) RedisNil()

func (*ExpectedMapStringString) SetErr

func (base *ExpectedMapStringString) SetErr(err error)

func (*ExpectedMapStringString) SetVal

func (cmd *ExpectedMapStringString) SetVal(val map[string]string)

type ExpectedScan

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

func (*ExpectedScan) RedisNil

func (base *ExpectedScan) RedisNil()

func (*ExpectedScan) SetErr

func (base *ExpectedScan) SetErr(err error)

func (*ExpectedScan) SetVal

func (cmd *ExpectedScan) SetVal(page []string, cursor uint64)

type ExpectedSlice

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

func (*ExpectedSlice) RedisNil

func (base *ExpectedSlice) RedisNil()

func (*ExpectedSlice) SetErr

func (base *ExpectedSlice) SetErr(err error)

func (*ExpectedSlice) SetVal

func (cmd *ExpectedSlice) SetVal(val []interface{})

type ExpectedSlowLog

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

func (*ExpectedSlowLog) RedisNil

func (base *ExpectedSlowLog) RedisNil()

func (*ExpectedSlowLog) SetErr

func (base *ExpectedSlowLog) SetErr(err error)

func (*ExpectedSlowLog) SetVal

func (cmd *ExpectedSlowLog) SetVal(val []redis.SlowLog)

type ExpectedStatus

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

func (*ExpectedStatus) RedisNil

func (base *ExpectedStatus) RedisNil()

func (*ExpectedStatus) SetErr

func (base *ExpectedStatus) SetErr(err error)

func (*ExpectedStatus) SetVal

func (cmd *ExpectedStatus) SetVal(val string)

type ExpectedString

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

func (*ExpectedString) RedisNil

func (base *ExpectedString) RedisNil()

func (*ExpectedString) SetErr

func (base *ExpectedString) SetErr(err error)

func (*ExpectedString) SetVal

func (cmd *ExpectedString) SetVal(val string)

type ExpectedStringSlice

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

func (*ExpectedStringSlice) RedisNil

func (base *ExpectedStringSlice) RedisNil()

func (*ExpectedStringSlice) SetErr

func (base *ExpectedStringSlice) SetErr(err error)

func (*ExpectedStringSlice) SetVal

func (cmd *ExpectedStringSlice) SetVal(val []string)

type ExpectedStringStructMap

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

func (*ExpectedStringStructMap) RedisNil

func (base *ExpectedStringStructMap) RedisNil()

func (*ExpectedStringStructMap) SetErr

func (base *ExpectedStringStructMap) SetErr(err error)

func (*ExpectedStringStructMap) SetVal

func (cmd *ExpectedStringStructMap) SetVal(val []string)

type ExpectedTSTimestampValue

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

func (*ExpectedTSTimestampValue) RedisNil

func (base *ExpectedTSTimestampValue) RedisNil()

func (*ExpectedTSTimestampValue) SetErr

func (base *ExpectedTSTimestampValue) SetErr(err error)

func (*ExpectedTSTimestampValue) SetVal

type ExpectedTSTimestampValueSlice

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

func (*ExpectedTSTimestampValueSlice) RedisNil

func (base *ExpectedTSTimestampValueSlice) RedisNil()

func (*ExpectedTSTimestampValueSlice) SetErr

func (base *ExpectedTSTimestampValueSlice) SetErr(err error)

func (*ExpectedTSTimestampValueSlice) SetVal

type ExpectedTime

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

func (*ExpectedTime) RedisNil

func (base *ExpectedTime) RedisNil()

func (*ExpectedTime) SetErr

func (base *ExpectedTime) SetErr(err error)

func (*ExpectedTime) SetVal

func (cmd *ExpectedTime) SetVal(val time.Time)

type ExpectedXAutoClaim

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

func (*ExpectedXAutoClaim) RedisNil

func (base *ExpectedXAutoClaim) RedisNil()

func (*ExpectedXAutoClaim) SetErr

func (base *ExpectedXAutoClaim) SetErr(err error)

func (*ExpectedXAutoClaim) SetVal

func (cmd *ExpectedXAutoClaim) SetVal(val []redis.XMessage, start string)

type ExpectedXAutoClaimJustID

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

func (*ExpectedXAutoClaimJustID) RedisNil

func (base *ExpectedXAutoClaimJustID) RedisNil()

func (*ExpectedXAutoClaimJustID) SetErr

func (base *ExpectedXAutoClaimJustID) SetErr(err error)

func (*ExpectedXAutoClaimJustID) SetVal

func (cmd *ExpectedXAutoClaimJustID) SetVal(val []string, start string)

type ExpectedXInfoConsumers

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

func (*ExpectedXInfoConsumers) RedisNil

func (base *ExpectedXInfoConsumers) RedisNil()

func (*ExpectedXInfoConsumers) SetErr

func (base *ExpectedXInfoConsumers) SetErr(err error)

func (*ExpectedXInfoConsumers) SetVal

func (cmd *ExpectedXInfoConsumers) SetVal(val []redis.XInfoConsumer)

type ExpectedXInfoGroups

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

func (*ExpectedXInfoGroups) RedisNil

func (base *ExpectedXInfoGroups) RedisNil()

func (*ExpectedXInfoGroups) SetErr

func (base *ExpectedXInfoGroups) SetErr(err error)

func (*ExpectedXInfoGroups) SetVal

func (cmd *ExpectedXInfoGroups) SetVal(val []redis.XInfoGroup)

type ExpectedXInfoStream

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

func (*ExpectedXInfoStream) RedisNil

func (base *ExpectedXInfoStream) RedisNil()

func (*ExpectedXInfoStream) SetErr

func (base *ExpectedXInfoStream) SetErr(err error)

func (*ExpectedXInfoStream) SetVal

func (cmd *ExpectedXInfoStream) SetVal(val *redis.XInfoStream)

type ExpectedXInfoStreamFull

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

func (*ExpectedXInfoStreamFull) RedisNil

func (base *ExpectedXInfoStreamFull) RedisNil()

func (*ExpectedXInfoStreamFull) SetErr

func (base *ExpectedXInfoStreamFull) SetErr(err error)

func (*ExpectedXInfoStreamFull) SetVal

func (cmd *ExpectedXInfoStreamFull) SetVal(val *redis.XInfoStreamFull)

type ExpectedXMessageSlice

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

func (*ExpectedXMessageSlice) RedisNil

func (base *ExpectedXMessageSlice) RedisNil()

func (*ExpectedXMessageSlice) SetErr

func (base *ExpectedXMessageSlice) SetErr(err error)

func (*ExpectedXMessageSlice) SetVal

func (cmd *ExpectedXMessageSlice) SetVal(val []redis.XMessage)

type ExpectedXPending

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

func (*ExpectedXPending) RedisNil

func (base *ExpectedXPending) RedisNil()

func (*ExpectedXPending) SetErr

func (base *ExpectedXPending) SetErr(err error)

func (*ExpectedXPending) SetVal

func (cmd *ExpectedXPending) SetVal(val *redis.XPending)

type ExpectedXPendingExt

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

func (*ExpectedXPendingExt) RedisNil

func (base *ExpectedXPendingExt) RedisNil()

func (*ExpectedXPendingExt) SetErr

func (base *ExpectedXPendingExt) SetErr(err error)

func (*ExpectedXPendingExt) SetVal

func (cmd *ExpectedXPendingExt) SetVal(val []redis.XPendingExt)

type ExpectedXStreamSlice

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

func (*ExpectedXStreamSlice) RedisNil

func (base *ExpectedXStreamSlice) RedisNil()

func (*ExpectedXStreamSlice) SetErr

func (base *ExpectedXStreamSlice) SetErr(err error)

func (*ExpectedXStreamSlice) SetVal

func (cmd *ExpectedXStreamSlice) SetVal(val []redis.XStream)

type ExpectedZSlice

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

func (*ExpectedZSlice) RedisNil

func (base *ExpectedZSlice) RedisNil()

func (*ExpectedZSlice) SetErr

func (base *ExpectedZSlice) SetErr(err error)

func (*ExpectedZSlice) SetVal

func (cmd *ExpectedZSlice) SetVal(val []redis.Z)

type ExpectedZSliceWithKey

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

func (*ExpectedZSliceWithKey) RedisNil

func (base *ExpectedZSliceWithKey) RedisNil()

func (*ExpectedZSliceWithKey) SetErr

func (base *ExpectedZSliceWithKey) SetErr(err error)

func (*ExpectedZSliceWithKey) SetVal

func (cmd *ExpectedZSliceWithKey) SetVal(key string, val []redis.Z)

type ExpectedZWithKey

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

func (*ExpectedZWithKey) RedisNil

func (base *ExpectedZWithKey) RedisNil()

func (*ExpectedZWithKey) SetErr

func (base *ExpectedZWithKey) SetErr(err error)

func (*ExpectedZWithKey) SetVal

func (cmd *ExpectedZWithKey) SetVal(val *redis.ZWithKey)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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