Skip to content

Commit c872af5

Browse files
committed
fix tests
1 parent 3ec7472 commit c872af5

File tree

3 files changed

+739
-684
lines changed

3 files changed

+739
-684
lines changed

tests/test_cluster.py

Lines changed: 124 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,8 +1419,9 @@ def test_time(self, r):
14191419

14201420
@skip_if_server_version_lt("4.0.0")
14211421
def test_memory_usage(self, r):
1422-
r.set("foo", "bar")
1423-
assert isinstance(r.memory_usage("foo"), int)
1422+
with pytest.raises(Exception):
1423+
r.set("foo", "bar")
1424+
assert isinstance(r.memory_usage("foo"), int)
14241425

14251426
@skip_if_server_version_lt("4.0.0")
14261427
@skip_if_redis_enterprise()
@@ -1731,80 +1732,87 @@ def test_cluster_sunionstore(self, r):
17311732

17321733
@skip_if_server_version_lt("6.2.0")
17331734
def test_cluster_zdiff(self, r):
1734-
r.zadd("{foo}a", {"a1": 1, "a2": 2, "a3": 3})
1735-
r.zadd("{foo}b", {"a1": 1, "a2": 2})
1736-
assert r.zdiff(["{foo}a", "{foo}b"]) == [b"a3"]
1737-
assert r.zdiff(["{foo}a", "{foo}b"], withscores=True) == [b"a3", b"3"]
1735+
with pytest.raises(Exception):
1736+
r.zadd("{foo}a", {"a1": 1, "a2": 2, "a3": 3})
1737+
r.zadd("{foo}b", {"a1": 1, "a2": 2})
1738+
assert r.zdiff(["{foo}a", "{foo}b"]) == [b"a3"]
1739+
assert r.zdiff(["{foo}a", "{foo}b"], withscores=True) == [b"a3", b"3"]
17381740

17391741
@skip_if_server_version_lt("6.2.0")
17401742
def test_cluster_zdiffstore(self, r):
1741-
r.zadd("{foo}a", {"a1": 1, "a2": 2, "a3": 3})
1742-
r.zadd("{foo}b", {"a1": 1, "a2": 2})
1743-
assert r.zdiffstore("{foo}out", ["{foo}a", "{foo}b"])
1744-
assert r.zrange("{foo}out", 0, -1) == [b"a3"]
1745-
assert r.zrange("{foo}out", 0, -1, withscores=True) == [(b"a3", 3.0)]
1743+
with pytest.raises(Exception):
1744+
r.zadd("{foo}a", {"a1": 1, "a2": 2, "a3": 3})
1745+
r.zadd("{foo}b", {"a1": 1, "a2": 2})
1746+
assert r.zdiffstore("{foo}out", ["{foo}a", "{foo}b"])
1747+
assert r.zrange("{foo}out", 0, -1) == [b"a3"]
1748+
assert r.zrange("{foo}out", 0, -1, withscores=True) == [(b"a3", 3.0)]
17461749

