Skip to content

Add support for new BITOP operations: DIFF, DIFF1, ANDOR, ONE #3690

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
97 changes: 97 additions & 0 deletions tests/test_asyncio/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,103 @@ async def test_bitop_string_operands(self, r: redis.Redis):
assert int(binascii.hexlify(await r.get("res2")), 16) == 0x0102FFFF
assert int(binascii.hexlify(await r.get("res3")), 16) == 0x000000FF

@pytest.mark.onlynoncluster
@skip_if_server_version_lt("8.1.224")
async def test_bitop_diff(self, r: redis.Redis):
await r.set("a", b"\xf0")
await r.set("b", b"\xc0")
await r.set("c", b"\x80")

result = await r.bitop("DIFF", "result", "a", "b", "c")
assert result == 1
assert await r.get("result") == b"\x30"

await r.bitop("DIFF", "result2", "a", "nonexistent")
assert await r.get("result2") == b"\xf0"

@pytest.mark.onlynoncluster
@skip_if_server_version_lt("8.1.224")
async def test_bitop_diff1(self, r: redis.Redis):
await r.set("a", b"\xf0")
await r.set("b", b"\xc0")
await r.set("c", b"\x80")

result = await r.bitop("DIFF1", "result", "a", "b", "c")
assert result == 1
assert await r.get("result") == b"\x00"

await r.set("d", b"\x0f")
await r.set("e", b"\x03")
await r.bitop("DIFF1", "result2", "d", "e")
assert await r.get("result2") == b"\x00"

@pytest.mark.onlynoncluster
@skip_if_server_version_lt("8.1.224")
async def test_bitop_andor(self, r: redis.Redis):
await r.set("a", b"\xf0")
await r.set("b", b"\xc0")
await r.set("c", b"\x80")

result = await r.bitop("ANDOR", "result", "a", "b", "c")
assert result == 1
assert await r.get("result") == b"\xc0"

await r.set("x", b"\xf0")
await r.set("y", b"\x0f")
await r.bitop("ANDOR", "result2", "x", "y")
assert await r.get("result2") == b"\x00"

@pytest.mark.onlynoncluster
@skip_if_server_version_lt("8.1.224")
async def test_bitop_one(self, r: redis.Redis):
await r.set("a", b"\xf0")
await r.set("b", b"\xc0")
await r.set("c", b"\x80")

result = await r.bitop("ONE", "result", "a", "b", "c")
assert result == 1
assert await r.get("result") == b"\x30"

await r.set("x", b"\xf0")
await r.set("y", b"\x0f")
await r.bitop("ONE", "result2", "x", "y")
assert await r.get("result2") == b"\xff"

@pytest.mark.onlynoncluster
@skip_if_server_version_lt("8.1.224")
async def test_bitop_new_operations_with_empty_keys(self, r: redis.Redis):
await r.set("a", b"\xff")

await r.bitop("DIFF", "empty_result", "nonexistent", "a")
assert await r.get("empty_result") == b"\x00"

await r.bitop("DIFF1", "empty_result2", "a", "nonexistent")
assert await r.get("empty_result2") == b"\x00"

await r.bitop("ANDOR", "empty_result3", "a", "nonexistent")
assert await r.get("empty_result3") == b"\x00"

await r.bitop("ONE", "empty_result4", "nonexistent")
assert await r.get("empty_result4") is None

@pytest.mark.onlynoncluster
@skip_if_server_version_lt("8.1.224")
async def test_bitop_new_operations_return_values(self, r: redis.Redis):
await r.set("a", b"\xff\x00\xff")
await r.set("b", b"\x00\xff")

result1 = await r.bitop("DIFF", "result1", "a", "b")
assert result1 == 3

result2 = await r.bitop("DIFF1", "result2", "a", "b")
assert result2 == 3

result3 = await r.bitop("ANDOR", "result3", "a", "b")
assert result3 == 3

