mirror of
https://github.com/hanzokv/mock.git
synced 2026-10-30 19:45:21 +00:00
main
go build, go vet and go test all pass clean on the Go 1.27.1 toolchain. Co-authored-by: Hanzo Dev <[email protected]>
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/PSubscribePipeline/TxPipelineWatch
Description
Mock client for Hanzo KV — fork of go-redis/redismock retargeted at github.com/hanzokv/go
323 KiB
Languages
Go
100%