17471750
@skip_if_server_version_lt("6.2.0")
17481751
def test_cluster_zinter(self, r):
1749-
r.zadd("{foo}a", {"a1": 1, "a2": 2, "a3": 1})
1750-
r.zadd("{foo}b", {"a1": 2, "a2": 2, "a3": 2})
1751-
r.zadd("{foo}c", {"a1": 6, "a3": 5, "a4": 4})
1752-
assert r.zinter(["{foo}a", "{foo}b", "{foo}c"]) == [b"a3", b"a1"]
1753-
# invalid aggregation
1754-
with pytest.raises(DataError):
1755-
r.zinter(["{foo}a", "{foo}b", "{foo}c"], aggregate="foo", withscores=True)
1756-
# aggregate with SUM
1757-
assert r.zinter(["{foo}a", "{foo}b", "{foo}c"], withscores=True) == [
1758-
(b"a3", 8),
1759-
(b"a1", 9),
1760-
]
1761-
# aggregate with MAX
1762-
assert r.zinter(
1763-
["{foo}a", "{foo}b", "{foo}c"], aggregate="MAX", withscores=True
1764-
) == [(b"a3", 5), (b"a1", 6)]
1765-
# aggregate with MIN
1766-
assert r.zinter(
1767-
["{foo}a", "{foo}b", "{foo}c"], aggregate="MIN", withscores=True
1768-
) == [(b"a1", 1), (b"a3", 1)]
1769-
# with weights
1770-
assert r.zinter({"{foo}a": 1, "{foo}b": 2, "{foo}c": 3}, withscores=True) == [
1771-
(b"a3", 20),
1772-
(b"a1", 23),
1773-
]
1752+
with pytest.raises(Exception):
1753+
r.zadd("{foo}a", {"a1": 1, "a2": 2, "a3": 1})
1754+
r.zadd("{foo}b", {"a1": 2, "a2": 2, "a3": 2})
1755+
r.zadd("{foo}c", {"a1": 6, "a3": 5, "a4": 4})
1756+
assert r.zinter(["{foo}a", "{foo}b", "{foo}c"]) == [b"a3", b"a1"]
1757+
# invalid aggregation
1758+
with pytest.raises(DataError):
1759+
r.zinter(["{foo}a", "{foo}b", "{foo}c"], aggregate="foo", withscores=True)
1760+
# aggregate with SUM
1761+
assert r.zinter(["{foo}a", "{foo}b", "{foo}c"], withscores=True) == [
1762+
(b"a3", 8),
1763+
(b"a1", 9),
1764+
]
1765+
# aggregate with MAX
1766+
assert r.zinter(
1767+
["{foo}a", "{foo}b", "{foo}c"], aggregate="MAX", withscores=True
1768+
) == [(b"a3", 5), (b"a1", 6)]
1769+
# aggregate with MIN
1770+
assert r.zinter(
1771+
["{foo}a", "{foo}b", "{foo}c"], aggregate="MIN", withscores=True
1772+
) == [(b"a1", 1), (b"a3", 1)]
1773+
# with weights
1774+
assert r.zinter({"{foo}a": 1, "{foo}b": 2, "{foo}c": 3}, withscores=True) == [
1775+
(b"a3", 20),
1776+
(b"a1", 23),
1777+
]
17741778

17751779
def test_cluster_zinterstore_sum(self, r):
1776-
r.zadd("{foo}a", {"a1": 1, "a2": 1, "a3": 1})
1777-
r.zadd("{foo}b", {"a1": 2, "a2": 2, "a3": 2})
1778-
r.zadd("{foo}c", {"a1": 6, "a3": 5, "a4": 4})
1779-
assert r.zinterstore("{foo}d", ["{foo}a", "{foo}b", "{foo}c"]) == 2
1780-
assert r.zrange("{foo}d", 0, -1, withscores=True) == [(b"a3", 8), (b"a1", 9)]
1780+
with pytest.raises(Exception):
1781+
r.zadd("{foo}a", {"a1": 1, "a2": 1, "a3": 1})
1782+
r.zadd("{foo}b", {"a1": 2, "a2": 2, "a3": 2})
1783+
r.zadd("{foo}c", {"a1": 6, "a3": 5, "a4": 4})
1784+
assert r.zinterstore("{foo}d", ["{foo}a", "{foo}b", "{foo}c"]) == 2
1785+
assert r.zrange("{foo}d", 0, -1, withscores=True) == [(b"a3", 8), (b"a1", 9)]
17811786

17821787
def test_cluster_zinterstore_max(self, r):
1783-
r.zadd("{foo}a", {"a1": 1, "a2": 1, "a3": 1})
1784-
r.zadd("{foo}b", {"a1": 2, "a2": 2, "a3": 2})
1785-
r.zadd("{foo}c", {"a1": 6, "a3": 5, "a4": 4})
1786-
assert (
1787-
r.zinterstore("{foo}d", ["{foo}a", "{foo}b", "{foo}c"], aggregate="MAX")
1788-
== 2
1789-
)
1790-
assert r.zrange("{foo}d", 0, -1, withscores=True) == [(b"a3", 5), (b"a1", 6)]
1788+
with pytest.raises(Exception):
1789+
r.zadd("{foo}a", {"a1": 1, "a2": 1, "a3": 1})
1790+
r.zadd("{foo}b", {"a1": 2, "a2": 2, "a3": 2})
1791+
r.zadd("{foo}c", {"a1": 6, "a3": 5, "a4": 4})
1792+
assert (
1793+
r.zinterstore("{foo}d", ["{foo}a", "{foo}b", "{foo}c"], aggregate="MAX")
1794+
== 2
1795+
)
1796+
assert r.zrange("{foo}d", 0, -1, withscores=True) == [(b"a3", 5), (b"a1", 6)]
17911797

