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
S
Description
Mock client for Hanzo KV — fork of go-redis/redismock retargeted at github.com/hanzokv/go
Readme BSD-2-Clause
323 KiB
Languages
Go 100%