Debian Codegraph @

tests/unittests/test_entry.py

raw
1 import pytest
2 import tempfile
3 from unittest.mock import patch
4 from prompt_toolkit.formatted_text import FormattedText
5
6 from iredis.entry import (
7 gather_args,
8 parse_url,
9 SkipAuthFileHistory,
10 write_result,
11 is_too_tall,
12 )
13
14 from iredis.utils import DSN
15
16
17 @pytest.mark.parametrize(
18 "is_tty,raw_arg_is_raw,final_config_is_raw",
19 [
20 (True, None, False),
21 (True, True, True),
22 (True, False, False),
23 (False, None, True),
24 (False, True, True),
25 (False, False, True), # not tty
26 ],
27 )
28 def test_command_entry_tty(is_tty, raw_arg_is_raw, final_config_is_raw, config):
29 # is tty + raw -> raw
30 with patch("sys.stdout.isatty") as patch_tty:
31 patch_tty.return_value = is_tty
32 if raw_arg_is_raw is None:
33 call = ["iredis"]
34 elif raw_arg_is_raw is True:
35 call = ["iredis", "--raw"]
36 elif raw_arg_is_raw is False:
37 call = ["iredis", "--no-raw"]
38 else:
39 raise Exception()
40 gather_args.main(call, standalone_mode=False)
41 assert config.raw == final_config_is_raw
42
43
44 def test_disable_pager():
45 from iredis.config import config
46
47 gather_args.main(["iredis", "--decode", "utf-8"], standalone_mode=False)
48 assert config.enable_pager
49
50 gather_args.main(["iredis", "--no-pager"], standalone_mode=False)
51 assert not config.enable_pager
52
53
54 def test_command_with_decode_utf_8():
55 from iredis.config import config
56
57 gather_args.main(["iredis", "--decode", "utf-8"], standalone_mode=False)
58 assert config.decode == "utf-8"
59
60 gather_args.main(["iredis"], standalone_mode=False)
61 assert config.decode == ""
62
63
64 def test_command_with_shell_pipeline():
65 from iredis.config import config
66
67 gather_args.main(["iredis", "--no-shell"], standalone_mode=False)
68 assert config.shell is False
69
70 gather_args.main(["iredis"], standalone_mode=False)
71 assert config.shell is True
72
73
74 def test_command_shell_options_higher_priority():
75 from iredis.config import config
76 from textwrap import dedent
77
78 config_content = dedent(
79 """
80 [main]
81 shell = False
82 """
83 )
84 with open("/tmp/iredisrc", "w+") as etc_config:
85 etc_config.write(config_content)
86
87 gather_args.main(["iredis", "--iredisrc", "/tmp/iredisrc"], standalone_mode=False)
88 assert config.shell is False
89
90 gather_args.main(
91 ["iredis", "--shell", "--iredisrc", "/tmp/iredisrc"], standalone_mode=False
92 )
93 assert config.shell is True
94
95
96 @pytest.mark.parametrize(
97 "url,dsn",
98 [
99 (
100 "redis://localhost:6379/3",
101 DSN(
102 scheme="redis",
103 host="localhost",
104 port=6379,
105 path=None,
106 db=3,
107 username=None,
108 password=None,
109 verify_ssl=None,
110 ),
111 ),
112 (
113 "redis://localhost:6379",
114 DSN(
115 scheme="redis",
116 host="localhost",
117 port=6379,
118 path=None,
119 db=0,
120 username=None,
121 password=None,
122 verify_ssl=None,
123 ),
124 ),
125 (
126 "rediss://localhost:6379",
127 DSN(
128 scheme="rediss",
129 host="localhost",
130 port=6379,
131 path=None,
132 db=0,
133 username=None,
134 password=None,
135 verify_ssl=None,
136 ),
137 ),
138 (
139 "rediss://localhost:6379/1?ssl_cert_reqs=optional",
140 DSN(
141 scheme="rediss",
142 host="localhost",
143 port=6379,
144 path=None,
145 db=1,
146 username=None,
147 password=None,
148 verify_ssl="optional",
149 ),
150 ),
151 (
152 "redis://username:password@localhost:6379",
153 DSN(
154 scheme="redis",
155 host="localhost",
156 port=6379,
157 path=None,
158 db=0,
159 username="username",
160 password="password",
161 verify_ssl=None,
162 ),
163 ),
164 (
165 "redis://:password@localhost:6379",
166 DSN(
167 scheme="redis",
168 host="localhost",
169 port=6379,
170 path=None,
171 db=0,
172 username=None,
173 password="password",
174 verify_ssl=None,
175 ),
176 ),
177 (
178 "redis://username:pass@word@localhost:12345/2",
179 DSN(
180 scheme="redis",
181 host="localhost",
182 port=12345,
183 path=None,
184 db=2,
185 username="username",
186 password="pass@word",
187 verify_ssl=None,
188 ),
189 ),
190 (
191 "redis://username@localhost:12345",
192 DSN(
193 scheme="redis",
194 host="localhost",
195 port=12345,
196 path=None,
197 db=0,
198 username="username",
199 password=None,
200 verify_ssl=None,
201 ),
202 ),
203 (
204 # query string won't work for redis://
205 "redis://username@localhost:6379?db=2",
206 DSN(
207 scheme="redis",
208 host="localhost",
209 port=6379,
210 path=None,
211 db=0,
212 username="username",
213 password=None,
214 verify_ssl=None,
215 ),
216 ),
217 (
218 "unix://username:password2@/tmp/to/socket.sock?db=0",
219 DSN(
220 scheme="unix",
221 host=None,
222 port=None,
223 path="/tmp/to/socket.sock",
224 db=0,
225 username="username",
226 password="password2",
227 verify_ssl=None,
228 ),
229 ),
230 (
231 "unix://:password3@/path/to/socket.sock",
232 DSN(
233 scheme="unix",
234 host=None,
235 port=None,
236 path="/path/to/socket.sock",
237 db=0,
238 username=None,
239 password="password3",
240 verify_ssl=None,
241 ),
242 ),
243 (
244 "unix:///tmp/socket.sock",
245 DSN(
246 scheme="unix",
247 host=None,
248 port=None,
249 path="/tmp/socket.sock",
250 db=0,
251 username=None,
252 password=None,
253 verify_ssl=None,
254 ),
255 ),
256 ],
257 )
258 def test_parse_url(url, dsn):
259 assert parse_url(url) == dsn
260
261
262 @pytest.mark.parametrize(
263 "command,record",
264 [
265 ("set foo bar", True),
266 ("set auth bar", True),
267 ("auth 123", False),
268 ("AUTH hello", False),
269 ("AUTH hello world", False),
270 ],
271 )
272 def test_history(command, record):
273 f = tempfile.TemporaryFile("w+")
274 history = SkipAuthFileHistory(f.name)
275 assert history._loaded_strings == []
276 history.append_string(command)
277 assert (command in history._loaded_strings) is record
278
279
280 def test_write_result_for_str(capsys):
281 write_result("hello")
282 captured = capsys.readouterr()
283 assert captured.out == "hello\n"
284
285
286 def test_write_result_for_bytes(capsys):
287 write_result(b"hello")
288 captured = capsys.readouterr()
289 assert captured.out == "hello\n"
290
291
292 def test_write_result_for_formatted_text():
293 ft = FormattedText([("class:keyword", "set"), ("class:string", "hello world")])
294 # just this test not raise means ok
295 write_result(ft)
296
297
298 def test_is_too_tall_for_formatted_text():
299 ft = FormattedText([("class:key", f"key-{index}\n") for index in range(21)])
300 assert is_too_tall(ft, 20)
301 assert not is_too_tall(ft, 22)
302
303
304 def test_is_too_tall_for_bytes():
305 byte_text = b"".join([b"key\n" for index in range(21)])
306 assert is_too_tall(byte_text, 20)
307 assert not is_too_tall(byte_text, 23)
308
309
310 def test_natmap_parsed_into_config():
311 from iredis.config import config
312
313 gather_args.main(
314 [
315 "iredis",
316 "--natmap",
317 "node1.example.com:6379:127.0.0.1:6371,node2.example.com:6379:127.0.0.1:6372",
318 ],
319 standalone_mode=False,
320 )
321 assert config.natmap == {
322 "node1.example.com:6379": ("127.0.0.1", 6371),
323 "node2.example.com:6379": ("127.0.0.1", 6372),
324 }