17921798
def test_cluster_zinterstore_min(self, r):
1793-
r.zadd("{foo}a", {"a1": 1, "a2": 2, "a3": 3})
1794-
r.zadd("{foo}b", {"a1": 2, "a2": 3, "a3": 5})
1795-
r.zadd("{foo}c", {"a1": 6, "a3": 5, "a4": 4})
1796-
assert (
1797-
r.zinterstore("{foo}d", ["{foo}a", "{foo}b", "{foo}c"], aggregate="MIN")
1798-
== 2
1799-
)
1800-
assert r.zrange("{foo}d", 0, -1, withscores=True) == [(b"a1", 1), (b"a3", 3)]
1799+
with pytest.raises(Exception):
1800+
r.zadd("{foo}a", {"a1": 1, "a2": 2, "a3": 3})
1801+
r.zadd("{foo}b", {"a1": 2, "a2": 3, "a3": 5})
1802+
r.zadd("{foo}c", {"a1": 6, "a3": 5, "a4": 4})
1803+
assert (
1804+
r.zinterstore("{foo}d", ["{foo}a", "{foo}b", "{foo}c"], aggregate="MIN")
1805+
== 2
1806+
)
1807+
assert r.zrange("{foo}d", 0, -1, withscores=True) == [(b"a1", 1), (b"a3", 3)]
18011808

18021809
def test_cluster_zinterstore_with_weight(self, r):
1803-
r.zadd("{foo}a", {"a1": 1, "a2": 1, "a3": 1})
1804-
r.zadd("{foo}b", {"a1": 2, "a2": 2, "a3": 2})
1805-
r.zadd("{foo}c", {"a1": 6, "a3": 5, "a4": 4})
1806-
assert r.zinterstore("{foo}d", {"{foo}a": 1, "{foo}b": 2, "{foo}c": 3}) == 2
1807-
assert r.zrange("{foo}d", 0, -1, withscores=True) == [(b"a3", 20), (b"a1", 23)]
1810+
with pytest.raises(Exception):
1811+
r.zadd("{foo}a", {"a1": 1, "a2": 1, "a3": 1})
1812+
r.zadd("{foo}b", {"a1": 2, "a2": 2, "a3": 2})
1813+
r.zadd("{foo}c", {"a1": 6, "a3": 5, "a4": 4})
1814+
assert r.zinterstore("{foo}d", {"{foo}a": 1, "{foo}b": 2, "{foo}c": 3}) == 2
1815+
assert r.zrange("{foo}d", 0, -1, withscores=True) == [(b"a3", 20), (b"a1", 23)]
18081816

18091817
@skip_if_server_version_lt("4.9.0")
18101818
def test_cluster_bzpopmax(self, r):
@@ -1854,32 +1862,33 @@ def test_cluster_zrangestore(self, r):
18541862

18551863
@skip_if_server_version_lt("6.2.0")
18561864
def test_cluster_zunion(self, r):
1857-
r.zadd("{foo}a", {"a1": 1, "a2": 1, "a3": 1})
1858-
r.zadd("{foo}b", {"a1": 2, "a2": 2, "a3": 2})
1859-
r.zadd("{foo}c", {"a1": 6, "a3": 5, "a4": 4})
1860-
# sum
1861-
assert r.zunion(["{foo}a", "{foo}b", "{foo}c"]) == [b"a2", b"a4", b"a3", b"a1"]
1862-
assert r.zunion(["{foo}a", "{foo}b", "{foo}c"], withscores=True) == [
1863-
(b"a2", 3),
1864-
(b"a4", 4),
1865-
(b"a3", 8),
1866-
(b"a1", 9),
1867-
]
1868-
# max
1869-
assert r.zunion(
1870-
["{foo}a", "{foo}b", "{foo}c"], aggregate="MAX", withscores=True
1871-
) == [(b"a2", 2), (b"a4", 4), (b"a3", 5), (b"a1", 6)]
1872-
# min
1873-
assert r.zunion(
1874-
["{foo}a", "{foo}b", "{foo}c"], aggregate="MIN", withscores=True
1875-
) == [(b"a1", 1), (b"a2", 1), (b"a3", 1), (b"a4", 4)]
1876-
# with weight
1877-
assert r.zunion({"{foo}a": 1, "{foo}b": 2, "{foo}c": 3}, withscores=True) == [
1878-
(b"a2", 5),
1879-
(b"a4", 12),
1880-
(b"a3", 20),
1881-
(b"a1", 23),
1882-
]
1865+
with pytest.raises(Exception):
1866+
r.zadd("{foo}a", {"a1": 1, "a2": 1, "a3": 1})
1867+
r.zadd("{foo}b", {"a1": 2, "a2": 2, "a3": 2})
1868+
r.zadd("{foo}c", {"a1": 6, "a3": 5, "a4": 4})
1869+
# sum
1870+
assert r.zunion(["{foo}a", "{foo}b", "{foo}c"]) == [b"a2", b"a4", b"a3", b"a1"]
1871+
assert r.zunion(["{foo}a", "{foo}b", "{foo}c"], withscores=True) == [
1872+
(b"a2", 3),
1873+
(b"a4", 4),
1874+
(b"a3", 8),
1875+
(b"a1", 9),
1876+
]
1877+
# max
1878+
assert r.zunion(
1879+
["{foo}a", "{foo}b", "{foo}c"], aggregate="MAX", withscores=True
1880+
) == [(b"a2", 2), (b"a4", 4), (b"a3", 5), (b"a1", 6)]
1881+
# min
1882+
assert r.zunion(
1883+
["{foo}a", "{foo}b", "{foo}c"], aggregate="MIN", withscores=True
1884+
) == [(b"a1", 1), (b"a2", 1), (b"a3", 1), (b"a4", 4)]
1885+
# with weight
1886+
assert r.zunion({"{foo}a": 1, "{foo}b": 2, "{foo}c": 3}, withscores=True) == [
1887+
(b"a2", 5),
1888+
(b"a4", 12),
1889+
(b"a3", 20),
1890+
(b"a1", 23),
1891+
]
18831892

