diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/cli_tests/__init__.py | 0 | ||||
-rw-r--r-- | tests/cli_tests/test_cli_start.py | 8 | ||||
-rw-r--r-- | tests/cli_tests/test_command_input.py | 21 | ||||
-rw-r--r-- | tests/cli_tests/test_shell_pipeline.py | 10 | ||||
-rw-r--r-- | tests/helpers.py | 11 | ||||
-rw-r--r-- | tests/unittests/__init__.py | 0 | ||||
-rw-r--r-- | tests/unittests/command_parse/test_cluster.py | 36 | ||||
-rw-r--r-- | tests/unittests/test_client.py | 102 | ||||
-rw-r--r-- | tests/unittests/test_render_functions.py | 6 | ||||
-rw-r--r-- | tests/unittests/test_utils.py | 6 |
10 files changed, 144 insertions, 56 deletions
diff --git a/tests/cli_tests/__init__.py b/tests/cli_tests/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/cli_tests/__init__.py diff --git a/tests/cli_tests/test_cli_start.py b/tests/cli_tests/test_cli_start.py index d33185a..00fcbf7 100644 --- a/tests/cli_tests/test_cli_start.py +++ b/tests/cli_tests/test_cli_start.py @@ -10,6 +10,14 @@ def test_start_on_connection_error(): cli.close() +def test_start_with_client_name(): + cli = pexpect.spawn("iredis --client_name custom_name", timeout=2) + cli.expect("iredis") + cli.sendline("CLIENT GETNAME") + cli.expect("custom_name") + cli.close() + + def test_short_help_option(config): c = pexpect.spawn("iredis -h", timeout=2) diff --git a/tests/cli_tests/test_command_input.py b/tests/cli_tests/test_command_input.py index 4f970f5..f70ee3c 100644 --- a/tests/cli_tests/test_command_input.py +++ b/tests/cli_tests/test_command_input.py @@ -1,3 +1,4 @@ +import os import pytest @@ -8,8 +9,13 @@ def test_wrong_select_db_index(cli): cli.sendline("select 128") cli.expect(["DB index is out of range", "127.0.0.1:6379[1]>"]) + if int(os.environ["REDIS_VERSION"]) > 5: + text = "value is not an integer or out of range" + else: + text = "invalid DB index" + cli.sendline("select abc") - cli.expect(["invalid DB index", "127.0.0.1:6379[1]>"]) + cli.expect([text, "127.0.0.1:6379[1]>"]) cli.sendline("select 15") cli.expect("OK") @@ -53,3 +59,16 @@ def test_auth_hidden_password(clean_redis, cli): def test_hello_command_is_not_supported(cli): cli.sendline("hello 3") cli.expect("IRedis currently not support RESP3") + + +def test_abort_reading_connection(cli): + cli.sendline("blpop mylist 30") + cli.send(chr(3)) + cli.expect( + r"KeyboardInterrupt received! User canceled reading response!", timeout=10 + ) + + cli.sendline("set foo bar") + cli.expect("OK") + cli.sendline("get foo") + cli.expect("bar") diff --git a/tests/cli_tests/test_shell_pipeline.py b/tests/cli_tests/test_shell_pipeline.py index 4bacf8d..8fe5e14 100644 --- a/tests/cli_tests/test_shell_pipeline.py +++ b/tests/cli_tests/test_shell_pipeline.py @@ -9,3 +9,13 @@ def test_running_disable_shell_pipeline(): cli.sendline("get foo | grep w") cli.expect(r"hello") cli.close() + + +def test_running_disable_shell_pipeline_with_decode_option(): + cli = pexpect.spawn("iredis -n 15 --decode=utf-8", timeout=2) + cli.expect("127.0.0.1") + cli.sendline("set foo hello") + cli.expect("OK") + cli.sendline("get foo | cat") + cli.expect(r"hello") + cli.close() diff --git a/tests/helpers.py b/tests/helpers.py new file mode 100644 index 0000000..8cf2069 --- /dev/null +++ b/tests/helpers.py @@ -0,0 +1,11 @@ +import re + + +def formatted_text_rematch(value_to_test, expected_formatted_text): + """ + ``expected_formatted_text`` can be regex. + """ + for value, expected in zip(value_to_test, expected_formatted_text): + assert value[0] == expected[0] + print(expected[1], value[1]) + assert re.match(expected[1], value[1]) diff --git a/tests/unittests/__init__.py b/tests/unittests/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/unittests/__init__.py diff --git a/tests/unittests/command_parse/test_cluster.py b/tests/unittests/command_parse/test_cluster.py index e60fe92..6dd877f 100644 --- a/tests/unittests/command_parse/test_cluster.py +++ b/tests/unittests/command_parse/test_cluster.py @@ -26,9 +26,15 @@ def test_command_cluster_count_failure_reports(judge_command): ) judge_command("cluster count-failure-reports 1 2 3 4", None) judge_command("cluster count-failure-reports 1 a", None) - judge_command("cluster count-failure-reports a", None) + judge_command( + "cluster count-failure-reports a", + {"command": "cluster count-failure-reports", "node": "a"}, + ) judge_command("cluster count-failure-reports a 2", None) - judge_command("cluster count-failure-reports abc", None) + judge_command( + "cluster count-failure-reports abc", + {"command": "cluster count-failure-reports", "node": "abc"}, + ) def test_command_cluster_countkeysinslot(judge_command): @@ -88,9 +94,22 @@ def test_command_cluster_forget(judge_command): ) judge_command("cluster forget 1 2 3 4", None) judge_command("cluster forget 1 a", None) - judge_command("cluster forget a", None) + judge_command("cluster forget a", {"command": "cluster forget", "node": "a"}) judge_command("cluster forget a 2", None) - judge_command("cluster forget abc", None) + judge_command( + "cluster forget abc", + { + "command": "cluster forget", + "node": "abc", + }, + ) + judge_command( + "cluster forget 07c37dfeb235213a872192d90877d0cd55635b91", + { + "command": "cluster forget", + "node": "07c37dfeb235213a872192d90877d0cd55635b91", + }, + ) def test_command_cluster_getkeysinslot(judge_command): @@ -209,6 +228,15 @@ def test_command_cluster_set_slot(judge_command): }, ) judge_command( + "cluster setslot 123 node e7d1eecce10fd6bb5eb35b9f99a514335d9ba9ca", + { + "command": "cluster setslot", + "slot": "123", + "slotsubcmd": "node", + "node": "e7d1eecce10fd6bb5eb35b9f99a514335d9ba9ca", + }, + ) + judge_command( "cluster setslot 123 MIGRATING 123123", { "command": "cluster setslot", diff --git a/tests/unittests/test_client.py b/tests/unittests/test_client.py index e78da17..5887579 100644 --- a/tests/unittests/test_client.py +++ b/tests/unittests/test_client.py @@ -11,6 +11,7 @@ from iredis.config import config, load_config_files from iredis.completers import IRedisCompleter from iredis.entry import Rainbow, prompt_message from iredis.exceptions import NotSupport +from ..helpers import formatted_text_rematch @pytest.fixture @@ -258,19 +259,15 @@ def test_peek_string(iredis_client, clean_redis): clean_redis.set("foo", "bar") peek_result = list(iredis_client.do_peek("foo")) - assert peek_result == [ - FormattedText( - [ - ("class:dockey", "key: "), - ("", "string (embstr) mem: 50 bytes, ttl: -1"), - ("", "\n"), - ("class:dockey", "strlen: "), - ("", "3"), - ("", "\n"), - ("class:dockey", "value: "), - ("", '"bar"'), - ] - ) + assert peek_result[0][0] == ("class:dockey", "key: ") + assert re.match(r"string \(embstr\) mem: \d+ bytes, ttl: -1", peek_result[0][1][1]) + assert peek_result[0][2:] == [ + ("", "\n"), + ("class:dockey", "strlen: "), + ("", "3"), + ("", "\n"), + ("class:dockey", "value: "), + ("", '"bar"'), ] @@ -278,39 +275,40 @@ def test_peek_list_fetch_all(iredis_client, clean_redis): clean_redis.lpush("mylist", *[f"hello-{index}" for index in range(5)]) peek_result = list(iredis_client.do_peek("mylist")) - assert peek_result == [ + formatted_text_rematch( + peek_result[0], FormattedText( [ ("class:dockey", "key: "), - ("", "list (quicklist) mem: 176 bytes, ttl: -1"), + ("", r"list \(quicklist\) mem: \d+ bytes, ttl: -1"), ("", "\n"), ("class:dockey", "llen: "), ("", "5"), ("", "\n"), ("class:dockey", "elements: "), ("", "\n"), - ("", "1)"), + ("", r"1\)"), ("", " "), ("class:string", '"hello-4"'), ("", "\n"), - ("", "2)"), + ("", r"2\)"), ("", " "), ("class:string", '"hello-3"'), ("", "\n"), - ("", "3)"), + ("", r"3\)"), ("", " "), ("class:string", '"hello-2"'), ("", "\n"), - ("", "4)"), + ("", r"4\)"), ("", " "), ("class:string", '"hello-1"'), ("", "\n"), - ("", "5)"), + ("", r"5\)"), ("", " "), ("class:string", '"hello-0"'), ] - ) - ] + ), + ) def test_peek_list_fetch_part(iredis_client, clean_redis): @@ -338,18 +336,21 @@ def test_peek_zset_fetch_all(iredis_client, clean_redis): "myzset", dict(zip([f"hello-{index}" for index in range(3)], range(3))) ) peek_result = list(iredis_client.do_peek("myzset")) - assert peek_result[0][0:9] == FormattedText( - [ - ("class:dockey", "key: "), - ("", "zset (ziplist) mem: 92 bytes, ttl: -1"), - ("", "\n"), - ("class:dockey", "zcount: "), - ("", "3"), - ("", "\n"), - ("class:dockey", "members: "), - ("", "\n"), - ("", "1)"), - ] + formatted_text_rematch( + peek_result[0][0:9], + FormattedText( + [ + ("class:dockey", "key: "), + ("", r"zset \(ziplist\) mem: \d+ bytes, ttl: -1"), + ("", "\n"), + ("class:dockey", "zcount: "), + ("", "3"), + ("", "\n"), + ("class:dockey", "members: "), + ("", "\n"), + ("", r"1\)"), + ] + ), ) @@ -358,17 +359,20 @@ def test_peek_zset_fetch_part(iredis_client, clean_redis): "myzset", dict(zip([f"hello-{index}" for index in range(40)], range(40))) ) peek_result = list(iredis_client.do_peek("myzset")) - assert peek_result[0][0:8] == FormattedText( - [ - ("class:dockey", "key: "), - ("", "zset (ziplist) mem: 556 bytes, ttl: -1"), - ("", "\n"), - ("class:dockey", "zcount: "), - ("", "40"), - ("", "\n"), - ("class:dockey", "members (first 40): "), - ("", "\n"), - ] + formatted_text_rematch( + peek_result[0][0:8], + FormattedText( + [ + ("class:dockey", "key: "), + ("", r"zset \(ziplist\) mem: \d+ bytes, ttl: -1"), + ("", "\n"), + ("class:dockey", "zcount: "), + ("", "40"), + ("", "\n"), + ("class:dockey", r"members \(first 40\): "), + ("", "\n"), + ] + ), ) @@ -395,10 +399,12 @@ def test_peek_stream(iredis_client, clean_redis): clean_redis.xadd("mystream", {"foo": "bar", "hello": "world"}) peek_result = list(iredis_client.do_peek("mystream")) - assert peek_result[0][0:18] == FormattedText( + assert peek_result[0][0] == ("class:dockey", "key: ") + assert re.match( + r"stream \((stream|unknown)\) mem: 6\d\d bytes, ttl: -1", peek_result[0][1][1] + ) + assert peek_result[0][2:18] == FormattedText( [ - ("class:dockey", "key: "), - ("", "stream (unknown) mem: 601 bytes, ttl: -1"), ("", "\n"), ("class:dockey", "XINFO: "), ("", "\n"), diff --git a/tests/unittests/test_render_functions.py b/tests/unittests/test_render_functions.py index 2da9b35..30b328e 100644 --- a/tests/unittests/test_render_functions.py +++ b/tests/unittests/test_render_functions.py @@ -484,3 +484,9 @@ def test_render_bytes(config): def test_render_bytes_raw(config): assert renders.OutputRender.render_raw(b"bytes\n") == b"bytes\n" + + +def test_render_help(config): + assert renders.OutputRender.render_help([b"foo", b"bar"]) == FormattedText( + [("class:string", "foo\nbar")] + ) diff --git a/tests/unittests/test_utils.py b/tests/unittests/test_utils.py index c9b5eff..98ea8db 100644 --- a/tests/unittests/test_utils.py +++ b/tests/unittests/test_utils.py @@ -53,11 +53,11 @@ def test_timer(): (r""" "hello\"world" """, ['hello"world']), (r"''", [""]), # set foo "" is a legal command (r'""', [""]), # set foo "" is a legal command - (r"\\", ["\\\\"]), # blackslash are legal - ("\\hello\\", ["\\hello\\"]), # blackslash are legal + (r"\\", ["\\\\"]), # backslash are legal + ("\\hello\\", ["\\hello\\"]), # backslash are legal ], ) -def test_stipe_quote_escaple_in_quote(test_input, expected): +def test_stripe_quote_escape_in_quote(test_input, expected): assert list(strip_quote_args(test_input)) == expected |