Skip to content

Fix tests for Redis 7 #2182

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

Merged
merged 4 commits into from
May 31, 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
1 change: 1 addition & 0 deletions redis/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ class AbstractRedisCluster:
"CLUSTER KEYSLOT",
"COMMAND",
"COMMAND COUNT",
"COMMAND LIST",
"COMMAND GETKEYS",
"CONFIG GET",
"DEBUG",
Expand Down
2 changes: 1 addition & 1 deletion redis/commands/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def get_keys(self, redis_conn, *args):
command = self.commands.get(cmd_name)
if "movablekeys" in command["flags"]:
keys = self._get_moveable_keys(redis_conn, *args)
elif "pubsub" in command["flags"]:
elif "pubsub" in command["flags"] or command["name"] == "pubsub":
keys = self._get_pubsub_keys(*args)
else:
if (
Expand Down
36 changes: 20 additions & 16 deletions tests/test_asyncio/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,13 +517,10 @@ async def test_config_resetstat(self, r: redis.Redis):
assert reset_commands_processed < prior_commands_processed

async def test_config_set(self, r: redis.Redis):
data = await r.config_get()
rdbname = data["dbfilename"]
try:
assert await r.config_set("dbfilename", "redis_py_test.rdb")
assert (await r.config_get())["dbfilename"] == "redis_py_test.rdb"
finally:
assert await r.config_set("dbfilename", rdbname)
await r.config_set("timeout", 70)
assert (await r.config_get())["timeout"] == "70"
assert await r.config_set("timeout", 0)
assert (await r.config_get())["timeout"] == "0"

@pytest.mark.onlynoncluster
async def test_dbsize(self, r: redis.Redis):
Expand All @@ -541,7 +538,8 @@ async def test_info(self, r: redis.Redis):
await r.set("b", "bar")
info = await r.info()
assert isinstance(info, dict)
assert info["db9"]["keys"] == 2
assert "arch_bits" in info.keys()
assert "redis_version" in info.keys()

@pytest.mark.onlynoncluster
async def test_lastsave(self, r: redis.Redis):
Expand Down Expand Up @@ -2180,6 +2178,7 @@ async def test_cluster_slaves(self, mock_cluster_resp_slaves):
)

@skip_if_server_version_lt("3.0.0")
@skip_if_server_version_gte("7.0.0")
@pytest.mark.onlynoncluster
async def test_readwrite(self, r: redis.Redis):
assert await r.readwrite()
Expand Down Expand Up @@ -2543,7 +2542,7 @@ async def test_xclaim(self, r: redis.Redis):
justid=True,
) == [message_id]

@skip_if_server_version_lt("5.0.0")
@skip_if_server_version_lt("7.0.0")
async def test_xclaim_trimmed(self, r: redis.Redis):
# xclaim should not raise an exception if the item is not there
stream = "stream"
Expand All @@ -2564,9 +2563,8 @@ async def test_xclaim_trimmed(self, r: redis.Redis):
# xclaim them from consumer2
# the item that is still in the stream should be returned
item = await r.xclaim(stream, group, "consumer2", 0, [sid1, sid2])
assert len(item) == 2
assert item[0] == (None, None)
assert item[1][0] == sid2
assert len(item) == 1
assert item[0][0] == sid2

@skip_if_server_version_lt("5.0.0")
async def test_xdel(self, r: redis.Redis):
Expand All @@ -2583,7 +2581,7 @@ async def test_xdel(self, r: redis.Redis):
assert await r.xdel(stream, m1) == 1
assert await r.xdel(stream, m2, m3) == 2

@skip_if_server_version_lt("5.0.0")
@skip_if_server_version_lt("7.0.0")
async def test_xgroup_create(self, r: redis.Redis):
# tests xgroup_create and xinfo_groups
stream = "stream"
Expand All @@ -2600,11 +2598,13 @@ async def test_xgroup_create(self, r: redis.Redis):
"consumers": 0,
"pending": 0,
"last-delivered-id": b"0-0",
"entries-read": None,
"lag": 1,
}
]
assert await r.xinfo_groups(stream) == expected

@skip_if_server_version_lt("5.0.0")
@skip_if_server_version_lt("7.0.0")
async def test_xgroup_create_mkstream(self, r: redis.Redis):
# tests xgroup_create and xinfo_groups
stream = "stream"
Expand All @@ -2624,6 +2624,8 @@ async def test_xgroup_create_mkstream(self, r: redis.Redis):
"consumers": 0,
"pending": 0,
"last-delivered-id": b"0-0",
"entries-read": None,
"lag": 0,
}
]
assert await r.xinfo_groups(stream) == expected
Expand Down Expand Up @@ -2658,21 +2660,23 @@ async def test_xgroup_destroy(self, r: redis.Redis):
await r.xgroup_create(stream, group, 0)
assert await r.xgroup_destroy(stream, group)

