1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
import pytest
import redis
import redis.client
from test import testtools
def test_hstrlen_missing(r: redis.Redis):
assert r.hstrlen("foo", "doesnotexist") == 0
r.hset("foo", "key", "value")
assert r.hstrlen("foo", "doesnotexist") == 0
def test_hstrlen(r: redis.Redis):
r.hset("foo", "key", "value")
assert r.hstrlen("foo", "key") == 5
def test_hset_then_hget(r: redis.Redis):
assert r.hset("foo", "key", "value") == 1
assert r.hget("foo", "key") == b"value"
def test_hset_update(r: redis.Redis):
assert r.hset("foo", "key", "value") == 1
assert r.hset("foo", "key", "value") == 0
def test_hset_wrong_type(r: redis.Redis):
r.zadd("foo", {"bar": 1})
with pytest.raises(redis.ResponseError):
r.hset("foo", "key", "value")
def test_hgetall(r: redis.Redis):
assert r.hset("foo", "k1", "v1") == 1
assert r.hset("foo", "k2", "v2") == 1
assert r.hset("foo", "k3", "v3") == 1
assert r.hgetall("foo") == {b"k1": b"v1", b"k2": b"v2", b"k3": b"v3"}
def test_hgetall_empty_key(r: redis.Redis):
assert r.hgetall("foo") == {}
def test_hgetall_wrong_type(r: redis.Redis):
r.zadd("foo", {"bar": 1})
with pytest.raises(redis.ResponseError):
r.hgetall("foo")
def test_hexists(r: redis.Redis):
r.hset("foo", "bar", "v1")
assert r.hexists("foo", "bar") == 1
assert r.hexists("foo", "baz") == 0
assert r.hexists("bar", "bar") == 0
def test_hexists_wrong_type(r: redis.Redis):
r.zadd("foo", {"bar": 1})
with pytest.raises(redis.ResponseError):
r.hexists("foo", "key")
def test_hkeys(r: redis.Redis):
r.hset("foo", "k1", "v1")
r.hset("foo", "k2", "v2")
assert set(r.hkeys("foo")) == {b"k1", b"k2"}
assert set(r.hkeys("bar")) == set()
def test_hkeys_wrong_type(r: redis.Redis):
r.zadd("foo", {"bar": 1})
with pytest.raises(redis.ResponseError):
r.hkeys("foo")
def test_hlen(r: redis.Redis):
r.hset("foo", "k1", "v1")
r.hset("foo", "k2", "v2")
assert r.hlen("foo") == 2
def test_hlen_wrong_type(r: redis.Redis):
r.zadd("foo", {"bar": 1})
with pytest.raises(redis.ResponseError):
r.hlen("foo")
def test_hvals(r: redis.Redis):
r.hset("foo", "k1", "v1")
r.hset("foo", "k2", "v2")
assert set(r.hvals("foo")) == {b"v1", b"v2"}
assert set(r.hvals("bar")) == set()
def test_hvals_wrong_type(r: redis.Redis):
r.zadd("foo", {"bar": 1})
with pytest.raises(redis.ResponseError):
r.hvals("foo")
def test_hmget(r: redis.Redis):
r.hset("foo", "k1", "v1")
r.hset("foo", "k2", "v2")
r.hset("foo", "k3", "v3")
# Normal case.
assert r.hmget("foo", ["k1", "k3"]) == [b"v1", b"v3"]
assert r.hmget("foo", "k1", "k3") == [b"v1", b"v3"]
# Key does not exist.
assert r.hmget("bar", ["k1", "k3"]) == [None, None]
assert r.hmget("bar", "k1", "k3") == [None, None]
# Some keys in the hash do not exist.
assert r.hmget("foo", ["k1", "k500"]) == [b"v1", None]
assert r.hmget("foo", "k1", "k500") == [b"v1", None]
def test_hmget_wrong_type(r: redis.Redis):
r.zadd("foo", {"bar": 1})
with pytest.raises(redis.ResponseError):
r.hmget("foo", "key1", "key2")
def test_hdel(r: redis.Redis):
r.hset("foo", "k1", "v1")
r.hset("foo", "k2", "v2")
r.hset("foo", "k3", "v3")
assert r.hget("foo", "k1") == b"v1"
assert r.hdel("foo", "k1") == 1
assert r.hget("foo", "k1") is None
assert r.hdel("foo", "k1") == 0
# Since redis>=2.7.6 returns number of deleted items.
assert r.hdel("foo", "k2", "k3") == 2
assert r.hget("foo", "k2") is None
assert r.hget("foo", "k3") is None
assert r.hdel("foo", "k2", "k3") == 0
def test_hdel_wrong_type(r: redis.Redis):
r.zadd("foo", {"bar": 1})
with pytest.raises(redis.ResponseError):
r.hdel("foo", "key")
def test_hincrby(r: redis.Redis):
r.hset("foo", "counter", 0)
assert r.hincrby("foo", "counter") == 1
assert r.hincrby("foo", "counter") == 2
assert r.hincrby("foo", "counter") == 3
def test_hincrby_with_no_starting_value(r: redis.Redis):
assert r.hincrby("foo", "counter") == 1
assert r.hincrby("foo", "counter") == 2
assert r.hincrby("foo", "counter") == 3
def test_hincrby_with_range_param(r: redis.Redis):
assert r.hincrby("foo", "counter", 2) == 2
assert r.hincrby("foo", "counter", 2) == 4
assert r.hincrby("foo", "counter", 2) == 6
def test_hincrby_wrong_type(r: redis.Redis):
r.zadd("foo", {"bar": 1})
with pytest.raises(redis.ResponseError):
r.hincrby("foo", "key", 2)
def test_hincrbyfloat(r: redis.Redis):
r.hset("foo", "counter", 0.0)
assert r.hincrbyfloat("foo", "counter") == 1.0
assert r.hincrbyfloat("foo", "counter") == 2.0
assert r.hincrbyfloat("foo", "counter") == 3.0
def test_hincrbyfloat_with_no_starting_value(r: redis.Redis):
assert r.hincrbyfloat("foo", "counter") == 1.0
assert r.hincrbyfloat("foo", "counter") == 2.0
assert r.hincrbyfloat("foo", "counter") == 3.0
def test_hincrbyfloat_with_range_param(r: redis.Redis):
assert r.hincrbyfloat("foo", "counter", 0.1) == pytest.approx(0.1)
assert r.hincrbyfloat("foo", "counter", 0.1) == pytest.approx(0.2)
assert r.hincrbyfloat("foo", "counter", 0.1) == pytest.approx(0.3)
def test_hincrbyfloat_on_non_float_value_raises_error(r: redis.Redis):
r.hset("foo", "counter", "cat")
with pytest.raises(redis.ResponseError):
r.hincrbyfloat("foo", "counter")
def test_hincrbyfloat_with_non_float_amount_raises_error(r: redis.Redis):
with pytest.raises(redis.ResponseError):
r.hincrbyfloat("foo", "counter", "cat")
def test_hincrbyfloat_wrong_type(r: redis.Redis):
r.zadd("foo", {"bar": 1})
with pytest.raises(redis.ResponseError):
r.hincrbyfloat("foo", "key", 0.1)
def test_hincrbyfloat_precision(r: redis.Redis):
x = 1.23456789123456789
assert r.hincrbyfloat("foo", "bar", x) == x
assert float(r.hget("foo", "bar")) == x
def test_hsetnx(r: redis.Redis):
assert r.hsetnx("foo", "newkey", "v1") == 1
assert r.hsetnx("foo", "newkey", "v1") == 0
assert r.hget("foo", "newkey") == b"v1"
def test_hmset_empty_raises_error(r: redis.Redis):
with pytest.raises(redis.DataError):
r.hmset("foo", {})
@testtools.run_test_if_redispy_ver("lte", "4.6")
def test_hmset_redispy4(r: redis.Redis):
r.hset("foo", "k1", "v1")
assert r.hmset("foo", {"k2": "v2", "k3": "v3"}) is True
def test_hmset_wrong_type(r: redis.Redis):
r.zadd("foo", {"bar": 1})
with pytest.raises(redis.ResponseError):
r.hmset("foo", {"key": "value"})
def test_empty_hash(r: redis.Redis):
r.hset("foo", "bar", "baz")
r.hdel("foo", "bar")
assert not r.exists("foo")
def test_hset_removing_last_field_delete_key(r: redis.Redis):
r.hset(b"3L", b"f1", b"v1")
r.hdel(b"3L", b"f1")
assert r.keys("*") == []
@pytest.mark.min_server("7.4")
@testtools.run_test_if_redispy_ver("gte", "5")
def test_hscan_no_values(r: redis.Redis):
name = "hscan-test"
for ix in range(20):
k = "key:%s" % ix
v = "result:%s" % ix
r.hset(name, k, v)
expected = r.hgetall(name)
assert len(expected) == 20 # Ensure we know what we're testing
# Test that we page through the results and get everything out
results = set()
cursor = "0"
while cursor != 0:
cursor, data = r.hscan(name, cursor, count=6, no_values=True)
results.update(data)
assert results == set(expected.keys())
def test_hscan(r: redis.Redis):
# Set up the data
name = "hscan-test"
for ix in range(20):
k = "key:%s" % ix
v = "result:%s" % ix
r.hset(name, k, v)
expected = r.hgetall(name)
assert len(expected) == 20 # Ensure we know what we're testing
# Test that we page through the results and get everything out
results = {}
cursor = "0"
while cursor != 0:
cursor, data = r.hscan(name, cursor, count=6)
results.update(data)
assert expected == results
# Test the iterator version
results = {}
for key, val in r.hscan_iter(name, count=6):
results[key] = val
assert expected == results
# Now test that the MATCH functionality works
results = {}
cursor = "0"
while cursor != 0:
cursor, data = r.hscan(name, cursor, match="*7", count=100)
results.update(data)
assert b"key:7" in results
assert b"key:17" in results
assert len(results) == 2
# Test the match on iterator
results = {}
for key, val in r.hscan_iter(name, match="*7"):
results[key] = val
assert b"key:7" in results
assert b"key:17" in results
assert len(results) == 2
def test_hrandfield(r: redis.Redis):
assert r.hrandfield("key") is None
hash = {b"a": 1, b"b": 2, b"c": 3, b"d": 4, b"e": 5}
r.hset("key", mapping=hash)
assert r.hrandfield("key") is not None
assert len(r.hrandfield("key", 0)) == 0
res = r.hrandfield("key", 2)
assert len(res) == 2
assert res[0] in set(hash.keys())
assert res[1] in set(hash.keys())
# with values
res = r.hrandfield("key", 2, True)
assert len(res) == 4
assert res[0] in set(hash.keys())
assert res[1] in {str(x).encode() for x in hash.values()}
assert res[2] in set(hash.keys())
assert res[3] in {str(x).encode() for x in hash.values()}
# without duplications
assert len(r.hrandfield("key", 10)) == 5
# with duplications
assert len(r.hrandfield("key", -10)) == 10
with pytest.raises(redis.ResponseError):
testtools.raw_command(r, "HRANDFIELD", "key", 3, "WITHVALUES", 3)
|