Skip to content

Commit

Permalink
Merge pull request #91 from antonyho/support-checking-unexpected-cmd-…
Browse files Browse the repository at this point in the history
…call

feat: Add UnexpectedCallsWereMade() to support checking unexpected command call
  • Loading branch information
monkey92t authored Jun 27, 2024
2 parents 80794c9 + 385baba commit 129fe29
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 19 deletions.
48 changes: 48 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ var _ = Describe("Client", func() {
pipe = client.TxPipeline()
})

AfterEach(func() {
hasUnexpectedCall, unexpectedCalls := clientMock.UnexpectedCallsWereMade()
Expect(hasUnexpectedCall).To(BeFalse())
Expect(unexpectedCalls).To(BeNil())
})

It("tx pipeline order", func() {
get := pipe.Get(ctx, "key1")
hashGet := pipe.HGet(ctx, "hash_key", "hash_field")
Expand Down Expand Up @@ -88,6 +94,12 @@ var _ = Describe("Client", func() {
pipe = client.Pipeline()
})

AfterEach(func() {
hasUnexpectedCall, unexpectedCalls := clientMock.UnexpectedCallsWereMade()
Expect(hasUnexpectedCall).To(BeFalse())
Expect(unexpectedCalls).To(BeNil())
})

It("pipeline order", func() {
clientMock.MatchExpectationsInOrder(true)

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

AfterEach(func() {
hasUnexpectedCall, unexpectedCalls := clientMock.UnexpectedCallsWereMade()
Expect(hasUnexpectedCall).To(BeTrue())
Expect(unexpectedCalls).ShouldNot(BeNil())
})

It("watch error", func() {
clientMock.MatchExpectationsInOrder(false)
txf := func(tx *redis.Tx) error {
Expand Down Expand Up @@ -220,6 +238,10 @@ var _ = Describe("Client", func() {
getSet := client.GetSet(ctx, "key", "0")
Expect(getSet.Err()).NotTo(HaveOccurred())
Expect(getSet.Val()).To(Equal("1"))

hasUnexpectedCall, unexpectedCalls := clientMock.UnexpectedCallsWereMade()
Expect(hasUnexpectedCall).To(BeFalse())
Expect(unexpectedCalls).To(BeNil())
})

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

hasUnexpectedCall, unexpectedCalls := clientMock.UnexpectedCallsWereMade()
Expect(hasUnexpectedCall).To(BeFalse())
Expect(unexpectedCalls).To(BeNil())

_ = client.GetSet(ctx, "key", "0")
})

Expand All @@ -247,6 +273,10 @@ var _ = Describe("Client", func() {
get := client.HGet(ctx, "key", "field")
Expect(get.Err()).To(HaveOccurred())
Expect(get.Val()).To(Equal(""))

hasUnexpectedCall, unexpectedCalls := clientMock.UnexpectedCallsWereMade()
Expect(hasUnexpectedCall).To(BeTrue())
Expect(unexpectedCalls).NotTo(BeNil())
})
})

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

AfterEach(func() {
hasUnexpectedCall, unexpectedCalls := clientMock.UnexpectedCallsWereMade()
Expect(hasUnexpectedCall).To(BeFalse())
Expect(unexpectedCalls).To(BeNil())
})

It("ordinary", func() {
get := client.Get(ctx, "key")
Expect(get.Err()).NotTo(HaveOccurred())
Expand All @@ -277,6 +313,12 @@ var _ = Describe("Client", func() {

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

AfterEach(func() {
hasUnexpectedCall, unexpectedCalls := clientMock.UnexpectedCallsWereMade()
Expect(hasUnexpectedCall).To(BeFalse())
Expect(unexpectedCalls).To(BeNil())
})

It("regexp match", func() {
clientMock.Regexp().ExpectSet("key", `^order_id_[0-9]{10}$`, 1*time.Second).SetVal("OK")
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")
Expand Down Expand Up @@ -320,6 +362,12 @@ var _ = Describe("Client", func() {

Describe("work error", func() {

AfterEach(func() {
hasUnexpectedCall, unexpectedCalls := clientMock.UnexpectedCallsWereMade()
Expect(hasUnexpectedCall).To(BeFalse())
Expect(unexpectedCalls).To(BeNil())
})

It("set error", func() {
clientMock.ExpectGet("key").SetErr(errors.New("set error"))

Expand Down
30 changes: 30 additions & 0 deletions cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ var _ = Describe("Cluster", func() {
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() {
Expand All @@ -63,6 +67,10 @@ var _ = Describe("Cluster", func() {
_ = 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")
})

Expand All @@ -76,6 +84,10 @@ var _ = Describe("Cluster", func() {
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())
})
})

Expand All @@ -89,6 +101,12 @@ var _ = Describe("Cluster", func() {
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())
Expand All @@ -106,6 +124,12 @@ var _ = Describe("Cluster", func() {

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")
Expand Down Expand Up @@ -149,6 +173,12 @@ var _ = Describe("Cluster", func() {

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"))

Expand Down
4 changes: 4 additions & 0 deletions expect.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ type baseMock interface {
// were met in order. If any of them was not met - an error is returned.
ExpectationsWereMet() error

// UnexpectedCallsWereMade returns any unexpected calls which were made.
// If any unexpected call was made, a list of unexpected call redis.Cmder is returned.
UnexpectedCallsWereMade() (bool, []redis.Cmder)

// MatchExpectationsInOrder gives an option whether to match all expectations in the order they were set or not.
MatchExpectationsInOrder(b bool)

Expand Down
48 changes: 29 additions & 19 deletions mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ type mock struct {

parent *mock

factory mockCmdable
client redis.Cmdable
expected []expectation
factory mockCmdable
client redis.Cmdable
expected []expectation
unexpected []redis.Cmder

strictOrder bool

Expand Down Expand Up @@ -180,6 +181,7 @@ func (m *mock) process(cmd redis.Cmder) (err error) {
}
err = fmt.Errorf(msg, cmd.Args())
cmd.SetErr(err)
m.unexpected = append(m.unexpected, cmd)
return err
}

Expand Down Expand Up @@ -362,6 +364,7 @@ func (m *mock) ClearExpect() {
return
}
m.expected = nil
m.unexpected = nil
}

func (m *mock) Regexp() *mock {
Expand Down Expand Up @@ -402,6 +405,13 @@ func (m *mock) ExpectationsWereMet() error {
return nil
}

func (m *mock) UnexpectedCallsWereMade() (bool, []redis.Cmder) {
if m.parent != nil {
return m.parent.UnexpectedCallsWereMade()
}
return len(m.unexpected) > 0, m.unexpected
}

func (m *mock) MatchExpectationsInOrder(b bool) {
if m.parent != nil {
m.MatchExpectationsInOrder(b)
Expand Down Expand Up @@ -2862,29 +2872,29 @@ func (m *mock) ExpectTSMRangeWithArgs(fromTimestamp int, toTimestamp int, filter
}

func (m *mock) ExpectTSMRevRange(fromTimestamp int, toTimestamp int, filterExpr []string) *ExpectedMapStringSliceInterface {
e := &ExpectedMapStringSliceInterface{}
e.cmd = m.factory.TSMRevRange(m.ctx, fromTimestamp, toTimestamp, filterExpr)
m.pushExpect(e)
return e
e := &ExpectedMapStringSliceInterface{}
e.cmd = m.factory.TSMRevRange(m.ctx, fromTimestamp, toTimestamp, filterExpr)
m.pushExpect(e)
return e
}

func (m *mock) ExpectTSMRevRangeWithArgs(fromTimestamp int, toTimestamp int, filterExpr []string, options *redis.TSMRevRangeOptions) *ExpectedMapStringSliceInterface {
e := &ExpectedMapStringSliceInterface{}
e.cmd = m.factory.TSMRevRangeWithArgs(m.ctx, fromTimestamp, toTimestamp, filterExpr, options)
m.pushExpect(e)
return e
e := &ExpectedMapStringSliceInterface{}
e.cmd = m.factory.TSMRevRangeWithArgs(m.ctx, fromTimestamp, toTimestamp, filterExpr, options)
m.pushExpect(e)
return e
}

func (m *mock) ExpectTSMGet(filters []string) *ExpectedMapStringSliceInterface {
e := &ExpectedMapStringSliceInterface{}
e.cmd = m.factory.TSMGet(m.ctx, filters)
m.pushExpect(e)
return e
e := &ExpectedMapStringSliceInterface{}
e.cmd = m.factory.TSMGet(m.ctx, filters)
m.pushExpect(e)
return e
}

func (m *mock) ExpectTSMGetWithArgs(filters []string, options *redis.TSMGetOptions) *ExpectedMapStringSliceInterface {
e := &ExpectedMapStringSliceInterface{}
e.cmd = m.factory.TSMGetWithArgs(m.ctx, filters, options)
m.pushExpect(e)
return e
e := &ExpectedMapStringSliceInterface{}
e.cmd = m.factory.TSMGetWithArgs(m.ctx, filters, options)
m.pushExpect(e)
return e
}

0 comments on commit 129fe29

Please sign in to comment.