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
|
import pytest
import tempfile
from unittest.mock import patch
from prompt_toolkit.formatted_text import FormattedText
from iredis.entry import (
gather_args,
parse_url,
SkipAuthFileHistory,
write_result,
is_too_tall,
)
from iredis.utils import DSN
@pytest.mark.parametrize(
"is_tty,raw_arg_is_raw,final_config_is_raw",
[
(True, None, False),
(True, True, True),
(True, False, False),
(False, None, True),
(False, True, True),
(False, False, True), # not tty
],
)
def test_command_entry_tty(is_tty, raw_arg_is_raw, final_config_is_raw, config):
# is tty + raw -> raw
with patch("sys.stdout.isatty") as patch_tty:
patch_tty.return_value = is_tty
if raw_arg_is_raw is None:
call = ["iredis"]
elif raw_arg_is_raw is True:
call = ["iredis", "--raw"]
elif raw_arg_is_raw is False:
call = ["iredis", "--no-raw"]
else:
raise Exception()
gather_args.main(call, standalone_mode=False)
assert config.raw == final_config_is_raw
def test_disable_pager():
from iredis.config import config
gather_args.main(["iredis", "--decode", "utf-8"], standalone_mode=False)
assert config.enable_pager
gather_args.main(["iredis", "--no-pager"], standalone_mode=False)
assert not config.enable_pager
def test_command_with_decode_utf_8():
from iredis.config import config
gather_args.main(["iredis", "--decode", "utf-8"], standalone_mode=False)
assert config.decode == "utf-8"
gather_args.main(["iredis"], standalone_mode=False)
assert config.decode == ""
def test_command_with_shell_pipeline():
from iredis.config import config
gather_args.main(["iredis", "--no-shell"], standalone_mode=False)
assert config.shell is False
gather_args.main(["iredis"], standalone_mode=False)
assert config.shell is True
def test_command_shell_options_higher_priority():
from iredis.config import config
from textwrap import dedent
config_content = dedent(
"""
[main]
shell = False
"""
)
with open("/tmp/iredisrc", "w+") as etc_config:
etc_config.write(config_content)
gather_args.main(["iredis", "--iredisrc", "/tmp/iredisrc"], standalone_mode=False)
assert config.shell is False
gather_args.main(
["iredis", "--shell", "--iredisrc", "/tmp/iredisrc"], standalone_mode=False
)
assert config.shell is True
@pytest.mark.parametrize(
"url,dsn",
[
(
"redis://localhost:6379/3",
DSN(
scheme="redis",
host="localhost",
port=6379,
path=None,
db=3,
username=None,
password=None,
),
),
(
"redis://localhost:6379",
DSN(
scheme="redis",
host="localhost",
port=6379,
path=None,
db=0,
username=None,
password=None,
),
),
(
"rediss://localhost:6379",
DSN(
scheme="rediss",
host="localhost",
port=6379,
path=None,
db=0,
username=None,
password=None,
),
),
(
"redis://username:password@localhost:6379",
DSN(
scheme="redis",
host="localhost",
port=6379,
path=None,
db=0,
username="username",
password="password",
),
),
(
"redis://:password@localhost:6379",
DSN(
scheme="redis",
host="localhost",
port=6379,
path=None,
db=0,
username=None,
password="password",
),
),
(
"redis://username@localhost:12345",
DSN(
scheme="redis",
host="localhost",
port=12345,
path=None,
db=0,
username="username",
password=None,
),
),
(
# query string won't work for redis://
"redis://username@localhost:6379?db=2",
DSN(
scheme="redis",
host="localhost",
port=6379,
path=None,
db=0,
username="username",
password=None,
),
),
(
"unix://username:password2@/tmp/to/socket.sock?db=0",
DSN(
scheme="unix",
host=None,
port=None,
path="/tmp/to/socket.sock",
db=0,
username="username",
password="password2",
),
),
(
"unix://:password3@/path/to/socket.sock",
DSN(
scheme="unix",
host=None,
port=None,
path="/path/to/socket.sock",
db=0,
username=None,
password="password3",
),
),
(
"unix:///tmp/socket.sock",
DSN(
scheme="unix",
host=None,
port=None,
path="/tmp/socket.sock",
db=0,
username=None,
password=None,
),
),
],
)
def test_parse_url(url, dsn):
assert parse_url(url) == dsn
@pytest.mark.parametrize(
"command,record",
[
("set foo bar", True),
("set auth bar", True),
("auth 123", False),
("AUTH hello", False),
("AUTH hello world", False),
],
)
def test_history(command, record):
f = tempfile.TemporaryFile("w+")
history = SkipAuthFileHistory(f.name)
assert history._loaded_strings == []
history.append_string(command)
assert (command in history._loaded_strings) is record
def test_write_result_for_str(capsys):
write_result("hello")
captured = capsys.readouterr()
assert captured.out == "hello\n"
def test_write_result_for_bytes(capsys):
write_result(b"hello")
captured = capsys.readouterr()
assert captured.out == "hello\n"
def test_write_result_for_formatted_text():
ft = FormattedText([("class:keyword", "set"), ("class:string", "hello world")])
# just this test not raise means ok
write_result(ft)
def test_is_too_tall_for_formatted_text():
ft = FormattedText([("class:key", f"key-{index}\n") for index in range(21)])
assert is_too_tall(ft, 20)
assert not is_too_tall(ft, 22)
def test_is_too_tall_for_bytes():
byte_text = b"".join([b"key\n" for index in range(21)])
assert is_too_tall(byte_text, 20)
assert not is_too_tall(byte_text, 23)
|