Skip to content

Commit

Permalink
fix:tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cunla committed Sep 22, 2024
1 parent d5b6aa0 commit b0c2f41
Showing 1 changed file with 27 additions and 52 deletions.
79 changes: 27 additions & 52 deletions test/test_mixins/test_hash_expire_commands.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import time
from datetime import datetime, timedelta
from typing import Optional, Dict

import pytest
import redis.client

from fakeredis._helpers import current_time
from test import testtools

pytestmark = []
Expand All @@ -26,7 +26,7 @@
# GT
(100, None, dict(gt=True), 0),
(100, 50, dict(gt=True), 1),
(100, 100, dict(gt=True), 0),
(100, 101, dict(gt=True), 0),
(100, 200, dict(gt=True), 0),
# LT
(100, None, dict(lt=True), 1),
Expand Down Expand Up @@ -60,6 +60,7 @@ def test_hexpire_basic(r: redis.Redis):
assert r.hexists("redis-key", "field2") is True


@pytest.mark.slow
def test_hexpire_conditions(r: redis.Redis):
r.delete("redis-key")
r.hset("redis-key", mapping={"field1": "value1"})
Expand All @@ -84,6 +85,7 @@ def test_hexpire_nonexistent_key_or_field(r: redis.Redis):
assert r.hexpire("redis-key", 1, "nonexistent_field") == [-2]


@pytest.mark.slow
def test_hexpire_multiple_fields(r: redis.Redis):
r.delete("redis-key")
r.hset(
Expand All @@ -101,20 +103,12 @@ def test_hpexpire_basic(r: redis.Redis):
r.delete("redis-key")
r.hset("redis-key", mapping={"field1": "value1", "field2": "value2"})
assert r.hpexpire("redis-key", 100, "field1") == [1]
time.sleep(0.1)
assert r.hexists("redis-key", "field1") is False
assert r.hexists("redis-key", "field2") is True


def test_hpexpire_with_timedelta(r: redis.Redis):
r.delete("redis-key")
r.hset("redis-key", mapping={"field1": "value1", "field2": "value2"})
assert r.hpexpire("redis-key", timedelta(milliseconds=500), "field1") == [1]
time.sleep(0.6)
time.sleep(0.11)
assert r.hexists("redis-key", "field1") is False
assert r.hexists("redis-key", "field2") is True


@pytest.mark.slow
def test_hpexpire_conditions(r: redis.Redis):
r.delete("redis-key")
r.hset("redis-key", mapping={"field1": "value1"})
Expand Down Expand Up @@ -145,8 +139,8 @@ def test_hpexpire_multiple_fields(r: redis.Redis):
"redis-key",
mapping={"field1": "value1", "field2": "value2", "field3": "value3"},
)
assert r.hpexpire("redis-key", 500, "field1", "field2") == [1, 1]
time.sleep(0.6)
assert r.hpexpire("redis-key", 100, "field1", "field2") == [1, 1]
time.sleep(0.11)
assert r.hexists("redis-key", "field1") is False
assert r.hexists("redis-key", "field2") is False
assert r.hexists("redis-key", "field3") is True
Expand All @@ -160,20 +154,11 @@ def test_hpexpire_multiple_condition_flags_error(r: redis.Redis):
assert "Only one of" in str(e)


@pytest.mark.slow
def test_hexpireat_basic(r: redis.Redis):
r.delete("redis-key")
r.hset("redis-key", mapping={"field1": "value1", "field2": "value2"})
exp_time = int((datetime.now() + timedelta(seconds=1)).timestamp())
assert r.hexpireat("redis-key", exp_time, "field1") == [1]
time.sleep(1.1)
assert r.hexists("redis-key", "field1") is False
assert r.hexists("redis-key", "field2") is True


