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

1.3 KiB

KV client Mock

Provide mock test for kv query, Compatible with github.com/hanzokv/go/v9

Install

Confirm that you are using kv.Client the version is github.com/hanzokv/go/v9

go get github.com/hanzokv/mock/v9

Quick Start

KVClient

var ctx = context.TODO()

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

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

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

	// mock ignoring `call api()`

	mock.ExpectGet(key).KVNil()
	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)
	}
}

KVCluster

clusterClient, clusterMock := kvmock.NewClusterMock()

Unsupported Command

KVClient:

  • Subscribe / PSubscribe

KVCluster

  • Subscribe / PSubscribe
  • Pipeline / TxPipeline
  • Watch