1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
|
import threading
from time import sleep
import pytest
import redis
import redis.client
from .. import testtools
def _push_thread(r: redis.Redis) -> threading.Thread:
def run():
sleep(0.5)
r.rpush("foo", "value1")
sleep(0.5)
# Will wake the condition variable
r.set("bar", "go back to sleep some more")
r.rpush("foo", "value2")
thread = threading.Thread(target=run)
thread.start()
return thread
def test_lpush_then_lrange_all(r: redis.Redis):
assert r.lpush("foo", "bar") == 1
assert r.lpush("foo", "baz") == 2
assert r.lpush("foo", "bam", "buzz") == 4
assert r.lrange("foo", 0, -1) == [b"buzz", b"bam", b"baz", b"bar"]
def test_lpush_then_lrange_portion(r: redis.Redis):
r.lpush("foo", "one")
r.lpush("foo", "two")
r.lpush("foo", "three")
r.lpush("foo", "four")
assert r.lrange("foo", 0, 2) == [b"four", b"three", b"two"]
assert r.lrange("foo", 0, 3) == [b"four", b"three", b"two", b"one"]
def test_lrange_negative_indices(r: redis.Redis):
r.rpush("foo", "a", "b", "c")
assert r.lrange("foo", -1, -2) == []
assert r.lrange("foo", -2, -1) == [b"b", b"c"]
def test_lpush_key_does_not_exist(r: redis.Redis):
assert r.lrange("foo", 0, -1) == []
def test_lpush_with_nonstr_key(r: redis.Redis):
r.lpush(1, "one")
r.lpush(1, "two")
r.lpush(1, "three")
assert r.lrange(1, 0, 2) == [b"three", b"two", b"one"]
assert r.lrange("1", 0, 2) == [b"three", b"two", b"one"]
def test_lpush_wrong_type(r: redis.Redis):
r.set("foo", "bar")
with pytest.raises(redis.ResponseError):
r.lpush("foo", "element")
def test_llen(r: redis.Redis):
r.lpush("foo", "one")
r.lpush("foo", "two")
r.lpush("foo", "three")
assert r.llen("foo") == 3
def test_llen_no_exist(r: redis.Redis):
assert r.llen("foo") == 0
def test_llen_wrong_type(r: redis.Redis):
r.set("foo", "bar")
with pytest.raises(redis.ResponseError):
r.llen("foo")
def test_lrem_positive_count(r: redis.Redis):
r.lpush("foo", "same")
r.lpush("foo", "same")
r.lpush("foo", "different")
r.lrem("foo", 2, "same")
assert r.lrange("foo", 0, -1) == [b"different"]
def test_lrem_negative_count(r: redis.Redis):
r.lpush("foo", "removeme")
r.lpush("foo", "three")
r.lpush("foo", "two")
r.lpush("foo", "one")
r.lpush("foo", "removeme")
r.lrem("foo", -1, "removeme")
# Should remove it from the end of the list,
# leaving the 'removeme' from the front of the list alone.
assert r.lrange("foo", 0, -1) == [b"removeme", b"one", b"two", b"three"]
def test_lrem_zero_count(r: redis.Redis):
r.lpush("foo", "one")
r.lpush("foo", "one")
r.lpush("foo", "one")
r.lrem("foo", 0, "one")
assert r.lrange("foo", 0, -1) == []
def test_lrem_default_value(r: redis.Redis):
r.lpush("foo", "one")
r.lpush("foo", "one")
r.lpush("foo", "one")
r.lrem("foo", 0, "one")
assert r.lrange("foo", 0, -1) == []
def test_lrem_does_not_exist(r: redis.Redis):
r.lpush("foo", "one")
r.lrem("foo", 0, "one")
# These should be noops.
r.lrem("foo", -2, "one")
r.lrem("foo", 2, "one")
def test_lrem_return_value(r: redis.Redis):
r.lpush("foo", "one")
count = r.lrem("foo", 0, "one")
assert count == 1
assert r.lrem("foo", 0, "one") == 0
def test_lrem_wrong_type(r: redis.Redis):
r.set("foo", "bar")
with pytest.raises(redis.ResponseError):
r.lrem("foo", 0, "element")
def test_rpush(r: redis.Redis):
r.rpush("foo", "one")
r.rpush("foo", "two")
r.rpush("foo", "three")
r.rpush("foo", "four", "five")
assert r.lrange("foo", 0, -1) == [b"one", b"two", b"three", b"four", b"five"]
def test_rpush_wrong_type(r: redis.Redis):
r.set("foo", "bar")
with pytest.raises(redis.ResponseError):
r.rpush("foo", "element")
def test_lpop(r: redis.Redis):
assert r.rpush("foo", "one") == 1
assert r.rpush("foo", "two") == 2
assert r.rpush("foo", "three") == 3
assert r.lpop("foo") == b"one"
assert r.lpop("foo") == b"two"
assert r.lpop("foo") == b"three"
def test_lpop_empty_list(r: redis.Redis):
r.rpush("foo", "one")
r.lpop("foo")
assert r.lpop("foo") is None
# Verify what happens if we try to pop from a key
# we've never seen before.
assert r.lpop("noexists") is None
def test_lpop_zero_elem(r: redis.Redis):
r.rpush(b"\x00", b"")
assert r.lpop(b"\x00", 0) == []
def test_lpop_zero_non_existing_list(r: redis.Redis):
assert r.lpop(b"", 0) is None
def test_lpop_zero_wrong_type(r: redis.Redis):
r.set(b"", b"")
with pytest.raises(redis.ResponseError):
r.lpop(b"", 0)
def test_lpop_wrong_type(r: redis.Redis):
r.set("foo", "bar")
with pytest.raises(redis.ResponseError):
r.lpop("foo")
@pytest.mark.min_server("6.2")
def test_lpop_count(r: redis.Redis):
assert r.rpush("foo", "one") == 1
assert r.rpush("foo", "two") == 2
assert r.rpush("foo", "three") == 3
assert testtools.raw_command(r, "lpop", "foo", 2) == [b"one", b"two"]
# See https://github.com/redis/redis/issues/9680
raw = testtools.raw_command(r, "rpop", "foo", 0)
assert raw is None or raw == [] # https://github.com/redis/redis/pull/10095
@pytest.mark.min_server("6.2")
def test_lpop_count_negative(r: redis.Redis):
with pytest.raises(redis.ResponseError):
testtools.raw_command(r, "lpop", "foo", -1)
def test_lset(r: redis.Redis):
r.rpush("foo", "one")
r.rpush("foo", "two")
r.rpush("foo", "three")
r.lset("foo", 0, "four")
r.lset("foo", -2, "five")
assert r.lrange("foo", 0, -1) == [b"four", b"five", b"three"]
def test_lset_index_out_of_range(r: redis.Redis):
r.rpush("foo", "one")
with pytest.raises(redis.ResponseError):
r.lset("foo", 3, "three")
def test_lset_wrong_type(r: redis.Redis):
r.set("foo", "bar")
with pytest.raises(redis.ResponseError):
r.lset("foo", 0, "element")
def test_rpushx(r: redis.Redis):
r.rpush("foo", "one")
r.rpushx("foo", "two")
r.rpushx("bar", "three")
assert r.lrange("foo", 0, -1) == [b"one", b"two"]
assert r.lrange("bar", 0, -1) == []
def test_rpushx_wrong_type(r: redis.Redis):
r.set("foo", "bar")
with pytest.raises(redis.ResponseError):
r.rpushx("foo", "element")
def test_ltrim(r: redis.Redis):
r.rpush("foo", "one")
r.rpush("foo", "two")
r.rpush("foo", "three")
r.rpush("foo", "four")
assert r.ltrim("foo", 1, 3)
assert r.lrange("foo", 0, -1) == [b"two", b"three", b"four"]
assert r.ltrim("foo", 1, -1)
assert r.lrange("foo", 0, -1) == [b"three", b"four"]
def test_ltrim_with_non_existent_key(r: redis.Redis):
assert r.ltrim("foo", 0, -1)
def test_ltrim_expiry(r: redis.Redis):
r.rpush("foo", "one", "two", "three")
r.expire("foo", 10)
r.ltrim("foo", 1, 2)
assert r.ttl("foo") > 0
def test_ltrim_wrong_type(r: redis.Redis):
r.set("foo", "bar")
with pytest.raises(redis.ResponseError):
r.ltrim("foo", 1, -1)
def test_lindex(r: redis.Redis):
r.rpush("foo", "one")
r.rpush("foo", "two")
assert r.lindex("foo", 0) == b"one"
assert r.lindex("foo", 4) is None
assert r.lindex("bar", 4) is None
def test_lindex_wrong_type(r: redis.Redis):
r.set("foo", "bar")
with pytest.raises(redis.ResponseError):
r.lindex("foo", 0)
def test_lpushx(r: redis.Redis):
r.lpush("foo", "two")
r.lpushx("foo", "one")
r.lpushx("bar", "one")
assert r.lrange("foo", 0, -1) == [b"one", b"two"]
assert r.lrange("bar", 0, -1) == []
def test_lpushx_wrong_type(r: redis.Redis):
r.set("foo", "bar")
with pytest.raises(redis.ResponseError):
r.lpushx("foo", "element")
def test_rpop(r: redis.Redis):
assert r.rpop("foo") is None
r.rpush("foo", "one")
r.rpush("foo", "two")
assert r.rpop("foo") == b"two"
assert r.rpop("foo") == b"one"
assert r.rpop("foo") is None
def test_rpop_wrong_type(r: redis.Redis):
r.set("foo", "bar")
with pytest.raises(redis.ResponseError):
r.rpop("foo")
@pytest.mark.min_server("6.2")
def test_rpop_count(r: redis.Redis):
assert r.rpush("foo", "one") == 1
assert r.rpush("foo", "two") == 2
assert r.rpush("foo", "three") == 3
assert testtools.raw_command(r, "rpop", "foo", 2) == [b"three", b"two"]
# See https://github.com/redis/redis/issues/9680
raw = testtools.raw_command(r, "rpop", "foo", 0)
assert raw is None or raw == [] # https://github.com/redis/redis/pull/10095
@pytest.mark.min_server("6.2")
def test_rpop_count_negative(r: redis.Redis):
with pytest.raises(redis.ResponseError):
testtools.raw_command(r, "rpop", "foo", -1)
def test_linsert_before(r: redis.Redis):
r.rpush("foo", "hello")
r.rpush("foo", "world")
assert r.linsert("foo", "before", "world", "there") == 3
assert r.lrange("foo", 0, -1) == [b"hello", b"there", b"world"]
assert r.linsert("empty_list", "before", "world", "there") == 0
def test_linsert_after(r: redis.Redis):
r.rpush("foo", "hello")
r.rpush("foo", "world")
assert r.linsert("foo", "after", "hello", "there") == 3
assert r.lrange("foo", 0, -1) == [b"hello", b"there", b"world"]
def test_linsert_bad_command(r: redis.Redis):
with pytest.raises(redis.ResponseError):
testtools.raw_command(r, "LINSERT", "x", "NOT_BEFORE", "pivot", "val")
def test_linsert_no_pivot(r: redis.Redis):
r.rpush("foo", "hello")
r.rpush("foo", "world")
assert r.linsert("foo", "after", "goodbye", "bar") == -1
assert r.lrange("foo", 0, -1) == [b"hello", b"world"]
def test_linsert_wrong_type(r: redis.Redis):
r.set("foo", "bar")
with pytest.raises(redis.ResponseError):
r.linsert("foo", "after", "bar", "element")
def test_rpoplpush(r: redis.Redis):
assert r.rpoplpush("foo", "bar") is None
assert r.lpop("bar") is None
r.rpush("foo", "one")
r.rpush("foo", "two")
r.rpush("bar", "one")
assert r.rpoplpush("foo", "bar") == b"two"
assert r.lrange("foo", 0, -1) == [b"one"]
assert r.lrange("bar", 0, -1) == [b"two", b"one"]
# Catch instances where we store bytes and strings inconsistently
# and thus bar = ['two', b'one']
assert r.lrem("bar", -1, "two") == 1
def test_rpoplpush_to_nonexistent_destination(r: redis.Redis):
r.rpush("foo", "one")
assert r.rpoplpush("foo", "bar") == b"one"
assert r.rpop("bar") == b"one"
def test_rpoplpush_expiry(r: redis.Redis):
r.rpush("foo", "one")
r.rpush("bar", "two")
r.expire("bar", 10)
r.rpoplpush("foo", "bar")
assert r.ttl("bar") > 0
def test_rpoplpush_one_to_self(r: redis.Redis):
r.rpush("list", "element")
assert r.brpoplpush("list", "list") == b"element"
assert r.lrange("list", 0, -1) == [b"element"]
def test_rpoplpush_wrong_type(r: redis.Redis):
r.set("foo", "bar")
r.rpush("list", "element")
with pytest.raises(redis.ResponseError):
r.rpoplpush("foo", "list")
assert r.get("foo") == b"bar"
assert r.lrange("list", 0, -1) == [b"element"]
with pytest.raises(redis.ResponseError):
r.rpoplpush("list", "foo")
assert r.get("foo") == b"bar"
assert r.lrange("list", 0, -1) == [b"element"]
def test_blpop_single_list(r: redis.Redis):
r.rpush("foo", "one")
r.rpush("foo", "two")
r.rpush("foo", "three")
assert r.blpop(["foo"], timeout=1) == (b"foo", b"one")
def test_blpop_test_multiple_lists(r: redis.Redis):
r.rpush("baz", "zero")
assert r.blpop(["foo", "baz"], timeout=1) == (b"baz", b"zero")
assert not r.exists("baz")
r.rpush("foo", "one")
r.rpush("foo", "two")
# bar has nothing, so the returned value should come
# from foo.
assert r.blpop(["bar", "foo"], timeout=1) == (b"foo", b"one")
r.rpush("bar", "three")
# bar now has something, so the returned value should come
# from bar.
assert r.blpop(["bar", "foo"], timeout=1) == (b"bar", b"three")
assert r.blpop(["bar", "foo"], timeout=1) == (b"foo", b"two")
def test_blpop_allow_single_key(r: redis.Redis):
# blpop converts single key arguments to a one element list.
r.rpush("foo", "one")
assert r.blpop("foo", timeout=1) == (b"foo", b"one")
@pytest.mark.slow
def test_blpop_block(r: redis.Redis):
thread = _push_thread(r)
try:
assert r.blpop("foo") == (b"foo", b"value1")
assert r.blpop("foo", timeout=5) == (b"foo", b"value2")
finally:
thread.join()
@pytest.mark.slow
def test_blpop_block_float(r: redis.Redis):
thread = _push_thread(r)
try:
assert testtools.raw_command(r, "blpop", "foo", 0) == [b"foo", b"value1"]
assert testtools.raw_command(r, "blpop", "foo", 1.1) == [b"foo", b"value2"]
finally:
thread.join()
@pytest.mark.slow
def test_brpop_block(r: redis.Redis):
thread = _push_thread(r)
try:
assert r.brpop("foo") == (b"foo", b"value1")
assert r.brpop("foo", timeout=5) == (b"foo", b"value2")
finally:
thread.join()
def test_blpop_wrong_type(r: redis.Redis):
r.set("foo", "bar")
with pytest.raises(redis.ResponseError):
r.blpop("foo", timeout=1)
def test_blpop_transaction(r: redis.Redis):
p = r.pipeline()
p.multi()
p.blpop("missing", timeout=1000)
result = p.execute()
# Blocking commands behave like non-blocking versions in transactions
assert result == [None]
def test_brpop_test_multiple_lists(r: redis.Redis):
r.rpush("baz", "zero")
assert r.brpop(["foo", "baz"], timeout=1) == (b"baz", b"zero")
assert not r.exists("baz")
r.rpush("foo", "one")
r.rpush("foo", "two")
assert r.brpop(["bar", "foo"], timeout=1) == (b"foo", b"two")
def test_brpop_single_key(r: redis.Redis):
r.rpush("foo", "one")
r.rpush("foo", "two")
assert r.brpop("foo", timeout=1) == (b"foo", b"two")
def test_brpop_wrong_type(r: redis.Redis):
r.set("foo", "bar")
with pytest.raises(redis.ResponseError):
r.brpop("foo", timeout=1)
def test_brpoplpush_multi_keys(r: redis.Redis):
assert r.lpop("bar") is None
r.rpush("foo", "one")
r.rpush("foo", "two")
assert r.brpoplpush("foo", "bar", timeout=1) == b"two"
assert r.lrange("bar", 0, -1) == [b"two"]
# Catch instances where we store bytes and strings inconsistently
# and thus bar = ['two']
assert r.lrem("bar", -1, "two") == 1
@pytest.mark.unsupported_server_types("dragonfly") # TODO Should this be supported?
def test_brpoplpush_wrong_type(r: redis.Redis):
r.set("foo", "bar")
r.rpush("list", "element")
with pytest.raises(redis.ResponseError):
r.brpoplpush("foo", "list")
assert r.get("foo") == b"bar"
assert r.lrange("list", 0, -1) == [b"element"]
with pytest.raises(redis.ResponseError):
r.brpoplpush("list", "foo")
assert r.get("foo") == b"bar"
assert r.lrange("list", 0, -1) == [b"element"]
@pytest.mark.slow
def test_blocking_operations_when_empty(r: redis.Redis):
assert r.blpop(["foo"], timeout=1) is None
assert r.blpop(["bar", "foo"], timeout=1) is None
assert r.brpop("foo", timeout=1) is None
assert r.brpoplpush("foo", "bar", timeout=1) is None
def test_empty_list(r: redis.Redis):
r.rpush("foo", "bar")
r.rpop("foo")
assert not r.exists("foo")
def test_lmove_to_nonexistent_destination(r: redis.Redis):
r.rpush("foo", "one")
assert r.lmove("foo", "bar", "RIGHT", "LEFT") == b"one"
assert r.rpop("bar") == b"one"
def test_lmove_expiry(r: redis.Redis):
r.rpush("foo", "one")
r.rpush("bar", "two")
r.expire("bar", 10)
r.lmove("foo", "bar", "RIGHT", "LEFT")
assert r.ttl("bar") > 0
def test_lmove_wrong_type(r: redis.Redis):
r.rpush("foo", "one")
r.rpush("bar", "two")
with pytest.raises(redis.ResponseError):
testtools.raw_command(r, "LMOVE", "foo", "bar", "left", "NOT_LEFT_OR_RIGHT")
r.set("foo", "bar")
r.rpush("list", "element")
with pytest.raises(redis.ResponseError):
r.lmove("foo", "list", "RIGHT", "LEFT")
assert r.get("foo") == b"bar"
assert r.lrange("list", 0, -1) == [b"element"]
with pytest.raises(redis.ResponseError):
r.lmove("list", "foo", "RIGHT", "LEFT")
assert r.get("foo") == b"bar"
assert r.lrange("list", 0, -1) == [b"element"]
def test_lmove(r: redis.Redis):
assert r.lmove("foo", "bar", "RIGHT", "LEFT") is None
assert r.lpop("bar") is None
r.rpush("foo", "one")
r.rpush("foo", "two")
r.rpush("bar", "one")
# RPOPLPUSH
assert r.lmove("foo", "bar", "RIGHT", "LEFT") == b"two"
assert r.lrange("foo", 0, -1) == [b"one"]
assert r.lrange("bar", 0, -1) == [b"two", b"one"]
# LPOPRPUSH
assert r.lmove("bar", "bar", "LEFT", "RIGHT") == b"two"
assert r.lrange("bar", 0, -1) == [b"one", b"two"]
# RPOPRPUSH
r.rpush("foo", "three")
assert r.lmove("foo", "bar", "RIGHT", "RIGHT") == b"three"
assert r.lrange("foo", 0, -1) == [b"one"]
assert r.lrange("bar", 0, -1) == [b"one", b"two", b"three"]
# LPOPLPUSH
assert r.lmove("bar", "foo", "LEFT", "LEFT") == b"one"
assert r.lrange("foo", 0, -1) == [b"one", b"one"]
assert r.lrange("bar", 0, -1) == [b"two", b"three"]
# Catch instances where we store bytes and strings inconsistently
# and thus bar = ['two', b'one']
assert r.lrem("bar", -1, "two") == 1
def test_blmove(r: redis.Redis):
r.rpush("a", "one", "two", "three", "four")
assert r.blmove("a", "b", 5)
assert r.blmove("a", "b", 1, "RIGHT", "LEFT")
@pytest.mark.disconnected
@testtools.fake_only
def test_lmove_disconnected_raises_connection_error(r: redis.Redis):
with pytest.raises(redis.ConnectionError):
r.lmove(1, 2, "LEFT", "RIGHT")
def test_lpos(r: redis.Redis):
assert r.rpush("a", "a", "b", "c", "1", "2", "3", "c", "c") == 8
assert r.lpos("a", "a") == 0
assert r.lpos("a", "c") == 2
assert r.lpos("a", "c", rank=1) == 2
assert r.lpos("a", "c", rank=2) == 6
assert r.lpos("a", "c", rank=4) is None
assert r.lpos("a", "c", rank=-1) == 7
assert r.lpos("a", "c", rank=-2) == 6
assert r.lpos("a", "c", count=0) == [2, 6, 7]
assert r.lpos("a", "c", count=1) == [2]
assert r.lpos("a", "c", count=2) == [2, 6]
assert r.lpos("a", "c", count=100) == [2, 6, 7]
assert r.lpos("a", "c", count=0, rank=2) == [6, 7]
assert r.lpos("a", "c", count=2, rank=-1) == [7, 6]
assert r.lpos("axxx", "c", count=0, rank=2) == []
assert r.lpos("axxx", "c") is None
assert r.lpos("a", "x", count=2) == []
assert r.lpos("a", "x") is None
assert r.lpos("a", "a", count=0, maxlen=1) == [0]
assert r.lpos("a", "c", count=0, maxlen=1) == []
assert r.lpos("a", "c", count=0, maxlen=3) == [2]
assert r.lpos("a", "c", count=0, maxlen=3, rank=-1) == [7, 6]
assert r.lpos("a", "c", count=0, maxlen=7, rank=2) == [6]
@pytest.mark.unsupported_server_types("dragonfly")
@pytest.mark.min_server("7")
def test_blmpop(r: redis.Redis):
r.rpush("a", "1", "2", "3", "4", "5")
res = [b"a", [b"1", b"2"]]
assert r.blmpop(1, "2", "b", "a", direction="LEFT", count=2) == res
with pytest.raises(TypeError):
r.blmpop(1, "2", "b", "a", count=2)
r.rpush("b", "6", "7", "8", "9")
assert r.blmpop(0, "2", "b", "a", direction="LEFT") == [b"b", [b"6"]]
assert r.blmpop(1, "2", "foo", "bar", direction="RIGHT") is None
@pytest.mark.unsupported_server_types("dragonfly")
@pytest.mark.min_server("7")
def test_lmpop(r: redis.Redis):
r.rpush("foo", "1", "2", "3", "4", "5")
result = [b"foo", [b"1", b"2"]]
assert r.lmpop("2", "bar", "foo", direction="LEFT", count=2) == result
with pytest.raises(redis.ResponseError):
r.lmpop("2", "bar", "foo", direction="up", count=2)
r.rpush("bar", "a", "b", "c", "d")
assert r.lmpop("2", "bar", "foo", direction="LEFT") == [b"bar", [b"a"]]
|