From b0c2f41ba27b879be0539b1b16fc361781f8acde Mon Sep 17 00:00:00 2001 From: Daniel M Date: Sun, 22 Sep 2024 13:46:55 -0400 Subject: [PATCH] fix:tests --- test/test_mixins/test_hash_expire_commands.py | 79 +++++++------------ 1 file changed, 27 insertions(+), 52 deletions(-) diff --git a/test/test_mixins/test_hash_expire_commands.py b/test/test_mixins/test_hash_expire_commands.py index d29242a9..4a24f3cd 100644 --- a/test/test_mixins/test_hash_expire_commands.py +++ b/test/test_mixins/test_hash_expire_commands.py @@ -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 = [] @@ -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), @@ -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"}) @@ -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( @@ -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"}) @@ -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 @@ -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 @@ -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] @@ -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] @@ -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 @@ -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) @@ -226,19 +211,9 @@ 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 @@ -246,8 +221,8 @@ def test_hpexpireat_with_datetime(r: redis.Redis): 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] @@ -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] @@ -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 @@ -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) @@ -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 @@ -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 @@ -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 @@ -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