result4 = await r.bitop("ONE", "result4", "a", "b")
assert result4 == 3

@pytest.mark.onlynoncluster
@skip_if_server_version_lt("2.8.7")
async def test_bitpos(self, r: redis.Redis):
Expand Down
97 changes: 97 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,103 @@ def test_bitop_string_operands(self, r):
assert int(binascii.hexlify(r["res2"]), 16) == 0x0102FFFF
assert int(binascii.hexlify(r["res3"]), 16) == 0x000000FF

@pytest.mark.onlynoncluster
@skip_if_server_version_lt("8.1.224")
def test_bitop_diff(self, r):
r["a"] = b"\xf0"
r["b"] = b"\xc0"
r["c"] = b"\x80"

result = r.bitop("DIFF", "result", "a", "b", "c")
assert result == 1
assert r["result"] == b"\x30"

r.bitop("DIFF", "result2", "a", "nonexistent")
assert r["result2"] == b"\xf0"

@pytest.mark.onlynoncluster
@skip_if_server_version_lt("8.1.224")
def test_bitop_diff1(self, r):
r["a"] = b"\xf0"
r["b"] = b"\xc0"
r["c"] = b"\x80"

result = r.bitop("DIFF1", "result", "a", "b", "c")
assert result == 1
assert r["result"] == b"\x00"

r["d"] = b"\x0f"
r["e"] = b"\x03"
r.bitop("DIFF1", "result2", "d", "e")
assert r["result2"] == b"\x00"

@pytest.mark.onlynoncluster
@skip_if_server_version_lt("8.1.224")
def test_bitop_andor(self, r):
r["a"] = b"\xf0"
r["b"] = b"\xc0"
r["c"] = b"\x80"

result = r.bitop("ANDOR", "result", "a", "b", "c")
assert result == 1
assert r["result"] == b"\xc0"

r["x"] = b"\xf0"
r["y"] = b"\x0f"
r.bitop("ANDOR", "result2", "x", "y")
assert r["result2"] == b"\x00"

@pytest.mark.onlynoncluster
@skip_if_server_version_lt("8.1.224")
def test_bitop_one(self, r):
r["a"] = b"\xf0"
r["b"] = b"\xc0"
r["c"] = b"\x80"

result = r.bitop("ONE", "result", "a", "b", "c")
assert result == 1
assert r["result"] == b"\x30"

r["x"] = b"\xf0"
r["y"] = b"\x0f"
r.bitop("ONE", "result2", "x", "y")
assert r["result2"] == b"\xff"

@pytest.mark.onlynoncluster
@skip_if_server_version_lt("8.1.224")
def test_bitop_new_operations_with_empty_keys(self, r):
r["a"] = b"\xff"

r.bitop("DIFF", "empty_result", "nonexistent", "a")
assert r.get("empty_result") == b"\x00"

r.bitop("DIFF1", "empty_result2", "a", "nonexistent")
assert r.get("empty_result2") == b"\x00"

r.bitop("ANDOR", "empty_result3", "a", "nonexistent")
assert r.get("empty_result3") == b"\x00"

r.bitop("ONE", "empty_result4", "nonexistent")
assert r.get("empty_result4") is None

@pytest.mark.onlynoncluster
@skip_if_server_version_lt("8.1.224")
def test_bitop_new_operations_return_values(self, r):
r["a"] = b"\xff\x00\xff"
r["b"] = b"\x00\xff"

result1 = r.bitop("DIFF", "result1", "a", "b")
assert result1 == 3

result2 = r.bitop("DIFF1", "result2", "a", "b")
assert result2 == 3

result3 = r.bitop("ANDOR", "result3", "a", "b")
assert result3 == 3

result4 = r.bitop("ONE", "result4", "a", "b")
assert result4 == 3

@pytest.mark.onlynoncluster
@skip_if_server_version_lt("2.8.7")
def test_bitpos(self, r):
Expand Down
Loading