def test_hexpireat_with_datetime(r: redis.Redis):
r.delete("redis-key")
r.hset("redis-key", mapping={"field1": "value1", "field2": "value2"})
exp_time = datetime.now() + timedelta(seconds=1)
exp_time = (current_time() + 1000) // 1000
assert r.hexpireat("redis-key", exp_time, "field1") == [1]
time.sleep(1.1)
assert r.hexists("redis-key", "field1") is False
Expand All @@ -183,8 +168,8 @@ def test_hexpireat_with_datetime(r: redis.Redis):
def test_hexpireat_conditions(r: redis.Redis):
r.delete("redis-key")
r.hset("redis-key", mapping={"field1": "value1"})
future_exp_time = int((datetime.now() + timedelta(seconds=20)).timestamp())
past_exp_time = int((datetime.now() - timedelta(seconds=10)).timestamp())
future_exp_time = current_time() // 1000 + 20
past_exp_time = current_time() // 1000 - 10
assert r.hexpireat("redis-key", future_exp_time, "field1", xx=True) == [0]
assert r.hexpireat("redis-key", future_exp_time, "field1", nx=True) == [1]
assert r.hexpireat("redis-key", past_exp_time, "field1", gt=True) == [0]
Expand All @@ -194,7 +179,7 @@ def test_hexpireat_conditions(r: redis.Redis):

def test_hexpireat_nonexistent_key_or_field(r: redis.Redis):
r.delete("redis-key")
future_exp_time = int((datetime.now() + timedelta(seconds=1)).timestamp())
future_exp_time = (current_time() + 1000) // 1000
assert r.hexpireat("redis-key", future_exp_time, "field1") == [-2]
r.hset("redis-key", "field1", "value1")
assert r.hexpireat("redis-key", future_exp_time, "nonexistent_field") == [-2]
Expand All @@ -206,7 +191,7 @@ def test_hexpireat_multiple_fields(r: redis.Redis):
"redis-key",
mapping={"field1": "value1", "field2": "value2", "field3": "value3"},
)
exp_time = int((datetime.now() + timedelta(seconds=1)).timestamp())
exp_time = (current_time() + 1000) // 1000
assert r.hexpireat("redis-key", exp_time, "field1", "field2") == [1, 1]
time.sleep(1.1)
assert r.hexists("redis-key", "field1") is False
Expand All @@ -217,7 +202,7 @@ def test_hexpireat_multiple_fields(r: redis.Redis):
def test_hexpireat_multiple_condition_flags_error(r: redis.Redis):
r.delete("redis-key")
r.hset("redis-key", mapping={"field1": "value1"})
exp_time = int((datetime.now() + timedelta(seconds=1)).timestamp())
exp_time = (current_time() + 1000) // 1000
with pytest.raises(ValueError) as e:
r.hexpireat("redis-key", exp_time, "field1", nx=True, xx=True)
assert "Only one of" in str(e)
Expand All @@ -226,28 +211,18 @@ def test_hexpireat_multiple_condition_flags_error(r: redis.Redis):
def test_hpexpireat_basic(r: redis.Redis):
r.delete("redis-key")
r.hset("redis-key", mapping={"field1": "value1", "field2": "value2"})
exp_time = int((datetime.now() + timedelta(milliseconds=400)).timestamp() * 1000)
assert r.hpexpireat("redis-key", exp_time, "field1") == [1]
time.sleep(0.5)
assert r.hexists("redis-key", "field1") is False
assert r.hexists("redis-key", "field2") is True


def test_hpexpireat_with_datetime(r: redis.Redis):
r.delete("redis-key")
r.hset("redis-key", mapping={"field1": "value1", "field2": "value2"})
exp_time = datetime.now() + timedelta(milliseconds=400)
exp_time = current_time() + 100
assert r.hpexpireat("redis-key", exp_time, "field1") == [1]
time.sleep(0.5)
time.sleep(0.11)
assert r.hexists("redis-key", "field1") is False
assert r.hexists("redis-key", "field2") is True


def test_hpexpireat_conditions(r: redis.Redis):
r.delete("redis-key")
r.hset("redis-key", mapping={"field1": "value1"})
future_exp_time = int((datetime.now() + timedelta(milliseconds=500)).timestamp() * 1000)
past_exp_time = int((datetime.now() - timedelta(milliseconds=500)).timestamp() * 1000)
future_exp_time = current_time() + 500
past_exp_time = current_time() - 500
assert r.hpexpireat("redis-key", future_exp_time, "field1", xx=True) == [0]
assert r.hpexpireat("redis-key", future_exp_time, "field1", nx=True) == [1]
assert r.hpexpireat("redis-key", past_exp_time, "field1", gt=True) == [0]
Expand All @@ -257,7 +232,7 @@ def test_hpexpireat_conditions(r: redis.Redis):

