|
| 1 | +import pytest |
| 2 | +import time |
| 3 | +from upstash_redis import Redis |
| 4 | + |
| 5 | + |
| 6 | +@pytest.fixture(autouse=True) |
| 7 | +def flush_hash(redis: Redis): |
| 8 | + hash_name = "myhash" |
| 9 | + redis.delete(hash_name) |
| 10 | + |
| 11 | + |
| 12 | +def test_hexpire_expires_hash_key(redis: Redis): |
| 13 | + hash_name = "myhash" |
| 14 | + field = "field1" |
| 15 | + value = "value1" |
| 16 | + |
| 17 | + redis.hset(hash_name, field, value) |
| 18 | + assert redis.hexpire(hash_name, field, 1) == [1] |
| 19 | + |
| 20 | + time.sleep(2) |
| 21 | + assert redis.hget(hash_name, field) is None |
| 22 | + |
| 23 | + |
| 24 | +def test_hexpire_nx_sets_expiry_if_no_expiry(redis: Redis): |
| 25 | + hash_name = "myhash" |
| 26 | + field = "field1" |
| 27 | + value = "value1" |
| 28 | + |
| 29 | + redis.hset(hash_name, field, value) |
| 30 | + assert redis.hexpire(hash_name, field, 1, nx=True) == [1] |
| 31 | + |
| 32 | + time.sleep(2) |
| 33 | + assert redis.hget(hash_name, field) is None |
| 34 | + |
| 35 | + |
| 36 | +def test_hexpire_nx_does_not_set_expiry_if_already_exists(redis: Redis): |
| 37 | + hash_name = "myhash" |
| 38 | + field = "field1" |
| 39 | + value = "value1" |
| 40 | + |
| 41 | + redis.hset(hash_name, field, value) |
| 42 | + redis.hexpire(hash_name, field, 1000) |
| 43 | + assert redis.hexpire(hash_name, field, 1, nx=True) == [0] |
| 44 | + |
| 45 | + |
| 46 | +def test_hexpire_xx_sets_expiry_if_exists(redis: Redis): |
| 47 | + hash_name = "myhash" |
| 48 | + field = "field1" |
| 49 | + value = "value1" |
| 50 | + |
| 51 | + redis.hset(hash_name, field, value) |
| 52 | + redis.hexpire(hash_name, field, 1) |
| 53 | + assert redis.hexpire(hash_name, [field], 5, xx=True) == [1] |
| 54 | + |
| 55 | + time.sleep(6) |
| 56 | + assert redis.hget(hash_name, field) is None |
| 57 | + |
| 58 | + |
| 59 | +def test_hexpire_xx_does_not_set_expiry_if_not_exists(redis: Redis): |
| 60 | + hash_name = "myhash" |
| 61 | + field = "field1" |
| 62 | + value = "value1" |
| 63 | + |
| 64 | + redis.hset(hash_name, field, value) |
| 65 | + assert redis.hexpire(hash_name, field, 5, xx=True) == [0] |
| 66 | + |
| 67 | + |
| 68 | +def test_hexpire_gt_sets_expiry_if_new_greater(redis: Redis): |
| 69 | + hash_name = "myhash" |
| 70 | + field = "field1" |
| 71 | + value = "value1" |
| 72 | + |
| 73 | + redis.hset(hash_name, field, value) |
| 74 | + redis.hexpire(hash_name, field, 1) |
| 75 | + assert redis.hexpire(hash_name, field, 5, gt=True) == [1] |
| 76 | + |
| 77 | + time.sleep(6) |
| 78 | + assert redis.hget(hash_name, field) is None |
| 79 | + |
| 80 | + |
| 81 | +def test_hexpire_gt_does_not_set_if_new_not_greater(redis: Redis): |
| 82 | + hash_name = "myhash" |
| 83 | + field = "field1" |
| 84 | + value = "value1" |
| 85 | + |
| 86 | + redis.hset(hash_name, field, value) |
| 87 | + redis.hexpire(hash_name, field, 10) |
| 88 | + assert redis.hexpire(hash_name, [field], 5, gt=True) == [0] |
| 89 | + |
| 90 | + |
| 91 | +def test_hexpire_lt_sets_expiry_if_new_less(redis: Redis): |
| 92 | + hash_name = "myhash" |
| 93 | + field = "field1" |
| 94 | + value = "value1" |
| 95 | + |
| 96 | + redis.hset(hash_name, field, value) |
| 97 | + redis.hexpire(hash_name, [field], 5) |
| 98 | + assert redis.hexpire(hash_name, field, 3, lt=True) == [1] |
| 99 | + |
| 100 | + time.sleep(4) |
| 101 | + assert redis.hget(hash_name, field) is None |
| 102 | + |
| 103 | + |
| 104 | +def test_hexpire_lt_does_not_set_if_new_not_less(redis: Redis): |
| 105 | + hash_name = "myhash" |
| 106 | + field = "field1" |
| 107 | + value = "value1" |
| 108 | + |
| 109 | + redis.hset(hash_name, field, value) |
| 110 | + redis.hexpire(hash_name, field, 10) |
| 111 | + assert redis.hexpire(hash_name, [field], 20, lt=True) == [0] |
| 112 | + |
| 113 | + |
| 114 | +def test_hexpire_returns_minus2_if_field_does_not_exist(redis: Redis): |
| 115 | + hash_name = "myhash" |
| 116 | + field = "field1" |
| 117 | + field2 = "field2" |
| 118 | + redis.hset(hash_name, field, "10") |
| 119 | + assert redis.hexpire(hash_name, field2, 1) == [-2] |
| 120 | + |
| 121 | + |
| 122 | +def test_hexpire_returns_minus2_if_hash_does_not_exist(redis: Redis): |
| 123 | + assert redis.hexpire("nonexistent_hash", "field1", 1) == [-2] |
| 124 | + |
| 125 | + |
| 126 | +def test_hexpire_returns_2_when_called_with_zero_seconds(redis: Redis): |
| 127 | + hash_name = "myhash" |
| 128 | + field = "field1" |
| 129 | + value = "value1" |
| 130 | + |
| 131 | + redis.hset(hash_name, field, value) |
| 132 | + assert redis.hexpire(hash_name, field, 0) == [2] |
| 133 | + assert redis.hget(hash_name, field) is None |
0 commit comments