Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for BIT|BYTE option #2068

Merged
merged 3 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,7 @@ def bitcount(
key: KeyT,
start: Union[int, None] = None,
end: Union[int, None] = None,
mode: Optional[str] = None,
) -> ResponseT:
"""
Returns the count of set bits in the value of ``key``. Optional
Expand All @@ -1376,6 +1377,8 @@ def bitcount(
params.append(end)
elif (start is not None and end is None) or (end is not None and start is None):
raise DataError("Both start and end must be specified")
if mode is not None:
params.append(mode)
return self.execute_command("BITCOUNT", *params)

def bitfield(
Expand Down Expand Up @@ -1411,6 +1414,7 @@ def bitpos(
bit: int,
start: Union[int, None] = None,
end: Union[int, None] = None,
mode: Optional[str] = None,
) -> ResponseT:
"""
Return the position of the first bit set to 1 or 0 in a string.
Expand All @@ -1430,6 +1434,9 @@ def bitpos(
params.append(end)
elif start is None and end is not None:
raise DataError("start argument is not set, " "when end is specified")

if mode is not None:
params.append(mode)
return self.execute_command("BITPOS", *params)

def copy(
Expand Down
18 changes: 18 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,15 @@ def test_bitcount(self, r):
assert r.bitcount("a", -2, -1) == 2
assert r.bitcount("a", 1, 1) == 1

@skip_if_server_version_lt("7.0.0")
def test_bitcount_mode(self, r):
r.set("mykey", "foobar")
assert r.bitcount("mykey") == 26
assert r.bitcount("mykey", 1, 1, "byte") == 6
assert r.bitcount("mykey", 5, 30, "bit") == 17
with pytest.raises(redis.ResponseError):
assert r.bitcount("mykey", 5, 30, "but")

@pytest.mark.onlynoncluster
@skip_if_server_version_lt("2.6.0")
def test_bitop_not_empty_string(self, r):
Expand Down Expand Up @@ -926,6 +935,15 @@ def test_bitpos_wrong_arguments(self, r):
with pytest.raises(exceptions.RedisError):
r.bitpos(key, 7) == 12

@skip_if_server_version_lt("7.0.0")
def test_bitpos_mode(self, r):
r.set("mykey", b"\x00\xff\xf0")
assert r.bitpos("mykey", 1, 0) == 8
assert r.bitpos("mykey", 1, 2, -1, "byte") == 16
assert r.bitpos("mykey", 0, 7, 15, "bit") == 7
with pytest.raises(redis.ResponseError):
r.bitpos("mykey", 1, 7, 15, "bite")

@pytest.mark.onlynoncluster
@skip_if_server_version_lt("6.2.0")
def test_copy(self, r):
Expand Down