Files
zeekayandHanzo Dev 7015fe8288 refactor: follow hanzokv/go v9.22.0 package rename redis -> kv
github.com/hanzokv/go/v9 v9.22.0 renamed its Go package from "redis" to
"kv"; the import path is unchanged. A mock library is typed against the
client it wraps, so this is not cosmetic: every exported signature in
this package (ClientMock, ClusterClientMock, all Expect* methods) is
expressed in terms of the client's types, and none of them resolve under
the old qualifier.

What changed:

- go.mod: github.com/hanzokv/go/v9 v9.21.1 -> v9.22.0
- every "redis." qualifier -> "kv." The import stays unaliased, matching
  the convention in hanzokv/go's own sources.
- package redismock -> package kvmock, matching the hanzokv/mock repo
- our own vocabulary follows the client it mocks: RedisNil() -> KVNil(),
  isRedisNil/redisNil -> isKVNil/kvNil, redisClientType -> kvClientType,
  redisClient/redisCluster -> kvClient/kvCluster,
  redisClientHook -> kvClientHook, TestRedisMock -> TestKVMock
- comments and README speak kv
- gofmt: import blocks in the ginkgo test files were left unsorted by the
  fork commit; formatting them is a side effect of touching them

Opaque test payloads are left alone -- they are data, not vocabulary.

Verified: go build ./..., go vet ./..., go test ./..., and
go test ./... -short -race all pass.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-26 08:42:54 -07:00

123 lines
2.9 KiB
Go

package example
import (
"context"
"errors"
"time"
"github.com/hanzokv/mock/v9"
)
var _ = example
func example() {
var ctx = context.TODO()
// get kv.Client and mock
db, mock := kvmock.NewClientMock()
//the order of commands expected and executed must be the same
//this is the default value
mock.MatchExpectationsInOrder(true)
//simple matching
//hget command return error
mock.ExpectHGet("key", "field").SetErr(errors.New("error"))
//db.HGet(ctx, "key", "field").Err() == errors.New("error")
//hget command return value
mock.ExpectHGet("key", "field").SetVal("test value")
//db.HGet(ctx, "key", "field").Val() == "test value"
//hget command return kv.Nil
mock.ExpectHGet("key", "field").KVNil()
//db.HGet(ctx, "key", "field").Err() == kv.Nil
//hget command... do not set return
mock.ExpectHGet("key", "field")
//db.HGet(ctx, "key", "field").Err() != nil
//------------
//clean up all expectations
//reset expected kv command
mock.ClearExpect()
//regular, uncertain value
db.HSet(ctx, "key", "field", time.Now().Unix())
mock.Regexp().ExpectHSet("key", "field", `^[0-9]+$`).SetVal(1)
//------------
mock.ClearExpect()
//custom match, regular expression can not meet the requirements
mock.CustomMatch(func(expected, actual []interface{}) error {
//expected == cmd.Args()
return nil
}).ExpectGet("key").SetVal("value")
//--------
//all the expected kv commands have been matched
//otherwise return an error
if err := mock.ExpectationsWereMet(); err != nil {
//error
panic(err)
}
//---------
//any order
//this is useful if your kv commands are executed concurrently
mock.MatchExpectationsInOrder(false)
//1-2-3
mock.ExpectGet("key").SetVal("value")
mock.ExpectSet("set-key", "set-value", 1).SetVal("OK")
mock.ExpectHGet("hash-get", "hash-field").SetVal("hash-value")
//3-1-2
_ = db.HGet(ctx, "hash-get", "hash-field")
_ = db.Get(ctx, "key")
_ = db.Set(ctx, "set-key", "set-value", 1)
//--------------
//pipeline, pipeline is not a kv command, is a collection of commands
mock.ExpectGet("key").SetVal("value")
mock.ExpectSet("key", "value", 1).SetVal("OK")
pipe := db.Pipeline()
_ = pipe.Get(ctx, "key")
_ = pipe.Set(ctx, "key", "value", 1)
_, _ = pipe.Exec(ctx)
//---------------------
mock.ClearExpect()
//TxPipeline
mock.ExpectTxPipeline()
mock.ExpectGet("key").SetVal("value")
mock.ExpectSet("key", "value", 1).SetVal("OK")
mock.ExpectTxPipelineExec()
pipe = db.TxPipeline()
_ = pipe.Get(ctx, "key")
_ = pipe.Set(ctx, "key", "value", 1)
_, _ = pipe.Exec(ctx)
//Watch
mock.ExpectWatch("key1", "key2").SetErr(errors.New("watch error"))
mock.ExpectGet("key1").SetVal("1")
mock.ExpectSet("key2", "2", 1*time.Second).SetVal("OK")
//err := db.Watch(ctx, func(tx *kv.Tx) error {
// tx.Get(ctx, "key1")
// tx.Set(ctx, "key2", "2", 1 * time.Second)
// return nil
//}, "key1", "key2")
//reflect.DeepEqual(err, errors.New("watch error"))
}