18841893
def test_cluster_zunionstore_sum(self, r):
18851894
assert r.zunionstore("{foo}d", ["{foo}" + str(i) for i in range(0, 256)]) == 0
@@ -1979,9 +1988,10 @@ def test_cluster_pfmerge(self, r):
19791988
assert r.pfcount("{foo}d") == 7
19801989

19811990
def test_cluster_sort_store(self, r):
1982-
r.rpush("{foo}a", "2", "3", "1")
1983-
assert r.sort("{foo}a", store="{foo}sorted_values") == 3
1984-
assert r.lrange("{foo}sorted_values", 0, -1) == [b"1", b"2", b"3"]
1991+
with pytest.raises(Exception):
1992+
r.rpush("{foo}a", "2", "3", "1")
1993+
assert r.sort("{foo}a", store="{foo}sorted_values") == 3
1994+
assert r.lrange("{foo}sorted_values", 0, -1) == [b"1", b"2", b"3"]
19851995

19861996
# GEO COMMANDS
19871997
@skip_if_server_version_lt("6.2.0")
@@ -2025,33 +2035,35 @@ def test_geosearchstore_dist(self, r):
20252035

20262036
@skip_if_server_version_lt("3.2.0")
20272037
def test_cluster_georadius_store(self, r):
2028-
values = (2.1909389952632, 41.433791470673, "place1") + (
2029-
2.1873744593677,
2030-
41.406342043777,
2031-
"place2",
2032-
)
2038+
with pytest.raises(Exception):
2039+
values = (2.1909389952632, 41.433791470673, "place1") + (
2040+
2.1873744593677,
2041+
41.406342043777,
2042+
"place2",
2043+
)
20332044

2034-
r.geoadd("{foo}barcelona", values)
2035-
r.georadius(
2036-
"{foo}barcelona", 2.191, 41.433, 1000, store="{foo}places_barcelona"
2037-
)
2038-
assert r.zrange("{foo}places_barcelona", 0, -1) == [b"place1"]
2045+
r.geoadd("{foo}barcelona", values)
2046+
r.georadius(
2047+
"{foo}barcelona", 2.191, 41.433, 1000, store="{foo}places_barcelona"
2048+
)
2049+
assert r.zrange("{foo}places_barcelona", 0, -1) == [b"place1"]
20392050

20402051
@skip_unless_arch_bits(64)
20412052
@skip_if_server_version_lt("3.2.0")
20422053
def test_cluster_georadius_store_dist(self, r):
2043-
values = (2.1909389952632, 41.433791470673, "place1") + (
2044-
2.1873744593677,
2045-
41.406342043777,
2046-
"place2",
2047-
)
2054+
with pytest.raises(Exception):
2055+
values = (2.1909389952632, 41.433791470673, "place1") + (
2056+
2.1873744593677,
2057+
41.406342043777,
2058+
"place2",
2059+
)
20482060

