Skip to content

Commit 5b39fcd

Browse files
authored
Merge pull request #1 from antonyho/record-unexpected-calls
Record unexpected command calls
2 parents 80794c9 + f635e61 commit 5b39fcd

File tree

6 files changed

+122
-23
lines changed

6 files changed

+122
-23
lines changed

client_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ var _ = Describe("Client", func() {
3838
pipe = client.TxPipeline()
3939
})
4040

41+
AfterEach(func() {
42+
hasUnexpectedCall, unexpectedCalls := clientMock.UnexpectedCallsWereCalled()
43+
Expect(hasUnexpectedCall).To(BeFalse())
44+
Expect(unexpectedCalls).To(BeNil())
45+
})
46+
4147
It("tx pipeline order", func() {
4248
get := pipe.Get(ctx, "key1")
4349
hashGet := pipe.HGet(ctx, "hash_key", "hash_field")
@@ -88,6 +94,12 @@ var _ = Describe("Client", func() {
8894
pipe = client.Pipeline()
8995
})
9096

97+
AfterEach(func() {
98+
hasUnexpectedCall, unexpectedCalls := clientMock.UnexpectedCallsWereCalled()
99+
Expect(hasUnexpectedCall).To(BeFalse())
100+
Expect(unexpectedCalls).To(BeNil())
101+
})
102+
91103
It("pipeline order", func() {
92104
clientMock.MatchExpectationsInOrder(true)
93105

@@ -136,6 +148,12 @@ var _ = Describe("Client", func() {
136148
clientMock.ExpectSet("key2", "2", 1*time.Second).SetVal("OK")
137149
})
138150

151+
AfterEach(func() {
152+
hasUnexpectedCall, unexpectedCalls := clientMock.UnexpectedCallsWereCalled()
153+
Expect(hasUnexpectedCall).To(BeTrue())
154+
Expect(unexpectedCalls).ShouldNot(BeNil())
155+
})
156+
139157
It("watch error", func() {
140158
clientMock.MatchExpectationsInOrder(false)
141159
txf := func(tx *redis.Tx) error {
@@ -220,6 +238,10 @@ var _ = Describe("Client", func() {
220238
getSet := client.GetSet(ctx, "key", "0")
221239
Expect(getSet.Err()).NotTo(HaveOccurred())
222240
Expect(getSet.Val()).To(Equal("1"))
241+
242+
hasUnexpectedCall, unexpectedCalls := clientMock.UnexpectedCallsWereCalled()
243+
Expect(hasUnexpectedCall).To(BeFalse())
244+
Expect(unexpectedCalls).To(BeNil())
223245
})
224246

225247
It("surplus", func() {
@@ -234,6 +256,10 @@ var _ = Describe("Client", func() {
234256
_ = client.Get(ctx, "key")
235257
Expect(clientMock.ExpectationsWereMet()).To(HaveOccurred())
236258

259+
hasUnexpectedCall, unexpectedCalls := clientMock.UnexpectedCallsWereCalled()
260+
Expect(hasUnexpectedCall).To(BeFalse())
261+
Expect(unexpectedCalls).To(BeNil())
262+
237263
_ = client.GetSet(ctx, "key", "0")
238264
})
239265

@@ -247,6 +273,10 @@ var _ = Describe("Client", func() {
247273
get := client.HGet(ctx, "key", "field")
248274
Expect(get.Err()).To(HaveOccurred())
249275
Expect(get.Val()).To(Equal(""))
276+
277+
hasUnexpectedCall, unexpectedCalls := clientMock.UnexpectedCallsWereCalled()
278+
Expect(hasUnexpectedCall).To(BeTrue())
279+
Expect(unexpectedCalls).NotTo(BeNil())
250280
})
251281
})
252282

@@ -260,6 +290,12 @@ var _ = Describe("Client", func() {
260290
clientMock.ExpectGetSet("key", "0").SetVal("1")
261291
})
262292

293+
AfterEach(func() {
294+
hasUnexpectedCall, unexpectedCalls := clientMock.UnexpectedCallsWereCalled()
295+
Expect(hasUnexpectedCall).To(BeFalse())
296+
Expect(unexpectedCalls).To(BeNil())
297+
})
298+
263299
It("ordinary", func() {
264300
get := client.Get(ctx, "key")
265301
Expect(get.Err()).NotTo(HaveOccurred())
@@ -277,6 +313,12 @@ var _ = Describe("Client", func() {
277313

278314
Describe("work other match", func() {
279315

316+
AfterEach(func() {
317+
hasUnexpectedCall, unexpectedCalls := clientMock.UnexpectedCallsWereCalled()
318+
Expect(hasUnexpectedCall).To(BeFalse())
319+
Expect(unexpectedCalls).To(BeNil())
320+
})
321+
280322
It("regexp match", func() {
281323
clientMock.Regexp().ExpectSet("key", `^order_id_[0-9]{10}$`, 1*time.Second).SetVal("OK")
282324
clientMock.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")
@@ -320,6 +362,12 @@ var _ = Describe("Client", func() {
320362

321363
Describe("work error", func() {
322364

365+
AfterEach(func() {
366+
hasUnexpectedCall, unexpectedCalls := clientMock.UnexpectedCallsWereCalled()
367+
Expect(hasUnexpectedCall).To(BeFalse())
368+
Expect(unexpectedCalls).To(BeNil())
369+
})
370+
323371
It("set error", func() {
324372
clientMock.ExpectGet("key").SetErr(errors.New("set error"))
325373

cluster_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ var _ = Describe("Cluster", func() {
4949
getSet := client.GetSet(ctx, "key", "0")
5050
Expect(getSet.Err()).NotTo(HaveOccurred())
5151
Expect(getSet.Val()).To(Equal("1"))
52+
53+
hasUnexpectedCall, unexpectedCalls := clusterMock.UnexpectedCallsWereCalled()
54+
Expect(hasUnexpectedCall).To(BeFalse())
55+
Expect(unexpectedCalls).To(BeNil())
5256
})
5357

5458
It("surplus", func() {
@@ -63,6 +67,10 @@ var _ = Describe("Cluster", func() {
6367
_ = client.Get(ctx, "key")
6468
Expect(clusterMock.ExpectationsWereMet()).To(HaveOccurred())
6569

70+
hasUnexpectedCall, unexpectedCalls := clusterMock.UnexpectedCallsWereCalled()
71+
Expect(hasUnexpectedCall).To(BeFalse())
72+
Expect(unexpectedCalls).To(BeNil())
73+
6674
_ = client.GetSet(ctx, "key", "0")
6775
})
6876

@@ -76,6 +84,10 @@ var _ = Describe("Cluster", func() {
7684
get := client.HGet(ctx, "key", "field")
7785
Expect(get.Err()).To(HaveOccurred())
7886
Expect(get.Val()).To(Equal(""))
87+
88+
hasUnexpectedCall, unexpectedCalls := clusterMock.UnexpectedCallsWereCalled()
89+
Expect(hasUnexpectedCall).To(BeTrue())
90+
Expect(unexpectedCalls).NotTo(BeNil())
7991
})
8092
})
8193

@@ -89,6 +101,12 @@ var _ = Describe("Cluster", func() {
89101
clusterMock.ExpectGetSet("key", "0").SetVal("1")
90102
})
91103

104+
AfterEach(func() {
105+
hasUnexpectedCall, unexpectedCalls := clusterMock.UnexpectedCallsWereCalled()
106+
Expect(hasUnexpectedCall).To(BeFalse())
107+
Expect(unexpectedCalls).To(BeNil())
108+
})
109+
92110
It("ordinary", func() {
93111
get := client.Get(ctx, "key")
94112
Expect(get.Err()).NotTo(HaveOccurred())
@@ -106,6 +124,12 @@ var _ = Describe("Cluster", func() {
106124

107125
Describe("work other match", func() {
108126

127+
AfterEach(func() {
128+
hasUnexpectedCall, unexpectedCalls := clusterMock.UnexpectedCallsWereCalled()
129+
Expect(hasUnexpectedCall).To(BeFalse())
130+
Expect(unexpectedCalls).To(BeNil())
131+
})
132+
109133
It("regexp match", func() {
110134
clusterMock.Regexp().ExpectSet("key", `^order_id_[0-9]{10}$`, 1*time.Second).SetVal("OK")
111135
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")
@@ -149,6 +173,12 @@ var _ = Describe("Cluster", func() {
149173

150174
Describe("work error", func() {
151175

176+
AfterEach(func() {
177+
hasUnexpectedCall, unexpectedCalls := clusterMock.UnexpectedCallsWereCalled()
178+
Expect(hasUnexpectedCall).To(BeFalse())
179+
Expect(unexpectedCalls).To(BeNil())
180+
})
181+
152182
It("set error", func() {
153183
clusterMock.ExpectGet("key").SetErr(errors.New("set error"))
154184

example/example.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package example
33
import (
44
"context"
55
"errors"
6+
"log"
67
"time"
78

89
"github.com/go-redis/redismock/v9"
@@ -67,6 +68,14 @@ func example() {
6768
panic(err)
6869
}
6970

71+
//--------
72+
73+
//unexpected redis commands have been called
74+
//returns true and the unexpected calls
75+
if unexpected, calls := mock.UnexpectedCallsWereCalled(); unexpected {
76+
log.Printf("Unexpected calls: %+v", calls)
77+
}
78+
7079
//---------
7180

7281
//any order

expect.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ type baseMock interface {
2424
// were met in order. If any of them was not met - an error is returned.
2525
ExpectationsWereMet() error
2626

27+
// UnexpectedCallsWereCalled returns any unexpected calls which were made.
28+
// If any unexpected call was made, a list of unexpected call redis.Cmder is returned.
29+
UnexpectedCallsWereCalled() (bool, []redis.Cmder)
30+
2731
// MatchExpectationsInOrder gives an option whether to match all expectations in the order they were set or not.
2832
MatchExpectationsInOrder(b bool)
2933

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/bsm/ginkgo/v2 v2.7.0 h1:ItPMPH90RbmZJt5GtkcNvIRuGEdwlBItdNVoyzaNQao=
2-
github.com/bsm/gomega v1.26.0 h1:LhQm+AFcgV2M0WyKroMASzAzCAJVpAxQXv4SaI9a69Y=
1+
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
2+
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
33
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
44
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
55
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -38,8 +38,6 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
3838
github.com/onsi/gomega v1.25.0 h1:Vw7br2PCDYijJHSfBOWhov+8cAnUf8MfMaIOV323l6Y=
3939
github.com/onsi/gomega v1.25.0/go.mod h1:r+zV744Re+DiYCIPRlYOTxn0YkOLcAnW8k1xXdMPGhM=
4040
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
41-
github.com/redis/go-redis/v9 v9.0.3 h1:+7mmR26M0IvyLxGZUHxu4GiBkJkVDid0Un+j4ScYu4k=
42-
github.com/redis/go-redis/v9 v9.0.3/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
4341
github.com/redis/go-redis/v9 v9.2.0 h1:zwMdX0A4eVzse46YN18QhuDiM4uf3JmkOB4VZrdt5uI=
4442
github.com/redis/go-redis/v9 v9.2.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
4543
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

mock.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ type mock struct {
2323

2424
parent *mock
2525

26-
factory mockCmdable
27-
client redis.Cmdable
28-
expected []expectation
26+
factory mockCmdable
27+
client redis.Cmdable
28+
expected []expectation
29+
unexpected []redis.Cmder
2930

3031
strictOrder bool
3132

@@ -180,6 +181,7 @@ func (m *mock) process(cmd redis.Cmder) (err error) {
180181
}
181182
err = fmt.Errorf(msg, cmd.Args())
182183
cmd.SetErr(err)
184+
m.unexpected = append(m.unexpected, cmd)
183185
return err
184186
}
185187

@@ -362,6 +364,7 @@ func (m *mock) ClearExpect() {
362364
return
363365
}
364366
m.expected = nil
367+
m.unexpected = nil
365368
}
366369

367370
func (m *mock) Regexp() *mock {
@@ -402,6 +405,13 @@ func (m *mock) ExpectationsWereMet() error {
402405
return nil
403406
}
404407

408+
func (m *mock) UnexpectedCallsWereCalled() (bool, []redis.Cmder) {
409+
if m.parent != nil {
410+
return m.parent.UnexpectedCallsWereCalled()
411+
}
412+
return len(m.unexpected) > 0, m.unexpected
413+
}
414+
405415
func (m *mock) MatchExpectationsInOrder(b bool) {
406416
if m.parent != nil {
407417
m.MatchExpectationsInOrder(b)
@@ -2862,29 +2872,29 @@ func (m *mock) ExpectTSMRangeWithArgs(fromTimestamp int, toTimestamp int, filter
28622872
}
28632873

28642874
func (m *mock) ExpectTSMRevRange(fromTimestamp int, toTimestamp int, filterExpr []string) *ExpectedMapStringSliceInterface {
2865-
e := &ExpectedMapStringSliceInterface{}
2866-
e.cmd = m.factory.TSMRevRange(m.ctx, fromTimestamp, toTimestamp, filterExpr)
2867-
m.pushExpect(e)
2868-
return e
2875+
e := &ExpectedMapStringSliceInterface{}
2876+
e.cmd = m.factory.TSMRevRange(m.ctx, fromTimestamp, toTimestamp, filterExpr)
2877+
m.pushExpect(e)
2878+
return e
28692879
}
28702880

28712881
func (m *mock) ExpectTSMRevRangeWithArgs(fromTimestamp int, toTimestamp int, filterExpr []string, options *redis.TSMRevRangeOptions) *ExpectedMapStringSliceInterface {
2872-
e := &ExpectedMapStringSliceInterface{}
2873-
e.cmd = m.factory.TSMRevRangeWithArgs(m.ctx, fromTimestamp, toTimestamp, filterExpr, options)
2874-
m.pushExpect(e)
2875-
return e
2882+
e := &ExpectedMapStringSliceInterface{}
2883+
e.cmd = m.factory.TSMRevRangeWithArgs(m.ctx, fromTimestamp, toTimestamp, filterExpr, options)
2884+
m.pushExpect(e)
2885+
return e
28762886
}
28772887

28782888
func (m *mock) ExpectTSMGet(filters []string) *ExpectedMapStringSliceInterface {
2879-
e := &ExpectedMapStringSliceInterface{}
2880-
e.cmd = m.factory.TSMGet(m.ctx, filters)
2881-
m.pushExpect(e)
2882-
return e
2889+
e := &ExpectedMapStringSliceInterface{}
2890+
e.cmd = m.factory.TSMGet(m.ctx, filters)
2891+
m.pushExpect(e)
2892+
return e
28832893
}
28842894

28852895
func (m *mock) ExpectTSMGetWithArgs(filters []string, options *redis.TSMGetOptions) *ExpectedMapStringSliceInterface {
2886-
e := &ExpectedMapStringSliceInterface{}
2887-
e.cmd = m.factory.TSMGetWithArgs(m.ctx, filters, options)
2888-
m.pushExpect(e)
2889-
return e
2896+
e := &ExpectedMapStringSliceInterface{}
2897+
e.cmd = m.factory.TSMGetWithArgs(m.ctx, filters, options)
2898+
m.pushExpect(e)
2899+
return e
28902900
}

0 commit comments

Comments
 (0)