From 11c986fe1215227f18e06d6a26a8e6202bf36eac Mon Sep 17 00:00:00 2001 From: Daniel M Date: Sun, 22 Sep 2024 14:12:16 -0400 Subject: [PATCH] feat: implement `EXPIRETIME` and `PEXPIRETIME` fix #323, #324 --- docs/about/changelog.md | 5 ++--- fakeredis/commands_mixins/generic_mixin.py | 20 ++++++++++++++++++-- test/test_mixins/test_generic_commands.py | 14 ++++++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/docs/about/changelog.md b/docs/about/changelog.md index 5fbfb5eb..2fa8a988 100644 --- a/docs/about/changelog.md +++ b/docs/about/changelog.md @@ -9,9 +9,8 @@ description: Change log of all fakeredis releases - Implement support for hash expiration related commands @j00bar #328 - `HEXPIRE`, `HEXPIREAT`, `HEXPIRETIME`, `HPERSIST`, `HPEXPIRE`, `HPEXPIREAT`, `HPEXPIRETIME`, `HPTTL`, `HTTL`, -- Implement support for `SORT_RO` #325 -- - +- Implement support for `SORT_RO` #325, `EXPIRETIME` #323, and `PEXPIRETIME` #324 + ## v2.24.1 ### 🐛 Bug Fixes diff --git a/fakeredis/commands_mixins/generic_mixin.py b/fakeredis/commands_mixins/generic_mixin.py index 522644a2..7f8e4ad6 100644 --- a/fakeredis/commands_mixins/generic_mixin.py +++ b/fakeredis/commands_mixins/generic_mixin.py @@ -161,10 +161,26 @@ def pexpire(self, key, ms): def pexpireat(self, key, ms_timestamp): return self._expireat(key, ms_timestamp / 1000.0) - @command((Key(),)) - def pttl(self, key): + @command(name="PTTL", fixed=(Key(),)) + def pttl(self, key: CommandItem) -> int: return self._ttl(key, 1000.0) + @command(name="EXPIRETIME", fixed=(Key(),)) + def expiretime(self, key: CommandItem) -> int: + if key.value is None: + return -2 + if key.expireat is None: + return -1 + return key.expireat + + @command(name="PEXPIRETIME", fixed=(Key(),)) + def pexpiretime(self, key: CommandItem) -> int: + if key.value is None: + return -2 + if key.expireat is None: + return -1 + return key.expireat * 1000 + @command(()) def randomkey(self): keys = list(self._db.keys()) diff --git a/test/test_mixins/test_generic_commands.py b/test/test_mixins/test_generic_commands.py index 9366f2b2..e90c26f4 100644 --- a/test/test_mixins/test_generic_commands.py +++ b/test/test_mixins/test_generic_commands.py @@ -127,6 +127,20 @@ def test_sort_ro(r: redis.Redis): assert r.sort_ro("b", desc=True) == [b"3", b"2", b"1"] +@pytest.mark.min_server("7") +def test_expiretime(r: redis.Redis): + r.set("a", "foo") + r.expireat("a", 33177117420) + assert r.expiretime("a") == 33177117420 + + +@pytest.mark.min_server("7") +def test_pexpiretime(r: redis.Redis): + r.set("a", "foo") + r.pexpireat("a", 33177117420000) + assert r.pexpiretime("a") == 33177117420000 + + def test_sort_empty(r: redis.Redis): assert r.sort("foo") == []