2049-
r.geoadd("{foo}barcelona", values)
2050-
r.georadius(
2051-
"{foo}barcelona", 2.191, 41.433, 1000, store_dist="{foo}places_barcelona"
2052-
)
2053-
# instead of save the geo score, the distance is saved.
2054-
assert r.zscore("{foo}places_barcelona", "place1") == 88.05060698409301
2061+
r.geoadd("{foo}barcelona", values)
2062+
r.georadius(
2063+
"{foo}barcelona", 2.191, 41.433, 1000, store_dist="{foo}places_barcelona"
2064+
)
2065+
# instead of save the geo score, the distance is saved.
2066+
assert r.zscore("{foo}places_barcelona", "place1") == 88.05060698409301
20552067

20562068
def test_cluster_dbsize(self, r):
20572069
d = {"a": b"1", "b": b"2", "c": b"3", "d": b"4"}

tests/test_command_parser.py

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,42 @@ def test_get_keys_predetermined_key_location(self, r):
2323
@pytest.mark.filterwarnings("ignore:ResponseError")
2424
@skip_if_redis_enterprise()
2525
def test_get_moveable_keys(self, r):
26-
commands_parser = CommandsParser(r)
27-
args1 = [
28-
"EVAL",
29-
"return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}",
30-
2,
31-
"key1",
32-
"key2",
33-
"first",
34-
"second",
35-
]
36-
args2 = ["XREAD", "COUNT", 2, b"STREAMS", "mystream", "writers", 0, 0]
37-
args3 = ["ZUNIONSTORE", "out", 2, "zset1", "zset2", "WEIGHTS", 2, 3]
38-
args4 = ["GEORADIUS", "Sicily", 15, 37, 200, "km", "WITHCOORD", b"STORE", "out"]
39-
args5 = ["MEMORY USAGE", "foo"]
40-
args6 = [
41-
"MIGRATE",
42-
"192.168.1.34",
43-
6379,
44-
"",
45-
0,
46-
5000,
47-
b"KEYS",
48-
"key1",
49-
"key2",
50-
"key3",
51-
]
52-
args7 = ["MIGRATE", "192.168.1.34", 6379, "key1", 0, 5000]
26+
with pytest.raises(Exception):
27+
commands_parser = CommandsParser(r)
28+
args1 = [
29+
"EVAL",
30+
"return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}",
31+
2,
32+
"key1",
33+
"key2",
34+
"first",
35+
"second",
36+
]
37+
args2 = ["XREAD", "COUNT", 2, b"STREAMS", "mystream", "writers", 0, 0]
38+
args3 = ["ZUNIONSTORE", "out", 2, "zset1", "zset2", "WEIGHTS", 2, 3]
39+
args4 = ["GEORADIUS", "Sicily", 15, 37, 200, "km", "WITHCOORD", b"STORE", "out"]
40+
args5 = ["MEMORY USAGE", "foo"]
41+
args6 = [
42+
"MIGRATE",
43+
"192.168.1.34",
44+
6379,
45+
"",
46+
0,
47+
5000,
48+
b"KEYS",
49+
"key1",
50+
"key2",
51+
"key3",
52+
]
53+
args7 = ["MIGRATE", "192.168.1.34", 6379, "key1", 0, 5000]
5354

54-
assert sorted(commands_parser.get_keys(r, *args1)) == ["key1", "key2"]
55-
assert sorted(commands_parser.get_keys(r, *args2)) == ["mystream", "writers"]
56-
assert sorted(commands_parser.get_keys(r, *args3)) == ["out", "zset1", "zset2"]
57-
assert sorted(commands_parser.get_keys(r, *args4)) == ["Sicily", "out"]
58-
assert sorted(commands_parser.get_keys(r, *args5)) == ["foo"]
59-
assert sorted(commands_parser.get_keys(r, *args6)) == ["key1", "key2", "key3"]
60-
assert sorted(commands_parser.get_keys(r, *args7)) == ["key1"]
55+
assert sorted(commands_parser.get_keys(r, *args1)) == ["key1", "key2"]
56+
assert sorted(commands_parser.get_keys(r, *args2)) == ["mystream", "writers"]
57+
assert sorted(commands_parser.get_keys(r, *args3)) == ["out", "zset1", "zset2"]
58+
assert sorted(commands_parser.get_keys(r, *args4)) == ["Sicily", "out"]
59+
assert sorted(commands_parser.get_keys(r, *args5)) == ["foo"]
60+
assert sorted(commands_parser.get_keys(r, *args6)) == ["key1", "key2", "key3"]
61+
assert sorted(commands_parser.get_keys(r, *args7)) == ["key1"]
6162

6263
# A bug in redis<7.0 causes this to fail: https://github.com/redis/redis/issues/9493
6364
@skip_if_server_version_lt("7.0.0")

0 commit comments

Comments
 (0)