@skip_if_server_version_lt("5.0.0")
@skip_if_server_version_lt("7.0.0")
async def test_xgroup_setid(self, r: redis.Redis):
stream = "stream"
group = "group"
message_id = await r.xadd(stream, {"foo": "bar"})

await r.xgroup_create(stream, group, 0)
# advance the last_delivered_id to the message_id
await r.xgroup_setid(stream, group, message_id)
await r.xgroup_setid(stream, group, message_id, entries_read=2)
expected = [
{
"name": group.encode(),
"consumers": 0,
"pending": 0,
"last-delivered-id": message_id,
"entries-read": 2,
"lag": -1,
}
]
assert await r.xinfo_groups(stream) == expected
Expand Down
37 changes: 26 additions & 11 deletions tests/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,15 +858,15 @@ def test_cluster_shards(self, r):
assert isinstance(cluster_shards, list)
assert isinstance(cluster_shards[0], dict)
attributes = [
"id",
"endpoint",
"ip",
"hostname",
"port",
"tls-port",
"role",
"replication-offset",
"health",
b"id",
b"endpoint",
b"ip",
b"hostname",
b"port",
b"tls-port",
b"role",
b"replication-offset",
b"health",
]
for x in cluster_shards:
assert list(x.keys()) == ["slots", "nodes"]
Expand Down Expand Up @@ -915,9 +915,24 @@ def test_cluster_delslots(self):

@skip_if_server_version_lt("7.0.0")
@skip_if_redis_enterprise()
def test_cluster_delslotsrange(self, r):
def test_cluster_delslotsrange(self):
cluster_slots = [
[
0,
8191,
["127.0.0.1", 7000, "node_0"],
],
[
8192,
16383,
["127.0.0.1", 7001, "node_1"],
],
]
r = get_mocked_redis_client(
host=default_host, port=default_port, cluster_slots=cluster_slots
)
mock_all_nodes_resp(r, "OK")
node = r.get_random_node()
mock_node_resp(node, "OK")
r.cluster_addslots(node, 1, 2, 3, 4, 5)
assert r.cluster_delslotsrange(1, 5)

Expand Down
4 changes: 0 additions & 4 deletions tests/test_command_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ def test_get_moveable_keys(self, r):
"key3",
]
args7 = ["MIGRATE", "192.168.1.34", 6379, "key1", 0, 5000]
args8 = ["STRALGO", "LCS", "STRINGS", "string_a", "string_b"]
args9 = ["STRALGO", "LCS", "KEYS", "key1", "key2"]

assert commands_parser.get_keys(r, *args1).sort() == ["key1", "key2"].sort()
assert (
Expand All @@ -68,8 +66,6 @@ def test_get_moveable_keys(self, r):
== ["key1", "key2", "key3"].sort()
)
assert commands_parser.get_keys(r, *args7).sort() == ["key1"].sort()
assert commands_parser.get_keys(r, *args8) is None
assert commands_parser.get_keys(r, *args9).sort() == ["key1", "key2"].sort()

# A bug in redis<7.0 causes this to fail: https://github.com/redis/redis/issues/9493
@skip_if_server_version_lt("7.0.0")
Expand Down
30 changes: 17 additions & 13 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ def test_client_unpause(self, r):
@pytest.mark.onlynoncluster
@skip_if_server_version_lt("7.0.0")
def test_client_no_evict(self, r):
assert r.client_no_evict("ON") == "OK"
assert r.client_no_evict("ON")
with pytest.raises(TypeError):
r.client_no_evict()

Expand Down Expand Up @@ -1089,9 +1089,9 @@ def test_unlink_with_multiple_keys(self, r):
@skip_if_server_version_lt("7.0.0")
def test_lcs(self, r):
r.mset({"foo": "ohmytext", "bar": "mynewtext"})
assert r.lcs("foo", "bar") == "mytext"
assert r.lcs("foo", "bar") == b"mytext"
assert r.lcs("foo", "bar", len=True) == 6
result = ["matches", [[[4, 7], [5, 8]]], "len", 6]
result = [b"matches", [[[4, 7], [5, 8]]], b"len", 6]
assert r.lcs("foo", "bar", idx=True, minmatchlen=3) == result
with pytest.raises(redis.ResponseError):
assert r.lcs("foo", "bar", len=True, idx=True)
Expand Down Expand Up @@ -1764,24 +1764,24 @@ def test_brpoplpush_empty_string(self, r):
@skip_if_server_version_lt("7.0.0")
def test_blmpop(self, r):
r.rpush("a", "1", "2", "3", "4", "5")
res = ["a", ["1", "2"]]
res = [b"a", [b"1", b"2"]]
assert r.blmpop(1, "2", "b", "a", direction="LEFT", count=2) == res
with pytest.raises(TypeError):
r.blmpop(1, "2", "b", "a", count=2)
r.rpush("b", "6", "7", "8", "9")
assert r.blmpop(0, "2", "b", "a", direction="LEFT") == ["b", ["6"]]
assert r.blmpop(0, "2", "b", "a", direction="LEFT") == [b"b", [b"6"]]
assert r.blmpop(1, "2", "foo", "bar", direction="RIGHT") is None

