Skip to content

Commit fbce374

Browse files
authored
Add support for new BITOP operations: DIFF, DIFF1, ANDOR, ONE (#3690)
* Add support for new BITOP operations: DIFF, DIFF1, ANDOR, ONE * fix linting issues * change version checking from 8.2.0 to 8.1.224
1 parent b7dcd80 commit fbce374

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed

tests/test_asyncio/test_commands.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,103 @@ async def test_bitop_string_operands(self, r: redis.Redis):
879879
assert int(binascii.hexlify(await r.get("res2")), 16) == 0x0102FFFF
880880
assert int(binascii.hexlify(await r.get("res3")), 16) == 0x000000FF
881881

882+
@pytest.mark.onlynoncluster
883+
@skip_if_server_version_lt("8.1.224")
884+
async def test_bitop_diff(self, r: redis.Redis):
885+
await r.set("a", b"\xf0")
886+
await r.set("b", b"\xc0")
887+
await r.set("c", b"\x80")
888+
889+
result = await r.bitop("DIFF", "result", "a", "b", "c")
890+
assert result == 1
891+
assert await r.get("result") == b"\x30"
892+
893+
await r.bitop("DIFF", "result2", "a", "nonexistent")
894+
assert await r.get("result2") == b"\xf0"
895+
896+
@pytest.mark.onlynoncluster
897+
@skip_if_server_version_lt("8.1.224")
898+
async def test_bitop_diff1(self, r: redis.Redis):
899+
await r.set("a", b"\xf0")
900+
await r.set("b", b"\xc0")
901+
await r.set("c", b"\x80")
902+
903+
result = await r.bitop("DIFF1", "result", "a", "b", "c")
904+
assert result == 1
905+
assert await r.get("result") == b"\x00"
906+
907+
await r.set("d", b"\x0f")
908+
await r.set("e", b"\x03")
909+
await r.bitop("DIFF1", "result2", "d", "e")
910+
assert await r.get("result2") == b"\x00"
911+
912+
@pytest.mark.onlynoncluster
913+
@skip_if_server_version_lt("8.1.224")
914+
async def test_bitop_andor(self, r: redis.Redis):
915+
await r.set("a", b"\xf0")
916+
await r.set("b", b"\xc0")
917+
await r.set("c", b"\x80")
918+
919+
result = await r.bitop("ANDOR", "result", "a", "b", "c")
920+
assert result == 1
921+
assert await r.get("result") == b"\xc0"
922+
923+
await r.set("x", b"\xf0")
924+
await r.set("y", b"\x0f")
925+
await r.bitop("ANDOR", "result2", "x", "y")
926+
assert await r.get("result2") == b"\x00"
927+
928+
@pytest.mark.onlynoncluster
929+
@skip_if_server_version_lt("8.1.224")
930+
async def test_bitop_one(self, r: redis.Redis):
931+
await r.set("a", b"\xf0")
932+
await r.set("b", b"\xc0")
933+
await r.set("c", b"\x80")
934+
935+
result = await r.bitop("ONE", "result", "a", "b", "c")
936+
assert result == 1
937+
assert await r.get("result") == b"\x30"
938+
939+
await r.set("x", b"\xf0")
940+
await r.set("y", b"\x0f")
941+
await r.bitop("ONE", "result2", "x", "y")
942+
assert await r.get("result2") == b"\xff"
943+
944+
@pytest.mark.onlynoncluster
945+
@skip_if_server_version_lt("8.1.224")
946+
async def test_bitop_new_operations_with_empty_keys(self, r: redis.Redis):
947+
await r.set("a", b"\xff")
948+
949+
await r.bitop("DIFF", "empty_result", "nonexistent", "a")
950+
assert await r.get("empty_result") == b"\x00"
951+
952+
await r.bitop("DIFF1", "empty_result2", "a", "nonexistent")
953+
assert await r.get("empty_result2") == b"\x00"
954+
955+
await r.bitop("ANDOR", "empty_result3", "a", "nonexistent")
956+
assert await r.get("empty_result3") == b"\x00"
957+
958+
await r.bitop("ONE", "empty_result4", "nonexistent")
959+
assert await r.get("empty_result4") is None
960+
961+
@pytest.mark.onlynoncluster
962+
@skip_if_server_version_lt("8.1.224")
963+
async def test_bitop_new_operations_return_values(self, r: redis.Redis):
964+
await r.set("a", b"\xff\x00\xff")
965+
await r.set("b", b"\x00\xff")
966+
967+
result1 = await r.bitop("DIFF", "result1", "a", "b")
968+
assert result1 == 3
969+
970+
result2 = await r.bitop("DIFF1", "result2", "a", "b")
971+
assert result2 == 3
972+
973+
result3 = await r.bitop("ANDOR", "result3", "a", "b")
974+
assert result3 == 3
975+
976+
result4 = await r.bitop("ONE", "result4", "a", "b")
977+
assert result4 == 3
978+
882979
@pytest.mark.onlynoncluster
883980
@skip_if_server_version_lt("2.8.7")
884981
async def test_bitpos(self, r: redis.Redis):

tests/test_commands.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,103 @@ def test_bitop_string_operands(self, r):
13131313
assert int(binascii.hexlify(r["res2"]), 16) == 0x0102FFFF
13141314
assert int(binascii.hexlify(r["res3"]), 16) == 0x000000FF
13151315

1316+
@pytest.mark.onlynoncluster
1317+
@skip_if_server_version_lt("8.1.224")
1318+
def test_bitop_diff(self, r):
1319+
r["a"] = b"\xf0"
1320+
r["b"] = b"\xc0"
1321+
r["c"] = b"\x80"
1322+
1323+
result = r.bitop("DIFF", "result", "a", "b", "c")
1324+
assert result == 1
1325+
assert r["result"] == b"\x30"
1326+
1327+
r.bitop("DIFF", "result2", "a", "nonexistent")
1328+
assert r["result2"] == b"\xf0"
1329+
1330+
@pytest.mark.onlynoncluster
1331+
@skip_if_server_version_lt("8.1.224")
1332+
def test_bitop_diff1(self, r):
1333+
r["a"] = b"\xf0"
1334+
r["b"] = b"\xc0"
1335+
r["c"] = b"\x80"
1336+
1337+
result = r.bitop("DIFF1", "result", "a", "b", "c")
1338+
assert result == 1
1339+
assert r["result"] == b"\x00"
1340+
1341+
r["d"] = b"\x0f"
1342+
r["e"] = b"\x03"
1343+
r.bitop("DIFF1", "result2", "d", "e")
1344+
assert r["result2"] == b"\x00"
1345+
1346+
@pytest.mark.onlynoncluster
1347+
@skip_if_server_version_lt("8.1.224")
1348+
def test_bitop_andor(self, r):
1349+
r["a"] = b"\xf0"
1350+
r["b"] = b"\xc0"
1351+
r["c"] = b"\x80"
1352+
1353+
result = r.bitop("ANDOR", "result", "a", "b", "c")
1354+
assert result == 1
1355+
assert r["result"] == b"\xc0"
1356+
1357+
r["x"] = b"\xf0"
1358+
r["y"] = b"\x0f"
1359+
r.bitop("ANDOR", "result2", "x", "y")
1360+
assert r["result2"] == b"\x00"
1361+
1362+
@pytest.mark.onlynoncluster
1363+
@skip_if_server_version_lt("8.1.224")
1364+
def test_bitop_one(self, r):
1365+
r["a"] = b"\xf0"
1366+
r["b"] = b"\xc0"
1367+
r["c"] = b"\x80"
1368+
1369+
result = r.bitop("ONE", "result", "a", "b", "c")
1370+
assert result == 1
1371+
assert r["result"] == b"\x30"
1372+
1373+
r["x"] = b"\xf0"
1374+
r["y"] = b"\x0f"
1375+
r.bitop("ONE", "result2", "x", "y")
1376+
assert r["result2"] == b"\xff"
1377+
1378+
@pytest.mark.onlynoncluster
1379+
@skip_if_server_version_lt("8.1.224")
1380+
def test_bitop_new_operations_with_empty_keys(self, r):
1381+
r["a"] = b"\xff"
1382+
1383+
r.bitop("DIFF", "empty_result", "nonexistent", "a")
1384+
assert r.get("empty_result") == b"\x00"
1385+
1386+
r.bitop("DIFF1", "empty_result2", "a", "nonexistent")
1387+
assert r.get("empty_result2") == b"\x00"
1388+
1389+
r.bitop("ANDOR", "empty_result3", "a", "nonexistent")
1390+
assert r.get("empty_result3") == b"\x00"
1391+
1392+
r.bitop("ONE", "empty_result4", "nonexistent")
1393+
assert r.get("empty_result4") is None
1394+
1395+
@pytest.mark.onlynoncluster
1396+
@skip_if_server_version_lt("8.1.224")
1397+
def test_bitop_new_operations_return_values(self, r):
1398+
r["a"] = b"\xff\x00\xff"
1399+
r["b"] = b"\x00\xff"
1400+
1401+
result1 = r.bitop("DIFF", "result1", "a", "b")
1402+
assert result1 == 3
1403+
1404+
result2 = r.bitop("DIFF1", "result2", "a", "b")
1405+
assert result2 == 3
1406+
1407+
result3 = r.bitop("ANDOR", "result3", "a", "b")
1408+
assert result3 == 3
1409+
1410+
result4 = r.bitop("ONE", "result4", "a", "b")
1411+
assert result4 == 3
1412+
13161413
@pytest.mark.onlynoncluster
13171414
@skip_if_server_version_lt("2.8.7")
13181415
def test_bitpos(self, r):

0 commit comments

Comments
 (0)