Debian Codegraph @

tests/cli_tests/test_dsn.py

raw
1 import os
2
3 import pexpect
4 from textwrap import dedent
5 import pytest
6
7
8 def test_using_dsn():
9 config_content = dedent(
10 """
11 [alias_dsn]
12 local = redis://localhost:6379/15
13 """
14 )
15 with open("/tmp/iredisrc", "w+") as etc_config:
16 etc_config.write(config_content)
17
18 cli = pexpect.spawn("iredis --iredisrc /tmp/iredisrc --dsn local", timeout=1)
19 cli.logfile_read = open("cli_test.log", "ab")
20 cli.expect(["iredis", "localhost:6379[15]>"])
21 cli.close()
22
23 # overwrite with -n
24 cli = pexpect.spawn("iredis --iredisrc /tmp/iredisrc --dsn local -n 3", timeout=1)
25 cli.logfile_read = open("cli_test.log", "ab")
26 cli.expect(["iredis", "localhost:6379[3]>"])
27 cli.close()
28
29 # dsn not exists
30 cli = pexpect.spawn("iredis --iredisrc /tmp/iredisrc --dsn ghost-dsn", timeout=1)
31 cli.expect(["Could not find the specified DSN in the config file."])
32 cli.close()
33 assert cli.status == 1
34
35
36 @pytest.mark.skipif(
37 not os.path.exists("/tmp/redis/redis.sock"), reason="unix socket is not found"
38 )
39 def test_using_dsn_unix():
40 config_content = dedent(
41 """
42 [alias_dsn]
43 unix = unix:///tmp/redis/redis.sock?db=3
44 """
45 )
46 with open("/tmp/iredisrc", "w+") as etc_config:
47 etc_config.write(config_content)
48
49 cli = pexpect.spawn("iredis --iredisrc /tmp/iredisrc --dsn unix", timeout=2)
50 cli.logfile_read = open("cli_test.log", "ab")
51 cli.expect(["iredis", "redis /tmp/redis/redis.sock[3]>"])
52
53 cli.close()