File: test_connection.py

package info (click to toggle)
python-fakeredis 2.29.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,772 kB
  • sloc: python: 19,002; sh: 8; makefile: 5
file content (544 lines) | stat: -rw-r--r-- 16,068 bytes parent folder | download
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
import pytest
import redis
import redis.client
from redis.exceptions import ResponseError

from fakeredis import _msgs as msgs
from test import testtools
from test.testtools import raw_command


def test_ping(r: redis.Redis):
    assert r.ping()
    assert testtools.raw_command(r, "ping", "test") == b"test"
    with pytest.raises(redis.ResponseError, match=msgs.WRONG_ARGS_MSG6.format("ping")[4:]):
        raw_command(r, "ping", "arg1", "arg2")


def test_echo(r: redis.Redis):
    assert r.echo(b"hello") == b"hello"
    assert r.echo("hello") == b"hello"


def test_unknown_command(r: redis.Redis):
    with pytest.raises(redis.ResponseError):
        raw_command(r, "0 3 3")


@testtools.fake_only
def test_time(r, mocker):
    fake_time = mocker.patch("time.time")
    fake_time.return_value = 1234567890.1234567
    assert r.time() == (1234567890, 123457)
    fake_time.return_value = 1234567890.000001
    assert r.time() == (1234567890, 1)
    fake_time.return_value = 1234567890.9999999
    assert r.time() == (1234567891, 0)


@pytest.mark.min_server("7")
def test_hello(r: redis.Redis):
    client_info = r.client_info()
    protocol = int(client_info.get("resp"))
    if protocol == 2:
        return
    assert r.hello() == {
        "server": "fakeredis",
        "version": "1.0.0",
        "proto": 2,
        "id": 1,
    }


@pytest.mark.min_server("7")
def test_client_list(r: redis.Redis):
    client_info = r.client_info()
    client_id = client_info["id"]
    client_list = r.client_list()
    assert isinstance(client_list, list)
    assert len(client_list) >= 1
    assert isinstance(client_list[0], dict)
    client_ids = [int(client["id"]) for client in client_list]
    assert client_id in client_ids

    client_list = r.client_list()
    assert isinstance(client_list, list)
    client_ids = [int(client["id"]) for client in client_list]
    non_existing_client_id = max(client_ids) + 1
    client_list = r.client_list(client_id=[str(non_existing_client_id)])
    assert isinstance(client_list, list)
    assert len(client_list) == 0


@pytest.mark.min_server("7")
@testtools.run_test_if_redispy_ver("gte", "5")
def test_client_info(r: redis.Redis):
    client_info = r.client_info()
    assert client_info.get("lib-name", "redis-py") == "redis-py"
    r.client_setinfo(b"lib-name", b"fakeredis")
    r.client_setinfo(b"lib-ver", b"1.0.0")
    client_info = r.client_info()
    assert client_info["lib-name"] == "fakeredis"
    assert client_info["lib-ver"] == "1.0.0"


def test_client_id(r: redis.Redis):
    client_id = r.client_id()
    client_info = r.client_info()
    assert client_id == client_info["id"]


def test_client_setname(r: redis.Redis):
    assert r.client_setname("test") is True
    assert r.client_getname() == "test"


@pytest.mark.decode_responses
class TestDecodeResponses:
    def test_decode_str(self, r):
        r.set("foo", "bar")
        assert r.get("foo") == "bar"

    def test_decode_set(self, r):
        r.sadd("foo", "member1")
        assert set(r.smembers("foo")) == {"member1"}

    def test_decode_list(self, r):
        r.rpush("foo", "a", "b")
        assert r.lrange("foo", 0, -1) == ["a", "b"]

    def test_decode_dict(self, r):
        r.hset("foo", "key", "value")
        assert r.hgetall("foo") == {"key": "value"}

    def test_decode_error(self, r):
        r.set("foo", "bar")
        with pytest.raises(ResponseError) as exc_info:
            r.hset("foo", "bar", "baz")
        assert isinstance(exc_info.value.args[0], str)


