mirror of
https://github.com/hanzokv/mock.git
synced 2026-10-30 19:45:21 +00:00
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]>
207 lines
5.7 KiB
Go
207 lines
5.7 KiB
Go
package kvmock
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/hanzokv/go/v9"
|
|
. "github.com/onsi/ginkgo"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = Describe("Cluster", func() {
|
|
var (
|
|
client *kv.ClusterClient
|
|
clusterMock ClusterClientMock
|
|
)
|
|
|
|
BeforeEach(func() {
|
|
client, clusterMock = NewClusterMock()
|
|
})
|
|
|
|
AfterEach(func() {
|
|
Expect(client.Close()).NotTo(HaveOccurred())
|
|
Expect(clusterMock.ExpectationsWereMet()).NotTo(HaveOccurred())
|
|
})
|
|
|
|
Describe("work order", func() {
|
|
BeforeEach(func() {
|
|
clusterMock.ExpectGet("key").KVNil()
|
|
clusterMock.ExpectSet("key", "1", 1*time.Second).SetVal("OK")
|
|
clusterMock.ExpectGet("key").SetVal("1")
|
|
clusterMock.ExpectGetSet("key", "0").SetVal("1")
|
|
})
|
|
|
|
It("ordinary", func() {
|
|
get := client.Get(ctx, "key")
|
|
Expect(get.Err()).To(Equal(kv.Nil))
|
|
Expect(get.Val()).To(Equal(""))
|
|
|
|
set := client.Set(ctx, "key", "1", 1*time.Second)
|
|
Expect(set.Err()).NotTo(HaveOccurred())
|
|
Expect(set.Val()).To(Equal("OK"))
|
|
|
|
get = client.Get(ctx, "key")
|
|
Expect(get.Err()).NotTo(HaveOccurred())
|
|
Expect(get.Val()).To(Equal("1"))
|
|
|
|
getSet := client.GetSet(ctx, "key", "0")
|
|
Expect(getSet.Err()).NotTo(HaveOccurred())
|
|
Expect(getSet.Val()).To(Equal("1"))
|
|
|
|
hasUnexpectedCall, unexpectedCalls := clusterMock.UnexpectedCallsWereMade()
|
|
Expect(hasUnexpectedCall).To(BeFalse())
|
|
Expect(unexpectedCalls).To(BeNil())
|
|
})
|
|
|
|
It("surplus", func() {
|
|
_ = client.Get(ctx, "key")
|
|
|
|
set := client.Set(ctx, "key", "1", 1*time.Second)
|
|
Expect(set.Err()).NotTo(HaveOccurred())
|
|
Expect(set.Val()).To(Equal("OK"))
|
|
|
|
Expect(clusterMock.ExpectationsWereMet()).To(HaveOccurred())
|
|
|
|
_ = client.Get(ctx, "key")
|
|
Expect(clusterMock.ExpectationsWereMet()).To(HaveOccurred())
|
|
|
|
hasUnexpectedCall, unexpectedCalls := clusterMock.UnexpectedCallsWereMade()
|
|
Expect(hasUnexpectedCall).To(BeFalse())
|
|
Expect(unexpectedCalls).To(BeNil())
|
|
|
|
_ = client.GetSet(ctx, "key", "0")
|
|
})
|
|
|
|
It("not enough", func() {
|
|
_ = client.Get(ctx, "key")
|
|
_ = client.Set(ctx, "key", "1", 1*time.Second)
|
|
_ = client.Get(ctx, "key")
|
|
_ = client.GetSet(ctx, "key", "0")
|
|
Expect(clusterMock.ExpectationsWereMet()).NotTo(HaveOccurred())
|
|
|
|
get := client.HGet(ctx, "key", "field")
|
|
Expect(get.Err()).To(HaveOccurred())
|
|
Expect(get.Val()).To(Equal(""))
|
|
|
|
hasUnexpectedCall, unexpectedCalls := clusterMock.UnexpectedCallsWereMade()
|
|
Expect(hasUnexpectedCall).To(BeTrue())
|
|
Expect(unexpectedCalls).NotTo(BeNil())
|
|
})
|
|
})
|
|
|
|
Describe("work not order", func() {
|
|
|
|
BeforeEach(func() {
|
|
clusterMock.MatchExpectationsInOrder(false)
|
|
|
|
clusterMock.ExpectSet("key", "1", 1*time.Second).SetVal("OK")
|
|
clusterMock.ExpectGet("key").SetVal("1")
|
|
clusterMock.ExpectGetSet("key", "0").SetVal("1")
|
|
})
|
|
|
|
AfterEach(func() {
|
|
hasUnexpectedCall, unexpectedCalls := clusterMock.UnexpectedCallsWereMade()
|
|
Expect(hasUnexpectedCall).To(BeFalse())
|
|
Expect(unexpectedCalls).To(BeNil())
|
|
})
|
|
|
|
It("ordinary", func() {
|
|
get := client.Get(ctx, "key")
|
|
Expect(get.Err()).NotTo(HaveOccurred())
|
|
Expect(get.Val()).To(Equal("1"))
|
|
|
|
set := client.Set(ctx, "key", "1", 1*time.Second)
|
|
Expect(set.Err()).NotTo(HaveOccurred())
|
|
Expect(set.Val()).To(Equal("OK"))
|
|
|
|
getSet := client.GetSet(ctx, "key", "0")
|
|
Expect(getSet.Err()).NotTo(HaveOccurred())
|
|
Expect(getSet.Val()).To(Equal("1"))
|
|
})
|
|
})
|
|
|
|
Describe("work other match", func() {
|
|
|
|
AfterEach(func() {
|
|
hasUnexpectedCall, unexpectedCalls := clusterMock.UnexpectedCallsWereMade()
|
|
Expect(hasUnexpectedCall).To(BeFalse())
|
|
Expect(unexpectedCalls).To(BeNil())
|
|
})
|
|
|
|
It("regexp match", func() {
|
|
clusterMock.Regexp().ExpectSet("key", `^order_id_[0-9]{10}$`, 1*time.Second).SetVal("OK")
|
|
clusterMock.Regexp().ExpectSet("key2", `^order_id_[0-9]{4}\-[0-9]{2}\-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.+$`, 1*time.Second).SetVal("OK")
|
|
|
|
set := client.Set(ctx, "key", fmt.Sprintf("order_id_%d", time.Now().Unix()), 1*time.Second)
|
|
Expect(set.Err()).NotTo(HaveOccurred())
|
|
Expect(set.Val()).To(Equal("OK"))
|
|
|
|
// no regexp
|
|
set = client.Set(ctx, "key2", fmt.Sprintf("order_id_%s", time.Now().Format(time.UnixDate)), 1*time.Second)
|
|
Expect(set.Err()).To(HaveOccurred())
|
|
Expect(set.Val()).To(Equal(""))
|
|
|
|
set = client.Set(ctx, "key2", fmt.Sprintf("order_id_%s", time.Now().Format(time.RFC3339)), 1*time.Second)
|
|
Expect(set.Err()).NotTo(HaveOccurred())
|
|
Expect(set.Val()).To(Equal("OK"))
|
|
})
|
|
|
|
It("custom match", func() {
|
|
clusterMock.CustomMatch(func(expected, actual []interface{}) error {
|
|
return errors.New("mismatch")
|
|
}).ExpectGet("key").SetVal("OK")
|
|
|
|
get := client.Get(ctx, "key")
|
|
Expect(get.Err()).To(Equal(errors.New("mismatch")))
|
|
Expect(get.Val()).To(Equal(""))
|
|
|
|
set := client.Incr(ctx, "key")
|
|
Expect(set.Err()).To(HaveOccurred())
|
|
Expect(set.Err()).NotTo(Equal(errors.New("mismatch")))
|
|
Expect(set.Val()).To(Equal(int64(0)))
|
|
|
|
// no match, no pass
|
|
Expect(clusterMock.ExpectationsWereMet()).To(HaveOccurred())
|
|
|
|
// let AfterEach pass
|
|
clusterMock.ClearExpect()
|
|
})
|
|
|
|
})
|
|
|
|
Describe("work error", func() {
|
|
|
|
AfterEach(func() {
|
|
hasUnexpectedCall, unexpectedCalls := clusterMock.UnexpectedCallsWereMade()
|
|
Expect(hasUnexpectedCall).To(BeFalse())
|
|
Expect(unexpectedCalls).To(BeNil())
|
|
})
|
|
|
|
It("set error", func() {
|
|
clusterMock.ExpectGet("key").SetErr(errors.New("set error"))
|
|
|
|
get := client.Get(ctx, "key")
|
|
Expect(get.Err()).To(Equal(errors.New("set error")))
|
|
Expect(get.Val()).To(Equal(""))
|
|
})
|
|
|
|
It("not set", func() {
|
|
clusterMock.ExpectGet("key")
|
|
|
|
get := client.Get(ctx, "key")
|
|
Expect(get.Err()).To(HaveOccurred())
|
|
Expect(get.Val()).To(Equal(""))
|
|
})
|
|
|
|
It("set zero", func() {
|
|
clusterMock.ExpectGet("key").SetVal("")
|
|
|
|
get := client.Get(ctx, "key")
|
|
Expect(get.Err()).NotTo(HaveOccurred())
|
|
Expect(get.Val()).To(Equal(""))
|
|
})
|
|
})
|
|
})
|