From 776eee3efa646fafbeb4c80bd5e0367226c6d384 Mon Sep 17 00:00:00 2001 From: aviadl Date: Thu, 30 Jun 2022 10:22:21 +0300 Subject: [PATCH 1/6] Support GetDel command in mocks --- mock.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mock.go b/mock.go index cf6e351..f66a368 100644 --- a/mock.go +++ b/mock.go @@ -660,6 +660,13 @@ func (m *mock) ExpectGet(key string) *ExpectedString { return e } +func (m *mock) ExpectGetDel(key string) *ExpectedString { + e := &ExpectedString{} + e.cmd = m.factory.GetDel(m.ctx, key) + m.pushExpect(e) + return e +} + func (m *mock) ExpectGetRange(key string, start, end int64) *ExpectedString { e := &ExpectedString{} e.cmd = m.factory.GetRange(m.ctx, key, start, end) From 99dd12aa244dbfab0372aa4906cfed91f504d7cd Mon Sep 17 00:00:00 2001 From: Omer Cohen Date: Wed, 9 Nov 2022 10:21:22 +0200 Subject: [PATCH 2/6] fix use of unsafe (#1) --- expect.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/expect.go b/expect.go index af81a47..1bc1958 100644 --- a/expect.go +++ b/expect.go @@ -5,7 +5,6 @@ import ( "reflect" "sync" "time" - "unsafe" "github.com/go-redis/redis/v8" ) @@ -312,7 +311,7 @@ func inflow(cmd redis.Cmder, key string, val interface{}) { if !v.IsValid() { panic(fmt.Sprintf("cmd did not find key '%s'", key)) } - v = reflect.NewAt(v.Type(), unsafe.Pointer(v.UnsafeAddr())).Elem() + v = reflect.NewAt(v.Type(), v.Addr().UnsafePointer()).Elem() setVal := reflect.ValueOf(val) if v.Kind() != reflect.Interface && setVal.Kind() != v.Kind() { From 013f11162fe6c52dc1e16141139ad5c9fdda528b Mon Sep 17 00:00:00 2001 From: Omer Cohen Date: Tue, 7 Feb 2023 17:32:31 +0200 Subject: [PATCH 3/6] Update go.mod --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index f937dfd..cfa3b5c 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/go-redis/redismock/v8 +module github.com/descope/redismock/v8 go 1.15 From 472bf0c23a2ab7634f30276b29067a368340e4b6 Mon Sep 17 00:00:00 2001 From: Omer Cohen Date: Tue, 19 Sep 2023 16:20:37 +0300 Subject: [PATCH 4/6] tidy --- .gitignore | 3 ++- go.sum | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 62c8935..e0caea8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.idea/ \ No newline at end of file +.idea/ +vendor/ \ No newline at end of file diff --git a/go.sum b/go.sum index 6a81ac5..67bf04e 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,8 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= +github.com/go-redis/redismock/v9 v9.0.3 h1:mtHQi2l51lCmXIbTRTqb1EiHYe9tL5Yk5oorlSJJqR0= +github.com/go-redis/redismock/v9 v9.0.3/go.mod h1:F6tJRfnU8R/NZ0E+Gjvoluk14MqMC5ueSZX6vVQypc0= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= From f35bd2c70251527fd382b1ac5fea51fb80054e2f Mon Sep 17 00:00:00 2001 From: Aviad Lichtenstadt Date: Tue, 19 Sep 2023 18:50:58 +0300 Subject: [PATCH 5/6] ADd option to run mock client with redis hooks (#4) --- mock.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/mock.go b/mock.go index e0ecb9a..e48168f 100644 --- a/mock.go +++ b/mock.go @@ -41,12 +41,17 @@ func NewClientMock() (*redis.Client, ClientMock) { return m.client.(*redis.Client), m } +func NewClientMockWithHooks(hooks ...redis.Hook) (*redis.Client, ClientMock) { + m := newMock(redisClient, hooks...) + return m.client.(*redis.Client), m +} + func NewClusterMock() (*redis.ClusterClient, ClusterClientMock) { m := newMock(redisCluster) return m.client.(*redis.ClusterClient), m } -func newMock(typ redisClientType) *mock { +func newMock(typ redisClientType, hooks ...redis.Hook) *mock { m := &mock{ ctx: context.Background(), clientType: typ, @@ -59,6 +64,9 @@ func newMock(typ redisClientType) *mock { factory := redis.NewClient(opt) client := redis.NewClient(opt) factory.AddHook(nilHook{}) + for i := range hooks { + client.AddHook(hooks[i]) + } client.AddHook(redisClientHook{fn: m.process}) m.factory = factory @@ -802,13 +810,6 @@ func (m *mock) ExpectGetEx(key string, expiration time.Duration) *ExpectedString return e } -func (m *mock) ExpectGetDel(key string) *ExpectedString { - e := &ExpectedString{} - e.cmd = m.factory.GetDel(m.ctx, key) - m.pushExpect(e) - return e -} - func (m *mock) ExpectIncr(key string) *ExpectedInt { e := &ExpectedInt{} e.cmd = m.factory.Incr(m.ctx, key) From af23fe3738053b989b0dc82e3c9c1b944c4da685 Mon Sep 17 00:00:00 2001 From: Doron Sharon Date: Mon, 27 Nov 2023 15:53:59 +0200 Subject: [PATCH 6/6] Fix regex compare to work with strings and bytes --- mock.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/mock.go b/mock.go index e48168f..32cd9b6 100644 --- a/mock.go +++ b/mock.go @@ -271,13 +271,20 @@ func (m *mock) match(expect expectation, cmd redis.Cmder) error { func (m *mock) compare(isRegexp bool, expect, cmd interface{}) error { expr, ok := expect.(string) if isRegexp && ok { - cmdValue := fmt.Sprint(cmd) + var cmdValue1 string + var cmdValue2 string + if bCmd, ok := cmd.([]byte); ok { + cmdValue1 = string(bCmd) + cmdValue2 = fmt.Sprint(cmd) + } else { + cmdValue1 = fmt.Sprint(cmd) + } re, err := regexp.Compile(expr) if err != nil { return err } - if !re.MatchString(cmdValue) { - return fmt.Errorf("args not match, expectation regular: '%s', but gave: '%s'", expr, cmdValue) + if !re.MatchString(cmdValue1) && !re.MatchString(cmdValue2) { + return fmt.Errorf("args not match, expectation regular: '%s', but gave: '%s', and: '%s", expr, cmdValue1, cmdValue2) } } else if !reflect.DeepEqual(expect, cmd) { return fmt.Errorf("args not `DeepEqual`, expectation: '%+v', but gave: '%+v'", expect, cmd)