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
|
import re
import time
import pytest
from unittest.mock import patch
from iredis.utils import timer, strip_quote_args
from iredis.commands import split_command_args, split_unknown_args
from iredis.utils import command_syntax
from iredis.style import STYLE
from iredis.exceptions import InvalidArguments, AmbiguousCommand
from iredis.commands import commands_summary
from prompt_toolkit import print_formatted_text
def test_timer():
with patch("iredis.utils.logger") as mock_logger:
timer("foo")
time.sleep(0.1)
timer("bar")
mock_logger.debug.assert_called()
args, kwargs = mock_logger.debug.call_args
matched = re.match(r"\[timer (\d)\] (0\.\d+) -> bar", args[0])
assert matched.group(1) == str(3)
assert 0.1 <= float(matched.group(2)) <= 0.2
# --- test again ---
timer("foo")
time.sleep(0.2)
timer("bar")
mock_logger.debug.assert_called()
args, kwargs = mock_logger.debug.call_args
matched = re.match(r"\[timer (\d)\] (0\.\d+) -> bar", args[0])
assert matched.group(1) == str(5)
assert 0.2 <= float(matched.group(2)) <= 0.3
@pytest.mark.parametrize(
"test_input,expected",
[
("hello world", ["hello", "world"]),
("'hello world'", ["hello world"]),
('''hello"world"''', ["helloworld"]),
(r'''hello\"world"''', [r"hello\world"]),
(r'"\\"', [r"\\"]),
(r"\\", [r"\\"]),
(r"\abcd ef", [r"\abcd", "ef"]),
# quotes in quotes
(r""" 'hello"world' """, ['hello"world']),
(r""" "hello'world" """, ["hello'world"]),
(r""" 'hello\'world'""", ["hello'world"]),
(r""" "hello\"world" """, ['hello"world']),
(r"''", [""]), # set foo "" is a legal command
(r'""', [""]), # set foo "" is a legal command
(r"\\", ["\\\\"]), # backslash are legal
("\\hello\\", ["\\hello\\"]), # backslash are legal
],
)
def test_stripe_quote_escape_in_quote(test_input, expected):
assert list(strip_quote_args(test_input)) == expected
@pytest.mark.parametrize(
"command,expected,args",
[
("GET a", "GET", ["a"]),
("cluster info", "cluster info", []),
("getbit foo 17", "getbit", ["foo", "17"]),
("command ", "command", []),
(" command count ", "command count", []),
(" command count ", "command count", []), # command with multi space
(" command count ' hello world'", "command count", [" hello world"]),
("set foo 'hello world'", "set", ["foo", "hello world"]),
],
)
def test_split_commands(command, expected, args):
parsed_command, parsed_args = split_command_args(command)
assert expected == parsed_command
assert args == parsed_args
def test_split_commands_fail_on_unknown_command():
with pytest.raises(InvalidArguments):
split_command_args("FOO BAR")
@pytest.mark.parametrize(
"command",
["command in", "command in", "Command in", "COMMAND in"],
)
def test_split_commands_fail_on_partially_input(command):
with pytest.raises(AmbiguousCommand):
split_command_args(command)
def test_split_commands_fail_on_unfinished_command():
with pytest.raises(InvalidArguments):
split_command_args("setn")
def test_render_bottom_with_command_json():
for command, info in commands_summary.items():
print_formatted_text(command_syntax(command, info), style=STYLE)
@pytest.mark.parametrize(
"raw,command,args",
[
("abc 123", "abc", ["123"]),
("abc", "abc", []),
("abc foo bar", "abc", ["foo", "bar"]),
("abc 'foo bar'", "abc", ["foo bar"]),
('abc "foo bar"', "abc", ["foo bar"]),
('abc "foo bar" 3 hello', "abc", ["foo bar", "3", "hello"]),
('abc "foo \nbar"', "abc", ["foo \nbar"]),
],
)
def test_split_unknown_commands(raw, command, args):
parsed_command, parsed_args = split_unknown_args(raw)
assert command == parsed_command
assert args == parsed_args
|