@pytest.mark.disconnected
@testtools.fake_only
class TestFakeStrictRedisConnectionErrors:
    def test_flushdb(self, r):
        with pytest.raises(redis.ConnectionError):
            r.flushdb()

    def test_flushall(self, r):
        with pytest.raises(redis.ConnectionError):
            r.flushall()

    def test_append(self, r):
        with pytest.raises(redis.ConnectionError):
            r.append("key", "value")

    def test_bitcount(self, r):
        with pytest.raises(redis.ConnectionError):
            r.bitcount("key", 0, 20)

    def test_decr(self, r):
        with pytest.raises(redis.ConnectionError):
            r.decr("key", 2)

    def test_exists(self, r):
        with pytest.raises(redis.ConnectionError):
            r.exists("key")

    def test_expire(self, r):
        with pytest.raises(redis.ConnectionError):
            r.expire("key", 20)

    def test_pexpire(self, r):
        with pytest.raises(redis.ConnectionError):
            r.pexpire("key", 20)

    def test_echo(self, r):
        with pytest.raises(redis.ConnectionError):
            r.echo("value")

    def test_get(self, r):
        with pytest.raises(redis.ConnectionError):
            r.get("key")

    def test_getbit(self, r):
        with pytest.raises(redis.ConnectionError):
            r.getbit("key", 2)

    def test_getset(self, r):
        with pytest.raises(redis.ConnectionError):
            r.getset("key", "value")

    def test_incr(self, r):
        with pytest.raises(redis.ConnectionError):
            r.incr("key")

    def test_incrby(self, r):
        with pytest.raises(redis.ConnectionError):
            r.incrby("key")

    def test_ncrbyfloat(self, r):
        with pytest.raises(redis.ConnectionError):
            r.incrbyfloat("key")

    def test_keys(self, r):
        with pytest.raises(redis.ConnectionError):
            r.keys()

    def test_mget(self, r):
        with pytest.raises(redis.ConnectionError):
            r.mget(["key1", "key2"])

    def test_mset(self, r):
        with pytest.raises(redis.ConnectionError):
            r.mset({"key": "value"})

    def test_msetnx(self, r):
        with pytest.raises(redis.ConnectionError):
            r.msetnx({"key": "value"})

    def test_persist(self, r):
        with pytest.raises(redis.ConnectionError):
            r.persist("key")

    def test_rename(self, r):
        server = r.connection_pool.connection_kwargs["server"]
        server.connected = True
        r.set("key1", "value")
        server.connected = False
        with pytest.raises(redis.ConnectionError):
            r.rename("key1", "key2")
        server.connected = True
        assert r.exists("key1")

    def test_eval(self, r):
        with pytest.raises(redis.ConnectionError):
            r.eval("", 0)

    def test_lpush(self, r):
        with pytest.raises(redis.ConnectionError):
            r.lpush("name", 1, 2)

    def test_lrange(self, r):
        with pytest.raises(redis.ConnectionError):
            r.lrange("name", 1, 5)

    def test_llen(self, r):
        with pytest.raises(redis.ConnectionError):
            r.llen("name")

    def test_lrem(self, r):
        with pytest.raises(redis.ConnectionError):
            r.lrem("name", 2, 2)

    def test_rpush(self, r):
        with pytest.raises(redis.ConnectionError):
            r.rpush("name", 1)

    def test_lpop(self, r):
        with pytest.raises(redis.ConnectionError):
            r.lpop("name")

    def test_lset(self, r):
        with pytest.raises(redis.ConnectionError):
            r.lset("name", 1, 4)

    def test_rpushx(self, r):
        with pytest.raises(redis.ConnectionError):
            r.rpushx("name", 1)

    def test_ltrim(self, r):
        with pytest.raises(redis.ConnectionError):
            r.ltrim("name", 1, 4)

    def test_lindex(self, r):
        with pytest.raises(redis.ConnectionError):
            r.lindex("name", 1)

    def test_lpushx(self, r):
        with pytest.raises(redis.ConnectionError):
            r.lpushx("name", 1)

    def test_rpop(self, r):
        with pytest.raises(redis.ConnectionError):
            r.rpop("name")

    def test_linsert(self, r):
        with pytest.raises(redis.ConnectionError):
            r.linsert("name", "where", "refvalue", "value")

    def test_rpoplpush(self, r):
        with pytest.raises(redis.ConnectionError):
            r.rpoplpush("src", "dst")

    def test_blpop(self, r):
        with pytest.raises(redis.ConnectionError):
            r.blpop("keys")

    def test_brpop(self, r):
        with pytest.raises(redis.ConnectionError):
            r.brpop("keys")

    def test_brpoplpush(self, r):
        with pytest.raises(redis.ConnectionError):
            r.brpoplpush("src", "dst")

    def test_hdel(self, r):
        with pytest.raises(redis.ConnectionError):
            r.hdel("name")

    def test_hexists(self, r):
        with pytest.raises(redis.ConnectionError):
            r.hexists("name", "key")

    def test_hget(self, r):
        with pytest.raises(redis.ConnectionError):
            r.hget("name", "key")

    def test_hgetall(self, r):
        with pytest.raises(redis.ConnectionError):
            r.hgetall("name")

    def test_hincrby(self, r):
        with pytest.raises(redis.ConnectionError):
            r.hincrby("name", "key")

    def test_hincrbyfloat(self, r):
        with pytest.raises(redis.ConnectionError):
            r.hincrbyfloat("name", "key")

    def test_hkeys(self, r):
        with pytest.raises(redis.ConnectionError):
            r.hkeys("name")

    def test_hlen(self, r):
        with pytest.raises(redis.ConnectionError):
            r.hlen("name")

    def test_hset(self, r):
        with pytest.raises(redis.ConnectionError):
            r.hset("name", "key", 1)

    def test_hsetnx(self, r):
        with pytest.raises(redis.ConnectionError):
            r.hsetnx("name", "key", 2)

    def test_hmset(self, r):
        with pytest.raises(redis.ConnectionError):
            r.hmset("name", {"key": 1})

    def test_hmget(self, r):
        with pytest.raises(redis.ConnectionError):
            r.hmget("name", ["a", "b"])

    def test_hvals(self, r):
        with pytest.raises(redis.ConnectionError):
            r.hvals("name")

    def test_sadd(self, r):
        with pytest.raises(redis.ConnectionError):
            r.sadd("name", 1, 2)

    def test_scard(self, r):
        with pytest.raises(redis.ConnectionError):
            r.scard("name")

    def test_sdiff(self, r):
        with pytest.raises(redis.ConnectionError):
            r.sdiff(["a", "b"])

    def test_sdiffstore(self, r):
        with pytest.raises(redis.ConnectionError):
            r.sdiffstore("dest", ["a", "b"])

    def test_sinter(self, r):
        with pytest.raises(redis.ConnectionError):
            r.sinter(["a", "b"])

    def test_sinterstore(self, r):
        with pytest.raises(redis.ConnectionError):
            r.sinterstore("dest", ["a", "b"])

    def test_sismember(self, r):
        with pytest.raises(redis.ConnectionError):
            r.sismember("name", 20)

    def test_smembers(self, r):
        with pytest.raises(redis.ConnectionError):
            r.smembers("name")

    def test_smove(self, r):
        with pytest.raises(redis.ConnectionError):
            r.smove("src", "dest", 20)

    def test_spop(self, r):
        with pytest.raises(redis.ConnectionError):
            r.spop("name")

    def test_srandmember(self, r):
        with pytest.raises(redis.ConnectionError):
            r.srandmember("name")

    def test_srem(self, r):
        with pytest.raises(redis.ConnectionError):
            r.srem("name")

    def test_sunion(self, r):
        with pytest.raises(redis.ConnectionError):
            r.sunion(["a", "b"])

    def test_sunionstore(self, r):
        with pytest.raises(redis.ConnectionError):
            r.sunionstore("dest", ["a", "b"])

    def test_zadd(self, r):
        with pytest.raises(redis.ConnectionError):
            r.zadd("name", {"key": "value"})

    def test_zcard(self, r):
        with pytest.raises(redis.ConnectionError):
            r.zcard("name")

    def test_zcount(self, r):
        with pytest.raises(redis.ConnectionError):
            r.zcount("name", 1, 5)

    def test_zincrby(self, r):
        with pytest.raises(redis.ConnectionError):
            r.zincrby("name", 1, 1)

    def test_zinterstore(self, r):
        with pytest.raises(redis.ConnectionError):
            r.zinterstore("dest", ["a", "b"])

    def test_zrange(self, r):
        with pytest.raises(redis.ConnectionError):
            r.zrange("name", 1, 5)

    def test_zrangebyscore(self, r):
        with pytest.raises(redis.ConnectionError):
            r.zrangebyscore("name", 1, 5)

    def test_rangebylex(self, r):
        with pytest.raises(redis.ConnectionError):
            r.zrangebylex("name", 1, 4)

    def test_zrem(self, r):
        with pytest.raises(redis.ConnectionError):
            r.zrem("name", "value")

    def test_zremrangebyrank(self, r):
        with pytest.raises(redis.ConnectionError):
            r.zremrangebyrank("name", 1, 5)

    def test_zremrangebyscore(self, r):
        with pytest.raises(redis.ConnectionError):
            r.zremrangebyscore("name", 1, 5)

    def test_zremrangebylex(self, r):
        with pytest.raises(redis.ConnectionError):
            r.zremrangebylex("name", 1, 5)

    def test_zlexcount(self, r):
        with pytest.raises(redis.ConnectionError):
            r.zlexcount("name", 1, 5)

    def test_zrevrange(self, r):
        with pytest.raises(redis.ConnectionError):
            r.zrevrange("name", 1, 5, 1)

    def test_zrevrangebyscore(self, r):
        with pytest.raises(redis.ConnectionError):
            r.zrevrangebyscore("name", 5, 1)

    def test_zrevrangebylex(self, r):
        with pytest.raises(redis.ConnectionError):
            r.zrevrangebylex("name", 5, 1)

    def test_zrevran(self, r):
        with pytest.raises(redis.ConnectionError):
            r.zrevrank("name", 2)

    def test_zscore(self, r):
        with pytest.raises(redis.ConnectionError):
            r.zscore("name", 2)

    def test_zunionstor(self, r):
        with pytest.raises(redis.ConnectionError):
            r.zunionstore("dest", ["1", "2"])

    def test_pipeline(self, r):
        with pytest.raises(redis.ConnectionError):
            r.pipeline().watch("key")

    def test_transaction(self, r):
        with pytest.raises(redis.ConnectionError):

            def func(a):
                return a * a

            r.transaction(func, 3)

    def test_lock(self, r):
        with pytest.raises(redis.ConnectionError):
            with r.lock("name"):
                pass

    def test_pubsub(self, r):
        with pytest.raises(redis.ConnectionError):
            r.pubsub().subscribe("channel")

    def test_pfadd(self, r):
        with pytest.raises(redis.ConnectionError):
            r.pfadd("name", 1)

    def test_pfmerge(self, r):
        with pytest.raises(redis.ConnectionError):
            r.pfmerge("dest", "a", "b")

    def test_scan(self, r):
        with pytest.raises(redis.ConnectionError):
            list(r.scan())

    def test_sscan(self, r):
        with pytest.raises(redis.ConnectionError):
            r.sscan("name")

    def test_hscan(self, r):
        with pytest.raises(redis.ConnectionError):
            r.hscan("name")

    def test_scan_iter(self, r):
        with pytest.raises(redis.ConnectionError):
            list(r.scan_iter())

    def test_sscan_iter(self, r):
        with pytest.raises(redis.ConnectionError):
            list(r.sscan_iter("name"))

    def test_hscan_iter(self, r):
        with pytest.raises(redis.ConnectionError):
            list(r.hscan_iter("name"))


@pytest.mark.disconnected
@testtools.fake_only
class TestPubSubConnected:
    @pytest.fixture
    def pubsub(self, r):
        return r.pubsub()

    def test_basic_subscribe(self, pubsub):
        with pytest.raises(redis.ConnectionError):
            pubsub.subscribe("logs")

    def test_subscription_conn_lost(self, fake_server, pubsub):
        fake_server.connected = True
        pubsub.subscribe("logs")
        fake_server.connected = False
        # The initial message is already in the pipe
        msg = pubsub.get_message()
        check = {"type": "subscribe", "pattern": None, "channel": b"logs", "data": 1}
        assert msg == check, "Message was not published to channel"
        with pytest.raises(redis.ConnectionError):
            pubsub.get_message()