| 1 |
package redis_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"fmt" |
| 6 |
"reflect" |
| 7 |
"time" |
| 8 |
|
| 9 |
. "github.com/onsi/ginkgo" |
| 10 |
. "github.com/onsi/gomega" |
| 11 |
|
| 12 |
"gopkg.in/redis.v5" |
| 13 |
) |
| 14 |
|
| 15 |
var _ = Describe("Commands", func() { |
| 16 |
var client *redis.Client |
| 17 |
|
| 18 |
BeforeEach(func() { |
| 19 |
client = redis.NewClient(redisOptions()) |
| 20 |
Expect(client.FlushDb().Err()).NotTo(HaveOccurred()) |
| 21 |
}) |
| 22 |
|
| 23 |
AfterEach(func() { |
| 24 |
Expect(client.Close()).NotTo(HaveOccurred()) |
| 25 |
}) |
| 26 |
|
| 27 |
Describe("server", func() { |
| 28 |
|
| 29 |
It("should Auth", func() { |
| 30 |
_, err := client.Pipelined(func(pipe *redis.Pipeline) error { |
| 31 |
pipe.Auth("password") |
| 32 |
return nil |
| 33 |
}) |
| 34 |
Expect(err).To(MatchError("ERR Client sent AUTH, but no password is set")) |
| 35 |
}) |
| 36 |
|
| 37 |
It("should Echo", func() { |
| 38 |
pipe := client.Pipeline() |
| 39 |
echo := pipe.Echo("hello") |
| 40 |
_, err := pipe.Exec() |
| 41 |
Expect(err).NotTo(HaveOccurred()) |
| 42 |
|
| 43 |
Expect(echo.Err()).NotTo(HaveOccurred()) |
| 44 |
Expect(echo.Val()).To(Equal("hello")) |
| 45 |
}) |
| 46 |
|
| 47 |
It("should Ping", func() { |
| 48 |
ping := client.Ping() |
| 49 |
Expect(ping.Err()).NotTo(HaveOccurred()) |
| 50 |
Expect(ping.Val()).To(Equal("PONG")) |
| 51 |
}) |
| 52 |
|
| 53 |
It("should Wait", func() { |
| 54 |
// assume testing on single redis instance |
| 55 |
start := time.Now() |
| 56 |
wait := client.Wait(1, time.Second) |
| 57 |
Expect(wait.Err()).NotTo(HaveOccurred()) |
| 58 |
Expect(wait.Val()).To(Equal(int64(0))) |
| 59 |
Expect(time.Now()).To(BeTemporally("~", start.Add(time.Second), 800*time.Millisecond)) |
| 60 |
}) |
| 61 |
|
| 62 |
It("should Select", func() { |
| 63 |
pipe := client.Pipeline() |
| 64 |
sel := pipe.Select(1) |
| 65 |
_, err := pipe.Exec() |
| 66 |
Expect(err).NotTo(HaveOccurred()) |
| 67 |
|
| 68 |
Expect(sel.Err()).NotTo(HaveOccurred()) |
| 69 |
Expect(sel.Val()).To(Equal("OK")) |
| 70 |
}) |
| 71 |
|
| 72 |
It("should BgRewriteAOF", func() { |
| 73 |
Skip("flaky test") |
| 74 |
|
| 75 |
val, err := client.BgRewriteAOF().Result() |
| 76 |
Expect(err).NotTo(HaveOccurred()) |
| 77 |
Expect(val).To(ContainSubstring("Background append only file rewriting")) |
| 78 |
}) |
| 79 |
|
| 80 |
It("should BgSave", func() { |
| 81 |
Skip("flaky test") |
| 82 |
|
| 83 |
// workaround for "ERR Can't BGSAVE while AOF log rewriting is in progress" |
| 84 |
Eventually(func() string { |
| 85 |
return client.BgSave().Val() |
| 86 |
}, "30s").Should(Equal("Background saving started")) |
| 87 |
}) |
| 88 |
|
| 89 |
It("should ClientKill", func() { |
| 90 |
r := client.ClientKill("1.1.1.1:1111") |
| 91 |
Expect(r.Err()).To(MatchError("ERR No such client")) |
| 92 |
Expect(r.Val()).To(Equal("")) |
| 93 |
}) |
| 94 |
|
| 95 |
It("should ClientPause", func() { |
| 96 |
err := client.ClientPause(time.Second).Err() |
| 97 |
Expect(err).NotTo(HaveOccurred()) |
| 98 |
|
| 99 |
start := time.Now() |
| 100 |
err = client.Ping().Err() |
| 101 |
Expect(err).NotTo(HaveOccurred()) |
| 102 |
Expect(time.Now()).To(BeTemporally("~", start.Add(time.Second), 800*time.Millisecond)) |
| 103 |
}) |
| 104 |
|
| 105 |
It("should ClientSetName and ClientGetName", func() { |
| 106 |
pipe := client.Pipeline() |
| 107 |
set := pipe.ClientSetName("theclientname") |
| 108 |
get := pipe.ClientGetName() |
| 109 |
_, err := pipe.Exec() |
| 110 |
Expect(err).NotTo(HaveOccurred()) |
| 111 |
|
| 112 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 113 |
Expect(set.Val()).To(BeTrue()) |
| 114 |
|
| 115 |
Expect(get.Err()).NotTo(HaveOccurred()) |
| 116 |
Expect(get.Val()).To(Equal("theclientname")) |
| 117 |
|
| 118 |
}) |
| 119 |
|
| 120 |
It("should ConfigGet", func() { |
| 121 |
r := client.ConfigGet("*") |
| 122 |
Expect(r.Err()).NotTo(HaveOccurred()) |
| 123 |
Expect(r.Val()).NotTo(BeEmpty()) |
| 124 |
}) |
| 125 |
|
| 126 |
It("should ConfigResetStat", func() { |
| 127 |
r := client.ConfigResetStat() |
| 128 |
Expect(r.Err()).NotTo(HaveOccurred()) |
| 129 |
Expect(r.Val()).To(Equal("OK")) |
| 130 |
}) |
| 131 |
|
| 132 |
It("should ConfigSet", func() { |
| 133 |
configGet := client.ConfigGet("maxmemory") |
| 134 |
Expect(configGet.Err()).NotTo(HaveOccurred()) |
| 135 |
Expect(configGet.Val()).To(HaveLen(2)) |
| 136 |
Expect(configGet.Val()[0]).To(Equal("maxmemory")) |
| 137 |
|
| 138 |
configSet := client.ConfigSet("maxmemory", configGet.Val()[1].(string)) |
| 139 |
Expect(configSet.Err()).NotTo(HaveOccurred()) |
| 140 |
Expect(configSet.Val()).To(Equal("OK")) |
| 141 |
}) |
| 142 |
|
| 143 |
It("should DbSize", func() { |
| 144 |
dbSize := client.DbSize() |
| 145 |
Expect(dbSize.Err()).NotTo(HaveOccurred()) |
| 146 |
Expect(dbSize.Val()).To(Equal(int64(0))) |
| 147 |
}) |
| 148 |
|
| 149 |
It("should Info", func() { |
| 150 |
info := client.Info() |
| 151 |
Expect(info.Err()).NotTo(HaveOccurred()) |
| 152 |
Expect(info.Val()).NotTo(Equal("")) |
| 153 |
}) |
| 154 |
|
| 155 |
It("should Info cpu", func() { |
| 156 |
info := client.Info("cpu") |
| 157 |
Expect(info.Err()).NotTo(HaveOccurred()) |
| 158 |
Expect(info.Val()).NotTo(Equal("")) |
| 159 |
Expect(info.Val()).To(ContainSubstring(`used_cpu_sys`)) |
| 160 |
}) |
| 161 |
|
| 162 |
It("should LastSave", func() { |
| 163 |
lastSave := client.LastSave() |
| 164 |
Expect(lastSave.Err()).NotTo(HaveOccurred()) |
| 165 |
Expect(lastSave.Val()).NotTo(Equal(0)) |
| 166 |
}) |
| 167 |
|
| 168 |
It("should Save", func() { |
| 169 |
// workaround for "ERR Background save already in progress" |
| 170 |
Eventually(func() string { |
| 171 |
return client.Save().Val() |
| 172 |
}, "10s").Should(Equal("OK")) |
| 173 |
}) |
| 174 |
|
| 175 |
It("should SlaveOf", func() { |
| 176 |
slaveOf := client.SlaveOf("localhost", "8888") |
| 177 |
Expect(slaveOf.Err()).NotTo(HaveOccurred()) |
| 178 |
Expect(slaveOf.Val()).To(Equal("OK")) |
| 179 |
|
| 180 |
slaveOf = client.SlaveOf("NO", "ONE") |
| 181 |
Expect(slaveOf.Err()).NotTo(HaveOccurred()) |
| 182 |
Expect(slaveOf.Val()).To(Equal("OK")) |
| 183 |
}) |
| 184 |
|
| 185 |
It("should Time", func() { |
| 186 |
tm, err := client.Time().Result() |
| 187 |
Expect(err).NotTo(HaveOccurred()) |
| 188 |
Expect(tm).To(BeTemporally("~", time.Now(), 3*time.Second)) |
| 189 |
}) |
| 190 |
|
| 191 |
}) |
| 192 |
|
| 193 |
Describe("debugging", func() { |
| 194 |
|
| 195 |
It("should DebugObject", func() { |
| 196 |
debug := client.DebugObject("foo") |
| 197 |
Expect(debug.Err()).To(HaveOccurred()) |
| 198 |
Expect(debug.Err().Error()).To(Equal("ERR no such key")) |
| 199 |
|
| 200 |
client.Set("foo", "bar", 0) |
| 201 |
debug = client.DebugObject("foo") |
| 202 |
Expect(debug.Err()).NotTo(HaveOccurred()) |
| 203 |
Expect(debug.Val()).To(ContainSubstring(`serializedlength:4`)) |
| 204 |
}) |
| 205 |
|
| 206 |
}) |
| 207 |
|
| 208 |
Describe("keys", func() { |
| 209 |
|
| 210 |
It("should Del", func() { |
| 211 |
err := client.Set("key1", "Hello", 0).Err() |
| 212 |
Expect(err).NotTo(HaveOccurred()) |
| 213 |
err = client.Set("key2", "World", 0).Err() |
| 214 |
Expect(err).NotTo(HaveOccurred()) |
| 215 |
|
| 216 |
n, err := client.Del("key1", "key2", "key3").Result() |
| 217 |
Expect(err).NotTo(HaveOccurred()) |
| 218 |
Expect(n).To(Equal(int64(2))) |
| 219 |
}) |
| 220 |
|
| 221 |
It("should Unlink", func() { |
| 222 |
err := client.Set("key1", "Hello", 0).Err() |
| 223 |
Expect(err).NotTo(HaveOccurred()) |
| 224 |
err = client.Set("key2", "World", 0).Err() |
| 225 |
Expect(err).NotTo(HaveOccurred()) |
| 226 |
|
| 227 |
n, err := client.Unlink("key1", "key2", "key3").Result() |
| 228 |
Expect(err).NotTo(HaveOccurred()) |
| 229 |
Expect(n).To(Equal(int64(2))) |
| 230 |
}) |
| 231 |
|
| 232 |
It("should Dump", func() { |
| 233 |
set := client.Set("key", "hello", 0) |
| 234 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 235 |
Expect(set.Val()).To(Equal("OK")) |
| 236 |
|
| 237 |
dump := client.Dump("key") |
| 238 |
Expect(dump.Err()).NotTo(HaveOccurred()) |
| 239 |
Expect(dump.Val()).NotTo(BeEmpty()) |
| 240 |
}) |
| 241 |
|
| 242 |
It("should Exists", func() { |
| 243 |
set := client.Set("key1", "Hello", 0) |
| 244 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 245 |
Expect(set.Val()).To(Equal("OK")) |
| 246 |
|
| 247 |
exists := client.Exists("key1") |
| 248 |
Expect(exists.Err()).NotTo(HaveOccurred()) |
| 249 |
Expect(exists.Val()).To(Equal(true)) |
| 250 |
|
| 251 |
exists = client.Exists("key2") |
| 252 |
Expect(exists.Err()).NotTo(HaveOccurred()) |
| 253 |
Expect(exists.Val()).To(Equal(false)) |
| 254 |
|
| 255 |
existsMul := client.ExistsMulti("key1", "key2") |
| 256 |
Expect(existsMul.Err()).NotTo(HaveOccurred()) |
| 257 |
Expect(existsMul.Val()).To(Equal(int64(1))) |
| 258 |
|
| 259 |
existsMul = client.ExistsMulti("key1", "key1") |
| 260 |
Expect(existsMul.Err()).NotTo(HaveOccurred()) |
| 261 |
Expect(existsMul.Val()).To(Equal(int64(2))) |
| 262 |
}) |
| 263 |
|
| 264 |
It("should Expire", func() { |
| 265 |
set := client.Set("key", "Hello", 0) |
| 266 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 267 |
Expect(set.Val()).To(Equal("OK")) |
| 268 |
|
| 269 |
expire := client.Expire("key", 10*time.Second) |
| 270 |
Expect(expire.Err()).NotTo(HaveOccurred()) |
| 271 |
Expect(expire.Val()).To(Equal(true)) |
| 272 |
|
| 273 |
ttl := client.TTL("key") |
| 274 |
Expect(ttl.Err()).NotTo(HaveOccurred()) |
| 275 |
Expect(ttl.Val()).To(Equal(10 * time.Second)) |
| 276 |
|
| 277 |
set = client.Set("key", "Hello World", 0) |
| 278 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 279 |
Expect(set.Val()).To(Equal("OK")) |
| 280 |
|
| 281 |
ttl = client.TTL("key") |
| 282 |
Expect(ttl.Err()).NotTo(HaveOccurred()) |
| 283 |
Expect(ttl.Val() < 0).To(Equal(true)) |
| 284 |
}) |
| 285 |
|
| 286 |
It("should ExpireAt", func() { |
| 287 |
set := client.Set("key", "Hello", 0) |
| 288 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 289 |
Expect(set.Val()).To(Equal("OK")) |
| 290 |
|
| 291 |
exists := client.Exists("key") |
| 292 |
Expect(exists.Err()).NotTo(HaveOccurred()) |
| 293 |
Expect(exists.Val()).To(Equal(true)) |
| 294 |
|
| 295 |
expireAt := client.ExpireAt("key", time.Now().Add(-time.Hour)) |
| 296 |
Expect(expireAt.Err()).NotTo(HaveOccurred()) |
| 297 |
Expect(expireAt.Val()).To(Equal(true)) |
| 298 |
|
| 299 |
exists = client.Exists("key") |
| 300 |
Expect(exists.Err()).NotTo(HaveOccurred()) |
| 301 |
Expect(exists.Val()).To(Equal(false)) |
| 302 |
}) |
| 303 |
|
| 304 |
It("should Keys", func() { |
| 305 |
mset := client.MSet("one", "1", "two", "2", "three", "3", "four", "4") |
| 306 |
Expect(mset.Err()).NotTo(HaveOccurred()) |
| 307 |
Expect(mset.Val()).To(Equal("OK")) |
| 308 |
|
| 309 |
keys := client.Keys("*o*") |
| 310 |
Expect(keys.Err()).NotTo(HaveOccurred()) |
| 311 |
Expect(keys.Val()).To(ConsistOf([]string{"four", "one", "two"})) |
| 312 |
|
| 313 |
keys = client.Keys("t??") |
| 314 |
Expect(keys.Err()).NotTo(HaveOccurred()) |
| 315 |
Expect(keys.Val()).To(Equal([]string{"two"})) |
| 316 |
|
| 317 |
keys = client.Keys("*") |
| 318 |
Expect(keys.Err()).NotTo(HaveOccurred()) |
| 319 |
Expect(keys.Val()).To(ConsistOf([]string{"four", "one", "three", "two"})) |
| 320 |
}) |
| 321 |
|
| 322 |
It("should Migrate", func() { |
| 323 |
migrate := client.Migrate("localhost", redisSecondaryPort, "key", 0, 0) |
| 324 |
Expect(migrate.Err()).NotTo(HaveOccurred()) |
| 325 |
Expect(migrate.Val()).To(Equal("NOKEY")) |
| 326 |
|
| 327 |
set := client.Set("key", "hello", 0) |
| 328 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 329 |
Expect(set.Val()).To(Equal("OK")) |
| 330 |
|
| 331 |
migrate = client.Migrate("localhost", redisSecondaryPort, "key", 0, 0) |
| 332 |
Expect(migrate.Err()).To(MatchError("IOERR error or timeout writing to target instance")) |
| 333 |
Expect(migrate.Val()).To(Equal("")) |
| 334 |
}) |
| 335 |
|
| 336 |
It("should Move", func() { |
| 337 |
move := client.Move("key", 2) |
| 338 |
Expect(move.Err()).NotTo(HaveOccurred()) |
| 339 |
Expect(move.Val()).To(Equal(false)) |
| 340 |
|
| 341 |
set := client.Set("key", "hello", 0) |
| 342 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 343 |
Expect(set.Val()).To(Equal("OK")) |
| 344 |
|
| 345 |
move = client.Move("key", 2) |
| 346 |
Expect(move.Err()).NotTo(HaveOccurred()) |
| 347 |
Expect(move.Val()).To(Equal(true)) |
| 348 |
|
| 349 |
get := client.Get("key") |
| 350 |
Expect(get.Err()).To(Equal(redis.Nil)) |
| 351 |
Expect(get.Val()).To(Equal("")) |
| 352 |
|
| 353 |
pipe := client.Pipeline() |
| 354 |
pipe.Select(2) |
| 355 |
get = pipe.Get("key") |
| 356 |
pipe.FlushDb() |
| 357 |
|
| 358 |
_, err := pipe.Exec() |
| 359 |
Expect(err).NotTo(HaveOccurred()) |
| 360 |
Expect(get.Val()).To(Equal("hello")) |
| 361 |
}) |
| 362 |
|
| 363 |
It("should Object", func() { |
| 364 |
set := client.Set("key", "hello", 0) |
| 365 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 366 |
Expect(set.Val()).To(Equal("OK")) |
| 367 |
|
| 368 |
refCount := client.ObjectRefCount("key") |
| 369 |
Expect(refCount.Err()).NotTo(HaveOccurred()) |
| 370 |
Expect(refCount.Val()).To(Equal(int64(1))) |
| 371 |
|
| 372 |
err := client.ObjectEncoding("key").Err() |
| 373 |
Expect(err).NotTo(HaveOccurred()) |
| 374 |
|
| 375 |
idleTime := client.ObjectIdleTime("key") |
| 376 |
Expect(idleTime.Err()).NotTo(HaveOccurred()) |
| 377 |
Expect(idleTime.Val()).To(Equal(time.Duration(0))) |
| 378 |
}) |
| 379 |
|
| 380 |
It("should Persist", func() { |
| 381 |
set := client.Set("key", "Hello", 0) |
| 382 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 383 |
Expect(set.Val()).To(Equal("OK")) |
| 384 |
|
| 385 |
expire := client.Expire("key", 10*time.Second) |
| 386 |
Expect(expire.Err()).NotTo(HaveOccurred()) |
| 387 |
Expect(expire.Val()).To(Equal(true)) |
| 388 |
|
| 389 |
ttl := client.TTL("key") |
| 390 |
Expect(ttl.Err()).NotTo(HaveOccurred()) |
| 391 |
Expect(ttl.Val()).To(Equal(10 * time.Second)) |
| 392 |
|
| 393 |
persist := client.Persist("key") |
| 394 |
Expect(persist.Err()).NotTo(HaveOccurred()) |
| 395 |
Expect(persist.Val()).To(Equal(true)) |
| 396 |
|
| 397 |
ttl = client.TTL("key") |
| 398 |
Expect(ttl.Err()).NotTo(HaveOccurred()) |
| 399 |
Expect(ttl.Val() < 0).To(Equal(true)) |
| 400 |
}) |
| 401 |
|
| 402 |
It("should PExpire", func() { |
| 403 |
set := client.Set("key", "Hello", 0) |
| 404 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 405 |
Expect(set.Val()).To(Equal("OK")) |
| 406 |
|
| 407 |
expiration := 900 * time.Millisecond |
| 408 |
pexpire := client.PExpire("key", expiration) |
| 409 |
Expect(pexpire.Err()).NotTo(HaveOccurred()) |
| 410 |
Expect(pexpire.Val()).To(Equal(true)) |
| 411 |
|
| 412 |
ttl := client.TTL("key") |
| 413 |
Expect(ttl.Err()).NotTo(HaveOccurred()) |
| 414 |
Expect(ttl.Val()).To(Equal(time.Second)) |
| 415 |
|
| 416 |
pttl := client.PTTL("key") |
| 417 |
Expect(pttl.Err()).NotTo(HaveOccurred()) |
| 418 |
Expect(pttl.Val()).To(BeNumerically("~", expiration, 10*time.Millisecond)) |
| 419 |
}) |
| 420 |
|
| 421 |
It("should PExpireAt", func() { |
| 422 |
set := client.Set("key", "Hello", 0) |
| 423 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 424 |
Expect(set.Val()).To(Equal("OK")) |
| 425 |
|
| 426 |
expiration := 900 * time.Millisecond |
| 427 |
pexpireat := client.PExpireAt("key", time.Now().Add(expiration)) |
| 428 |
Expect(pexpireat.Err()).NotTo(HaveOccurred()) |
| 429 |
Expect(pexpireat.Val()).To(Equal(true)) |
| 430 |
|
| 431 |
ttl := client.TTL("key") |
| 432 |
Expect(ttl.Err()).NotTo(HaveOccurred()) |
| 433 |
Expect(ttl.Val()).To(Equal(time.Second)) |
| 434 |
|
| 435 |
pttl := client.PTTL("key") |
| 436 |
Expect(pttl.Err()).NotTo(HaveOccurred()) |
| 437 |
Expect(pttl.Val()).To(BeNumerically("~", expiration, 10*time.Millisecond)) |
| 438 |
}) |
| 439 |
|
| 440 |
It("should PTTL", func() { |
| 441 |
set := client.Set("key", "Hello", 0) |
| 442 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 443 |
Expect(set.Val()).To(Equal("OK")) |
| 444 |
|
| 445 |
expiration := time.Second |
| 446 |
expire := client.Expire("key", expiration) |
| 447 |
Expect(expire.Err()).NotTo(HaveOccurred()) |
| 448 |
Expect(set.Val()).To(Equal("OK")) |
| 449 |
|
| 450 |
pttl := client.PTTL("key") |
| 451 |
Expect(pttl.Err()).NotTo(HaveOccurred()) |
| 452 |
Expect(pttl.Val()).To(BeNumerically("~", expiration, 10*time.Millisecond)) |
| 453 |
}) |
| 454 |
|
| 455 |
It("should RandomKey", func() { |
| 456 |
randomKey := client.RandomKey() |
| 457 |
Expect(randomKey.Err()).To(Equal(redis.Nil)) |
| 458 |
Expect(randomKey.Val()).To(Equal("")) |
| 459 |
|
| 460 |
set := client.Set("key", "hello", 0) |
| 461 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 462 |
Expect(set.Val()).To(Equal("OK")) |
| 463 |
|
| 464 |
randomKey = client.RandomKey() |
| 465 |
Expect(randomKey.Err()).NotTo(HaveOccurred()) |
| 466 |
Expect(randomKey.Val()).To(Equal("key")) |
| 467 |
}) |
| 468 |
|
| 469 |
It("should Rename", func() { |
| 470 |
set := client.Set("key", "hello", 0) |
| 471 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 472 |
Expect(set.Val()).To(Equal("OK")) |
| 473 |
|
| 474 |
status := client.Rename("key", "key1") |
| 475 |
Expect(status.Err()).NotTo(HaveOccurred()) |
| 476 |
Expect(status.Val()).To(Equal("OK")) |
| 477 |
|
| 478 |
get := client.Get("key1") |
| 479 |
Expect(get.Err()).NotTo(HaveOccurred()) |
| 480 |
Expect(get.Val()).To(Equal("hello")) |
| 481 |
}) |
| 482 |
|
| 483 |
It("should RenameNX", func() { |
| 484 |
set := client.Set("key", "hello", 0) |
| 485 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 486 |
Expect(set.Val()).To(Equal("OK")) |
| 487 |
|
| 488 |
renameNX := client.RenameNX("key", "key1") |
| 489 |
Expect(renameNX.Err()).NotTo(HaveOccurred()) |
| 490 |
Expect(renameNX.Val()).To(Equal(true)) |
| 491 |
|
| 492 |
get := client.Get("key1") |
| 493 |
Expect(get.Err()).NotTo(HaveOccurred()) |
| 494 |
Expect(get.Val()).To(Equal("hello")) |
| 495 |
}) |
| 496 |
|
| 497 |
It("should Restore", func() { |
| 498 |
err := client.Set("key", "hello", 0).Err() |
| 499 |
Expect(err).NotTo(HaveOccurred()) |
| 500 |
|
| 501 |
dump := client.Dump("key") |
| 502 |
Expect(dump.Err()).NotTo(HaveOccurred()) |
| 503 |
|
| 504 |
err = client.Del("key").Err() |
| 505 |
Expect(err).NotTo(HaveOccurred()) |
| 506 |
|
| 507 |
restore, err := client.Restore("key", 0, dump.Val()).Result() |
| 508 |
Expect(err).NotTo(HaveOccurred()) |
| 509 |
Expect(restore).To(Equal("OK")) |
| 510 |
|
| 511 |
type_, err := client.Type("key").Result() |
| 512 |
Expect(err).NotTo(HaveOccurred()) |
| 513 |
Expect(type_).To(Equal("string")) |
| 514 |
|
| 515 |
val, err := client.Get("key").Result() |
| 516 |
Expect(err).NotTo(HaveOccurred()) |
| 517 |
Expect(val).To(Equal("hello")) |
| 518 |
}) |
| 519 |
|
| 520 |
It("should RestoreReplace", func() { |
| 521 |
err := client.Set("key", "hello", 0).Err() |
| 522 |
Expect(err).NotTo(HaveOccurred()) |
| 523 |
|
| 524 |
dump := client.Dump("key") |
| 525 |
Expect(dump.Err()).NotTo(HaveOccurred()) |
| 526 |
|
| 527 |
restore, err := client.RestoreReplace("key", 0, dump.Val()).Result() |
| 528 |
Expect(err).NotTo(HaveOccurred()) |
| 529 |
Expect(restore).To(Equal("OK")) |
| 530 |
|
| 531 |
type_, err := client.Type("key").Result() |
| 532 |
Expect(err).NotTo(HaveOccurred()) |
| 533 |
Expect(type_).To(Equal("string")) |
| 534 |
|
| 535 |
val, err := client.Get("key").Result() |
| 536 |
Expect(err).NotTo(HaveOccurred()) |
| 537 |
Expect(val).To(Equal("hello")) |
| 538 |
}) |
| 539 |
|
| 540 |
It("should Sort", func() { |
| 541 |
size, err := client.LPush("list", "1").Result() |
| 542 |
Expect(err).NotTo(HaveOccurred()) |
| 543 |
Expect(size).To(Equal(int64(1))) |
| 544 |
|
| 545 |
size, err = client.LPush("list", "3").Result() |
| 546 |
Expect(err).NotTo(HaveOccurred()) |
| 547 |
Expect(size).To(Equal(int64(2))) |
| 548 |
|
| 549 |
size, err = client.LPush("list", "2").Result() |
| 550 |
Expect(err).NotTo(HaveOccurred()) |
| 551 |
Expect(size).To(Equal(int64(3))) |
| 552 |
|
| 553 |
els, err := client.Sort("list", redis.Sort{ |
| 554 |
Offset: 0, |
| 555 |
Count: 2, |
| 556 |
Order: "ASC", |
| 557 |
}).Result() |
| 558 |
Expect(err).NotTo(HaveOccurred()) |
| 559 |
Expect(els).To(Equal([]string{"1", "2"})) |
| 560 |
}) |
| 561 |
|
| 562 |
It("should Sort and Get", func() { |
| 563 |
size, err := client.LPush("list", "1").Result() |
| 564 |
Expect(err).NotTo(HaveOccurred()) |
| 565 |
Expect(size).To(Equal(int64(1))) |
| 566 |
|
| 567 |
size, err = client.LPush("list", "3").Result() |
| 568 |
Expect(err).NotTo(HaveOccurred()) |
| 569 |
Expect(size).To(Equal(int64(2))) |
| 570 |
|
| 571 |
size, err = client.LPush("list", "2").Result() |
| 572 |
Expect(err).NotTo(HaveOccurred()) |
| 573 |
Expect(size).To(Equal(int64(3))) |
| 574 |
|
| 575 |
err = client.Set("object_2", "value2", 0).Err() |
| 576 |
Expect(err).NotTo(HaveOccurred()) |
| 577 |
|
| 578 |
{ |
| 579 |
els, err := client.Sort("list", redis.Sort{ |
| 580 |
Get: []string{"object_*"}, |
| 581 |
}).Result() |
| 582 |
Expect(err).NotTo(HaveOccurred()) |
| 583 |
Expect(els).To(Equal([]string{"", "value2", ""})) |
| 584 |
} |
| 585 |
|
| 586 |
{ |
| 587 |
els, err := client.SortInterfaces("list", redis.Sort{ |
| 588 |
Get: []string{"object_*"}, |
| 589 |
}).Result() |
| 590 |
Expect(err).NotTo(HaveOccurred()) |
| 591 |
Expect(els).To(Equal([]interface{}{nil, "value2", nil})) |
| 592 |
} |
| 593 |
}) |
| 594 |
|
| 595 |
It("should TTL", func() { |
| 596 |
ttl := client.TTL("key") |
| 597 |
Expect(ttl.Err()).NotTo(HaveOccurred()) |
| 598 |
Expect(ttl.Val() < 0).To(Equal(true)) |
| 599 |
|
| 600 |
set := client.Set("key", "hello", 0) |
| 601 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 602 |
Expect(set.Val()).To(Equal("OK")) |
| 603 |
|
| 604 |
expire := client.Expire("key", 60*time.Second) |
| 605 |
Expect(expire.Err()).NotTo(HaveOccurred()) |
| 606 |
Expect(expire.Val()).To(Equal(true)) |
| 607 |
|
| 608 |
ttl = client.TTL("key") |
| 609 |
Expect(ttl.Err()).NotTo(HaveOccurred()) |
| 610 |
Expect(ttl.Val()).To(Equal(60 * time.Second)) |
| 611 |
}) |
| 612 |
|
| 613 |
It("should Type", func() { |
| 614 |
set := client.Set("key", "hello", 0) |
| 615 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 616 |
Expect(set.Val()).To(Equal("OK")) |
| 617 |
|
| 618 |
type_ := client.Type("key") |
| 619 |
Expect(type_.Err()).NotTo(HaveOccurred()) |
| 620 |
Expect(type_.Val()).To(Equal("string")) |
| 621 |
}) |
| 622 |
|
| 623 |
}) |
| 624 |
|
| 625 |
Describe("scanning", func() { |
| 626 |
|
| 627 |
It("should Scan", func() { |
| 628 |
for i := 0; i < 1000; i++ { |
| 629 |
set := client.Set(fmt.Sprintf("key%d", i), "hello", 0) |
| 630 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 631 |
} |
| 632 |
|
| 633 |
keys, cursor, err := client.Scan(0, "", 0).Result() |
| 634 |
Expect(err).NotTo(HaveOccurred()) |
| 635 |
Expect(keys).NotTo(BeEmpty()) |
| 636 |
Expect(cursor).NotTo(BeZero()) |
| 637 |
}) |
| 638 |
|
| 639 |
It("should SScan", func() { |
| 640 |
for i := 0; i < 1000; i++ { |
| 641 |
sadd := client.SAdd("myset", fmt.Sprintf("member%d", i)) |
| 642 |
Expect(sadd.Err()).NotTo(HaveOccurred()) |
| 643 |
} |
| 644 |
|
| 645 |
keys, cursor, err := client.SScan("myset", 0, "", 0).Result() |
| 646 |
Expect(err).NotTo(HaveOccurred()) |
| 647 |
Expect(keys).NotTo(BeEmpty()) |
| 648 |
Expect(cursor).NotTo(BeZero()) |
| 649 |
}) |
| 650 |
|
| 651 |
It("should HScan", func() { |
| 652 |
for i := 0; i < 1000; i++ { |
| 653 |
sadd := client.HSet("myhash", fmt.Sprintf("key%d", i), "hello") |
| 654 |
Expect(sadd.Err()).NotTo(HaveOccurred()) |
| 655 |
} |
| 656 |
|
| 657 |
keys, cursor, err := client.HScan("myhash", 0, "", 0).Result() |
| 658 |
Expect(err).NotTo(HaveOccurred()) |
| 659 |
Expect(keys).NotTo(BeEmpty()) |
| 660 |
Expect(cursor).NotTo(BeZero()) |
| 661 |
}) |
| 662 |
|
| 663 |
It("should ZScan", func() { |
| 664 |
for i := 0; i < 1000; i++ { |
| 665 |
sadd := client.ZAdd("myset", redis.Z{float64(i), fmt.Sprintf("member%d", i)}) |
| 666 |
Expect(sadd.Err()).NotTo(HaveOccurred()) |
| 667 |
} |
| 668 |
|
| 669 |
keys, cursor, err := client.ZScan("myset", 0, "", 0).Result() |
| 670 |
Expect(err).NotTo(HaveOccurred()) |
| 671 |
Expect(keys).NotTo(BeEmpty()) |
| 672 |
Expect(cursor).NotTo(BeZero()) |
| 673 |
}) |
| 674 |
|
| 675 |
}) |
| 676 |
|
| 677 |
Describe("strings", func() { |
| 678 |
|
| 679 |
It("should Append", func() { |
| 680 |
exists := client.Exists("key") |
| 681 |
Expect(exists.Err()).NotTo(HaveOccurred()) |
| 682 |
Expect(exists.Val()).To(Equal(false)) |
| 683 |
|
| 684 |
append := client.Append("key", "Hello") |
| 685 |
Expect(append.Err()).NotTo(HaveOccurred()) |
| 686 |
Expect(append.Val()).To(Equal(int64(5))) |
| 687 |
|
| 688 |
append = client.Append("key", " World") |
| 689 |
Expect(append.Err()).NotTo(HaveOccurred()) |
| 690 |
Expect(append.Val()).To(Equal(int64(11))) |
| 691 |
|
| 692 |
get := client.Get("key") |
| 693 |
Expect(get.Err()).NotTo(HaveOccurred()) |
| 694 |
Expect(get.Val()).To(Equal("Hello World")) |
| 695 |
}) |
| 696 |
|
| 697 |
It("should BitCount", func() { |
| 698 |
set := client.Set("key", "foobar", 0) |
| 699 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 700 |
Expect(set.Val()).To(Equal("OK")) |
| 701 |
|
| 702 |
bitCount := client.BitCount("key", nil) |
| 703 |
Expect(bitCount.Err()).NotTo(HaveOccurred()) |
| 704 |
Expect(bitCount.Val()).To(Equal(int64(26))) |
| 705 |
|
| 706 |
bitCount = client.BitCount("key", &redis.BitCount{0, 0}) |
| 707 |
Expect(bitCount.Err()).NotTo(HaveOccurred()) |
| 708 |
Expect(bitCount.Val()).To(Equal(int64(4))) |
| 709 |
|
| 710 |
bitCount = client.BitCount("key", &redis.BitCount{1, 1}) |
| 711 |
Expect(bitCount.Err()).NotTo(HaveOccurred()) |
| 712 |
Expect(bitCount.Val()).To(Equal(int64(6))) |
| 713 |
}) |
| 714 |
|
| 715 |
It("should BitOpAnd", func() { |
| 716 |
set := client.Set("key1", "1", 0) |
| 717 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 718 |
Expect(set.Val()).To(Equal("OK")) |
| 719 |
|
| 720 |
set = client.Set("key2", "0", 0) |
| 721 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 722 |
Expect(set.Val()).To(Equal("OK")) |
| 723 |
|
| 724 |
bitOpAnd := client.BitOpAnd("dest", "key1", "key2") |
| 725 |
Expect(bitOpAnd.Err()).NotTo(HaveOccurred()) |
| 726 |
Expect(bitOpAnd.Val()).To(Equal(int64(1))) |
| 727 |
|
| 728 |
get := client.Get("dest") |
| 729 |
Expect(get.Err()).NotTo(HaveOccurred()) |
| 730 |
Expect(get.Val()).To(Equal("0")) |
| 731 |
}) |
| 732 |
|
| 733 |
It("should BitOpOr", func() { |
| 734 |
set := client.Set("key1", "1", 0) |
| 735 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 736 |
Expect(set.Val()).To(Equal("OK")) |
| 737 |
|
| 738 |
set = client.Set("key2", "0", 0) |
| 739 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 740 |
Expect(set.Val()).To(Equal("OK")) |
| 741 |
|
| 742 |
bitOpOr := client.BitOpOr("dest", "key1", "key2") |
| 743 |
Expect(bitOpOr.Err()).NotTo(HaveOccurred()) |
| 744 |
Expect(bitOpOr.Val()).To(Equal(int64(1))) |
| 745 |
|
| 746 |
get := client.Get("dest") |
| 747 |
Expect(get.Err()).NotTo(HaveOccurred()) |
| 748 |
Expect(get.Val()).To(Equal("1")) |
| 749 |
}) |
| 750 |
|
| 751 |
It("should BitOpXor", func() { |
| 752 |
set := client.Set("key1", "\xff", 0) |
| 753 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 754 |
Expect(set.Val()).To(Equal("OK")) |
| 755 |
|
| 756 |
set = client.Set("key2", "\x0f", 0) |
| 757 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 758 |
Expect(set.Val()).To(Equal("OK")) |
| 759 |
|
| 760 |
bitOpXor := client.BitOpXor("dest", "key1", "key2") |
| 761 |
Expect(bitOpXor.Err()).NotTo(HaveOccurred()) |
| 762 |
Expect(bitOpXor.Val()).To(Equal(int64(1))) |
| 763 |
|
| 764 |
get := client.Get("dest") |
| 765 |
Expect(get.Err()).NotTo(HaveOccurred()) |
| 766 |
Expect(get.Val()).To(Equal("\xf0")) |
| 767 |
}) |
| 768 |
|
| 769 |
It("should BitOpNot", func() { |
| 770 |
set := client.Set("key1", "\x00", 0) |
| 771 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 772 |
Expect(set.Val()).To(Equal("OK")) |
| 773 |
|
| 774 |
bitOpNot := client.BitOpNot("dest", "key1") |
| 775 |
Expect(bitOpNot.Err()).NotTo(HaveOccurred()) |
| 776 |
Expect(bitOpNot.Val()).To(Equal(int64(1))) |
| 777 |
|
| 778 |
get := client.Get("dest") |
| 779 |
Expect(get.Err()).NotTo(HaveOccurred()) |
| 780 |
Expect(get.Val()).To(Equal("\xff")) |
| 781 |
}) |
| 782 |
|
| 783 |
It("should BitPos", func() { |
| 784 |
err := client.Set("mykey", "\xff\xf0\x00", 0).Err() |
| 785 |
Expect(err).NotTo(HaveOccurred()) |
| 786 |
|
| 787 |
pos, err := client.BitPos("mykey", 0).Result() |
| 788 |
Expect(err).NotTo(HaveOccurred()) |
| 789 |
Expect(pos).To(Equal(int64(12))) |
| 790 |
|
| 791 |
pos, err = client.BitPos("mykey", 1).Result() |
| 792 |
Expect(err).NotTo(HaveOccurred()) |
| 793 |
Expect(pos).To(Equal(int64(0))) |
| 794 |
|
| 795 |
pos, err = client.BitPos("mykey", 0, 2).Result() |
| 796 |
Expect(err).NotTo(HaveOccurred()) |
| 797 |
Expect(pos).To(Equal(int64(16))) |
| 798 |
|
| 799 |
pos, err = client.BitPos("mykey", 1, 2).Result() |
| 800 |
Expect(err).NotTo(HaveOccurred()) |
| 801 |
Expect(pos).To(Equal(int64(-1))) |
| 802 |
|
| 803 |
pos, err = client.BitPos("mykey", 0, -1).Result() |
| 804 |
Expect(err).NotTo(HaveOccurred()) |
| 805 |
Expect(pos).To(Equal(int64(16))) |
| 806 |
|
| 807 |
pos, err = client.BitPos("mykey", 1, -1).Result() |
| 808 |
Expect(err).NotTo(HaveOccurred()) |
| 809 |
Expect(pos).To(Equal(int64(-1))) |
| 810 |
|
| 811 |
pos, err = client.BitPos("mykey", 0, 2, 1).Result() |
| 812 |
Expect(err).NotTo(HaveOccurred()) |
| 813 |
Expect(pos).To(Equal(int64(-1))) |
| 814 |
|
| 815 |
pos, err = client.BitPos("mykey", 0, 0, -3).Result() |
| 816 |
Expect(err).NotTo(HaveOccurred()) |
| 817 |
Expect(pos).To(Equal(int64(-1))) |
| 818 |
|
| 819 |
pos, err = client.BitPos("mykey", 0, 0, 0).Result() |
| 820 |
Expect(err).NotTo(HaveOccurred()) |
| 821 |
Expect(pos).To(Equal(int64(-1))) |
| 822 |
}) |
| 823 |
|
| 824 |
It("should Decr", func() { |
| 825 |
set := client.Set("key", "10", 0) |
| 826 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 827 |
Expect(set.Val()).To(Equal("OK")) |
| 828 |
|
| 829 |
decr := client.Decr("key") |
| 830 |
Expect(decr.Err()).NotTo(HaveOccurred()) |
| 831 |
Expect(decr.Val()).To(Equal(int64(9))) |
| 832 |
|
| 833 |
set = client.Set("key", "234293482390480948029348230948", 0) |
| 834 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 835 |
Expect(set.Val()).To(Equal("OK")) |
| 836 |
|
| 837 |
decr = client.Decr("key") |
| 838 |
Expect(decr.Err()).To(MatchError("ERR value is not an integer or out of range")) |
| 839 |
Expect(decr.Val()).To(Equal(int64(0))) |
| 840 |
}) |
| 841 |
|
| 842 |
It("should DecrBy", func() { |
| 843 |
set := client.Set("key", "10", 0) |
| 844 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 845 |
Expect(set.Val()).To(Equal("OK")) |
| 846 |
|
| 847 |
decrBy := client.DecrBy("key", 5) |
| 848 |
Expect(decrBy.Err()).NotTo(HaveOccurred()) |
| 849 |
Expect(decrBy.Val()).To(Equal(int64(5))) |
| 850 |
}) |
| 851 |
|
| 852 |
It("should Get", func() { |
| 853 |
get := client.Get("_") |
| 854 |
Expect(get.Err()).To(Equal(redis.Nil)) |
| 855 |
Expect(get.Val()).To(Equal("")) |
| 856 |
|
| 857 |
set := client.Set("key", "hello", 0) |
| 858 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 859 |
Expect(set.Val()).To(Equal("OK")) |
| 860 |
|
| 861 |
get = client.Get("key") |
| 862 |
Expect(get.Err()).NotTo(HaveOccurred()) |
| 863 |
Expect(get.Val()).To(Equal("hello")) |
| 864 |
}) |
| 865 |
|
| 866 |
It("should GetBit", func() { |
| 867 |
setBit := client.SetBit("key", 7, 1) |
| 868 |
Expect(setBit.Err()).NotTo(HaveOccurred()) |
| 869 |
Expect(setBit.Val()).To(Equal(int64(0))) |
| 870 |
|
| 871 |
getBit := client.GetBit("key", 0) |
| 872 |
Expect(getBit.Err()).NotTo(HaveOccurred()) |
| 873 |
Expect(getBit.Val()).To(Equal(int64(0))) |
| 874 |
|
| 875 |
getBit = client.GetBit("key", 7) |
| 876 |
Expect(getBit.Err()).NotTo(HaveOccurred()) |
| 877 |
Expect(getBit.Val()).To(Equal(int64(1))) |
| 878 |
|
| 879 |
getBit = client.GetBit("key", 100) |
| 880 |
Expect(getBit.Err()).NotTo(HaveOccurred()) |
| 881 |
Expect(getBit.Val()).To(Equal(int64(0))) |
| 882 |
}) |
| 883 |
|
| 884 |
It("should GetRange", func() { |
| 885 |
set := client.Set("key", "This is a string", 0) |
| 886 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 887 |
Expect(set.Val()).To(Equal("OK")) |
| 888 |
|
| 889 |
getRange := client.GetRange("key", 0, 3) |
| 890 |
Expect(getRange.Err()).NotTo(HaveOccurred()) |
| 891 |
Expect(getRange.Val()).To(Equal("This")) |
| 892 |
|
| 893 |
getRange = client.GetRange("key", -3, -1) |
| 894 |
Expect(getRange.Err()).NotTo(HaveOccurred()) |
| 895 |
Expect(getRange.Val()).To(Equal("ing")) |
| 896 |
|
| 897 |
getRange = client.GetRange("key", 0, -1) |
| 898 |
Expect(getRange.Err()).NotTo(HaveOccurred()) |
| 899 |
Expect(getRange.Val()).To(Equal("This is a string")) |
| 900 |
|
| 901 |
getRange = client.GetRange("key", 10, 100) |
| 902 |
Expect(getRange.Err()).NotTo(HaveOccurred()) |
| 903 |
Expect(getRange.Val()).To(Equal("string")) |
| 904 |
}) |
| 905 |
|
| 906 |
It("should GetSet", func() { |
| 907 |
incr := client.Incr("key") |
| 908 |
Expect(incr.Err()).NotTo(HaveOccurred()) |
| 909 |
Expect(incr.Val()).To(Equal(int64(1))) |
| 910 |
|
| 911 |
getSet := client.GetSet("key", "0") |
| 912 |
Expect(getSet.Err()).NotTo(HaveOccurred()) |
| 913 |
Expect(getSet.Val()).To(Equal("1")) |
| 914 |
|
| 915 |
get := client.Get("key") |
| 916 |
Expect(get.Err()).NotTo(HaveOccurred()) |
| 917 |
Expect(get.Val()).To(Equal("0")) |
| 918 |
}) |
| 919 |
|
| 920 |
It("should Incr", func() { |
| 921 |
set := client.Set("key", "10", 0) |
| 922 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 923 |
Expect(set.Val()).To(Equal("OK")) |
| 924 |
|
| 925 |
incr := client.Incr("key") |
| 926 |
Expect(incr.Err()).NotTo(HaveOccurred()) |
| 927 |
Expect(incr.Val()).To(Equal(int64(11))) |
| 928 |
|
| 929 |
get := client.Get("key") |
| 930 |
Expect(get.Err()).NotTo(HaveOccurred()) |
| 931 |
Expect(get.Val()).To(Equal("11")) |
| 932 |
}) |
| 933 |
|
| 934 |
It("should IncrBy", func() { |
| 935 |
set := client.Set("key", "10", 0) |
| 936 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 937 |
Expect(set.Val()).To(Equal("OK")) |
| 938 |
|
| 939 |
incrBy := client.IncrBy("key", 5) |
| 940 |
Expect(incrBy.Err()).NotTo(HaveOccurred()) |
| 941 |
Expect(incrBy.Val()).To(Equal(int64(15))) |
| 942 |
}) |
| 943 |
|
| 944 |
It("should IncrByFloat", func() { |
| 945 |
set := client.Set("key", "10.50", 0) |
| 946 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 947 |
Expect(set.Val()).To(Equal("OK")) |
| 948 |
|
| 949 |
incrByFloat := client.IncrByFloat("key", 0.1) |
| 950 |
Expect(incrByFloat.Err()).NotTo(HaveOccurred()) |
| 951 |
Expect(incrByFloat.Val()).To(Equal(10.6)) |
| 952 |
|
| 953 |
set = client.Set("key", "5.0e3", 0) |
| 954 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 955 |
Expect(set.Val()).To(Equal("OK")) |
| 956 |
|
| 957 |
incrByFloat = client.IncrByFloat("key", 2.0e2) |
| 958 |
Expect(incrByFloat.Err()).NotTo(HaveOccurred()) |
| 959 |
Expect(incrByFloat.Val()).To(Equal(float64(5200))) |
| 960 |
}) |
| 961 |
|
| 962 |
It("should IncrByFloatOverflow", func() { |
| 963 |
incrByFloat := client.IncrByFloat("key", 996945661) |
| 964 |
Expect(incrByFloat.Err()).NotTo(HaveOccurred()) |
| 965 |
Expect(incrByFloat.Val()).To(Equal(float64(996945661))) |
| 966 |
}) |
| 967 |
|
| 968 |
It("should MSetMGet", func() { |
| 969 |
mSet := client.MSet("key1", "hello1", "key2", "hello2") |
| 970 |
Expect(mSet.Err()).NotTo(HaveOccurred()) |
| 971 |
Expect(mSet.Val()).To(Equal("OK")) |
| 972 |
|
| 973 |
mGet := client.MGet("key1", "key2", "_") |
| 974 |
Expect(mGet.Err()).NotTo(HaveOccurred()) |
| 975 |
Expect(mGet.Val()).To(Equal([]interface{}{"hello1", "hello2", nil})) |
| 976 |
}) |
| 977 |
|
| 978 |
It("should MSetNX", func() { |
| 979 |
mSetNX := client.MSetNX("key1", "hello1", "key2", "hello2") |
| 980 |
Expect(mSetNX.Err()).NotTo(HaveOccurred()) |
| 981 |
Expect(mSetNX.Val()).To(Equal(true)) |
| 982 |
|
| 983 |
mSetNX = client.MSetNX("key2", "hello1", "key3", "hello2") |
| 984 |
Expect(mSetNX.Err()).NotTo(HaveOccurred()) |
| 985 |
Expect(mSetNX.Val()).To(Equal(false)) |
| 986 |
}) |
| 987 |
|
| 988 |
It("should Set with expiration", func() { |
| 989 |
err := client.Set("key", "hello", 100*time.Millisecond).Err() |
| 990 |
Expect(err).NotTo(HaveOccurred()) |
| 991 |
|
| 992 |
val, err := client.Get("key").Result() |
| 993 |
Expect(err).NotTo(HaveOccurred()) |
| 994 |
Expect(val).To(Equal("hello")) |
| 995 |
|
| 996 |
Eventually(func() error { |
| 997 |
return client.Get("foo").Err() |
| 998 |
}, "1s", "100ms").Should(Equal(redis.Nil)) |
| 999 |
}) |
| 1000 |
|
| 1001 |
It("should SetGet", func() { |
| 1002 |
set := client.Set("key", "hello", 0) |
| 1003 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 1004 |
Expect(set.Val()).To(Equal("OK")) |
| 1005 |
|
| 1006 |
get := client.Get("key") |
| 1007 |
Expect(get.Err()).NotTo(HaveOccurred()) |
| 1008 |
Expect(get.Val()).To(Equal("hello")) |
| 1009 |
}) |
| 1010 |
|
| 1011 |
It("should SetNX", func() { |
| 1012 |
setNX := client.SetNX("key", "hello", 0) |
| 1013 |
Expect(setNX.Err()).NotTo(HaveOccurred()) |
| 1014 |
Expect(setNX.Val()).To(Equal(true)) |
| 1015 |
|
| 1016 |
setNX = client.SetNX("key", "hello2", 0) |
| 1017 |
Expect(setNX.Err()).NotTo(HaveOccurred()) |
| 1018 |
Expect(setNX.Val()).To(Equal(false)) |
| 1019 |
|
| 1020 |
get := client.Get("key") |
| 1021 |
Expect(get.Err()).NotTo(HaveOccurred()) |
| 1022 |
Expect(get.Val()).To(Equal("hello")) |
| 1023 |
}) |
| 1024 |
|
| 1025 |
It("should SetNX with expiration", func() { |
| 1026 |
isSet, err := client.SetNX("key", "hello", time.Second).Result() |
| 1027 |
Expect(err).NotTo(HaveOccurred()) |
| 1028 |
Expect(isSet).To(Equal(true)) |
| 1029 |
|
| 1030 |
isSet, err = client.SetNX("key", "hello2", time.Second).Result() |
| 1031 |
Expect(err).NotTo(HaveOccurred()) |
| 1032 |
Expect(isSet).To(Equal(false)) |
| 1033 |
|
| 1034 |
val, err := client.Get("key").Result() |
| 1035 |
Expect(err).NotTo(HaveOccurred()) |
| 1036 |
Expect(val).To(Equal("hello")) |
| 1037 |
}) |
| 1038 |
|
| 1039 |
It("should SetXX", func() { |
| 1040 |
isSet, err := client.SetXX("key", "hello2", 0).Result() |
| 1041 |
Expect(err).NotTo(HaveOccurred()) |
| 1042 |
Expect(isSet).To(Equal(false)) |
| 1043 |
|
| 1044 |
err = client.Set("key", "hello", 0).Err() |
| 1045 |
Expect(err).NotTo(HaveOccurred()) |
| 1046 |
|
| 1047 |
isSet, err = client.SetXX("key", "hello2", 0).Result() |
| 1048 |
Expect(err).NotTo(HaveOccurred()) |
| 1049 |
Expect(isSet).To(Equal(true)) |
| 1050 |
|
| 1051 |
val, err := client.Get("key").Result() |
| 1052 |
Expect(err).NotTo(HaveOccurred()) |
| 1053 |
Expect(val).To(Equal("hello2")) |
| 1054 |
}) |
| 1055 |
|
| 1056 |
It("should SetXX with expiration", func() { |
| 1057 |
isSet, err := client.SetXX("key", "hello2", time.Second).Result() |
| 1058 |
Expect(err).NotTo(HaveOccurred()) |
| 1059 |
Expect(isSet).To(Equal(false)) |
| 1060 |
|
| 1061 |
err = client.Set("key", "hello", time.Second).Err() |
| 1062 |
Expect(err).NotTo(HaveOccurred()) |
| 1063 |
|
| 1064 |
isSet, err = client.SetXX("key", "hello2", time.Second).Result() |
| 1065 |
Expect(err).NotTo(HaveOccurred()) |
| 1066 |
Expect(isSet).To(Equal(true)) |
| 1067 |
|
| 1068 |
val, err := client.Get("key").Result() |
| 1069 |
Expect(err).NotTo(HaveOccurred()) |
| 1070 |
Expect(val).To(Equal("hello2")) |
| 1071 |
}) |
| 1072 |
|
| 1073 |
It("should SetRange", func() { |
| 1074 |
set := client.Set("key", "Hello World", 0) |
| 1075 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 1076 |
Expect(set.Val()).To(Equal("OK")) |
| 1077 |
|
| 1078 |
range_ := client.SetRange("key", 6, "Redis") |
| 1079 |
Expect(range_.Err()).NotTo(HaveOccurred()) |
| 1080 |
Expect(range_.Val()).To(Equal(int64(11))) |
| 1081 |
|
| 1082 |
get := client.Get("key") |
| 1083 |
Expect(get.Err()).NotTo(HaveOccurred()) |
| 1084 |
Expect(get.Val()).To(Equal("Hello Redis")) |
| 1085 |
}) |
| 1086 |
|
| 1087 |
It("should StrLen", func() { |
| 1088 |
set := client.Set("key", "hello", 0) |
| 1089 |
Expect(set.Err()).NotTo(HaveOccurred()) |
| 1090 |
Expect(set.Val()).To(Equal("OK")) |
| 1091 |
|
| 1092 |
strLen := client.StrLen("key") |
| 1093 |
Expect(strLen.Err()).NotTo(HaveOccurred()) |
| 1094 |
Expect(strLen.Val()).To(Equal(int64(5))) |
| 1095 |
|
| 1096 |
strLen = client.StrLen("_") |
| 1097 |
Expect(strLen.Err()).NotTo(HaveOccurred()) |
| 1098 |
Expect(strLen.Val()).To(Equal(int64(0))) |
| 1099 |
}) |
| 1100 |
|
| 1101 |
}) |
| 1102 |
|
| 1103 |
Describe("hashes", func() { |
| 1104 |
|
| 1105 |
It("should HDel", func() { |
| 1106 |
hSet := client.HSet("hash", "key", "hello") |
| 1107 |
Expect(hSet.Err()).NotTo(HaveOccurred()) |
| 1108 |
|
| 1109 |
hDel := client.HDel("hash", "key") |
| 1110 |
Expect(hDel.Err()).NotTo(HaveOccurred()) |
| 1111 |
Expect(hDel.Val()).To(Equal(int64(1))) |
| 1112 |
|
| 1113 |
hDel = client.HDel("hash", "key") |
| 1114 |
Expect(hDel.Err()).NotTo(HaveOccurred()) |
| 1115 |
Expect(hDel.Val()).To(Equal(int64(0))) |
| 1116 |
}) |
| 1117 |
|
| 1118 |
It("should HExists", func() { |
| 1119 |
hSet := client.HSet("hash", "key", "hello") |
| 1120 |
Expect(hSet.Err()).NotTo(HaveOccurred()) |
| 1121 |
|
| 1122 |
hExists := client.HExists("hash", "key") |
| 1123 |
Expect(hExists.Err()).NotTo(HaveOccurred()) |
| 1124 |
Expect(hExists.Val()).To(Equal(true)) |
| 1125 |
|
| 1126 |
hExists = client.HExists("hash", "key1") |
| 1127 |
Expect(hExists.Err()).NotTo(HaveOccurred()) |
| 1128 |
Expect(hExists.Val()).To(Equal(false)) |
| 1129 |
}) |
| 1130 |
|
| 1131 |
It("should HGet", func() { |
| 1132 |
hSet := client.HSet("hash", "key", "hello") |
| 1133 |
Expect(hSet.Err()).NotTo(HaveOccurred()) |
| 1134 |
|
| 1135 |
hGet := client.HGet("hash", "key") |
| 1136 |
Expect(hGet.Err()).NotTo(HaveOccurred()) |
| 1137 |
Expect(hGet.Val()).To(Equal("hello")) |
| 1138 |
|
| 1139 |
hGet = client.HGet("hash", "key1") |
| 1140 |
Expect(hGet.Err()).To(Equal(redis.Nil)) |
| 1141 |
Expect(hGet.Val()).To(Equal("")) |
| 1142 |
}) |
| 1143 |
|
| 1144 |
It("should HGetAll", func() { |
| 1145 |
err := client.HSet("hash", "key1", "hello1").Err() |
| 1146 |
Expect(err).NotTo(HaveOccurred()) |
| 1147 |
err = client.HSet("hash", "key2", "hello2").Err() |
| 1148 |
Expect(err).NotTo(HaveOccurred()) |
| 1149 |
|
| 1150 |
m, err := client.HGetAll("hash").Result() |
| 1151 |
Expect(err).NotTo(HaveOccurred()) |
| 1152 |
Expect(m).To(Equal(map[string]string{"key1": "hello1", "key2": "hello2"})) |
| 1153 |
}) |
| 1154 |
|
| 1155 |
It("should HIncrBy", func() { |
| 1156 |
hSet := client.HSet("hash", "key", "5") |
| 1157 |
Expect(hSet.Err()).NotTo(HaveOccurred()) |
| 1158 |
|
| 1159 |
hIncrBy := client.HIncrBy("hash", "key", 1) |
| 1160 |
Expect(hIncrBy.Err()).NotTo(HaveOccurred()) |
| 1161 |
Expect(hIncrBy.Val()).To(Equal(int64(6))) |
| 1162 |
|
| 1163 |
hIncrBy = client.HIncrBy("hash", "key", -1) |
| 1164 |
Expect(hIncrBy.Err()).NotTo(HaveOccurred()) |
| 1165 |
Expect(hIncrBy.Val()).To(Equal(int64(5))) |
| 1166 |
|
| 1167 |
hIncrBy = client.HIncrBy("hash", "key", -10) |
| 1168 |
Expect(hIncrBy.Err()).NotTo(HaveOccurred()) |
| 1169 |
Expect(hIncrBy.Val()).To(Equal(int64(-5))) |
| 1170 |
}) |
| 1171 |
|
| 1172 |
It("should HIncrByFloat", func() { |
| 1173 |
hSet := client.HSet("hash", "field", "10.50") |
| 1174 |
Expect(hSet.Err()).NotTo(HaveOccurred()) |
| 1175 |
Expect(hSet.Val()).To(Equal(true)) |
| 1176 |
|
| 1177 |
hIncrByFloat := client.HIncrByFloat("hash", "field", 0.1) |
| 1178 |
Expect(hIncrByFloat.Err()).NotTo(HaveOccurred()) |
| 1179 |
Expect(hIncrByFloat.Val()).To(Equal(10.6)) |
| 1180 |
|
| 1181 |
hSet = client.HSet("hash", "field", "5.0e3") |
| 1182 |
Expect(hSet.Err()).NotTo(HaveOccurred()) |
| 1183 |
Expect(hSet.Val()).To(Equal(false)) |
| 1184 |
|
| 1185 |
hIncrByFloat = client.HIncrByFloat("hash", "field", 2.0e2) |
| 1186 |
Expect(hIncrByFloat.Err()).NotTo(HaveOccurred()) |
| 1187 |
Expect(hIncrByFloat.Val()).To(Equal(float64(5200))) |
| 1188 |
}) |
| 1189 |
|
| 1190 |
It("should HKeys", func() { |
| 1191 |
hkeys := client.HKeys("hash") |
| 1192 |
Expect(hkeys.Err()).NotTo(HaveOccurred()) |
| 1193 |
Expect(hkeys.Val()).To(Equal([]string{})) |
| 1194 |
|
| 1195 |
hset := client.HSet("hash", "key1", "hello1") |
| 1196 |
Expect(hset.Err()).NotTo(HaveOccurred()) |
| 1197 |
hset = client.HSet("hash", "key2", "hello2") |
| 1198 |
Expect(hset.Err()).NotTo(HaveOccurred()) |
| 1199 |
|
| 1200 |
hkeys = client.HKeys("hash") |
| 1201 |
Expect(hkeys.Err()).NotTo(HaveOccurred()) |
| 1202 |
Expect(hkeys.Val()).To(Equal([]string{"key1", "key2"})) |
| 1203 |
}) |
| 1204 |
|
| 1205 |
It("should HLen", func() { |
| 1206 |
hSet := client.HSet("hash", "key1", "hello1") |
| 1207 |
Expect(hSet.Err()).NotTo(HaveOccurred()) |
| 1208 |
hSet = client.HSet("hash", "key2", "hello2") |
| 1209 |
Expect(hSet.Err()).NotTo(HaveOccurred()) |
| 1210 |
|
| 1211 |
hLen := client.HLen("hash") |
| 1212 |
Expect(hLen.Err()).NotTo(HaveOccurred()) |
| 1213 |
Expect(hLen.Val()).To(Equal(int64(2))) |
| 1214 |
}) |
| 1215 |
|
| 1216 |
It("should HMGet", func() { |
| 1217 |
err := client.HSet("hash", "key1", "hello1").Err() |
| 1218 |
Expect(err).NotTo(HaveOccurred()) |
| 1219 |
err = client.HSet("hash", "key2", "hello2").Err() |
| 1220 |
Expect(err).NotTo(HaveOccurred()) |
| 1221 |
|
| 1222 |
vals, err := client.HMGet("hash", "key1", "key2", "_").Result() |
| 1223 |
Expect(err).NotTo(HaveOccurred()) |
| 1224 |
Expect(vals).To(Equal([]interface{}{"hello1", "hello2", nil})) |
| 1225 |
}) |
| 1226 |
|
| 1227 |
It("should HMSet", func() { |
| 1228 |
ok, err := client.HMSet("hash", map[string]string{ |
| 1229 |
"key1": "hello1", |
| 1230 |
"key2": "hello2", |
| 1231 |
}).Result() |
| 1232 |
Expect(err).NotTo(HaveOccurred()) |
| 1233 |
Expect(ok).To(Equal("OK")) |
| 1234 |
|
| 1235 |
v, err := client.HGet("hash", "key1").Result() |
| 1236 |
Expect(err).NotTo(HaveOccurred()) |
| 1237 |
Expect(v).To(Equal("hello1")) |
| 1238 |
|
| 1239 |
v, err = client.HGet("hash", "key2").Result() |
| 1240 |
Expect(err).NotTo(HaveOccurred()) |
| 1241 |
Expect(v).To(Equal("hello2")) |
| 1242 |
}) |
| 1243 |
|
| 1244 |
It("should HSet", func() { |
| 1245 |
hSet := client.HSet("hash", "key", "hello") |
| 1246 |
Expect(hSet.Err()).NotTo(HaveOccurred()) |
| 1247 |
Expect(hSet.Val()).To(Equal(true)) |
| 1248 |
|
| 1249 |
hGet := client.HGet("hash", "key") |
| 1250 |
Expect(hGet.Err()).NotTo(HaveOccurred()) |
| 1251 |
Expect(hGet.Val()).To(Equal("hello")) |
| 1252 |
}) |
| 1253 |
|
| 1254 |
It("should HSetNX", func() { |
| 1255 |
hSetNX := client.HSetNX("hash", "key", "hello") |
| 1256 |
Expect(hSetNX.Err()).NotTo(HaveOccurred()) |
| 1257 |
Expect(hSetNX.Val()).To(Equal(true)) |
| 1258 |
|
| 1259 |
hSetNX = client.HSetNX("hash", "key", "hello") |
| 1260 |
Expect(hSetNX.Err()).NotTo(HaveOccurred()) |
| 1261 |
Expect(hSetNX.Val()).To(Equal(false)) |
| 1262 |
|
| 1263 |
hGet := client.HGet("hash", "key") |
| 1264 |
Expect(hGet.Err()).NotTo(HaveOccurred()) |
| 1265 |
Expect(hGet.Val()).To(Equal("hello")) |
| 1266 |
}) |
| 1267 |
|
| 1268 |
It("should HVals", func() { |
| 1269 |
err := client.HSet("hash", "key1", "hello1").Err() |
| 1270 |
Expect(err).NotTo(HaveOccurred()) |
| 1271 |
err = client.HSet("hash", "key2", "hello2").Err() |
| 1272 |
Expect(err).NotTo(HaveOccurred()) |
| 1273 |
|
| 1274 |
v, err := client.HVals("hash").Result() |
| 1275 |
Expect(err).NotTo(HaveOccurred()) |
| 1276 |
Expect(v).To(Equal([]string{"hello1", "hello2"})) |
| 1277 |
|
| 1278 |
var slice []string |
| 1279 |
err = client.HVals("hash").ScanSlice(&slice) |
| 1280 |
Expect(err).NotTo(HaveOccurred()) |
| 1281 |
Expect(slice).To(Equal([]string{"hello1", "hello2"})) |
| 1282 |
}) |
| 1283 |
|
| 1284 |
}) |
| 1285 |
|
| 1286 |
Describe("hyperloglog", func() { |
| 1287 |
It("should PFMerge", func() { |
| 1288 |
pfAdd := client.PFAdd("hll1", "1", "2", "3", "4", "5") |
| 1289 |
Expect(pfAdd.Err()).NotTo(HaveOccurred()) |
| 1290 |
|
| 1291 |
pfCount := client.PFCount("hll1") |
| 1292 |
Expect(pfCount.Err()).NotTo(HaveOccurred()) |
| 1293 |
Expect(pfCount.Val()).To(Equal(int64(5))) |
| 1294 |
|
| 1295 |
pfAdd = client.PFAdd("hll2", "a", "b", "c", "d", "e") |
| 1296 |
Expect(pfAdd.Err()).NotTo(HaveOccurred()) |
| 1297 |
|
| 1298 |
pfMerge := client.PFMerge("hllMerged", "hll1", "hll2") |
| 1299 |
Expect(pfMerge.Err()).NotTo(HaveOccurred()) |
| 1300 |
|
| 1301 |
pfCount = client.PFCount("hllMerged") |
| 1302 |
Expect(pfCount.Err()).NotTo(HaveOccurred()) |
| 1303 |
Expect(pfCount.Val()).To(Equal(int64(10))) |
| 1304 |
|
| 1305 |
pfCount = client.PFCount("hll1", "hll2") |
| 1306 |
Expect(pfCount.Err()).NotTo(HaveOccurred()) |
| 1307 |
Expect(pfCount.Val()).To(Equal(int64(10))) |
| 1308 |
}) |
| 1309 |
}) |
| 1310 |
|
| 1311 |
Describe("lists", func() { |
| 1312 |
|
| 1313 |
It("should BLPop", func() { |
| 1314 |
rPush := client.RPush("list1", "a", "b", "c") |
| 1315 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1316 |
|
| 1317 |
bLPop := client.BLPop(0, "list1", "list2") |
| 1318 |
Expect(bLPop.Err()).NotTo(HaveOccurred()) |
| 1319 |
Expect(bLPop.Val()).To(Equal([]string{"list1", "a"})) |
| 1320 |
}) |
| 1321 |
|
| 1322 |
It("should BLPopBlocks", func() { |
| 1323 |
started := make(chan bool) |
| 1324 |
done := make(chan bool) |
| 1325 |
go func() { |
| 1326 |
defer GinkgoRecover() |
| 1327 |
|
| 1328 |
started <- true |
| 1329 |
bLPop := client.BLPop(0, "list") |
| 1330 |
Expect(bLPop.Err()).NotTo(HaveOccurred()) |
| 1331 |
Expect(bLPop.Val()).To(Equal([]string{"list", "a"})) |
| 1332 |
done <- true |
| 1333 |
}() |
| 1334 |
<-started |
| 1335 |
|
| 1336 |
select { |
| 1337 |
case <-done: |
| 1338 |
Fail("BLPop is not blocked") |
| 1339 |
case <-time.After(time.Second): |
| 1340 |
// ok |
| 1341 |
} |
| 1342 |
|
| 1343 |
rPush := client.RPush("list", "a") |
| 1344 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1345 |
|
| 1346 |
select { |
| 1347 |
case <-done: |
| 1348 |
// ok |
| 1349 |
case <-time.After(time.Second): |
| 1350 |
Fail("BLPop is still blocked") |
| 1351 |
} |
| 1352 |
}) |
| 1353 |
|
| 1354 |
It("should BLPop timeout", func() { |
| 1355 |
val, err := client.BLPop(time.Second, "list1").Result() |
| 1356 |
Expect(err).To(Equal(redis.Nil)) |
| 1357 |
Expect(val).To(BeNil()) |
| 1358 |
|
| 1359 |
Expect(client.Ping().Err()).NotTo(HaveOccurred()) |
| 1360 |
|
| 1361 |
stats := client.PoolStats() |
| 1362 |
Expect(stats.Requests).To(Equal(uint32(3))) |
| 1363 |
Expect(stats.Hits).To(Equal(uint32(1))) |
| 1364 |
Expect(stats.Timeouts).To(Equal(uint32(0))) |
| 1365 |
}) |
| 1366 |
|
| 1367 |
It("should BRPop", func() { |
| 1368 |
rPush := client.RPush("list1", "a", "b", "c") |
| 1369 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1370 |
|
| 1371 |
bRPop := client.BRPop(0, "list1", "list2") |
| 1372 |
Expect(bRPop.Err()).NotTo(HaveOccurred()) |
| 1373 |
Expect(bRPop.Val()).To(Equal([]string{"list1", "c"})) |
| 1374 |
}) |
| 1375 |
|
| 1376 |
It("should BRPop blocks", func() { |
| 1377 |
started := make(chan bool) |
| 1378 |
done := make(chan bool) |
| 1379 |
go func() { |
| 1380 |
defer GinkgoRecover() |
| 1381 |
|
| 1382 |
started <- true |
| 1383 |
brpop := client.BRPop(0, "list") |
| 1384 |
Expect(brpop.Err()).NotTo(HaveOccurred()) |
| 1385 |
Expect(brpop.Val()).To(Equal([]string{"list", "a"})) |
| 1386 |
done <- true |
| 1387 |
}() |
| 1388 |
<-started |
| 1389 |
|
| 1390 |
select { |
| 1391 |
case <-done: |
| 1392 |
Fail("BRPop is not blocked") |
| 1393 |
case <-time.After(time.Second): |
| 1394 |
// ok |
| 1395 |
} |
| 1396 |
|
| 1397 |
rPush := client.RPush("list", "a") |
| 1398 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1399 |
|
| 1400 |
select { |
| 1401 |
case <-done: |
| 1402 |
// ok |
| 1403 |
case <-time.After(time.Second): |
| 1404 |
Fail("BRPop is still blocked") |
| 1405 |
// ok |
| 1406 |
} |
| 1407 |
}) |
| 1408 |
|
| 1409 |
It("should BRPopLPush", func() { |
| 1410 |
_, err := client.BRPopLPush("list1", "list2", time.Second).Result() |
| 1411 |
Expect(err).To(Equal(redis.Nil)) |
| 1412 |
|
| 1413 |
err = client.RPush("list1", "a", "b", "c").Err() |
| 1414 |
Expect(err).NotTo(HaveOccurred()) |
| 1415 |
|
| 1416 |
v, err := client.BRPopLPush("list1", "list2", 0).Result() |
| 1417 |
Expect(err).NotTo(HaveOccurred()) |
| 1418 |
Expect(v).To(Equal("c")) |
| 1419 |
}) |
| 1420 |
|
| 1421 |
It("should LIndex", func() { |
| 1422 |
lPush := client.LPush("list", "World") |
| 1423 |
Expect(lPush.Err()).NotTo(HaveOccurred()) |
| 1424 |
lPush = client.LPush("list", "Hello") |
| 1425 |
Expect(lPush.Err()).NotTo(HaveOccurred()) |
| 1426 |
|
| 1427 |
lIndex := client.LIndex("list", 0) |
| 1428 |
Expect(lIndex.Err()).NotTo(HaveOccurred()) |
| 1429 |
Expect(lIndex.Val()).To(Equal("Hello")) |
| 1430 |
|
| 1431 |
lIndex = client.LIndex("list", -1) |
| 1432 |
Expect(lIndex.Err()).NotTo(HaveOccurred()) |
| 1433 |
Expect(lIndex.Val()).To(Equal("World")) |
| 1434 |
|
| 1435 |
lIndex = client.LIndex("list", 3) |
| 1436 |
Expect(lIndex.Err()).To(Equal(redis.Nil)) |
| 1437 |
Expect(lIndex.Val()).To(Equal("")) |
| 1438 |
}) |
| 1439 |
|
| 1440 |
It("should LInsert", func() { |
| 1441 |
rPush := client.RPush("list", "Hello") |
| 1442 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1443 |
rPush = client.RPush("list", "World") |
| 1444 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1445 |
|
| 1446 |
lInsert := client.LInsert("list", "BEFORE", "World", "There") |
| 1447 |
Expect(lInsert.Err()).NotTo(HaveOccurred()) |
| 1448 |
Expect(lInsert.Val()).To(Equal(int64(3))) |
| 1449 |
|
| 1450 |
lRange := client.LRange("list", 0, -1) |
| 1451 |
Expect(lRange.Err()).NotTo(HaveOccurred()) |
| 1452 |
Expect(lRange.Val()).To(Equal([]string{"Hello", "There", "World"})) |
| 1453 |
}) |
| 1454 |
|
| 1455 |
It("should LLen", func() { |
| 1456 |
lPush := client.LPush("list", "World") |
| 1457 |
Expect(lPush.Err()).NotTo(HaveOccurred()) |
| 1458 |
lPush = client.LPush("list", "Hello") |
| 1459 |
Expect(lPush.Err()).NotTo(HaveOccurred()) |
| 1460 |
|
| 1461 |
lLen := client.LLen("list") |
| 1462 |
Expect(lLen.Err()).NotTo(HaveOccurred()) |
| 1463 |
Expect(lLen.Val()).To(Equal(int64(2))) |
| 1464 |
}) |
| 1465 |
|
| 1466 |
It("should LPop", func() { |
| 1467 |
rPush := client.RPush("list", "one") |
| 1468 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1469 |
rPush = client.RPush("list", "two") |
| 1470 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1471 |
rPush = client.RPush("list", "three") |
| 1472 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1473 |
|
| 1474 |
lPop := client.LPop("list") |
| 1475 |
Expect(lPop.Err()).NotTo(HaveOccurred()) |
| 1476 |
Expect(lPop.Val()).To(Equal("one")) |
| 1477 |
|
| 1478 |
lRange := client.LRange("list", 0, -1) |
| 1479 |
Expect(lRange.Err()).NotTo(HaveOccurred()) |
| 1480 |
Expect(lRange.Val()).To(Equal([]string{"two", "three"})) |
| 1481 |
}) |
| 1482 |
|
| 1483 |
It("should LPush", func() { |
| 1484 |
lPush := client.LPush("list", "World") |
| 1485 |
Expect(lPush.Err()).NotTo(HaveOccurred()) |
| 1486 |
lPush = client.LPush("list", "Hello") |
| 1487 |
Expect(lPush.Err()).NotTo(HaveOccurred()) |
| 1488 |
|
| 1489 |
lRange := client.LRange("list", 0, -1) |
| 1490 |
Expect(lRange.Err()).NotTo(HaveOccurred()) |
| 1491 |
Expect(lRange.Val()).To(Equal([]string{"Hello", "World"})) |
| 1492 |
}) |
| 1493 |
|
| 1494 |
It("should LPushX", func() { |
| 1495 |
lPush := client.LPush("list", "World") |
| 1496 |
Expect(lPush.Err()).NotTo(HaveOccurred()) |
| 1497 |
|
| 1498 |
lPushX := client.LPushX("list", "Hello") |
| 1499 |
Expect(lPushX.Err()).NotTo(HaveOccurred()) |
| 1500 |
Expect(lPushX.Val()).To(Equal(int64(2))) |
| 1501 |
|
| 1502 |
lPushX = client.LPushX("list2", "Hello") |
| 1503 |
Expect(lPushX.Err()).NotTo(HaveOccurred()) |
| 1504 |
Expect(lPushX.Val()).To(Equal(int64(0))) |
| 1505 |
|
| 1506 |
lRange := client.LRange("list", 0, -1) |
| 1507 |
Expect(lRange.Err()).NotTo(HaveOccurred()) |
| 1508 |
Expect(lRange.Val()).To(Equal([]string{"Hello", "World"})) |
| 1509 |
|
| 1510 |
lRange = client.LRange("list2", 0, -1) |
| 1511 |
Expect(lRange.Err()).NotTo(HaveOccurred()) |
| 1512 |
Expect(lRange.Val()).To(Equal([]string{})) |
| 1513 |
}) |
| 1514 |
|
| 1515 |
It("should LRange", func() { |
| 1516 |
rPush := client.RPush("list", "one") |
| 1517 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1518 |
rPush = client.RPush("list", "two") |
| 1519 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1520 |
rPush = client.RPush("list", "three") |
| 1521 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1522 |
|
| 1523 |
lRange := client.LRange("list", 0, 0) |
| 1524 |
Expect(lRange.Err()).NotTo(HaveOccurred()) |
| 1525 |
Expect(lRange.Val()).To(Equal([]string{"one"})) |
| 1526 |
|
| 1527 |
lRange = client.LRange("list", -3, 2) |
| 1528 |
Expect(lRange.Err()).NotTo(HaveOccurred()) |
| 1529 |
Expect(lRange.Val()).To(Equal([]string{"one", "two", "three"})) |
| 1530 |
|
| 1531 |
lRange = client.LRange("list", -100, 100) |
| 1532 |
Expect(lRange.Err()).NotTo(HaveOccurred()) |
| 1533 |
Expect(lRange.Val()).To(Equal([]string{"one", "two", "three"})) |
| 1534 |
|
| 1535 |
lRange = client.LRange("list", 5, 10) |
| 1536 |
Expect(lRange.Err()).NotTo(HaveOccurred()) |
| 1537 |
Expect(lRange.Val()).To(Equal([]string{})) |
| 1538 |
}) |
| 1539 |
|
| 1540 |
It("should LRem", func() { |
| 1541 |
rPush := client.RPush("list", "hello") |
| 1542 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1543 |
rPush = client.RPush("list", "hello") |
| 1544 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1545 |
rPush = client.RPush("list", "key") |
| 1546 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1547 |
rPush = client.RPush("list", "hello") |
| 1548 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1549 |
|
| 1550 |
lRem := client.LRem("list", -2, "hello") |
| 1551 |
Expect(lRem.Err()).NotTo(HaveOccurred()) |
| 1552 |
Expect(lRem.Val()).To(Equal(int64(2))) |
| 1553 |
|
| 1554 |
lRange := client.LRange("list", 0, -1) |
| 1555 |
Expect(lRange.Err()).NotTo(HaveOccurred()) |
| 1556 |
Expect(lRange.Val()).To(Equal([]string{"hello", "key"})) |
| 1557 |
}) |
| 1558 |
|
| 1559 |
It("should LSet", func() { |
| 1560 |
rPush := client.RPush("list", "one") |
| 1561 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1562 |
rPush = client.RPush("list", "two") |
| 1563 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1564 |
rPush = client.RPush("list", "three") |
| 1565 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1566 |
|
| 1567 |
lSet := client.LSet("list", 0, "four") |
| 1568 |
Expect(lSet.Err()).NotTo(HaveOccurred()) |
| 1569 |
Expect(lSet.Val()).To(Equal("OK")) |
| 1570 |
|
| 1571 |
lSet = client.LSet("list", -2, "five") |
| 1572 |
Expect(lSet.Err()).NotTo(HaveOccurred()) |
| 1573 |
Expect(lSet.Val()).To(Equal("OK")) |
| 1574 |
|
| 1575 |
lRange := client.LRange("list", 0, -1) |
| 1576 |
Expect(lRange.Err()).NotTo(HaveOccurred()) |
| 1577 |
Expect(lRange.Val()).To(Equal([]string{"four", "five", "three"})) |
| 1578 |
}) |
| 1579 |
|
| 1580 |
It("should LTrim", func() { |
| 1581 |
rPush := client.RPush("list", "one") |
| 1582 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1583 |
rPush = client.RPush("list", "two") |
| 1584 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1585 |
rPush = client.RPush("list", "three") |
| 1586 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1587 |
|
| 1588 |
lTrim := client.LTrim("list", 1, -1) |
| 1589 |
Expect(lTrim.Err()).NotTo(HaveOccurred()) |
| 1590 |
Expect(lTrim.Val()).To(Equal("OK")) |
| 1591 |
|
| 1592 |
lRange := client.LRange("list", 0, -1) |
| 1593 |
Expect(lRange.Err()).NotTo(HaveOccurred()) |
| 1594 |
Expect(lRange.Val()).To(Equal([]string{"two", "three"})) |
| 1595 |
}) |
| 1596 |
|
| 1597 |
It("should RPop", func() { |
| 1598 |
rPush := client.RPush("list", "one") |
| 1599 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1600 |
rPush = client.RPush("list", "two") |
| 1601 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1602 |
rPush = client.RPush("list", "three") |
| 1603 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1604 |
|
| 1605 |
rPop := client.RPop("list") |
| 1606 |
Expect(rPop.Err()).NotTo(HaveOccurred()) |
| 1607 |
Expect(rPop.Val()).To(Equal("three")) |
| 1608 |
|
| 1609 |
lRange := client.LRange("list", 0, -1) |
| 1610 |
Expect(lRange.Err()).NotTo(HaveOccurred()) |
| 1611 |
Expect(lRange.Val()).To(Equal([]string{"one", "two"})) |
| 1612 |
}) |
| 1613 |
|
| 1614 |
It("should RPopLPush", func() { |
| 1615 |
rPush := client.RPush("list", "one") |
| 1616 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1617 |
rPush = client.RPush("list", "two") |
| 1618 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1619 |
rPush = client.RPush("list", "three") |
| 1620 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1621 |
|
| 1622 |
rPopLPush := client.RPopLPush("list", "list2") |
| 1623 |
Expect(rPopLPush.Err()).NotTo(HaveOccurred()) |
| 1624 |
Expect(rPopLPush.Val()).To(Equal("three")) |
| 1625 |
|
| 1626 |
lRange := client.LRange("list", 0, -1) |
| 1627 |
Expect(lRange.Err()).NotTo(HaveOccurred()) |
| 1628 |
Expect(lRange.Val()).To(Equal([]string{"one", "two"})) |
| 1629 |
|
| 1630 |
lRange = client.LRange("list2", 0, -1) |
| 1631 |
Expect(lRange.Err()).NotTo(HaveOccurred()) |
| 1632 |
Expect(lRange.Val()).To(Equal([]string{"three"})) |
| 1633 |
}) |
| 1634 |
|
| 1635 |
It("should RPush", func() { |
| 1636 |
rPush := client.RPush("list", "Hello") |
| 1637 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1638 |
Expect(rPush.Val()).To(Equal(int64(1))) |
| 1639 |
|
| 1640 |
rPush = client.RPush("list", "World") |
| 1641 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1642 |
Expect(rPush.Val()).To(Equal(int64(2))) |
| 1643 |
|
| 1644 |
lRange := client.LRange("list", 0, -1) |
| 1645 |
Expect(lRange.Err()).NotTo(HaveOccurred()) |
| 1646 |
Expect(lRange.Val()).To(Equal([]string{"Hello", "World"})) |
| 1647 |
}) |
| 1648 |
|
| 1649 |
It("should RPushX", func() { |
| 1650 |
rPush := client.RPush("list", "Hello") |
| 1651 |
Expect(rPush.Err()).NotTo(HaveOccurred()) |
| 1652 |
Expect(rPush.Val()).To(Equal(int64(1))) |
| 1653 |
|
| 1654 |
rPushX := client.RPushX("list", "World") |
| 1655 |
Expect(rPushX.Err()).NotTo(HaveOccurred()) |
| 1656 |
Expect(rPushX.Val()).To(Equal(int64(2))) |
| 1657 |
|
| 1658 |
rPushX = client.RPushX("list2", "World") |
| 1659 |
Expect(rPushX.Err()).NotTo(HaveOccurred()) |
| 1660 |
Expect(rPushX.Val()).To(Equal(int64(0))) |
| 1661 |
|
| 1662 |
lRange := client.LRange("list", 0, -1) |
| 1663 |
Expect(lRange.Err()).NotTo(HaveOccurred()) |
| 1664 |
Expect(lRange.Val()).To(Equal([]string{"Hello", "World"})) |
| 1665 |
|
| 1666 |
lRange = client.LRange("list2", 0, -1) |
| 1667 |
Expect(lRange.Err()).NotTo(HaveOccurred()) |
| 1668 |
Expect(lRange.Val()).To(Equal([]string{})) |
| 1669 |
}) |
| 1670 |
|
| 1671 |
}) |
| 1672 |
|
| 1673 |
Describe("sets", func() { |
| 1674 |
|
| 1675 |
It("should SAdd", func() { |
| 1676 |
sAdd := client.SAdd("set", "Hello") |
| 1677 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1678 |
Expect(sAdd.Val()).To(Equal(int64(1))) |
| 1679 |
|
| 1680 |
sAdd = client.SAdd("set", "World") |
| 1681 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1682 |
Expect(sAdd.Val()).To(Equal(int64(1))) |
| 1683 |
|
| 1684 |
sAdd = client.SAdd("set", "World") |
| 1685 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1686 |
Expect(sAdd.Val()).To(Equal(int64(0))) |
| 1687 |
|
| 1688 |
sMembers := client.SMembers("set") |
| 1689 |
Expect(sMembers.Err()).NotTo(HaveOccurred()) |
| 1690 |
Expect(sMembers.Val()).To(ConsistOf([]string{"Hello", "World"})) |
| 1691 |
}) |
| 1692 |
|
| 1693 |
It("should SCard", func() { |
| 1694 |
sAdd := client.SAdd("set", "Hello") |
| 1695 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1696 |
Expect(sAdd.Val()).To(Equal(int64(1))) |
| 1697 |
|
| 1698 |
sAdd = client.SAdd("set", "World") |
| 1699 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1700 |
Expect(sAdd.Val()).To(Equal(int64(1))) |
| 1701 |
|
| 1702 |
sCard := client.SCard("set") |
| 1703 |
Expect(sCard.Err()).NotTo(HaveOccurred()) |
| 1704 |
Expect(sCard.Val()).To(Equal(int64(2))) |
| 1705 |
}) |
| 1706 |
|
| 1707 |
It("should SDiff", func() { |
| 1708 |
sAdd := client.SAdd("set1", "a") |
| 1709 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1710 |
sAdd = client.SAdd("set1", "b") |
| 1711 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1712 |
sAdd = client.SAdd("set1", "c") |
| 1713 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1714 |
|
| 1715 |
sAdd = client.SAdd("set2", "c") |
| 1716 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1717 |
sAdd = client.SAdd("set2", "d") |
| 1718 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1719 |
sAdd = client.SAdd("set2", "e") |
| 1720 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1721 |
|
| 1722 |
sDiff := client.SDiff("set1", "set2") |
| 1723 |
Expect(sDiff.Err()).NotTo(HaveOccurred()) |
| 1724 |
Expect(sDiff.Val()).To(ConsistOf([]string{"a", "b"})) |
| 1725 |
}) |
| 1726 |
|
| 1727 |
It("should SDiffStore", func() { |
| 1728 |
sAdd := client.SAdd("set1", "a") |
| 1729 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1730 |
sAdd = client.SAdd("set1", "b") |
| 1731 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1732 |
sAdd = client.SAdd("set1", "c") |
| 1733 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1734 |
|
| 1735 |
sAdd = client.SAdd("set2", "c") |
| 1736 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1737 |
sAdd = client.SAdd("set2", "d") |
| 1738 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1739 |
sAdd = client.SAdd("set2", "e") |
| 1740 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1741 |
|
| 1742 |
sDiffStore := client.SDiffStore("set", "set1", "set2") |
| 1743 |
Expect(sDiffStore.Err()).NotTo(HaveOccurred()) |
| 1744 |
Expect(sDiffStore.Val()).To(Equal(int64(2))) |
| 1745 |
|
| 1746 |
sMembers := client.SMembers("set") |
| 1747 |
Expect(sMembers.Err()).NotTo(HaveOccurred()) |
| 1748 |
Expect(sMembers.Val()).To(ConsistOf([]string{"a", "b"})) |
| 1749 |
}) |
| 1750 |
|
| 1751 |
It("should SInter", func() { |
| 1752 |
sAdd := client.SAdd("set1", "a") |
| 1753 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1754 |
sAdd = client.SAdd("set1", "b") |
| 1755 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1756 |
sAdd = client.SAdd("set1", "c") |
| 1757 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1758 |
|
| 1759 |
sAdd = client.SAdd("set2", "c") |
| 1760 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1761 |
sAdd = client.SAdd("set2", "d") |
| 1762 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1763 |
sAdd = client.SAdd("set2", "e") |
| 1764 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1765 |
|
| 1766 |
sInter := client.SInter("set1", "set2") |
| 1767 |
Expect(sInter.Err()).NotTo(HaveOccurred()) |
| 1768 |
Expect(sInter.Val()).To(Equal([]string{"c"})) |
| 1769 |
}) |
| 1770 |
|
| 1771 |
It("should SInterStore", func() { |
| 1772 |
sAdd := client.SAdd("set1", "a") |
| 1773 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1774 |
sAdd = client.SAdd("set1", "b") |
| 1775 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1776 |
sAdd = client.SAdd("set1", "c") |
| 1777 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1778 |
|
| 1779 |
sAdd = client.SAdd("set2", "c") |
| 1780 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1781 |
sAdd = client.SAdd("set2", "d") |
| 1782 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1783 |
sAdd = client.SAdd("set2", "e") |
| 1784 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1785 |
|
| 1786 |
sInterStore := client.SInterStore("set", "set1", "set2") |
| 1787 |
Expect(sInterStore.Err()).NotTo(HaveOccurred()) |
| 1788 |
Expect(sInterStore.Val()).To(Equal(int64(1))) |
| 1789 |
|
| 1790 |
sMembers := client.SMembers("set") |
| 1791 |
Expect(sMembers.Err()).NotTo(HaveOccurred()) |
| 1792 |
Expect(sMembers.Val()).To(Equal([]string{"c"})) |
| 1793 |
}) |
| 1794 |
|
| 1795 |
It("should IsMember", func() { |
| 1796 |
sAdd := client.SAdd("set", "one") |
| 1797 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1798 |
|
| 1799 |
sIsMember := client.SIsMember("set", "one") |
| 1800 |
Expect(sIsMember.Err()).NotTo(HaveOccurred()) |
| 1801 |
Expect(sIsMember.Val()).To(Equal(true)) |
| 1802 |
|
| 1803 |
sIsMember = client.SIsMember("set", "two") |
| 1804 |
Expect(sIsMember.Err()).NotTo(HaveOccurred()) |
| 1805 |
Expect(sIsMember.Val()).To(Equal(false)) |
| 1806 |
}) |
| 1807 |
|
| 1808 |
It("should SMembers", func() { |
| 1809 |
sAdd := client.SAdd("set", "Hello") |
| 1810 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1811 |
sAdd = client.SAdd("set", "World") |
| 1812 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1813 |
|
| 1814 |
sMembers := client.SMembers("set") |
| 1815 |
Expect(sMembers.Err()).NotTo(HaveOccurred()) |
| 1816 |
Expect(sMembers.Val()).To(ConsistOf([]string{"Hello", "World"})) |
| 1817 |
}) |
| 1818 |
|
| 1819 |
It("should SMove", func() { |
| 1820 |
sAdd := client.SAdd("set1", "one") |
| 1821 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1822 |
sAdd = client.SAdd("set1", "two") |
| 1823 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1824 |
|
| 1825 |
sAdd = client.SAdd("set2", "three") |
| 1826 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1827 |
|
| 1828 |
sMove := client.SMove("set1", "set2", "two") |
| 1829 |
Expect(sMove.Err()).NotTo(HaveOccurred()) |
| 1830 |
Expect(sMove.Val()).To(Equal(true)) |
| 1831 |
|
| 1832 |
sMembers := client.SMembers("set1") |
| 1833 |
Expect(sMembers.Err()).NotTo(HaveOccurred()) |
| 1834 |
Expect(sMembers.Val()).To(Equal([]string{"one"})) |
| 1835 |
|
| 1836 |
sMembers = client.SMembers("set2") |
| 1837 |
Expect(sMembers.Err()).NotTo(HaveOccurred()) |
| 1838 |
Expect(sMembers.Val()).To(ConsistOf([]string{"three", "two"})) |
| 1839 |
}) |
| 1840 |
|
| 1841 |
It("should SPop", func() { |
| 1842 |
sAdd := client.SAdd("set", "one") |
| 1843 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1844 |
sAdd = client.SAdd("set", "two") |
| 1845 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1846 |
sAdd = client.SAdd("set", "three") |
| 1847 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1848 |
|
| 1849 |
sPop := client.SPop("set") |
| 1850 |
Expect(sPop.Err()).NotTo(HaveOccurred()) |
| 1851 |
Expect(sPop.Val()).NotTo(Equal("")) |
| 1852 |
|
| 1853 |
sMembers := client.SMembers("set") |
| 1854 |
Expect(sMembers.Err()).NotTo(HaveOccurred()) |
| 1855 |
Expect(sMembers.Val()).To(HaveLen(2)) |
| 1856 |
|
| 1857 |
}) |
| 1858 |
|
| 1859 |
It("should SPopN", func() { |
| 1860 |
sAdd := client.SAdd("set", "one") |
| 1861 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1862 |
sAdd = client.SAdd("set", "two") |
| 1863 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1864 |
sAdd = client.SAdd("set", "three") |
| 1865 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1866 |
sAdd = client.SAdd("set", "four") |
| 1867 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1868 |
|
| 1869 |
sPopN := client.SPopN("set", 1) |
| 1870 |
Expect(sPopN.Err()).NotTo(HaveOccurred()) |
| 1871 |
Expect(sPopN.Val()).NotTo(Equal([]string{""})) |
| 1872 |
|
| 1873 |
sMembers := client.SMembers("set") |
| 1874 |
Expect(sMembers.Err()).NotTo(HaveOccurred()) |
| 1875 |
Expect(sMembers.Val()).To(HaveLen(3)) |
| 1876 |
|
| 1877 |
sPopN = client.SPopN("set", 4) |
| 1878 |
Expect(sPopN.Err()).NotTo(HaveOccurred()) |
| 1879 |
Expect(sPopN.Val()).To(HaveLen(3)) |
| 1880 |
|
| 1881 |
sMembers = client.SMembers("set") |
| 1882 |
Expect(sMembers.Err()).NotTo(HaveOccurred()) |
| 1883 |
Expect(sMembers.Val()).To(HaveLen(0)) |
| 1884 |
}) |
| 1885 |
|
| 1886 |
It("should SRandMember and SRandMemberN", func() { |
| 1887 |
err := client.SAdd("set", "one").Err() |
| 1888 |
Expect(err).NotTo(HaveOccurred()) |
| 1889 |
err = client.SAdd("set", "two").Err() |
| 1890 |
Expect(err).NotTo(HaveOccurred()) |
| 1891 |
err = client.SAdd("set", "three").Err() |
| 1892 |
Expect(err).NotTo(HaveOccurred()) |
| 1893 |
|
| 1894 |
members, err := client.SMembers("set").Result() |
| 1895 |
Expect(err).NotTo(HaveOccurred()) |
| 1896 |
Expect(members).To(HaveLen(3)) |
| 1897 |
|
| 1898 |
member, err := client.SRandMember("set").Result() |
| 1899 |
Expect(err).NotTo(HaveOccurred()) |
| 1900 |
Expect(member).NotTo(Equal("")) |
| 1901 |
|
| 1902 |
members, err = client.SRandMemberN("set", 2).Result() |
| 1903 |
Expect(err).NotTo(HaveOccurred()) |
| 1904 |
Expect(members).To(HaveLen(2)) |
| 1905 |
}) |
| 1906 |
|
| 1907 |
It("should SRem", func() { |
| 1908 |
sAdd := client.SAdd("set", "one") |
| 1909 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1910 |
sAdd = client.SAdd("set", "two") |
| 1911 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1912 |
sAdd = client.SAdd("set", "three") |
| 1913 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1914 |
|
| 1915 |
sRem := client.SRem("set", "one") |
| 1916 |
Expect(sRem.Err()).NotTo(HaveOccurred()) |
| 1917 |
Expect(sRem.Val()).To(Equal(int64(1))) |
| 1918 |
|
| 1919 |
sRem = client.SRem("set", "four") |
| 1920 |
Expect(sRem.Err()).NotTo(HaveOccurred()) |
| 1921 |
Expect(sRem.Val()).To(Equal(int64(0))) |
| 1922 |
|
| 1923 |
sMembers := client.SMembers("set") |
| 1924 |
Expect(sMembers.Err()).NotTo(HaveOccurred()) |
| 1925 |
Expect(sMembers.Val()).To(ConsistOf([]string{"three", "two"})) |
| 1926 |
}) |
| 1927 |
|
| 1928 |
It("should SUnion", func() { |
| 1929 |
sAdd := client.SAdd("set1", "a") |
| 1930 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1931 |
sAdd = client.SAdd("set1", "b") |
| 1932 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1933 |
sAdd = client.SAdd("set1", "c") |
| 1934 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1935 |
|
| 1936 |
sAdd = client.SAdd("set2", "c") |
| 1937 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1938 |
sAdd = client.SAdd("set2", "d") |
| 1939 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1940 |
sAdd = client.SAdd("set2", "e") |
| 1941 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1942 |
|
| 1943 |
sUnion := client.SUnion("set1", "set2") |
| 1944 |
Expect(sUnion.Err()).NotTo(HaveOccurred()) |
| 1945 |
Expect(sUnion.Val()).To(HaveLen(5)) |
| 1946 |
}) |
| 1947 |
|
| 1948 |
It("should SUnionStore", func() { |
| 1949 |
sAdd := client.SAdd("set1", "a") |
| 1950 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1951 |
sAdd = client.SAdd("set1", "b") |
| 1952 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1953 |
sAdd = client.SAdd("set1", "c") |
| 1954 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1955 |
|
| 1956 |
sAdd = client.SAdd("set2", "c") |
| 1957 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1958 |
sAdd = client.SAdd("set2", "d") |
| 1959 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1960 |
sAdd = client.SAdd("set2", "e") |
| 1961 |
Expect(sAdd.Err()).NotTo(HaveOccurred()) |
| 1962 |
|
| 1963 |
sUnionStore := client.SUnionStore("set", "set1", "set2") |
| 1964 |
Expect(sUnionStore.Err()).NotTo(HaveOccurred()) |
| 1965 |
Expect(sUnionStore.Val()).To(Equal(int64(5))) |
| 1966 |
|
| 1967 |
sMembers := client.SMembers("set") |
| 1968 |
Expect(sMembers.Err()).NotTo(HaveOccurred()) |
| 1969 |
Expect(sMembers.Val()).To(HaveLen(5)) |
| 1970 |
}) |
| 1971 |
|
| 1972 |
}) |
| 1973 |
|
| 1974 |
Describe("sorted sets", func() { |
| 1975 |
|
| 1976 |
It("should ZAdd", func() { |
| 1977 |
added, err := client.ZAdd("zset", redis.Z{1, "one"}).Result() |
| 1978 |
Expect(err).NotTo(HaveOccurred()) |
| 1979 |
Expect(added).To(Equal(int64(1))) |
| 1980 |
|
| 1981 |
added, err = client.ZAdd("zset", redis.Z{1, "uno"}).Result() |
| 1982 |
Expect(err).NotTo(HaveOccurred()) |
| 1983 |
Expect(added).To(Equal(int64(1))) |
| 1984 |
|
| 1985 |
added, err = client.ZAdd("zset", redis.Z{2, "two"}).Result() |
| 1986 |
Expect(err).NotTo(HaveOccurred()) |
| 1987 |
Expect(added).To(Equal(int64(1))) |
| 1988 |
|
| 1989 |
added, err = client.ZAdd("zset", redis.Z{3, "two"}).Result() |
| 1990 |
Expect(err).NotTo(HaveOccurred()) |
| 1991 |
Expect(added).To(Equal(int64(0))) |
| 1992 |
|
| 1993 |
vals, err := client.ZRangeWithScores("zset", 0, -1).Result() |
| 1994 |
Expect(err).NotTo(HaveOccurred()) |
| 1995 |
Expect(vals).To(Equal([]redis.Z{{1, "one"}, {1, "uno"}, {3, "two"}})) |
| 1996 |
}) |
| 1997 |
|
| 1998 |
It("should ZAdd bytes", func() { |
| 1999 |
added, err := client.ZAdd("zset", redis.Z{1, []byte("one")}).Result() |
| 2000 |
Expect(err).NotTo(HaveOccurred()) |
| 2001 |
Expect(added).To(Equal(int64(1))) |
| 2002 |
|
| 2003 |
added, err = client.ZAdd("zset", redis.Z{1, []byte("uno")}).Result() |
| 2004 |
Expect(err).NotTo(HaveOccurred()) |
| 2005 |
Expect(added).To(Equal(int64(1))) |
| 2006 |
|
| 2007 |
added, err = client.ZAdd("zset", redis.Z{2, []byte("two")}).Result() |
| 2008 |
Expect(err).NotTo(HaveOccurred()) |
| 2009 |
Expect(added).To(Equal(int64(1))) |
| 2010 |
|
| 2011 |
added, err = client.ZAdd("zset", redis.Z{3, []byte("two")}).Result() |
| 2012 |
Expect(err).NotTo(HaveOccurred()) |
| 2013 |
Expect(added).To(Equal(int64(0))) |
| 2014 |
|
| 2015 |
val, err := client.ZRangeWithScores("zset", 0, -1).Result() |
| 2016 |
Expect(err).NotTo(HaveOccurred()) |
| 2017 |
Expect(val).To(Equal([]redis.Z{{1, "one"}, {1, "uno"}, {3, "two"}})) |
| 2018 |
}) |
| 2019 |
|
| 2020 |
It("should ZAddNX", func() { |
| 2021 |
added, err := client.ZAddNX("zset", redis.Z{1, "one"}).Result() |
| 2022 |
Expect(err).NotTo(HaveOccurred()) |
| 2023 |
Expect(added).To(Equal(int64(1))) |
| 2024 |
|
| 2025 |
vals, err := client.ZRangeWithScores("zset", 0, -1).Result() |
| 2026 |
Expect(err).NotTo(HaveOccurred()) |
| 2027 |
Expect(vals).To(Equal([]redis.Z{{1, "one"}})) |
| 2028 |
|
| 2029 |
added, err = client.ZAddNX("zset", redis.Z{2, "one"}).Result() |
| 2030 |
Expect(err).NotTo(HaveOccurred()) |
| 2031 |
Expect(added).To(Equal(int64(0))) |
| 2032 |
|
| 2033 |
vals, err = client.ZRangeWithScores("zset", 0, -1).Result() |
| 2034 |
Expect(err).NotTo(HaveOccurred()) |
| 2035 |
Expect(vals).To(Equal([]redis.Z{{1, "one"}})) |
| 2036 |
}) |
| 2037 |
|
| 2038 |
It("should ZAddXX", func() { |
| 2039 |
added, err := client.ZAddXX("zset", redis.Z{1, "one"}).Result() |
| 2040 |
Expect(err).NotTo(HaveOccurred()) |
| 2041 |
Expect(added).To(Equal(int64(0))) |
| 2042 |
|
| 2043 |
vals, err := client.ZRangeWithScores("zset", 0, -1).Result() |
| 2044 |
Expect(err).NotTo(HaveOccurred()) |
| 2045 |
Expect(vals).To(BeEmpty()) |
| 2046 |
|
| 2047 |
added, err = client.ZAdd("zset", redis.Z{1, "one"}).Result() |
| 2048 |
Expect(err).NotTo(HaveOccurred()) |
| 2049 |
Expect(added).To(Equal(int64(1))) |
| 2050 |
|
| 2051 |
added, err = client.ZAddXX("zset", redis.Z{2, "one"}).Result() |
| 2052 |
Expect(err).NotTo(HaveOccurred()) |
| 2053 |
Expect(added).To(Equal(int64(0))) |
| 2054 |
|
| 2055 |
vals, err = client.ZRangeWithScores("zset", 0, -1).Result() |
| 2056 |
Expect(err).NotTo(HaveOccurred()) |
| 2057 |
Expect(vals).To(Equal([]redis.Z{{2, "one"}})) |
| 2058 |
}) |
| 2059 |
|
| 2060 |
It("should ZAddCh", func() { |
| 2061 |
changed, err := client.ZAddCh("zset", redis.Z{1, "one"}).Result() |
| 2062 |
Expect(err).NotTo(HaveOccurred()) |
| 2063 |
Expect(changed).To(Equal(int64(1))) |
| 2064 |
|
| 2065 |
changed, err = client.ZAddCh("zset", redis.Z{1, "one"}).Result() |
| 2066 |
Expect(err).NotTo(HaveOccurred()) |
| 2067 |
Expect(changed).To(Equal(int64(0))) |
| 2068 |
}) |
| 2069 |
|
| 2070 |
It("should ZAddNXCh", func() { |
| 2071 |
changed, err := client.ZAddNXCh("zset", redis.Z{1, "one"}).Result() |
| 2072 |
Expect(err).NotTo(HaveOccurred()) |
| 2073 |
Expect(changed).To(Equal(int64(1))) |
| 2074 |
|
| 2075 |
vals, err := client.ZRangeWithScores("zset", 0, -1).Result() |
| 2076 |
Expect(err).NotTo(HaveOccurred()) |
| 2077 |
Expect(vals).To(Equal([]redis.Z{{1, "one"}})) |
| 2078 |
|
| 2079 |
changed, err = client.ZAddNXCh("zset", redis.Z{2, "one"}).Result() |
| 2080 |
Expect(err).NotTo(HaveOccurred()) |
| 2081 |
Expect(changed).To(Equal(int64(0))) |
| 2082 |
|
| 2083 |
vals, err = client.ZRangeWithScores("zset", 0, -1).Result() |
| 2084 |
Expect(err).NotTo(HaveOccurred()) |
| 2085 |
Expect(vals).To(Equal([]redis.Z{{1, "one"}})) |
| 2086 |
}) |
| 2087 |
|
| 2088 |
It("should ZAddXXCh", func() { |
| 2089 |
changed, err := client.ZAddXXCh("zset", redis.Z{1, "one"}).Result() |
| 2090 |
Expect(err).NotTo(HaveOccurred()) |
| 2091 |
Expect(changed).To(Equal(int64(0))) |
| 2092 |
|
| 2093 |
vals, err := client.ZRangeWithScores("zset", 0, -1).Result() |
| 2094 |
Expect(err).NotTo(HaveOccurred()) |
| 2095 |
Expect(vals).To(BeEmpty()) |
| 2096 |
|
| 2097 |
added, err := client.ZAdd("zset", redis.Z{1, "one"}).Result() |
| 2098 |
Expect(err).NotTo(HaveOccurred()) |
| 2099 |
Expect(added).To(Equal(int64(1))) |
| 2100 |
|
| 2101 |
changed, err = client.ZAddXXCh("zset", redis.Z{2, "one"}).Result() |
| 2102 |
Expect(err).NotTo(HaveOccurred()) |
| 2103 |
Expect(changed).To(Equal(int64(1))) |
| 2104 |
|
| 2105 |
vals, err = client.ZRangeWithScores("zset", 0, -1).Result() |
| 2106 |
Expect(err).NotTo(HaveOccurred()) |
| 2107 |
Expect(vals).To(Equal([]redis.Z{{2, "one"}})) |
| 2108 |
}) |
| 2109 |
|
| 2110 |
It("should ZIncr", func() { |
| 2111 |
score, err := client.ZIncr("zset", redis.Z{1, "one"}).Result() |
| 2112 |
Expect(err).NotTo(HaveOccurred()) |
| 2113 |
Expect(score).To(Equal(float64(1))) |
| 2114 |
|
| 2115 |
vals, err := client.ZRangeWithScores("zset", 0, -1).Result() |
| 2116 |
Expect(err).NotTo(HaveOccurred()) |
| 2117 |
Expect(vals).To(Equal([]redis.Z{{1, "one"}})) |
| 2118 |
|
| 2119 |
score, err = client.ZIncr("zset", redis.Z{1, "one"}).Result() |
| 2120 |
Expect(err).NotTo(HaveOccurred()) |
| 2121 |
Expect(score).To(Equal(float64(2))) |
| 2122 |
|
| 2123 |
vals, err = client.ZRangeWithScores("zset", 0, -1).Result() |
| 2124 |
Expect(err).NotTo(HaveOccurred()) |
| 2125 |
Expect(vals).To(Equal([]redis.Z{{2, "one"}})) |
| 2126 |
}) |
| 2127 |
|
| 2128 |
It("should ZIncrNX", func() { |
| 2129 |
score, err := client.ZIncrNX("zset", redis.Z{1, "one"}).Result() |
| 2130 |
Expect(err).NotTo(HaveOccurred()) |
| 2131 |
Expect(score).To(Equal(float64(1))) |
| 2132 |
|
| 2133 |
vals, err := client.ZRangeWithScores("zset", 0, -1).Result() |
| 2134 |
Expect(err).NotTo(HaveOccurred()) |
| 2135 |
Expect(vals).To(Equal([]redis.Z{{1, "one"}})) |
| 2136 |
|
| 2137 |
score, err = client.ZIncrNX("zset", redis.Z{1, "one"}).Result() |
| 2138 |
Expect(err).To(Equal(redis.Nil)) |
| 2139 |
Expect(score).To(Equal(float64(0))) |
| 2140 |
|
| 2141 |
vals, err = client.ZRangeWithScores("zset", 0, -1).Result() |
| 2142 |
Expect(err).NotTo(HaveOccurred()) |
| 2143 |
Expect(vals).To(Equal([]redis.Z{{1, "one"}})) |
| 2144 |
}) |
| 2145 |
|
| 2146 |
It("should ZIncrXX", func() { |
| 2147 |
score, err := client.ZIncrXX("zset", redis.Z{1, "one"}).Result() |
| 2148 |
Expect(err).To(Equal(redis.Nil)) |
| 2149 |
Expect(score).To(Equal(float64(0))) |
| 2150 |
|
| 2151 |
vals, err := client.ZRangeWithScores("zset", 0, -1).Result() |
| 2152 |
Expect(err).NotTo(HaveOccurred()) |
| 2153 |
Expect(vals).To(BeEmpty()) |
| 2154 |
|
| 2155 |
added, err := client.ZAdd("zset", redis.Z{1, "one"}).Result() |
| 2156 |
Expect(err).NotTo(HaveOccurred()) |
| 2157 |
Expect(added).To(Equal(int64(1))) |
| 2158 |
|
| 2159 |
score, err = client.ZIncrXX("zset", redis.Z{1, "one"}).Result() |
| 2160 |
Expect(err).NotTo(HaveOccurred()) |
| 2161 |
Expect(score).To(Equal(float64(2))) |
| 2162 |
|
| 2163 |
vals, err = client.ZRangeWithScores("zset", 0, -1).Result() |
| 2164 |
Expect(err).NotTo(HaveOccurred()) |
| 2165 |
Expect(vals).To(Equal([]redis.Z{{2, "one"}})) |
| 2166 |
}) |
| 2167 |
|
| 2168 |
It("should ZCard", func() { |
| 2169 |
zAdd := client.ZAdd("zset", redis.Z{1, "one"}) |
| 2170 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2171 |
zAdd = client.ZAdd("zset", redis.Z{2, "two"}) |
| 2172 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2173 |
|
| 2174 |
zCard := client.ZCard("zset") |
| 2175 |
Expect(zCard.Err()).NotTo(HaveOccurred()) |
| 2176 |
Expect(zCard.Val()).To(Equal(int64(2))) |
| 2177 |
}) |
| 2178 |
|
| 2179 |
It("should ZCount", func() { |
| 2180 |
zAdd := client.ZAdd("zset", redis.Z{1, "one"}) |
| 2181 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2182 |
zAdd = client.ZAdd("zset", redis.Z{2, "two"}) |
| 2183 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2184 |
zAdd = client.ZAdd("zset", redis.Z{3, "three"}) |
| 2185 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2186 |
|
| 2187 |
zCount := client.ZCount("zset", "-inf", "+inf") |
| 2188 |
Expect(zCount.Err()).NotTo(HaveOccurred()) |
| 2189 |
Expect(zCount.Val()).To(Equal(int64(3))) |
| 2190 |
|
| 2191 |
zCount = client.ZCount("zset", "(1", "3") |
| 2192 |
Expect(zCount.Err()).NotTo(HaveOccurred()) |
| 2193 |
Expect(zCount.Val()).To(Equal(int64(2))) |
| 2194 |
}) |
| 2195 |
|
| 2196 |
It("should ZIncrBy", func() { |
| 2197 |
zAdd := client.ZAdd("zset", redis.Z{1, "one"}) |
| 2198 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2199 |
zAdd = client.ZAdd("zset", redis.Z{2, "two"}) |
| 2200 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2201 |
|
| 2202 |
zIncrBy := client.ZIncrBy("zset", 2, "one") |
| 2203 |
Expect(zIncrBy.Err()).NotTo(HaveOccurred()) |
| 2204 |
Expect(zIncrBy.Val()).To(Equal(float64(3))) |
| 2205 |
|
| 2206 |
val, err := client.ZRangeWithScores("zset", 0, -1).Result() |
| 2207 |
Expect(err).NotTo(HaveOccurred()) |
| 2208 |
Expect(val).To(Equal([]redis.Z{{2, "two"}, {3, "one"}})) |
| 2209 |
}) |
| 2210 |
|
| 2211 |
It("should ZInterStore", func() { |
| 2212 |
zAdd := client.ZAdd("zset1", redis.Z{1, "one"}) |
| 2213 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2214 |
zAdd = client.ZAdd("zset1", redis.Z{2, "two"}) |
| 2215 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2216 |
|
| 2217 |
zAdd = client.ZAdd("zset2", redis.Z{1, "one"}) |
| 2218 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2219 |
zAdd = client.ZAdd("zset2", redis.Z{2, "two"}) |
| 2220 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2221 |
zAdd = client.ZAdd("zset3", redis.Z{3, "two"}) |
| 2222 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2223 |
|
| 2224 |
zInterStore := client.ZInterStore( |
| 2225 |
"out", redis.ZStore{Weights: []float64{2, 3}}, "zset1", "zset2") |
| 2226 |
Expect(zInterStore.Err()).NotTo(HaveOccurred()) |
| 2227 |
Expect(zInterStore.Val()).To(Equal(int64(2))) |
| 2228 |
|
| 2229 |
val, err := client.ZRangeWithScores("out", 0, -1).Result() |
| 2230 |
Expect(err).NotTo(HaveOccurred()) |
| 2231 |
Expect(val).To(Equal([]redis.Z{{5, "one"}, {10, "two"}})) |
| 2232 |
}) |
| 2233 |
|
| 2234 |
It("should ZRange", func() { |
| 2235 |
zAdd := client.ZAdd("zset", redis.Z{1, "one"}) |
| 2236 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2237 |
zAdd = client.ZAdd("zset", redis.Z{2, "two"}) |
| 2238 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2239 |
zAdd = client.ZAdd("zset", redis.Z{3, "three"}) |
| 2240 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2241 |
|
| 2242 |
zRange := client.ZRange("zset", 0, -1) |
| 2243 |
Expect(zRange.Err()).NotTo(HaveOccurred()) |
| 2244 |
Expect(zRange.Val()).To(Equal([]string{"one", "two", "three"})) |
| 2245 |
|
| 2246 |
zRange = client.ZRange("zset", 2, 3) |
| 2247 |
Expect(zRange.Err()).NotTo(HaveOccurred()) |
| 2248 |
Expect(zRange.Val()).To(Equal([]string{"three"})) |
| 2249 |
|
| 2250 |
zRange = client.ZRange("zset", -2, -1) |
| 2251 |
Expect(zRange.Err()).NotTo(HaveOccurred()) |
| 2252 |
Expect(zRange.Val()).To(Equal([]string{"two", "three"})) |
| 2253 |
}) |
| 2254 |
|
| 2255 |
It("should ZRangeWithScores", func() { |
| 2256 |
zAdd := client.ZAdd("zset", redis.Z{1, "one"}) |
| 2257 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2258 |
zAdd = client.ZAdd("zset", redis.Z{2, "two"}) |
| 2259 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2260 |
zAdd = client.ZAdd("zset", redis.Z{3, "three"}) |
| 2261 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2262 |
|
| 2263 |
val, err := client.ZRangeWithScores("zset", 0, -1).Result() |
| 2264 |
Expect(err).NotTo(HaveOccurred()) |
| 2265 |
Expect(val).To(Equal([]redis.Z{{1, "one"}, {2, "two"}, {3, "three"}})) |
| 2266 |
|
| 2267 |
val, err = client.ZRangeWithScores("zset", 2, 3).Result() |
| 2268 |
Expect(err).NotTo(HaveOccurred()) |
| 2269 |
Expect(val).To(Equal([]redis.Z{{3, "three"}})) |
| 2270 |
|
| 2271 |
val, err = client.ZRangeWithScores("zset", -2, -1).Result() |
| 2272 |
Expect(err).NotTo(HaveOccurred()) |
| 2273 |
Expect(val).To(Equal([]redis.Z{{2, "two"}, {3, "three"}})) |
| 2274 |
}) |
| 2275 |
|
| 2276 |
It("should ZRangeByScore", func() { |
| 2277 |
zAdd := client.ZAdd("zset", redis.Z{1, "one"}) |
| 2278 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2279 |
zAdd = client.ZAdd("zset", redis.Z{2, "two"}) |
| 2280 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2281 |
zAdd = client.ZAdd("zset", redis.Z{3, "three"}) |
| 2282 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2283 |
|
| 2284 |
zRangeByScore := client.ZRangeByScore("zset", redis.ZRangeBy{ |
| 2285 |
Min: "-inf", |
| 2286 |
Max: "+inf", |
| 2287 |
}) |
| 2288 |
Expect(zRangeByScore.Err()).NotTo(HaveOccurred()) |
| 2289 |
Expect(zRangeByScore.Val()).To(Equal([]string{"one", "two", "three"})) |
| 2290 |
|
| 2291 |
zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeBy{ |
| 2292 |
Min: "1", |
| 2293 |
Max: "2", |
| 2294 |
}) |
| 2295 |
Expect(zRangeByScore.Err()).NotTo(HaveOccurred()) |
| 2296 |
Expect(zRangeByScore.Val()).To(Equal([]string{"one", "two"})) |
| 2297 |
|
| 2298 |
zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeBy{ |
| 2299 |
Min: "(1", |
| 2300 |
Max: "2", |
| 2301 |
}) |
| 2302 |
Expect(zRangeByScore.Err()).NotTo(HaveOccurred()) |
| 2303 |
Expect(zRangeByScore.Val()).To(Equal([]string{"two"})) |
| 2304 |
|
| 2305 |
zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeBy{ |
| 2306 |
Min: "(1", |
| 2307 |
Max: "(2", |
| 2308 |
}) |
| 2309 |
Expect(zRangeByScore.Err()).NotTo(HaveOccurred()) |
| 2310 |
Expect(zRangeByScore.Val()).To(Equal([]string{})) |
| 2311 |
}) |
| 2312 |
|
| 2313 |
It("should ZRangeByLex", func() { |
| 2314 |
zAdd := client.ZAdd("zset", redis.Z{0, "a"}) |
| 2315 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2316 |
zAdd = client.ZAdd("zset", redis.Z{0, "b"}) |
| 2317 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2318 |
zAdd = client.ZAdd("zset", redis.Z{0, "c"}) |
| 2319 |
|
| 2320 |
zRangeByLex := client.ZRangeByLex("zset", redis.ZRangeBy{ |
| 2321 |
Min: "-", |
| 2322 |
Max: "+", |
| 2323 |
}) |
| 2324 |
Expect(zRangeByLex.Err()).NotTo(HaveOccurred()) |
| 2325 |
Expect(zRangeByLex.Val()).To(Equal([]string{"a", "b", "c"})) |
| 2326 |
|
| 2327 |
zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeBy{ |
| 2328 |
Min: "[a", |
| 2329 |
Max: "[b", |
| 2330 |
}) |
| 2331 |
Expect(zRangeByLex.Err()).NotTo(HaveOccurred()) |
| 2332 |
Expect(zRangeByLex.Val()).To(Equal([]string{"a", "b"})) |
| 2333 |
|
| 2334 |
zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeBy{ |
| 2335 |
Min: "(a", |
| 2336 |
Max: "[b", |
| 2337 |
}) |
| 2338 |
Expect(zRangeByLex.Err()).NotTo(HaveOccurred()) |
| 2339 |
Expect(zRangeByLex.Val()).To(Equal([]string{"b"})) |
| 2340 |
|
| 2341 |
zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeBy{ |
| 2342 |
Min: "(a", |
| 2343 |
Max: "(b", |
| 2344 |
}) |
| 2345 |
Expect(zRangeByLex.Err()).NotTo(HaveOccurred()) |
| 2346 |
Expect(zRangeByLex.Val()).To(Equal([]string{})) |
| 2347 |
}) |
| 2348 |
|
| 2349 |
It("should ZRangeByScoreWithScoresMap", func() { |
| 2350 |
zAdd := client.ZAdd("zset", redis.Z{1, "one"}) |
| 2351 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2352 |
zAdd = client.ZAdd("zset", redis.Z{2, "two"}) |
| 2353 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2354 |
zAdd = client.ZAdd("zset", redis.Z{3, "three"}) |
| 2355 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2356 |
|
| 2357 |
val, err := client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{ |
| 2358 |
Min: "-inf", |
| 2359 |
Max: "+inf", |
| 2360 |
}).Result() |
| 2361 |
Expect(err).NotTo(HaveOccurred()) |
| 2362 |
Expect(val).To(Equal([]redis.Z{{1, "one"}, {2, "two"}, {3, "three"}})) |
| 2363 |
|
| 2364 |
val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{ |
| 2365 |
Min: "1", |
| 2366 |
Max: "2", |
| 2367 |
}).Result() |
| 2368 |
Expect(err).NotTo(HaveOccurred()) |
| 2369 |
Expect(val).To(Equal([]redis.Z{{1, "one"}, {2, "two"}})) |
| 2370 |
|
| 2371 |
val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{ |
| 2372 |
Min: "(1", |
| 2373 |
Max: "2", |
| 2374 |
}).Result() |
| 2375 |
Expect(err).NotTo(HaveOccurred()) |
| 2376 |
Expect(val).To(Equal([]redis.Z{{2, "two"}})) |
| 2377 |
|
| 2378 |
val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{ |
| 2379 |
Min: "(1", |
| 2380 |
Max: "(2", |
| 2381 |
}).Result() |
| 2382 |
Expect(err).NotTo(HaveOccurred()) |
| 2383 |
Expect(val).To(Equal([]redis.Z{})) |
| 2384 |
}) |
| 2385 |
|
| 2386 |
It("should ZRank", func() { |
| 2387 |
zAdd := client.ZAdd("zset", redis.Z{1, "one"}) |
| 2388 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2389 |
zAdd = client.ZAdd("zset", redis.Z{2, "two"}) |
| 2390 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2391 |
zAdd = client.ZAdd("zset", redis.Z{3, "three"}) |
| 2392 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2393 |
|
| 2394 |
zRank := client.ZRank("zset", "three") |
| 2395 |
Expect(zRank.Err()).NotTo(HaveOccurred()) |
| 2396 |
Expect(zRank.Val()).To(Equal(int64(2))) |
| 2397 |
|
| 2398 |
zRank = client.ZRank("zset", "four") |
| 2399 |
Expect(zRank.Err()).To(Equal(redis.Nil)) |
| 2400 |
Expect(zRank.Val()).To(Equal(int64(0))) |
| 2401 |
}) |
| 2402 |
|
| 2403 |
It("should ZRem", func() { |
| 2404 |
zAdd := client.ZAdd("zset", redis.Z{1, "one"}) |
| 2405 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2406 |
zAdd = client.ZAdd("zset", redis.Z{2, "two"}) |
| 2407 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2408 |
zAdd = client.ZAdd("zset", redis.Z{3, "three"}) |
| 2409 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2410 |
|
| 2411 |
zRem := client.ZRem("zset", "two") |
| 2412 |
Expect(zRem.Err()).NotTo(HaveOccurred()) |
| 2413 |
Expect(zRem.Val()).To(Equal(int64(1))) |
| 2414 |
|
| 2415 |
val, err := client.ZRangeWithScores("zset", 0, -1).Result() |
| 2416 |
Expect(err).NotTo(HaveOccurred()) |
| 2417 |
Expect(val).To(Equal([]redis.Z{{1, "one"}, {3, "three"}})) |
| 2418 |
}) |
| 2419 |
|
| 2420 |
It("should ZRemRangeByRank", func() { |
| 2421 |
zAdd := client.ZAdd("zset", redis.Z{1, "one"}) |
| 2422 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2423 |
zAdd = client.ZAdd("zset", redis.Z{2, "two"}) |
| 2424 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2425 |
zAdd = client.ZAdd("zset", redis.Z{3, "three"}) |
| 2426 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2427 |
|
| 2428 |
zRemRangeByRank := client.ZRemRangeByRank("zset", 0, 1) |
| 2429 |
Expect(zRemRangeByRank.Err()).NotTo(HaveOccurred()) |
| 2430 |
Expect(zRemRangeByRank.Val()).To(Equal(int64(2))) |
| 2431 |
|
| 2432 |
val, err := client.ZRangeWithScores("zset", 0, -1).Result() |
| 2433 |
Expect(err).NotTo(HaveOccurred()) |
| 2434 |
Expect(val).To(Equal([]redis.Z{{3, "three"}})) |
| 2435 |
}) |
| 2436 |
|
| 2437 |
It("should ZRemRangeByScore", func() { |
| 2438 |
zAdd := client.ZAdd("zset", redis.Z{1, "one"}) |
| 2439 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2440 |
zAdd = client.ZAdd("zset", redis.Z{2, "two"}) |
| 2441 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2442 |
zAdd = client.ZAdd("zset", redis.Z{3, "three"}) |
| 2443 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2444 |
|
| 2445 |
zRemRangeByScore := client.ZRemRangeByScore("zset", "-inf", "(2") |
| 2446 |
Expect(zRemRangeByScore.Err()).NotTo(HaveOccurred()) |
| 2447 |
Expect(zRemRangeByScore.Val()).To(Equal(int64(1))) |
| 2448 |
|
| 2449 |
val, err := client.ZRangeWithScores("zset", 0, -1).Result() |
| 2450 |
Expect(err).NotTo(HaveOccurred()) |
| 2451 |
Expect(val).To(Equal([]redis.Z{{2, "two"}, {3, "three"}})) |
| 2452 |
}) |
| 2453 |
|
| 2454 |
It("should ZRemRangeByLex", func() { |
| 2455 |
zz := []redis.Z{ |
| 2456 |
{0, "aaaa"}, |
| 2457 |
{0, "b"}, |
| 2458 |
{0, "c"}, |
| 2459 |
{0, "d"}, |
| 2460 |
{0, "e"}, |
| 2461 |
{0, "foo"}, |
| 2462 |
{0, "zap"}, |
| 2463 |
{0, "zip"}, |
| 2464 |
{0, "ALPHA"}, |
| 2465 |
{0, "alpha"}, |
| 2466 |
} |
| 2467 |
for _, z := range zz { |
| 2468 |
err := client.ZAdd("zset", z).Err() |
| 2469 |
Expect(err).NotTo(HaveOccurred()) |
| 2470 |
} |
| 2471 |
|
| 2472 |
n, err := client.ZRemRangeByLex("zset", "[alpha", "[omega").Result() |
| 2473 |
Expect(err).NotTo(HaveOccurred()) |
| 2474 |
Expect(n).To(Equal(int64(6))) |
| 2475 |
|
| 2476 |
vals, err := client.ZRange("zset", 0, -1).Result() |
| 2477 |
Expect(err).NotTo(HaveOccurred()) |
| 2478 |
Expect(vals).To(Equal([]string{"ALPHA", "aaaa", "zap", "zip"})) |
| 2479 |
}) |
| 2480 |
|
| 2481 |
It("should ZRevRange", func() { |
| 2482 |
zAdd := client.ZAdd("zset", redis.Z{1, "one"}) |
| 2483 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2484 |
zAdd = client.ZAdd("zset", redis.Z{2, "two"}) |
| 2485 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2486 |
zAdd = client.ZAdd("zset", redis.Z{3, "three"}) |
| 2487 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2488 |
|
| 2489 |
zRevRange := client.ZRevRange("zset", 0, -1) |
| 2490 |
Expect(zRevRange.Err()).NotTo(HaveOccurred()) |
| 2491 |
Expect(zRevRange.Val()).To(Equal([]string{"three", "two", "one"})) |
| 2492 |
|
| 2493 |
zRevRange = client.ZRevRange("zset", 2, 3) |
| 2494 |
Expect(zRevRange.Err()).NotTo(HaveOccurred()) |
| 2495 |
Expect(zRevRange.Val()).To(Equal([]string{"one"})) |
| 2496 |
|
| 2497 |
zRevRange = client.ZRevRange("zset", -2, -1) |
| 2498 |
Expect(zRevRange.Err()).NotTo(HaveOccurred()) |
| 2499 |
Expect(zRevRange.Val()).To(Equal([]string{"two", "one"})) |
| 2500 |
}) |
| 2501 |
|
| 2502 |
It("should ZRevRangeWithScoresMap", func() { |
| 2503 |
zAdd := client.ZAdd("zset", redis.Z{1, "one"}) |
| 2504 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2505 |
zAdd = client.ZAdd("zset", redis.Z{2, "two"}) |
| 2506 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2507 |
zAdd = client.ZAdd("zset", redis.Z{3, "three"}) |
| 2508 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2509 |
|
| 2510 |
val, err := client.ZRevRangeWithScores("zset", 0, -1).Result() |
| 2511 |
Expect(err).NotTo(HaveOccurred()) |
| 2512 |
Expect(val).To(Equal([]redis.Z{{3, "three"}, {2, "two"}, {1, "one"}})) |
| 2513 |
|
| 2514 |
val, err = client.ZRevRangeWithScores("zset", 2, 3).Result() |
| 2515 |
Expect(err).NotTo(HaveOccurred()) |
| 2516 |
Expect(val).To(Equal([]redis.Z{{1, "one"}})) |
| 2517 |
|
| 2518 |
val, err = client.ZRevRangeWithScores("zset", -2, -1).Result() |
| 2519 |
Expect(err).NotTo(HaveOccurred()) |
| 2520 |
Expect(val).To(Equal([]redis.Z{{2, "two"}, {1, "one"}})) |
| 2521 |
}) |
| 2522 |
|
| 2523 |
It("should ZRevRangeByScore", func() { |
| 2524 |
zadd := client.ZAdd("zset", redis.Z{1, "one"}) |
| 2525 |
Expect(zadd.Err()).NotTo(HaveOccurred()) |
| 2526 |
zadd = client.ZAdd("zset", redis.Z{2, "two"}) |
| 2527 |
Expect(zadd.Err()).NotTo(HaveOccurred()) |
| 2528 |
zadd = client.ZAdd("zset", redis.Z{3, "three"}) |
| 2529 |
Expect(zadd.Err()).NotTo(HaveOccurred()) |
| 2530 |
|
| 2531 |
vals, err := client.ZRevRangeByScore( |
| 2532 |
"zset", redis.ZRangeBy{Max: "+inf", Min: "-inf"}).Result() |
| 2533 |
Expect(err).NotTo(HaveOccurred()) |
| 2534 |
Expect(vals).To(Equal([]string{"three", "two", "one"})) |
| 2535 |
|
| 2536 |
vals, err = client.ZRevRangeByScore( |
| 2537 |
"zset", redis.ZRangeBy{Max: "2", Min: "(1"}).Result() |
| 2538 |
Expect(err).NotTo(HaveOccurred()) |
| 2539 |
Expect(vals).To(Equal([]string{"two"})) |
| 2540 |
|
| 2541 |
vals, err = client.ZRevRangeByScore( |
| 2542 |
"zset", redis.ZRangeBy{Max: "(2", Min: "(1"}).Result() |
| 2543 |
Expect(err).NotTo(HaveOccurred()) |
| 2544 |
Expect(vals).To(Equal([]string{})) |
| 2545 |
}) |
| 2546 |
|
| 2547 |
It("should ZRevRangeByLex", func() { |
| 2548 |
zadd := client.ZAdd("zset", redis.Z{0, "a"}) |
| 2549 |
Expect(zadd.Err()).NotTo(HaveOccurred()) |
| 2550 |
zadd = client.ZAdd("zset", redis.Z{0, "b"}) |
| 2551 |
Expect(zadd.Err()).NotTo(HaveOccurred()) |
| 2552 |
zadd = client.ZAdd("zset", redis.Z{0, "c"}) |
| 2553 |
Expect(zadd.Err()).NotTo(HaveOccurred()) |
| 2554 |
|
| 2555 |
vals, err := client.ZRevRangeByLex( |
| 2556 |
"zset", redis.ZRangeBy{Max: "+", Min: "-"}).Result() |
| 2557 |
Expect(err).NotTo(HaveOccurred()) |
| 2558 |
Expect(vals).To(Equal([]string{"c", "b", "a"})) |
| 2559 |
|
| 2560 |
vals, err = client.ZRevRangeByLex( |
| 2561 |
"zset", redis.ZRangeBy{Max: "[b", Min: "(a"}).Result() |
| 2562 |
Expect(err).NotTo(HaveOccurred()) |
| 2563 |
Expect(vals).To(Equal([]string{"b"})) |
| 2564 |
|
| 2565 |
vals, err = client.ZRevRangeByLex( |
| 2566 |
"zset", redis.ZRangeBy{Max: "(b", Min: "(a"}).Result() |
| 2567 |
Expect(err).NotTo(HaveOccurred()) |
| 2568 |
Expect(vals).To(Equal([]string{})) |
| 2569 |
}) |
| 2570 |
|
| 2571 |
It("should ZRevRangeByScoreWithScores", func() { |
| 2572 |
zadd := client.ZAdd("zset", redis.Z{1, "one"}) |
| 2573 |
Expect(zadd.Err()).NotTo(HaveOccurred()) |
| 2574 |
zadd = client.ZAdd("zset", redis.Z{2, "two"}) |
| 2575 |
Expect(zadd.Err()).NotTo(HaveOccurred()) |
| 2576 |
zadd = client.ZAdd("zset", redis.Z{3, "three"}) |
| 2577 |
Expect(zadd.Err()).NotTo(HaveOccurred()) |
| 2578 |
|
| 2579 |
vals, err := client.ZRevRangeByScoreWithScores( |
| 2580 |
"zset", redis.ZRangeBy{Max: "+inf", Min: "-inf"}).Result() |
| 2581 |
Expect(err).NotTo(HaveOccurred()) |
| 2582 |
Expect(vals).To(Equal([]redis.Z{{3, "three"}, {2, "two"}, {1, "one"}})) |
| 2583 |
}) |
| 2584 |
|
| 2585 |
It("should ZRevRangeByScoreWithScoresMap", func() { |
| 2586 |
zAdd := client.ZAdd("zset", redis.Z{1, "one"}) |
| 2587 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2588 |
zAdd = client.ZAdd("zset", redis.Z{2, "two"}) |
| 2589 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2590 |
zAdd = client.ZAdd("zset", redis.Z{3, "three"}) |
| 2591 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2592 |
|
| 2593 |
val, err := client.ZRevRangeByScoreWithScores( |
| 2594 |
"zset", redis.ZRangeBy{Max: "+inf", Min: "-inf"}).Result() |
| 2595 |
Expect(err).NotTo(HaveOccurred()) |
| 2596 |
Expect(val).To(Equal([]redis.Z{{3, "three"}, {2, "two"}, {1, "one"}})) |
| 2597 |
|
| 2598 |
val, err = client.ZRevRangeByScoreWithScores( |
| 2599 |
"zset", redis.ZRangeBy{Max: "2", Min: "(1"}).Result() |
| 2600 |
Expect(err).NotTo(HaveOccurred()) |
| 2601 |
Expect(val).To(Equal([]redis.Z{{2, "two"}})) |
| 2602 |
|
| 2603 |
val, err = client.ZRevRangeByScoreWithScores( |
| 2604 |
"zset", redis.ZRangeBy{Max: "(2", Min: "(1"}).Result() |
| 2605 |
Expect(err).NotTo(HaveOccurred()) |
| 2606 |
Expect(val).To(Equal([]redis.Z{})) |
| 2607 |
}) |
| 2608 |
|
| 2609 |
It("should ZRevRank", func() { |
| 2610 |
zAdd := client.ZAdd("zset", redis.Z{1, "one"}) |
| 2611 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2612 |
zAdd = client.ZAdd("zset", redis.Z{2, "two"}) |
| 2613 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2614 |
zAdd = client.ZAdd("zset", redis.Z{3, "three"}) |
| 2615 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2616 |
|
| 2617 |
zRevRank := client.ZRevRank("zset", "one") |
| 2618 |
Expect(zRevRank.Err()).NotTo(HaveOccurred()) |
| 2619 |
Expect(zRevRank.Val()).To(Equal(int64(2))) |
| 2620 |
|
| 2621 |
zRevRank = client.ZRevRank("zset", "four") |
| 2622 |
Expect(zRevRank.Err()).To(Equal(redis.Nil)) |
| 2623 |
Expect(zRevRank.Val()).To(Equal(int64(0))) |
| 2624 |
}) |
| 2625 |
|
| 2626 |
It("should ZScore", func() { |
| 2627 |
zAdd := client.ZAdd("zset", redis.Z{1.001, "one"}) |
| 2628 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2629 |
|
| 2630 |
zScore := client.ZScore("zset", "one") |
| 2631 |
Expect(zScore.Err()).NotTo(HaveOccurred()) |
| 2632 |
Expect(zScore.Val()).To(Equal(float64(1.001))) |
| 2633 |
}) |
| 2634 |
|
| 2635 |
It("should ZUnionStore", func() { |
| 2636 |
zAdd := client.ZAdd("zset1", redis.Z{1, "one"}) |
| 2637 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2638 |
zAdd = client.ZAdd("zset1", redis.Z{2, "two"}) |
| 2639 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2640 |
|
| 2641 |
zAdd = client.ZAdd("zset2", redis.Z{1, "one"}) |
| 2642 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2643 |
zAdd = client.ZAdd("zset2", redis.Z{2, "two"}) |
| 2644 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2645 |
zAdd = client.ZAdd("zset2", redis.Z{3, "three"}) |
| 2646 |
Expect(zAdd.Err()).NotTo(HaveOccurred()) |
| 2647 |
|
| 2648 |
zUnionStore := client.ZUnionStore( |
| 2649 |
"out", redis.ZStore{Weights: []float64{2, 3}}, "zset1", "zset2") |
| 2650 |
Expect(zUnionStore.Err()).NotTo(HaveOccurred()) |
| 2651 |
Expect(zUnionStore.Val()).To(Equal(int64(3))) |
| 2652 |
|
| 2653 |
val, err := client.ZRangeWithScores("out", 0, -1).Result() |
| 2654 |
Expect(err).NotTo(HaveOccurred()) |
| 2655 |
Expect(val).To(Equal([]redis.Z{{5, "one"}, {9, "three"}, {10, "two"}})) |
| 2656 |
}) |
| 2657 |
|
| 2658 |
}) |
| 2659 |
|
| 2660 |
Describe("Geo add and radius search", func() { |
| 2661 |
BeforeEach(func() { |
| 2662 |
geoAdd := client.GeoAdd( |
| 2663 |
"Sicily", |
| 2664 |
&redis.GeoLocation{Longitude: 13.361389, Latitude: 38.115556, Name: "Palermo"}, |
| 2665 |
&redis.GeoLocation{Longitude: 15.087269, Latitude: 37.502669, Name: "Catania"}, |
| 2666 |
) |
| 2667 |
Expect(geoAdd.Err()).NotTo(HaveOccurred()) |
| 2668 |
Expect(geoAdd.Val()).To(Equal(int64(2))) |
| 2669 |
}) |
| 2670 |
|
| 2671 |
It("should not add same geo location", func() { |
| 2672 |
geoAdd := client.GeoAdd( |
| 2673 |
"Sicily", |
| 2674 |
&redis.GeoLocation{Longitude: 13.361389, Latitude: 38.115556, Name: "Palermo"}, |
| 2675 |
) |
| 2676 |
Expect(geoAdd.Err()).NotTo(HaveOccurred()) |
| 2677 |
Expect(geoAdd.Val()).To(Equal(int64(0))) |
| 2678 |
}) |
| 2679 |
|
| 2680 |
It("should search geo radius", func() { |
| 2681 |
res, err := client.GeoRadius("Sicily", 15, 37, &redis.GeoRadiusQuery{ |
| 2682 |
Radius: 200, |
| 2683 |
}).Result() |
| 2684 |
Expect(err).NotTo(HaveOccurred()) |
| 2685 |
Expect(res).To(HaveLen(2)) |
| 2686 |
Expect(res[0].Name).To(Equal("Palermo")) |
| 2687 |
Expect(res[1].Name).To(Equal("Catania")) |
| 2688 |
}) |
| 2689 |
|
| 2690 |
It("should search geo radius with options", func() { |
| 2691 |
res, err := client.GeoRadius("Sicily", 15, 37, &redis.GeoRadiusQuery{ |
| 2692 |
Radius: 200, |
| 2693 |
Unit: "km", |
| 2694 |
WithGeoHash: true, |
| 2695 |
WithCoord: true, |
| 2696 |
WithDist: true, |
| 2697 |
Count: 2, |
| 2698 |
Sort: "ASC", |
| 2699 |
}).Result() |
| 2700 |
Expect(err).NotTo(HaveOccurred()) |
| 2701 |
Expect(res).To(HaveLen(2)) |
| 2702 |
Expect(res[1].Name).To(Equal("Palermo")) |
| 2703 |
Expect(res[1].Dist).To(Equal(190.4424)) |
| 2704 |
Expect(res[1].GeoHash).To(Equal(int64(3479099956230698))) |
| 2705 |
Expect(res[1].Longitude).To(Equal(13.361389338970184)) |
| 2706 |
Expect(res[1].Latitude).To(Equal(38.115556395496299)) |
| 2707 |
Expect(res[0].Name).To(Equal("Catania")) |
| 2708 |
Expect(res[0].Dist).To(Equal(56.4413)) |
| 2709 |
Expect(res[0].GeoHash).To(Equal(int64(3479447370796909))) |
| 2710 |
Expect(res[0].Longitude).To(Equal(15.087267458438873)) |
| 2711 |
Expect(res[0].Latitude).To(Equal(37.50266842333162)) |
| 2712 |
}) |
| 2713 |
|
| 2714 |
It("should search geo radius with WithDist=false", func() { |
| 2715 |
res, err := client.GeoRadius("Sicily", 15, 37, &redis.GeoRadiusQuery{ |
| 2716 |
Radius: 200, |
| 2717 |
Unit: "km", |
| 2718 |
WithGeoHash: true, |
| 2719 |
WithCoord: true, |
| 2720 |
Count: 2, |
| 2721 |
Sort: "ASC", |
| 2722 |
}).Result() |
| 2723 |
Expect(err).NotTo(HaveOccurred()) |
| 2724 |
Expect(res).To(HaveLen(2)) |
| 2725 |
Expect(res[1].Name).To(Equal("Palermo")) |
| 2726 |
Expect(res[1].Dist).To(Equal(float64(0))) |
| 2727 |
Expect(res[1].GeoHash).To(Equal(int64(3479099956230698))) |
| 2728 |
Expect(res[1].Longitude).To(Equal(13.361389338970184)) |
| 2729 |
Expect(res[1].Latitude).To(Equal(38.115556395496299)) |
| 2730 |
Expect(res[0].Name).To(Equal("Catania")) |
| 2731 |
Expect(res[0].Dist).To(Equal(float64(0))) |
| 2732 |
Expect(res[0].GeoHash).To(Equal(int64(3479447370796909))) |
| 2733 |
Expect(res[0].Longitude).To(Equal(15.087267458438873)) |
| 2734 |
Expect(res[0].Latitude).To(Equal(37.50266842333162)) |
| 2735 |
}) |
| 2736 |
|
| 2737 |
It("should search geo radius by member with options", func() { |
| 2738 |
res, err := client.GeoRadiusByMember("Sicily", "Catania", &redis.GeoRadiusQuery{ |
| 2739 |
Radius: 200, |
| 2740 |
Unit: "km", |
| 2741 |
WithGeoHash: true, |
| 2742 |
WithCoord: true, |
| 2743 |
WithDist: true, |
| 2744 |
Count: 2, |
| 2745 |
Sort: "ASC", |
| 2746 |
}).Result() |
| 2747 |
Expect(err).NotTo(HaveOccurred()) |
| 2748 |
Expect(res).To(HaveLen(2)) |
| 2749 |
Expect(res[0].Name).To(Equal("Catania")) |
| 2750 |
Expect(res[0].Dist).To(Equal(0.0)) |
| 2751 |
Expect(res[0].GeoHash).To(Equal(int64(3479447370796909))) |
| 2752 |
Expect(res[0].Longitude).To(Equal(15.087267458438873)) |
| 2753 |
Expect(res[0].Latitude).To(Equal(37.50266842333162)) |
| 2754 |
Expect(res[1].Name).To(Equal("Palermo")) |
| 2755 |
Expect(res[1].Dist).To(Equal(166.2742)) |
| 2756 |
Expect(res[1].GeoHash).To(Equal(int64(3479099956230698))) |
| 2757 |
Expect(res[1].Longitude).To(Equal(13.361389338970184)) |
| 2758 |
Expect(res[1].Latitude).To(Equal(38.115556395496299)) |
| 2759 |
}) |
| 2760 |
|
| 2761 |
It("should search geo radius with no results", func() { |
| 2762 |
res, err := client.GeoRadius("Sicily", 99, 37, &redis.GeoRadiusQuery{ |
| 2763 |
Radius: 200, |
| 2764 |
Unit: "km", |
| 2765 |
WithGeoHash: true, |
| 2766 |
WithCoord: true, |
| 2767 |
WithDist: true, |
| 2768 |
}).Result() |
| 2769 |
Expect(err).NotTo(HaveOccurred()) |
| 2770 |
Expect(res).To(HaveLen(0)) |
| 2771 |
}) |
| 2772 |
|
| 2773 |
It("should get geo distance with unit options", func() { |
| 2774 |
// From Redis CLI, note the difference in rounding in m vs |
| 2775 |
// km on Redis itself. |
| 2776 |
// |
| 2777 |
// GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania" |
| 2778 |
// GEODIST Sicily Palermo Catania m |
| 2779 |
// "166274.15156960033" |
| 2780 |
// GEODIST Sicily Palermo Catania km |
| 2781 |
// "166.27415156960032" |
| 2782 |
dist, err := client.GeoDist("Sicily", "Palermo", "Catania", "km").Result() |
| 2783 |
Expect(err).NotTo(HaveOccurred()) |
| 2784 |
Expect(dist).To(BeNumerically("~", 166.27, 0.01)) |
| 2785 |
|
| 2786 |
dist, err = client.GeoDist("Sicily", "Palermo", "Catania", "m").Result() |
| 2787 |
Expect(err).NotTo(HaveOccurred()) |
| 2788 |
Expect(dist).To(BeNumerically("~", 166274.15, 0.01)) |
| 2789 |
}) |
| 2790 |
|
| 2791 |
It("should get geo hash in string representation", func() { |
| 2792 |
hashes, err := client.GeoHash("Sicily", "Palermo", "Catania").Result() |
| 2793 |
Expect(err).NotTo(HaveOccurred()) |
| 2794 |
Expect(hashes).To(ConsistOf([]string{"sqc8b49rny0", "sqdtr74hyu0"})) |
| 2795 |
}) |
| 2796 |
|
| 2797 |
It("should return geo position", func() { |
| 2798 |
pos, err := client.GeoPos("Sicily", "Palermo", "Catania", "NonExisting").Result() |
| 2799 |
Expect(err).NotTo(HaveOccurred()) |
| 2800 |
Expect(pos).To(ConsistOf([]*redis.GeoPos{ |
| 2801 |
{ |
| 2802 |
Longitude: 13.361389338970184, |
| 2803 |
Latitude: 38.1155563954963, |
| 2804 |
}, |
| 2805 |
{ |
| 2806 |
Longitude: 15.087267458438873, |
| 2807 |
Latitude: 37.50266842333162, |
| 2808 |
}, |
| 2809 |
nil, |
| 2810 |
})) |
| 2811 |
}) |
| 2812 |
}) |
| 2813 |
|
| 2814 |
Describe("marshaling/unmarshaling", func() { |
| 2815 |
|
| 2816 |
type convTest struct { |
| 2817 |
value interface{} |
| 2818 |
wanted string |
| 2819 |
dest interface{} |
| 2820 |
} |
| 2821 |
|
| 2822 |
convTests := []convTest{ |
| 2823 |
{nil, "", nil}, |
| 2824 |
{"hello", "hello", new(string)}, |
| 2825 |
{[]byte("hello"), "hello", new([]byte)}, |
| 2826 |
{int(1), "1", new(int)}, |
| 2827 |
{int8(1), "1", new(int8)}, |
| 2828 |
{int16(1), "1", new(int16)}, |
| 2829 |
{int32(1), "1", new(int32)}, |
| 2830 |
{int64(1), "1", new(int64)}, |
| 2831 |
{uint(1), "1", new(uint)}, |
| 2832 |
{uint8(1), "1", new(uint8)}, |
| 2833 |
{uint16(1), "1", new(uint16)}, |
| 2834 |
{uint32(1), "1", new(uint32)}, |
| 2835 |
{uint64(1), "1", new(uint64)}, |
| 2836 |
{float32(1.0), "1", new(float32)}, |
| 2837 |
{float64(1.0), "1", new(float64)}, |
| 2838 |
{true, "1", new(bool)}, |
| 2839 |
{false, "0", new(bool)}, |
| 2840 |
} |
| 2841 |
|
| 2842 |
It("should convert to string", func() { |
| 2843 |
for _, test := range convTests { |
| 2844 |
err := client.Set("key", test.value, 0).Err() |
| 2845 |
Expect(err).NotTo(HaveOccurred()) |
| 2846 |
|
| 2847 |
s, err := client.Get("key").Result() |
| 2848 |
Expect(err).NotTo(HaveOccurred()) |
| 2849 |
Expect(s).To(Equal(test.wanted)) |
| 2850 |
|
| 2851 |
if test.dest == nil { |
| 2852 |
continue |
| 2853 |
} |
| 2854 |
|
| 2855 |
err = client.Get("key").Scan(test.dest) |
| 2856 |
Expect(err).NotTo(HaveOccurred()) |
| 2857 |
Expect(deref(test.dest)).To(Equal(test.value)) |
| 2858 |
} |
| 2859 |
}) |
| 2860 |
|
| 2861 |
}) |
| 2862 |
|
| 2863 |
Describe("json marshaling/unmarshaling", func() { |
| 2864 |
|
| 2865 |
BeforeEach(func() { |
| 2866 |
value := &numberStruct{Number: 42} |
| 2867 |
err := client.Set("key", value, 0).Err() |
| 2868 |
Expect(err).NotTo(HaveOccurred()) |
| 2869 |
}) |
| 2870 |
|
| 2871 |
It("should marshal custom values using json", func() { |
| 2872 |
s, err := client.Get("key").Result() |
| 2873 |
Expect(err).NotTo(HaveOccurred()) |
| 2874 |
Expect(s).To(Equal(`{"Number":42}`)) |
| 2875 |
}) |
| 2876 |
|
| 2877 |
It("should scan custom values using json", func() { |
| 2878 |
value := &numberStruct{} |
| 2879 |
err := client.Get("key").Scan(value) |
| 2880 |
Expect(err).NotTo(HaveOccurred()) |
| 2881 |
Expect(value.Number).To(Equal(42)) |
| 2882 |
}) |
| 2883 |
|
| 2884 |
}) |
| 2885 |
|
| 2886 |
Describe("Command", func() { |
| 2887 |
|
| 2888 |
It("returns map of commands", func() { |
| 2889 |
cmds, err := client.Command().Result() |
| 2890 |
Expect(err).NotTo(HaveOccurred()) |
| 2891 |
Expect(len(cmds)).To(BeNumerically("~", 173, 5)) |
| 2892 |
|
| 2893 |
cmd := cmds["mget"] |
| 2894 |
Expect(cmd.Name).To(Equal("mget")) |
| 2895 |
Expect(cmd.Arity).To(Equal(int8(-2))) |
| 2896 |
Expect(cmd.Flags).To(Equal([]string{"readonly"})) |
| 2897 |
Expect(cmd.FirstKeyPos).To(Equal(int8(1))) |
| 2898 |
Expect(cmd.LastKeyPos).To(Equal(int8(-1))) |
| 2899 |
Expect(cmd.StepCount).To(Equal(int8(1))) |
| 2900 |
}) |
| 2901 |
|
| 2902 |
}) |
| 2903 |
|
| 2904 |
Describe("Eval", func() { |
| 2905 |
|
| 2906 |
It("returns keys and values", func() { |
| 2907 |
vals, err := client.Eval( |
| 2908 |
"return {KEYS[1],ARGV[1]}", |
| 2909 |
[]string{"key"}, |
| 2910 |
"hello", |
| 2911 |
).Result() |
| 2912 |
Expect(err).NotTo(HaveOccurred()) |
| 2913 |
Expect(vals).To(Equal([]interface{}{"key", "hello"})) |
| 2914 |
}) |
| 2915 |
|
| 2916 |
}) |
| 2917 |
|
| 2918 |
}) |
| 2919 |
|
| 2920 |
type numberStruct struct { |
| 2921 |
Number int |
| 2922 |
} |
| 2923 |
|
| 2924 |
func (s *numberStruct) MarshalBinary() ([]byte, error) { |
| 2925 |
return json.Marshal(s) |
| 2926 |
} |
| 2927 |
|
| 2928 |
func (s *numberStruct) UnmarshalBinary(b []byte) error { |
| 2929 |
return json.Unmarshal(b, s) |
| 2930 |
} |
| 2931 |
|
| 2932 |
func deref(viface interface{}) interface{} { |
| 2933 |
v := reflect.ValueOf(viface) |
| 2934 |
for v.Kind() == reflect.Ptr { |
| 2935 |
v = v.Elem() |
| 2936 |
} |
| 2937 |
return v.Interface() |
| 2938 |
} |