Skip to content

Commit 8c1d029

Browse files
CahidArdamdumandag
andauthored
Add hash expiration commands (#62)
* fix: add hash expiration commands * fix: fmt * fix: rm list spread operator * fix: types in commands.pyi * fix: add hpersist tests * fix: update docstrings * Update upstash_redis/commands.py --------- Co-authored-by: Metin Dumandag <29387993+mdumandag@users.noreply.github.com>
1 parent 14432d4 commit 8c1d029

File tree

10 files changed

+642
-1
lines changed

10 files changed

+642
-1
lines changed

tests/commands/hash/test_hexpireat.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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_hexpireat_sets_expiry(redis: Redis):
13+
hash_name = "myhash"
14+
field = "field1"
15+
value = "value1"
16+
17+
redis.hset(hash_name, field, value)
18+
future_timestamp = int(time.time()) + 2
19+
assert redis.hexpireat(hash_name, field, future_timestamp)[0] == 1
20+
21+
time.sleep(3)
22+
assert redis.hexists(hash_name, field) == 0
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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_hexpiretime_returns_correct_expiry(redis: Redis):
13+
hash_name = "myhash"
14+
field = "field1"
15+
value = "value1"
16+
17+
redis.hset(hash_name, field, value)
18+
redis.hexpire(hash_name, field, 5)
19+
expiry_time = redis.hexpiretime(hash_name, [field])[0]
20+
21+
assert expiry_time > int(time.time())
22+
assert expiry_time <= int(time.time()) + 5
23+
24+
25+
def test_hexpiretime_returns_minus1_if_no_expiry(redis: Redis):
26+
hash_name = "myhash"
27+
field = "field1"
28+
value = "value1"
29+
30+
redis.hset(hash_name, field, value)
31+
assert redis.hexpiretime(hash_name, [field])[0] == -1
32+
33+
34+
def test_hexpiretime_returns_minus2_if_field_does_not_exist(redis: Redis):
35+
hash_name = "myhash"
36+
field = "field1"
37+
38+
assert redis.hexpiretime(hash_name, [field])[0] == -2

tests/commands/hash/test_hpersist.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import pytest
2+
from upstash_redis import Redis
3+
4+
5+
@pytest.fixture(autouse=True)
6+
def flush_hash(redis: Redis):
7+
hash_name = "myhash"
8+
redis.delete(hash_name)
9+
10+
11+
def test_hpersist_removes_expiry(redis: Redis):
12+
hash_name = "myhash"
13+
field = "field1"
14+
value = "value1"
15+
16+
redis.hset(hash_name, field, value)
17+
redis.hexpire(hash_name, field, 500)
18+
ttl_before = redis.httl(hash_name, field)
19+
assert ttl_before[0] > 0
20+
21+
redis.hpersist(hash_name, field)
22+
ttl_after = redis.httl(hash_name, field)
23+
assert ttl_after == [-1]
24+
25+
26+
def test_hpersist_does_nothing_if_no_expiry(redis: Redis):
27+
hash_name = "myhash"
28+
field = "field1"
29+
value = "value1500"
30+
31+
redis.hset(hash_name, field, value)
32+
assert redis.hpersist(hash_name, field) == [-1]

tests/commands/hash/test_hpexpire.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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_hpexpire_sets_expiry_in_milliseconds(redis: Redis):
13+
hash_name = "myhash"
14+
field = "field1"
15+
value = "value1"
16+
17+
redis.hset(hash_name, field, value)
18+
assert redis.hpexpire(hash_name, field, 500) == [1]
19+
20+
time.sleep(1)
21+
assert redis.hget(hash_name, field) is None
22+
23+
24+
def test_hpexpire_does_not_set_expiry_if_field_does_not_exist(redis: Redis):
25+
hash_name = "myhash"
26+
field = "field1"
27+
28+
assert redis.hpexpire(hash_name, field, 500) == [-2]
29+
30+
31+
def test_hpexpire_overwrites_existing_expiry(redis: Redis):
32+
hash_name = "myhash"
33+
field = "field1"
34+
value = "value1"
35+
36+
redis.hset(hash_name, field, value)
37+
redis.hpexpire(hash_name, field, 1000)
38+
assert redis.hpexpire(hash_name, field, 2000) == [1]
39+
40+
time.sleep(2.5)
41+
assert redis.hget(hash_name, field) is None
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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_hpexpireat_sets_expiry_in_milliseconds(redis: Redis):
13+
hash_name = "myhash"
14+
field = "field1"
15+
value = "value1"
16+
17+
redis.hset(hash_name, field, value)
18+
future_timestamp_ms = int(time.time() * 1000) + 500
19+
assert redis.hpexpireat(hash_name, field, future_timestamp_ms) == [1]
20+
21+
time.sleep(1)
22+
assert redis.hget(hash_name, field) is None
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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_hpexpiretime_returns_correct_expiry(redis: Redis):
13+
hash_name = "myhash"
14+
field = "field1"
15+
value = "value1"
16+
17+
redis.hset(hash_name, field, value)
18+
redis.hpexpire(hash_name, field, 500)
19+
expiry_time_ms = redis.hpexpiretime(hash_name, [field])[0]
20+
21+
assert expiry_time_ms > 0
22+
assert expiry_time_ms <= int(time.time() * 1000) + 1000
23+
24+
25+
def test_hpexpiretime_returns_minus1_if_no_expiry(redis: Redis):
26+
hash_name = "myhash"
27+
field = "field1"
28+
value = "value1"
29+
30+
redis.hset(hash_name, field, value)
31+
assert redis.hpexpiretime(hash_name, [field])[0] == -1
32+
33+
34+
def test_hpexpiretime_returns_minus2_if_field_does_not_exist(redis: Redis):
35+
hash_name = "myhash"
36+
field = "field1"
37+
38+
assert redis.hpexpiretime(hash_name, [field])[0] == -2

tests/commands/hash/test_hpttl.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import pytest
2+
from upstash_redis import Redis
3+
4+
5+
@pytest.fixture(autouse=True)
6+
def flush_hash(redis: Redis):
7+
hash_name = "myhash"
8+
redis.delete(hash_name)
9+
10+
11+
def test_hpttl_returns_correct_ttl(redis: Redis):
12+
hash_name = "myhash"
13+
field = "field1"
14+
value = "value1"
15+
16+
redis.hset(hash_name, field, value)
17+
redis.hpexpire(hash_name, field, 1500)
18+
ttl = redis.hpttl(hash_name, [field])[0]
19+
20+
assert ttl > 0
21+
22+
23+
def test_hpttl_returns_minus1_if_no_expiry(redis: Redis):
24+
hash_name = "myhash"
25+
field = "field1"
26+
value = "value1"
27+
28+
redis.hset(hash_name, field, value)
29+
assert redis.hpttl(hash_name, [field])[0] == -1
30+
31+
32+
def test_hpttl_returns_minus2_if_field_does_not_exist(redis: Redis):
33+
hash_name = "myhash"
34+
field = "field1"
35+
36+
assert redis.hpttl(hash_name, [field])[0] == -2

tests/commands/hash/test_httl.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import pytest
2+
from upstash_redis import Redis
3+
4+
5+
@pytest.fixture(autouse=True)
6+
def flush_hash(redis: Redis):
7+
hash_name = "myhash"
8+
redis.delete(hash_name)
9+
10+
11+
def test_httl_returns_correct_ttl(redis: Redis):
12+
hash_name = "myhash"
13+
field = "field1"
14+
value = "value1"
15+
16+
redis.hset(hash_name, field, value)
17+
redis.hexpire(hash_name, field, 5)
18+
ttl = redis.httl(hash_name, [field])[0]
19+
20+
assert ttl > 0
21+
assert ttl <= 5
22+
23+
24+
def test_httl_returns_minus1_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.httl(hash_name, [field])[0] == -1
31+
32+
33+
def test_httl_returns_minus2_if_field_does_not_exist(redis: Redis):
34+
hash_name = "myhash"
35+
field = "field1"
36+
37+
assert redis.httl(hash_name, [field])[0] == -2

0 commit comments

Comments
 (0)