Skip to content

Commit aaa7c80

Browse files
authored
Merge pull request #61 from monkey92t/new_cmd
feat: add ZMPop,BZMPop command
2 parents fde8176 + 83d47d3 commit aaa7c80

File tree

6 files changed

+92
-3
lines changed

6 files changed

+92
-3
lines changed

commands_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,6 +1653,14 @@ var _ = Describe("Commands", func() {
16531653
})
16541654
})
16551655

1656+
It("BZMPop", func() {
1657+
operationZSliceWithKeyCmd(clientMock, func() *ExpectedZSliceWithKey {
1658+
return clientMock.ExpectBZMPop(time.Minute, "max", 3, "key1", "key2")
1659+
}, func() *redis.ZSliceWithKeyCmd {
1660+
return client.BZMPop(ctx, time.Minute, "max", 3, "key1", "key2")
1661+
})
1662+
})
1663+
16561664
It("ZAdd", func() {
16571665
operationIntCmd(clientMock, func() *ExpectedInt {
16581666
return clientMock.ExpectZAdd("zset", redis.Z{
@@ -1829,6 +1837,14 @@ var _ = Describe("Commands", func() {
18291837
})
18301838
})
18311839

1840+
It("ZMPop", func() {
1841+
operationZSliceWithKeyCmd(clientMock, func() *ExpectedZSliceWithKey {
1842+
return clientMock.ExpectZMPop("min", 3, "key1", "key2")
1843+
}, func() *redis.ZSliceWithKeyCmd {
1844+
return client.ZMPop(ctx, "min", 3, "key1", "key2")
1845+
})
1846+
})
1847+
18321848
It("ZMScore", func() {
18331849
operationFloatSliceCmd(clientMock, func() *ExpectedFloatSlice {
18341850
return clientMock.ExpectZMScore("key", "m1", "m2", "m3")

expect.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ type baseMock interface {
204204

205205
ExpectBZPopMax(timeout time.Duration, keys ...string) *ExpectedZWithKey
206206
ExpectBZPopMin(timeout time.Duration, keys ...string) *ExpectedZWithKey
207+
ExpectBZMPop(timeout time.Duration, order string, count int64, keys ...string) *ExpectedZSliceWithKey
207208

208209
ExpectZAdd(key string, members ...redis.Z) *ExpectedInt
209210
ExpectZAddNX(key string, members ...redis.Z) *ExpectedInt
@@ -218,6 +219,7 @@ type baseMock interface {
218219
ExpectZInterWithScores(store *redis.ZStore) *ExpectedZSlice
219220
ExpectZInterCard(limit int64, keys ...string) *ExpectedInt
220221
ExpectZInterStore(destination string, store *redis.ZStore) *ExpectedInt
222+
ExpectZMPop(order string, count int64, keys ...string) *ExpectedZSliceWithKey
221223
ExpectZMScore(key string, members ...string) *ExpectedFloatSlice
222224
ExpectZPopMax(key string, count ...int64) *ExpectedZSlice
223225
ExpectZPopMin(key string, count ...int64) *ExpectedZSlice
@@ -1132,6 +1134,27 @@ func (cmd *ExpectedKeyValues) inflow(c redis.Cmder) {
11321134

11331135
// ------------------------------------------------------------
11341136

1137+
type ExpectedZSliceWithKey struct {
1138+
expectedBase
1139+
1140+
key string
1141+
val []redis.Z
1142+
}
1143+
1144+
func (cmd *ExpectedZSliceWithKey) SetVal(key string, val []redis.Z) {
1145+
cmd.setVal = true
1146+
cmd.key = key
1147+
cmd.val = make([]redis.Z, len(val))
1148+
copy(cmd.val, val)
1149+
}
1150+
1151+
func (cmd *ExpectedZSliceWithKey) inflow(c redis.Cmder) {
1152+
inflow(c, "key", cmd.key)
1153+
inflow(c, "val", cmd.val)
1154+
}
1155+
1156+
// ------------------------------------------------------------
1157+
11351158
type ExpectedSlowLog struct {
11361159
expectedBase
11371160

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.18
55
require (
66
github.com/onsi/ginkgo v1.16.5
77
github.com/onsi/gomega v1.25.0
8-
github.com/redis/go-redis/v9 v9.0.3-0.20230217130544-3b5361dc1067
8+
github.com/redis/go-redis/v9 v9.0.3-0.20230223112541-6045d2e4561c
99
)
1010

1111
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ github.com/onsi/gomega v1.25.0 h1:Vw7br2PCDYijJHSfBOWhov+8cAnUf8MfMaIOV323l6Y=
4040
github.com/onsi/gomega v1.25.0/go.mod h1:r+zV744Re+DiYCIPRlYOTxn0YkOLcAnW8k1xXdMPGhM=
4141
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4242
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
43-
github.com/redis/go-redis/v9 v9.0.3-0.20230217130544-3b5361dc1067 h1:e+fsCwY18jOIumrC71yG6ZcLUB7AwyS/GX+bspxzaOs=
44-
github.com/redis/go-redis/v9 v9.0.3-0.20230217130544-3b5361dc1067/go.mod h1:6kjiKPy72LcR7xJX4bMLuf+2Rd/tpBFk4YKJTZ1jTNA=
43+
github.com/redis/go-redis/v9 v9.0.3-0.20230223112541-6045d2e4561c h1:HU8TAwucfDaYERowWS2hTe3GyMx46wOdg5iNnhZzZuY=
44+
github.com/redis/go-redis/v9 v9.0.3-0.20230223112541-6045d2e4561c/go.mod h1:6kjiKPy72LcR7xJX4bMLuf+2Rd/tpBFk4YKJTZ1jTNA=
4545
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
4646
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
4747
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=

mock.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,13 @@ func (m *mock) ExpectBZPopMin(timeout time.Duration, keys ...string) *ExpectedZW
16391639
return e
16401640
}
16411641

1642+
func (m *mock) ExpectBZMPop(timeout time.Duration, order string, count int64, keys ...string) *ExpectedZSliceWithKey {
1643+
e := &ExpectedZSliceWithKey{}
1644+
e.cmd = m.factory.BZMPop(m.ctx, timeout, order, count, keys...)
1645+
m.pushExpect(e)
1646+
return e
1647+
}
1648+
16421649
func (m *mock) ExpectZAdd(key string, members ...redis.Z) *ExpectedInt {
16431650
e := &ExpectedInt{}
16441651
e.cmd = m.factory.ZAdd(m.ctx, key, members...)
@@ -1730,6 +1737,13 @@ func (m *mock) ExpectZInterStore(destination string, store *redis.ZStore) *Expec
17301737
return e
17311738
}
17321739

1740+
func (m *mock) ExpectZMPop(order string, count int64, keys ...string) *ExpectedZSliceWithKey {
1741+
e := &ExpectedZSliceWithKey{}
1742+
e.cmd = m.factory.ZMPop(m.ctx, order, count, keys...)
1743+
m.pushExpect(e)
1744+
return e
1745+
}
1746+
17331747
func (m *mock) ExpectZMScore(key string, members ...string) *ExpectedFloatSlice {
17341748
e := &ExpectedFloatSlice{}
17351749
e.cmd = m.factory.ZMScore(m.ctx, key, members...)

mock_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,3 +1366,39 @@ func operationKeyValuesCmd(base baseMock, expected func() *ExpectedKeyValues, ac
13661366
Expect(key).To(Equal("key1"))
13671367
Expect(val).To(Equal([]string{"v1", "v2"}))
13681368
}
1369+
1370+
func operationZSliceWithKeyCmd(base baseMock, expected func() *ExpectedZSliceWithKey, actual func() *redis.ZSliceWithKeyCmd) {
1371+
var (
1372+
setErr = errors.New("z slice with key cmd error")
1373+
key string
1374+
val []redis.Z
1375+
err error
1376+
)
1377+
1378+
base.ClearExpect()
1379+
expected().SetErr(setErr)
1380+
key, val, err = actual().Result()
1381+
Expect(err).To(Equal(setErr))
1382+
Expect(key).To(Equal(""))
1383+
Expect(val).To(Equal([]redis.Z(nil)))
1384+
1385+
base.ClearExpect()
1386+
expected()
1387+
key, val, err = actual().Result()
1388+
Expect(err).To(HaveOccurred())
1389+
Expect(key).To(Equal(""))
1390+
Expect(val).To(Equal([]redis.Z(nil)))
1391+
1392+
base.ClearExpect()
1393+
expected().SetVal("key1", []redis.Z{
1394+
{Score: 100, Member: "one"},
1395+
{Score: 200, Member: "two"},
1396+
})
1397+
key, val, err = actual().Result()
1398+
Expect(err).NotTo(HaveOccurred())
1399+
Expect(key).To(Equal("key1"))
1400+
Expect(val).To(Equal([]redis.Z{
1401+
{Score: 100, Member: "one"},
1402+
{Score: 200, Member: "two"},
1403+
}))
1404+
}

0 commit comments

Comments
 (0)