@pytest.mark.onlynoncluster
@skip_if_server_version_lt("7.0.0")
def test_lmpop(self, r):
r.rpush("foo", "1", "2", "3", "4", "5")
result = ["foo", ["1", "2"]]
result = [b"foo", [b"1", b"2"]]
assert r.lmpop("2", "bar", "foo", direction="LEFT", count=2) == result
with pytest.raises(redis.ResponseError):
r.lmpop("2", "bar", "foo", direction="up", count=2)
r.rpush("bar", "a", "b", "c", "d")
assert r.lmpop("2", "bar", "foo", direction="LEFT") == ["bar", ["a"]]
assert r.lmpop("2", "bar", "foo", direction="LEFT") == [b"bar", [b"a"]]

def test_lindex(self, r):
r.rpush("a", "1", "2", "3")
Expand Down Expand Up @@ -2393,23 +2393,23 @@ def test_bzpopmin(self, r):
@skip_if_server_version_lt("7.0.0")
def test_zmpop(self, r):
r.zadd("a", {"a1": 1, "a2": 2, "a3": 3})
res = ["a", [["a1", "1"], ["a2", "2"]]]
res = [b"a", [[b"a1", b"1"], [b"a2", b"2"]]]
assert r.zmpop("2", ["b", "a"], min=True, count=2) == res
with pytest.raises(redis.DataError):
r.zmpop("2", ["b", "a"], count=2)
r.zadd("b", {"b1": 10, "ab": 9, "b3": 8})
assert r.zmpop("2", ["b", "a"], max=True) == ["b", [["b1", "10"]]]
assert r.zmpop("2", ["b", "a"], max=True) == [b"b", [[b"b1", b"10"]]]

@pytest.mark.onlynoncluster
@skip_if_server_version_lt("7.0.0")
def test_bzmpop(self, r):
r.zadd("a", {"a1": 1, "a2": 2, "a3": 3})
res = ["a", [["a1", "1"], ["a2", "2"]]]
res = [b"a", [[b"a1", b"1"], [b"a2", b"2"]]]
assert r.bzmpop(1, "2", ["b", "a"], min=True, count=2) == res
with pytest.raises(redis.DataError):
r.bzmpop(1, "2", ["b", "a"], count=2)
r.zadd("b", {"b1": 10, "ab": 9, "b3": 8})
res = ["b", [["b1", "10"]]]
res = [b"b", [[b"b1", b"10"]]]
assert r.bzmpop(0, "2", ["b", "a"], max=True) == res
assert r.bzmpop(1, "2", ["foo", "bar"], max=True) is None

Expand Down Expand Up @@ -3100,6 +3100,7 @@ def test_cluster_slaves(self, mock_cluster_resp_slaves):

@pytest.mark.onlynoncluster
@skip_if_server_version_lt("3.0.0")
@skip_if_server_version_gte("7.0.0")
@skip_if_redis_enterprise()
def test_readwrite(self, r):
assert r.readwrite()
Expand Down Expand Up @@ -4510,7 +4511,7 @@ def test_command_list(self, r: redis.Redis):
assert len(r.command_list()) > 300
assert len(r.command_list(module="fakemod")) == 0
assert len(r.command_list(category="list")) > 15
assert "lpop" in r.command_list(pattern="l*")
assert b"lpop" in r.command_list(pattern="l*")
with pytest.raises(redis.ResponseError):
r.command_list(category="list", pattern="l*")

Expand Down Expand Up @@ -4546,7 +4547,10 @@ def test_command(self, r):
@skip_if_server_version_lt("7.0.0")
@skip_if_redis_enterprise()
def test_command_getkeysandflags(self, r: redis.Redis):
res = [["mylist1", ["RW", "access", "delete"]], ["mylist2", ["RW", "insert"]]]
res = [
[b"mylist1", [b"RW", b"access", b"delete"]],
[b"mylist2", [b"RW", b"insert"]],
]
assert res == r.command_getkeysandflags(
"LMOVE", "mylist1", "mylist2", "left", "left"
)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_memoryviews_are_not_packed(self):
class TestCommandsAreNotEncoded:
@pytest.fixture()
def r(self, request):
return _get_client(redis.Redis, request=request, encoding="utf-16")
return _get_client(redis.Redis, request=request, encoding="utf-8")

def test_basic_command(self, r):
r.set("hello", "world")
Expand Down
Loading