| 1 |
import re |
| 2 |
import time |
| 3 |
import pytest |
| 4 |
from unittest.mock import patch |
| 5 |
|
| 6 |
from iredis.utils import timer, strip_quote_args |
| 7 |
from iredis.commands import split_command_args, split_unknown_args |
| 8 |
from iredis.utils import ( |
| 9 |
command_syntax, |
| 10 |
parse_argument_to_formatted_text, |
| 11 |
compose_command_syntax, |
| 12 |
) |
| 13 |
from iredis.style import STYLE |
| 14 |
from iredis.exceptions import InvalidArguments, AmbiguousCommand |
| 15 |
from iredis.commands import commands_summary |
| 16 |
from prompt_toolkit import print_formatted_text |
| 17 |
|
| 18 |
|
| 19 |
def test_timer(): |
| 20 |
with patch("iredis.utils.logger") as mock_logger: |
| 21 |
timer("foo") |
| 22 |
time.sleep(0.1) |
| 23 |
timer("bar") |
| 24 |
mock_logger.debug.assert_called() |
| 25 |
args, kwargs = mock_logger.debug.call_args |
| 26 |
matched = re.match(r"\[timer (\d)\] (0\.\d+) -> bar", args[0]) |
| 27 |
|
| 28 |
assert matched.group(1) == str(3) |
| 29 |
assert 0.1 <= float(matched.group(2)) <= 0.2 |
| 30 |
|
| 31 |
# --- test again --- |
| 32 |
timer("foo") |
| 33 |
time.sleep(0.2) |
| 34 |
timer("bar") |
| 35 |
mock_logger.debug.assert_called() |
| 36 |
args, kwargs = mock_logger.debug.call_args |
| 37 |
matched = re.match(r"\[timer (\d)\] (0\.\d+) -> bar", args[0]) |
| 38 |
|
| 39 |
assert matched.group(1) == str(5) |
| 40 |
assert 0.2 <= float(matched.group(2)) <= 0.3 |
| 41 |
|
| 42 |
|
| 43 |
@pytest.mark.parametrize( |
| 44 |
"test_input,expected", |
| 45 |
[ |
| 46 |
("hello world", ["hello", "world"]), |
| 47 |
("'hello world'", ["hello world"]), |
| 48 |
('''hello"world"''', ["helloworld"]), |
| 49 |
(r'''hello\"world"''', [r"hello\world"]), |
| 50 |
(r'"\\"', [r"\\"]), |
| 51 |
(r"\\", [r"\\"]), |
| 52 |
(r"\abcd ef", [r"\abcd", "ef"]), |
| 53 |
# quotes in quotes |
| 54 |
(r""" 'hello"world' """, ['hello"world']), |
| 55 |
(r""" "hello'world" """, ["hello'world"]), |
| 56 |
(r""" 'hello\'world'""", ["hello'world"]), |
| 57 |
(r""" "hello\"world" """, ['hello"world']), |
| 58 |
(r"''", [""]), # set foo "" is a legal command |
| 59 |
(r'""', [""]), # set foo "" is a legal command |
| 60 |
(r"\\", ["\\\\"]), # backslash are legal |
| 61 |
("\\hello\\", ["\\hello\\"]), # backslash are legal |
| 62 |
('foo "bar\\n1"', ["foo", "bar\n1"]), |
| 63 |
], |
| 64 |
) |
| 65 |
def test_stripe_quote_escape_in_quote(test_input, expected): |
| 66 |
assert list(strip_quote_args(test_input)) == expected |
| 67 |
|
| 68 |
|
| 69 |
@pytest.mark.parametrize( |
| 70 |
"command,expected,args", |
| 71 |
[ |
| 72 |
("GET a", "GET", ["a"]), |
| 73 |
("cluster info", "cluster info", []), |
| 74 |
("getbit foo 17", "getbit", ["foo", "17"]), |
| 75 |
("command ", "command", []), |
| 76 |
(" command count ", "command count", []), |
| 77 |
(" command count ", "command count", []), # command with multi space |
| 78 |
(" command count ' hello world'", "command count", [" hello world"]), |
| 79 |
("set foo 'hello world'", "set", ["foo", "hello world"]), |
| 80 |
], |
| 81 |
) |
| 82 |
def test_split_commands(command, expected, args): |
| 83 |
parsed_command, parsed_args = split_command_args(command) |
| 84 |
assert expected == parsed_command |
| 85 |
assert args == parsed_args |
| 86 |
|
| 87 |
|
| 88 |
def test_split_commands_fail_on_unknown_command(): |
| 89 |
with pytest.raises(InvalidArguments): |
| 90 |
split_command_args("FOO BAR") |
| 91 |
|
| 92 |
|
| 93 |
@pytest.mark.parametrize( |
| 94 |
"command", |
| 95 |
["command in", "command in", "Command in", "COMMAND in"], |
| 96 |
) |
| 97 |
def test_split_commands_fail_on_partially_input(command): |
| 98 |
with pytest.raises(AmbiguousCommand): |
| 99 |
split_command_args(command) |
| 100 |
|
| 101 |
|
| 102 |
def test_split_commands_fail_on_unfinished_command(): |
| 103 |
with pytest.raises(InvalidArguments): |
| 104 |
split_command_args("setn") |
| 105 |
|
| 106 |
|
| 107 |
def test_render_bottom_with_command_json(): |
| 108 |
for command, info in commands_summary.items(): |
| 109 |
print_formatted_text(command_syntax(command, info), style=STYLE) |
| 110 |
|
| 111 |
|
| 112 |
@pytest.mark.parametrize( |
| 113 |
"raw,command,args", |
| 114 |
[ |
| 115 |
("abc 123", "abc", ["123"]), |
| 116 |
("abc", "abc", []), |
| 117 |
("abc foo bar", "abc", ["foo", "bar"]), |
| 118 |
("abc 'foo bar'", "abc", ["foo bar"]), |
| 119 |
('abc "foo bar"', "abc", ["foo bar"]), |
| 120 |
('abc "foo bar" 3 hello', "abc", ["foo bar", "3", "hello"]), |
| 121 |
('abc "foo \nbar"', "abc", ["foo \nbar"]), |
| 122 |
], |
| 123 |
) |
| 124 |
def test_split_unknown_commands(raw, command, args): |
| 125 |
parsed_command, parsed_args = split_unknown_args(raw) |
| 126 |
assert command == parsed_command |
| 127 |
assert args == parsed_args |
| 128 |
|
| 129 |
|
| 130 |
class TestParseArgumentToFormattedText: |
| 131 |
"""Tests for parse_argument_to_formatted_text function.""" |
| 132 |
|
| 133 |
def test_simple_argument_without_token(self): |
| 134 |
"""Test argument without token.""" |
| 135 |
result = parse_argument_to_formatted_text("cursor", "integer", False) |
| 136 |
assert len(result) == 1 |
| 137 |
assert result[0][1] == " cursor" |
| 138 |
|
| 139 |
def test_optional_argument_without_token(self): |
| 140 |
"""Test optional argument without token.""" |
| 141 |
result = parse_argument_to_formatted_text("key", "key", True) |
| 142 |
assert len(result) == 1 |
| 143 |
assert result[0][1] == " [key]" |
| 144 |
|
| 145 |
def test_argument_with_token(self): |
| 146 |
"""Test argument with token (like MATCH pattern).""" |
| 147 |
result = parse_argument_to_formatted_text( |
| 148 |
"pattern", "pattern", False, token="MATCH" |
| 149 |
) |
| 150 |
assert len(result) == 1 |
| 151 |
assert result[0][1] == " MATCH pattern" |
| 152 |
|
| 153 |
def test_optional_argument_with_token(self): |
| 154 |
"""Test optional argument with token (like [MATCH pattern]).""" |
| 155 |
result = parse_argument_to_formatted_text( |
| 156 |
"pattern", "pattern", True, token="MATCH" |
| 157 |
) |
| 158 |
assert len(result) == 1 |
| 159 |
assert result[0][1] == " [MATCH pattern]" |
| 160 |
|
| 161 |
def test_multiple_tokens(self): |
| 162 |
"""Test multiple arguments with different tokens.""" |
| 163 |
result1 = parse_argument_to_formatted_text( |
| 164 |
"pattern", "pattern", True, token="MATCH" |
| 165 |
) |
| 166 |
result2 = parse_argument_to_formatted_text( |
| 167 |
"count", "integer", True, token="COUNT" |
| 168 |
) |
| 169 |
result3 = parse_argument_to_formatted_text("type", "string", True, token="TYPE") |
| 170 |
|
| 171 |
assert result1[0][1] == " [MATCH pattern]" |
| 172 |
assert result2[0][1] == " [COUNT count]" |
| 173 |
assert result3[0][1] == " [TYPE type]" |
| 174 |
|
| 175 |
|
| 176 |
class TestComposeCommandSyntax: |
| 177 |
"""Tests for compose_command_syntax function.""" |
| 178 |
|
| 179 |
def test_scan_command_syntax(self): |
| 180 |
"""Test SCAN command shows correct syntax with tokens.""" |
| 181 |
scan_info = { |
| 182 |
"arguments": [ |
| 183 |
{"name": "cursor", "type": "integer"}, |
| 184 |
{ |
| 185 |
"name": "pattern", |
| 186 |
"type": "pattern", |
| 187 |
"token": "MATCH", |
| 188 |
"optional": True, |
| 189 |
}, |
| 190 |
{ |
| 191 |
"name": "count", |
| 192 |
"type": "integer", |
| 193 |
"token": "COUNT", |
| 194 |
"optional": True, |
| 195 |
}, |
| 196 |
{"name": "type", "type": "string", "token": "TYPE", "optional": True}, |
| 197 |
] |
| 198 |
} |
| 199 |
result = compose_command_syntax(scan_info) |
| 200 |
text = "".join([t[1] for t in result]) |
| 201 |
|
| 202 |
assert "cursor" in text |
| 203 |
assert "[MATCH pattern]" in text |
| 204 |
assert "[COUNT count]" in text |
| 205 |
assert "[TYPE type]" in text |
| 206 |
|
| 207 |
def test_hscan_command_syntax(self): |
| 208 |
"""Test HSCAN command shows correct syntax with tokens.""" |
| 209 |
hscan_info = { |
| 210 |
"arguments": [ |
| 211 |
{"name": "key", "type": "key"}, |
| 212 |
{"name": "cursor", "type": "integer"}, |
| 213 |
{ |
| 214 |
"name": "pattern", |
| 215 |
"type": "pattern", |
| 216 |
"token": "MATCH", |
| 217 |
"optional": True, |
| 218 |
}, |
| 219 |
{ |
| 220 |
"name": "count", |
| 221 |
"type": "integer", |
| 222 |
"token": "COUNT", |
| 223 |
"optional": True, |
| 224 |
}, |
| 225 |
] |
| 226 |
} |
| 227 |
result = compose_command_syntax(hscan_info) |
| 228 |
text = "".join([t[1] for t in result]) |
| 229 |
|
| 230 |
assert "key" in text |
| 231 |
assert "cursor" in text |
| 232 |
assert "[MATCH pattern]" in text |
| 233 |
assert "[COUNT count]" in text |
| 234 |
|
| 235 |
def test_command_without_tokens(self): |
| 236 |
"""Test command without any tokens.""" |
| 237 |
get_info = { |
| 238 |
"arguments": [ |
| 239 |
{"name": "key", "type": "key"}, |
| 240 |
] |
| 241 |
} |
| 242 |
result = compose_command_syntax(get_info) |
| 243 |
text = "".join([t[1] for t in result]) |
| 244 |
|
| 245 |
assert "key" in text |
| 246 |
assert "MATCH" not in text |
| 247 |
|
| 248 |
def test_real_scan_command_from_commands_summary(self): |
| 249 |
"""Test real SCAN command from commands_summary.""" |
| 250 |
scan_info = commands_summary["SCAN"] |
| 251 |
result = compose_command_syntax(scan_info) |
| 252 |
text = "".join([t[1] for t in result]) |
| 253 |
|
| 254 |
assert "[MATCH pattern]" in text |
| 255 |
assert "[COUNT count]" in text |
| 256 |
assert "[TYPE type]" in text |