From 3d5884da0b8eb9a5ca5c5b1ec116610297c6215a Mon Sep 17 00:00:00 2001 From: Alibi Shalgymbay Date: Tue, 16 Aug 2022 23:10:28 +0600 Subject: [PATCH] Add BITFIELD_RO --- redis/commands/core.py | 23 +++++++++++++++++++++++ tests/test_asyncio/test_commands.py | 13 +++++++++++++ tests/test_commands.py | 13 +++++++++++++ 3 files changed, 49 insertions(+) diff --git a/redis/commands/core.py b/redis/commands/core.py index 6cf04577fc..51f30d2ef0 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -1504,6 +1504,29 @@ def bitfield( """ return BitFieldOperation(self, key, default_overflow=default_overflow) + def bitfield_ro( + self: Union["Redis", "AsyncRedis"], + key: KeyT, + encoding: str, + offset: BitfieldOffsetT, + items: Optional[list] = None, + ) -> ResponseT: + """ + Return an array of the specified bitfield values + where the first value is found using ``encoding`` and ``offset`` + parameters and remaining values are result of corresponding + encoding/offset pairs in optional list ``items`` + Read-only variant of the BITFIELD command. + + For more information see https://redis.io/commands/bitfield_ro + """ + params = [key, "GET", encoding, offset] + + items = items or [] + for encoding, offset in items: + params.extend(["GET", encoding, offset]) + return self.execute_command("BITFIELD_RO", *params) + def bitop(self, operation: str, dest: KeyT, *keys: KeyT) -> ResponseT: """ Perform a bitwise operation using ``operation`` between ``keys`` and diff --git a/tests/test_asyncio/test_commands.py b/tests/test_asyncio/test_commands.py index 4c25277616..422328a043 100644 --- a/tests/test_asyncio/test_commands.py +++ b/tests/test_asyncio/test_commands.py @@ -2953,6 +2953,19 @@ async def test_bitfield_operations(self, r: redis.Redis): ) assert resp == [0, None, 255] + @skip_if_server_version_lt("6.0.0") + async def test_bitfield_ro(self, r: redis.Redis): + bf = r.bitfield("a") + resp = await bf.set("u8", 8, 255).execute() + assert resp == [0] + + resp = await r.bitfield_ro("a", "u8", 0) + assert resp == [0] + + items = [("u4", 8), ("u4", 12), ("u4", 13)] + resp = await r.bitfield_ro("a", "u8", 0, items) + assert resp == [0, 15, 15, 14] + @skip_if_server_version_lt("4.0.0") async def test_memory_stats(self, r: redis.Redis): # put a key into the current db to make sure that "db." diff --git a/tests/test_commands.py b/tests/test_commands.py index f9134d858b..de8020cf07 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -4438,6 +4438,19 @@ def test_bitfield_operations(self, r): ) assert resp == [0, None, 255] + @skip_if_server_version_lt("6.0.0") + def test_bitfield_ro(self, r: redis.Redis): + bf = r.bitfield("a") + resp = bf.set("u8", 8, 255).execute() + assert resp == [0] + + resp = r.bitfield_ro("a", "u8", 0) + assert resp == [0] + + items = [("u4", 8), ("u4", 12), ("u4", 13)] + resp = r.bitfield_ro("a", "u8", 0, items) + assert resp == [0, 15, 15, 14] + @skip_if_server_version_lt("4.0.0") def test_memory_help(self, r): with pytest.raises(NotImplementedError):