def test_hpexpireat_nonexistent_key_or_field(r: redis.Redis):
r.delete("redis-key")
future_exp_time = int((datetime.now() + timedelta(milliseconds=500)).timestamp() * 1000)
future_exp_time = current_time() + 500
assert r.hpexpireat("redis-key", future_exp_time, "field1") == [-2]
r.hset("redis-key", "field1", "value1")
assert r.hpexpireat("redis-key", future_exp_time, "nonexistent_field") == [-2]
Expand All @@ -269,9 +244,9 @@ def test_hpexpireat_multiple_fields(r: redis.Redis):
"redis-key",
mapping={"field1": "value1", "field2": "value2", "field3": "value3"},
)
exp_time = int((datetime.now() + timedelta(milliseconds=400)).timestamp() * 1000)
exp_time = current_time() + 100
assert r.hpexpireat("redis-key", exp_time, "field1", "field2") == [1, 1]
time.sleep(0.5)
time.sleep(0.11)
assert r.hexists("redis-key", "field1") is False
assert r.hexists("redis-key", "field2") is False
assert r.hexists("redis-key", "field3") is True
Expand All @@ -280,7 +255,7 @@ def test_hpexpireat_multiple_fields(r: redis.Redis):
def test_hpexpireat_multiple_condition_flags_error(r: redis.Redis):
r.delete("redis-key")
r.hset("redis-key", mapping={"field1": "value1"})
exp_time = int((datetime.now() + timedelta(milliseconds=500)).timestamp())
exp_time = current_time() + 500
with pytest.raises(ValueError) as e:
r.hpexpireat("redis-key", exp_time, "field1", nx=True, xx=True)
assert "Only one of" in str(e)
Expand All @@ -301,7 +276,7 @@ def test_hpersist_nonexistent_key(r: redis.Redis):
def test_hexpiretime_multiple_fields_mixed_conditions(r: redis.Redis):
r.delete("redis-key")
r.hset("redis-key", mapping={"field1": "value1", "field2": "value2"})
future_time = int((datetime.now() + timedelta(minutes=30)).timestamp())
future_time = current_time() // 1000 + 30 * 60
r.hexpireat("redis-key", future_time, "field1")
result = r.hexpiretime("redis-key", "field1", "field2", "field3")
assert future_time - 10 < result[0] <= future_time
Expand All @@ -316,7 +291,7 @@ def test_hexpiretime_nonexistent_key(r: redis.Redis):
def test_hpexpiretime_multiple_fields_mixed_conditions(r: redis.Redis):
r.delete("redis-key")
r.hset("redis-key", mapping={"field1": "value1", "field2": "value2"})
future_time = int((datetime.now() + timedelta(minutes=30)).timestamp())
future_time = current_time() // 1000 + 30 * 60
r.hexpireat("redis-key", future_time, "field1")
result = r.hpexpiretime("redis-key", "field1", "field2", "field3")
assert future_time * 1000 - 10000 < result[0] <= future_time * 1000
Expand All @@ -331,7 +306,7 @@ def test_hpexpiretime_nonexistent_key(r: redis.Redis):
def test_httl_multiple_fields_mixed_conditions(r: redis.Redis):
r.delete("redis-key")
r.hset("redis-key", mapping={"field1": "value1", "field2": "value2"})
future_time = int((datetime.now() + timedelta(minutes=30)).timestamp())
future_time = current_time() // 1000 + 30 * 60
r.hexpireat("redis-key", future_time, "field1")
result = r.httl("redis-key", "field1", "field2", "field3")
assert 30 * 60 - 10 < result[0] <= 30 * 60
Expand All @@ -346,7 +321,7 @@ def test_httl_nonexistent_key(r: redis.Redis):
def test_hpttl_multiple_fields_mixed_conditions(r: redis.Redis):
r.delete("redis-key")
r.hset("redis-key", mapping={"field1": "value1", "field2": "value2"})
future_time = int((datetime.now() + timedelta(minutes=30)).timestamp())
future_time = current_time() // 1000 + 30 * 60
r.hexpireat("redis-key", future_time, "field1")
result = r.hpttl("redis-key", "field1", "field2", "field3")
assert 30 * 60000 - 10000 < result[0] <= 30 * 60000
Expand Down

0 comments on commit b0c2f41

Please sign in to comment.